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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. public class OutputSpec {
  19. private List<String> expectedOutputLines = new ArrayList<String>();
  20. public void addLine(OutputLine line) {
  21. expectedOutputLines.add(line.getText());
  22. }
  23. public void matchAgainst(String output) {
  24. matchAgainst(output, "yes");
  25. }
  26. public void matchAgainst(String output, String ordered) {
  27. if (ordered != null && ordered.equals("no")) {
  28. unorderedMatchAgainst(output);
  29. return;
  30. }
  31. boolean matches = false;
  32. int lineNo = 0;
  33. StringTokenizer strTok = new StringTokenizer(output,"\n");
  34. if (strTok.countTokens() == expectedOutputLines.size()) {
  35. matches = true;
  36. for (String line: expectedOutputLines) {
  37. lineNo++;
  38. String outputLine = strTok.nextToken().trim();
  39. /* Avoid trying to match on ajSandbox source names that appear in messages */
  40. if (outputLine.indexOf(line) == -1) {
  41. matches = false;
  42. break;
  43. }
  44. }
  45. } else { lineNo = -1; }
  46. if (!matches) {
  47. createFailureMessage(output, lineNo, strTok.countTokens());
  48. }
  49. }
  50. public void unorderedMatchAgainst(String output) {
  51. List<String> outputFound = getOutputFound(output);
  52. if(outputFound.size() != expectedOutputLines.size()) {
  53. createFailureMessage(output, -1, outputFound.size());
  54. return;
  55. }
  56. List<String> expected = new ArrayList<String>();
  57. expected.addAll(expectedOutputLines);
  58. List<String> found = new ArrayList<String>();
  59. found.addAll(outputFound);
  60. for (Iterator<String> iterator = outputFound.iterator(); iterator.hasNext();) {
  61. String lineFound = (String) iterator.next();
  62. for (Iterator<String> iterator2 = expectedOutputLines.iterator(); iterator2.hasNext();) {
  63. String lineExpected = (String) iterator2.next();
  64. if (lineFound.indexOf(lineExpected)!= -1) {
  65. found.remove(lineFound);
  66. expected.remove(lineExpected);
  67. continue;
  68. }
  69. }
  70. }
  71. if (!found.isEmpty() || !expected.isEmpty()) {
  72. createFailureMessage(output,-2,outputFound.size());
  73. }
  74. }
  75. private void createFailureMessage(String output, int lineNo, int sizeFound) {
  76. StringBuffer failMessage = new StringBuffer();
  77. failMessage.append("\n expecting output:\n");
  78. for (String line: expectedOutputLines) {
  79. failMessage.append(line);
  80. failMessage.append("\n");
  81. }
  82. failMessage.append(" but found output:\n");
  83. failMessage.append(output);
  84. failMessage.append("\n");
  85. if (lineNo==-1) {
  86. failMessage.append("Expected "+expectedOutputLines.size()+" lines of output but there are "+sizeFound);
  87. } else if (lineNo >= 0) {
  88. failMessage.append("First difference is on line " + lineNo);
  89. }
  90. failMessage.append("\n");
  91. AjcTestCase.fail(failMessage.toString());
  92. }
  93. private List<String> getOutputFound(String output) {
  94. List<String> found = new ArrayList<String>();
  95. StringTokenizer strTok = new StringTokenizer(output,"\n");
  96. while(strTok.hasMoreTokens()) {
  97. String outputLine = strTok.nextToken().trim();
  98. found.add(outputLine);
  99. }
  100. return found;
  101. }
  102. }