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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
|
/* *******************************************************************
* Copyright (c) 1999-2001 Xerox Corporation,
* 2002 Palo Alto Research Center, Incorporated (PARC)
* 2003 Contributors.
* 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.*;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.selectors.FilenameSelector;
import org.aspectj.bridge.*;
import org.aspectj.util.*;
import java.io.*;
import java.io.File;
import java.util.Arrays;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import junit.framework.TestCase;
/**
* Some API tests, but mostly functional tests driving
* the task execute using data in ../taskdefs/testdata.
* This will re-run in forked mode for any nonfailing
* compile if aspectjtools-dist is built into
* ../aj-build/dist/tools/lib/aspectjtools.jar.
*/
public class AjcTaskTest extends TestCase {
private static final Class NO_EXCEPTION = null;
private static final String NOFILE = "NOFILE";
private static final File tempDir;
private static final String aspectjtoolsJar;
private static final String testdataDir;
private static final StringBuffer MESSAGES = new StringBuffer();
/** accept writable .class files */
private static FileFilter PICK_CLASS_FILES;
static {
tempDir = new File("IncrementalAjcTaskTest-temp");
String toolsPath = "../aj-build/dist/tools/lib/aspectjtools.jar";
File toolsjar = new File(toolsPath);
if (toolsjar.canRead()) {
aspectjtoolsJar = toolsjar.getAbsolutePath();
} else {
aspectjtoolsJar = null;
String s =
"AjcTaskTest not forking - build aspectjtools-dist to get "
+ toolsPath;
System.out.println(s);
}
File dir = new File("../taskdefs/testdata");
if (dir.canRead() && dir.isDirectory()) {
testdataDir = dir.getAbsolutePath();
} else {
testdataDir = null;
}
PICK_CLASS_FILES = new FileFilter() {
public boolean accept(File file) {
return (
(null != file)
&& file.isFile()
&& file.canWrite()
&& file.getPath().endsWith(".class"));
}
};
}
/**
* Check that aspectjtools are found on the classpath,
* reporting any errors to System.err.
*
* Run multiple times with different classpaths.
* This should find variants
* aspectjtools.jar,
* aspectj-tools.jar,
* aspectj-tools-1.1.jar, and
* aspectjtools-1.0.6.jar
* but not
* aspectjrt.jar or
* aspectj/tools.jar.
* XXX use testing aspect to stub out
* <code>System.getProperty("java.class.path")</code>
* @param args a String[], first is expected path, if any
*/
public static void main(String[] args) {
java.io.File toolsjar = AjcTask.findAspectjtoolsJar();
if ((null == args) || (0 == args.length)) {
if (null != toolsjar) {
System.err.println("FAIL - not expected: " + toolsjar);
}
} else if ("-help".equals(args[0])) {
System.out.println(
"java "
+ AjcTaskTest.class.getName()
+ " <expectedPathToAspectjtoolsJar>");
} else if (null == toolsjar) {
System.err.println("FAIL - expected: " + args[0]);
} else {
String path = toolsjar.getAbsolutePath();
if (!path.equals(args[0])) {
System.err.println(
"FAIL - expected: " + args[0] + " actual: " + path);
}
}
}
public static void collectMessage(String s) {
MESSAGES.append(s);
}
private static void deleteTempDir() {
if ((null != tempDir) && tempDir.exists()) {
FileUtil.deleteContents(tempDir);
tempDir.delete();
// when tempDir not used...
if (null != testdataDir) {
File dataDir = new File(testdataDir);
if (dataDir.canRead()) {
FileUtil.deleteContents(dataDir, PICK_CLASS_FILES, false);
}
}
}
}
private static final File getTempDir() {
return tempDir;
}
public AjcTaskTest(String name) {
super(name);
}
public void tearDown() {
deleteTempDir();
MESSAGES.setLength(0);
}
public void testNullDestDir() {
AjcTask task = getTask(NOFILE, null);
String[] cmd = task.makeCommand();
for (int i = 0; i < cmd.length; i++) {
assertTrue(!"-d".equals(cmd[i]));
}
}
public void testOutputRequirement() {
AjcTask task = getTask("default.lst");
checkRun(task, null);
// copyInJars now just emits warning b/c unused
task = getTask("default.lst", null);
task.setCopyInjars(true);
checkRun(task, null);
// sourceRootCopyFilter requires destDir
task = getTask("default.lst", null);
task.setSourceRootCopyFilter("**/*.java");
checkRun(task, "sourceRoot");
}
public void testSourceRootCopyFilter() {
// sourceRootCopyFilter works..
File destDir = getTempDir();
assertTrue(
"unable to create " + destDir,
destDir.canRead() || destDir.mkdirs());
AjcTask task = getTask("sourceroot", destDir);
task.setSourceRootCopyFilter("doNotCopy,**/*.txt");
File file = new File(destDir, "Default.java").getAbsoluteFile();
assertTrue(file + ".canRead() prematurely", !file.canRead());
checkRun(task, null);
// got expected resources
assertTrue(file + ".canRead() failed", file.canRead());
File pack = new File(destDir, "pack");
file = new File(pack, "Pack.java").getAbsoluteFile();
assertTrue(file + ".canRead() failed", file.canRead());
file = new File(pack, "includeme").getAbsoluteFile();
assertTrue(file + ".canRead() failed", file.canRead());
// didn't get unexpected resources
file = new File(pack, "something.txt");
assertTrue(file + ".canRead() passed", !file.canRead());
file = new File(destDir, "doNotCopy");
assertTrue(file + ".canRead() passed", !file.canRead());
file = new File(destDir, "skipTxtFiles.txt");
assertTrue(file + ".canRead() passed", !file.canRead());
}
public void testInpathDirCopyFilter() {
// inpathDirCopyFilter works with output directory
File destDir = getTempDir();
assertTrue(
"unable to create " + destDir,
destDir.canRead() || destDir.mkdirs());
AjcTask task = getTask(NOFILE, destDir);
Project p = task.getProject();
Path indirs = new Path(p);
File dir = new File(testdataDir, "inpathDirs").getAbsoluteFile();
indirs.addExisting(new Path(p, new File(dir, "inpathDirOne").getAbsolutePath()));
indirs.addExisting(new Path(p, new File(dir, "inpathDirTwo").getAbsolutePath()));
task.setInpath(indirs);
task.setInpathDirCopyFilter("doNotCopy,**/*.txt");
File file = new File(destDir, "Default.java").getAbsoluteFile();
assertTrue(file + ".canRead() prematurely", !file.canRead());
checkRun(task, null);
// got expected resources
File pack = new File(destDir, "pack");
file = new File(pack, "includeme").getAbsoluteFile();
assertTrue(file + ".canRead() failed", file.canRead());
file = new File(pack, "Pack.class").getAbsoluteFile();
assertTrue(file + ".canRead() failed", file.canRead());
file = new File(destDir, "copyMe.htm").getAbsoluteFile();
assertTrue(file + ".canRead() failed", file.canRead());
file = new File(destDir, "Default.class").getAbsoluteFile();
assertTrue(file + ".canRead() failed", file.canRead());
// didn't get unexpected resources
file = new File(pack, "something.txt");
assertTrue(file + ".canRead() passed", !file.canRead());
file = new File(destDir, "doNotCopy");
assertTrue(file + ".canRead() passed", !file.canRead());
file = new File(destDir, "skipTxtFiles.txt");
assertTrue(file + ".canRead() passed", !file.canRead());
}
public void testInpathDirCopyFilterWithJar() throws IOException {
// inpathDirCopyFilter works with output jar
File destDir = getTempDir();
assertTrue(
"unable to create " + destDir,
destDir.canRead() || destDir.mkdirs());
AjcTask task = getTask(NOFILE, null);
File destJar = new File(destDir, "testInpathDirCopyFilterWithJar-out.jar");
task.setOutjar(destJar);
Project p = task.getProject();
Path indirs = new Path(p);
File dir = new File(testdataDir, "inpathDirs").getAbsoluteFile();
indirs.addExisting(new Path(p, new File(dir, "inpathDirOne").getAbsolutePath()));
indirs.addExisting(new Path(p, new File(dir, "inpathDirTwo").getAbsolutePath()));
task.setInpath(indirs);
task.setInpathDirCopyFilter("doNotCopy,**/*.txt,**/*.class");
checkRun(task, null);
JarFile jarFile = new JarFile(destJar);
String[] expected = {"copyMe.htm", "pack/includeme",
"pack/Pack.class", "Default.class"};
String[] unexpected = {"doNotCopy", "skipTxtFiles.txt", "pack/something.txt"};
for (int i = 0; i < expected.length; i++) {
JarEntry entry = jarFile.getJarEntry(expected[i]);
assertTrue(expected[i] + " not found", null != entry);
}
for (int i = 0; i < unexpected.length; i++) {
JarEntry entry = jarFile.getJarEntry(unexpected[i]);
assertTrue(unexpected[i] + " found", null == entry);
}
}
public void testInpathDirCopyFilterError() {
// inpathDirCopyFilter fails with no output directory or jar iff specified
AjcTask task = getTask(NOFILE, null);
Project p = task.getProject();
Path indirs = new Path(p);
File dir = new File(testdataDir, "inpathDirs").getAbsoluteFile();
indirs.addExisting(new Path(p, new File(dir, "inpathDirOne").getAbsolutePath()));
indirs.addExisting(new Path(p, new File(dir, "inpathDirTwo").getAbsolutePath()));
task.setInpath(indirs);
task.setInpathDirCopyFilter("doNotCopy,**/*.txt,**/*.class");
// expecting error
checkRun(task, "inpathDirCopyFilter");
}
private void checkRun(AjcTask task, String exceptionString) {
try {
task.execute();
assertTrue(null == exceptionString);
} catch (BuildException e) {
if (null == exceptionString) {
assertTrue("unexpected " + e.getMessage(), false);
} else {
String m = e.getMessage();
if (null == m) {
assertTrue("not " + exceptionString, false);
} else if (-1 == m.indexOf(exceptionString)) {
assertEquals(exceptionString, e.getMessage());
}
}
}
}
public void testCommandEditor() {
String className = VerboseCommandEditor.class.getName();
System.setProperty(AjcTask.COMMAND_EDITOR_NAME, className);
assertEquals(
className,
System.getProperty(AjcTask.COMMAND_EDITOR_NAME));
AjcTask task = getTask(NOFILE);
task.setCommandEditor(new VerboseCommandEditor());
String[] cmd = task.makeCommand();
assertEquals(VerboseCommandEditor.VERBOSE, cmd[0]);
task = getTask(NOFILE);
task.setCommandEditorClass(VerboseCommandEditor.class.getName());
cmd = task.makeCommand();
assertEquals(VerboseCommandEditor.VERBOSE, cmd[0]);
}
// public void testStaticCommandEditor() {
// // XXX need to test COMMAND_EDITOR, but can't require property when run
// }
public void testLimitTo() {
int numArgs = 100;
String arg = "123456789";
String[] args = new String[numArgs];
for (int i = 0; i < args.length; i++) {
args[i] = arg;
}
// no limit
int max = numArgs * (arg.length() + 1);
Location location = new Location("AjcTaskTest.java");
String[] newArgs = AjcTask.GuardedCommand.limitTo(args, max, location);
assertTrue("same", args == newArgs);
// limited - read file and verify arguments
max--;
newArgs = AjcTask.GuardedCommand.limitTo(args, max, location);
assertTrue("not same", args != newArgs);
assertTrue("not null", null != newArgs);
String label = "newArgs " + Arrays.asList(newArgs);
assertTrue("size 2" + label, 2 == newArgs.length);
assertEquals("-argfile", newArgs[0]);
File file = new File(newArgs[1]);
assertTrue("readable newArgs[1]" + label, file.canRead());
FileReader fin = null;
try {
fin = new FileReader(file);
BufferedReader reader = new BufferedReader(fin);
String line;
int i = 0;
while (null != (line = reader.readLine())) {
assertEquals(i + ": ", args[i++], line);
}
assertEquals("num entries", i, args.length);
} catch (IOException e) {
assertTrue("IOException " + e.getMessage(), false);
} finally {
if (null != fin) {
try {
fin.close();
} catch (IOException e) {
// ignore
}
}
file.delete();
}
}
public void testFindAspectjtoolsJar() {
File toolsJar = AjcTask.findAspectjtoolsJar();
if (null != toolsJar) {
assertNull("tools jar found?: " + toolsJar, toolsJar);
}
// not found when unit testing b/c not on system classpath
// so just checking for exceptions.
// XXX need aspect to stub out System.getProperty(..)
}
private void checkContains(String[] cmd, String option, boolean contains) {
for (int i = 0; i < cmd.length; i++) {
if (option.equals(cmd[i])) {
if (contains) {
return;
} else {
assertTrue(
"not expecting " + option + " in " + Arrays.asList(cmd),
false);
}
}
}
if (contains) {
assertTrue(
"expecting " + option + " in " + Arrays.asList(cmd),
false);
}
}
protected AjcTask getTask(String input) {
return getTask(input, getTempDir());
}
protected AjcTask getTask(String input, File destDir) {
AjcTask task = new AjcTask();
Project p = new Project();
task.setProject(p);
if (null != destDir) {
task.setDestdir(destDir);
}
if (NOFILE.equals(input)) {
// add nothing
} else if (input.endsWith(".lst")) {
if (-1 != input.indexOf(",")) {
throw new IllegalArgumentException(
"lists not supported: " + input);
} else if (null == testdataDir) {
throw new Error("testdata not found - run in ../taskdefs");
} else {
String path = testdataDir + File.separator + input;
task.setArgfiles(new Path(task.getProject(), path));
}
} else if ((input.endsWith(".java") || input.endsWith(".aj"))) {
FilenameSelector fns = new FilenameSelector();
fns.setName(input);
task.addFilename(fns);
} else {
String path = testdataDir + File.separator + input;
task.setSourceRoots(new Path(task.getProject(), path));
}
task.setClasspath(new Path(p, "../lib/test/aspectjrt.jar"));
return task;
}
/** used in testMessageHolderClassName */
public static class InfoHolder extends MessageHandler {
public InfoHolder() {
}
public boolean handleMessage(IMessage message) {
if (0 == IMessage.INFO.compareTo(message.getKind())) {
AjcTaskTest.collectMessage(message.getMessage());
}
return true;
}
}
/** used in testMessageHolderClassName */
public static class Holder extends MessageHandler {
public Holder() {
}
public boolean handleMessage(IMessage message) {
IMessage.Kind kind = message.getKind();
if (IMessage.ERROR.isSameOrLessThan(kind)) {
String m = kind.toString();
AjcTaskTest.collectMessage(m.substring(0, 1));
}
return true;
}
}
public void testMessageHolderClassName() {
AjcTask task = getTask("compileError.lst");
task.setFailonerror(false);
MESSAGES.setLength(0);
runTest(
task,
null,
MessageHolderChecker.ONE_ERROR,
Holder.class.getName());
String result = MESSAGES.toString();
MESSAGES.setLength(0);
assertEquals("messages", "e", result);
}
// TODO skipped test - works locally but not on build machine?
public void skip_testMessageHolderClassWithDoneSignal() {
AjcTask task = getTask("default.lst");
task.setFailonerror(false);
String DONE = "This is a unique message, not confused with others.";
task.setXDoneSignal(DONE);
MESSAGES.setLength(0);
runTest(
task,
null,
MessageHolderChecker.INFOS,
InfoHolder.class.getName());
final String result = MESSAGES.toString();
String temp = new String(result);
MESSAGES.setLength(0);
if (!temp.endsWith(DONE)) {
if (temp.length() > 20) {
temp = "..." + temp.substring(temp.length()-20, temp.length());
}
assertTrue(DONE + " is not suffix of \"" + temp + "\"", false);
}
// exactly one such message
temp = new String(result);
temp = temp.substring(0, temp.length()-DONE.length());
if (temp.endsWith(DONE)) {
temp = new String(result);
if (temp.length() > 20) {
temp = "..." + temp.substring(temp.length()-20, temp.length());
}
assertTrue(DONE + " signalled twice: \"" + temp + "\"", false);
}
}
public void testDefaultListForkedNoTools() {
AjcTask task = getTask("default.lst");
task.setFork(true);
boolean passed = false;
try {
runTest(task, BuildException.class, MessageHolderChecker.NONE);
passed = true;
} finally {
if (!passed) {
String m =
"AjcTaskTest.testDefaultListForkedNoTools()"
+ " fails if aspectjtools.jar is on the classpath";
System.err.println(m);
}
}
}
public void testDefaultListForkedIncremental() {
AjcTask task = getTask("default.lst");
task.setFork(true);
task.setIncremental(true);
runTest(task, BuildException.class, MessageHolderChecker.NONE);
}
/** failonerror should default to true, unlike other booleans */
public void testCompileErrorFailOnErrorDefault() {
AjcTask task = getTask("compileError.lst");
runTest(task, BuildException.class, MessageHolderChecker.ONE_ERROR);
}
public void testDefaultList() {
AjcTask task = getTask("default.lst");
runTest(task, NO_EXCEPTION, MessageHolderChecker.INFOS);
}
public void testCompileErrorList() {
AjcTask task = getTask("compileError.lst");
task.setFailonerror(false);
runTest(task, NO_EXCEPTION, MessageHolderChecker.ONE_ERROR);
}
public void testShowWeaveInfo() {
AjcTask task = getTask("showweaveinfo.lst");
task.setShowWeaveInfo(true);
MessageHandler mh = new MessageHandler(false);
mh.dontIgnore(IMessage.WEAVEINFO);
MessageHolderChecker mhc = new MessageHolderChecker(0,0,0,0,MessageHolderChecker.IGNORE);
mhc.weaveinfos = 2; // Expect 2 weaving messages
runTest(task,NO_EXCEPTION,mhc);
mhc.weaveinfos = MessageHolderChecker.IGNORE;
}
public void testCompileWarningList() {
AjcTask task = getTask("compileWarning.lst");
runTest(task, NO_EXCEPTION, MessageHolderChecker.ONE_WARNING);
}
public void testNoSuchFileList() {
AjcTask task = getTask("NoSuchFile.lst");
task.setFailonerror(false);
runTest(task, NO_EXCEPTION, MessageHolderChecker.ONE_ERROR_ONE_ABORT);
}
public void testVersions() {
String[] inputs = AjcTask.TARGET_INPUTS;
for (int i = 0; i < inputs.length; i++) {
AjcTask task = getTask(NOFILE);
task.setTarget(inputs[i]);
String[] cmd = task.makeCommand();
checkContains(cmd, "-target", true);
checkContains(cmd, inputs[i], true);
}
inputs = AjcTask.SOURCE_INPUTS;
for (int i = 0; i < inputs.length; i++) {
AjcTask task = getTask(NOFILE);
task.setSource(inputs[i]);
String[] cmd = task.makeCommand();
checkContains(cmd, "-source", true);
checkContains(cmd, inputs[i], true);
}
inputs = AjcTask.COMPLIANCE_INPUTS;
for (int i = 0; i < inputs.length; i++) {
AjcTask task = getTask(NOFILE);
task.setCompliance(inputs[i]);
String[] cmd = task.makeCommand();
checkContains(cmd, inputs[i], true);
}
}
public void testClasspath() {
AjcTask task = getTask(NOFILE);
String[] cmd = task.makeCommand();
checkContains(cmd, "-bootclasspath", false);
String classpath = null;
for (int i = 0; i < cmd.length; i++) {
if ("-classpath".equals(cmd[i])) {
classpath = cmd[i + 1];
break;
}
}
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, MessageHolderChecker.INFOS);
// }
public void testNoFile() {
AjcTask task = getTask(NOFILE);
task.setFailonerror(false);
runTest(task, NO_EXCEPTION, MessageHolderChecker.ONE_ERROR_ONE_ABORT);
}
public void testCompileErrorFile() {
AjcTask task = getTask("compileError.lst");
task.setFailonerror(false);
runTest(task, NO_EXCEPTION, MessageHolderChecker.ONE_ERROR);
}
public void testCompileWarningFile() {
AjcTask task = getTask("compileWarning.lst");
task.setFailonerror(false);
runTest(task, NO_EXCEPTION, MessageHolderChecker.ONE_WARNING);
}
public void testNoSuchFile() {
AjcTask task = getTask("NoSuchFile.lst");
task.setFailonerror(false);
runTest(task, NO_EXCEPTION, MessageHolderChecker.ONE_ERROR_ONE_ABORT);
}
public void testDefaultFileComplete() {
AjcTask task = getTask("default.lst");
task.setDebugLevel("none");
task.setDeprecation(true);
task.setFailonerror(false);
task.setNoExit(true); // ok to override Ant?
task.setNoImportError(true);
task.setNowarn(true);
task.setXNoweave(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.setXlint("info");
runTest(task, NO_EXCEPTION, MessageHolderChecker.INFOS);
}
public void testXOptions() {
String[] xopts = new String[] {
"serializableAspects",
"lazyTjp",
"reweavable",
"reweavable:compress",
"noInline"
};
for (int i = 0; i < xopts.length; i++) {
AjcTask task = getTask(NOFILE);
task.setX(xopts[i]);
String[] cmd = task.makeCommand();
checkContains(cmd,"-X" + xopts[i],true);
}
}
protected void runTest(
AjcTask task,
Class exceptionType,
MessageHolderChecker checker,
String messageHolderClass) {
task.setMessageHolderClass(messageHolderClass);
runTest(task, exceptionType, checker, (MessageHandler) null);
}
protected void runTest(
AjcTask task,
Class exceptionType,
MessageHolderChecker checker) {
MessageHandler holder = new MessageHandler();
task.setMessageHolder(holder);
runTest(task, exceptionType, checker, holder);
}
protected void runTest(
AjcTask task,
Class exceptionType,
MessageHolderChecker checker,
MessageHandler holder) {
Throwable thrown = null;
// re-run forked iff tools.jar and expect to pass
boolean rerunForked =
((null != aspectjtoolsJar)
&& (null == exceptionType)
&& ((null == checker) || !checker.expectFail()));
String label = "same-vm ";
while (true) { // same vm, then perhaps forked
try {
task.execute();
} catch (Throwable t) {
thrown = t;
} finally {
deleteTempDir();
}
if (null == exceptionType) {
if (null != thrown) {
assertTrue(label + "thrown: " + render(thrown), false);
}
} else if (null == thrown) {
assertTrue(
label + "expected " + exceptionType.getName(),
false);
} else if (!(exceptionType.isAssignableFrom(thrown.getClass()))) {
assertTrue(
label
+ "expected "
+ exceptionType.getName()
+ " got "
+ render(thrown),
false);
}
if (null != holder) {
if (null == checker) {
checker = MessageHolderChecker.NONE;
}
checker.check(holder, label);
}
if (!rerunForked) {
break;
} else {
label = "other-vm ";
rerunForked = false;
// can't reset without losing values...
task.setFork(true);
task.setFailonerror(true);
task.setForkclasspath(
new Path(task.getProject(), aspectjtoolsJar));
}
}
}
protected String render(Throwable thrown) {
return LangUtil.renderException(thrown);
}
static class MessageHolderChecker { // XXX export to testing-utils
/** use as value to ignore results */
static int IGNORE = Integer.MIN_VALUE;
static MessageHolderChecker NONE =
new MessageHolderChecker(0, 0, 0, 0, 0);
/** any number (0+) of info messages */
static MessageHolderChecker INFOS =
new MessageHolderChecker(0, 0, 0, 0, IGNORE);
/** one error, any number of info messages */
static MessageHolderChecker ONE_ERROR =
new MessageHolderChecker(0, 0, 1, 0, IGNORE);
static MessageHolderChecker ONE_ERROR_ONE_ABORT =
new MessageHolderChecker(1, 0, 1, 0, IGNORE);
/** one warning, any number of info messages */
static MessageHolderChecker ONE_WARNING =
new MessageHolderChecker(0, 0, 0, 1, IGNORE);
int aborts, fails, errors, warnings, infos;
int weaveinfos;
public MessageHolderChecker(
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;
this.weaveinfos = IGNORE;
}
public boolean expectFail() {
return (0 < (aborts + fails + errors));
}
public void check(IMessageHolder holder, String label) {
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);
check(holder, weaveinfos, IMessage.WEAVEINFO);
failed = false;
} finally {
if (failed) {
MessageUtil.print(System.err, holder, label + "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);
}
}
}
}
}
class SnoopingCommandEditor implements ICommandEditor {
private static final String[] NONE = new String[0];
String[] lastCommand;
public String[] editCommand(String[] command) {
lastCommand = (String[]) LangUtil.safeCopy(command, NONE);
return command;
}
public String[] lastCommand() {
return (String[]) LangUtil.safeCopy(lastCommand, NONE);
}
}
class VerboseCommandEditor implements ICommandEditor {
public static final String VERBOSE = "-verbose";
public String[] editCommand(String[] command) {
for (int i = 0; i < command.length; i++) {
if (VERBOSE.equals(command[i])) {
return command;
}
}
String[] result = new String[1 + command.length];
result[0] = VERBOSE;
System.arraycopy(result, 1, command, 0, command.length);
return result;
}
}
class AppendingCommandEditor implements ICommandEditor {
private static String[] NONE = new String[0];
public static ICommandEditor VERBOSE =
new AppendingCommandEditor(new String[] { "-verbose" }, NONE);
public static ICommandEditor INVALID =
new AppendingCommandEditor(NONE, new String[] { "-invalidOption" });
final String[] prefix;
final String[] suffix;
public AppendingCommandEditor(String[] prefix, String[] suffix) {
this.prefix = prefix;
this.suffix = suffix;
}
public String[] editCommand(String[] command) {
int len = command.length + prefix.length + suffix.length;
String[] result = new String[len];
System.arraycopy(result, 0, prefix, 0, prefix.length);
System.arraycopy(result, prefix.length, command, 0, command.length);
System.arraycopy(
result,
prefix.length + command.length,
suffix,
0,
suffix.length);
return result;
}
}
|