123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- /* *******************************************************************
- * Copyright (c) 2004 IBM Corporation
- * 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:
- * Adrian Colyer,
- * ******************************************************************/
- package org.aspectj.testing;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FilenameFilter;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Stack;
-
- import junit.extensions.TestSetup;
- import junit.framework.Test;
- import junit.framework.TestSuite;
-
- import org.apache.commons.digester.Digester;
- import org.aspectj.apache.bcel.classfile.JavaClass;
- import org.aspectj.apache.bcel.classfile.LocalVariable;
- import org.aspectj.apache.bcel.classfile.LocalVariableTable;
- import org.aspectj.apache.bcel.util.ClassPath;
- import org.aspectj.apache.bcel.util.SyntheticRepository;
- import org.aspectj.tools.ajc.AjcTestCase;
- import org.aspectj.tools.ajc.CompilationResult;
- import org.aspectj.util.FileUtil;
-
- /**
- * Root class for all Test suites that are based on an AspectJ XML test suite file. Extends AjcTestCase allowing a mix of
- * programmatic and spec-file driven testing. See org.aspectj.systemtest.incremental.IncrementalTests for an example of this mixed
- * style.
- * <p>
- * The class org.aspectj.testing.MakeTestClass will generate a subclass of this class for you, given a suite spec. file as input...
- * </p>
- */
- public abstract class XMLBasedAjcTestCase extends AjcTestCase {
-
- private static Map testMap = new HashMap();
- private static boolean suiteLoaded = false;
- private AjcTest currentTest = null;
- private Stack clearTestAfterRun = new Stack();
-
- public XMLBasedAjcTestCase() {
- }
-
- /**
- * You must define a suite() method in subclasses, and return the result of calling this method. (Don't you hate static methods
- * in programming models). For example:
- *
- * <pre>
- * public static Test suite() {
- * return XMLBasedAjcTestCase.loadSuite(MyTestCaseClass.class);
- * }
- * </pre>
- *
- * @param testCaseClass
- * @return
- */
- public static Test loadSuite(Class testCaseClass) {
- TestSuite suite = new TestSuite(testCaseClass.getName());
- suite.addTestSuite(testCaseClass);
- TestSetup wrapper = new TestSetup(suite) {
- /*
- * (non-Javadoc)
- *
- * @see junit.extensions.TestSetup#setUp()
- */
- protected void setUp() throws Exception {
- super.setUp();
- suiteLoaded = false;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see junit.extensions.TestSetup#tearDown()
- */
- protected void tearDown() throws Exception {
- super.tearDown();
- suiteLoaded = false;
- }
- };
- return wrapper;
- }
-
- /**
- * The file containing the XML specification for the tests.
- */
- protected abstract File getSpecFile();
-
- /*
- * Return a map from (String) test title -> AjcTest
- */
- protected Map getSuiteTests() {
- return testMap;
- }
-
- /**
- * This helper method runs the test with the given title in the suite spec file. All tests steps in given ajc-test execute in
- * the same sandbox.
- */
- protected void runTest(String title, boolean print) {
- try {
- currentTest = (AjcTest) testMap.get(title);
- final boolean clearTest = clearTestAfterRun();
- if (currentTest == null) {
- if (clearTest) {
- System.err.println("test already run: " + title);
- return;
- } else {
- fail("No test '" + title + "' in suite.");
- }
- }
- boolean run = currentTest.runTest(this);
- assertTrue("Test not run", run);
- if (clearTest) {
- testMap.remove(title);
- }
- } finally {
- if (print) {
- System.out.println("SYSOUT");
- System.out.println(ajc.getLastCompilationResult().getStandardOutput());
- }
- }
- }
-
- protected void runTest(String title) {
- runTest(title, false);
- }
-
- /**
- * Get the currently executing test. Useful for access to e.g. AjcTest.getTitle() etc..
- */
- protected AjcTest getCurrentTest() {
- return currentTest;
- }
-
- /**
- * For use by the Digester. As the XML document is parsed, it creates instances of AjcTest objects, which are added to this
- * TestCase by the Digester by calling this method.
- */
- public void addTest(AjcTest test) {
- testMap.put(test.getTitle(), test);
- }
-
- protected final void pushClearTestAfterRun(boolean val) {
- clearTestAfterRun.push(val ? Boolean.FALSE : Boolean.TRUE);
- }
-
- protected final boolean popClearTestAfterRun() {
- return clearTest(true);
- }
-
- protected final boolean clearTestAfterRun() {
- return clearTest(false);
- }
-
- private boolean clearTest(boolean pop) {
- if (clearTestAfterRun.isEmpty()) {
- return false;
- }
- boolean result = ((Boolean) clearTestAfterRun.peek()).booleanValue();
- if (pop) {
- clearTestAfterRun.pop();
- }
- return result;
- }
-
- /*
- * The rules for parsing a suite spec file. The Digester using bean properties to match attributes in the XML document to
- * properties in the associated classes, so this simple implementation should be very easy to maintain and extend should you
- * ever need to.
- */
- protected Digester getDigester() {
- Digester digester = new Digester();
- digester.push(this);
- digester.addObjectCreate("suite/ajc-test", AjcTest.class);
- digester.addSetProperties("suite/ajc-test");
- digester.addSetNext("suite/ajc-test", "addTest", "org.aspectj.testing.AjcTest");
- digester.addObjectCreate("suite/ajc-test/compile", CompileSpec.class);
- digester.addSetProperties("suite/ajc-test/compile");
- digester.addSetNext("suite/ajc-test/compile", "addTestStep", "org.aspectj.testing.ITestStep");
- digester.addObjectCreate("suite/ajc-test/file", FileSpec.class);
- digester.addSetProperties("suite/ajc-test/file");
- digester.addSetNext("suite/ajc-test/file", "addTestStep", "org.aspectj.testing.ITestStep");
- digester.addObjectCreate("suite/ajc-test/run", RunSpec.class);
- digester.addSetProperties("suite/ajc-test/run", "class", "classToRun");
- digester.addSetProperties("suite/ajc-test/run", "ltw", "ltwFile");
- digester.addSetProperties("suite/ajc-test/run", "xlintfile", "xlintFile");
- digester.addSetProperties("suite/ajc-test/run/stderr", "ordered", "orderedStderr");
- digester.addSetNext("suite/ajc-test/run", "addTestStep", "org.aspectj.testing.ITestStep");
- digester.addObjectCreate("*/message", ExpectedMessageSpec.class);
- digester.addSetProperties("*/message");
- digester.addSetNext("*/message", "addExpectedMessage", "org.aspectj.testing.ExpectedMessageSpec");
- digester.addObjectCreate("suite/ajc-test/weave", WeaveSpec.class);
- digester.addSetProperties("suite/ajc-test/weave");
- digester.addSetNext("suite/ajc-test/weave", "addTestStep", "org.aspectj.testing.ITestStep");
-
- digester.addObjectCreate("suite/ajc-test/ant", AntSpec.class);
- digester.addSetProperties("suite/ajc-test/ant");
- digester.addSetNext("suite/ajc-test/ant", "addTestStep", "org.aspectj.testing.ITestStep");
- digester.addObjectCreate("suite/ajc-test/ant/stderr", OutputSpec.class);
- digester.addSetProperties("suite/ajc-test/ant/stderr");
- digester.addSetNext("suite/ajc-test/ant/stderr", "addStdErrSpec", "org.aspectj.testing.OutputSpec");
- digester.addObjectCreate("suite/ajc-test/ant/stdout", OutputSpec.class);
- digester.addSetProperties("suite/ajc-test/ant/stdout");
- digester.addSetNext("suite/ajc-test/ant/stdout", "addStdOutSpec", "org.aspectj.testing.OutputSpec");
-
- digester.addObjectCreate("suite/ajc-test/run/stderr", OutputSpec.class);
- digester.addSetProperties("suite/ajc-test/run/stderr");
- digester.addSetNext("suite/ajc-test/run/stderr", "addStdErrSpec", "org.aspectj.testing.OutputSpec");
- digester.addObjectCreate("suite/ajc-test/run/stdout", OutputSpec.class);
- digester.addSetProperties("suite/ajc-test/run/stdout");
- digester.addSetNext("suite/ajc-test/run/stdout", "addStdOutSpec", "org.aspectj.testing.OutputSpec");
- digester.addObjectCreate("*/line", OutputLine.class);
- digester.addSetProperties("*/line");
- digester.addSetNext("*/line", "addLine", "org.aspectj.testing.OutputLine");
- return digester;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.aspectj.tools.ajc.AjcTestCase#setUp()
- */
- protected void setUp() throws Exception {
- super.setUp();
- if (!suiteLoaded) {
- testMap = new HashMap();
- System.out.println("LOADING SUITE: " + getSpecFile().getPath());
- Digester d = getDigester();
- try {
- InputStreamReader isr = new InputStreamReader(new FileInputStream(getSpecFile()));
- d.parse(isr);
- } catch (Exception ex) {
- fail("Unable to load suite " + getSpecFile().getPath() + " : " + ex);
- }
- suiteLoaded = true;
- }
- }
-
- protected long nextIncrement(boolean doWait) {
- long time = System.currentTimeMillis();
- if (doWait) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException intEx) {
- }
- }
- return time;
- }
-
- protected void copyFile(String from, String to) throws Exception {
- String dir = getCurrentTest().getDir();
- FileUtil.copyFile(new File(dir + File.separator + from), new File(ajc.getSandboxDirectory(), to));
- }
-
- protected void copyFileAndDoIncrementalBuild(String from, String to) throws Exception {
- copyFile(from, to);
- CompilationResult result = ajc.doIncrementalCompile();
- assertNoMessages(result, "Expected clean compile from test '" + getCurrentTest().getTitle() + "'");
- }
-
- protected void copyFileAndDoIncrementalBuild(String from, String to, MessageSpec expectedResults) throws Exception {
- String dir = getCurrentTest().getDir();
- FileUtil.copyFile(new File(dir + File.separator + from), new File(ajc.getSandboxDirectory(), to));
- CompilationResult result = ajc.doIncrementalCompile();
- assertMessages(result, "Test '" + getCurrentTest().getTitle() + "' did not produce expected messages", expectedResults);
- }
-
- protected void deleteFile(String file) {
- new File(ajc.getSandboxDirectory(), file).delete();
- }
-
- protected void deleteFileAndDoIncrementalBuild(String file, MessageSpec expectedResult) throws Exception {
- deleteFile(file);
- CompilationResult result = ajc.doIncrementalCompile();
- assertMessages(result, "Test '" + getCurrentTest().getTitle() + "' did not produce expected messages", expectedResult);
- }
-
- protected void deleteFileAndDoIncrementalBuild(String file) throws Exception {
- deleteFileAndDoIncrementalBuild(file, MessageSpec.EMPTY_MESSAGE_SET);
- }
-
- protected void assertAdded(String file) {
- assertTrue("File " + file + " should have been added", new File(ajc.getSandboxDirectory(), file).exists());
- }
-
- protected void assertDeleted(String file) {
- assertFalse("File " + file + " should have been deleted", new File(ajc.getSandboxDirectory(), file).exists());
- }
-
- protected void assertUpdated(String file, long sinceTime) {
- File f = new File(ajc.getSandboxDirectory(), file);
- assertTrue("File " + file + " should have been updated", f.lastModified() > sinceTime);
- }
-
- public SyntheticRepository createRepos(File cpentry) {
- ClassPath cp = new ClassPath(cpentry + File.pathSeparator + System.getProperty("java.class.path"));
- return SyntheticRepository.getInstance(cp);
- }
-
- public JavaClass getClassFrom(File where, String clazzname) throws ClassNotFoundException {
- SyntheticRepository repos = createRepos(where);
- return repos.loadClass(clazzname);
- }
-
- /**
- * Sort it by name then start position
- */
- public List<LocalVariable> sortedLocalVariables(LocalVariableTable lvt) {
- List<LocalVariable> l = new ArrayList<LocalVariable>();
- LocalVariable lv[] = lvt.getLocalVariableTable();
- for (int i = 0; i < lv.length; i++) {
- LocalVariable lvEntry = lv[i];
- l.add(lvEntry);
- }
- Collections.sort(l, new MyComparator());
- return l;
- }
-
- public String stringify(LocalVariableTable lvt, int slotIndex) {
- LocalVariable lv[] = lvt.getLocalVariableTable();
- LocalVariable lvEntry = lv[slotIndex];
- StringBuffer sb = new StringBuffer();
- sb.append(lvEntry.getSignature()).append(" ").append(lvEntry.getName()).append("(").append(lvEntry.getIndex())
- .append(") start=").append(lvEntry.getStartPC()).append(" len=").append(lvEntry.getLength());
- return sb.toString();
- }
-
- public String stringify(List<LocalVariable> l, int slotIndex) {
- LocalVariable lvEntry = (LocalVariable) l.get(slotIndex);
- StringBuffer sb = new StringBuffer();
- sb.append(lvEntry.getSignature()).append(" ").append(lvEntry.getName()).append("(").append(lvEntry.getIndex())
- .append(") start=").append(lvEntry.getStartPC()).append(" len=").append(lvEntry.getLength());
- return sb.toString();
- }
-
- public String stringify(LocalVariableTable lvt) {
- if (lvt == null) {
- return "";
- }
- StringBuffer sb = new StringBuffer();
- sb.append("LocalVariableTable. Entries=#" + lvt.getTableLength()).append("\n");
- LocalVariable lv[] = lvt.getLocalVariableTable();
- for (int i = 0; i < lv.length; i++) {
- LocalVariable lvEntry = lv[i];
- sb.append(lvEntry.getSignature()).append(" ").append(lvEntry.getName()).append("(").append(lvEntry.getIndex())
- .append(") start=").append(lvEntry.getStartPC()).append(" len=").append(lvEntry.getLength()).append("\n");
- }
-
- return sb.toString();
- }
-
- public static class CountingFilenameFilter implements FilenameFilter {
-
- private String suffix;
- private int count;
-
- public CountingFilenameFilter(String s) {
- this.suffix = s;
- }
-
- public boolean accept(File dir, String name) {
- if (name.endsWith(suffix)) {
- count++;
- }
- return false;
- }
-
- public int getCount() {
- return count;
- }
- }
-
- public static class MyComparator implements Comparator<LocalVariable> {
- public int compare(LocalVariable o1, LocalVariable o2) {
- LocalVariable l1 = (LocalVariable) o1;
- LocalVariable l2 = (LocalVariable) o2;
- if (l1.getName().equals(l2.getName())) {
- return l1.getStartPC() - l2.getStartPC();
- } else {
- return l1.getName().compareTo(l2.getName());
- }
- }
-
- }
- }
|