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.Pool;
33 import org.onap.aaf.cadi.util.Pool.*;
35 public class JU_Pool {
37 private StringBuilder sb = new StringBuilder();
39 private class IntegerCreator implements Creator<Integer> {
40 private int current = 0;
43 public Integer create() {
48 public void destroy(Integer t) {
53 public boolean isValid(Integer t) {
54 return (t & 0x1) == 0;
58 public void reuse(Integer t) {
62 private class CustomLogger implements Log {
64 public void log(Object... o) {
65 for (Object item : o) {
66 sb.append(item.toString());
72 public void getTest() throws CadiException {
73 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
75 List<Pooled<Integer>> gotten = new ArrayList<>();
76 for (int i = 0; i < 10; i++) {
77 gotten.add(intPool.get());
78 assertThat(gotten.get(i).content, is(i));
82 gotten.set(9, intPool.get());
83 assertThat(gotten.get(9).content, is(9));
85 for (int i = 0; i < 10; i++) {
89 for (int i = 0; i < 10; i++) {
90 gotten.set(i, intPool.get());
92 assertThat(gotten.get(i).content, is(i));
94 assertThat(gotten.get(i).content, is(i + 5));
98 for (int i = 0; i < 10; i++) {
101 gotten.get(i).toss();
102 gotten.get(i).done();
104 // only set some objects to null -> this is for the finalize coverage test
110 // Coverage of finalize()
115 public void bulkTest() throws CadiException {
116 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
119 // Remove all of the invalid items (in this case, odd numbers)
120 assertFalse(intPool.validate());
122 // Make sure we got them all
123 assertTrue(intPool.validate());
125 // Get an item from the pool
126 Pooled<Integer> gotten = intPool.get();
127 assertThat(gotten.content, is(0));
129 // finalize that item, then check the next one to make sure we actually purged
131 gotten = intPool.get();
132 assertThat(gotten.content, is(2));
139 public void setMaxTest() {
140 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
141 intPool.setMaxRange(10);
142 assertThat(intPool.getMaxRange(), is(10));
143 intPool.setMaxRange(-10);
144 assertThat(intPool.getMaxRange(), is(0));
148 public void loggingTest() {
149 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
151 // Log to Log.NULL for coverage
152 intPool.log("Test log output");
154 intPool.setLogger(new CustomLogger());
155 intPool.log("Test log output");
157 assertThat(sb.toString(), is("Test log output"));