First part of onap rename
[appc.git] / appc-common / src / test / java / org / openecomp / appc / pool / CachedElementTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25
26
27 package org.onap.appc.pool;
28
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertFalse;
31 import static org.junit.Assert.assertNotNull;
32 import static org.junit.Assert.assertNull;
33 import static org.junit.Assert.assertTrue;
34
35 import java.io.IOException;
36 import java.lang.reflect.InvocationTargetException;
37 import java.lang.reflect.Method;
38
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.appc.pool.Allocator;
42 import org.onap.appc.pool.CachedElement;
43 import org.onap.appc.pool.Destructor;
44 import org.onap.appc.pool.Pool;
45 import org.onap.appc.pool.PoolDrainedException;
46 import org.onap.appc.pool.PoolExtensionException;
47 import org.onap.appc.pool.PoolSpecificationException;
48 import org.onap.appc.pool.*;
49
50
51 public class CachedElementTest implements Allocator<Testable>, Destructor<Testable> {
52     private static final int MIN = 10;
53     private static final int MAX = 100;
54     private Pool<Testable> pool;
55     private int index = 0;
56     private int destroyCount = 0;
57
58     /**
59      * setup
60      *
61      * @throws PoolSpecificationException
62      *             If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
63      */
64     @Before
65     public void setup() throws PoolSpecificationException {
66         pool = new Pool<>(MIN, MAX);
67     }
68
69     /**
70      * Test state
71      */
72     @Test
73     public void testAllocator() {
74         assertNull(pool.getAllocator());
75         pool.setAllocator(this);
76         assertNotNull(pool.getAllocator());
77     }
78
79     /**
80      * Test state
81      */
82     @Test
83     public void testDestructor() {
84         assertNull(pool.getDestructor());
85         pool.setDestructor(this);
86         assertNotNull(pool.getDestructor());
87     }
88
89     /**
90      * Test that we can allocate and release elements and that the pool maintains them in MRU order
91      *
92      * @throws PoolExtensionException
93      *             If the pool cannot be extended
94      * @throws PoolDrainedException
95      *             If the caller is trying to reserve an element from a drained pool
96      */
97     @Test
98     public void testAllocateAndRelease() throws PoolExtensionException, PoolDrainedException {
99         pool.setAllocator(this);
100
101         assertFalse(pool.isDrained());
102
103         /*
104          * Allocate three elements
105          */
106         Testable value1 = pool.reserve();
107         assertNotNull(value1);
108         assertEquals(Integer.valueOf(MIN - 1), Integer.valueOf(value1.getId()));
109         assertEquals(1, pool.getAllocatedSize());
110         assertEquals(MIN - 1, pool.getFreeSize());
111         assertEquals(1, pool.getAllocatedSize());
112
113         Testable value2 = pool.reserve();
114         assertNotNull(value2);
115         assertEquals(Integer.valueOf(MIN - 2), Integer.valueOf(value2.getId()));
116         assertEquals(2, pool.getAllocatedSize());
117         assertEquals(MIN - 2, pool.getFreeSize());
118         assertEquals(2, pool.getAllocatedSize());
119
120         Testable value3 = pool.reserve();
121         assertNotNull(value3);
122         assertEquals(Integer.valueOf(MIN - 3), Integer.valueOf(value3.getId()));
123         assertEquals(3, pool.getAllocatedSize());
124         assertEquals(MIN - 3, pool.getFreeSize());
125         assertEquals(3, pool.getAllocatedSize());
126
127         /*
128          * Now, release them in the order obtained
129          */
130         pool.release(value1);
131         pool.release(value2);
132         pool.release(value3);
133
134         assertEquals(0, pool.getAllocatedSize());
135         assertEquals(MIN, pool.getFreeSize());
136
137         /*
138          * Now, allocate them again, but their values should be reversed (3, 2, 1) representing the most recently used
139          * to the least recently used.
140          */
141         value1 = pool.reserve();
142         assertNotNull(value1);
143         assertEquals(Integer.valueOf(MIN - 3), Integer.valueOf(value1.getId()));
144
145         value2 = pool.reserve();
146         assertNotNull(value2);
147         assertEquals(Integer.valueOf(MIN - 2), Integer.valueOf(value2.getId()));
148
149         value3 = pool.reserve();
150         assertNotNull(value3);
151         assertEquals(Integer.valueOf(MIN - 1), Integer.valueOf(value3.getId()));
152     }
153
154     /**
155      * Test that we can trim the pool to a desired size
156      *
157      * @throws PoolDrainedException
158      *             If the caller is trying to release or reserve an element from a drained pool
159      * @throws PoolExtensionException
160      *             If the pool cannot be extended
161      * @throws IllegalAccessException
162      *             if this Method object is enforcing Java language access control and the underlying method is
163      *             inaccessible.
164      * @throws IllegalArgumentException
165      *             if the method is an instance method and the specified object argument is not an instance of the class
166      *             or interface declaring the underlying method (or of a subclass or implementor thereof); if the number
167      *             of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or
168      *             if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal
169      *             parameter type by a method invocation conversion.
170      * @throws InvocationTargetException
171      *             if the underlying method throws an exception.
172      * @throws SecurityException
173      *             If a security manager, s, is present and any of the following conditions is met:
174      *             <ul>
175      *             <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared method</li>
176      *             <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
177      *             class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
178      *             </ul>
179      * @throws NoSuchMethodException
180      *             if a matching method is not found.
181      */
182     @SuppressWarnings("nls")
183     @Test
184     public void testTrim() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
185         PoolDrainedException, PoolExtensionException, NoSuchMethodException, SecurityException {
186
187         pool.setAllocator(this);
188         int SIZE = 50;
189         Testable[] array = new Testable[SIZE];
190
191         assertEquals(0, pool.getAllocatedSize());
192         for (int i = 0; i < SIZE; i++) {
193             array[i] = pool.reserve();
194         }
195         assertEquals(SIZE, pool.getAllocatedSize());
196
197         for (int i = 0; i < SIZE; i++) {
198             pool.release(array[i]);
199         }
200         assertEquals(0, pool.getAllocatedSize());
201
202         assertEquals(SIZE, pool.getFreeSize());
203
204         Method trimMethod = Pool.class.getDeclaredMethod("trim", new Class[] {
205             Integer.TYPE
206         });
207         trimMethod.setAccessible(true);
208         trimMethod.invoke(pool, new Object[] {
209             SIZE - MIN
210         });
211
212         assertEquals(MIN, pool.getFreeSize());
213     }
214
215     /**
216      * Test that we can drain a pool containing a mix of free and allocated elements
217      *
218      * @throws PoolDrainedException
219      *             If the caller is trying to release or reserve an element from a drained pool
220      * @throws PoolExtensionException
221      *             If the pool cannot be extended
222      * @throws IOException
223      *             if an I/O error occurs
224      */
225     @Test
226     public void testDrain() throws PoolExtensionException, PoolDrainedException, IOException {
227         int SIZE = 50;
228         int FREE = 20;
229         int ALLOC = SIZE - FREE;
230
231         Testable[] array = new Testable[SIZE];
232         pool.setAllocator(this);
233         pool.setDestructor(this);
234
235         assertFalse(pool.isDrained());
236
237         assertEquals(0, pool.getAllocatedSize());
238         for (int i = 0; i < SIZE; i++) {
239             array[i] = pool.reserve();
240         }
241         assertEquals(SIZE, pool.getAllocatedSize());
242
243         for (int i = 0; i < FREE; i++) {
244             array[i].close();
245         }
246         assertEquals(ALLOC, pool.getAllocatedSize());
247         assertEquals(FREE, pool.getFreeSize());
248
249         pool.drain();
250         assertEquals(0, pool.getFreeSize());
251         assertEquals(0, pool.getAllocatedSize());
252         assertTrue(pool.isDrained());
253
254         assertEquals(SIZE, destroyCount);
255     }
256
257     /**
258      * @see org.onap.appc.pool.Allocator#allocate(org.onap.appc.pool.Pool)
259      */
260     @Override
261     public Testable allocate(Pool<Testable> pool) {
262         Testable element = new Element(index++);
263         Testable ce = CachedElement.newInstance(pool, element, new Class[] {
264             Testable.class
265         });
266         return ce;
267     }
268
269     /**
270      * @see org.onap.appc.pool.Destructor#destroy(java.io.Closeable, org.onap.appc.pool.Pool)
271      */
272     @Override
273     public void destroy(Testable obj, Pool<Testable> pool) {
274         destroyCount++;
275     }
276 }