1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package nl.toolforge.karma.core.cmd.impl;
20
21 import java.io.File;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.DirectoryScanner;
27 import org.apache.tools.ant.Project;
28
29 import nl.toolforge.karma.core.cmd.Command;
30 import nl.toolforge.karma.core.cmd.CommandDescriptor;
31 import nl.toolforge.karma.core.cmd.CommandException;
32 import nl.toolforge.karma.core.cmd.CommandFactory;
33 import nl.toolforge.karma.core.cmd.CommandLoadException;
34 import nl.toolforge.karma.core.cmd.CommandResponse;
35 import nl.toolforge.karma.core.cmd.event.ErrorEvent;
36 import nl.toolforge.karma.core.cmd.event.MessageEvent;
37 import nl.toolforge.karma.core.cmd.event.SimpleMessage;
38 import nl.toolforge.karma.core.cmd.util.DependencyException;
39 import nl.toolforge.karma.core.cmd.util.DependencyHelper;
40 import nl.toolforge.karma.core.module.ModuleTypeException;
41
42 /***
43 * Run the unit tests of a given module.
44 * <p>
45 * At this moment this class only supports Java/JUnit in combination with Ant.
46 * </p>
47 *
48 * @author W.H. Schraal
49 * @version $Id: TestModule.java,v 1.32 2004/11/16 22:28:02 hippe Exp $
50 */
51 public class TestModule extends AbstractBuildCommand {
52
53 private static final Log logger = LogFactory.getLog(TestModule.class);
54
55 private CommandResponse commandResponse = new CommandResponse();
56
57 public TestModule(CommandDescriptor descriptor) {
58 super(descriptor);
59 }
60
61 public void execute() throws CommandException {
62
63 super.execute();
64
65 Command command = null;
66 try {
67 String commandLineString;
68 if (!getCommandLine().hasOption("n")) {
69 commandLineString = "bm -m " + module.getName();
70 } else {
71 commandLineString = "bm -n -m " + module.getName();
72 }
73 logger.debug("Going to: "+commandLineString);
74 command = CommandFactory.getInstance().getCommand(commandLineString);
75 command.setContext(getContext());
76 command.registerCommandResponseListener(getResponseListener());
77 command.execute();
78 } catch (CommandException ce) {
79 if (ce.getErrorCode().equals(CommandException.DEPENDENCY_DOES_NOT_EXIST) ||
80 ce.getErrorCode().equals(CommandException.BUILD_FAILED) ||
81 ce.getErrorCode().equals(DependencyException.DEPENDENCY_NOT_FOUND) ) {
82 commandResponse.addEvent(new ErrorEvent(this, ce.getErrorCode(), ce.getMessageArguments()));
83 throw new CommandException(ce, CommandException.TEST_FAILED, new Object[]{module.getName()});
84 } else if (ce.getErrorCode().equals(CommandException.NO_SRC_DIR)) {
85
86
87 } else {
88 commandResponse.addEvent(new ErrorEvent(this, ce.getErrorCode(), ce.getMessageArguments()));
89 }
90 } catch (CommandLoadException e) {
91 throw new CommandException(e.getErrorCode(), e.getMessageArguments());
92 } finally {
93 if ( command != null ) {
94 command.deregisterCommandResponseListener(getResponseListener());
95 }
96 }
97
98
99
100
101 if (!getBuildEnvironment().getModuleTestSourceDirectory().exists()) {
102
103
104 throw new CommandException(CommandException.NO_TEST_DIR, new Object[] {getCurrentModule().getName()});
105 }
106 DirectoryScanner scanner = new DirectoryScanner();
107 scanner.setBasedir(getBuildEnvironment().getModuleTestSourceDirectory());
108 scanner.setIncludes(new String[]{"**/*.java"});
109 scanner.scan();
110 if (scanner.getIncludedFiles().length == 0) {
111
112
113 throw new CommandException(CommandException.NO_TEST_DIR, new Object[] {getCurrentModule().getName(), "test/java"});
114 }
115
116
117
118 Project project = getAntProject("test-module.xml");
119
120 logger.debug("Setting 'module-source-dir' to: "+getBuildEnvironment().getModuleTestSourceDirectory().getPath());
121 logger.debug("Setting 'module-test-dir' to: "+getBuildEnvironment().getModuleTestBuildDirectory().getPath());
122 project.setProperty("module-source-dir", getBuildEnvironment().getModuleTestSourceDirectory().getPath());
123 project.setProperty("module-test-dir", getBuildEnvironment().getModuleTestBuildDirectory().getPath());
124
125 try {
126
127 logger.debug("Setting 'module-compile-dir' to: "+getCompileDirectory().getPath());
128 project.setProperty("module-compile-dir", getCompileDirectory().getPath());
129
130 String deps = "";
131
132 DependencyHelper helper = new DependencyHelper(getCurrentManifest());
133
134 if (getCurrentModule().getDependencies().size() > 0) {
135 deps = helper.getTestClassPath(getCurrentModule()) + ";";
136 }
137
138 File f = getCurrentManifest().getBuildBaseDirectory();
139 f = new File(f, getCurrentModule().getName());
140 f = new File(f, "build");
141
142 deps += f.getPath();
143 logger.debug("Setting 'module-classpath' to: "+deps);
144 project.setProperty("module-classpath", deps);
145 } catch (DependencyException d) {
146 throw new CommandException(d.getErrorCode(), d.getMessageArguments());
147 } catch (ModuleTypeException d) {
148 throw new CommandException(d.getErrorCode(), d.getMessageArguments());
149 }
150
151 try {
152 project.executeTarget("run");
153 } catch (BuildException e) {
154 logger.info(e.getMessage(), e);
155 throw new CommandException(CommandException.TEST_FAILED, new Object[] {getCurrentModule().getName()});
156 }
157
158
159 commandResponse.addEvent(new MessageEvent(this, new SimpleMessage("Module " + getCurrentModule().getName() + " tested succesfully.")));
160 }
161
162 /***
163 * Gets the commands' response object.
164 *
165 * @return The commands' response object.
166 */
167 public CommandResponse getCommandResponse() {
168 return this.commandResponse;
169 }
170 }