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