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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  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. public class OutputSpec {
  19. private List<String> expectedOutputLines = new ArrayList<>();
  20. public void addLine(OutputLine line) {
  21. if (line.getVm() == null || matchesThisVm(line.getVm())) {
  22. expectedOutputLines.add(line.getText());
  23. }
  24. }
  25. /**
  26. * For a test output line that has specified a vm version, check if it matches the vm we are running on.
  27. * vm might be "1.2,1.3,1.4,1.5" or simply "9" or it may be a version with a '+' suffix indicating that
  28. * level or later, e.g. "9+" should be ok on Java 10
  29. * @return true if the current vm version matches the spec
  30. */
  31. private boolean matchesThisVm(String vm) {
  32. // vm might be 1.2, 1.3, 1.4, 1.5 or 1.9 possibly with a '+' in there
  33. // For now assume + is attached to there only being one version, like "9+"
  34. // System.out.println("Checking "+vm+" for "+LangUtil.getVmVersionString());
  35. String v = LangUtil.getVmVersionString();
  36. if (v.endsWith(".0")) {
  37. v = v.substring(0,v.length()-2);
  38. }
  39. if (vm.contains(v)) {
  40. return true;
  41. }
  42. if (vm.endsWith("+")) {
  43. double vmVersion = LangUtil.getVmVersion();
  44. double vmSpecified = Double.parseDouble(vm.substring(0,vm.length()-1));
  45. return vmVersion >= vmSpecified;
  46. }
  47. return false;
  48. }
  49. public void matchAgainst(String output) {
  50. matchAgainst(output, "yes");
  51. }
  52. public void matchAgainst(String output, String ordered) {
  53. if (ordered != null && ordered.equals("no")) {
  54. unorderedMatchAgainst(output);
  55. return;
  56. }
  57. boolean matches = false;
  58. int lineNo = 0;
  59. String[] actualOutputLines = getTrimmedLines(output);
  60. if (actualOutputLines.length == expectedOutputLines.size()) {
  61. matches = true;
  62. for (String lineExpected : expectedOutputLines) {
  63. String lineFound = actualOutputLines[lineNo++];
  64. /* Avoid trying to match on ajSandbox source names that appear in messages */
  65. if (!lineFound.contains(lineExpected.trim())) {
  66. matches = false;
  67. break;
  68. }
  69. }
  70. } else { lineNo = -1; }
  71. if (!matches) {
  72. createFailureMessage(output, lineNo, actualOutputLines.length);
  73. }
  74. }
  75. private void unorderedMatchAgainst(String output) {
  76. List<String> actualOutputLines = Arrays.asList(getTrimmedLines(output));
  77. int numberOfOutputLines = actualOutputLines.size();
  78. if(numberOfOutputLines != expectedOutputLines.size()) {
  79. createFailureMessage(output, -1, numberOfOutputLines);
  80. return;
  81. }
  82. List<String> expected = new ArrayList<>(expectedOutputLines);
  83. List<String> found = new ArrayList<>(actualOutputLines);
  84. for (String lineFound : actualOutputLines) {
  85. for (String lineExpected : expectedOutputLines) {
  86. if (lineFound.contains(lineExpected.trim())) {
  87. found.remove(lineFound);
  88. expected.remove(lineExpected);
  89. }
  90. }
  91. }
  92. if (!found.isEmpty() || !expected.isEmpty()) {
  93. createFailureMessage(output, -2, numberOfOutputLines);
  94. }
  95. }
  96. private void createFailureMessage(String output, int lineNo, int sizeFound) {
  97. StringBuffer failMessage = new StringBuffer();
  98. failMessage.append("\n expecting output:\n");
  99. for (String line: expectedOutputLines) {
  100. failMessage.append(line+"\n");
  101. }
  102. failMessage.append(" but found output:\n");
  103. failMessage.append(output);
  104. failMessage.append("\n");
  105. if (lineNo==-1) {
  106. failMessage.append("Expected "+expectedOutputLines.size()+" lines of output but there are "+sizeFound);
  107. } else if (lineNo >= 0) {
  108. failMessage.append("First difference is on line " + lineNo);
  109. }
  110. failMessage.append("\n");
  111. AjcTestCase.fail(failMessage.toString());
  112. }
  113. private String[] getTrimmedLines(String text) {
  114. // Remove leading/trailing empty lines and leading/trailing whitespace from each line
  115. String[] trimmedLines = text.trim().split("\\s*\n\\s*");
  116. return trimmedLines.length == 1 && trimmedLines[0].equals("") ? new String[0] : trimmedLines;
  117. }
  118. }