123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- /* *******************************************************************
- * Copyright (c) 2004 IBM Corporation
- * All rights reserved.
- * This program and the accompanying materials are made available
- * under the terms of the Common Public License v1.0
- * which accompanies this distribution and is available at
- * http://www.eclipse.org/legal/cpl-v10.html
- *
- * Contributors:
- * Adrian Colyer,
- * ******************************************************************/
- package org.aspectj.testing;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStreamReader;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.logging.Logger;
-
- import junit.extensions.TestSetup;
- import junit.framework.Test;
- import junit.framework.TestSuite;
-
- import org.apache.commons.digester.Digester;
- import org.aspectj.tools.ajc.AjcTestCase;
- import org.aspectj.tools.ajc.CompilationResult;
- import org.aspectj.util.FileUtil;
- import org.xml.sax.EntityResolver;
- import org.xml.sax.InputSource;
- import org.xml.sax.SAXException;
- import org.xml.sax.Locator;
-
- /**
- * 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;
-
- 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) {
- try {
- 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;
- } catch (Throwable e) {
- e.printStackTrace();
- throw new RuntimeException(e);
- }
- }
-
- /**
- * The file containing the XML specification for the tests.
- */
- protected abstract File getSpecFile();
-
- /*
- * Return a map from (String) test title -> AjcTest
- */
- private 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) {
- currentTest = (AjcTest) testMap.get(title);
- if (currentTest == null) {
- fail("No test '" + title + "' in suite.");
- }
- ajc.setShouldEmptySandbox(true);
- currentTest.runTest(this);
- }
-
- /**
- * 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);
- }
-
- /*
- * 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.
- */
- private Digester getDigester() {
- Digester digester = new Digester();/* {
- //ALEX
- public InputSource resolveEntity(String s, String s1) throws SAXException {
- System.out.println("**********XMLBasedAjcTestCase.resolveEntity");
- if (s1.indexOf("org.aspectj/tests/") > 0) {
- String newS1 = s1.substring(s1.indexOf("org.aspectj/tests/")+"org.aspectj/tests/".length());
- return new InputSource("tests/"+newS1);
- }
- return super.resolveEntity(s, s1);
- }
- };*/
- 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/run",RunSpec.class);
- digester.addSetProperties("suite/ajc-test/run","class","classToRun");
- 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");
- 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();
- d.getXMLReader().setEntityResolver(new EntityResolver() {
- public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
- System.out.println("!!!!!!!!!XMLBasedAjcTestCase.resolveEntity");
- return null; //To change body of implemented methods use File | Settings | File Templates.
- }
- });
- try {
- InputStreamReader isr = new InputStreamReader(new FileInputStream(getSpecFile()));
- d.parse(isr);
- } catch (Exception ex) {
- //ALEX was: but painfull in IntelliJ single module styled config
- fail("Unable to load suite " + getSpecFile().getPath() + " : " + ex);
- // ALEX is below
- // ex.printStackTrace();
- // try {
- // InputStreamReader isr = new InputStreamReader(new FileInputStream(new File(getSpecFile().getPath().substring(3))));
- // d.parse(isr);
- // } catch (Exception ex2) {
- // ex2.printStackTrace();
- // fail("Unable to load suite " + getSpecFile().getPath() + " : " + ex + " : " + ex2);
- // }
- }
- 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);
- }
- }
|