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 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
public class TestModule extends AbstractBuildCommand { |
52 |
|
|
53 |
0 |
private static final Log logger = LogFactory.getLog(TestModule.class); |
54 |
|
|
55 |
0 |
private CommandResponse commandResponse = new CommandResponse(); |
56 |
|
|
57 |
|
public TestModule(CommandDescriptor descriptor) { |
58 |
0 |
super(descriptor); |
59 |
0 |
} |
60 |
|
|
61 |
|
public void execute() throws CommandException { |
62 |
|
|
63 |
0 |
super.execute(); |
64 |
|
|
65 |
0 |
Command command = null; |
66 |
|
try { |
67 |
|
String commandLineString; |
68 |
0 |
if (!getCommandLine().hasOption("n")) { |
69 |
0 |
commandLineString = "bm -m " + module.getName(); |
70 |
|
} else { |
71 |
0 |
commandLineString = "bm -n -m " + module.getName(); |
72 |
|
} |
73 |
0 |
logger.debug("Going to: "+commandLineString); |
74 |
0 |
command = CommandFactory.getInstance().getCommand(commandLineString); |
75 |
0 |
command.setContext(getContext()); |
76 |
0 |
command.registerCommandResponseListener(getResponseListener()); |
77 |
0 |
command.execute(); |
78 |
0 |
} catch (CommandException ce) { |
79 |
0 |
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 |
0 |
commandResponse.addEvent(new ErrorEvent(this, ce.getErrorCode(), ce.getMessageArguments())); |
83 |
0 |
throw new CommandException(ce, CommandException.TEST_FAILED, class="keyword">new Object[]{module.getName()}); |
84 |
0 |
} else if (ce.getErrorCode().equals(CommandException.NO_SRC_DIR)) { |
85 |
|
|
86 |
|
|
87 |
|
} else { |
88 |
0 |
commandResponse.addEvent(new ErrorEvent(this, ce.getErrorCode(), ce.getMessageArguments())); |
89 |
|
} |
90 |
0 |
} catch (CommandLoadException e) { |
91 |
0 |
throw new CommandException(e.getErrorCode(), e.getMessageArguments()); |
92 |
|
} finally { |
93 |
0 |
if ( command != null ) { |
94 |
0 |
command.deregisterCommandResponseListener(getResponseListener()); |
95 |
|
} |
96 |
0 |
} |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
0 |
if (!getBuildEnvironment().getModuleTestSourceDirectory().exists()) { |
102 |
|
|
103 |
|
|
104 |
0 |
throw new CommandException(CommandException.NO_TEST_DIR, class="keyword">new Object[] {getCurrentModule().getName()}); |
105 |
|
} |
106 |
0 |
DirectoryScanner scanner = new DirectoryScanner(); |
107 |
0 |
scanner.setBasedir(getBuildEnvironment().getModuleTestSourceDirectory()); |
108 |
0 |
scanner.setIncludes(new String[]{"**/*.java"}); |
109 |
0 |
scanner.scan(); |
110 |
0 |
if (scanner.getIncludedFiles().length == 0) { |
111 |
|
|
112 |
|
|
113 |
0 |
throw new CommandException(CommandException.NO_TEST_DIR, class="keyword">new Object[] {getCurrentModule().getName(), "test/java"}); |
114 |
|
} |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
0 |
Project project = getAntProject("test-module.xml"); |
119 |
|
|
120 |
0 |
logger.debug("Setting 'module-source-dir' to: "+getBuildEnvironment().getModuleTestSourceDirectory().getPath()); |
121 |
0 |
logger.debug("Setting 'module-test-dir' to: "+getBuildEnvironment().getModuleTestBuildDirectory().getPath()); |
122 |
0 |
project.setProperty("module-source-dir", getBuildEnvironment().getModuleTestSourceDirectory().getPath()); |
123 |
0 |
project.setProperty("module-test-dir", getBuildEnvironment().getModuleTestBuildDirectory().getPath()); |
124 |
|
|
125 |
|
try { |
126 |
|
|
127 |
0 |
logger.debug("Setting 'module-compile-dir' to: "+getCompileDirectory().getPath()); |
128 |
0 |
project.setProperty("module-compile-dir", getCompileDirectory().getPath()); |
129 |
|
|
130 |
0 |
String deps = ""; |
131 |
|
|
132 |
0 |
DependencyHelper helper = new DependencyHelper(getCurrentManifest()); |
133 |
|
|
134 |
0 |
if (getCurrentModule().getDependencies().size() > 0) { |
135 |
0 |
deps = helper.getTestClassPath(getCurrentModule()) + ";"; |
136 |
|
} |
137 |
|
|
138 |
0 |
File f = getCurrentManifest().getBuildBaseDirectory(); |
139 |
0 |
f = new File(f, getCurrentModule().getName()); |
140 |
0 |
f = new File(f, "build"); |
141 |
|
|
142 |
0 |
deps += f.getPath(); |
143 |
0 |
logger.debug("Setting 'module-classpath' to: "+deps); |
144 |
0 |
project.setProperty("module-classpath", deps); |
145 |
0 |
} catch (DependencyException d) { |
146 |
0 |
throw new CommandException(d.getErrorCode(), d.getMessageArguments()); |
147 |
0 |
} catch (ModuleTypeException d) { |
148 |
0 |
throw new CommandException(d.getErrorCode(), d.getMessageArguments()); |
149 |
0 |
} |
150 |
|
|
151 |
|
try { |
152 |
0 |
project.executeTarget("run"); |
153 |
0 |
} catch (BuildException e) { |
154 |
0 |
logger.info(e.getMessage(), e); |
155 |
0 |
throw new CommandException(CommandException.TEST_FAILED, class="keyword">new Object[] {getCurrentModule().getName()}); |
156 |
0 |
} |
157 |
|
|
158 |
|
|
159 |
0 |
commandResponse.addEvent(new MessageEvent(this, class="keyword">new SimpleMessage("Module " + getCurrentModule().getName() + " tested succesfully."))); |
160 |
0 |
} |
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
public CommandResponse getCommandResponse() { |
168 |
0 |
return this.commandResponse; |
169 |
|
} |
170 |
|
} |