123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- /* *******************************************************************
- * Copyright (c) 1999-2001 Xerox Corporation,
- * 2002 Palo Alto Research Center, Incorporated (PARC).
- * All rights reserved.
- * This program and the accompanying materials are made available
- * under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Xerox/PARC initial implementation
- * ******************************************************************/
-
- package org.aspectj.testing.harness.bridge;
-
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Iterator;
- import java.util.List;
- import java.util.ListIterator;
-
- import org.aspectj.bridge.IMessage;
- import org.aspectj.bridge.IMessageHolder;
- import org.aspectj.bridge.ISourceLocation;
- import org.aspectj.bridge.MessageUtil;
-
- import junit.framework.Assert;
- import junit.framework.AssertionFailedError;
- import junit.framework.TestCase;
-
- /**
- * Primarily used by others to test AjcTest
- */
- public class AjcSpecTest extends TestCase {
- public static final String NOTSAME = " != ";
- public static void sameAjcSuiteSpec(
- AjcTest.Suite.Spec lhsSpec,
- AjcTest.Suite.Spec rhsSpec,
- Assert a) {
- assertNotNull(lhsSpec);
- assertNotNull(rhsSpec);
- Iterator lhs = lhsSpec.getChildren().iterator();
- Iterator rhs = rhsSpec.getChildren().iterator();
- while (lhs.hasNext() && rhs.hasNext()) {
- AjcTest.Spec lhsTest = (AjcTest.Spec) lhs.next();
- AjcTest.Spec rhsTest = (AjcTest.Spec) rhs.next();
- AjcSpecTest.sameAjcTestSpec(lhsTest, rhsTest, a);
- }
- assertTrue(!lhs.hasNext());
- assertTrue(!rhs.hasNext());
- }
-
- public static void sameAjcTestSpec(
- AjcTest.Spec lhsTest,
- AjcTest.Spec rhsTest,
- Assert a) {
- assertNotNull(lhsTest);
- assertNotNull(rhsTest);
- assertEquals(lhsTest.getBugId(), rhsTest.getBugId());
- assertEquals(lhsTest.getTestDirOffset(), rhsTest.getTestDirOffset());
- // XXX suiteDir varies by run..
- // description, options, paths, comments, keywords
- sameAbstractRunSpec(lhsTest, rhsTest, a);
- }
-
- public static void sameAbstractRunSpec(
- AbstractRunSpec lhs,
- AbstractRunSpec rhs,
- Assert a) {
- assertEquals(lhs.description, rhs.description);
- // XXX keywords added in .txt reading -
- //sameList(lhs.getKeywordsList(), rhs.getKeywordsList(), a);
- // XXX sameList(lhs.globalOptions, rhs.globalOptions, a);
- sameList(lhs.getOptionsList(), rhs.getOptionsList(), a);
- sameList(lhs.getPathsList(), rhs.getPathsList(), a);
- assertEquals(lhs.isStaging(), rhs.isStaging());
- sameList(lhs.keywords, rhs.keywords, a);
- assertEquals(lhs.comment, rhs.comment);
- assertEquals(lhs.badInput, rhs.badInput);
- // xml adds sourceloc?
- //sameSourceLocation(lhs.getSourceLocation(), rhs.getSourceLocation(), a);
- // XXX also sourceLocations?
- sameMessages(lhs.getMessages(), rhs.getMessages(), a);
- }
-
- /** @return normal form - null is "", "" is "", and others are {fully.qualified.class}.toString().trim() */
- static String normal(Object input) {
- if ((null == input) || ("".equals(input))) {
- return "";
- } else {
- return input.getClass().getName() + "." + input.toString().trim();
- }
- }
-
- /** @return true if these match after normalizing */
- public static void same(Object lhs, Object rhs, Assert a) {
- lhs = normal(lhs);
- rhs = normal(rhs);
- assertTrue(lhs + NOTSAME + rhs, lhs.equals(rhs));
- }
-
- /** @return true if both are empty (null or no entries) or if all match */
- public static void sameRA(String[] lhs, String[] rhs, Assert a) {
- if (null == lhs) {
- assertTrue((null == rhs) || (0 == rhs.length));
- } else if (null == rhs) {
- assertTrue(0 == lhs.length);
- } else {
- String l = normal(lhs);
- String r = normal(rhs);
- assertTrue(l + NOTSAME + r, l.equals(r));
- }
- }
-
- /** @return normal form for String[] items*/
- static String normal(String[] items) {
- return (null == items ? "[]" : normal(Arrays.asList(items)));
- }
-
- /** @return normal form for list items */
- static String normal(List list) {
- StringBuffer sb = new StringBuffer();
- sb.append("[");
- boolean first = true;
- for (Iterator iter = list.iterator(); iter.hasNext();) {
- Object o = iter.next();
- if (!first) {
- sb.append(", ");
- } else {
- first = false;
- }
- sb.append(normal(o));
- }
- sb.append("]");
- return sb.toString();
- }
-
- /** @return true if both are empty (null or no entries) or if all match after trimming */
- public static void sameListSize(List lhs, List rhs) {
- if (null == lhs) {
- assertTrue((null == rhs) || (0 == rhs.size()));
- } else if (null == rhs) {
- assertTrue(0 == lhs.size());
- } else {
- assertTrue(rhs.size() == lhs.size());
- }
- }
-
- /** @return true if both are empty (null or no entries) or if all match after trimming */
- public static void sameList(List lhs, List rhs, Assert a) {
- sameListSize(lhs, rhs);
- String l = normal(lhs);
- String r = normal(rhs);
- String label = l + NOTSAME + r;
- assertTrue(label, l.equals(r));
- }
-
- // /**
- // * Normalize and compare:
- // * <li>bug id's are not compared since extracted during xml writing</li>
- // * <li>keyword compare is disabled since keywords are generated during xml reading.</li>
- // * <li>description compare is normalized by stripping bug ids</li>
- // * <li>String and arrays are equal when empty (null or 0-length)</li>
- // * @see Ajctest#stripBugId(String)
- // */
- // public static void sameAjcTest(AjcTest lhs, AjcTest rhs, Assert reporter) {
- // Assert a = reporter;
- // String label = lhs + NOTSAME + rhs;
- // assertTrue(label, null != lhs);
- // assertTrue(label, null != rhs);
- // //assertTrue(label, lhs.ignoreWarnings == rhs.ignoreWarnings);
- // // XXX disabled - not in .txt
- // // sameStringList(lhs.keywords, rhs.keywords, a);
- // // sameString(lhs.bugId, rhs.bugId, a);
- // // argh - bugid stripped from description
- // //same(AjcTest.stripBugId(lhs.description), AjcTest.stripBugId(lhs.description), a);
- // //sameRA(lhs.globals, rhs.globals, a);
- // //lhs.reset();
- // //rhs.reset();
- // boolean gotOne = false;
- // IMessageHolder holder = new MessageHandler();
- // assertTrue(label, !holder.hasAnyMessage(IMessage.FAIL, IMessageHolder.ORGREATER));
- // while (lhs.hasNextRun() && rhs.hasNextRun()) {
- // sameIAjcRun((IAjcRun) lhs.nextRun(holder), (IAjcRun) rhs.nextRun(holder), reporter);
- // assertTrue(label, !holder.hasAnyMessage(IMessage.FAIL, IMessageHolder.ORGREATER));
- // if (!gotOne) {
- // gotOne = true;
- // }
- // }
- // assertTrue(label, gotOne);
- // assertTrue(label, !lhs.hasNextRun());
- // assertTrue(label, !rhs.hasNextRun());
- // }
-
- public static void sameIAjcRun(IAjcRun lhs, IAjcRun rhs, Assert reporter) {
- // Assert a = reporter;
- assertTrue(lhs != null);
- assertTrue(rhs != null);
- Class c = lhs.getClass();
- assertTrue(c == rhs.getClass());
- AbstractRunSpec lhsSpec;
- AbstractRunSpec rhsSpec;
-
- if (c == CompilerRun.class) {
- CompilerRun.Spec l = ((CompilerRun) lhs).spec;
- CompilerRun.Spec r = ((CompilerRun) rhs).spec;
- lhsSpec = l;
- rhsSpec = r;
- assertEquals(l.argfiles, r.argfiles);
- assertEquals(l.aspectpath, r.aspectpath);
- assertEquals(l.testSrcDirOffset, r.testSrcDirOffset);
- assertEquals(l.compiler, r.compiler);
- assertEquals(l.includeClassesDir, r.includeClassesDir);
- assertEquals(l.reuseCompiler, r.reuseCompiler);
- assertEquals(l.sourceroots, r.sourceroots);
- assertEquals(l.extdirs, r.extdirs);
- } else if (c == JavaRun.class) {
- JavaRun.Spec l = ((JavaRun) lhs).spec;
- JavaRun.Spec r = ((JavaRun) rhs).spec;
- lhsSpec = l;
- rhsSpec = r;
- assertTrue(l.skipTester == r.skipTester);
- assertEquals(l.className, r.className);
- assertEquals(l.javaVersion, r.javaVersion);
- assertEquals(l.skipTester, r.skipTester);
- assertEquals(l.outStreamIsError, r.outStreamIsError);
- assertEquals(l.errStreamIsError, r.errStreamIsError);
- } else if (c == IncCompilerRun.class) {
- IncCompilerRun.Spec l = ((IncCompilerRun) lhs).spec;
- IncCompilerRun.Spec r = ((IncCompilerRun) rhs).spec;
- lhsSpec = l;
- rhsSpec = r;
- assertEquals(l.tag, r.tag);
- assertEquals(l.fresh, r.fresh);
- } else {
- assertTrue(lhs.equals(rhs));
- return;
- }
- sameSpec(lhsSpec, rhsSpec, reporter);
- }
-
- public static void sameSpec(AbstractRunSpec lhs, AbstractRunSpec rhs, Assert a) {
- if ((null == lhs) && (null == rhs)) {
- return;
- }
- assertTrue(lhs != null);
- assertTrue(rhs != null);
- assertEquals(""+lhs.getOptionsList(), ""+rhs.getOptionsList());
- sameList(lhs.getPathsList(), rhs.getPathsList(), a);
- sameMessages(lhs.getMessages(), rhs.getMessages(), a);
- sameDirChangesList(lhs.dirChanges, rhs.dirChanges, a);
- }
-
- public static void sameDirChangesList(ArrayList lhs, ArrayList rhs, Assert a) {
- if ((null == lhs) && (null == rhs)) {
- return;
- }
- assertTrue(rhs != null);
- assertTrue(lhs != null);
- sameListSize(lhs, rhs);
- Iterator lhsIter = lhs.iterator();
- Iterator rhsIter = rhs.iterator();
- while (lhsIter.hasNext() && rhsIter.hasNext()) {
- sameDirChangesSpec((DirChanges.Spec) lhsIter.next(), (DirChanges.Spec) rhsIter.next(), a);
- }
- }
-
- public static void sameDirChangesSpec(DirChanges.Spec lhs, DirChanges.Spec rhs, Assert a) {
- if ((null == lhs) && (null == rhs)) {
- return;
- }
- assertTrue(rhs != null);
- assertTrue(lhs != null);
- assertEquals(lhs.defaultSuffix, rhs.defaultSuffix);
- assertEquals(lhs.dirToken, rhs.dirToken);
- assertEquals(lhs.fastFail, rhs.fastFail);
- assertEquals(lhs.expDir, rhs.expDir); // XXX normalize?
- sameList(lhs.updated, rhs.updated, a);
- sameList(lhs.removed, rhs.removed, a);
- sameList(lhs.added, rhs.added, a);
- }
-
- public static void sameMessages(IMessageHolder one, IMessageHolder two, Assert a) {
- if ((null == one) && (null == two)) {
- return;
- }
- // order matters here
- ListIterator lhs = one.getUnmodifiableListView().listIterator();
- ListIterator rhs = two.getUnmodifiableListView().listIterator();
- while (lhs.hasNext() && rhs.hasNext()) {
- sameMessage((IMessage) lhs.next(), (IMessage) rhs.next(), a);
- }
- assertTrue(!lhs.hasNext());
- assertTrue(!rhs.hasNext());
- }
-
- public static void sameMessage(IMessage lhs, IMessage rhs, Assert a) {
- if ((null == lhs) && (null == rhs)) {
- return;
- }
- assertTrue(lhs != null);
- assertTrue(rhs != null);
- assertTrue(lhs.getKind() == rhs.getKind());
- same(lhs.getMessage(), rhs.getMessage(), a);
- same(lhs.getDetails(), rhs.getDetails(), a);
- assertEquals(lhs.getThrown(), rhs.getThrown());
- sameSourceLocation(lhs.getSourceLocation(), rhs.getSourceLocation());
- sameSourceLocations(lhs.getExtraSourceLocations(), rhs.getExtraSourceLocations());
- }
- public static void sameSourceLocations(List lhs, List rhs) {
- sameListSize(lhs, rhs);
- if ((null == lhs) || (0 == lhs.size())) {
- return;
- }
- // ok, do order-dependent check..
- ListIterator iterLeft = lhs.listIterator();
- ListIterator iterRight = rhs.listIterator();
- while (iterLeft.hasNext() && iterRight.hasNext()) {
- ISourceLocation left = (ISourceLocation) iterLeft.next();
- ISourceLocation right = (ISourceLocation) iterRight.next();
- sameSourceLocation(left, right);
- }
- assertTrue(!iterLeft.hasNext());
- assertTrue(!iterRight.hasNext());
-
- }
-
- public static void sameSourceLocation(ISourceLocation lhs, ISourceLocation rhs) {
- if ((null == lhs) && (null == rhs)) {
- return;
- }
- assertTrue(lhs != null);
- assertTrue(rhs != null);
- assertTrue(lhs.getLine() == rhs.getLine());
- assertTrue(lhs.getColumn() == rhs.getColumn());
- assertTrue(lhs.getOffset() == rhs.getOffset());
- assertTrue(lhs.getEndLine() == rhs.getEndLine());
- // XXX need to compare files, permitting null == NONE
- }
-
- /**
- * Constructor for AjcSpecTest.
- * @param name
- */
- public AjcSpecTest(String name) {
- super(name);
- }
-
- public void testMinimal() {
- AjcTest.Spec one = new AjcTest.Spec();
- AjcTest.Spec two = new AjcTest.Spec();
- // empty/identity tests
- sameAjcTestSpec(one, two, this);
-
- one.addOption("-one");
- one.addKeyword("keyword");
- one.addPath("path");
- IMessage m = MessageUtil.info("info message");
- one.addMessage(m);
- DirChanges.Spec dcspec = new DirChanges.Spec();
- dcspec.setDirToken("dirToken");
- dcspec.setDefaultSuffix(".suffix");
- one.addDirChanges(dcspec);
-
- // full/identity tests
- sameAjcTestSpec(one, one, this);
- // XXX need to clone...
-
- // XXX need to test that more differences are detected
- boolean passed = false;
- try {
- sameAjcTestSpec(one, two, this);
- } catch (AssertionFailedError e) {
- passed = true;
- }
- assertTrue("did not get expected exception", passed);
- }
- }
|