001: /* CallTest.java */
002:
003: package org.quilt.frontend.ant;
004:
005: import org.apache.tools.ant.AntClassLoader;
006: import org.apache.tools.ant.BuildException;
007: import org.apache.tools.ant.Project;
008: import org.apache.tools.ant.Task;
009: import org.apache.tools.ant.taskdefs.Execute;
010: import org.apache.tools.ant.types.CommandlineJava;
011: import org.apache.tools.ant.types.EnumeratedAttribute;
012: import org.apache.tools.ant.types.Environment;
013: import org.apache.tools.ant.types.Path;
014:
015: import java.io.File;
016:
017: import java.util.Enumeration;
018: import java.util.Hashtable;
019: import java.util.Properties;
020:
021: import org.quilt.cl.QuiltClassLoader;
022: import org.quilt.framework.*;
023: import org.quilt.reports.*;
024: import org.quilt.runner.*;
025:
026: /**
027: * Handles an individual QuiltTest suite by directly calling it
028: * rather than forking it.
029: */
030: public class CallTest {
031: private Project project = null;
032: private Task task = null;
033: private TaskControl tc = null;
034: private QuiltTest qt = null;
035:
036: private CommandlineJava cmdLine = null;
037: private Runner runner;
038:
039: /** No-arg constructor. */
040: public CallTest() {
041: }
042:
043: /**
044: * Run the test suite in this Java virtual machine.
045: *
046: * @param test Data structure describing the individual test.
047: * @param tc Task control which sets parameters for the entire run.
048: */
049: protected int execTest(QuiltTest test, TaskControl tc)
050: throws BuildException {
051: // structure gets altered during the test run, so needs to be
052: // cloned
053: QuiltTest qt = (QuiltTest) test.clone();
054: this .tc = tc;
055: task = tc.getTask();
056: project = task.getProject();
057:
058: boolean usingQuilt = (tc.getLoader() != null)
059: && qt.getCheckCoverage()
060: && qt.getCheckIncludes() != null;
061:
062: cmdLine = (CommandlineJava) tc.getCommandline().clone();
063: qt.setProperties(project.getProperties());
064: if (tc.getDir() != null) {
065: task
066: .log(
067: "Dir attribute ignored, running in the same virtual machine",
068: Project.MSG_WARN);
069: }
070: if (tc.getNewEnvironment()
071: || null != tc.getEnv().getVariables()) {
072: task.log("Changes to environment variables are ignored, "
073: + "running in the same virtual machine.",
074: Project.MSG_WARN);
075: }
076:
077: CommandlineJava.SysProperties sysProperties = cmdLine
078: .getSystemProperties();
079: if (sysProperties != null) {
080: sysProperties.setSystem();
081: }
082: AntClassLoader antLoader = null;
083: QuiltClassLoader quiltLoader = tc.getLoader();
084: try {
085: task.log("Using System properties "
086: + System.getProperties(), Project.MSG_VERBOSE);
087: Path userClasspath = cmdLine.getClasspath();
088: Path classpath = userClasspath == null ? null
089: : (Path) userClasspath.clone();
090: if (usingQuilt) {
091: String pathForQuilt = (classpath == null) ? null
092: : classpath.toString();
093: quiltLoader.setClassPath(pathForQuilt);
094: quiltLoader.setIncluded(qt.getCheckIncludes());
095: quiltLoader.setExcluded(qt.getCheckExcludes());
096: } else if (classpath != null) {
097: // XXX either document the fact that this option isn't
098: // supported with Quilt - or support it
099: if (tc.getIncludeAntRuntime()) {
100: task.log("Adding " + tc.getAntRuntimeClasses()
101: + " to CLASSPATH", Project.MSG_VERBOSE);
102: classpath.append(tc.getAntRuntimeClasses());
103: }
104:
105: antLoader = new AntClassLoader(null, project,
106: classpath, false);
107: task.log("Using CLASSPATH " + antLoader.getClasspath(),
108: Project.MSG_VERBOSE);
109:
110: antLoader.addSystemPackageRoot("junit");
111: }
112: if (usingQuilt) {
113: // // DEBUG
114: // System.out.println(
115: // "CallTest invoking BaseTestRunner with classpath "
116: // + classpath);
117: // // END
118: runner = new BaseTestRunner(qt, quiltLoader);
119: } else {
120: runner = new BaseTestRunner(qt, antLoader);
121: }
122: if (tc.getSummary()) {
123: task.log("Running " + qt.getName(), Project.MSG_INFO);
124:
125: SummaryFormatter fmt = new SummaryFormatter();
126: fmt.setWithOutAndErr("withoutanderr".equals(tc
127: .getSummaryValue()));
128: fmt.setOutput(tc.getDefaultOutput());
129: runner.addFormatter(fmt);
130: }
131:
132: final FmtSelector[] selectors = tc.mergeSelectors(qt);
133: for (int i = 0; i < selectors.length; i++) {
134: FmtSelector fs = selectors[i];
135: File outFile = tc.getOutput(fs, qt);
136: if (outFile != null) {
137: fs.setOutfile(outFile);
138: } else {
139: fs.setOutput(tc.getDefaultOutput());
140: }
141: runner.addFormatter(fs.createFormatter());
142: }
143: runner.run();
144: return runner.getRetCode();
145:
146: } finally {
147: if (sysProperties != null) {
148: sysProperties.restoreSystem();
149: }
150: if (antLoader != null) {
151: antLoader.resetThreadContextLoader();
152: }
153: }
154: }
155: }
|