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

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 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.Iterator;
  14. import java.util.List;
  15. import java.util.ArrayList;
  16. import java.util.StringTokenizer;
  17. import org.aspectj.tools.ajc.AjcTestCase;
  18. import org.aspectj.util.LangUtil;
  19. public class OutputSpec {
  20. private List<String> expectedOutputLines = new ArrayList<String>();
  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 a vm version, check if it matches the vm we are running on.
  28. * 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
  29. * level or later, e.g. "9+" should be ok on Java 10
  30. * @return true if the current vm version matches the spec
  31. */
  32. private boolean matchesThisVm(String vm) {
  33. // vm might be 1.2, 1.3, 1.4, 1.5 or 1.9 possibly with a '+' in there
  34. // For now assume + is attached to there only being one version, like "9+"
  35. if (vm.contains(LangUtil.getVmVersionString())) {
  36. return true;
  37. }
  38. if (vm.endsWith("+")) {
  39. double vmVersion = LangUtil.getVmVersion();
  40. double vmSpecified = Double.parseDouble(vm.substring(0,vm.length()-1));
  41. return vmVersion >= vmSpecified;
  42. }
  43. return false;
  44. }
  45. public void matchAgainst(String output) {
  46. matchAgainst(output, "yes");
  47. }
  48. public void matchAgainst(String output, String ordered) {
  49. if (ordered != null && ordered.equals("no")) {
  50. unorderedMatchAgainst(output);
  51. return;
  52. }
  53. boolean matches = false;
  54. int lineNo = 0;
  55. StringTokenizer strTok = new StringTokenizer(output,"\n");
  56. if (strTok.countTokens() == expectedOutputLines.size()) {
  57. matches = true;
  58. for (String line: expectedOutputLines) {
  59. lineNo++;
  60. String outputLine = strTok.nextToken().trim();
  61. /* Avoid trying to match on ajSandbox source names that appear in messages */
  62. if (outputLine.indexOf(line) == -1) {
  63. matches = false;
  64. break;
  65. }
  66. }
  67. } else { lineNo = -1; }
  68. if (!matches) {
  69. createFailureMessage(output, lineNo, strTok.countTokens());
  70. }
  71. }
  72. public void unorderedMatchAgainst(String output) {
  73. List<String> outputFound = getOutputFound(output);
  74. if(outputFound.size() != expectedOutputLines.size()) {
  75. createFailureMessage(output, -1, outputFound.size());
  76. return;
  77. }
  78. List<String> expected = new ArrayList<String>();
  79. expected.addAll(expectedOutputLines);
  80. List<String> found = new ArrayList<String>();
  81. found.addAll(outputFound);
  82. for (Iterator<String> iterator = outputFound.iterator(); iterator.hasNext();) {
  83. String lineFound = (String) iterator.next();
  84. for (Iterator<String> iterator2 = expectedOutputLines.iterator(); iterator2.hasNext();) {
  85. String lineExpected = (String) iterator2.next();
  86. if (lineFound.indexOf(lineExpected)!= -1) {
  87. found.remove(lineFound);
  88. expected.remove(lineExpected);
  89. continue;
  90. }
  91. }
  92. }
  93. if (!found.isEmpty() || !expected.isEmpty()) {
  94. createFailureMessage(output,-2,outputFound.size());
  95. }
  96. }
  97. private void createFailureMessage(String output, int lineNo, int sizeFound) {
  98. StringBuffer failMessage = new StringBuffer();
  99. failMessage.append("\n expecting output:\n");
  100. for (String line: expectedOutputLines) {
  101. failMessage.append(line);
  102. failMessage.append("\n");
  103. }
  104. failMessage.append(" but found output:\n");
  105. failMessage.append(output);
  106. failMessage.append("\n");
  107. if (lineNo==-1) {
  108. failMessage.append("Expected "+expectedOutputLines.size()+" lines of output but there are "+sizeFound);
  109. } else if (lineNo >= 0) {
  110. failMessage.append("First difference is on line " + lineNo);
  111. }
  112. failMessage.append("\n");
  113. AjcTestCase.fail(failMessage.toString());
  114. }
  115. private List<String> getOutputFound(String output) {
  116. List<String> found = new ArrayList<String>();
  117. StringTokenizer strTok = new StringTokenizer(output,"\n");
  118. while(strTok.hasMoreTokens()) {
  119. String outputLine = strTok.nextToken().trim();
  120. found.add(outputLine);
  121. }
  122. return found;
  123. }
  124. }