Skip to content

modernize testAdditionComparisons() file handling? #1372

Description

@markusicu

While looking into something else, I got down a rabbit hole of modernizing the file handling in TestTestUnicodeInvariants.testAdditionComparisons().

  • File.listFiles() and other APIs point to Files.newDirectoryStream() as a modern replacement
  • Path is new/better(?) for dealing with paths
  • get the one rmgIssueFilter file ahead of the loop instead of listing all files and ignoring most of them

I didn't have time to get to my main goal, and while this code should be "better" I wasn't sure it's worth the change. And I have to work on something else.

So I am parking this here for now. @eggrobin WDYT?

    @Test
    void testAdditionComparisons() throws IOException {
        // Ideally, this would be a @ParameterizedTest, and we would use the Dtest parameter of the
        // mvn command line to filter a specific test.
        // Unfortunately, this does not appear to be possible with JUnit 5 and Surefire, see
        // https://stackoverflow.com/questions/69198795/junit5-running-a-single-instance-of-parameterized-test,
        // so we have one big test and we filter by hand.
        final var dir = Path.of(getClass().getResource("AdditionComparisons").getPath());
        List<File> files;
        final String rmgIssueFilter = CldrUtility.getProperty("RMG_ISSUE", null);
        if (rmgIssueFilter == null) {
            try (var contents = Files.newDirectoryStream(dir)) {
                files =
                        StreamSupport.stream(contents.spliterator(), false)
                                .filter(p -> p.toString().endsWith(".txt"))
                                .map(Path::toFile)
                                .toList();
            }
        } else {
            File rmgFile = dir.resolve(rmgIssueFilter + ".txt").toFile();
            assertTrue(rmgFile.exists(), "Could not find test " + rmgIssueFilter + ".txt");
            files = List.of(rmgFile);
        }
        int rc = 0;
        for (var file : files) {
            final String filename = file.getName();
            final String nameWithoutExtension = filename.substring(0, filename.length() - 4);
            final int errors =
                    TestUnicodeInvariants.testInvariants(
                            "AdditionComparisons/" + filename,
                            "addition-comparisons-" + nameWithoutExtension,
                            true);
            if (errors != 0) {
                System.err.println(errors + " errors in " + filename);
            }
            rc += errors;
        }
        assertEquals(0, rc, "TestUnicodeInvariants.testInvariants(addition-comparisons) failed");
    }
diff --git a/unicodetools/src/test/java/org/unicode/text/UCD/TestTestUnicodeInvariants.java b/unicodetools/src/test/java/org/unicode/text/UCD/TestTestUnicodeInvariants.java
index 093f806c..69df9238 100644
--- a/unicodetools/src/test/java/org/unicode/text/UCD/TestTestUnicodeInvariants.java
+++ b/unicodetools/src/test/java/org/unicode/text/UCD/TestTestUnicodeInvariants.java
@@ -8,8 +8,12 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.text.ParseException;
 import java.text.ParsePosition;
+import java.util.List;
+import java.util.stream.StreamSupport;
 import org.junit.jupiter.api.Test;
 import org.unicode.cldr.util.CldrUtility;
 import org.unicode.text.UCD.TestUnicodeInvariants.BackwardParseException;
@@ -42,31 +46,31 @@ void testUnicodeInvariants() throws IOException {
 
     @Test
     void testAdditionComparisons() throws IOException {
-        final var directory = new File(getClass().getResource("AdditionComparisons").getPath());
-        if (!directory.exists()) {
-            throw new IOException(directory.getAbsolutePath() + " does not exist");
-        }
-        int rc = 0;
         // Ideally, this would be a @ParameterizedTest, and we would use the Dtest parameter of the
         // mvn command line to filter a specific test.
         // Unfortunately, this does not appear to be possible with JUnit 5 and Surefire, see
         // https://stackoverflow.com/questions/69198795/junit5-running-a-single-instance-of-parameterized-test,
         // so we have one big test and we filter by hand.
+        final var dir = Path.of(getClass().getResource("AdditionComparisons").getPath());
+        List<File> files;
         final String rmgIssueFilter = CldrUtility.getProperty("RMG_ISSUE", null);
-        boolean matchedFilter = false;
-        for (var file : directory.listFiles()) {
-            final String filename = file.getName();
-            if (!file.getName().endsWith(".txt")) {
-                continue;
+        if (rmgIssueFilter == null) {
+            try (var contents = Files.newDirectoryStream(dir)) {
+                files =
+                        StreamSupport.stream(contents.spliterator(), false)
+                                .filter(p -> p.toString().endsWith(".txt"))
+                                .map(Path::toFile)
+                                .toList();
             }
+        } else {
+            File rmgFile = dir.resolve(rmgIssueFilter + ".txt").toFile();
+            assertTrue(rmgFile.exists(), "Could not find test " + rmgIssueFilter + ".txt");
+            files = List.of(rmgFile);
+        }
+        int rc = 0;
+        for (var file : files) {
+            final String filename = file.getName();
             final String nameWithoutExtension = filename.substring(0, filename.length() - 4);
-            if (rmgIssueFilter != null) {
-                if (nameWithoutExtension.equals(rmgIssueFilter)) {
-                    matchedFilter = true;
-                } else {
-                    continue;
-                }
-            }
             final int errors =
                     TestUnicodeInvariants.testInvariants(
                             "AdditionComparisons/" + filename,
@@ -77,9 +81,6 @@ void testAdditionComparisons() throws IOException {
             }
             rc += errors;
         }
-        if (rmgIssueFilter != null) {
-            assertTrue(matchedFilter, "Could not find test " + rmgIssueFilter + ".txt");
-        }
         assertEquals(0, rc, "TestUnicodeInvariants.testInvariants(addition-comparisons) failed");
     }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions