Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6aefdf66cf37ca37d9ca73ca1702090a024f35a1 (plain) (blame)
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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
/*******************************************************************************
 * Copyright (c) 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.compiler.tool.tests;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.logging.LogRecord;

import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileObject;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject.Kind;

import junit.framework.Test;

import org.eclipse.jdt.internal.compiler.batch.Main;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;

public class CompilerInvocationTests extends AbstractCompilerToolTest {
	static {
//		TESTS_NAMES = new String[] { "test000" };
//		TESTS_NUMBERS = new int[] { 5 };
//		TESTS_RANGE = new int[] { 1, -1 };
	}
public CompilerInvocationTests(String name) {
	super(name);
}
public static Test suite() {
	return buildUniqueComplianceTestSuite(CompilerInvocationTests.class, ClassFileConstants.JDK1_6);
}
public static Class<CompilerInvocationTests> testClass() {
	return CompilerInvocationTests.class;
}

protected void checkClassFiles(String[] fileNames) {
	for (int i = 0, l = fileNames.length; i < l; i++) {
		ClassFileReader reader = null;
		try {
			reader = ClassFileReader.read(new File(OUTPUT_DIR, fileNames[i]), true);
		} catch (ClassFormatException e) {
			fail("Class format exception for file " + fileNames[i]);
		} catch (IOException e) {
			fail("IO exception for file " + fileNames[i]);
		}
		assertNotNull("Could not read " + fileNames[i], reader);
		assertEquals("Wrong Java version for " + fileNames[i], ClassFileConstants.JDK1_6, reader.getVersion());
	}
}
void runTest(
		boolean shouldCompileOK, 
		String[] sourceFiles,
		StandardJavaFileManager standardJavaFileManager,
		List<String> options,
		String[] compileFileNames,
		String expectedOutOutputString,
		String expectedErrOutputString, 
		boolean shouldFlushOutputDirectory,
		String[] classFileNames) {
	super.runTest(
		shouldCompileOK,
		sourceFiles, 
		new CompilerInvocationTestsArguments(standardJavaFileManager, options, compileFileNames),
		expectedOutOutputString,
		expectedErrOutputString,
		shouldFlushOutputDirectory,
		null /* progress */);
	// TODO maxime introduce stderr comparison based upon specific diagnostic listener
	if (classFileNames != null) {
		checkClassFiles(classFileNames);
	}
}
class GetLocationDetector extends ForwardingStandardJavaFileManager<StandardJavaFileManager>  {
	private Location match;
	private boolean matchFound;
	GetLocationDetector(StandardJavaFileManager javaFileManager, Location location) {
		super(javaFileManager);
		this.match = location;
	}
	@Override
	public Iterable<? extends File> getLocation(Location location) {
		if (location == this.match) {
			this.matchFound = true;
		}
		return super.getLocation(location);
	}
	boolean matchFound() {
		return this.matchFound;
	}
}
abstract class GetJavaFileDetector extends ForwardingStandardJavaFileManager<StandardJavaFileManager>  {
	boolean matchFound;
	String discriminatingSuffix;
	GetJavaFileDetector(StandardJavaFileManager javaFileManager) {
		super(javaFileManager);
	}
	GetJavaFileDetector(StandardJavaFileManager javaFileManager,
			String discriminatingSuffix) {
		super(javaFileManager);
		this.discriminatingSuffix = discriminatingSuffix;
	}
	abstract JavaFileObject detector(JavaFileObject original);
	@Override
	public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) {
		return getJavaFileObjectsFromFiles(Arrays.asList(files));
	}
	@Override
	public Iterable<? extends JavaFileObject> getJavaFileObjects(
			String... names) {
		return getJavaFileObjectsFromStrings(Arrays.asList(names));
	}
	@Override
	public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(
			Iterable<? extends File> files) {
		ArrayList<JavaFileObject> result = new ArrayList<JavaFileObject>();
		for (JavaFileObject file: super.getJavaFileObjectsFromFiles(files)) {
			result.add(detector(file));
		}
		return result;
	}
	@Override
	public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(
			Iterable<String> names) {
		ArrayList<JavaFileObject> result = new ArrayList<JavaFileObject>();
		for (JavaFileObject file: getJavaFileObjectsFromStrings(names)) {
			result.add(detector(file));
		}
		return result;
	}
	@Override
	public Iterable<JavaFileObject> list(Location location, String packageName,
			Set<Kind> kinds, boolean recurse) throws IOException {
		ArrayList<JavaFileObject> result = new ArrayList<JavaFileObject>();
		for (JavaFileObject file: super.list(location, packageName, kinds, recurse)) {
			result.add(detector(file));
		}
		return result;
	}
}
class GetJavaFileForInputDetector extends GetJavaFileDetector  {
	private Kind discriminatingKind;
	GetJavaFileForInputDetector(StandardJavaFileManager javaFileManager) {
		super(javaFileManager);
		this.discriminatingKind = Kind.SOURCE;
	}
	GetJavaFileForInputDetector(StandardJavaFileManager javaFileManager,
			String discriminatingSuffix,
			Kind discriminatingKind) {
		super(javaFileManager, discriminatingSuffix);
		this.discriminatingKind = discriminatingKind;
	}
	class JavaFileInputDetector extends ForwardingJavaFileObject<JavaFileObject> {
		JavaFileInputDetector(JavaFileObject fileObject) {
			super(fileObject);
		}
		@Override
		public CharSequence getCharContent(boolean ignoreEncodingErrors)
				throws IOException {
			matchFound = true;
			return super.getCharContent(ignoreEncodingErrors);
		}
		@Override
		public InputStream openInputStream() throws IOException {
			matchFound = true;
			return super.openInputStream();
		}
		@Override
		public Reader openReader(boolean ignoreEncodingErrors)
				throws IOException {
			matchFound = true;
			return super.openReader(ignoreEncodingErrors);
		}
	}
	JavaFileObject detector(JavaFileObject original) {
		if (original != null && original.getKind() == this.discriminatingKind
				&& (this.discriminatingSuffix == null || original.getName().endsWith(this.discriminatingSuffix))) {
			return new JavaFileInputDetector(original);
		}
		return original;
	}
	@Override
	public FileObject getFileForInput(Location location, String packageName,
			String relativeName) throws IOException {
		FileObject result = 
			super.getFileForInput(location, packageName, relativeName);
		if (result instanceof JavaFileObject) {
			return detector((JavaFileObject) result);
		}
		return result;
	}
	@Override
	public JavaFileObject getJavaFileForInput(Location location,
			String className, Kind kind) throws IOException {
		return detector(super.getJavaFileForInput(location, className, kind));
	}
}
class GetJavaFileForOutputDetector extends GetJavaFileDetector  {
	GetJavaFileForOutputDetector(StandardJavaFileManager javaFileManager) {
		super(javaFileManager);
	}
	GetJavaFileForOutputDetector(StandardJavaFileManager javaFileManager,
			String discriminatingSuffix) {
		super(javaFileManager, discriminatingSuffix);
	}
	class JavaFileOutputDetector extends ForwardingJavaFileObject<JavaFileObject> {
		JavaFileOutputDetector(JavaFileObject fileObject) {
			super(fileObject);
		}
		@Override
		public OutputStream openOutputStream() throws IOException {
			matchFound = true;
			return super.openOutputStream();
		}
		@Override
		public Writer openWriter() throws IOException {
			matchFound = true;
			return super.openWriter();
		}
	}
	JavaFileObject detector(JavaFileObject original) {
		if (original != null && original.getKind() == Kind.CLASS
				&& (this.discriminatingSuffix == null || original.getName().endsWith(this.discriminatingSuffix))) {
			return new JavaFileOutputDetector(original);
		}
		return original;
	}
	@Override
	public FileObject getFileForOutput(Location location, String packageName,
			String relativeName, FileObject sibling) throws IOException {
		FileObject result = 
			super.getFileForOutput(location, packageName, relativeName, sibling);
		if (result instanceof JavaFileObject) {
			return detector((JavaFileObject) result);
		}
		return result;
	}
	@Override
	public JavaFileObject getJavaFileForOutput(Location location,
			String className, Kind kind, FileObject sibling) throws IOException {
		return detector(super.getJavaFileForOutput(location, className, kind, sibling));
	}
}
class SetLocationDetector extends ForwardingStandardJavaFileManager<StandardJavaFileManager>  {
	private Location match;
	private boolean matchFound;
	SetLocationDetector(StandardJavaFileManager javaFileManager, Location location) {
		super(javaFileManager);
		this.match = location;
	}
	@Override
	public void setLocation(Location location, Iterable<? extends File> path)
			throws IOException {
		if (location == this.match) {
			this.matchFound = true;
		}
		super.setLocation(location, path);
	}
	boolean matchFound() {
		return this.matchFound;
	}
}
class SubstringDetector extends java.util.logging.Logger {
	private String match;
	private boolean matchFound;
	SubstringDetector(String match) {
		super("SubstringDetector", null);
		this.match = match;
	}
	@Override
	public void log(LogRecord record) {
		if (!this.matchFound && record.getMessage().indexOf(this.match) != -1) {
			this.matchFound = true;
		}
	}
	boolean matchFound() {
		return this.matchFound;
	}
}
// most possibly basic test
public void test001_basic() {
	runTest(
		true /* shouldCompileOK */,
		new String [] { /* sourceFiles */
			"X.java",
			"public class X {}",
		}, 
		null /* standardJavaFileManager */,
		Arrays.asList("-d", OUTPUT_DIR) /* options */, 
		new String[] { /* compileFileNames */
			"X.java"
		}, 
		"" /* expectedOutOutputString */, 
		"" /* expectedErrOutputString */,
		true /* shouldFlushOutputDirectory */,
		new String[] { /* classFileNames */
			"X.class"
		});
}
// exploring -d / FileManager interaction
// -d changes CLASS_OUTPUT location
public void test002_dash_d_option() {
	StandardJavaFileManager javacStandardJavaFileManager =  JAVAC_COMPILER.getStandardFileManager(null, null, null); // will pick defaults up
	runTest(
		true /* shouldCompileOK */,
		new String [] { /* sourceFiles */
			"X.java",
			"public class X {}",
		}, 
		javacStandardJavaFileManager /* standardJavaFileManager */,
		Arrays.asList("-d", OUTPUT_DIR) /* options */, 
		new String[] { /* compileFileNames */
			"X.java"
		}, 
		"" /* expectedOutOutputString */, 
		"" /* expectedErrOutputString */,
		true /* shouldFlushOutputDirectory */,
		new String[] { /* classFileNames */
			"X.class"
		});
	assertEquals(OUTPUT_DIR, javacStandardJavaFileManager.getLocation(StandardLocation.CLASS_OUTPUT).toString());
}
// exploring -d / FileManager interaction
// -d changes CLASS_OUTPUT location (OUTPUT_DIR subdirectory)
public void test003_dash_d_option() {
	StandardJavaFileManager javacStandardJavaFileManager =  JAVAC_COMPILER.getStandardFileManager(null, null, null); // will pick defaults up
	String outputDir = OUTPUT_DIR + File.separator + "bin";
	runTest(
		true /* shouldCompileOK */,
		new String [] { /* sourceFiles */
			"src/X.java",
			"public class X {}",
		}, 
		javacStandardJavaFileManager /* standardJavaFileManager */,
		Arrays.asList("-d", outputDir) /* options */, 
		new String[] { /* compileFileNames */
			"src/X.java"
		}, 
		"" /* expectedOutOutputString */, 
		"" /* expectedErrOutputString */,
		true /* shouldFlushOutputDirectory */,
		new String[] { /* classFileNames */
			"bin/X.class"
		});
	assertEquals(outputDir, javacStandardJavaFileManager.getLocation(StandardLocation.CLASS_OUTPUT).toString());
}
// exploring -d / FileManager interaction
// ecj uses the output location from the javac standard Java file manager if it
// is set
public void test004_no_dash_d_option() throws IOException {
	File binDirectory = new File(OUTPUT_DIR + File.separator + "bin");
	binDirectory.mkdir();
	StandardJavaFileManager javacStandardJavaFileManager =  JAVAC_COMPILER.getStandardFileManager(null, null, null); // will pick defaults up
	javacStandardJavaFileManager.setLocation(
			StandardLocation.CLASS_OUTPUT, 
			Arrays.asList(binDirectory));
	runTest(
		true /* shouldCompileOK */,
		new String [] { /* sourceFiles */
			"src/X.java",
			"public class X {}",
		}, 
		javacStandardJavaFileManager /* standardJavaFileManager */,
		null /* options */, 
		new String[] { /* compileFileNames */
			"src/X.java"
		}, 
		"" /* expectedOutOutputString */, 
		"" /* expectedErrOutputString */,
		true /* shouldFlushOutputDirectory */,
		new String[] { /* classFileNames */
			"bin/X.class"
		});
}
// exploring -d / FileManager interaction
// ecj does not call setLocation on standard Java file managers; it uses 
// handleOption instead; javac does the same
public void test005_dash_d_option_custom_file_manager() {
	StandardJavaFileManager javacJavaFileManager = JAVAC_COMPILER.getStandardFileManager(null, null, null);
	SetLocationDetector customJavaFileManager =
		new SetLocationDetector(
				javacJavaFileManager, 
				StandardLocation.CLASS_OUTPUT);
	runTest(
		true /* shouldCompileOK */,
		new String [] { /* sourceFiles */
			"X.java",
			"public class X {}",
		}, 
		customJavaFileManager /* standardJavaFileManager */,
		Arrays.asList("-d", OUTPUT_DIR) /* options */, 
		new String[] { /* compileFileNames */
			"X.java"
		}, 
		"" /* expectedOutOutputString */, 
		"" /* expectedErrOutputString */,
		true /* shouldFlushOutputDirectory */,
		new String[] { /* classFileNames */
			"X.class"
		});
	assertEquals(OUTPUT_DIR, customJavaFileManager.getLocation(StandardLocation.CLASS_OUTPUT).toString());
	assertFalse(customJavaFileManager.matchFound());
	if (RUN_JAVAC) {
		customJavaFileManager =	new SetLocationDetector(javacJavaFileManager, 
					StandardLocation.CLASS_OUTPUT);
		assertTrue(JAVAC_COMPILER.getTask(null, customJavaFileManager, null, 
				Arrays.asList("-d", OUTPUT_DIR), null, 
				customJavaFileManager.getJavaFileObjectsFromFiles(
						Arrays.asList(new File(OUTPUT_DIR + File.separator + "X.java")))).call());
		assertFalse(customJavaFileManager.matchFound());
	}
}
// exploring -d / FileManager interaction
// ecj calls getLocation on a non-javac standard Java file manager
public void test006_no_dash_d_option_custom_file_manager() throws IOException {
	File binDirectory = new File(OUTPUT_DIR + File.separator + "bin");
	binDirectory.mkdirs();
	GetLocationDetector customJavaFileManager =
		new GetLocationDetector(
				JAVAC_COMPILER.getStandardFileManager(null, null, null),
				StandardLocation.CLASS_OUTPUT);
	customJavaFileManager.setLocation(
			StandardLocation.CLASS_OUTPUT, 
			Arrays.asList(binDirectory));
	runTest(
		true /* shouldCompileOK */,
		new String [] { /* sourceFiles */
			"src/X.java",
			"public class X {}",
		}, 
		customJavaFileManager /* standardJavaFileManager */,
		null /* options */, 
		new String[] { /* compileFileNames */
			"src/X.java"
		}, 
		"" /* expectedOutOutputString */, 
		"" /* expectedErrOutputString */,
		true /* shouldFlushOutputDirectory */,
		new String[] { /* classFileNames */
			"bin/X.class"
		});
	assertTrue(customJavaFileManager.matchFound()); // failure here means that getLocation was not called for the class output location
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=226918
// options consumption - compare with javac and ensure the consumption mechanism
// behaves the same on an option that is supported by both compilers
public void test007_options_consumption() throws IOException {
	List<String> remainingAsList = Arrays.asList("output", "remainder");
	StandardJavaFileManager ecjStandardJavaFileManager = 
		COMPILER.getStandardFileManager(null /* diagnosticListener */, null /* locale */, null /* charset */);
	Iterator<String> remaining = remainingAsList.iterator();
	assertTrue("does not support -d option", ecjStandardJavaFileManager.handleOption("-d", remaining));
	assertEquals("unexpected consumption rate", "remainder", remaining.next());
	if (RUN_JAVAC) {
		StandardJavaFileManager javacStandardJavaFileManager =  
			ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null); // will pick defaults up
		remaining = remainingAsList.iterator();
		assertTrue("does not support -d option", javacStandardJavaFileManager.handleOption("-d", remaining));
		assertEquals("unexpected consumption rate", "remainder", remaining.next());
	}
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=226918
// options consumption - check consumption rate on supported zero-args options
public void test008_options_consumption() throws IOException {
	final String REMAINDER = "remainder";
	List<String> remainingAsList = Arrays.asList("output", REMAINDER);
	StandardJavaFileManager ecjStandardJavaFileManager = 
		COMPILER.getStandardFileManager(null /* diagnosticListener */, null /* locale */, null /* charset */);
	for (String option: CompilerToolTests.ZERO_ARG_OPTIONS) {
		if (ecjStandardJavaFileManager.isSupportedOption(option) != -1) { // some options that the compiler support could well not be supported by the file manager
			Iterator<String> remaining = remainingAsList.iterator();
			assertTrue("does not support " + option + " option", ecjStandardJavaFileManager.handleOption(option, remaining));
			assertEquals("unexpected consumption rate", REMAINDER, remaining.next());
		}
	}
	for (String option: CompilerToolTests.FAKE_ZERO_ARG_OPTIONS) {
		if (ecjStandardJavaFileManager.isSupportedOption(option) != -1) {
			Iterator<String> remaining = remainingAsList.iterator();
			assertTrue("does not support " + option + " option", ecjStandardJavaFileManager.handleOption(option, remaining));
			assertEquals("unexpected consumption rate", REMAINDER, remaining.next());
		}
	}
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=226918
// options consumption - check consumption rate on supported one-arg options
public void test009_options_consumption() throws IOException {
	final String REMAINDER = "remainder";
	List<String> remainingAsList = Arrays.asList("utf-8", REMAINDER);
	StandardJavaFileManager ecjStandardJavaFileManager = 
		COMPILER.getStandardFileManager(null /* diagnosticListener */, null /* locale */, null /* charset */);
	for (String option: CompilerToolTests.ONE_ARG_OPTIONS) {
		if (ecjStandardJavaFileManager.isSupportedOption(option) != -1) { // some options that the compiler support could well not be supported by the file manager
			Iterator<String> remaining = remainingAsList.iterator();
			assertTrue("does not support " + option + " option", ecjStandardJavaFileManager.handleOption(option, remaining));
			assertEquals("unexpected consumption rate", REMAINDER, remaining.next());
		}
	}
}
// tests #10-11 show that ecj throws a RuntimeException when encountering a wrong
// encoding in its parameters, while the default compiler swallows it silently
// based upon the behavior of the command-line javac for the same level, we
// would expect an error to be raised in some fashion here, hence we make the
// tests fail when RUN_JAVAC is on
public void test010_inappropriate_encoding_diagnosis() throws IOException {
	List<String> buggyEncoding = Arrays.asList("dummy");
	boolean passed = true;
	try {
		passed = COMPILER.getStandardFileManager(null /* diagnosticListener */, null /* locale */, null /* charset */).
		handleOption("-encoding", buggyEncoding.iterator());
	} catch (RuntimeException e) {
		passed = false;
	}
	assertFalse("does not catch inappropriate -encoding option", passed);
	if (RUN_JAVAC) {
		// this fails, which may be deemed appropriate or not; but at least
		// test #11 shows that the behavior that can be observed from the 
		// outside is inappropriate
		passed = true;
		try {
			passed = JAVAC_COMPILER.getStandardFileManager(null, null, null).
				handleOption("-encoding", buggyEncoding.iterator());
		} catch (Throwable t) {
			passed = false;
		}
		assertFalse("does not catch inappropriate -encoding option", passed);
	}
}
public void test011_inappropriate_encoding_diagnosis() {
	List<String> options = Arrays.asList("-d", OUTPUT_DIR, "-encoding", "dummy");
	boolean passed = true;
	try {
	runTest(
		false /* shouldCompileOK */,
		new String [] { /* sourceFiles */
			"X.java",
			"public class X {}",
		}, 
		null /* standardJavaFileManager */,
		options /* options */, 
		new String[] { /* compileFileNames */
			"X.java"
		}, 
		"" /* expectedOutOutputString */, 
		"" /* expectedErrOutputString */,
		true /* shouldFlushOutputDirectory */,
		null /* classFileNames */);
	} catch (RuntimeException e) {
		passed = false;
	}
	assertFalse("does not catch inappropriate -encoding option", passed);
	if (RUN_JAVAC) {
		// compared to what the command-line javac does, this is due to be a
		// bug
		passed = true;
		try {
			passed = JAVAC_COMPILER.getTask(null, null, null, options, null, 
					JAVAC_COMPILER.getStandardFileManager(null, null, null).getJavaFileObjectsFromFiles(
						Arrays.asList(new File(OUTPUT_DIR + File.separator + "X.java")))).call();
		} catch (Throwable t) {
			passed = false;
		}
		assertFalse("does not catch inappropriate -encoding option", passed);		
	}
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=188796
// files access must happen through the user-specified file manager
// simplest source read case
public void test012_files_access_read() throws IOException {
	GetJavaFileForInputDetector customJavaFileManager =
		new GetJavaFileForInputDetector(
				JAVAC_COMPILER.getStandardFileManager(null /* diagnosticListener */, null /* locale */, null /* charset */));
	runTest(
		true /* shouldCompileOK */,
		new String [] { /* sourceFiles */
			"X.java",
			"public class X {}",
		}, 
		customJavaFileManager /* standardJavaFileManager */,
		Arrays.asList("-d", OUTPUT_DIR) /* options */, 
		new String[] { /* compileFileNames */
			"X.java"
		}, 
		"" /* expectedOutOutputString */, 
		"" /* expectedErrOutputString */,
		true /* shouldFlushOutputDirectory */,
		new String[] { /* classFileNames */
			"X.class"
		});
	assertTrue(customJavaFileManager.matchFound);
	if (RUN_JAVAC) {
		customJavaFileManager.matchFound = false;
		assertTrue(JAVAC_COMPILER.getTask(null, customJavaFileManager, null, 
				Arrays.asList("-d", OUTPUT_DIR), null, 
				customJavaFileManager.getJavaFileObjectsFromFiles(
						Arrays.asList(new File(OUTPUT_DIR + File.separator + "X.java")))).call());
		assertTrue(customJavaFileManager.matchFound);
	}
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=188796
// files access must happen through the user-specified file manager
// source file accessed through the sourcepath
public void _test013_files_access_read() throws IOException {
	GetJavaFileForInputDetector customJavaFileManager =
		new GetJavaFileForInputDetector(
				JAVAC_COMPILER.getStandardFileManager(null /* diagnosticListener */, null /* locale */, null /* charset */),
				"Y.java", Kind.SOURCE);
	List<String> options = Arrays.asList(
			"-d", OUTPUT_DIR, 
			"-sourcepath", OUTPUT_DIR + File.separator + "src2");
	runTest(
		true /* shouldCompileOK */,
		new String [] { /* sourceFiles */
			"src1/X.java",
			"public class X {\n" +
			"  Y y;\n" +
			"}",
			"src2/Y.java",
			"public class Y {}",
		}, 
		customJavaFileManager /* standardJavaFileManager */,
		options /* options */, 
		new String[] { /* compileFileNames */
			"src1/X.java"
		}, 
		"" /* expectedOutOutputString */, 
		"" /* expectedErrOutputString */,
		true /* shouldFlushOutputDirectory */,
		new String[] { /* classFileNames */
			"X.class"
		});
	assertTrue(customJavaFileManager.matchFound);
	if (RUN_JAVAC) {
		customJavaFileManager.matchFound = false;
		assertTrue(JAVAC_COMPILER.getTask(null, customJavaFileManager, null, 
				options, null, 
				customJavaFileManager.getJavaFileObjectsFromFiles(
						Arrays.asList(new File(OUTPUT_DIR + File.separator + "src1/X.java")))).call());
		assertTrue(customJavaFileManager.matchFound);
	}
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=188796
// files access must happen through the user-specified file manager
// class file accessed for read through the classpath
public void _test014_files_access_read() throws IOException {
	GetJavaFileForInputDetector customJavaFileManager =
		new GetJavaFileForInputDetector(
				JAVAC_COMPILER.getStandardFileManager(null /* diagnosticListener */, null /* locale */, null /* charset */),
				"Y.class", Kind.CLASS);
	List<String> options = Arrays.asList(
			"-d", OUTPUT_DIR,
			"-classpath", OUTPUT_DIR);
	runTest(
			true /* shouldCompileOK */,
			new String [] { /* sourceFiles */
				"src2/Y.java",
				"public class Y {}",
			}, 
			customJavaFileManager /* standardJavaFileManager */,
			options /* options */, 
			new String[] { /* compileFileNames */
				"src2/Y.java"
			}, 
			"" /* expectedOutOutputString */, 
			"" /* expectedErrOutputString */,
			true /* shouldFlushOutputDirectory */,
			new String[] { /* classFileNames */
				"Y.class"
			});
	runTest(
		true /* shouldCompileOK */,
		new String [] { /* sourceFiles */
			"src1/X.java",
			"public class X {\n" +
			"  Y y;\n" +
			"}",
		}, 
		customJavaFileManager /* standardJavaFileManager */,
		options /* options */, 
		new String[] { /* compileFileNames */
			"src1/X.java"
		}, 
		"" /* expectedOutOutputString */, 
		"" /* expectedErrOutputString */,
		false /* shouldFlushOutputDirectory */,
		new String[] { /* classFileNames */
			"X.class"
		});
	assertTrue(customJavaFileManager.matchFound);
	if (RUN_JAVAC) {
		// javac merely throws an exception, which is due to be a bug on their
		// side
		customJavaFileManager.matchFound = false;
		assertTrue(JAVAC_COMPILER.getTask(null, customJavaFileManager, null, 
				options, null, 
				customJavaFileManager.getJavaFileObjectsFromFiles(
						Arrays.asList(new File(OUTPUT_DIR + File.separator + "src1/X.java")))).call());
		assertTrue(customJavaFileManager.matchFound);
	}
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=188796
// files access must happen through the user-specified file manager
// class file accessed for write
public void test015_files_access_write() throws IOException {
	GetJavaFileForOutputDetector customJavaFileManager =
		new GetJavaFileForOutputDetector(
				JAVAC_COMPILER.getStandardFileManager(null /* diagnosticListener */, null /* locale */, null /* charset */),
				"X.class");
	List<String> options = Arrays.asList("-d", OUTPUT_DIR);
	runTest(
		true /* shouldCompileOK */,
		new String [] { /* sourceFiles */
			"src/X.java",
			"public class X {\n" +
			"}",
		}, 
		customJavaFileManager /* standardJavaFileManager */,
		options /* options */, 
		new String[] { /* compileFileNames */
			"src/X.java"
		}, 
		"" /* expectedOutOutputString */, 
		"" /* expectedErrOutputString */,
		true /* shouldFlushOutputDirectory */,
		new String[] { /* classFileNames */
			"X.class"
		});
	assertTrue(customJavaFileManager.matchFound);
	if (RUN_JAVAC) {
		customJavaFileManager.matchFound = false;
		assertTrue(JAVAC_COMPILER.getTask(null, customJavaFileManager, null, 
				options, null, 
				customJavaFileManager.getJavaFileObjectsFromFiles(
						Arrays.asList(new File(OUTPUT_DIR + File.separator + "src/X.java")))).call());
		assertTrue(customJavaFileManager.matchFound);
	}
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=188796
// files access must happen through the user-specified file manager
// class file accessed for write
public void test016_files_access_write() throws IOException {
	GetJavaFileForOutputDetector customJavaFileManager =
		new GetJavaFileForOutputDetector(
				JAVAC_COMPILER.getStandardFileManager(null /* diagnosticListener */, null /* locale */, null /* charset */),
				"Y.class");
	List<String> options = Arrays.asList(
			"-sourcepath", OUTPUT_DIR + File.separator + "src2");
	runTest(
		true /* shouldCompileOK */,
		new String [] { /* sourceFiles */
			"src/X.java",
			"public class X {\n" +
			"  Y y;\n" +
			"}",
			"src2/Y.java",
			"public class Y {\n" +
			"}",
		}, 
		customJavaFileManager /* standardJavaFileManager */,
		options /* options */, 
		new String[] { /* compileFileNames */
			"src/X.java"
		}, 
		"" /* expectedOutOutputString */, 
		"" /* expectedErrOutputString */,
		true /* shouldFlushOutputDirectory */,
		new String[] { /* classFileNames */
			"src/X.class"
		});
	assertTrue(customJavaFileManager.matchFound);
	if (RUN_JAVAC) {
		customJavaFileManager.matchFound = false;
		assertTrue(JAVAC_COMPILER.getTask(null, customJavaFileManager, null, 
				options, null, 
				customJavaFileManager.getJavaFileObjectsFromFiles(
						Arrays.asList(new File(OUTPUT_DIR + File.separator + "src/X.java")))).call());
		assertTrue(customJavaFileManager.matchFound);
	}
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=227583
public void test017_sourcepath_without_destination() throws IOException {
	runTest(
		true /* shouldCompileOK */,
		new String [] { /* sourceFiles */
			"src1/X.java",
			"public class X {\n" +
			"  Y y;\n" +
			"}",
			"src2/Y.java",
			"public class Y {}",
		}, 
		null /* standardJavaFileManager */,
		Arrays.asList(
				"-d", OUTPUT_DIR + "/bin1", /* options */
				"-sourcepath", OUTPUT_DIR + "/src2"), 
		new String[] { /* compileFileNames */
			"src1/X.java"
		}, 
		"" /* expectedOutOutputString */, 
		"" /* expectedErrOutputString */,
		true /* shouldFlushOutputDirectory */,
		new String[] { /* classFileNames */
			"bin1/X.class",
			"bin1/Y.class"
		});
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=227583
// see BatchCompilerTest#68 and following, that show how the option works
// with jsr199-less ecj
public void _test018_sourcepath_with_destination() throws IOException {
	runTest(
		true /* shouldCompileOK */,
		new String [] { /* sourceFiles */
			"src1/X.java",
			"public class X {\n" +
			"  Y y;\n" +
			"}",
			"src2/Y.java",
			"public class Y {}",
		}, 
		null /* standardJavaFileManager */,
		Arrays.asList(
				"-d", OUTPUT_DIR + "/bin1", /* options */
				"-sourcepath", "\"" + OUTPUT_DIR + "/src2\"[-d \"" + OUTPUT_DIR + "/bin2\"]"), 
		new String[] { /* compileFileNames */
			"src1/X.java"
		}, 
		"" /* expectedOutOutputString */, 
		"" /* expectedErrOutputString */,
		true /* shouldFlushOutputDirectory */,
		new String[] { /* classFileNames */
			"bin1/X.class",
			"bin2/Y.class"
		});
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=227583
public void test019_sourcepath_without_destination() throws IOException {
	StandardJavaFileManager ecjStandardJavaFileManager =
		JAVAC_COMPILER.getStandardFileManager(null /* diagnosticListener */, null /* locale */, null /* charset */);
	assertTrue(ecjStandardJavaFileManager.handleOption(
			"-sourcepath", 
			Arrays.asList(OUTPUT_DIR + "/src2").iterator()));
	runTest(
		true /* shouldCompileOK */,
		new String [] { /* sourceFiles */
			"src1/X.java",
			"public class X {\n" +
			"  Y y;\n" +
			"}",
			"src2/Y.java",
			"public class Y {}",
		}, 
		ecjStandardJavaFileManager /* standardJavaFileManager */,
		Arrays.asList("-d", OUTPUT_DIR + "/bin1") /* options */, 
		new String[] { /* compileFileNames */
			"src1/X.java"
		}, 
		"" /* expectedOutOutputString */, 
		"" /* expectedErrOutputString */,
		true /* shouldFlushOutputDirectory */,
		new String[] { /* classFileNames */
			"bin1/X.class",
			"bin1/Y.class"
		});
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=227583
public void _test020_sourcepath_with_destination() throws IOException {
	StandardJavaFileManager ecjStandardJavaFileManager =
		JAVAC_COMPILER.getStandardFileManager(null /* diagnosticListener */, null /* locale */, null /* charset */);
	assertTrue(ecjStandardJavaFileManager.handleOption(
			"-sourcepath", 
			Arrays.asList("\"" + OUTPUT_DIR + "/src2\"[-d \"" + OUTPUT_DIR + "/bin2\"]").iterator()));
	runTest(
		true /* shouldCompileOK */,
		new String [] { /* sourceFiles */
			"src1/X.java",
			"public class X {\n" +
			"  Y y;\n" +
			"}",
			"src2/Y.java",
			"public class Y {}",
		}, 
		ecjStandardJavaFileManager /* standardJavaFileManager */,
		Arrays.asList("-d", OUTPUT_DIR + "/bin1") /* options */, 
		new String[] { /* compileFileNames */
			"src1/X.java"
		}, 
		"" /* expectedOutOutputString */, 
		"" /* expectedErrOutputString */,
		true /* shouldFlushOutputDirectory */,
		new String[] { /* classFileNames */
			"bin1/X.class",
			"bin2/Y.class"
		});
}
// most basic output test
public void test021_output_streams() throws IOException {
	ByteArrayOutputStream 
			outBuffer = new ByteArrayOutputStream(),
			errBuffer = new ByteArrayOutputStream();
	CompilationTask task = COMPILER.getTask(
		new PrintWriter(outBuffer), 
		JAVAC_COMPILER.getStandardFileManager(null /* diagnosticListener */, null /* locale */, null /* charset */), 
		new CompilerInvocationDiagnosticListener(new PrintWriter(errBuffer)), 
		Arrays.asList("-v"), null, null);
	assertTrue(task.call());
	Properties properties = new Properties();
	InputStream resourceAsStream = null;
	try {
		resourceAsStream = Main.class.getResourceAsStream("messages.properties");
		properties.load(resourceAsStream);
	} finally {
		if (resourceAsStream != null) {
			resourceAsStream.close();
		}
	}
	assertTrue(outBuffer.toString().startsWith(properties.getProperty("compiler.name")));
	assertTrue(errBuffer.toString().isEmpty());
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=236814
public void test022_output_streams() throws IOException {
	ByteArrayOutputStream 
	outBuffer = new ByteArrayOutputStream(),
	errBuffer = new ByteArrayOutputStream();
	PrintStream 
	systemOut = System.out,
	systemErr = System.err;
	System.setOut(new PrintStream(outBuffer));
	System.setErr(new PrintStream(errBuffer));
	CompilationTask task = COMPILER.getTask(
			null, 
			JAVAC_COMPILER.getStandardFileManager(null /* diagnosticListener */, null /* locale */, null /* charset */), 
			new CompilerInvocationDiagnosticListener(new PrintWriter(errBuffer)), 
			Arrays.asList("-v"), null, null);
	try {
		assertTrue(task.call());
		assertTrue(outBuffer.toString().isEmpty());
		assertTrue(errBuffer.toString().startsWith("Eclipse Compiler for Java"));
	} finally {
		System.setOut(systemOut);
		System.setErr(systemErr);
	}
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=236817
// according to JavaCompiler#getTask, out should receive supplementary compiler
// output only; errors should be funneled through the diagnostic listener 
public void _test023_output_streams() throws IOException {
	runTest(
		false /* shouldCompileOK */,
		new String [] { /* sourceFiles */
			"X.java",
			"public class Y {}",
		}, 
		null /* standardJavaFileManager */,
		Arrays.asList("-d", OUTPUT_DIR) /* options */, 
		new String[] { /* compileFileNames */
			"X.java"
		}, 
		"" /* expectedOutOutputString */, 
		"----------\n" + /* expectedErrOutputString */
		"1. ERROR in X.java (at line 1)\n" + 
		"	public class Y {}\n" + 
		"	             ^\n" + 
		"The public type Y must be defined in its own file\n" + 
		"----------\n" + 
		"1 problem (1 error)",
		true /* shouldFlushOutputDirectory */,
		null /* classFileNames */);
}
}

Back to the top