Skip to content

Commit

Permalink
GH-42101: [Java] Create File for Output Validation in FileRoundtrip (#…
Browse files Browse the repository at this point in the history
…42115)

### Rationale for this change

Enhance the logic to ensure that the output directory and file are created if they do not exist. While the input directory and file are mandatory, the output directory and file might not exist.

### What changes are included in this PR?

- [x] Create file if output directory or file does not exist.
- [x] Add unit tests
  - Test with different directories to validate creation of non-existent directory
  - Test for non-existent input file

### Are these changes tested?

Yes. Additional unit tests have been added.

### Are there any user-facing changes?

Maybe. Yes.

* GitHub Issue: #42101

Authored-by: Hyunseok Seo <[email protected]>
Signed-off-by: David Li <[email protected]>
  • Loading branch information
llama90 authored Jun 14, 2024
1 parent 870b315 commit 02f461f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
16 changes: 13 additions & 3 deletions java/tools/src/main/java/org/apache/arrow/tools/FileRoundtrip.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,23 @@ public static void main(String[] args) {
System.exit(new FileRoundtrip(System.err).run(args));
}

private File validateFile(String type, String fileName) {
private File validateFile(String type, String fileName) throws IOException {
if (fileName == null) {
throw new IllegalArgumentException("missing " + type + " file parameter");
}
File f = new File(fileName);
if (!f.exists() || f.isDirectory()) {
throw new IllegalArgumentException(type + " file not found: " + f.getAbsolutePath());
if (type.equals("input")) {
if (!f.exists() || f.isDirectory()) {
throw new IllegalArgumentException(type + " file not found: " + f.getAbsolutePath());
}
} else if (type.equals("output")) {
File parentDir = f.getParentFile();
if (parentDir != null && !parentDir.exists()) {
if (!parentDir.mkdirs()) {
throw new IOException(
"Failed to create parent directory: " + parentDir.getAbsolutePath());
}
}
}
return f;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import static org.apache.arrow.tools.ArrowFileTestFixtures.validateOutput;
import static org.apache.arrow.tools.ArrowFileTestFixtures.writeInput;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

import java.io.File;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.ipc.InvalidArrowFileException;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -32,6 +34,7 @@
public class TestFileRoundtrip {

@Rule public TemporaryFolder testFolder = new TemporaryFolder();
@Rule public TemporaryFolder testAnotherFolder = new TemporaryFolder();

private BufferAllocator allocator;

Expand All @@ -58,4 +61,36 @@ public void test() throws Exception {

validateOutput(testOutFile, allocator);
}

@Test
public void testDiffFolder() throws Exception {
File testInFile = testFolder.newFile("testIn.arrow");
File testOutFile = testAnotherFolder.newFile("testOut.arrow");

writeInput(testInFile, allocator);

String[] args = {"-i", testInFile.getAbsolutePath(), "-o", testOutFile.getAbsolutePath()};
int result = new FileRoundtrip(System.err).run(args);
assertEquals(0, result);

validateOutput(testOutFile, allocator);
}

@Test
public void testNotPreparedInput() throws Exception {
File testInFile = testFolder.newFile("testIn.arrow");
File testOutFile = testFolder.newFile("testOut.arrow");

String[] args = {"-i", testInFile.getAbsolutePath(), "-o", testOutFile.getAbsolutePath()};

// In JUnit 5, since the file itself is not created, the exception and message will be
// different.
Exception exception =
assertThrows(
InvalidArrowFileException.class,
() -> {
new FileRoundtrip(System.err).run(args);
});
assertEquals("file too small: 0", exception.getMessage());
}
}

0 comments on commit 02f461f

Please sign in to comment.