X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cadi%2Fcore%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Faaf%2Fcadi%2Futil%2Ftest%2FJU_Pool.java;h=230c6b3b26b68f4be09ce034e2e07b9b3f7979c2;hb=1296352d8eafee57f982a4342ad79ada4aa56d28;hp=792093228e990638fa4a9c639f1083838bb6d9e1;hpb=77ed20565d7089c3efeaef51afc4e087ea65a192;p=aaf%2Fauthz.git diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Pool.java b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Pool.java index 79209322..230c6b3b 100644 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Pool.java +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Pool.java @@ -6,9 +6,9 @@ * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * * You may obtain a copy of the License at - * * + * * * * http://www.apache.org/licenses/LICENSE-2.0 - * * + * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -18,6 +18,7 @@ * * * * ******************************************************************************/ + package org.onap.aaf.cadi.util.test; import static org.junit.Assert.*; @@ -33,127 +34,127 @@ import org.onap.aaf.cadi.util.Pool.*; public class JU_Pool { - private StringBuilder sb = new StringBuilder(); - - private class IntegerCreator implements Creator { - private int current = 0; - - @Override - public Integer create() { - return current++; - } - - @Override - public void destroy(Integer t) { - t = 0; - } - - @Override - public boolean isValid(Integer t) { - return (t & 0x1) == 0; - } - - @Override - public void reuse(Integer t) { - } - } - - private class CustomLogger implements Log { - @Override - public void log(Object... o) { - for (Object item : o) { - sb.append(item.toString()); - } - } - } - - @Test - public void getTest() throws CadiException { - Pool intPool = new Pool(new IntegerCreator()); - - List> gotten = new ArrayList>(); - for (int i = 0; i < 10; i++) { - gotten.add(intPool.get()); - assertThat(gotten.get(i).content, is(i)); - } - - gotten.get(9).done(); - gotten.set(9, intPool.get()); - assertThat(gotten.get(9).content, is(9)); - - for (int i = 0; i < 10; i++) { - gotten.get(i).done(); - } - - for (int i = 0; i < 10; i++) { - gotten.set(i, intPool.get()); - if (i < 5) { - assertThat(gotten.get(i).content, is(i)); - } else { - assertThat(gotten.get(i).content, is(i + 5)); - } - } - - for (int i = 0; i < 10; i++) { - gotten.get(i).toss(); - // Coverage calls - gotten.get(i).toss(); - gotten.get(i).done(); - - // only set some objects to null -> this is for the finalize coverage test - if (i < 5) { - gotten.set(i, null); - } - } - - // Coverage of finalize() - System.gc(); - } - - @Test - public void bulkTest() throws CadiException { - Pool intPool = new Pool(new IntegerCreator()); - - intPool.prime(10); - // Remove all of the invalid items (in this case, odd numbers) - assertFalse(intPool.validate()); - - // Make sure we got them all - assertTrue(intPool.validate()); - - // Get an item from the pool - Pooled gotten = intPool.get(); - assertThat(gotten.content, is(0)); - - // finalize that item, then check the next one to make sure we actually purged - // the odd numbers - gotten = intPool.get(); - assertThat(gotten.content, is(2)); - - intPool.drain(); - - } - - @Test - public void setMaxTest() { - Pool intPool = new Pool(new IntegerCreator()); - intPool.setMaxRange(10); - assertThat(intPool.getMaxRange(), is(10)); - intPool.setMaxRange(-10); - assertThat(intPool.getMaxRange(), is(0)); - } - - @Test - public void loggingTest() { - Pool intPool = new Pool(new IntegerCreator()); - - // Log to Log.NULL for coverage - intPool.log("Test log output"); - - intPool.setLogger(new CustomLogger()); - intPool.log("Test log output"); - - assertThat(sb.toString(), is("Test log output")); - } + private StringBuilder sb = new StringBuilder(); + + private class IntegerCreator implements Creator { + private int current = 0; + + @Override + public Integer create() { + return current++; + } + + @Override + public void destroy(Integer t) { + t = 0; + } + + @Override + public boolean isValid(Integer t) { + return (t & 0x1) == 0; + } + + @Override + public void reuse(Integer t) { + } + } + + private class CustomLogger implements Log { + @Override + public void log(Object... o) { + for (Object item : o) { + sb.append(item.toString()); + } + } + } + + @Test + public void getTest() throws CadiException { + Pool intPool = new Pool(new IntegerCreator()); + + List> gotten = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + gotten.add(intPool.get()); + assertThat(gotten.get(i).content, is(i)); + } + + gotten.get(9).done(); + gotten.set(9, intPool.get()); + assertThat(gotten.get(9).content, is(9)); + + for (int i = 0; i < 10; i++) { + gotten.get(i).done(); + } + + for (int i = 0; i < 10; i++) { + gotten.set(i, intPool.get()); + if (i < 5) { + assertThat(gotten.get(i).content, is(i)); + } else { + assertThat(gotten.get(i).content, is(i + 5)); + } + } + + for (int i = 0; i < 10; i++) { + gotten.get(i).toss(); + // Coverage calls + gotten.get(i).toss(); + gotten.get(i).done(); + + // only set some objects to null -> this is for the finalize coverage test + if (i < 5) { + gotten.set(i, null); + } + } + + // Coverage of finalize() + System.gc(); + } + + @Test + public void bulkTest() throws CadiException { + Pool intPool = new Pool(new IntegerCreator()); + + intPool.prime(10); + // Remove all of the invalid items (in this case, odd numbers) + assertFalse(intPool.validate()); + + // Make sure we got them all + assertTrue(intPool.validate()); + + // Get an item from the pool + Pooled gotten = intPool.get(); + assertThat(gotten.content, is(0)); + + // finalize that item, then check the next one to make sure we actually purged + // the odd numbers + gotten = intPool.get(); + assertThat(gotten.content, is(2)); + + intPool.drain(); + + } + + @Test + public void setMaxTest() { + Pool intPool = new Pool(new IntegerCreator()); + intPool.setMaxRange(10); + assertThat(intPool.getMaxRange(), is(10)); + intPool.setMaxRange(-10); + assertThat(intPool.getMaxRange(), is(0)); + } + + @Test + public void loggingTest() { + Pool intPool = new Pool(new IntegerCreator()); + + // Log to Log.NULL for coverage + intPool.log("Test log output"); + + intPool.setLogger(new CustomLogger()); + intPool.log("Test log output"); + + assertThat(sb.toString(), is("Test log output")); + } }