Switching to use Junit's TempDir annotation in a few other places
diff --git a/ws-security-dom/src/test/java/org/apache/wss4j/dom/common/SecurityTestUtil.java b/ws-security-dom/src/test/java/org/apache/wss4j/dom/common/SecurityTestUtil.java
deleted file mode 100644
index 1867781..0000000
--- a/ws-security-dom/src/test/java/org/apache/wss4j/dom/common/SecurityTestUtil.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.wss4j.dom.common;
-
-import java.io.File;
-import java.util.Random;
-
-import org.apache.wss4j.common.cache.EHCacheReplayCache;
-import org.apache.wss4j.common.cache.ReplayCache;
-import org.apache.wss4j.common.ext.WSSecurityException;
-
-/**
- * A utility class for security tests
- */
-public final class SecurityTestUtil {
-
- private SecurityTestUtil() {
- // complete
- }
-
- public static ReplayCache createCache(String key) throws WSSecurityException {
- String diskKey = key + "-" + Math.abs(new Random().nextInt());
- File diskstore = new File(System.getProperty("java.io.tmpdir"), diskKey);
- return new EHCacheReplayCache(key, diskstore.toPath());
- }
-}
diff --git a/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/ReplayTest.java b/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/ReplayTest.java
index 7342b7e..de74a7f 100644
--- a/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/ReplayTest.java
+++ b/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/ReplayTest.java
@@ -19,16 +19,17 @@
package org.apache.wss4j.dom.message;
+import java.nio.file.Path;
import java.util.List;
import javax.security.auth.callback.CallbackHandler;
+import org.apache.wss4j.common.cache.EHCacheReplayCache;
import org.apache.wss4j.dom.WSConstants;
import org.apache.wss4j.dom.common.KeystoreCallbackHandler;
import org.apache.wss4j.dom.common.SAML2CallbackHandler;
import org.apache.wss4j.dom.common.SOAPUtil;
-import org.apache.wss4j.dom.common.SecurityTestUtil;
import org.apache.wss4j.dom.common.UsernamePasswordCallbackHandler;
import org.apache.wss4j.dom.engine.WSSConfig;
import org.apache.wss4j.dom.engine.WSSecurityEngine;
@@ -50,6 +51,7 @@
import org.apache.wss4j.dom.validate.SamlAssertionValidator;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -66,10 +68,17 @@
private CallbackHandler callbackHandler = new KeystoreCallbackHandler();
private Crypto crypto;
+ @TempDir
+ Path tempDir;
+
public ReplayTest() throws Exception {
crypto = CryptoFactory.getInstance();
}
+ private ReplayCache createCache(String key) throws WSSecurityException {
+ return new EHCacheReplayCache(key, tempDir);
+ }
+
@Test
public void testReplayedTimestamp() throws Exception {
@@ -158,7 +167,7 @@
RequestData data = new RequestData();
data.setWssConfig(wssConfig);
data.setCallbackHandler(callbackHandler);
- ReplayCache replayCache = SecurityTestUtil.createCache("wss4j.timestamp.cache-");
+ ReplayCache replayCache = createCache("wss4j.timestamp.cache-");
data.setTimestampReplayCache(replayCache);
// Successfully verify timestamp
@@ -253,7 +262,7 @@
RequestData data = new RequestData();
data.setWssConfig(wssConfig);
data.setCallbackHandler(callbackHandler);
- ReplayCache replayCache = SecurityTestUtil.createCache("wss4j.timestamp.cache-");
+ ReplayCache replayCache = createCache("wss4j.timestamp.cache-");
data.setTimestampReplayCache(replayCache);
// Successfully verify timestamp
@@ -358,7 +367,7 @@
RequestData data = new RequestData();
data.setWssConfig(wssConfig);
data.setCallbackHandler(callbackHandler);
- ReplayCache replayCache = SecurityTestUtil.createCache("wss4j.timestamp.cache-");
+ ReplayCache replayCache = createCache("wss4j.timestamp.cache-");
data.setTimestampReplayCache(replayCache);
// Successfully verify timestamp
@@ -431,7 +440,7 @@
RequestData data = new RequestData();
data.setCallbackHandler(new UsernamePasswordCallbackHandler());
data.setWssConfig(wssConfig);
- ReplayCache replayCache = SecurityTestUtil.createCache("wss4j.nonce.cache-");
+ ReplayCache replayCache = createCache("wss4j.nonce.cache-");
data.setNonceReplayCache(replayCache);
// Successfully verify UsernameToken
@@ -492,7 +501,7 @@
RequestData data = new RequestData();
data.setWssConfig(wssConfig);
data.setCallbackHandler(callbackHandler);
- ReplayCache replayCache = SecurityTestUtil.createCache("wss4j.saml.one.time.use.cache-");
+ ReplayCache replayCache = createCache("wss4j.saml.one.time.use.cache-");
data.setSamlOneTimeUseReplayCache(replayCache);
// Successfully verify SAML Token
@@ -549,7 +558,7 @@
RequestData data = new RequestData();
data.setWssConfig(wssConfig);
data.setCallbackHandler(callbackHandler);
- ReplayCache replayCache = SecurityTestUtil.createCache("wss4j.saml.one.time.use.cache-");
+ ReplayCache replayCache = createCache("wss4j.saml.one.time.use.cache-");
data.setSamlOneTimeUseReplayCache(replayCache);
// Successfully verify SAML Token
diff --git a/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/ReplayTest.java b/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/ReplayTest.java
index 672842c..df62e2a 100644
--- a/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/ReplayTest.java
+++ b/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/ReplayTest.java
@@ -20,10 +20,9 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
-import java.io.File;
import java.io.InputStream;
+import java.nio.file.Path;
import java.util.Properties;
-import java.util.Random;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
@@ -46,6 +45,7 @@
import org.apache.wss4j.stax.validate.SamlTokenValidatorImpl;
import org.apache.xml.security.exceptions.XMLSecurityException;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
@@ -56,12 +56,12 @@
public class ReplayTest extends AbstractTestBase {
+ @TempDir
+ Path tempDir;
+
private ReplayCache createCache(String key) throws WSSecurityException {
try {
- String diskKey = key + "-" + Math.abs(new Random().nextInt());
- File diskstore = new File(System.getProperty("java.io.tmpdir"), diskKey);
-
- return new EHCacheReplayCache(key, diskstore.toPath());
+ return new EHCacheReplayCache(key, tempDir);
} catch (XMLSecurityException e) {
throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, e);
}
diff --git a/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/UsernameTokenTest.java b/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/UsernameTokenTest.java
index 98b6611..253e1e0 100644
--- a/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/UsernameTokenTest.java
+++ b/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/UsernameTokenTest.java
@@ -20,9 +20,9 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
-import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Path;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
@@ -30,7 +30,6 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
-import java.util.Random;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
@@ -61,6 +60,7 @@
import org.apache.xml.security.stax.securityEvent.SecurityEventListener;
import org.apache.xml.security.utils.XMLUtils;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.w3c.dom.Document;
@@ -249,7 +249,7 @@
*/
@Test
- public void testReusedNonce() throws Exception {
+ public void testReusedNonce(@TempDir Path tempDir) throws Exception {
ZonedDateTime created = ZonedDateTime.now(ZoneOffset.UTC);
String createdString = DateUtil.getDateTimeFormatter(true).format(created);
@@ -276,7 +276,7 @@
WSSSecurityProperties securityProperties = new WSSSecurityProperties();
securityProperties.setCallbackHandler(new CallbackHandlerImpl());
- ReplayCache replayCache = createCache("wss4j.nonce.cache-");
+ ReplayCache replayCache = createCache("wss4j.nonce.cache", tempDir);
securityProperties.setNonceReplayCache(replayCache);
InboundWSSec wsSecIn = WSSec.getInboundWSSec(securityProperties, false, true);
XMLStreamReader xmlStreamReader = wsSecIn.processInMessage(xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(req.getBytes())));
@@ -868,11 +868,9 @@
}
}
- private ReplayCache createCache(String key) throws WSSecurityException {
+ private ReplayCache createCache(String key, Path tempDir) throws WSSecurityException {
try {
- String diskKey = key + "-" + Math.abs(new Random().nextInt());
- File diskstore = new File(System.getProperty("java.io.tmpdir"), diskKey);
- return new EHCacheReplayCache(key, diskstore.toPath());
+ return new EHCacheReplayCache(key, tempDir);
} catch (XMLSecurityException e) {
throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, e);
}