Skip to content

Commit

Permalink
[Fix] Fix FileUtils::createNewFile not create new file (#5943)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hisoka-X authored Dec 12, 2023
1 parent 3add212 commit 354fc4b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static void createParentFile(File file) {
*
* @param filePath filePath
*/
public static void createNewFile(String filePath) {
public static void createNewFile(String filePath) throws IOException {
File file = new File(filePath);
if (file.exists()) {
file.delete();
Expand All @@ -110,6 +110,7 @@ public static void createNewFile(String filePath) {
if (!file.getParentFile().exists()) {
createParentFile(file);
}
file.createNewFile();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileUtilsTest {
@Test
Expand Down Expand Up @@ -78,4 +80,27 @@ public void writeTestDataToFile(@NonNull String filePath) throws IOException {
}
}
}

@Test
public void createNewFile() throws IOException {
// create new file
FileUtils.createNewFile("/tmp/test.txt");
Assertions.assertEquals("", FileUtils.readFileToStr(Paths.get("/tmp/test.txt")));

// delete exist file and create new file
FileUtils.writeStringToFile("/tmp/test2.txt", "test");
Path test2 = Paths.get("/tmp/test2.txt");
Assertions.assertEquals("test", FileUtils.readFileToStr(test2).trim());
FileUtils.createNewFile("/tmp/test2.txt");
Assertions.assertEquals("", FileUtils.readFileToStr(test2));

// create new file with not exist folder
FileUtils.createNewFile("/tmp/newfolder/test.txt");
Assertions.assertEquals("", FileUtils.readFileToStr(Paths.get("/tmp/newfolder/test.txt")));

FileUtils.createNewFile("/tmp/newfolder/newfolder2/newfolde3/test.txt");
Assertions.assertEquals(
"",
FileUtils.readFileToStr(Paths.get("/tmp/newfolder/newfolder2/newfolde3/test.txt")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import static org.apache.seatunnel.shade.com.google.common.base.Preconditions.checkArgument;
Expand All @@ -69,7 +69,7 @@ public class ClusterFaultToleranceIT {
public static final String DYNAMIC_TEST_PARALLELISM = "dynamic_test_parallelism";

@Test
public void testBatchJobRunOkIn2Node() throws ExecutionException, InterruptedException {
public void testBatchJobRunOkIn2Node() throws Exception {
String testCaseName = "testBatchJobRunOkIn2Node";
String testClusterName = "ClusterFaultToleranceIT_testBatchJobRunOkIn2Node";
long testRowNumber = 1000;
Expand Down Expand Up @@ -161,10 +161,8 @@ public void testBatchJobRunOkIn2Node() throws ExecutionException, InterruptedExc
* @param parallelism FakeSource parallelism
*/
private ImmutablePair<String, String> createTestResources(
@NonNull String testCaseName,
@NonNull JobMode jobMode,
long rowNumber,
int parallelism) {
@NonNull String testCaseName, @NonNull JobMode jobMode, long rowNumber, int parallelism)
throws IOException {
checkArgument(rowNumber > 0, "rowNumber must greater than 0");
checkArgument(parallelism > 0, "parallelism must greater than 0");
Map<String, String> valueMap = new HashMap<>();
Expand Down Expand Up @@ -194,7 +192,7 @@ private ImmutablePair<String, String> createTestResources(
}

@Test
public void testStreamJobRunOkIn2Node() throws ExecutionException, InterruptedException {
public void testStreamJobRunOkIn2Node() throws Exception {
String testCaseName = "testStreamJobRunOkIn2Node";
String testClusterName = "ClusterFaultToleranceIT_testStreamJobRunOkIn2Node";
long testRowNumber = 1000;
Expand Down Expand Up @@ -287,8 +285,7 @@ public void testStreamJobRunOkIn2Node() throws ExecutionException, InterruptedEx
}

@Test
public void testBatchJobRestoreIn2NodeWorkerDown()
throws ExecutionException, InterruptedException {
public void testBatchJobRestoreIn2NodeWorkerDown() throws Exception {
String testCaseName = "testBatchJobRestoreIn2NodeWorkerDown";
String testClusterName = "ClusterFaultToleranceIT_testBatchJobRestoreIn2NodeWorkerDown";
long testRowNumber = 1000;
Expand Down Expand Up @@ -387,8 +384,7 @@ public void testBatchJobRestoreIn2NodeWorkerDown()
}

@Test
public void testStreamJobRestoreIn2NodeWorkerDown()
throws ExecutionException, InterruptedException {
public void testStreamJobRestoreIn2NodeWorkerDown() throws Exception {
String testCaseName = "testStreamJobRestoreIn2NodeWorkerDown";
String testClusterName = "ClusterFaultToleranceIT_testStreamJobRestoreIn2NodeWorkerDown";
long testRowNumber = 1000;
Expand Down Expand Up @@ -506,8 +502,7 @@ public void testStreamJobRestoreIn2NodeWorkerDown()
}

@Test
public void testBatchJobRestoreIn2NodeMasterDown()
throws ExecutionException, InterruptedException {
public void testBatchJobRestoreIn2NodeMasterDown() throws Exception {
String testCaseName = "testBatchJobRestoreIn2NodeMasterDown";
String testClusterName = "ClusterFaultToleranceIT_testBatchJobRestoreIn2NodeMasterDown";
long testRowNumber = 1000;
Expand Down Expand Up @@ -609,8 +604,7 @@ public void testBatchJobRestoreIn2NodeMasterDown()
}

@Test
public void testStreamJobRestoreIn2NodeMasterDown()
throws ExecutionException, InterruptedException {
public void testStreamJobRestoreIn2NodeMasterDown() throws Exception {
String testCaseName = "testStreamJobRestoreIn2NodeMasterDown";
String testClusterName = "ClusterFaultToleranceIT_testStreamJobRestoreIn2NodeMasterDown";
long testRowNumber = 1000;
Expand Down Expand Up @@ -730,15 +724,14 @@ public void testStreamJobRestoreIn2NodeMasterDown()

@Test
@Disabled
public void testFor() throws ExecutionException, InterruptedException {
public void testFor() throws Exception {
for (int i = 0; i < 200; i++) {
testStreamJobRestoreInAllNodeDown();
}
}

@Test
public void testStreamJobRestoreInAllNodeDown()
throws ExecutionException, InterruptedException {
public void testStreamJobRestoreInAllNodeDown() throws Exception {
String testCaseName = "testStreamJobRestoreInAllNodeDown";
String testClusterName =
"ClusterFaultToleranceIT_testStreamJobRestoreInAllNodeDown_"
Expand Down Expand Up @@ -938,8 +931,7 @@ public void testStreamJobRestoreInAllNodeDown()

@Test
@Disabled
public void testStreamJobRestoreFromOssInAllNodeDown()
throws ExecutionException, InterruptedException {
public void testStreamJobRestoreFromOssInAllNodeDown() throws Exception {
String OSS_BUCKET_NAME = "oss://your bucket name/";
String OSS_ENDPOINT = "your oss endpoint";
String OSS_ACCESS_KEY_ID = "oss accessKey id";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import static org.apache.seatunnel.shade.com.google.common.base.Preconditions.checkArgument;
Expand All @@ -70,8 +70,7 @@ public class ClusterFaultToleranceTwoPipelineIT {
public static final String DYNAMIC_TEST_PARALLELISM = "dynamic_test_parallelism";

@Test
public void testTwoPipelineBatchJobRunOkIn2Node()
throws ExecutionException, InterruptedException {
public void testTwoPipelineBatchJobRunOkIn2Node() throws Exception {
String testCaseName = "testTwoPipelineBatchJobRunOkIn2Node";
String testClusterName =
"ClusterFaultToleranceTwoPipelineIT_testTwoPipelineBatchJobRunOkIn2Node";
Expand Down Expand Up @@ -171,7 +170,8 @@ private ImmutablePair<String, String> createTestResources(
@NonNull JobMode jobMode,
long rowNumber,
int parallelism,
@NonNull String templateFileName) {
@NonNull String templateFileName)
throws IOException {
checkArgument(rowNumber > 0, "rowNumber must greater than 0");
checkArgument(parallelism > 0, "parallelism must greater than 0");
Map<String, String> valueMap = new HashMap<>();
Expand Down Expand Up @@ -201,8 +201,7 @@ private ImmutablePair<String, String> createTestResources(
}

@Test
public void testTwoPipelineStreamJobRunOkIn2Node()
throws ExecutionException, InterruptedException {
public void testTwoPipelineStreamJobRunOkIn2Node() throws Exception {
String testCaseName = "testTwoPipelineStreamJobRunOkIn2Node";
String testClusterName =
"ClusterFaultToleranceTwoPipelineIT_testTwoPipelineStreamJobRunOkIn2Node";
Expand Down Expand Up @@ -300,8 +299,7 @@ public void testTwoPipelineStreamJobRunOkIn2Node()
}

@Test
public void testTwoPipelineBatchJobRestoreIn2NodeWorkerDown()
throws ExecutionException, InterruptedException {
public void testTwoPipelineBatchJobRestoreIn2NodeWorkerDown() throws Exception {
String testCaseName = "testTwoPipelineBatchJobRestoreIn2NodeWorkerDown";
String testClusterName =
"ClusterFaultToleranceTwoPipelineIT_testTwoPipelineBatchJobRestoreIn2NodeWorkerDown";
Expand Down Expand Up @@ -409,15 +407,14 @@ public void testTwoPipelineBatchJobRestoreIn2NodeWorkerDown()

@Test
@Disabled
public void testFor() throws ExecutionException, InterruptedException {
public void testFor() throws Exception {
for (int i = 0; i < 200; i++) {
testTwoPipelineStreamJobRestoreIn2NodeMasterDown();
}
}

@Test
public void testTwoPipelineStreamJobRestoreIn2NodeWorkerDown()
throws ExecutionException, InterruptedException {
public void testTwoPipelineStreamJobRestoreIn2NodeWorkerDown() throws Exception {
String testCaseName = "testTwoPipelineStreamJobRestoreIn2NodeWorkerDown";
String testClusterName =
"ClusterFaultToleranceTwoPipelineIT_testTwoPipelineStreamJobRestoreIn2NodeWorkerDown";
Expand Down Expand Up @@ -545,8 +542,7 @@ public void testTwoPipelineStreamJobRestoreIn2NodeWorkerDown()
}

@Test
public void testTwoPipelineBatchJobRestoreIn2NodeMasterDown()
throws ExecutionException, InterruptedException {
public void testTwoPipelineBatchJobRestoreIn2NodeMasterDown() throws Exception {
String testCaseName =
"testTwoPipelineBatchJobRestoreIn2NodeMasterDown" + System.currentTimeMillis();
String testClusterName =
Expand Down Expand Up @@ -656,8 +652,7 @@ public void testTwoPipelineBatchJobRestoreIn2NodeMasterDown()
}

@Test
public void testTwoPipelineStreamJobRestoreIn2NodeMasterDown()
throws ExecutionException, InterruptedException {
public void testTwoPipelineStreamJobRestoreIn2NodeMasterDown() throws Exception {
String testCaseName =
"testTwoPipelineStreamJobRestoreIn2NodeMasterDown" + System.currentTimeMillis();
String testClusterName =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Map;

Expand Down Expand Up @@ -54,7 +55,8 @@ public static String getResource(String confFile) {
public static void createTestConfigFileFromTemplate(
@NonNull String templateFile,
@NonNull Map<String, String> valueMap,
@NonNull String targetFilePath) {
@NonNull String targetFilePath)
throws IOException {
String templateFilePath = getResource(templateFile);
String confContent = FileUtils.readFileToStr(Paths.get(templateFilePath));
String targetConfContent = VariablesSubstitute.substitute(confContent, valueMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -92,14 +92,14 @@ public void testEnableWriteHeader() {
try {
enableWriteHeader(
t.getFileStyle(), t.getEnableWriteHeader(), t.getHeaderName());
} catch (ExecutionException | InterruptedException e) {
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}

public void enableWriteHeader(String file_format_type, String headerWrite, String headerContent)
throws ExecutionException, InterruptedException {
throws Exception {
String testClusterName = "ClusterFaultToleranceIT_EnableWriteHeaderNode";
HazelcastInstanceImpl node1 = null;
SeaTunnelClient engineClient = null;
Expand Down Expand Up @@ -170,7 +170,7 @@ public void enableWriteHeader(String file_format_type, String headerWrite, Strin
}

private ImmutablePair<String, String> createTestResources(
@NonNull String headerWrite, @NonNull String formatType) {
@NonNull String headerWrite, @NonNull String formatType) throws IOException {
Map<String, String> valueMap = new HashMap<>();
valueMap.put(ENABLE_HEADER_WRITE, headerWrite);
valueMap.put(FILE_FORMAT_TYPE, formatType);
Expand Down

0 comments on commit 354fc4b

Please sign in to comment.