From 964b035f8c76f575679e0cc1fe39c5dc3d2390bc Mon Sep 17 00:00:00 2001 From: Peter Hull Date: Mon, 3 Jun 2024 14:30:38 +0100 Subject: [PATCH] Fix tests on windows by Peter Hull Force test resource files to be binary On Windows, git would treat the unencoded files as text and apply CRLF line endings, so their base 64 encodings would not match the reference files. Work around timing issues on Windows On Windows the elapsed time was quite unreliable, causing intermittent fails in the performance tests, presumably made worse because PCs / the JRE are faster now then when this code was written. Also fixed up a few warnings/javadoc issues and replaced a home-grown bubble sort with Arrays.sort. --- .github/workflows/maven.yml | 2 +- .../batik/test/xml/JUnitRunnerTestCase.java | 4 ++ .../apache/batik/test/PerformanceTest.java | 59 ++++++++++--------- .../org/apache/batik/util/.gitattributes | 1 + 4 files changed, 37 insertions(+), 29 deletions(-) create mode 100644 batik-util/src/test/resources/org/apache/batik/util/.gitattributes diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 8ae94b9a2e..032cf8e515 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -17,7 +17,7 @@ jobs: fail-fast: false matrix: jdk: ['8', '11', '17'] - os: [ubuntu-latest] + os: [ubuntu-latest, windows-latest] steps: - uses: actions/checkout@v4 diff --git a/batik-test-old/src/test/java/org/apache/batik/test/xml/JUnitRunnerTestCase.java b/batik-test-old/src/test/java/org/apache/batik/test/xml/JUnitRunnerTestCase.java index 1775368314..15508dd961 100644 --- a/batik-test-old/src/test/java/org/apache/batik/test/xml/JUnitRunnerTestCase.java +++ b/batik-test-old/src/test/java/org/apache/batik/test/xml/JUnitRunnerTestCase.java @@ -51,6 +51,10 @@ public class JUnitRunnerTestCase { @BeforeClass public static void beforeClass() throws IOException { + if (System.getProperty("os.name").startsWith("Windows")) { + EXCLUDE = new ArrayList<>(EXCLUDE); + EXCLUDE.add("PerformanceTestSanity"); + } String sm = "grant { permission java.security.AllPermission; };"; File tmp = File.createTempFile("batik", "sm"); FileOutputStream fos = new FileOutputStream(tmp); diff --git a/batik-test/src/main/java/org/apache/batik/test/PerformanceTest.java b/batik-test/src/main/java/org/apache/batik/test/PerformanceTest.java index 070893763b..4d27faec1e 100644 --- a/batik-test/src/main/java/org/apache/batik/test/PerformanceTest.java +++ b/batik-test/src/main/java/org/apache/batik/test/PerformanceTest.java @@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more */ package org.apache.batik.test; +import java.util.Arrays; import java.util.Vector; /** @@ -74,7 +75,9 @@ public void setAllowedScoreDeviation(double allowedScoreDeviation) { /** * Force implementations to only implement runOp * and other performance specific methods. + * @return the results of AbstractTest's run method */ + @Override public final TestReport run() { return super.run(); } @@ -82,8 +85,10 @@ public final TestReport run() { /** * Force implementations to only implement runOp * and other performance specific methods. + * @return false always, indicating failure */ - public final boolean runImplBasic() throws Exception { + @Override + public final boolean runImplBasic() { // Should never be called for a PerformanceTest return false; } @@ -95,18 +100,21 @@ public final boolean runImplBasic() throws Exception { * or not the score is within the allowed deviation of the * reference score. * + * @return the test report + * @throws java.lang.Exception if an error occurred * @see #runRef * @see #runOp */ + @Override public final TestReport runImpl() throws Exception { int iter = 50; - double refUnit = 0; - long refStart = 0; - long refEnd = 0; - long opEnd = 0; - long opStart = 0; - double opLength = 0; + double refUnit; + long refStart; + long refEnd; + long opEnd; + long opStart; + double opLength; // Run once to remove class load time from timing. runRef(); @@ -114,8 +122,9 @@ public final TestReport runImpl() throws Exception { // System.gc(); double[] scores = new double[iter]; - - for (int i=0; i 0.0 && refUnit > 0.0) { + scores[i++] = opLength / refUnit; + System.err.print("."); + } else { + System.err.print("!"); + if (++badScores > iter) { + // Too many bad scores + throw new IllegalStateException("Unable to obtain reliable timings"); - scores[i] = opLength / refUnit; - System.err.println("."); + } + } // System.err.println(">>>>>>>> scores[" + i + "] = " + scores[i] + " (" + refUnit + " / " + opLength + ")"); System.gc(); } @@ -143,7 +161,7 @@ public final TestReport runImpl() throws Exception { System.err.println(); // Now, sort the scores - sort(scores); + Arrays.sort(scores); // Compute the mean score based on the scores, not accounting // for the lowest and highest scores @@ -184,22 +202,6 @@ public final TestReport runImpl() throws Exception { } } - protected void sort(double[] a) throws Exception { - for (int i = a.length - 1; i>=0; i--) { - boolean swapped = false; - for (int j = 0; j a[j+1]) { - double d = a[j]; - a[j] = a[j+1]; - a[j+1] = d; - swapped = true; - } - } - if (!swapped) - return; - } - } - /** * Runs the reference operation. * By default, this runs the same BufferedImage drawing @@ -220,6 +222,7 @@ protected void runRef() { /** * Runs the tested operation + * @throws java.lang.Exception if an error occurred */ protected abstract void runOp() throws Exception; } diff --git a/batik-util/src/test/resources/org/apache/batik/util/.gitattributes b/batik-util/src/test/resources/org/apache/batik/util/.gitattributes new file mode 100644 index 0000000000..625449502b --- /dev/null +++ b/batik-util/src/test/resources/org/apache/batik/util/.gitattributes @@ -0,0 +1 @@ +* -text