2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.openecomp.appc.pool;
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;
32 import java.io.IOException;
33 import java.lang.reflect.InvocationTargetException;
34 import java.lang.reflect.Method;
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.*;
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;
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.
62 public void setup() throws PoolSpecificationException {
63 pool = new Pool<>(MIN, MAX);
70 public void testAllocator() {
71 assertNull(pool.getAllocator());
72 pool.setAllocator(this);
73 assertNotNull(pool.getAllocator());
80 public void testDestructor() {
81 assertNull(pool.getDestructor());
82 pool.setDestructor(this);
83 assertNotNull(pool.getDestructor());
87 * Test that we can allocate and release elements and that the pool maintains them in MRU order
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
95 public void testAllocateAndRelease() throws PoolExtensionException, PoolDrainedException {
96 pool.setAllocator(this);
98 assertFalse(pool.isDrained());
101 * Allocate three elements
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());
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());
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());
125 * Now, release them in the order obtained
127 pool.release(value1);
128 pool.release(value2);
129 pool.release(value3);
131 assertEquals(0, pool.getAllocatedSize());
132 assertEquals(MIN, pool.getFreeSize());
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.
138 value1 = pool.reserve();
139 assertNotNull(value1);
140 assertEquals(Integer.valueOf(MIN - 3), Integer.valueOf(value1.getId()));
142 value2 = pool.reserve();
143 assertNotNull(value2);
144 assertEquals(Integer.valueOf(MIN - 2), Integer.valueOf(value2.getId()));
146 value3 = pool.reserve();
147 assertNotNull(value3);
148 assertEquals(Integer.valueOf(MIN - 1), Integer.valueOf(value3.getId()));
152 * Test that we can trim the pool to a desired size
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
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:
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>
176 * @throws NoSuchMethodException
177 * if a matching method is not found.
179 @SuppressWarnings("nls")
181 public void testTrim() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
182 PoolDrainedException, PoolExtensionException, NoSuchMethodException, SecurityException {
184 pool.setAllocator(this);
186 Testable[] array = new Testable[SIZE];
188 assertEquals(0, pool.getAllocatedSize());
189 for (int i = 0; i < SIZE; i++) {
190 array[i] = pool.reserve();
192 assertEquals(SIZE, pool.getAllocatedSize());
194 for (int i = 0; i < SIZE; i++) {
195 pool.release(array[i]);
197 assertEquals(0, pool.getAllocatedSize());
199 assertEquals(SIZE, pool.getFreeSize());
201 Method trimMethod = Pool.class.getDeclaredMethod("trim", new Class[] {
204 trimMethod.setAccessible(true);
205 trimMethod.invoke(pool, new Object[] {
209 assertEquals(MIN, pool.getFreeSize());
213 * Test that we can drain a pool containing a mix of free and allocated elements
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
223 public void testDrain() throws PoolExtensionException, PoolDrainedException, IOException {
226 int ALLOC = SIZE - FREE;
228 Testable[] array = new Testable[SIZE];
229 pool.setAllocator(this);
230 pool.setDestructor(this);
232 assertFalse(pool.isDrained());
234 assertEquals(0, pool.getAllocatedSize());
235 for (int i = 0; i < SIZE; i++) {
236 array[i] = pool.reserve();
238 assertEquals(SIZE, pool.getAllocatedSize());
240 for (int i = 0; i < FREE; i++) {
243 assertEquals(ALLOC, pool.getAllocatedSize());
244 assertEquals(FREE, pool.getFreeSize());
247 assertEquals(0, pool.getFreeSize());
248 assertEquals(0, pool.getAllocatedSize());
249 assertTrue(pool.isDrained());
251 assertEquals(SIZE, destroyCount);
255 * @see org.openecomp.appc.pool.Allocator#allocate(org.openecomp.appc.pool.Pool)
258 public Testable allocate(Pool<Testable> pool) {
259 Testable element = new Element(index++);
260 Testable ce = CachedElement.newInstance(pool, element, new Class[] {
267 * @see org.openecomp.appc.pool.Destructor#destroy(java.io.Closeable, org.openecomp.appc.pool.Pool)
270 public void destroy(Testable obj, Pool<Testable> pool) {