1 /*******************************************************************************
3 * * ===========================================================================
4 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
5 * * ===========================================================================
6 * * Licensed under the Apache License, Version 2.0 (the "License");
7 * * you may not use this file except in compliance with the License.
8 * * You may obtain a copy of the License at
10 * * http://www.apache.org/licenses/LICENSE-2.0
12 * * Unless required by applicable law or agreed to in writing, software
13 * * distributed under the License is distributed on an "AS IS" BASIS,
14 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * * See the License for the specific language governing permissions and
16 * * limitations under the License.
17 * * ============LICENSE_END====================================================
20 ******************************************************************************/
22 package org.onap.aaf.cadi.util.test;
24 import static org.junit.Assert.*;
26 import java.util.ArrayList;
27 import java.util.List;
29 import static org.hamcrest.CoreMatchers.*;
31 import org.onap.aaf.cadi.CadiException;
32 import org.onap.aaf.cadi.util.Log;
33 import org.onap.aaf.cadi.util.Pool;
34 import org.onap.aaf.cadi.util.Pool.*;
36 public class JU_Pool {
38 private class IntegerCreator implements Creator<Integer> {
39 private int current = 0;
42 public Integer create() {
47 public void destroy(Integer t) {
52 public boolean isValid(Integer t) {
53 return (t & 0x1) == 0;
57 public void reuse(Integer t) {
61 // Used for CustomLogger Testing
62 private StringBuilder sb = new StringBuilder();
64 private class CustomLogger implements Log {
66 public void log(Log.Type type, Object... o) {
67 for (Object item : o) {
68 sb.append(item.toString());
74 * Enter variable amount in this order
76 * count, used, max_range, max_objects
80 private void check(Pool<Integer> intPool, int ... ints) {
81 String rpt = intPool.toString();
82 // Fallthrough on purpose, to process only the ints entered, but in the right order.
85 assertTrue(rpt.contains(String.format("max_objects(%d)", ints[3])));
87 assertTrue(rpt.contains(String.format("max_range(%d)", ints[2])));
89 assertTrue(rpt.contains(String.format("used(%d)", ints[1])));
91 assertTrue(rpt.contains(String.format("count(%d)", ints[0])));
96 public void settings() throws CadiException {
97 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
98 check(intPool,0,0,Pool.MAX_RANGE,Pool.MAX_OBJECTS);
100 // Check MaxObjects, min is 0
101 intPool.setMaxObjects(-10);
102 check(intPool,0,0,Pool.MAX_RANGE,0);
104 intPool.setMaxObjects(10);
105 check(intPool,0,0,Pool.MAX_RANGE,10);
107 // Check MaxRange, min is 0
108 intPool.setMaxRange(-10);
109 check(intPool,0,0,0,10);
111 intPool.setMaxRange(2);
112 check(intPool,0,0,2,10);
116 check(intPool,3,3,2,10);
120 check(intPool,0,0,2,10);
124 public void range() throws CadiException {
125 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
126 intPool.setMaxRange(2);
127 check(intPool,0,0,2);
131 check(intPool,3,3,2);
133 // Using 3 leaves count (in Pool) and Used (by System) 3
134 List<Pooled<Integer>> using = new ArrayList<>();
135 for(int i=0;i<3;++i) {
136 using.add(intPool.get());
138 check(intPool,0,3,2);
140 // Using 3 more creates more Objects, and uses immediately
141 for(int i=0;i<3;++i) {
142 using.add(intPool.get());
144 check(intPool,0,6,2);
146 // Clean out all Objects in possession, but there are 6 Objects not returned yet.
148 check(intPool,0,6,2);
151 for(Pooled<Integer> i : using) {
155 // Since Range is 2, keep only 2, and destroy the rest
156 check(intPool,2,2,2);
158 // Shutdown (helpful for stopping Services) involves turning off range
159 intPool.setMaxRange(0).drain();
160 check(intPool,0,0,0);
164 public void tooManyObjects() throws CadiException {
166 * It should be noted that "tooManyObjects" isn't enforced by the Pool, because Objects are not
167 * tracked (other than used) once they leave the pool.
169 * It is information that using entities, like Thread Pools, can use to limit creations of expensive objects
171 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
172 intPool.setMaxObjects(10).setMaxRange(2);
173 check(intPool,0,0,2,10);
175 assertFalse(intPool.tooManyObjects());
177 // Obtain up to maxium Objects
178 List<Pooled<Integer>> using = new ArrayList<>();
179 for(int i=0;i<10;++i) {
180 using.add(intPool.get());
183 check(intPool,0,10,2,10);
184 assertFalse(intPool.tooManyObjects());
186 using.add(intPool.get());
187 check(intPool,0,11,2,10);
188 assertTrue(intPool.tooManyObjects());
191 for(Pooled<Integer> i : using) {
195 // Returning Objects puts Pool back in range
196 check(intPool,2,2,2,10);
197 assertFalse(intPool.tooManyObjects());
202 public void bulkTest() throws CadiException {
203 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
206 // Remove all of the invalid items (in this case, odd numbers)
207 assertFalse(intPool.validate());
209 // Make sure we got them all
210 assertTrue(intPool.validate());
212 // Get an item from the pool
213 Pooled<Integer> gotten = intPool.get();
214 assertThat(gotten.content, is(0));
216 // finalize that item, then check the next one to make sure we actually purged
218 gotten = intPool.get();
219 assertThat(gotten.content, is(2));
226 public void loggingTest() {
227 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
229 // Log to Log.NULL for coverage
230 intPool.log(Log.Type.info,"Test log output");
232 intPool.setLogger(new CustomLogger());
233 intPool.log(Log.Type.info,"Test log output");
235 assertThat(sb.toString(), is("Test log output"));