f4703bb86e5abd4a0440c6caf6ab512dea67d7c0
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / test / util / 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.test.util;
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                 @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) { }
44         }
45
46         private class CustomLogger implements Log {
47                 @Override
48                 public void log(Object ... o) {
49                         for (Object item : o) {
50                                 sb.append(item.toString());
51                         }
52                 }
53         }
54
55         @Test
56         public void getTest() throws CadiException {
57                 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
58
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));
63                 }
64
65                 gotten.get(9).done();
66                 gotten.set(9, intPool.get());
67                 assertThat(gotten.get(9).content, is(9));
68
69                 for (int i = 0; i < 10; i++) {
70                         gotten.get(i).done();
71                 }
72
73                 for (int i = 0; i < 10; i++) {
74                         gotten.set(i, intPool.get());
75                         if (i < 5) {
76                                 assertThat(gotten.get(i).content, is(i));
77                         } else {
78                                 assertThat(gotten.get(i).content, is(i + 5));
79                         }
80                 }
81
82                 for (int i = 0; i < 10; i++) {
83                         gotten.get(i).toss();
84                         // Coverage calls
85                         gotten.get(i).toss();
86                         gotten.get(i).done();
87
88                         // only set some objects to null -> this is for the finalize coverage test
89                         if (i < 5) {
90                                 gotten.set(i, null);
91                         }
92                 }
93
94                 // Coverage of finalize()
95                 System.gc();
96         }
97
98         @Test
99         public void bulkTest() throws CadiException {
100                 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
101
102                 intPool.prime(10);
103                 // Remove all of the invalid items (in this case, odd numbers)
104                 assertFalse(intPool.validate());
105
106                 // Make sure we got them all
107                 assertTrue(intPool.validate());
108
109                 // Get an item from the pool
110                 Pooled<Integer> gotten = intPool.get();
111                 assertThat(gotten.content, is(0));
112
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));
116
117                 intPool.drain();
118
119         }
120
121
122         @Test
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));
129         }
130
131         @Test
132         public void loggingTest() {
133                 Pool<Integer> intPool = new Pool<Integer>(new IntegerCreator());
134
135                 // Log to Log.NULL for coverage
136                 intPool.log("Test log output");
137
138                 intPool.setLogger(new CustomLogger());
139                 intPool.log("Test log output");
140
141                 assertThat(sb.toString(), is("Test log output"));
142         }
143
144 }