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 ******************************************************************************/
21 package org.onap.aaf.cadi.util.test;
23 import static org.junit.Assert.*;
25 import java.util.ArrayList;
26 import java.util.List;
28 import static org.hamcrest.CoreMatchers.*;
30 import org.onap.aaf.cadi.CadiException;
31 import org.onap.aaf.cadi.util.Pool;
32 import org.onap.aaf.cadi.util.Pool.*;
34 public class JU_Pool {
36 private StringBuilder sb = new StringBuilder();
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 private class CustomLogger implements Log {
63 public void log(Object... o) {
64 for (Object item : o) {
65 sb.append(item.toString());
71 public void getTest() throws CadiException {
72 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
74 List<Pooled<Integer>> gotten = new ArrayList<>();
75 for (int i = 0; i < 10; i++) {
76 gotten.add(intPool.get());
77 assertThat(gotten.get(i).content, is(i));
81 gotten.set(9, intPool.get());
82 assertThat(gotten.get(9).content, is(9));
84 for (int i = 0; i < 10; i++) {
88 for (int i = 0; i < 10; i++) {
89 gotten.set(i, intPool.get());
91 assertThat(gotten.get(i).content, is(i));
93 assertThat(gotten.get(i).content, is(i + 5));
97 for (int i = 0; i < 10; i++) {
100 gotten.get(i).toss();
101 gotten.get(i).done();
103 // only set some objects to null -> this is for the finalize coverage test
109 // Coverage of finalize()
114 public void bulkTest() throws CadiException {
115 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
118 // Remove all of the invalid items (in this case, odd numbers)
119 assertFalse(intPool.validate());
121 // Make sure we got them all
122 assertTrue(intPool.validate());
124 // Get an item from the pool
125 Pooled<Integer> gotten = intPool.get();
126 assertThat(gotten.content, is(0));
128 // finalize that item, then check the next one to make sure we actually purged
130 gotten = intPool.get();
131 assertThat(gotten.content, is(2));
138 public void setMaxTest() {
139 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
140 intPool.setMaxRange(10);
141 assertThat(intPool.getMaxRange(), is(10));
142 intPool.setMaxRange(-10);
143 assertThat(intPool.getMaxRange(), is(0));
147 public void loggingTest() {
148 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
150 // Log to Log.NULL for coverage
151 intPool.log("Test log output");
153 intPool.setLogger(new CustomLogger());
154 intPool.log("Test log output");
156 assertThat(sb.toString(), is("Test log output"));