2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
26 package org.onap.appc.pool;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertNull;
32 import static org.junit.Assert.assertTrue;
34 import java.io.IOException;
35 import java.lang.reflect.InvocationTargetException;
36 import java.lang.reflect.Method;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.appc.pool.Allocator;
41 import org.onap.appc.pool.CachedElement;
42 import org.onap.appc.pool.Destructor;
43 import org.onap.appc.pool.Pool;
44 import org.onap.appc.pool.PoolDrainedException;
45 import org.onap.appc.pool.PoolExtensionException;
46 import org.onap.appc.pool.PoolSpecificationException;
47 import org.onap.appc.pool.*;
50 public class CachedElementTest implements Allocator<Testable>, Destructor<Testable> {
51 private static final int MIN = 10;
52 private static final int MAX = 100;
53 private Pool<Testable> pool;
54 private int index = 0;
55 private int destroyCount = 0;
60 * @throws PoolSpecificationException
61 * If the minimum size is less than 0, or if the max size is non-zero and less than the min size.
64 public void setup() throws PoolSpecificationException {
65 pool = new Pool<>(MIN, MAX);
72 public void testAllocator() {
73 assertNull(pool.getAllocator());
74 pool.setAllocator(this);
75 assertNotNull(pool.getAllocator());
82 public void testDestructor() {
83 assertNull(pool.getDestructor());
84 pool.setDestructor(this);
85 assertNotNull(pool.getDestructor());
89 * Test that we can allocate and release elements and that the pool maintains them in MRU order
91 * @throws PoolExtensionException
92 * If the pool cannot be extended
93 * @throws PoolDrainedException
94 * If the caller is trying to reserve an element from a drained pool
97 public void testAllocateAndRelease() throws PoolExtensionException, PoolDrainedException {
98 pool.setAllocator(this);
100 assertFalse(pool.isDrained());
103 * Allocate three elements
105 Testable value1 = pool.reserve();
106 assertNotNull(value1);
107 assertEquals(Integer.valueOf(MIN - 1), Integer.valueOf(value1.getId()));
108 assertEquals(1, pool.getAllocatedSize());
109 assertEquals(MIN - 1, pool.getFreeSize());
110 assertEquals(1, pool.getAllocatedSize());
112 Testable value2 = pool.reserve();
113 assertNotNull(value2);
114 assertEquals(Integer.valueOf(MIN - 2), Integer.valueOf(value2.getId()));
115 assertEquals(2, pool.getAllocatedSize());
116 assertEquals(MIN - 2, pool.getFreeSize());
117 assertEquals(2, pool.getAllocatedSize());
119 Testable value3 = pool.reserve();
120 assertNotNull(value3);
121 assertEquals(Integer.valueOf(MIN - 3), Integer.valueOf(value3.getId()));
122 assertEquals(3, pool.getAllocatedSize());
123 assertEquals(MIN - 3, pool.getFreeSize());
124 assertEquals(3, pool.getAllocatedSize());
127 * Now, release them in the order obtained
129 pool.release(value1);
130 pool.release(value2);
131 pool.release(value3);
133 assertEquals(0, pool.getAllocatedSize());
134 assertEquals(MIN, pool.getFreeSize());
137 * Now, allocate them again, but their values should be reversed (3, 2, 1) representing the most recently used
138 * to the least recently used.
140 value1 = pool.reserve();
141 assertNotNull(value1);
142 assertEquals(Integer.valueOf(MIN - 3), Integer.valueOf(value1.getId()));
144 value2 = pool.reserve();
145 assertNotNull(value2);
146 assertEquals(Integer.valueOf(MIN - 2), Integer.valueOf(value2.getId()));
148 value3 = pool.reserve();
149 assertNotNull(value3);
150 assertEquals(Integer.valueOf(MIN - 1), Integer.valueOf(value3.getId()));
154 * Test that we can trim the pool to a desired size
156 * @throws PoolDrainedException
157 * If the caller is trying to release or reserve an element from a drained pool
158 * @throws PoolExtensionException
159 * If the pool cannot be extended
160 * @throws IllegalAccessException
161 * if this Method object is enforcing Java language access control and the underlying method is
163 * @throws IllegalArgumentException
164 * if the method is an instance method and the specified object argument is not an instance of the class
165 * or interface declaring the underlying method (or of a subclass or implementor thereof); if the number
166 * of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or
167 * if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal
168 * parameter type by a method invocation conversion.
169 * @throws InvocationTargetException
170 * if the underlying method throws an exception.
171 * @throws SecurityException
172 * If a security manager, s, is present and any of the following conditions is met:
174 * <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared method</li>
175 * <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
176 * class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
178 * @throws NoSuchMethodException
179 * if a matching method is not found.
181 @SuppressWarnings("nls")
183 public void testTrim() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
184 PoolDrainedException, PoolExtensionException, NoSuchMethodException, SecurityException {
186 pool.setAllocator(this);
188 Testable[] array = new Testable[SIZE];
190 assertEquals(0, pool.getAllocatedSize());
191 for (int i = 0; i < SIZE; i++) {
192 array[i] = pool.reserve();
194 assertEquals(SIZE, pool.getAllocatedSize());
196 for (int i = 0; i < SIZE; i++) {
197 pool.release(array[i]);
199 assertEquals(0, pool.getAllocatedSize());
201 assertEquals(SIZE, pool.getFreeSize());
203 Method trimMethod = Pool.class.getDeclaredMethod("trim", new Class[] {
206 trimMethod.setAccessible(true);
207 trimMethod.invoke(pool, new Object[] {
211 assertEquals(MIN, pool.getFreeSize());
215 * Test that we can drain a pool containing a mix of free and allocated elements
217 * @throws PoolDrainedException
218 * If the caller is trying to release or reserve an element from a drained pool
219 * @throws PoolExtensionException
220 * If the pool cannot be extended
221 * @throws IOException
222 * if an I/O error occurs
225 public void testDrain() throws PoolExtensionException, PoolDrainedException, IOException {
228 int ALLOC = SIZE - FREE;
230 Testable[] array = new Testable[SIZE];
231 pool.setAllocator(this);
232 pool.setDestructor(this);
234 assertFalse(pool.isDrained());
236 assertEquals(0, pool.getAllocatedSize());
237 for (int i = 0; i < SIZE; i++) {
238 array[i] = pool.reserve();
240 assertEquals(SIZE, pool.getAllocatedSize());
242 for (int i = 0; i < FREE; i++) {
245 assertEquals(ALLOC, pool.getAllocatedSize());
246 assertEquals(FREE, pool.getFreeSize());
249 assertEquals(0, pool.getFreeSize());
250 assertEquals(0, pool.getAllocatedSize());
251 assertTrue(pool.isDrained());
253 assertEquals(SIZE, destroyCount);
257 * @see org.onap.appc.pool.Allocator#allocate(org.onap.appc.pool.Pool)
260 public Testable allocate(Pool<Testable> pool) {
261 Testable element = new Element(index++);
262 Testable ce = CachedElement.newInstance(pool, element, new Class[] {
269 * @see org.onap.appc.pool.Destructor#destroy(java.io.Closeable, org.onap.appc.pool.Pool)
272 public void destroy(Testable obj, Pool<Testable> pool) {