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.

DirChangesTest.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.testing.harness.bridge;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.io.PrintWriter;
  17. import java.io.StringWriter;
  18. import org.aspectj.bridge.IMessage;
  19. import org.aspectj.bridge.MessageHandler;
  20. import org.aspectj.bridge.MessageUtil;
  21. import org.aspectj.testing.xml.MessageListXmlReader;
  22. import org.aspectj.testing.xml.XMLWriter;
  23. import org.aspectj.util.LangUtil;
  24. import junit.framework.AssertionFailedError;
  25. import junit.framework.TestCase;
  26. /**
  27. * (broken) tests for unutilized feature to read in
  28. * expected directory changes as messages.
  29. */
  30. public class DirChangesTest extends TestCase {
  31. private static final boolean PRINTING = false;
  32. /** name of file in srcBaseDir with any expected messages */
  33. public static final String EXPECTED_NAME = "expectedMessages.xml";
  34. public DirChangesTest(String name) {
  35. super(name);
  36. }
  37. /* XXX update tests to read expected messages from files
  38. * then just iterate over directories in dirChangesTestDir
  39. */
  40. /**
  41. * Uses testdata/dirChangesTestDir/same
  42. */
  43. public void skip_testSameExpDir() {
  44. doCheck("same");
  45. }
  46. public void testNothingForAntJUnit() {}
  47. /**
  48. * Uses testdata/dirChangesTestDir/diff
  49. */
  50. public void skip_testDiffExpDir() {
  51. doCheck("diff");
  52. }
  53. public void skip_testWriteEmpty() {
  54. DirChanges.Spec spec = new DirChanges.Spec();
  55. String expected = "";
  56. checkWrite(spec, expected);
  57. }
  58. public void skip_testWriteExpDir() {
  59. DirChanges.Spec spec = new DirChanges.Spec();
  60. spec.setExpDir("expected directory");
  61. String expected =
  62. "<dir-changes expDir=\"expected directory\"/>"
  63. + LangUtil.EOL;
  64. checkWrite(spec, expected);
  65. }
  66. public void skip_testWriteAdded() {
  67. DirChanges.Spec spec = new DirChanges.Spec();
  68. spec.setAdded("one,two,three");
  69. String expected =
  70. "<dir-changes added=\"one,two,three\"/>"
  71. + LangUtil.EOL;
  72. checkWrite(spec, expected);
  73. }
  74. /** write spec to XML and compare with expected */
  75. private void checkWrite(DirChanges.Spec spec, String expected) {
  76. StringWriter actual = new StringWriter();
  77. XMLWriter writer = new XMLWriter(new PrintWriter(actual, true));
  78. spec.writeXml(writer);
  79. assertEquals(expected, actual.toString());
  80. }
  81. private void doCheck(String dir) {
  82. DirChanges.Spec spec = new DirChanges.Spec();
  83. File srcBaseDir = new File("testdata/dirChangesTestDir/" + dir);
  84. // actual = baseDir
  85. File baseDir = new File(srcBaseDir, "actual");
  86. // expected = srcBaseDir + spec.expDir
  87. spec.setExpDir("expected");
  88. IMessage[] expected = null;
  89. File expMssgFile = new File(srcBaseDir, EXPECTED_NAME);
  90. if (expMssgFile.canRead()) {
  91. try {
  92. expected = new MessageListXmlReader().readMessages(expMssgFile);
  93. } catch (IOException e) {
  94. System.err.println("Continuing after error reading " + expMssgFile);
  95. e.printStackTrace(System.err);
  96. }
  97. }
  98. checkDirChanges(spec, baseDir, srcBaseDir, null, expected);
  99. }
  100. // XXX WEAK upgrade to read expected-diffs from file in directory
  101. private void checkDirChanges(
  102. DirChanges.Spec spec,
  103. File baseDir,
  104. File srcBaseDir,
  105. Runnable dirChanger,
  106. IMessage[] expected) {
  107. DirChanges dc = new DirChanges(spec);
  108. MessageHandler handler = new MessageHandler();
  109. try {
  110. if (!dc.start(handler, baseDir)) {
  111. //assertTrue(!shouldPass);
  112. assertSameMessages(expected, handler);
  113. return; // exiting after (XXX) undertested expected failure?
  114. } else {
  115. assertTrue(0 == handler.numMessages(IMessage.ERROR, true));
  116. }
  117. if (null != dirChanger) {
  118. dirChanger.run();
  119. }
  120. if (!dc.end(handler, srcBaseDir)) {
  121. //assertTrue(!shouldPass);
  122. assertSameMessages(expected, handler);
  123. } else {
  124. assertTrue(0 == handler.numMessages(IMessage.ERROR, true));
  125. }
  126. } catch (Throwable t) {
  127. if (PRINTING) {
  128. t.printStackTrace(System.err);
  129. }
  130. throw new AssertionFailedError(LangUtil.renderException(t));
  131. } finally {
  132. if (0 < handler.numMessages(null, true)) {
  133. if (PRINTING) {
  134. MessageUtil.print(System.err, handler, "checkDirChanges: ");
  135. }
  136. IMessage[] toprint = handler.getMessages(null, true);
  137. File output = new File(srcBaseDir, EXPECTED_NAME);
  138. try {
  139. //toprint[0].getISourceLocation().getSourceFile();
  140. System.out.println("XXX writing to " + output
  141. + " messages " + LangUtil.arrayAsList(toprint));
  142. new MessageListXmlReader().writeMessages(output, toprint);
  143. } catch (IOException e) {
  144. System.err.println("Error writing to " + output
  145. + " messages " + LangUtil.arrayAsList(toprint));
  146. e.printStackTrace(System.err);
  147. }
  148. }
  149. }
  150. }
  151. /**
  152. * Assert unless messages in handler match all expected messages.
  153. * @param expected
  154. * @param handler
  155. */
  156. private void assertSameMessages(
  157. IMessage[] expected,
  158. MessageHandler handler) {
  159. IMessage[] actual = handler.getMessages(null, true);
  160. for (int i = 0; i < actual.length; i++) {
  161. int found = find(actual[i], expected);
  162. if (-1 != found) {
  163. expected[found] = null;
  164. actual[i] = null;
  165. }
  166. }
  167. StringBuffer sb = new StringBuffer();
  168. {
  169. IMessage[] expNotFound = (IMessage[])
  170. LangUtil.safeCopy(expected, new IMessage[0]);
  171. if (0 < expNotFound.length) {
  172. sb.append("expected not found: ");
  173. sb.append(LangUtil.arrayAsList(expNotFound).toString());
  174. }
  175. }
  176. {
  177. IMessage[] actFound = (IMessage[])
  178. LangUtil.safeCopy(actual, new IMessage[0]);
  179. if (0 < actFound.length) {
  180. sb.append(" not expected but found: ");
  181. sb.append(LangUtil.arrayAsList(actFound).toString());
  182. }
  183. }
  184. if (0 < sb.length()) {
  185. assertTrue(sb.toString(), false);
  186. }
  187. }
  188. /**
  189. * Find message in array, comparing by contents
  190. * but ignoring exceptions thrown and source location XXX.
  191. * @param message the IMessage to find
  192. * @param expected the
  193. * @return int
  194. */
  195. private int find(IMessage message, IMessage[] expected) {
  196. if ((null != expected) && (0 != expected.length)
  197. && (null != message)) {
  198. final IMessage.Kind kind = message.getKind();
  199. final String text = message.getMessage();
  200. for (int i = 0; i < expected.length; i++) {
  201. IMessage exp = expected[i];
  202. if (null != exp) {
  203. if (kind.equals(exp.getKind())
  204. && text.equals(exp.getMessage())) {
  205. return i;
  206. }
  207. }
  208. }
  209. }
  210. return -1;
  211. }
  212. }