2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
27 package org.onap.appc.pool;
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;
35 import java.io.IOException;
36 import java.lang.reflect.InvocationTargetException;
37 import java.lang.reflect.Method;
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.*;
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;
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.
65 public void setup() throws PoolSpecificationException {
66 pool = new Pool<>(MIN, MAX);
73 public void testAllocator() {
74 assertNull(pool.getAllocator());
75 pool.setAllocator(this);
76 assertNotNull(pool.getAllocator());
83 public void testDestructor() {
84 assertNull(pool.getDestructor());
85 pool.setDestructor(this);
86 assertNotNull(pool.getDestructor());
90 * Test that we can allocate and release elements and that the pool maintains them in MRU order
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
98 public void testAllocateAndRelease() throws PoolExtensionException, PoolDrainedException {
99 pool.setAllocator(this);
101 assertFalse(pool.isDrained());
104 * Allocate three elements
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());
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());
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());
128 * Now, release them in the order obtained
130 pool.release(value1);
131 pool.release(value2);
132 pool.release(value3);
134 assertEquals(0, pool.getAllocatedSize());
135 assertEquals(MIN, pool.getFreeSize());
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.
141 value1 = pool.reserve();
142 assertNotNull(value1);
143 assertEquals(Integer.valueOf(MIN - 3), Integer.valueOf(value1.getId()));
145 value2 = pool.reserve();
146 assertNotNull(value2);
147 assertEquals(Integer.valueOf(MIN - 2), Integer.valueOf(value2.getId()));
149 value3 = pool.reserve();
150 assertNotNull(value3);
151 assertEquals(Integer.valueOf(MIN - 1), Integer.valueOf(value3.getId()));
155 * Test that we can trim the pool to a desired size
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
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:
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>
179 * @throws NoSuchMethodException
180 * if a matching method is not found.
182 @SuppressWarnings("nls")
184 public void testTrim() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
185 PoolDrainedException, PoolExtensionException, NoSuchMethodException, SecurityException {
187 pool.setAllocator(this);
189 Testable[] array = new Testable[SIZE];
191 assertEquals(0, pool.getAllocatedSize());
192 for (int i = 0; i < SIZE; i++) {
193 array[i] = pool.reserve();
195 assertEquals(SIZE, pool.getAllocatedSize());
197 for (int i = 0; i < SIZE; i++) {
198 pool.release(array[i]);
200 assertEquals(0, pool.getAllocatedSize());
202 assertEquals(SIZE, pool.getFreeSize());
204 Method trimMethod = Pool.class.getDeclaredMethod("trim", new Class[] {
207 trimMethod.setAccessible(true);
208 trimMethod.invoke(pool, new Object[] {
212 assertEquals(MIN, pool.getFreeSize());
216 * Test that we can drain a pool containing a mix of free and allocated elements
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
226 public void testDrain() throws PoolExtensionException, PoolDrainedException, IOException {
229 int ALLOC = SIZE - FREE;
231 Testable[] array = new Testable[SIZE];
232 pool.setAllocator(this);
233 pool.setDestructor(this);
235 assertFalse(pool.isDrained());
237 assertEquals(0, pool.getAllocatedSize());
238 for (int i = 0; i < SIZE; i++) {
239 array[i] = pool.reserve();
241 assertEquals(SIZE, pool.getAllocatedSize());
243 for (int i = 0; i < FREE; i++) {
246 assertEquals(ALLOC, pool.getAllocatedSize());
247 assertEquals(FREE, pool.getFreeSize());
250 assertEquals(0, pool.getFreeSize());
251 assertEquals(0, pool.getAllocatedSize());
252 assertTrue(pool.isDrained());
254 assertEquals(SIZE, destroyCount);
258 * @see org.onap.appc.pool.Allocator#allocate(org.onap.appc.pool.Pool)
261 public Testable allocate(Pool<Testable> pool) {
262 Testable element = new Element(index++);
263 Testable ce = CachedElement.newInstance(pool, element, new Class[] {
270 * @see org.onap.appc.pool.Destructor#destroy(java.io.Closeable, org.onap.appc.pool.Pool)
273 public void destroy(Testable obj, Pool<Testable> pool) {