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