001: package dalma.test;
002:
003: import dalma.Conversation;
004: import dalma.Engine;
005: import dalma.ErrorHandler;
006: import dalma.helpers.ThreadPoolExecutor;
007: import dalma.impl.EngineImpl;
008: import dalma.impl.Util;
009: import junit.framework.TestCase;
010:
011: import java.io.File;
012: import java.io.FileInputStream;
013: import java.io.IOException;
014: import java.lang.reflect.Constructor;
015: import java.util.Properties;
016:
017: /**
018: * Base class for workflow-based tests.
019: *
020: * @author Kohsuke Kawaguchi
021: */
022: public abstract class WorkflowTestProgram extends TestCase {
023: protected Engine engine;
024: private File root;
025: private ClassLoader classLoader;
026:
027: protected WorkflowTestProgram(String name) {
028: super (name);
029: }
030:
031: protected final void setUp() throws Exception {
032: // creates a new class loader that loads test classes with javaflow enhancements
033: classLoader = new TestClassLoader(Launcher.class
034: .getClassLoader());
035:
036: root = File.createTempFile("dalma", "test-case");
037: root.delete();
038: root.mkdirs();
039:
040: Runtime.getRuntime().addShutdownHook(new Thread() {
041: public void run() {
042: try {
043: Util.deleteRecursive(root);
044: } catch (IOException e) {
045: e.printStackTrace();
046: }
047: }
048: });
049:
050: engine = new EngineImpl(root, classLoader,
051: new ThreadPoolExecutor(3, true));
052:
053: engine.setErrorHandler(new ErrorHandler() {
054: final Thread mainThread = Thread.currentThread();
055:
056: public void onError(Throwable t) {
057: // log the message
058: ErrorHandler.DEFAULT.onError(t);
059: // then interrupt the main thread to cause a test failure
060: mainThread.interrupt();
061: }
062: });
063:
064: setupEndPoints();
065:
066: engine.start();
067: }
068:
069: protected abstract void setupEndPoints() throws Exception;
070:
071: /**
072: * Creates a new conversation instance with given parameters.
073: */
074: protected final Conversation createConversation(
075: Class<? extends Runnable> c, Object... args)
076: throws Exception {
077: Class clazz = classLoader.loadClass(c.getName());
078: Constructor constructor = findConstructor(clazz, args.length);
079: constructor.setAccessible(true);
080: Runnable r = (Runnable) constructor.newInstance(args);
081:
082: return engine.createConversation(r);
083: }
084:
085: private Constructor findConstructor(Class c, int length)
086: throws NoSuchMethodException {
087: for (Constructor init : c.getConstructors())
088: if (init.getParameterTypes().length == length)
089: return init;
090:
091: throw new NoSuchMethodException(
092: "Unable to find a matching constructor on "
093: + c.getName());
094: }
095:
096: protected void tearDown() throws Exception {
097: System.out.println("tearing down");
098: engine.stop();
099: Util.deleteRecursive(root);
100: }
101:
102: /**
103: * Obtains the property value of the given key.
104: *
105: * This mechanism allows the test harness to configure the tests.
106: */
107: protected final String getProperty(String key) {
108: // Maven can pass in the property value as a system property
109: String v = System.getProperty(key);
110: if (v != null)
111: return v;
112:
113: // otherwise let's check those properties by ourselves.
114: File f = new File(".").getAbsoluteFile();
115: do {
116: File buildProperties = new File(f, "build.properties");
117: if (buildProperties.exists()) {
118: try {
119: Properties props = new Properties();
120: props.load(new FileInputStream(buildProperties));
121: if (props.containsKey(key))
122: return props.getProperty(key);
123: } catch (IOException e) {
124: throw new Error(e);
125: }
126: }
127: } while (!new File(f, "dalma.iml").exists());
128:
129: // TODO: explain what it means better
130: throw new Error("test property " + key + " is not set.");
131: }
132: }
|