]> source.dussan.org Git - aspectj.git/commitdiff
Improve text matching in OutputSpec, fixing some failing Windows tests
authorAlexander Kriegisch <Alexander@Kriegisch.name>
Sun, 14 Mar 2021 03:19:21 +0000 (10:19 +0700)
committerAlexander Kriegisch <Alexander@Kriegisch.name>
Sun, 14 Mar 2021 03:19:21 +0000 (10:19 +0700)
Some Java 14 text block tests failed on Windows because a
StringTokenizer was used to split by LF, but the Windows line
separator is CR+LF. Because a multi-line string ending with CR+LF is
printed via 'System.out.println' in the test code, another CR+LF is
added to the output, resulting in trailing CR+LF+CR+LF. Hence, between
the two LFs, the tokenizer actually found an additional line consisting
of CR (only on Windows, of course). Despite each line token actually
containing a trailing CR token, that did not matter much because
'String.trim' was used everywhere before comparing values.

Anyway, the improved OutputSpec uses text.trim().split("\\s*\n\\s*"),
which takes care of leading/trailing whitespace both around the whole
output and for each separate line.

Signed-off-by: Alexander Kriegisch <Alexander@Kriegisch.name>
testing/src/test/java/org/aspectj/testing/OutputSpec.java
tests/features195/textblock/Code.java
tests/features195/textblock/Code2.java

index 8e1fd261234c57cfd8b5f0821a642dd36905c11e..9d334386aa11fc1f79c9d6d17bd3845eb2f4513d 100644 (file)
@@ -11,8 +11,8 @@
  * ******************************************************************/
 package org.aspectj.testing;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
-import java.util.StringTokenizer;
 
 import org.aspectj.tools.ajc.AjcTestCase;
 import org.aspectj.util.LangUtil;
@@ -63,43 +63,43 @@ public class OutputSpec {
                }
                boolean matches = false;
                int lineNo = 0;
-               StringTokenizer strTok = new StringTokenizer(output,"\n");
-               if (strTok.countTokens() == expectedOutputLines.size()) {
+               String[] actualOutputLines = getTrimmedLines(output);
+
+               if (actualOutputLines.length == expectedOutputLines.size()) {
                        matches = true;
-                       for (String line: expectedOutputLines) {
-                               lineNo++;
-                               String outputLine = strTok.nextToken().trim();
+                       for (String lineExpected : expectedOutputLines) {
+                               String lineFound = actualOutputLines[lineNo++];
                                /* Avoid trying to match on ajSandbox source names that appear in messages */
-                               if (!outputLine.contains(line)) {
+                               if (!lineFound.contains(lineExpected.trim())) {
                                        matches = false;
                                        break;
                                }
                        }
                } else { lineNo = -1; }
                if (!matches) {
-                       createFailureMessage(output, lineNo, strTok.countTokens());
+                       createFailureMessage(output, lineNo, actualOutputLines.length);
                }
        }
 
-       public void unorderedMatchAgainst(String output) {
-               List<String> outputFound = getOutputFound(output);
-               if(outputFound.size() != expectedOutputLines.size()) {
-                       createFailureMessage(output, -1, outputFound.size());
+       private void unorderedMatchAgainst(String output) {
+               List<String> actualOutputLines = Arrays.asList(getTrimmedLines(output));
+               int numberOfOutputLines = actualOutputLines.size();
+               if(numberOfOutputLines != expectedOutputLines.size()) {
+                       createFailureMessage(output, -1, numberOfOutputLines);
                        return;
                }
                List<String> expected = new ArrayList<>(expectedOutputLines);
-               List<String> found = new ArrayList<>(outputFound);
-               for (String lineFound : outputFound) {
+               List<String> found = new ArrayList<>(actualOutputLines);
+               for (String lineFound : actualOutputLines) {
                        for (String lineExpected : expectedOutputLines) {
-                               if (lineFound.contains(lineExpected)) {
+                               if (lineFound.contains(lineExpected.trim())) {
                                        found.remove(lineFound);
                                        expected.remove(lineExpected);
-                                       continue;
                                }
                        }
                }
                if (!found.isEmpty() || !expected.isEmpty()) {
-                       createFailureMessage(output,-2,outputFound.size());
+                       createFailureMessage(output, -2, numberOfOutputLines);
                }
        }
 
@@ -121,13 +121,8 @@ public class OutputSpec {
                AjcTestCase.fail(failMessage.toString());
        }
 
-       private List<String> getOutputFound(String output) {
-               List<String> found = new ArrayList<>();
-               StringTokenizer strTok = new StringTokenizer(output,"\n");
-               while(strTok.hasMoreTokens()) {
-                       String outputLine = strTok.nextToken().trim();
-                       found.add(outputLine);
-               }
-               return found;
+       private String[] getTrimmedLines(String text) {
+               // Remove leading/trailing empty lines and leading/trailing whitespace from each line
+               return text.trim().split("\\s*\n\\s*");
        }
 }
index 89df1e537bffbd6c409ec111ec3c08590acec19a..5dc4308721513944f57d2b56c157f0ae51b39a83 100644 (file)
@@ -1,9 +1,9 @@
 public class Code {
   public static void main(String[] argv) {
+    // Caveat: Putting the closing '"""' on a separate line adds a line break and 'println' (not 'print'!) adds another.
     System.out.println("""
                       this is a text
                        block
                        """);
   }
 }
-
index f0b39e08bdf2bd88107a6ca12cd314d9b33bd289..7020b67a5619b5cc2ba3613c1f0cebfdefc60170 100644 (file)
@@ -6,10 +6,10 @@ public class Code2 {
 aspect X {
 
   before(): execution(* Code2.main(..)) {
+    // Caveat: Putting the closing '"""' on a separate line adds a line break and 'println' (not 'print'!) adds another.
     System.out.println("""
                       this is a text
                        block in advice
                        """);
   }
 }
-