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.test.util;
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;
40 @Override public Integer create() { return current++; }
41 @Override public void destroy(Integer t) { t = 0; }
42 @Override public boolean isValid(Integer t) { return (t & 0x1) == 0; }
43 @Override public void reuse(Integer t) { }
46 private class CustomLogger implements Log {
48 public void log(Object ... o) {
49 for (Object item : o) {
50 sb.append(item.toString());
56 public void getTest() throws CadiException {
57 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
59 List<Pooled<Integer>> gotten = new ArrayList<Pooled<Integer>>();
60 for (int i = 0; i < 10; i++) {
61 gotten.add(intPool.get());
62 assertThat(gotten.get(i).content, is(i));
66 gotten.set(9, intPool.get());
67 assertThat(gotten.get(9).content, is(9));
69 for (int i = 0; i < 10; i++) {
73 for (int i = 0; i < 10; i++) {
74 gotten.set(i, intPool.get());
76 assertThat(gotten.get(i).content, is(i));
78 assertThat(gotten.get(i).content, is(i + 5));
82 for (int i = 0; i < 10; i++) {
88 // only set some objects to null -> this is for the finalize coverage test
94 // Coverage of finalize()
99 public void bulkTest() throws CadiException {
100 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
103 // Remove all of the invalid items (in this case, odd numbers)
104 assertFalse(intPool.validate());
106 // Make sure we got them all
107 assertTrue(intPool.validate());
109 // Get an item from the pool
110 Pooled<Integer> gotten = intPool.get();
111 assertThat(gotten.content, is(0));
113 // finalize that item, then check the next one to make sure we actually purged the odd numbers
114 gotten = intPool.get();
115 assertThat(gotten.content, is(2));
123 public void setMaxTest() {
124 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
125 intPool.setMaxRange(10);
126 assertThat(intPool.getMaxRange(), is(10));
127 intPool.setMaxRange(-10);
128 assertThat(intPool.getMaxRange(), is(0));
132 public void loggingTest() {
133 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
135 // Log to Log.NULL for coverage
136 intPool.log("Test log output");
138 intPool.setLogger(new CustomLogger());
139 intPool.log("Test log output");
141 assertThat(sb.toString(), is("Test log output"));