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.

AjcSpecTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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.util.ArrayList;
  15. import java.util.Arrays;
  16. import java.util.Iterator;
  17. import java.util.List;
  18. import java.util.ListIterator;
  19. import org.aspectj.bridge.IMessage;
  20. import org.aspectj.bridge.IMessageHolder;
  21. import org.aspectj.bridge.ISourceLocation;
  22. import org.aspectj.bridge.MessageUtil;
  23. import junit.framework.Assert;
  24. import junit.framework.AssertionFailedError;
  25. import junit.framework.TestCase;
  26. /**
  27. * Primarily used by others to test AjcTest
  28. */
  29. public class AjcSpecTest extends TestCase {
  30. public static final String NOTSAME = " != ";
  31. public static void sameAjcSuiteSpec(
  32. AjcTest.Suite.Spec lhsSpec,
  33. AjcTest.Suite.Spec rhsSpec,
  34. Assert a) {
  35. assertNotNull(lhsSpec);
  36. assertNotNull(rhsSpec);
  37. Iterator lhs = lhsSpec.getChildren().iterator();
  38. Iterator rhs = rhsSpec.getChildren().iterator();
  39. while (lhs.hasNext() && rhs.hasNext()) {
  40. AjcTest.Spec lhsTest = (AjcTest.Spec) lhs.next();
  41. AjcTest.Spec rhsTest = (AjcTest.Spec) rhs.next();
  42. AjcSpecTest.sameAjcTestSpec(lhsTest, rhsTest, a);
  43. }
  44. assertTrue(!lhs.hasNext());
  45. assertTrue(!rhs.hasNext());
  46. }
  47. public static void sameAjcTestSpec(
  48. AjcTest.Spec lhsTest,
  49. AjcTest.Spec rhsTest,
  50. Assert a) {
  51. assertNotNull(lhsTest);
  52. assertNotNull(rhsTest);
  53. assertEquals(lhsTest.getBugId(), rhsTest.getBugId());
  54. assertEquals(lhsTest.getTestDirOffset(), rhsTest.getTestDirOffset());
  55. // XXX suiteDir varies by run..
  56. // description, options, paths, comments, keywords
  57. sameAbstractRunSpec(lhsTest, rhsTest, a);
  58. }
  59. public static void sameAbstractRunSpec(
  60. AbstractRunSpec lhs,
  61. AbstractRunSpec rhs,
  62. Assert a) {
  63. assertEquals(lhs.description, rhs.description);
  64. // XXX keywords added in .txt reading -
  65. //sameList(lhs.getKeywordsList(), rhs.getKeywordsList(), a);
  66. // XXX sameList(lhs.globalOptions, rhs.globalOptions, a);
  67. sameList(lhs.getOptionsList(), rhs.getOptionsList(), a);
  68. sameList(lhs.getPathsList(), rhs.getPathsList(), a);
  69. assertEquals(lhs.isStaging(), rhs.isStaging());
  70. sameList(lhs.keywords, rhs.keywords, a);
  71. assertEquals(lhs.comment, rhs.comment);
  72. assertEquals(lhs.badInput, rhs.badInput);
  73. // xml adds sourceloc?
  74. //sameSourceLocation(lhs.getSourceLocation(), rhs.getSourceLocation(), a);
  75. // XXX also sourceLocations?
  76. sameMessages(lhs.getMessages(), rhs.getMessages(), a);
  77. }
  78. /** @return normal form - null is "", "" is "", and others are {fully.qualified.class}.toString().trim() */
  79. static String normal(Object input) {
  80. if ((null == input) || ("".equals(input))) {
  81. return "";
  82. } else {
  83. return input.getClass().getName() + "." + input.toString().trim();
  84. }
  85. }
  86. /** @return true if these match after normalizing */
  87. public static void same(Object lhs, Object rhs, Assert a) {
  88. lhs = normal(lhs);
  89. rhs = normal(rhs);
  90. assertTrue(lhs + NOTSAME + rhs, lhs.equals(rhs));
  91. }
  92. /** @return true if both are empty (null or no entries) or if all match */
  93. public static void sameRA(String[] lhs, String[] rhs, Assert a) {
  94. if (null == lhs) {
  95. assertTrue((null == rhs) || (0 == rhs.length));
  96. } else if (null == rhs) {
  97. assertTrue(0 == lhs.length);
  98. } else {
  99. String l = normal(lhs);
  100. String r = normal(rhs);
  101. assertTrue(l + NOTSAME + r, l.equals(r));
  102. }
  103. }
  104. /** @return normal form for String[] items*/
  105. static String normal(String[] items) {
  106. return (null == items ? "[]" : normal(Arrays.asList(items)));
  107. }
  108. /** @return normal form for list items */
  109. static String normal(List list) {
  110. StringBuffer sb = new StringBuffer();
  111. sb.append("[");
  112. boolean first = true;
  113. for (Iterator iter = list.iterator(); iter.hasNext();) {
  114. Object o = iter.next();
  115. if (!first) {
  116. sb.append(", ");
  117. } else {
  118. first = false;
  119. }
  120. sb.append(normal(o));
  121. }
  122. sb.append("]");
  123. return sb.toString();
  124. }
  125. /** @return true if both are empty (null or no entries) or if all match after trimming */
  126. public static void sameListSize(List lhs, List rhs) {
  127. if (null == lhs) {
  128. assertTrue((null == rhs) || (0 == rhs.size()));
  129. } else if (null == rhs) {
  130. assertTrue(0 == lhs.size());
  131. } else {
  132. assertTrue(rhs.size() == lhs.size());
  133. }
  134. }
  135. /** @return true if both are empty (null or no entries) or if all match after trimming */
  136. public static void sameList(List lhs, List rhs, Assert a) {
  137. sameListSize(lhs, rhs);
  138. String l = normal(lhs);
  139. String r = normal(rhs);
  140. String label = l + NOTSAME + r;
  141. assertTrue(label, l.equals(r));
  142. }
  143. // /**
  144. // * Normalize and compare:
  145. // * <li>bug id's are not compared since extracted during xml writing</li>
  146. // * <li>keyword compare is disabled since keywords are generated during xml reading.</li>
  147. // * <li>description compare is normalized by stripping bug ids</li>
  148. // * <li>String and arrays are equal when empty (null or 0-length)</li>
  149. // * @see Ajctest#stripBugId(String)
  150. // */
  151. // public static void sameAjcTest(AjcTest lhs, AjcTest rhs, Assert reporter) {
  152. // Assert a = reporter;
  153. // String label = lhs + NOTSAME + rhs;
  154. // assertTrue(label, null != lhs);
  155. // assertTrue(label, null != rhs);
  156. // //assertTrue(label, lhs.ignoreWarnings == rhs.ignoreWarnings);
  157. // // XXX disabled - not in .txt
  158. // // sameStringList(lhs.keywords, rhs.keywords, a);
  159. // // sameString(lhs.bugId, rhs.bugId, a);
  160. // // argh - bugid stripped from description
  161. // //same(AjcTest.stripBugId(lhs.description), AjcTest.stripBugId(lhs.description), a);
  162. // //sameRA(lhs.globals, rhs.globals, a);
  163. // //lhs.reset();
  164. // //rhs.reset();
  165. // boolean gotOne = false;
  166. // IMessageHolder holder = new MessageHandler();
  167. // assertTrue(label, !holder.hasAnyMessage(IMessage.FAIL, IMessageHolder.ORGREATER));
  168. // while (lhs.hasNextRun() && rhs.hasNextRun()) {
  169. // sameIAjcRun((IAjcRun) lhs.nextRun(holder), (IAjcRun) rhs.nextRun(holder), reporter);
  170. // assertTrue(label, !holder.hasAnyMessage(IMessage.FAIL, IMessageHolder.ORGREATER));
  171. // if (!gotOne) {
  172. // gotOne = true;
  173. // }
  174. // }
  175. // assertTrue(label, gotOne);
  176. // assertTrue(label, !lhs.hasNextRun());
  177. // assertTrue(label, !rhs.hasNextRun());
  178. // }
  179. public static void sameIAjcRun(IAjcRun lhs, IAjcRun rhs, Assert reporter) {
  180. // Assert a = reporter;
  181. assertTrue(lhs != null);
  182. assertTrue(rhs != null);
  183. Class c = lhs.getClass();
  184. assertTrue(c == rhs.getClass());
  185. AbstractRunSpec lhsSpec;
  186. AbstractRunSpec rhsSpec;
  187. if (c == CompilerRun.class) {
  188. CompilerRun.Spec l = ((CompilerRun) lhs).spec;
  189. CompilerRun.Spec r = ((CompilerRun) rhs).spec;
  190. lhsSpec = l;
  191. rhsSpec = r;
  192. assertEquals(l.argfiles, r.argfiles);
  193. assertEquals(l.aspectpath, r.aspectpath);
  194. assertEquals(l.testSrcDirOffset, r.testSrcDirOffset);
  195. assertEquals(l.compiler, r.compiler);
  196. assertEquals(l.includeClassesDir, r.includeClassesDir);
  197. assertEquals(l.reuseCompiler, r.reuseCompiler);
  198. assertEquals(l.sourceroots, r.sourceroots);
  199. assertEquals(l.extdirs, r.extdirs);
  200. } else if (c == JavaRun.class) {
  201. JavaRun.Spec l = ((JavaRun) lhs).spec;
  202. JavaRun.Spec r = ((JavaRun) rhs).spec;
  203. lhsSpec = l;
  204. rhsSpec = r;
  205. assertTrue(l.skipTester == r.skipTester);
  206. assertEquals(l.className, r.className);
  207. assertEquals(l.javaVersion, r.javaVersion);
  208. assertEquals(l.skipTester, r.skipTester);
  209. assertEquals(l.outStreamIsError, r.outStreamIsError);
  210. assertEquals(l.errStreamIsError, r.errStreamIsError);
  211. } else if (c == IncCompilerRun.class) {
  212. IncCompilerRun.Spec l = ((IncCompilerRun) lhs).spec;
  213. IncCompilerRun.Spec r = ((IncCompilerRun) rhs).spec;
  214. lhsSpec = l;
  215. rhsSpec = r;
  216. assertEquals(l.tag, r.tag);
  217. assertEquals(l.fresh, r.fresh);
  218. } else {
  219. assertTrue(lhs.equals(rhs));
  220. return;
  221. }
  222. sameSpec(lhsSpec, rhsSpec, reporter);
  223. }
  224. public static void sameSpec(AbstractRunSpec lhs, AbstractRunSpec rhs, Assert a) {
  225. if ((null == lhs) && (null == rhs)) {
  226. return;
  227. }
  228. assertTrue(lhs != null);
  229. assertTrue(rhs != null);
  230. assertEquals(""+lhs.getOptionsList(), ""+rhs.getOptionsList());
  231. sameList(lhs.getPathsList(), rhs.getPathsList(), a);
  232. sameMessages(lhs.getMessages(), rhs.getMessages(), a);
  233. sameDirChangesList(lhs.dirChanges, rhs.dirChanges, a);
  234. }
  235. public static void sameDirChangesList(ArrayList lhs, ArrayList rhs, Assert a) {
  236. if ((null == lhs) && (null == rhs)) {
  237. return;
  238. }
  239. assertTrue(rhs != null);
  240. assertTrue(lhs != null);
  241. sameListSize(lhs, rhs);
  242. Iterator lhsIter = lhs.iterator();
  243. Iterator rhsIter = rhs.iterator();
  244. while (lhsIter.hasNext() && rhsIter.hasNext()) {
  245. sameDirChangesSpec((DirChanges.Spec) lhsIter.next(), (DirChanges.Spec) rhsIter.next(), a);
  246. }
  247. }
  248. public static void sameDirChangesSpec(DirChanges.Spec lhs, DirChanges.Spec rhs, Assert a) {
  249. if ((null == lhs) && (null == rhs)) {
  250. return;
  251. }
  252. assertTrue(rhs != null);
  253. assertTrue(lhs != null);
  254. assertEquals(lhs.defaultSuffix, rhs.defaultSuffix);
  255. assertEquals(lhs.dirToken, rhs.dirToken);
  256. assertEquals(lhs.fastFail, rhs.fastFail);
  257. assertEquals(lhs.expDir, rhs.expDir); // XXX normalize?
  258. sameList(lhs.updated, rhs.updated, a);
  259. sameList(lhs.removed, rhs.removed, a);
  260. sameList(lhs.added, rhs.added, a);
  261. }
  262. public static void sameMessages(IMessageHolder one, IMessageHolder two, Assert a) {
  263. if ((null == one) && (null == two)) {
  264. return;
  265. }
  266. // order matters here
  267. ListIterator lhs = one.getUnmodifiableListView().listIterator();
  268. ListIterator rhs = two.getUnmodifiableListView().listIterator();
  269. while (lhs.hasNext() && rhs.hasNext()) {
  270. sameMessage((IMessage) lhs.next(), (IMessage) rhs.next(), a);
  271. }
  272. assertTrue(!lhs.hasNext());
  273. assertTrue(!rhs.hasNext());
  274. }
  275. public static void sameMessage(IMessage lhs, IMessage rhs, Assert a) {
  276. if ((null == lhs) && (null == rhs)) {
  277. return;
  278. }
  279. assertTrue(lhs != null);
  280. assertTrue(rhs != null);
  281. assertTrue(lhs.getKind() == rhs.getKind());
  282. same(lhs.getMessage(), rhs.getMessage(), a);
  283. same(lhs.getDetails(), rhs.getDetails(), a);
  284. assertEquals(lhs.getThrown(), rhs.getThrown());
  285. sameSourceLocation(lhs.getSourceLocation(), rhs.getSourceLocation());
  286. sameSourceLocations(lhs.getExtraSourceLocations(), rhs.getExtraSourceLocations());
  287. }
  288. public static void sameSourceLocations(List lhs, List rhs) {
  289. sameListSize(lhs, rhs);
  290. if ((null == lhs) || (0 == lhs.size())) {
  291. return;
  292. }
  293. // ok, do order-dependent check..
  294. ListIterator iterLeft = lhs.listIterator();
  295. ListIterator iterRight = rhs.listIterator();
  296. while (iterLeft.hasNext() && iterRight.hasNext()) {
  297. ISourceLocation left = (ISourceLocation) iterLeft.next();
  298. ISourceLocation right = (ISourceLocation) iterRight.next();
  299. sameSourceLocation(left, right);
  300. }
  301. assertTrue(!iterLeft.hasNext());
  302. assertTrue(!iterRight.hasNext());
  303. }
  304. public static void sameSourceLocation(ISourceLocation lhs, ISourceLocation rhs) {
  305. if ((null == lhs) && (null == rhs)) {
  306. return;
  307. }
  308. assertTrue(lhs != null);
  309. assertTrue(rhs != null);
  310. assertTrue(lhs.getLine() == rhs.getLine());
  311. assertTrue(lhs.getColumn() == rhs.getColumn());
  312. assertTrue(lhs.getOffset() == rhs.getOffset());
  313. assertTrue(lhs.getEndLine() == rhs.getEndLine());
  314. // XXX need to compare files, permitting null == NONE
  315. }
  316. /**
  317. * Constructor for AjcSpecTest.
  318. * @param name
  319. */
  320. public AjcSpecTest(String name) {
  321. super(name);
  322. }
  323. public void testMinimal() {
  324. AjcTest.Spec one = new AjcTest.Spec();
  325. AjcTest.Spec two = new AjcTest.Spec();
  326. // empty/identity tests
  327. sameAjcTestSpec(one, two, this);
  328. one.addOption("-one");
  329. one.addKeyword("keyword");
  330. one.addPath("path");
  331. IMessage m = MessageUtil.info("info message");
  332. one.addMessage(m);
  333. DirChanges.Spec dcspec = new DirChanges.Spec();
  334. dcspec.setDirToken("dirToken");
  335. dcspec.setDefaultSuffix(".suffix");
  336. one.addDirChanges(dcspec);
  337. // full/identity tests
  338. sameAjcTestSpec(one, one, this);
  339. // XXX need to clone...
  340. // XXX need to test that more differences are detected
  341. boolean passed = false;
  342. try {
  343. sameAjcTestSpec(one, two, this);
  344. } catch (AssertionFailedError e) {
  345. passed = true;
  346. }
  347. assertTrue("did not get expected exception", passed);
  348. }
  349. }