You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by co...@apache.org on 2020/09/01 10:28:54 UTC

[ws-wss4j] branch master updated: wss4j-ws-security-common: test: use @TempDir (#13)

This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ws-wss4j.git


The following commit(s) were added to refs/heads/master by this push:
     new 1c6c14a  wss4j-ws-security-common: test: use @TempDir (#13)
1c6c14a is described below

commit 1c6c14ab9173ce7b5808845ffc229d92b2f68945
Author: Alexey Markevich <bu...@gmail.com>
AuthorDate: Tue Sep 1 13:28:47 2020 +0300

    wss4j-ws-security-common: test: use @TempDir (#13)
---
 .../apache/wss4j/common/cache/ReplayCacheTest.java | 57 +++++++++-------------
 1 file changed, 24 insertions(+), 33 deletions(-)

diff --git a/ws-security-common/src/test/java/org/apache/wss4j/common/cache/ReplayCacheTest.java b/ws-security-common/src/test/java/org/apache/wss4j/common/cache/ReplayCacheTest.java
index cd41960..3bcdb3a 100644
--- a/ws-security-common/src/test/java/org/apache/wss4j/common/cache/ReplayCacheTest.java
+++ b/ws-security-common/src/test/java/org/apache/wss4j/common/cache/ReplayCacheTest.java
@@ -19,16 +19,15 @@
 
 package org.apache.wss4j.common.cache;
 
-import java.io.File;
 import java.io.IOException;
 import java.nio.file.Path;
 import java.time.Instant;
 import java.time.temporal.ChronoUnit;
-import java.util.Random;
 import java.util.UUID;
 
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -41,38 +40,35 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
  */
 public class ReplayCacheTest {
 
+    @TempDir
+    Path tempDir;
+
     @Test
     public void testMemoryReplayCache() throws InterruptedException, IOException {
-        ReplayCache replayCache = new MemoryReplayCache();
-
-        testReplayCacheInstance(replayCache);
-
-        replayCache.close();
+        try (ReplayCache replayCache = new MemoryReplayCache()) {
+            testReplayCacheInstance(replayCache);
+        }
     }
 
     @Test
     public void testEhCacheReplayCache() throws Exception {
-        ReplayCache replayCache = new EHCacheReplayCache("xyz", getDiskstorePath("abc"));
-
-        testReplayCacheInstance(replayCache);
-
-        replayCache.close();
+        try (ReplayCache replayCache = new EHCacheReplayCache("xyz", tempDir)) {
+            testReplayCacheInstance(replayCache);
+        }
     }
 
     @Test
     public void testEhCacheReplayCacheNoPath() throws Exception {
-        ReplayCache replayCache = new EHCacheReplayCache("xyz");
-
-        testReplayCacheInstance(replayCache);
-
-        replayCache.close();
+        try (ReplayCache replayCache = new EHCacheReplayCache("xyz")) {
+            testReplayCacheInstance(replayCache);
+        }
     }
 
     @Test
     public void testEhCacheDifferentCaches() throws Exception {
-        ReplayCache replayCache = new EHCacheReplayCache("abc", getDiskstorePath("abc"));
+        ReplayCache replayCache = new EHCacheReplayCache("abc", tempDir.resolve("abc"));
 
-        ReplayCache replayCache2 = new EHCacheReplayCache("cba", getDiskstorePath("cba"));
+        ReplayCache replayCache2 = new EHCacheReplayCache("cba", tempDir.resolve("cba"));
 
         String id = UUID.randomUUID().toString();
         replayCache.add(id);
@@ -85,7 +81,7 @@ public class ReplayCacheTest {
 
     @Test
     public void testOverflowToDisk() throws Exception {
-        ReplayCache replayCache = new EHCacheReplayCache("abc", getDiskstorePath("abc"));
+        ReplayCache replayCache = new EHCacheReplayCache("abc", tempDir);
         
         for (int i = 0; i < 10050; i++) {
             String id = Integer.toString(i);
@@ -98,7 +94,7 @@ public class ReplayCacheTest {
 
     @Test
     public void testEhCacheCloseCacheTwice() throws Exception {
-        ReplayCache replayCache = new EHCacheReplayCache("abc", getDiskstorePath("abc"));
+        ReplayCache replayCache = new EHCacheReplayCache("abc", tempDir);
         replayCache.close();
         replayCache.close();
     }
@@ -106,7 +102,7 @@ public class ReplayCacheTest {
     // No expiry specified so it falls back to the default
     @Test
     public void testEhCacheReplayCacheNoExpirySpecified() throws Exception {
-        ReplayCache replayCache = new EHCacheReplayCache("xyz", getDiskstorePath("xyz"));
+        ReplayCache replayCache = new EHCacheReplayCache("xyz", tempDir);
 
         String id = UUID.randomUUID().toString();
         replayCache.add(id);
@@ -123,7 +119,7 @@ public class ReplayCacheTest {
     // The negative expiry is rejected and it falls back to the default
     @Test
     public void testEhCacheReplayCacheNegativeExpiry() throws Exception {
-        ReplayCache replayCache = new EHCacheReplayCache("xyz", getDiskstorePath("xyz"));
+        ReplayCache replayCache = new EHCacheReplayCache("xyz", tempDir);
 
         String id = UUID.randomUUID().toString();
         replayCache.add(id, Instant.now().minusSeconds(100L));
@@ -140,7 +136,7 @@ public class ReplayCacheTest {
     // The huge expiry is rejected and it falls back to the default
     @Test
     public void testEhCacheReplayCacheHugeExpiry() throws Exception {
-        ReplayCache replayCache = new EHCacheReplayCache("xyz", getDiskstorePath("xyz"));
+        ReplayCache replayCache = new EHCacheReplayCache("xyz", tempDir);
 
         String id = UUID.randomUUID().toString();
         replayCache.add(id, Instant.now().plus(14, ChronoUnit.HOURS));
@@ -157,7 +153,7 @@ public class ReplayCacheTest {
     @Test
     public void testNullKey() throws Exception {
         Assertions.assertThrows(NullPointerException.class, () ->
-                new EHCacheReplayCache(null, getDiskstorePath("xyz")));
+                new EHCacheReplayCache(null, tempDir));
     }
 
     @Test
@@ -169,19 +165,19 @@ public class ReplayCacheTest {
     @Test
     public void testZeroDiskSize() throws Exception {
         Assertions.assertThrows(IllegalArgumentException.class, () ->
-                new EHCacheReplayCache("abc", getDiskstorePath("abc"), 0, 10000, false));
+                new EHCacheReplayCache("abc", tempDir, 0, 10000, false));
     }
 
     @Test
     public void testTooLargeDiskSize() throws Exception {
         Assertions.assertThrows(IllegalArgumentException.class, () ->
-                new EHCacheReplayCache("abc", getDiskstorePath("abc"), 10001, 10000, false));
+                new EHCacheReplayCache("abc", tempDir, 10001, 10000, false));
     }
 
     @Test
     public void testTooSmallHeapEntries() throws Exception {
         Assertions.assertThrows(IllegalArgumentException.class, () ->
-                new EHCacheReplayCache("abc", getDiskstorePath("abc"), 10, 10, false));
+                new EHCacheReplayCache("abc", tempDir, 10, 10, false));
     }
 
     private void testReplayCacheInstance(ReplayCache replayCache) throws InterruptedException, IOException {
@@ -203,9 +199,4 @@ public class ReplayCacheTest {
         assertFalse(replayCache.contains(id));
     }
 
-    private Path getDiskstorePath(String prefix) {
-        String diskKey = prefix + "-" + Math.abs(new Random().nextInt());
-        File diskstore = new File(System.getProperty("java.io.tmpdir"), diskKey);
-        return diskstore.toPath();
-    }
 }
\ No newline at end of file