Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-42101: [Java] Create File for Output Validation in FileRoundtrip #42115

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this PR is merged, changes will be needed to make it compatible with JUnit 5.

When using JUnit 5's @TempDir annotation, it was found that the logic works with non-existent files, causing IllegalArgumentException with the message input file not found.

This seems to be due to differences in behavior between JUnit 4's @TemporaryFolder and JUnit 5's @TempDir annotations.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM! Let's check the CIs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is actually being tested here? A 0-length input file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No.

In JUnit 5, I want to check for IllegalArgumentException and input file not found errors.

The code I wrote still works with JUnit 4 logic, so the tests don't write the input (writeInput(testInFile, allocator)). Because the file exists, I can see these exceptions happening.

}
}
Loading