Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

OutputSpec.java 3.6KB

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