1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/* *******************************************************************
* Copyright (c) 1999-2001 Xerox Corporation,
* 2002 Palo Alto Research Center, Incorporated (PARC).
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Common Public License v1.0
* which accompanies this distribution and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* Xerox/PARC initial implementation
* ******************************************************************/
package org.aspectj.tools.ant.taskdefs;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.selectors.FilenameSelector;
import org.aspectj.bridge.IMessage;
import org.aspectj.bridge.IMessageHolder;
import org.aspectj.bridge.MessageHandler;
import org.aspectj.bridge.MessageUtil;
import org.aspectj.util.FileUtil;
import org.aspectj.util.LangUtil;
import java.io.File;
import junit.framework.TestCase;
/**
*
*/
public class AjcTaskTest extends TestCase {
private static final Class NO_EXCEPTION = null;
private static final String NOFILE = "NOFILE";
private static File tempDir;
private static void deleteTempDir() {
if ((null != tempDir) && tempDir.exists()) {
FileUtil.deleteContents(tempDir);
tempDir.delete();
}
}
private static final File getTempDir() {
if (null == tempDir) {
tempDir = new File("IncrementalAjcTaskTest-temp");
}
return tempDir;
}
public AjcTaskTest(String name) {
super(name);
}
public void tearDown() {
deleteTempDir();
}
protected AjcTask getTask(String input) {
AjcTask task = new AjcTask();
Project p = new Project();
task.setProject(p);
task.setDestdir(getTempDir());
if (NOFILE.equals(input)) {
// add nothing
} else if (input.endsWith(".lst")) {
if (-1 != input.indexOf(",")) {
throw new IllegalArgumentException("lists not supported: " + input);
} else {
task.setArgfiles(new Path(task.getProject(), input));
}
} else if ((input.endsWith(".java") || input.endsWith(".aj"))) {
// not working
FilenameSelector fns = new FilenameSelector();
fns.setName(input);
task.addFilename(fns);
} else {
File dir = new File(input);
if (dir.canRead() && dir.isDirectory()) {
task.setSourceRoots(new Path(task.getProject(), input));
}
}
task.setClasspath(new Path(p, "../lib/test/aspectjrt.jar"));
task.setVerbose(true); // XXX
return task;
}
// ---------------------------------------- argfile
public void testDefaultListForked() {
AjcTask task = getTask("testdata/default.lst");
task.setFork(true);
runTest(task, NO_EXCEPTION, IMessageHolderChecker.NONE);
}
public void testCompileErrorListForked() {
AjcTask task = getTask("testdata/compileError.lst");
task.setFork(true);
runTest(task, NO_EXCEPTION, IMessageHolderChecker.NONE);
}
public void testCompileErrorListForkedFailonerror() {
AjcTask task = getTask("testdata/compileError.lst");
task.setFork(true);
task.setFailonerror(true);
runTest(task, BuildException.class, IMessageHolderChecker.NONE);
}
public void testDefaultFileForked() {
AjcTask task = getTask("testdata/Default.java");
task.setFork(true);
runTest(task, NO_EXCEPTION, IMessageHolderChecker.NONE);
}
public void testDefaultList() {
AjcTask task = getTask("testdata/default.lst");
runTest(task, NO_EXCEPTION, IMessageHolderChecker.INFOS);
}
public void testCompileErrorList() {
AjcTask task = getTask("testdata/compileError.lst");
runTest(task, NO_EXCEPTION, IMessageHolderChecker.ONE_ERROR);
}
public void testCompileWarningList() {
AjcTask task = getTask("testdata/compileWarning.lst");
runTest(task, NO_EXCEPTION, IMessageHolderChecker.ONE_WARNING);
}
public void testNoSuchFileList() {
AjcTask task = getTask("testdata/NoSuchFile.lst");
runTest(task, NO_EXCEPTION, IMessageHolderChecker.ONE_ERROR_ONE_ABORT);
}
public void testClasspath() {
AjcTask task = getTask(NOFILE);
String[] cmd = task.setupCommand(true);
String classpath = null;
String bootclasspath = null;
for (int i = 0; i < cmd.length; i++) {
if ("-classpath".equals(cmd[i])) {
classpath = cmd[i+1];
} else if ("-bootclasspath".equals(cmd[i])) {
bootclasspath = cmd[i+1];
}
}
assertTrue("not expecting bootclasspath",
null == bootclasspath);
assertTrue("expecting aspectj in classpath",
(-1 != classpath.indexOf("aspectjrt.jar")));
}
// ---------------------------------------- sourcefile
// XXX need to figure out how to specify files directly programmatically
// public void testDefaultFile() {
// AjcTask task = getTask("testdata/Default.java");
// runTest(task, NO_EXCEPTION, IMessageHolderChecker.INFOS);
// }
public void testNoFile() {
AjcTask task = getTask(NOFILE);
runTest(task, NO_EXCEPTION, IMessageHolderChecker.ONE_ERROR_ONE_ABORT);
}
// -------- comment-disabled tests
// ---------------------------------------- ant drivers?
// doesn't work..
// public void testAntScript() {
// Ant ant = new Ant();
// ant.setProject(new Project());
// ant.setDir(new File("."));
// ant.setAntfile("test-build.xml");
// ant.execute();
// }
// XXX find out how to feed files into MatchingTask
// public void testCompileErrorFile() {
// AjcTask task = getTask("testdata/CompilerError.java");
// runTest(task, NO_EXCEPTION, IMessageHolderChecker.ONE_ERROR);
// }
//
// public void testCompileWarningFile() {
// AjcTask task = getTask("testdata/CompilerWarning.lst");
// runTest(task, NO_EXCEPTION, IMessageHolderChecker.ONE_WARNING);
// }
//
// public void testNoSuchFile() {
// AjcTask task = getTask("testdata/NoSuchFile.java");
// runTest(task, NO_EXCEPTION, IMessageHolderChecker.ONE_ERROR);
// }
//
// public void testDefaultFileComplete() {
// AjcTask task = getTask("testdata/Default.java");
// task.setDebugLevel("none");
// task.setDeprecation(true);
// task.setFailonerror(false);
// task.setNoExit(true); // ok to override Ant?
// task.setNoImportError(true);
// task.setNowarn(true);
// task.setNoweave(true);
// task.setPreserveAllLocals(true);
// task.setProceedOnError(true);
// task.setReferenceInfo(true);
// task.setSource("1.3");
// task.setTarget("1.1");
// task.setTime(true);
// task.setVerbose(true);
// task.setXlintenabled(true);
// runTest(task, NO_EXCEPTION, IMessageHolderChecker.INFOS);
// }
protected void runTest(
AjcTask task,
Class exceptionType,
IMessageHolderChecker checker) {
Throwable thrown = null;
MessageHandler holder = new MessageHandler();
task.setMessageHolder(holder);
try {
task.execute();
} catch (Throwable t) {
thrown = t;
} finally {
deleteTempDir();
}
if (null == exceptionType) {
if (null != thrown) {
assertTrue("thrown: " + render(thrown), false);
}
} else if (null == thrown) {
assertTrue("expected " + exceptionType.getName(), false);
} else if (!(exceptionType.isAssignableFrom(thrown.getClass()))) {
assertTrue("expected " + exceptionType.getName()
+ " got " + render(thrown), false);
}
if (null == checker) {
checker = IMessageHolderChecker.NONE;
}
checker.check(holder);
}
protected String render(Throwable thrown) {
return LangUtil.renderException(thrown);
}
static class IMessageHolderChecker { // XXX export to testing-utils
/** use as value to ignore results */
static int IGNORE = Integer.MIN_VALUE;
static IMessageHolderChecker NONE =
new IMessageHolderChecker(0,0,0,0,0);
/** any number (0+) of info messages */
static IMessageHolderChecker INFOS =
new IMessageHolderChecker(0,0,0,0,IGNORE);
/** one error, any number of info messages */
static IMessageHolderChecker ONE_ERROR=
new IMessageHolderChecker(0,0,1,0,IGNORE);
static IMessageHolderChecker ONE_ERROR_ONE_ABORT =
new IMessageHolderChecker(1,0,1,0,IGNORE);
/** one warning, any number of info messages */
static IMessageHolderChecker ONE_WARNING =
new IMessageHolderChecker(0,0,0,1,IGNORE);
int aborts, fails, errors, warnings, infos;
public IMessageHolderChecker(int aborts, int fails, int errors, int warnings, int infos) {
this.aborts = aborts;
this.fails = fails;
this.errors = errors;
this.warnings = warnings;
this.infos = infos;
}
public void check(IMessageHolder holder) {
boolean failed = true;
try {
check(holder, aborts, IMessage.ABORT);
check(holder, fails, IMessage.FAIL);
check(holder, errors, IMessage.ERROR);
check(holder, warnings,IMessage.WARNING);
check(holder, infos, IMessage.INFO);
failed = false;
} finally {
if (failed) {
MessageUtil.print(System.err, holder, "failed?");
}
}
}
private void check(IMessageHolder holder, int num, IMessage.Kind kind) {
if (num != IGNORE) {
int actual = holder.numMessages(kind, false);
if (num != actual) {
if (actual > 0) {
MessageUtil.print(System.err, holder, kind + " expected " + num + " got " + actual);
}
assertEquals(kind.toString(), num, actual);
}
}
}
}
}
|