1
2
3 package org.virtualmock.taskdef;
4
5 import java.io.File;
6 import org.apache.tools.ant.BuildException;
7 import org.apache.tools.ant.taskdefs.Java;
8 import org.apache.tools.ant.taskdefs.optional.junit.JUnitTask;
9 import org.apache.tools.ant.types.Commandline.Argument;
10 import org.apache.tools.ant.types.Path;
11
12
13 /***
14 * Custom Ant task to execute JUnit tests in VirtualMock environment.
15 *
16 * @author Chad Woolley
17 * @version $Revision: 1.6 $
18 *
19 * @todo What's the easiest way to write a unit test for an Ant task?
20 */
21 public class VMUnitTask extends JUnitTask {
22 /*** The classpath. */
23 private Path classpath = null;
24
25 /***
26 * Overridden constructor from superclass.
27 *
28 * @throws Exception if any exception occurs in constructor
29 */
30 public VMUnitTask() throws Exception {
31 super();
32 System.out.println("!!!!!!!!!!!!!!!!! VMUNIT CONSTRUCTOR");
33 }
34
35 /***
36 * Overridden method to handle classpath. It will take the classpath that
37 * is currently specified, and append all elements required to support
38 * the VirtualMock environment.
39 *
40 * @return the modified classpath
41 *
42 * @todo Do we need to add anything to the classpath? If so, what?
43 * @todo What about forked mode?
44 */
45 public Path createClasspath() {
46
47 classpath = super.createClasspath();
48
49 return classpath;
50 }
51
52 /***
53 * Perform execution of the task, first performing necessary setup to run
54 * VirtualMock unit tests.
55 *
56 * @throws BuildException DOCUMENT ME! (Constructor Exception)
57 *
58 * @todo This is currently hardcoded for AspectJ
59 * @todo This is currently hardcoded for absolute path on Chad's machine.
60 * Make these task parameters
61 * @todo How can the transformed jar be put at the beginning of the
62 * classpath?
63 */
64 public void execute() throws BuildException {
65 String outjar = "d://temp//jar//_outjar_VM.jar";
66 String injars =
67 "d://virtualmock//virtualmock//lib//virtualmock-0.1.2.jar;"
68 + "d://virtualmock//virtualmock//lib//virtualmock-test-0.1.2.jar";
69
70
71 Java java = new Java();
72
73
74 java.setFork(false);
75
76
77 String args = "-outjar " + outjar + " -injars " + injars;
78
79
80
81 java.setClassname("org.aspectj.tools.ajc.Main");
82
83 Argument arg = java.createArg();
84 arg.setLine(args);
85
86 java.setClasspath(classpath);
87
88
89 int returnCode = -1;
90
91 System.out.println("!!!!!!!!!!!!!!!!! EXECUTING COMMAND, ARGS=" + args);
92 System.out.println("!!!!!!!!!!!!!!!!! EXECUTING COMMAND, CLASSPATH="
93 + classpath);
94 System.out.println("!!!!!!!!!!!!!!!!! PROJECT=" + this.getProject());
95 java.setProject(project);
96 System.out.println("!!!!!!!!!!!!!!!!! JAVA PROJECT="
97 + java.getProject());
98
99 java.setFork(true);
100 returnCode = java.executeJava();
101
102 System.out.println("!!!!!!!!!!!!!!!!! EXECUTE RC=" + returnCode);
103
104 if (returnCode == 0) {
105
106
107 Path oldClasspath = (Path) classpath.clone();
108 Path newClasspath = new Path(project);
109 newClasspath.setLocation(new File(outjar));
110 newClasspath.addExisting(oldClasspath);
111 classpath = newClasspath;
112 super.setFork(true);
113
114 System.out.println("!!!!!!!!!!!!!!!!! EXECUTING JUNIT, CLASSPATH="
115 + classpath);
116 super.execute();
117
118 return;
119 }
120
121 System.out.println("VirtualMock transformation failed, return code = "
122 + returnCode);
123 }
124 }