You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OutputSpec.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* *******************************************************************
  2. * Copyright (c) 2005 IBM Corporation
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * Adrian Colyer,
  11. * ******************************************************************/
  12. package org.aspectj.testing;
  13. import java.util.ArrayList;
  14. import java.util.Arrays;
  15. import java.util.List;
  16. import org.aspectj.tools.ajc.AjcTestCase;
  17. import org.aspectj.util.LangUtil;
  18. import static java.lang.Double.parseDouble;
  19. public class OutputSpec {
  20. private List<String> expectedOutputLines = new ArrayList<>();
  21. public void addLine(OutputLine line) {
  22. if (line.getVm() == null || matchesThisVm(line.getVm())) {
  23. expectedOutputLines.add(line.getText());
  24. }
  25. }
  26. /**
  27. * For a test output line that has specified list of JVM version ranges, determine whether the JVM we are running on
  28. * matches at least one of those ranges.
  29. *
  30. * @param vmVersionRanges might be a single version like "9", a list of versions like "1.2,1.3,1.4,1.5", an equivalent
  31. * range of "1.2-1.5", an open range like "-1.8", "9-" (equivalent to "9+") or a more complex list of ranges
  32. * like "-1.6,9-11,13-14,17-" or "8,11,16+". Empty ranges like in "", " ", "8,,14", ",5", "6-," will be
  33. * ignored. I.e., they will not yield a positive match. Bogus ranges like "9-11-14" will be ignored, too.
  34. *
  35. * @return true if the current vmVersionRanges version matches the spec
  36. */
  37. private boolean matchesThisVm(String vmVersionRanges) {
  38. double currentVmVersion = LangUtil.getVmVersion();
  39. return Arrays.stream(vmVersionRanges.split(","))
  40. .map(String::trim)
  41. .filter(range -> !range.isEmpty())
  42. .map(range -> range
  43. .replaceFirst("^([0-9.]+)$", "$1-$1") // single version 'n' to range 'n-n'
  44. .replaceFirst("^-", "0-") // left open range '-n' to '0-n'
  45. .replaceFirst("[+-]$", "-99999") // right open range 'n-' or 'n+' to 'n-99999'
  46. .split("-") // range 'n-m' to array ['n', 'm']
  47. )
  48. .filter(range -> range.length == 2)
  49. .map(range -> new double[] { parseDouble(range[0]), parseDouble(range[1]) })
  50. //.filter(range -> { System.out.println(range[0] + " - " +range[1]); return true; })
  51. .anyMatch(range -> range[0] <= currentVmVersion && range[1] >= currentVmVersion);
  52. }
  53. public void matchAgainst(String output) {
  54. matchAgainst(output, "yes");
  55. }
  56. public void matchAgainst(String output, String ordered) {
  57. if (ordered != null && ordered.equals("no")) {
  58. unorderedMatchAgainst(output);
  59. return;
  60. }
  61. boolean matches = false;
  62. int lineNo = 0;
  63. String[] actualOutputLines = getTrimmedLines(output);
  64. if (actualOutputLines.length == expectedOutputLines.size()) {
  65. matches = true;
  66. for (String lineExpected : expectedOutputLines) {
  67. String lineFound = actualOutputLines[lineNo++];
  68. /* Avoid trying to match on ajSandbox source names that appear in messages */
  69. if (!lineFound.contains(lineExpected.trim())) {
  70. matches = false;
  71. break;
  72. }
  73. }
  74. } else { lineNo = -1; }
  75. if (!matches) {
  76. createFailureMessage(output, lineNo, actualOutputLines.length);
  77. }
  78. }
  79. private void unorderedMatchAgainst(String output) {
  80. List<String> actualOutputLines = Arrays.asList(getTrimmedLines(output));
  81. int numberOfOutputLines = actualOutputLines.size();
  82. if(numberOfOutputLines != expectedOutputLines.size()) {
  83. createFailureMessage(output, -1, numberOfOutputLines);
  84. return;
  85. }
  86. List<String> expected = new ArrayList<>(expectedOutputLines);
  87. List<String> found = new ArrayList<>(actualOutputLines);
  88. for (String lineFound : actualOutputLines) {
  89. for (String lineExpected : expectedOutputLines) {
  90. if (lineFound.contains(lineExpected.trim())) {
  91. found.remove(lineFound);
  92. expected.remove(lineExpected);
  93. }
  94. }
  95. }
  96. if (!found.isEmpty() || !expected.isEmpty()) {
  97. createFailureMessage(output, -2, numberOfOutputLines);
  98. }
  99. }
  100. private void createFailureMessage(String output, int lineNo, int sizeFound) {
  101. StringBuilder failMessage = new StringBuilder();
  102. failMessage.append("\n expecting output:\n");
  103. for (String line: expectedOutputLines) {
  104. failMessage.append(line+"\n");
  105. }
  106. failMessage.append(" but found output:\n");
  107. failMessage.append(output);
  108. failMessage.append("\n");
  109. if (lineNo==-1) {
  110. failMessage.append("Expected "+expectedOutputLines.size()+" lines of output but there are "+sizeFound);
  111. } else if (lineNo >= 0) {
  112. failMessage.append("First difference is on line " + lineNo);
  113. }
  114. failMessage.append("\n");
  115. AjcTestCase.fail(failMessage.toString());
  116. }
  117. private String[] getTrimmedLines(String text) {
  118. // Remove leading/trailing empty lines and leading/trailing whitespace from each line
  119. String[] trimmedLines = text.trim().split("\\s*\n\\s*");
  120. return trimmedLines.length == 1 && trimmedLines[0].equals("") ? new String[0] : trimmedLines;
  121. }
  122. }