diff --git a/c2/c2-client-bundle/c2-client-service/src/test/java/org/apache/nifi/c2/client/service/operation/TransferDebugOperationHandlerTest.java b/c2/c2-client-bundle/c2-client-service/src/test/java/org/apache/nifi/c2/client/service/operation/TransferDebugOperationHandlerTest.java index 07d83329f3..201163eb6a 100644 --- a/c2/c2-client-bundle/c2-client-service/src/test/java/org/apache/nifi/c2/client/service/operation/TransferDebugOperationHandlerTest.java +++ b/c2/c2-client-bundle/c2-client-service/src/test/java/org/apache/nifi/c2/client/service/operation/TransferDebugOperationHandlerTest.java @@ -18,6 +18,7 @@ package org.apache.nifi.c2.client.service.operation; import static java.nio.charset.StandardCharsets.UTF_8; +import static java.nio.file.Files.newDirectoryStream; import static java.nio.file.Files.write; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; @@ -46,6 +47,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.UncheckedIOException; +import java.nio.file.DirectoryStream; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; @@ -56,9 +58,11 @@ import java.util.stream.Stream; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; +import org.apache.commons.io.FileUtils; import org.apache.nifi.c2.client.api.C2Client; import org.apache.nifi.c2.protocol.api.C2Operation; import org.apache.nifi.c2.protocol.api.C2OperationAck; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.io.TempDir; @@ -87,6 +91,18 @@ public class TransferDebugOperationHandlerTest { @TempDir private File tempDir; + @AfterEach + public void cleanUpAfterEach() { + // Cleanup all the temporary operationId[0-9]* directories which are generated by TransferDebugOperationHandler. + final Path tempDir = Paths.get(System.getProperty("java.io.tmpdir")); + try (DirectoryStream directoryStream = newDirectoryStream(tempDir, "operationId[0-9]*")) { + for (Path operationId : directoryStream) { + FileUtils.deleteDirectory(operationId.toFile()); + } + } catch (Exception ignore) { + } + } + private static Stream invalidConstructorArguments() { C2Client mockC2Client = mock(C2Client.class); return Stream.of( diff --git a/minifi/minifi-nar-bundles/minifi-framework-bundle/minifi-framework/minifi-framework-core/src/test/java/org/apache/nifi/minifi/c2/command/syncresource/DefaultSyncResourceStrategyTest.java b/minifi/minifi-nar-bundles/minifi-framework-bundle/minifi-framework/minifi-framework-core/src/test/java/org/apache/nifi/minifi/c2/command/syncresource/DefaultSyncResourceStrategyTest.java index e59eeed402..dd352580dd 100644 --- a/minifi/minifi-nar-bundles/minifi-framework-bundle/minifi-framework/minifi-framework-core/src/test/java/org/apache/nifi/minifi/c2/command/syncresource/DefaultSyncResourceStrategyTest.java +++ b/minifi/minifi-nar-bundles/minifi-framework-bundle/minifi-framework/minifi-framework-core/src/test/java/org/apache/nifi/minifi/c2/command/syncresource/DefaultSyncResourceStrategyTest.java @@ -19,6 +19,7 @@ package org.apache.nifi.minifi.c2.command.syncresource; import static java.lang.Boolean.TRUE; import static java.nio.file.Files.createTempFile; +import static java.nio.file.Files.newDirectoryStream; import static java.util.Optional.empty; import static java.util.Optional.ofNullable; import static org.apache.nifi.c2.protocol.api.C2OperationState.OperationState.FULLY_APPLIED; @@ -38,17 +39,21 @@ import static org.mockito.Mockito.when; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.List; import java.util.Optional; import java.util.function.BiFunction; import java.util.function.Function; import java.util.stream.Stream; + import org.apache.nifi.c2.protocol.api.C2OperationState.OperationState; import org.apache.nifi.c2.protocol.api.ResourceItem; import org.apache.nifi.c2.protocol.api.ResourceType; import org.apache.nifi.c2.protocol.api.ResourcesGlobalHash; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -77,6 +82,18 @@ public class DefaultSyncResourceStrategyTest { private DefaultSyncResourceStrategy testSyncResourceStrategy; + @AfterAll + public static void cleanUpAfterAll() { + // Cleanup all the temporary uuid-.tmp files which are generated by DefaultSyncResourceStrategy. + final Path tempDir = Paths.get(System.getProperty("java.io.tmpdir")); + try (DirectoryStream directoryStream = newDirectoryStream(tempDir, "[0-9a-f]*-[0-9a-f]*-[0-9a-f]*-[0-9a-f]*[0-9]*.tmp")) { + for (Path tmpFile : directoryStream) { + Files.deleteIfExists(tmpFile); + } + } catch (Exception ignore) { + } + } + @BeforeEach public void setup() { testSyncResourceStrategy = new DefaultSyncResourceStrategy(mockResourceRepository); diff --git a/nifi-extension-bundles/nifi-extension-utils/nifi-database-utils/src/test/java/org/apache/nifi/util/db/TestJdbcClobReadable.java b/nifi-extension-bundles/nifi-extension-utils/nifi-database-utils/src/test/java/org/apache/nifi/util/db/TestJdbcClobReadable.java index dc13406505..dece413b15 100644 --- a/nifi-extension-bundles/nifi-extension-utils/nifi-database-utils/src/test/java/org/apache/nifi/util/db/TestJdbcClobReadable.java +++ b/nifi-extension-bundles/nifi-extension-utils/nifi-database-utils/src/test/java/org/apache/nifi/util/db/TestJdbcClobReadable.java @@ -25,13 +25,13 @@ import org.apache.nifi.util.file.FileUtils; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.nio.file.Files; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; @@ -46,10 +46,12 @@ public class TestJdbcClobReadable { private static final String DERBY_LOG_PROPERTY = "derby.stream.error.file"; + @TempDir + private static File tempDir; + @BeforeAll public static void setDerbyLog() { - final File derbyLog = new File(System.getProperty("java.io.tmpdir"), "derby.log"); - derbyLog.deleteOnExit(); + final File derbyLog = new File(tempDir, "derby.log"); System.setProperty(DERBY_LOG_PROPERTY, derbyLog.getAbsolutePath()); } @@ -101,9 +103,8 @@ public class TestJdbcClobReadable { private File folder; private void validateClob(String someClob) throws SQLException, ClassNotFoundException, IOException { - folder = Files.createTempDirectory(String.valueOf(System.currentTimeMillis())) - .resolve("db") - .toFile(); + File topLevelTempDir = new File(tempDir, String.valueOf(System.currentTimeMillis())); + folder = new File(topLevelTempDir, "db"); final Connection con = createConnection(folder.getAbsolutePath()); final Statement st = con.createStatement(); diff --git a/nifi-extension-bundles/nifi-extension-utils/nifi-database-utils/src/test/java/org/apache/nifi/util/db/TestJdbcHugeStream.java b/nifi-extension-bundles/nifi-extension-utils/nifi-database-utils/src/test/java/org/apache/nifi/util/db/TestJdbcHugeStream.java index f20dfb0fd0..cd8f9dcbaa 100644 --- a/nifi-extension-bundles/nifi-extension-utils/nifi-database-utils/src/test/java/org/apache/nifi/util/db/TestJdbcHugeStream.java +++ b/nifi-extension-bundles/nifi-extension-utils/nifi-database-utils/src/test/java/org/apache/nifi/util/db/TestJdbcHugeStream.java @@ -21,11 +21,11 @@ import org.apache.avro.generic.GenericDatumReader; import org.apache.avro.generic.GenericRecord; import org.apache.avro.io.DatumReader; import org.apache.derby.jdbc.EmbeddedDriver; -import org.apache.nifi.util.file.FileUtils; import org.junit.jupiter.api.AfterEach; 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.FileInputStream; @@ -57,12 +57,15 @@ import static org.junit.jupiter.api.Assertions.assertThrows; * */ public class TestJdbcHugeStream { + private static final String DERBY_LOG_PROPERTY = "derby.stream.error.file"; + @TempDir + private static File tempDir; + @BeforeAll public static void setDerbyLog() { - final File derbyLog = new File(System.getProperty("java.io.tmpdir"), "derby.log"); - derbyLog.deleteOnExit(); + final File derbyLog = new File(tempDir, "derby.log"); System.setProperty(DERBY_LOG_PROPERTY, derbyLog.getAbsolutePath()); } @@ -71,17 +74,16 @@ public class TestJdbcHugeStream { @BeforeEach public void setup() throws IOException, SQLException { DriverManager.registerDriver(new EmbeddedDriver()); - tempFile = Files.createTempDirectory(String.valueOf(System.currentTimeMillis())) - .resolve("db") - .toFile(); + File topLevelTempDir = new File(tempDir, String.valueOf(System.currentTimeMillis())); + Files.createDirectories(topLevelTempDir.toPath()); + tempFile = new File(topLevelTempDir, "db"); } @AfterEach - public void cleanup() throws IOException { + public void cleanup() { if (tempFile != null && tempFile.exists()) { final SQLException exception = assertThrows(SQLException.class, () -> DriverManager.getConnection("jdbc:derby:;shutdown=true")); assertEquals("XJ015", exception.getSQLState()); - FileUtils.deleteFile(tempFile, true); } } @@ -137,7 +139,7 @@ public class TestJdbcHugeStream { static String createProducts = "create table products (id integer, name varchar(100), code integer)"; static String createRelationships = "create table relationships (id integer,name varchar(100), code integer)"; - static public void loadTestData2Database(Connection con, int nrOfPersons, int nrOfProducts, int nrOfRels) throws ClassNotFoundException, SQLException { + static public void loadTestData2Database(Connection con, int nrOfPersons, int nrOfProducts, int nrOfRels) throws SQLException { final Statement st = con.createStatement(); diff --git a/nifi-extension-bundles/nifi-extension-utils/nifi-database-utils/src/test/java/org/apache/nifi/util/db/TestJdbcTypesH2.java b/nifi-extension-bundles/nifi-extension-utils/nifi-database-utils/src/test/java/org/apache/nifi/util/db/TestJdbcTypesH2.java index 0702d07d42..1da2e779ae 100644 --- a/nifi-extension-bundles/nifi-extension-utils/nifi-database-utils/src/test/java/org/apache/nifi/util/db/TestJdbcTypesH2.java +++ b/nifi-extension-bundles/nifi-extension-utils/nifi-database-utils/src/test/java/org/apache/nifi/util/db/TestJdbcTypesH2.java @@ -16,16 +16,14 @@ */ package org.apache.nifi.util.db; -import org.apache.nifi.util.file.FileUtils; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import java.io.ByteArrayOutputStream; -import java.io.File; import java.io.IOException; -import java.nio.file.Files; +import java.nio.file.Path; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; @@ -64,19 +62,16 @@ public class TestJdbcTypesH2 { String dbPath; + @TempDir + private Path tempDir; + @BeforeEach public void beforeEach() throws IOException { - dbPath = Files.createTempDirectory(String.valueOf(System.currentTimeMillis())) - .resolve("db") + dbPath = tempDir.resolve("db") .toFile() .getAbsolutePath(); } - @AfterEach - public void afterEach() throws IOException { - FileUtils.deleteFile(new File(dbPath), true); - } - @Test public void testSQLTypesMapping() throws SQLException, IOException { final Connection con = createConnection(dbPath); diff --git a/nifi-extension-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/test/java/org/apache/nifi/processors/groovyx/ExecuteGroovyScriptTest.java b/nifi-extension-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/test/java/org/apache/nifi/processors/groovyx/ExecuteGroovyScriptTest.java index 56e14c094d..4b308978e8 100644 --- a/nifi-extension-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/test/java/org/apache/nifi/processors/groovyx/ExecuteGroovyScriptTest.java +++ b/nifi-extension-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/test/java/org/apache/nifi/processors/groovyx/ExecuteGroovyScriptTest.java @@ -39,6 +39,7 @@ 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.ByteArrayOutputStream; import java.io.File; @@ -46,6 +47,7 @@ import java.io.FileInputStream; import java.io.PrintStream; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; import java.sql.Connection; import java.sql.DriverManager; @@ -55,6 +57,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.UUID; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -163,11 +166,12 @@ public class ExecuteGroovyScriptTest { } @Test - public void testAdditionalClasspath() throws Exception { + public void testAdditionalClasspath(@TempDir File tempDir) throws Exception { Set expectedClasspathURLs = new HashSet<>(); StringBuilder additionalClasspath = new StringBuilder(); for (int i = 0; i < 3; i++) { - Path p = java.nio.file.Files.createTempFile(getClass().getName(), ".tmp"); + Path p = new File(tempDir, getClass().getName() + UUID.randomUUID() + ".tmp").toPath(); + Files.createFile(p); expectedClasspathURLs.add(p.toUri().toURL()); additionalClasspath.append(p); additionalClasspath.append(i == 0 ? ',' : ';'); // create additional classpath string separated by ; and , diff --git a/nifi-extension-bundles/nifi-hadoop-bundle/nifi-hdfs-processors/src/test/java/org/apache/nifi/processors/hadoop/PutHDFSTest.java b/nifi-extension-bundles/nifi-hadoop-bundle/nifi-hdfs-processors/src/test/java/org/apache/nifi/processors/hadoop/PutHDFSTest.java index f9f9f2cad5..b9efab511b 100644 --- a/nifi-extension-bundles/nifi-hadoop-bundle/nifi-hdfs-processors/src/test/java/org/apache/nifi/processors/hadoop/PutHDFSTest.java +++ b/nifi-extension-bundles/nifi-hadoop-bundle/nifi-hdfs-processors/src/test/java/org/apache/nifi/processors/hadoop/PutHDFSTest.java @@ -43,13 +43,13 @@ import org.apache.nifi.util.TestRunners; import org.ietf.jgss.GSSException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import org.mockito.Mockito; import javax.security.sasl.SaslException; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.Arrays; import java.util.Collection; @@ -710,7 +710,7 @@ public class PutHDFSTest { } @Test - public void testPutFileFromLocalFile() throws Exception { + public void testPutFileFromLocalFile(@TempDir java.nio.file.Path tempDir) throws Exception { final FileSystem spyFileSystem = Mockito.spy(mockFileSystem); final PutHDFS proc = new TestablePutHDFS(spyFileSystem); final TestRunner runner = TestRunners.newTestRunner(proc); @@ -723,7 +723,6 @@ public class PutHDFSTest { String serviceId = FileResourceService.class.getSimpleName(); FileResourceService service = new StandardFileResourceService(); - byte[] FILE_DATA = "0123456789".getBytes(StandardCharsets.UTF_8); byte[] EMPTY_CONTENT = new byte[0]; runner.addControllerService(serviceId, service); runner.setProperty(service, StandardFileResourceService.FILE_PATH, String.format("${%s}", attributeName)); @@ -731,8 +730,8 @@ public class PutHDFSTest { runner.setProperty(ResourceTransferProperties.RESOURCE_TRANSFER_SOURCE, ResourceTransferSource.FILE_RESOURCE_SERVICE.getValue()); runner.setProperty(ResourceTransferProperties.FILE_RESOURCE_SERVICE, serviceId); - java.nio.file.Path tempFilePath = Files.createTempFile("PutHDFS_testPutFileFromLocalFile_", ""); - Files.write(tempFilePath, FILE_DATA); + java.nio.file.Path tempFilePath = tempDir.resolve("PutHDFS_testPutFileFromLocalFile_" + System.currentTimeMillis()); + Files.writeString(tempFilePath, "0123456789"); Map attributes = new HashMap<>(); diff --git a/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/excel/TestExcelHeaderSchemaStrategy.java b/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/excel/TestExcelHeaderSchemaStrategy.java index 0513f38547..bafc3600ae 100644 --- a/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/excel/TestExcelHeaderSchemaStrategy.java +++ b/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/excel/TestExcelHeaderSchemaStrategy.java @@ -28,6 +28,7 @@ import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; @@ -37,9 +38,13 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.HashMap; import java.util.Map; +import static java.nio.file.Files.newDirectoryStream; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -52,6 +57,21 @@ public class TestExcelHeaderSchemaStrategy { @Mock ComponentLog logger; + /* + * Cleanup the temporary poifiles directory which is created by org.apache.poi.util.DefaultTempFileCreationStrategy + * the strategy org.apache.poi.util.TempFile uses which in turn is used by com.github.pjfanning.xlsx.impl.StreamingSheetReader. + */ + @AfterAll + public static void cleanUpAfterAll() { + final Path tempDir = Path.of(System.getProperty("java.io.tmpdir")).resolve("poifiles"); + try (DirectoryStream directoryStream = newDirectoryStream(tempDir, "tmp-[0-9]*.xlsx")) { + for (Path tmpFile : directoryStream) { + Files.deleteIfExists(tmpFile); + } + } catch (Exception ignore) { + } + } + @Test void testWhereConfiguredStartRowIsEmpty() throws IOException { Object[][] data = {{}, {1, "Manny"}, {2, "Moe"}, {3, "Jack"}}; diff --git a/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/excel/TestExcelRecordReader.java b/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/excel/TestExcelRecordReader.java index 57edb9d381..6136caff61 100644 --- a/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/excel/TestExcelRecordReader.java +++ b/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/excel/TestExcelRecordReader.java @@ -35,6 +35,7 @@ import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -47,12 +48,16 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import static java.nio.file.Files.newDirectoryStream; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; @@ -76,6 +81,21 @@ public class TestExcelRecordReader { @Mock ComponentLog logger; + /* + * Cleanup the temporary poifiles directory which is created by org.apache.poi.util.DefaultTempFileCreationStrategy + * the strategy org.apache.poi.util.TempFile uses which in turn is used by com.github.pjfanning.xlsx.impl.StreamingSheetReader. + */ + @AfterAll + public static void cleanUpAfterAll() { + final Path tempDir = Path.of(System.getProperty("java.io.tmpdir")).resolve("poifiles"); + try (DirectoryStream directoryStream = newDirectoryStream(tempDir, "tmp-[0-9]*.xlsx")) { + for (Path tmpFile : directoryStream) { + Files.deleteIfExists(tmpFile); + } + } catch (Exception ignore) { + } + } + @BeforeAll static void setUpBeforeAll() throws Exception { //Generate an Excel file and populate it with data diff --git a/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/excel/TestExcelSchemaInference.java b/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/excel/TestExcelSchemaInference.java index 8565f95986..e9e35fe043 100644 --- a/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/excel/TestExcelSchemaInference.java +++ b/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/excel/TestExcelSchemaInference.java @@ -20,10 +20,14 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; + import org.apache.nifi.components.PropertyDescriptor; import org.apache.nifi.context.PropertyContext; import org.apache.nifi.logging.ComponentLog; @@ -39,11 +43,13 @@ import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import static java.nio.file.Files.newDirectoryStream; import static org.junit.jupiter.api.Assertions.assertEquals; @ExtendWith(MockitoExtension.class) @@ -61,6 +67,21 @@ public class TestExcelSchemaInference { @Mock private TimeValueInference timeValueInference; + /* + * Cleanup the temporary poifiles directory which is created by org.apache.poi.util.DefaultTempFileCreationStrategy + * the strategy org.apache.poi.util.TempFile uses which in turn is used by com.github.pjfanning.xlsx.impl.StreamingSheetReader. + */ + @AfterAll + public static void cleanUpAfterAll() { + final Path tempDir = Path.of(System.getProperty("java.io.tmpdir")).resolve("poifiles"); + try (DirectoryStream directoryStream = newDirectoryStream(tempDir, "tmp-[0-9]*.xlsx")) { + for (Path tmpFile : directoryStream) { + Files.deleteIfExists(tmpFile); + } + } catch (Exception ignore) { + } + } + @Test public void testInferenceIncludesAllRecords() throws IOException { final Map properties = new HashMap<>(); diff --git a/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/processors/excel/TestSplitExcel.java b/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/processors/excel/TestSplitExcel.java index 1f1267c19f..00f0e16414 100644 --- a/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/processors/excel/TestSplitExcel.java +++ b/nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/test/java/org/apache/nifi/processors/excel/TestSplitExcel.java @@ -24,16 +24,20 @@ import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.IOException; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.Objects; import java.util.stream.Stream; +import static java.nio.file.Files.newDirectoryStream; import static org.apache.nifi.flowfile.attributes.FragmentAttributes.FRAGMENT_COUNT; import static org.apache.nifi.flowfile.attributes.FragmentAttributes.FRAGMENT_ID; import static org.apache.nifi.flowfile.attributes.FragmentAttributes.FRAGMENT_INDEX; @@ -45,6 +49,21 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public class TestSplitExcel { private TestRunner runner; + /* + * Cleanup the temporary poifiles directory which is created by org.apache.poi.util.DefaultTempFileCreationStrategy + * the strategy org.apache.poi.util.TempFile uses which in turn is used by com.github.pjfanning.xlsx.impl.StreamingSheetReader. + */ + @AfterAll + public static void cleanUpAfterAll() { + final Path tempDir = Path.of(System.getProperty("java.io.tmpdir")).resolve("poifiles"); + try (DirectoryStream directoryStream = newDirectoryStream(tempDir, "tmp-[0-9]*.xlsx")) { + for (Path tmpFile : directoryStream) { + Files.deleteIfExists(tmpFile); + } + } catch (Exception ignore) { + } + } + @BeforeEach void setUp() { runner = TestRunners.newTestRunner(SplitExcel.class); diff --git a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/InvokeHTTPTest.java b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/InvokeHTTPTest.java index 42cd6d3682..d8b40230d1 100644 --- a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/InvokeHTTPTest.java +++ b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/InvokeHTTPTest.java @@ -20,6 +20,7 @@ import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import okhttp3.mockwebserver.RecordedRequest; import okio.Buffer; +import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.nifi.flowfile.attributes.CoreAttributes; @@ -45,6 +46,7 @@ import org.apache.nifi.util.LogMessage; import org.apache.nifi.util.MockFlowFile; import org.apache.nifi.util.TestRunner; import org.apache.nifi.util.TestRunners; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -62,6 +64,9 @@ import java.io.IOException; import java.net.Proxy; import java.net.URI; import java.nio.charset.StandardCharsets; +import java.nio.file.DirectoryStream; +import java.nio.file.Path; +import java.nio.file.Paths; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.KeyStore; @@ -85,6 +90,7 @@ import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR; import static java.net.HttpURLConnection.HTTP_MOVED_TEMP; import static java.net.HttpURLConnection.HTTP_OK; import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED; +import static java.nio.file.Files.newDirectoryStream; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; @@ -175,6 +181,18 @@ public class InvokeHTTPTest { trustManager = new StandardTrustManagerBuilder().trustStore(keyStore).build(); } + @AfterAll + public static void cleanUpAfterAll() { + // Cleanup all the temporary InvokeHttp[0-9]* directories which are generated by InvokeHttp. + final Path tempDir = Paths.get(System.getProperty("java.io.tmpdir")); + try (DirectoryStream directoryStream = newDirectoryStream(tempDir, "InvokeHTTP[0-9]*")) { + for (Path operationId : directoryStream) { + FileUtils.deleteDirectory(operationId.toFile()); + } + } catch (Exception ignore) { + } + } + @BeforeEach public void setRunner() { mockWebServer = new MockWebServer(); diff --git a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestPutSQL.java b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestPutSQL.java index d7de64e631..3737fa43b7 100644 --- a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestPutSQL.java +++ b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestPutSQL.java @@ -16,8 +16,27 @@ */ package org.apache.nifi.processors.standard; -import java.io.File; +import jakarta.xml.bind.DatatypeConverter; +import org.apache.commons.io.FileUtils; +import org.apache.nifi.controller.AbstractControllerService; +import org.apache.nifi.dbcp.DBCPService; +import org.apache.nifi.processor.FlowFileFilter; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.pattern.RollbackOnFailure; +import org.apache.nifi.provenance.ProvenanceEventRecord; +import org.apache.nifi.provenance.ProvenanceEventType; +import org.apache.nifi.reporting.InitializationException; +import org.apache.nifi.util.MockFlowFile; +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.Test; +import org.mockito.Mockito; + import java.nio.ByteBuffer; +import java.nio.file.Path; import java.nio.file.Paths; import java.sql.Connection; import java.sql.DriverManager; @@ -40,23 +59,6 @@ import java.util.Map; import java.util.Random; import java.util.UUID; import java.util.function.Function; -import jakarta.xml.bind.DatatypeConverter; -import org.apache.nifi.controller.AbstractControllerService; -import org.apache.nifi.dbcp.DBCPService; -import org.apache.nifi.processor.FlowFileFilter; -import org.apache.nifi.processor.Relationship; -import org.apache.nifi.processor.exception.ProcessException; -import org.apache.nifi.processor.util.pattern.RollbackOnFailure; -import org.apache.nifi.provenance.ProvenanceEventRecord; -import org.apache.nifi.provenance.ProvenanceEventType; -import org.apache.nifi.reporting.InitializationException; -import org.apache.nifi.util.MockFlowFile; -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.Test; -import org.mockito.Mockito; import static java.nio.charset.StandardCharsets.US_ASCII; import static org.apache.nifi.processor.FlowFileFilter.FlowFileFilterResult.ACCEPT_AND_CONTINUE; @@ -76,6 +78,9 @@ public class TestPutSQL { private static final String createPersonsAutoId = "CREATE TABLE PERSONS_AI (id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1), name VARCHAR(100), code INTEGER check(code <= 100))"; private static final String DERBY_LOG_PROPERTY = "derby.stream.error.file"; + private static final Path SYSTEM_TEMP_DIR = Paths.get(System.getProperty("java.io.tmpdir")); + private static final String TEST_DIRECTORY_NAME = "%s-%s".formatted(TestPutSQL.class.getSimpleName(), UUID.randomUUID()); + private static final Path DB_DIRECTORY = SYSTEM_TEMP_DIR.resolve(TEST_DIRECTORY_NAME); private static final Random random = new Random(); /** @@ -85,11 +90,9 @@ public class TestPutSQL { static protected DBCPService service; @BeforeAll - public static void setupDerbyLog() throws ProcessException, SQLException { + public static void setupBeforeAll() throws ProcessException, SQLException { System.setProperty(DERBY_LOG_PROPERTY, "target/derby.log"); - final File dbDir = new File(getEmptyDirectory(), "db"); - dbDir.deleteOnExit(); - service = new MockDBCPService(dbDir.getAbsolutePath()); + service = new MockDBCPService(DB_DIRECTORY.toAbsolutePath().toString()); try (final Connection conn = service.getConnection()) { try (final Statement stmt = conn.createStatement()) { stmt.executeUpdate(createPersons); @@ -99,8 +102,14 @@ public class TestPutSQL { } @AfterAll - public static void cleanupDerbyLog() { + public static void cleanupAfterAll() { System.clearProperty(DERBY_LOG_PROPERTY); + + try { + FileUtils.deleteDirectory(DB_DIRECTORY.toFile()); + } catch (final Exception ignored) { + + } } @Test @@ -1756,11 +1765,6 @@ public class TestPutSQL { return runner; } - private static File getEmptyDirectory() { - final String randomDirectory = String.format("%s-%s", TestPutSQL.class.getSimpleName(), UUID.randomUUID()); - return Paths.get(getSystemTemporaryDirectory(), randomDirectory).toFile(); - } - private static void assertSQLExceptionRelatedAttributes(final TestRunner runner, Relationship relationship) { List flowFiles = runner.getFlowFilesForRelationship(relationship); flowFiles.forEach(ff -> { @@ -1803,8 +1807,4 @@ public class TestPutSQL { && ff.getAttribute("error.code") != null && ff.getAttribute("error.sql.state") != null; } - - private static String getSystemTemporaryDirectory() { - return System.getProperty("java.io.tmpdir"); - } } diff --git a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestReplaceText.java b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestReplaceText.java index 6b4a5ddcfe..86802fc753 100644 --- a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestReplaceText.java +++ b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestReplaceText.java @@ -20,6 +20,7 @@ import org.apache.nifi.util.MockFlowFile; import org.apache.nifi.util.TestRunner; import org.apache.nifi.util.TestRunners; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import java.io.File; import java.io.IOException; @@ -925,19 +926,20 @@ public class TestReplaceText { @Test - public void testZeroByteContentFileLineByLine() throws IOException { + public void testZeroByteContentFileLineByLine(@TempDir Path tempDir) throws IOException { final TestRunner runner = getRunner(); runner.setProperty(ReplaceText.EVALUATION_MODE, ReplaceText.LINE_BY_LINE); runner.setProperty(ReplaceText.SEARCH_VALUE, "odo"); runner.setProperty(ReplaceText.REPLACEMENT_VALUE, "ood"); - final File zeroByteFile = File.createTempFile("zeroByte", ".txt"); - runner.enqueue(translateNewLines(zeroByteFile.getPath())); + final Path zeroByteFile = tempDir.resolve("zeroByte.txt"); + Files.createFile(zeroByteFile); + runner.enqueue(translateNewLines(zeroByteFile)); runner.run(); runner.assertAllFlowFilesTransferred(ReplaceText.REL_SUCCESS, 1); - final MockFlowFile out = runner.getFlowFilesForRelationship(ReplaceText.REL_SUCCESS).get(0); - out.assertContentEquals(translateNewLines(zeroByteFile.getPath())); + final MockFlowFile out = runner.getFlowFilesForRelationship(ReplaceText.REL_SUCCESS).getFirst(); + out.assertContentEquals(translateNewLines(zeroByteFile)); } diff --git a/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/state/providers/zookeeper/TestZooKeeperStateProvider.java b/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/state/providers/zookeeper/TestZooKeeperStateProvider.java index a0a223191b..5a4cc8477f 100644 --- a/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/state/providers/zookeeper/TestZooKeeperStateProvider.java +++ b/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/state/providers/zookeeper/TestZooKeeperStateProvider.java @@ -27,6 +27,7 @@ import org.apache.nifi.components.state.exception.StateTooLargeException; import org.apache.nifi.controller.state.providers.AbstractTestStateProvider; import org.apache.nifi.logging.ComponentLog; import org.apache.nifi.util.NiFiProperties; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -34,6 +35,10 @@ import org.junit.jupiter.api.Timeout; import javax.net.ssl.SSLContext; import java.io.IOException; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -64,6 +69,18 @@ public class TestZooKeeperStateProvider extends AbstractTestStateProvider { defaultProperties.put(ZooKeeperStateProvider.ACCESS_CONTROL, ZooKeeperStateProvider.OPEN_TO_WORLD.getValue()); } + @AfterAll + public static void cleanUpAfterAll() { + // Cleanup all the temporary zookeeper.configuration property files which are generated by the underlying Zookeeper API. + final Path tempDir = Paths.get(System.getProperty("java.io.tmpdir")); + try (DirectoryStream directoryStream = Files.newDirectoryStream(tempDir, "zookeeper.configuration*.properties")) { + for (Path zookeeperConfiguration : directoryStream) { + Files.deleteIfExists(zookeeperConfiguration); + } + } catch (Exception ignore) { + } + } + @BeforeEach public void setup() throws Exception { zkServer = new TestingServer(true);