1 package org.codehaus.plexus.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.Vector;
20
21 import org.junit.jupiter.api.AfterEach;
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertNotSame;
27 import static org.junit.jupiter.api.Assertions.assertNull;
28 import static org.junit.jupiter.api.Assertions.assertSame;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30 import static org.junit.jupiter.api.Assertions.fail;
31
32
33
34
35
36
37
38
39 public class SweeperPoolTest {
40
41 TestObjectPool pool;
42
43
44 Object o1;
45
46 Object o2;
47
48 Object o3;
49
50 Object o4;
51
52 Object o5;
53
54 Object o6;
55
56
57
58
59 @Test
60 public void testMaxSize() {
61 int sweepInterval = 0;
62 int initialCapacity = 5;
63 int maxSize = 2;
64 int minSize = 1;
65 int triggerSize = 2;
66
67 pool = new TestObjectPool(maxSize, minSize, initialCapacity, sweepInterval, triggerSize);
68
69 Object tmp = pool.get();
70 assertNull(tmp, "Expected object from pool to be null");
71 pool.put(o1);
72 assertEquals(1, pool.getSize(), "Expected pool to contain 1 object");
73 tmp = pool.get();
74 assertSame(tmp, o1, "Expected returned pool object to be the same as the one put in");
75 pool.put(o1);
76 pool.put(o2);
77 assertEquals(2, pool.getSize(), "Expected pool to contain 2 objects");
78 pool.put(o3);
79 assertEquals(2, pool.getSize(), "Expected pool to contain only a maximum of 2 objects.");
80 assertEquals(1, pool.testGetDisposedObjects().size(), "Expected 1 disposed pool object");
81 tmp = pool.testGetDisposedObjects().iterator().next();
82
83 tmp = pool.get();
84 assertEquals(1, pool.getSize(), "Expected pool size to be 1 after removing one object");
85 Object tmp2 = pool.get();
86 assertEquals(0, pool.getSize(), "Expected pool size to be 0 after removing 2 objects");
87 assertNotSame(tmp, tmp2, "Expected returned objects to be different");
88 }
89
90
91
92
93 @org.junit.jupiter.api.Test
94 public void testSweepAndTrim1() {
95
96 int sweepInterval = 1;
97 int initialCapacity = 5;
98 int maxSize = 5;
99 int minSize = 1;
100 int triggerSize = 2;
101
102 pool = new TestObjectPool(maxSize, minSize, initialCapacity, sweepInterval, triggerSize);
103 pool.put(o1);
104 pool.put(o2);
105 pool.put(o3);
106 pool.put(o4);
107
108 synchronized (this) {
109 try {
110 wait(2 * 1000);
111 } catch (InterruptedException e) {
112 fail("Unexpected exception thrown. e=" + Tracer.traceToString(e));
113 }
114 }
115 assertEquals(1, pool.getSize(), "Expected pool to only contain 1 object");
116 assertEquals(3, pool.testGetDisposedObjects().size(), "Expected 3 disposed objects");
117 }
118
119
120
121
122
123
124 @BeforeEach
125 public void setUp() throws Exception {
126 o1 = new Object();
127 o2 = new Object();
128 o3 = new Object();
129 o4 = new Object();
130 o5 = new Object();
131 o6 = new Object();
132 }
133
134
135
136
137
138
139 @AfterEach
140 public void tearDown() throws Exception {
141 pool.dispose();
142 assertTrue(pool.isDisposed());
143 pool = null;
144 }
145
146 class TestObjectPool extends SweeperPool {
147 private Vector<Object> disposedObjects = new Vector<Object>();
148
149 public TestObjectPool(int maxSize, int minSize, int intialCapacity, int sweepInterval, int triggerSize) {
150 super(maxSize, minSize, intialCapacity, sweepInterval, triggerSize);
151 }
152
153 public void reset() {
154 disposedObjects.clear();
155 }
156
157
158
159
160 public void objectDisposed(Object obj) {
161 disposedObjects.add(obj);
162 }
163
164 public Vector<Object> testGetDisposedObjects() {
165 return disposedObjects;
166 }
167 }
168 }