This is an automated email from the ASF dual-hosted git repository.
exceptionfactory pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git
The following commit(s) were added to refs/heads/main by this push:
new 83643e2b167 NIFI-16389 Replaced the use of Files.createTempDirectory
in unit tests with JUnit5 TempDir (#11724)
83643e2b167 is described below
commit 83643e2b1675ac856c0c38c25eb54b98ac07d5c6
Author: dan-s1 <[email protected]>
AuthorDate: Mon Sep 28 12:28:03 2026 -0400
NIFI-16389 Replaced the use of Files.createTempDirectory in unit tests with
JUnit5 TempDir (#11724)
Signed-off-by: David Handermann <[email protected]>
---
.../nifi/services/protobuf/TestProtobufReader.java | 25 ++++++++++++----------
.../nifi-file-resource-service/pom.xml | 5 -----
.../service/StandardFileResourceServiceTest.java | 13 +++++------
...ropertiesFileEngineConfigurationParserTest.java | 18 ++++++----------
4 files changed, 26 insertions(+), 35 deletions(-)
diff --git
a/nifi-extension-bundles/nifi-protobuf-bundle/nifi-protobuf-services/src/test/java/org/apache/nifi/services/protobuf/TestProtobufReader.java
b/nifi-extension-bundles/nifi-protobuf-bundle/nifi-protobuf-services/src/test/java/org/apache/nifi/services/protobuf/TestProtobufReader.java
index bfb794e30cf..40dccbc0b20 100644
---
a/nifi-extension-bundles/nifi-protobuf-bundle/nifi-protobuf-services/src/test/java/org/apache/nifi/services/protobuf/TestProtobufReader.java
+++
b/nifi-extension-bundles/nifi-protobuf-bundle/nifi-protobuf-services/src/test/java/org/apache/nifi/services/protobuf/TestProtobufReader.java
@@ -26,6 +26,7 @@ import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.jupiter.api.BeforeEach;
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.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
@@ -64,6 +65,9 @@ class TestProtobufReader {
private TestRunner runner;
private ProtobufReader protobufReader;
+ @TempDir
+ private Path testTempDir;
+
@BeforeEach
void setUp() throws InitializationException {
runner = TestRunners.newTestRunner(NoOpProcessor.class);
@@ -82,7 +86,7 @@ class TestProtobufReader {
@ParameterizedTest
@MethodSource("validConfigurationProvider")
void testValidConfigurations(final String protoFileName, final String
messageType) throws IOException {
- final Path testTempDir = createTempDirWithProtoFile(protoFileName);
+ createProtoFile(protoFileName);
runner.setProperty(protobufReader, PROTOBUF_DIRECTORY,
testTempDir.toString());
runner.setProperty(protobufReader, MESSAGE_TYPE, messageType);
runner.enableControllerService(protobufReader);
@@ -104,7 +108,7 @@ class TestProtobufReader {
@Test
void testInvalidConfigurationMissingMessageType() throws IOException {
- final Path testTempDir =
createTempDirWithProtoFile("test_proto3.proto");
+ createProtoFile("test_proto3.proto");
runner.setProperty(protobufReader, PROTOBUF_DIRECTORY,
testTempDir.toString());
final Collection<ValidationResult> results =
runner.validate(protobufReader);
@@ -117,7 +121,7 @@ class TestProtobufReader {
@Test
void testInvalidConfigurationNonExistentDirectory() throws IOException {
- final Path testTempDir =
createTempDirWithProtoFile("test_proto3.proto");
+ createProtoFile("test_proto3.proto");
final String nonExistentDir =
testTempDir.resolve("non-existent").toString();
runner.setProperty(protobufReader, PROTOBUF_DIRECTORY, nonExistentDir);
runner.setProperty(protobufReader, MESSAGE_TYPE, PROTO3_MESSAGE_TYPE);
@@ -132,7 +136,7 @@ class TestProtobufReader {
@Test
void testCustomValidationWithInvalidMessageType() throws IOException {
- final Path testTempDir =
createTempDirWithProtoFile("test_proto3.proto");
+ createProtoFile("test_proto3.proto");
runner.setProperty(protobufReader, PROTOBUF_DIRECTORY,
testTempDir.toString());
runner.setProperty(protobufReader, MESSAGE_TYPE, "NonExistentMessage");
@@ -163,7 +167,7 @@ class TestProtobufReader {
@Test
void testCreateRecordReaderWithValidConfiguration() throws Exception {
- final Path testTempDir =
createTempDirWithProtoFile("test_proto3.proto");
+ createProtoFile("test_proto3.proto");
runner.setProperty(protobufReader, PROTOBUF_DIRECTORY,
testTempDir.toString());
runner.setProperty(protobufReader, MESSAGE_TYPE, PROTO3_MESSAGE_TYPE);
runner.enableControllerService(protobufReader);
@@ -182,7 +186,7 @@ class TestProtobufReader {
@Test
void testCreateRecordReaderWithRepeatedFields() throws Exception {
- final Path testTempDir =
createTempDirWithProtoFile("test_repeated_proto3.proto");
+ createProtoFile("test_repeated_proto3.proto");
runner.setProperty(protobufReader, PROTOBUF_DIRECTORY,
testTempDir.toString());
runner.setProperty(protobufReader, MESSAGE_TYPE,
REPEATED_PROTO3_MESSAGE_TYPE);
runner.enableControllerService(protobufReader);
@@ -198,7 +202,7 @@ class TestProtobufReader {
@Test
void testValidationWithCircularReferenceProto() throws IOException {
- final Path testTempDir =
createTempDirWithProtoFile("test_circular_reference.proto");
+ createProtoFile("test_circular_reference.proto");
runner.setProperty(protobufReader, PROTOBUF_DIRECTORY,
testTempDir.toString());
runner.setProperty(protobufReader, MESSAGE_TYPE, "A");
@@ -206,14 +210,13 @@ class TestProtobufReader {
assertTrue(results.stream().allMatch(ValidationResult::isValid));
}
- private Path createTempDirWithProtoFile(final String protoFileName) throws
IOException {
- final Path testTempDir = Files.createTempDirectory("proto-test-");
+ private void createProtoFile(final String protoFileName) throws
IOException {
+ final Path protoFileNamePath = testTempDir.resolve(protoFileName);
try (final InputStream resourceStream =
getClass().getClassLoader().getResourceAsStream(protoFileName)) {
if (resourceStream != null) {
- Files.copy(resourceStream, testTempDir.resolve(protoFileName),
StandardCopyOption.REPLACE_EXISTING);
+ Files.copy(resourceStream, protoFileNamePath,
StandardCopyOption.REPLACE_EXISTING);
}
}
- return testTempDir;
}
private ValidationResult findFirstInvalid(final
Collection<ValidationResult> results) {
diff --git
a/nifi-extension-bundles/nifi-standard-services/nifi-file-resource-service-bundle/nifi-file-resource-service/pom.xml
b/nifi-extension-bundles/nifi-standard-services/nifi-file-resource-service-bundle/nifi-file-resource-service/pom.xml
index 6641c0a2ab2..4bdb8227c42 100644
---
a/nifi-extension-bundles/nifi-standard-services/nifi-file-resource-service-bundle/nifi-file-resource-service/pom.xml
+++
b/nifi-extension-bundles/nifi-standard-services/nifi-file-resource-service-bundle/nifi-file-resource-service/pom.xml
@@ -26,10 +26,5 @@
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-file-resource-service-api</artifactId>
</dependency>
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- <scope>test</scope>
- </dependency>
</dependencies>
</project>
\ No newline at end of file
diff --git
a/nifi-extension-bundles/nifi-standard-services/nifi-file-resource-service-bundle/nifi-file-resource-service/src/test/java/org/apache/nifi/fileresource/service/StandardFileResourceServiceTest.java
b/nifi-extension-bundles/nifi-standard-services/nifi-file-resource-service-bundle/nifi-file-resource-service/src/test/java/org/apache/nifi/fileresource/service/StandardFileResourceServiceTest.java
index f5380adbe65..af4728d83c2 100644
---
a/nifi-extension-bundles/nifi-standard-services/nifi-file-resource-service-bundle/nifi-file-resource-service/src/test/java/org/apache/nifi/fileresource/service/StandardFileResourceServiceTest.java
+++
b/nifi-extension-bundles/nifi-standard-services/nifi-file-resource-service-bundle/nifi-file-resource-service/src/test/java/org/apache/nifi/fileresource/service/StandardFileResourceServiceTest.java
@@ -16,7 +16,6 @@
*/
package org.apache.nifi.fileresource.service;
-import org.apache.commons.io.FileUtils;
import org.apache.nifi.fileresource.service.api.FileResource;
import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.reporting.InitializationException;
@@ -25,12 +24,12 @@ import org.apache.nifi.util.NoOpProcessor;
import org.apache.nifi.util.PropertyMigrationResult;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
-import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
+import org.junit.jupiter.api.io.TempDir;
import java.io.IOException;
import java.io.InputStream;
@@ -54,18 +53,16 @@ class StandardFileResourceServiceTest {
private static Path directoryPath;
+ @TempDir
+ private static Path tempDir;
+
private TestRunner runner;
private StandardFileResourceService service;
@BeforeAll
static void createTestDirectory() throws IOException {
- directoryPath = Files.createTempDirectory(TEST_NAME);
- }
-
- @AfterAll
- static void removeTestDirectory() throws IOException {
- FileUtils.deleteDirectory(directoryPath.toFile());
+ directoryPath = Files.createDirectory(tempDir.resolve(TEST_NAME));
}
@BeforeEach
diff --git
a/nifi-stateless/nifi-stateless-api/src/test/java/org/apache/nifi/stateless/config/PropertiesFileEngineConfigurationParserTest.java
b/nifi-stateless/nifi-stateless-api/src/test/java/org/apache/nifi/stateless/config/PropertiesFileEngineConfigurationParserTest.java
index a61ac8116bb..91d0af803eb 100644
---
a/nifi-stateless/nifi-stateless-api/src/test/java/org/apache/nifi/stateless/config/PropertiesFileEngineConfigurationParserTest.java
+++
b/nifi-stateless/nifi-stateless-api/src/test/java/org/apache/nifi/stateless/config/PropertiesFileEngineConfigurationParserTest.java
@@ -17,10 +17,10 @@
package org.apache.nifi.stateless.config;
import org.apache.nifi.stateless.engine.StatelessEngineConfiguration;
-import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
import java.io.File;
import java.io.FileOutputStream;
@@ -43,16 +43,13 @@ public class PropertiesFileEngineConfigurationParserTest {
private static Path workingDirectory;
+ @TempDir
+ private static Path tempDir;
+
@BeforeAll
public static void setDirectories() throws IOException {
- narDirectory =
Files.createTempDirectory(PropertiesFileEngineConfigurationParserTest.class.getSimpleName());
- workingDirectory =
Files.createTempDirectory(PropertiesFileEngineConfigurationParserTest.class.getSimpleName());
- }
-
- @AfterAll
- public static void deleteDirectories() throws IOException {
- Files.deleteIfExists(narDirectory);
- Files.deleteIfExists(workingDirectory);
+ narDirectory =
Files.createDirectory(tempDir.resolve(PropertiesFileEngineConfigurationParserTest.class.getSimpleName()
+ "-nar"));
+ workingDirectory =
Files.createDirectory(tempDir.resolve(PropertiesFileEngineConfigurationParserTest.class.getSimpleName()
+ "working"));
}
@BeforeEach
@@ -148,8 +145,7 @@ public class PropertiesFileEngineConfigurationParserTest {
}
private File getPropertiesFile(final Properties properties) throws
IOException {
- final File file = File.createTempFile(getClass().getSimpleName(),
".properties");
- file.deleteOnExit();
+ final File file = tempDir.resolve(getClass().getSimpleName() +
".properties").toFile();
try (final OutputStream outputStream = new FileOutputStream(file)) {
properties.store(outputStream, null);