230c6b3b26b68f4be09ce034e2e07b9b3f7979c2
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / util / test / JU_Pool.java
1 /*******************************************************************************
2  * * org.onap.aaf
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
9  * *
10  *  *      http://www.apache.org/licenses/LICENSE-2.0
11  * *
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====================================================
18  * *
19  * *
20  ******************************************************************************/
21
22 package org.onap.aaf.cadi.util.test;
23
24 import static org.junit.Assert.*;
25
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import static org.hamcrest.CoreMatchers.*;
30 import org.junit.*;
31 import org.onap.aaf.cadi.CadiException;
32 import org.onap.aaf.cadi.util.Pool;
33 import org.onap.aaf.cadi.util.Pool.*;
34
35 public class JU_Pool {
36
37     private StringBuilder sb = new StringBuilder();
38
39     private class IntegerCreator implements Creator<Integer> {
40         private int current = 0;
41
42         @Override
43         public Integer create() {
44             return current++;
45         }
46
47         @Override
48         public void destroy(Integer t) {
49             t = 0;
50         }
51
52         @Override
53         public boolean isValid(Integer t) {
54             return (t & 0x1) == 0;
55         }
56
57         @Override
58         public void reuse(Integer t) {
59         }
60     }
61
62     private class CustomLogger implements Log {
63         @Override
64         public void log(Object... o) {
65             for (Object item : o) {
66                 sb.append(item.toString());
67             }
68         }
69     }
70
71     @Test
72     public void getTest() throws CadiException {
73         Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
74
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));
79         }
80
81         gotten.get(9).done();
82         gotten.set(9, intPool.get());
83         assertThat(gotten.get(9).content, is(9));
84
85         for (int i = 0; i < 10; i++) {
86             gotten.get(i).done();
87         }
88
89         for (int i = 0; i < 10; i++) {
90             gotten.set(i, intPool.get());
91             if (i < 5) {
92                 assertThat(gotten.get(i).content, is(i));
93             } else {
94                 assertThat(gotten.get(i).content, is(i + 5));
95             }
96         }
97
98         for (int i = 0; i < 10; i++) {
99             gotten.get(i).toss();
100             // Coverage calls
101             gotten.get(i).toss();
102             gotten.get(i).done();
103
104             // only set some objects to null -> this is for the finalize coverage test
105             if (i < 5) {
106                 gotten.set(i, null);
107             }
108         }
109
110         // Coverage of finalize()
111         System.gc();
112     }
113
114     @Test
115     public void bulkTest() throws CadiException {
116         Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
117
118         intPool.prime(10);
119         // Remove all of the invalid items (in this case, odd numbers)
120         assertFalse(intPool.validate());
121
122         // Make sure we got them all
123         assertTrue(intPool.validate());
124
125         // Get an item from the pool
126         Pooled<Integer> gotten = intPool.get();
127         assertThat(gotten.content, is(0));
128
129         // finalize that item, then check the next one to make sure we actually purged
130         // the odd numbers
131         gotten = intPool.get();
132         assertThat(gotten.content, is(2));
133
134         intPool.drain();
135
136     }
137
138     @Test
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));
145     }
146
147     @Test
148     public void loggingTest() {
149         Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
150
151         // Log to Log.NULL for coverage
152         intPool.log("Test log output");
153
154         intPool.setLogger(new CustomLogger());
155         intPool.log("Test log output");
156
157         assertThat(sb.toString(), is("Test log output"));
158     }
159
160 }