NIFI-13996 Improved removal of temporary files after a build (#9551)

Signed-off-by: David Handerman <exceptionfactory@apache.org>
Co-authored-by: David Handermann <exceptionfactory@apache.org>
This commit is contained in:
dan-s1 2024-12-02 18:39:31 -05:00 committed by GitHub
parent 1c9f083682
commit 82fe051413
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 221 additions and 70 deletions

View File

@ -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<Path> directoryStream = newDirectoryStream(tempDir, "operationId[0-9]*")) {
for (Path operationId : directoryStream) {
FileUtils.deleteDirectory(operationId.toFile());
}
} catch (Exception ignore) {
}
}
private static Stream<Arguments> invalidConstructorArguments() {
C2Client mockC2Client = mock(C2Client.class);
return Stream.of(

View File

@ -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-<large numbers>.tmp files which are generated by DefaultSyncResourceStrategy.
final Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
try (DirectoryStream<Path> 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);

View File

@ -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();

View File

@ -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();

View File

@ -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);

View File

@ -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<URL> 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 ,

View File

@ -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<String, String> attributes = new HashMap<>();

View File

@ -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<Path> 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"}};

View File

@ -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<Path> 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

View File

@ -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<Path> 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<PropertyDescriptor, String> properties = new HashMap<>();

View File

@ -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<Path> 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);

View File

@ -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<Path> directoryStream = newDirectoryStream(tempDir, "InvokeHTTP[0-9]*")) {
for (Path operationId : directoryStream) {
FileUtils.deleteDirectory(operationId.toFile());
}
} catch (Exception ignore) {
}
}
@BeforeEach
public void setRunner() {
mockWebServer = new MockWebServer();

View File

@ -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<MockFlowFile> 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");
}
}

View File

@ -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));
}

View File

@ -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<Path> 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);