1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.wss4j.common.cache;
21
22 import java.io.IOException;
23 import java.nio.file.Path;
24 import java.time.Instant;
25 import java.time.temporal.ChronoUnit;
26 import java.util.UUID;
27
28 import org.junit.jupiter.api.Assertions;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.io.TempDir;
31
32 import static org.junit.jupiter.api.Assertions.assertEquals;
33 import static org.junit.jupiter.api.Assertions.assertFalse;
34 import static org.junit.jupiter.api.Assertions.assertNotNull;
35 import static org.junit.jupiter.api.Assertions.assertNull;
36 import static org.junit.jupiter.api.Assertions.assertTrue;
37
38
39
40
41 public class ReplayCacheTest {
42
43 @TempDir
44 Path tempDir;
45
46 @Test
47 public void testMemoryReplayCache() throws InterruptedException, IOException {
48 try (ReplayCache replayCache = new MemoryReplayCache()) {
49 testReplayCacheInstance(replayCache);
50 }
51 }
52
53 @Test
54 public void testEhCacheReplayCache() throws Exception {
55 try (ReplayCache replayCache = new EHCacheReplayCache("xyz", tempDir)) {
56 testReplayCacheInstance(replayCache);
57 }
58 }
59
60 @Test
61 public void testEhCacheReplayCacheNoPath() throws Exception {
62 try (ReplayCache replayCache = new EHCacheReplayCache("xyz")) {
63 testReplayCacheInstance(replayCache);
64 }
65 }
66
67 @Test
68 public void testEhCacheDifferentCaches() throws Exception {
69 ReplayCache replayCache = new EHCacheReplayCache("abc", tempDir.resolve("abc"));
70
71 ReplayCache replayCache2 = new EHCacheReplayCache("cba", tempDir.resolve("cba"));
72
73 String id = UUID.randomUUID().toString();
74 replayCache.add(id);
75 assertTrue(replayCache.contains(id));
76 assertFalse(replayCache2.contains(id));
77
78 replayCache.close();
79 replayCache2.close();
80 }
81
82 @Test
83 public void testOverflowToDisk() throws Exception {
84 ReplayCache replayCache = new EHCacheReplayCache("abc", tempDir);
85
86 for (int i = 0; i < 10050; i++) {
87 String id = Integer.toString(i);
88 replayCache.add(id);
89 assertTrue(replayCache.contains(id));
90 }
91
92 replayCache.close();
93 }
94
95 @Test
96 public void testEhCacheCloseCacheTwice() throws Exception {
97 ReplayCache replayCache = new EHCacheReplayCache("abc", tempDir);
98 replayCache.close();
99 replayCache.close();
100 }
101
102
103 @Test
104 public void testEhCacheReplayCacheNoExpirySpecified() throws Exception {
105 ReplayCache replayCache = new EHCacheReplayCache("xyz", tempDir);
106
107 String id = UUID.randomUUID().toString();
108 replayCache.add(id);
109 assertTrue(replayCache.contains(id));
110
111 EHCacheValue ehCacheValue = ((EHCacheReplayCache) replayCache).get(id);
112 assertNotNull(ehCacheValue);
113 assertNull(ehCacheValue.getExpiry());
114 assertEquals(id, ehCacheValue.getIdentifier());
115
116 replayCache.close();
117 }
118
119
120 @Test
121 public void testEhCacheReplayCacheNegativeExpiry() throws Exception {
122 ReplayCache replayCache = new EHCacheReplayCache("xyz", tempDir);
123
124 String id = UUID.randomUUID().toString();
125 replayCache.add(id, Instant.now().minusSeconds(100L));
126 assertTrue(replayCache.contains(id));
127
128 EHCacheValue ehCacheValue = ((EHCacheReplayCache) replayCache).get(id);
129 assertNotNull(ehCacheValue);
130 assertNotNull(ehCacheValue.getExpiry());
131 assertEquals(id, ehCacheValue.getIdentifier());
132
133 replayCache.close();
134 }
135
136
137 @Test
138 public void testEhCacheReplayCacheHugeExpiry() throws Exception {
139 ReplayCache replayCache = new EHCacheReplayCache("xyz", tempDir);
140
141 String id = UUID.randomUUID().toString();
142 replayCache.add(id, Instant.now().plus(14, ChronoUnit.HOURS));
143 assertTrue(replayCache.contains(id));
144
145 EHCacheValue ehCacheValue = ((EHCacheReplayCache) replayCache).get(id);
146 assertNotNull(ehCacheValue);
147 assertNotNull(ehCacheValue.getExpiry());
148 assertEquals(id, ehCacheValue.getIdentifier());
149
150 replayCache.close();
151 }
152
153 @Test
154 public void testNullKey() throws Exception {
155 Assertions.assertThrows(NullPointerException.class, () ->
156 new EHCacheReplayCache(null, tempDir));
157 }
158
159 @Test
160 public void testPersistentAndDiskstoreNull() throws Exception {
161 Assertions.assertThrows(NullPointerException.class, () ->
162 new EHCacheReplayCache("abc", null, 10, 10000, true));
163 }
164
165 @Test
166 public void testZeroDiskSize() throws Exception {
167 Assertions.assertThrows(IllegalArgumentException.class, () ->
168 new EHCacheReplayCache("abc", tempDir, 0, 10000, false));
169 }
170
171 @Test
172 public void testTooLargeDiskSize() throws Exception {
173 Assertions.assertThrows(IllegalArgumentException.class, () ->
174 new EHCacheReplayCache("abc", tempDir, 10001, 10000, false));
175 }
176
177 @Test
178 public void testTooSmallHeapEntries() throws Exception {
179 Assertions.assertThrows(IllegalArgumentException.class, () ->
180 new EHCacheReplayCache("abc", tempDir, 10, 10, false));
181 }
182
183 private void testReplayCacheInstance(ReplayCache replayCache) throws InterruptedException, IOException {
184
185
186 String id = UUID.randomUUID().toString();
187 replayCache.add(id);
188 assertTrue(replayCache.contains(id));
189
190
191 id = UUID.randomUUID().toString();
192 replayCache.add(id, Instant.now().plusSeconds(100L));
193 assertTrue(replayCache.contains(id));
194
195
196 id = UUID.randomUUID().toString();
197 replayCache.add(id, Instant.now().plusSeconds(1L));
198 Thread.sleep(1250L);
199 assertFalse(replayCache.contains(id));
200 }
201
202 }