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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.List;
  15. import java.util.StringTokenizer;
  16. import org.aspectj.tools.ajc.AjcTestCase;
  17. import org.aspectj.util.LangUtil;
  18. public class OutputSpec {
  19. private List<String> expectedOutputLines = new ArrayList<String>();
  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. if (vm.contains(LangUtil.getVmVersionString())) {
  35. return true;
  36. }
  37. if (vm.endsWith("+")) {
  38. double vmVersion = LangUtil.getVmVersion();
  39. double vmSpecified = Double.parseDouble(vm.substring(0,vm.length()-1));
  40. return vmVersion >= vmSpecified;
  41. }
  42. return false;
  43. }
  44. public void matchAgainst(String output) {
  45. matchAgainst(output, "yes");
  46. }
  47. public void matchAgainst(String output, String ordered) {
  48. if (ordered != null && ordered.equals("no")) {
  49. unorderedMatchAgainst(output);
  50. return;
  51. }
  52. boolean matches = false;
  53. int lineNo = 0;
  54. StringTokenizer strTok = new StringTokenizer(output,"\n");
  55. if (strTok.countTokens() == expectedOutputLines.size()) {
  56. matches = true;
  57. for (String line: expectedOutputLines) {
  58. lineNo++;
  59. String outputLine = strTok.nextToken().trim();
  60. /* Avoid trying to match on ajSandbox source names that appear in messages */
  61. if (!outputLine.contains(line)) {
  62. matches = false;
  63. break;
  64. }
  65. }
  66. } else { lineNo = -1; }
  67. if (!matches) {
  68. createFailureMessage(output, lineNo, strTok.countTokens());
  69. }
  70. }
  71. public void unorderedMatchAgainst(String output) {
  72. List<String> outputFound = getOutputFound(output);
  73. if(outputFound.size() != expectedOutputLines.size()) {
  74. createFailureMessage(output, -1, outputFound.size());
  75. return;
  76. }
  77. List<String> expected = new ArrayList<String>();
  78. expected.addAll(expectedOutputLines);
  79. List<String> found = new ArrayList<String>();
  80. found.addAll(outputFound);
  81. for (String lineFound : outputFound) {
  82. for (String lineExpected : expectedOutputLines) {
  83. if (lineFound.contains(lineExpected)) {
  84. found.remove(lineFound);
  85. expected.remove(lineExpected);
  86. continue;
  87. }
  88. }
  89. }
  90. if (!found.isEmpty() || !expected.isEmpty()) {
  91. createFailureMessage(output,-2,outputFound.size());
  92. }
  93. }
  94. private void createFailureMessage(String output, int lineNo, int sizeFound) {
  95. StringBuffer failMessage = new StringBuffer();
  96. failMessage.append("\n expecting output:\n");
  97. for (String line: expectedOutputLines) {
  98. failMessage.append(line+"\n");
  99. }
  100. failMessage.append(" but found output:\n");
  101. failMessage.append(output);
  102. failMessage.append("\n");
  103. if (lineNo==-1) {
  104. failMessage.append("Expected "+expectedOutputLines.size()+" lines of output but there are "+sizeFound);
  105. } else if (lineNo >= 0) {
  106. failMessage.append("First difference is on line " + lineNo);
  107. }
  108. failMessage.append("\n");
  109. AjcTestCase.fail(failMessage.toString());
  110. }
  111. private List<String> getOutputFound(String output) {
  112. List<String> found = new ArrayList<String>();
  113. StringTokenizer strTok = new StringTokenizer(output,"\n");
  114. while(strTok.hasMoreTokens()) {
  115. String outputLine = strTok.nextToken().trim();
  116. found.add(outputLine);
  117. }
  118. return found;
  119. }
  120. }