Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9daf6b44dfbde54552e66eab08935d65c835b098 (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
/*******************************************************************************
 * Copyright (c) 2000, 2018 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.core.tests.builder;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;

import junit.framework.Test;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.compiler.CategorizedProblem;
import org.eclipse.jdt.core.tests.util.Util;

@SuppressWarnings("rawtypes")
public class PackageInfoTest extends BuilderTests {

public PackageInfoTest(String name) {
	super(name);
}
// Static initializer to specify tests subset using TESTS_* static variables
// All specified tests which does not belong to the class are skipped...
static {
//	TESTS_NAMES = new String[] { "testBug374063" };
//	TESTS_NUMBERS = new int[] { 3 };
//	TESTS_RANGE = new int[] { 21, 50 };
}
public static Test suite() {
	return buildTestSuite(PackageInfoTest.class);
}
public void test001() throws JavaModelException {
	IPath projectPath = env.addProject("Project", "1.5"); //$NON-NLS-1$
	env.addExternalJars(projectPath, Util.getJavaClassLibs());
	fullBuild(projectPath);

	// remove old package fragment root so that names don't collide
	env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$

	IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
	env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$

	env.addClass(root, "pack", "Annot", //$NON-NLS-1$ //$NON-NLS-2$
		"package pack;\n" + //$NON-NLS-1$
		"public @interface Annot {}" //$NON-NLS-1$
	);

	incrementalBuild(projectPath);

	IPath packageInfoPath = env.addFile(root, "pack/package-info.java", //$NON-NLS-1$ //$NON-NLS-2$
		"@Annot package p1" //$NON-NLS-1$
	);

	incrementalBuild(projectPath);
	expectingOnlyProblemsFor(packageInfoPath);
	final Problem[] problems = env.getProblems();
	Arrays.sort(problems);
	assertNotNull(problems);
	final StringWriter stringWriter = new StringWriter();
	final PrintWriter writer = new PrintWriter(stringWriter);
	final int problemsLength = problems.length;
	if (problemsLength == 1) {
		writer.print(problems[0].getMessage());
	} else {
		for (int i = 0; i < problemsLength - 1; i++)
			writer.println(problems[i].getMessage());
		writer.print(problems[problemsLength - 1].getMessage());
	}
	writer.close();
	final String expectedOutput =
		"Syntax error on token \"p1\", ; expected after this token\n" +
		"The declared package \"p1\" does not match the expected package \"pack\"";
	assertSourceEquals("Different messages", expectedOutput, stringWriter.toString());
}
public void test002() throws JavaModelException {
	IPath projectPath = env.addProject("Project", "1.5"); //$NON-NLS-1$
	env.addExternalJars(projectPath, Util.getJavaClassLibs());
	fullBuild(projectPath);

	// remove old package fragment root so that names don't collide
	env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$

	IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
	env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$

	env.addClass(root, "testcase", "Main", //$NON-NLS-1$ //$NON-NLS-2$
		"package testcase;\n" +
		"\n" +
		"public class Main {\n" +
		"    public static void main(String[] argv) throws Exception {\n" +
		"		Package pkg = Package.getPackage(\"testcase\");\n" +
		"		System.out.print(pkg.getAnnotation(TestAnnotation.class));\n" +
		"		pkg = Class.forName(\"testcase.package-info\").getPackage();\n" +
		"		System.out.print(pkg.getAnnotation(TestAnnotation.class));\n" +
		"    }\n" +
		"}"
	);

	env.addClass(root, "testcase", "TestAnnotation", //$NON-NLS-1$ //$NON-NLS-2$
		"package testcase;\n" +
		"\n" +
		"import static java.lang.annotation.ElementType.PACKAGE;\n" +
		"import static java.lang.annotation.RetentionPolicy.RUNTIME;\n" +
		"\n" +
		"import java.lang.annotation.Retention;\n" +
		"import java.lang.annotation.Target;\n" +
		"\n" +
		"@Target(PACKAGE)\n" +
		"@Retention(RUNTIME)\n" +
		"public @interface TestAnnotation {\n" +
		"}"
	);

	env.addFile(root, "testcase/package-info.java", //$NON-NLS-1$ //$NON-NLS-2$
		"@TestAnnotation package testcase;" //$NON-NLS-1$
	);
	incrementalBuild(projectPath);
	expectingNoProblems();
	executeClass(projectPath, "testcase.Main", "@testcase.TestAnnotation()@testcase.TestAnnotation()", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
public void test003() throws JavaModelException {
	IPath projectPath = env.addProject("Project", "1.5"); //$NON-NLS-1$
	env.addExternalJars(projectPath, Util.getJavaClassLibs());
	fullBuild(projectPath);

	// remove old package fragment root so that names don't collide
	env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$

	IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
	env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$

	env.addPackage(root, "testcase");
	IPath packageInfoPath = env.addFile(root, "testcase/package-info.java", //$NON-NLS-1$ //$NON-NLS-2$
		"" //$NON-NLS-1$
	);

	incrementalBuild(projectPath);
	expectingOnlySpecificProblemFor(
		packageInfoPath,
		new Problem("testcase/package-info.java", "The declared package \"\" does not match the expected package \"testcase\"", packageInfoPath, 0, 0, CategorizedProblem.CAT_INTERNAL, IMarker.SEVERITY_ERROR)); //$NON-NLS-1$ //$NON-NLS-2$
}
// test for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=252555 : NPE
// on duplicate package-info
public void test004() throws JavaModelException {
	IPath projectPath = env.addProject("Project", "1.5"); //$NON-NLS-1$
	env.addExternalJars(projectPath, Util.getJavaClassLibs());
	fullBuild(projectPath);

	// remove old package fragment root so that names don't collide
	env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$

	IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
	env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$

	IPath otherRoot = env.addPackageFragmentRoot(projectPath, "test"); //$NON-NLS-1$
	env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$

	env.addPackage(root, "my.foo");
	env.addPackage(otherRoot, "my.foo");

	env.addFile(root, "my/foo/package-info.java", //$NON-NLS-1$ //$NON-NLS-2$
		"/**\n" +
		"* A demo package for foo.\n" +
		"*/\n" +
		"package my.foo;\n"
	);

	IPath otherPackageInfoPath = env.addFile(otherRoot,
		"my/foo/package-info.java", //$NON-NLS-1$ //$NON-NLS-2$
		"/**\n" +
		"* A demo package for foo.\n" +
		"*/\n" +
		"package my.foo;\n"
	);

	incrementalBuild(projectPath);
	expectingOnlySpecificProblemFor(
		otherPackageInfoPath,
		new Problem("my/foo/package-info.java", "The type package-info is already defined", otherPackageInfoPath, 0, 0, CategorizedProblem.CAT_TYPE, IMarker.SEVERITY_ERROR)); //$NON-NLS-1$ //$NON-NLS-2$
}
// test for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=258145 : JME
// on duplicate package-info
public void test258145() throws JavaModelException {
	IPath projectPath = env.addProject("Project", "1.5"); //$NON-NLS-1$
	env.addExternalJars(projectPath, Util.getJavaClassLibs());
	fullBuild(projectPath);

	// remove old package fragment root so that names don't collide
	env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$

	IPath root = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
	env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$

	IPath otherRoot = env.addPackageFragmentRoot(projectPath, "test"); //$NON-NLS-1$
	env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$

	env.addPackage(root, "my.foo");
	env.addFile(root, "my/foo/package-info.java", //$NON-NLS-1$ //$NON-NLS-2$
		"/**\n" +
		"* A demo package for foo.\n" +
		"*/\n" +
		"package my.foo;\n"
	);

	fullBuild(projectPath);

	env.addPackage(otherRoot, "my.foo");

	IPath otherPackageInfoPath = env.addFile(otherRoot,
		"my/foo/package-info.java", //$NON-NLS-1$ //$NON-NLS-2$
		"/**\n" +
		"* A demo package for foo.\n" +
		"*/\n" +
		"package my.foo;\n"
	);

	incrementalBuild(projectPath);
	expectingOnlySpecificProblemFor(
		otherPackageInfoPath,
		new Problem("my/foo/package-info.java", "The type package-info is already defined", otherPackageInfoPath, 0, 0, CategorizedProblem.CAT_TYPE, IMarker.SEVERITY_ERROR)); //$NON-NLS-1$ //$NON-NLS-2$
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=323785 
// (NPE upon creation/deletion of package-info.java in default package)
public void test323785 () throws JavaModelException {
	
	IPath projectPath = env.addProject("Project", "1.5"); 
	env.addExternalJars(projectPath, Util.getJavaClassLibs());
	fullBuild(projectPath);

	// remove old package fragment root so that names don't collide
	env.removePackageFragmentRoot(projectPath, ""); 

	IPath root = env.addPackageFragmentRoot(projectPath, "src");
	env.setOutputFolder(projectPath, "bin"); 
	
	fullBuild(projectPath);

	env.addFile(root, "package-info.java",	"");

	incrementalBuild(projectPath);
	expectingNoProblems();
	
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=323785
// verify that changes to package info containing secondary types do trigger incremental build.
public void test323785a () throws JavaModelException {
	
	IPath projectPath = env.addProject("Project", "1.5"); 
	env.addExternalJars(projectPath, Util.getJavaClassLibs());
	fullBuild(projectPath);

	// remove old package fragment root so that names don't collide
	env.removePackageFragmentRoot(projectPath, ""); 

	IPath root = env.addPackageFragmentRoot(projectPath, "src");
	env.setOutputFolder(projectPath, "bin"); 
	
	IPath xJavaPath = env.addFile(root, "X.java",	"class X extends Y {}\n");
	fullBuild(projectPath);
	env.addFile(root, "package-info.java",	"class Y {}\n");
	incrementalBuild(projectPath);
	expectingNoProblems();
	env.addFile(root, "package-info.java",	"final class Y {}\n");
	incrementalBuild(projectPath);
	expectingOnlySpecificProblemFor(
			xJavaPath,
			new Problem("X.java", "The type X cannot subclass the final class Y", xJavaPath, 16, 17, CategorizedProblem.CAT_TYPE, IMarker.SEVERITY_ERROR));
	
}

// https://bugs.eclipse.org/bugs/show_bug.cgi?id=372012
// test missing default nullness annotation for a package without package-info
// test when the package-info is added with the default annotation, the problem disappears
public void testBug372012() throws JavaModelException, IOException {
	
	IPath projectPath = env.addProject("Project", "1.5"); 
	env.addExternalJars(projectPath, Util.getJavaClassLibs());
	fullBuild(projectPath);

	// remove old package fragment root so that names don't collide
	env.removePackageFragmentRoot(projectPath, ""); 

	IPath srcRoot = env.addPackageFragmentRoot(projectPath, "src");
	env.setOutputFolder(projectPath, "bin"); 
	// prepare the project:
	setupProjectForNullAnnotations(projectPath);
	env.getJavaProject(projectPath).setOption(JavaCore.COMPILER_PB_MISSING_NONNULL_BY_DEFAULT_ANNOTATION, JavaCore.ERROR);
	env.getJavaProject(projectPath).setOption(JavaCore.COMPILER_PB_REDUNDANT_NULL_ANNOTATION, JavaCore.ERROR);
	String test1Code = "package p1;\n"	+
		"public class Test1 {\n" +
		"    public void foo() {\n" +
		"        new Test2().bar(\"\");\n" +
		"    }\n" +
		"	 class Test1Inner{}\n" +
		"}";
	String test2Code = "package p1;\n" +
		"@org.eclipse.jdt.annotation.NonNullByDefault\n" +
		"public class Test2 {\n" +
		"    public void bar(String str) {}\n" +
		"}";
	String test3Code = "package p1;\n" +
			"public class Test3 {\n" +
			"    public void bar(String str) {}\n" +
			"}";

	IPath test1Path = env.addClass(srcRoot, "p1", "Test1", test1Code);
	env.addClass(srcRoot, "p1", "Test2", test2Code);
	env.addClass(srcRoot, "p1", "Test3", test3Code);
	
	fullBuild(projectPath);
	expectingNoProblemsFor(test1Path);
	// should have only one marker
	expectingProblemsFor(srcRoot, 
			"Problem : A default nullness annotation has not been specified for the package p1 [ resource : </Project/src/p1> range : <8,10> category : <90> severity : <2>]");

	// add package-info.java with default annotation
	String packageInfoCode = "@org.eclipse.jdt.annotation.NonNullByDefault\n" +
		"package p1;\n";
	env.addClass(srcRoot, "p1", "package-info", packageInfoCode);
	incrementalBuild(projectPath);
	expectingProblemsFor(projectPath,
			"Problem : Nullness default is redundant with a default specified for the enclosing package p1 [ resource : </Project/src/p1/Test2.java> range : <12,56> category : <120> severity : <2>]");

	// verify that all package CU's were recompiled
	expectingUniqueCompiledClasses(new String[] { "p1.Test1", "p1.Test1$Test1Inner", "p1.Test2", "p1.Test3", "p1.package-info" });
}

// https://bugs.eclipse.org/bugs/show_bug.cgi?id=372012
// test missing default nullness annotation for a package without package-info
// test when the the default annotations are added to all top level types, the problem stays
public void testBug372012a() throws JavaModelException, IOException {
	
	IPath projectPath = env.addProject("Project", "1.5"); 
	env.addExternalJars(projectPath, Util.getJavaClassLibs());
	fullBuild(projectPath);

	// remove old package fragment root so that names don't collide
	env.removePackageFragmentRoot(projectPath, ""); 

	IPath srcRoot = env.addPackageFragmentRoot(projectPath, "src");
	env.setOutputFolder(projectPath, "bin"); 
	// prepare the project:
	setupProjectForNullAnnotations(projectPath);
	env.getJavaProject(projectPath).setOption(JavaCore.COMPILER_PB_MISSING_NONNULL_BY_DEFAULT_ANNOTATION, JavaCore.ERROR);
	env.getJavaProject(projectPath).setOption(JavaCore.COMPILER_PB_REDUNDANT_NULL_ANNOTATION, JavaCore.ERROR);
	String test1Code = "package p1;\n"	+
		"public class Test1 {\n" +
		"    public void foo() {\n" +
		"        new Test2().bar(\"\");\n" +
		"    }\n" +
		"	 class Test1Inner{}\n" +
		"}";
	String test2Code = "package p1;\n" +
		"@org.eclipse.jdt.annotation.NonNullByDefault\n" +
		"public class Test2 {\n" +
		"    public void bar(String str) {}\n" +
		"}";

	IPath test1Path = env.addClass(srcRoot, "p1", "Test1", test1Code);
	env.addClass(srcRoot, "p1", "Test2", test2Code);
	
	fullBuild(projectPath);
	expectingNoProblemsFor(test1Path);
	// should have only one marker
	expectingProblemsFor(srcRoot, 
			"Problem : A default nullness annotation has not been specified for the package p1 [ resource : </Project/src/p1> range : <8,10> category : <90> severity : <2>]");

	// add default annotation to Test1
	test1Code = "package p1;\n"	+
			"@org.eclipse.jdt.annotation.NonNullByDefault\n" +
			"public class Test1 {\n" +
			"    public void foo() {\n" +
			"        new Test2().bar(\"\");\n" +
			"    }\n" +
			"	 class Test1Inner{}\n" +
			"}";
	env.addClass(srcRoot, "p1", "Test1", test1Code);
	incrementalBuild(projectPath);
	// should have only one marker
	expectingProblemsFor(srcRoot, 
			"Problem : A default nullness annotation has not been specified for the package p1 [ resource : </Project/src/p1> range : <8,10> category : <90> severity : <2>]");

	// verify that all package CU's were recompiled
	expectingUniqueCompiledClasses(new String[] { "p1.Test1", "p1.Test1$Test1Inner"});
}

// https://bugs.eclipse.org/bugs/show_bug.cgi?id=372012
// test missing default nullness annotation for a package without package-info
// test when the the default annotations is added to only 1 top level type, the problem stays
public void testBug372012b() throws JavaModelException, IOException {
	
	IPath projectPath = env.addProject("Project", "1.5"); 
	env.addExternalJars(projectPath, Util.getJavaClassLibs());
	fullBuild(projectPath);

	// remove old package fragment root so that names don't collide
	env.removePackageFragmentRoot(projectPath, ""); 

	IPath srcRoot = env.addPackageFragmentRoot(projectPath, "src");
	env.setOutputFolder(projectPath, "bin"); 
	// prepare the project:
	setupProjectForNullAnnotations(projectPath);
	env.getJavaProject(projectPath).setOption(JavaCore.COMPILER_PB_MISSING_NONNULL_BY_DEFAULT_ANNOTATION, JavaCore.ERROR);
	env.getJavaProject(projectPath).setOption(JavaCore.COMPILER_PB_REDUNDANT_NULL_ANNOTATION, JavaCore.ERROR);
	String test1Code = "package p1;\n"	+
		"public class Test1 {\n" +
		"    public void foo() {\n" +
		"        new Test2().bar(\"\");\n" +
		"    }\n" +
		"	 class Test1Inner{}\n" +
		"}";
	String test2Code = "package p1;\n" +
		"public class Test2 {\n" +
		"    public void bar(String str) {}\n" +
		"}";

	IPath test1Path = env.addClass(srcRoot, "p1", "Test1", test1Code);
	env.addClass(srcRoot, "p1", "Test2", test2Code);
	
	fullBuild(projectPath);
	expectingNoProblemsFor(test1Path);
	// should have only one marker
	expectingProblemsFor(srcRoot, 
			"Problem : A default nullness annotation has not been specified for the package p1 [ resource : </Project/src/p1> range : <8,10> category : <90> severity : <2>]");

	// add default annotation to Test1
	test1Code = "package p1;\n"	+
			"@org.eclipse.jdt.annotation.NonNullByDefault\n" +
			"public class Test1 {\n" +
			"    public void foo() {\n" +
			"        new Test2().bar(\"\");\n" +
			"    }\n" +
			"	 class Test1Inner{}\n" +
			"}";
	env.addClass(srcRoot, "p1", "Test1", test1Code);
	incrementalBuild(projectPath);
	// should have only one marker
	expectingProblemsFor(srcRoot, 
			"Problem : A default nullness annotation has not been specified for the package p1 [ resource : </Project/src/p1> range : <8,10> category : <90> severity : <2>]");

	// verify that only Test1's CU's were recompiled
	expectingUniqueCompiledClasses(new String[] { "p1.Test1", "p1.Test1$Test1Inner"});
}

// https://bugs.eclipse.org/bugs/show_bug.cgi?id=372012
// test missing default nullness annotation for a package with package-info
// test when the the default annotation is removed from package-info, the problem comes back
public void testBug372012c() throws JavaModelException, IOException {
	
	IPath projectPath = env.addProject("Project", "1.5"); 
	env.addExternalJars(projectPath, Util.getJavaClassLibs());
	fullBuild(projectPath);

	// remove old package fragment root so that names don't collide
	env.removePackageFragmentRoot(projectPath, ""); 

	IPath srcRoot = env.addPackageFragmentRoot(projectPath, "src");
	env.setOutputFolder(projectPath, "bin"); 
	// prepare the project:
	setupProjectForNullAnnotations(projectPath);
	env.getJavaProject(projectPath).setOption(JavaCore.COMPILER_PB_MISSING_NONNULL_BY_DEFAULT_ANNOTATION, JavaCore.ERROR);
	env.getJavaProject(projectPath).setOption(JavaCore.COMPILER_PB_REDUNDANT_NULL_ANNOTATION, JavaCore.ERROR);
	String test1Code = "package p1;\n"	+
		"public class Test1 {\n" +
		"    public void foo() {\n" +
		"        new Test2().bar(\"\");\n" +
		"    }\n" +
		"	 class Test1Inner{}\n" +
		"}";
	String test2Code = "package p1;\n" +
		"public class Test2 {\n" +
		"    public void bar(String str) {}\n" +
		"}";
	// add package-info.java with default annotation
	String packageInfoCode = "@org.eclipse.jdt.annotation.NonNullByDefault\n" +
		"package p1;\n";
	env.addClass(srcRoot, "p1", "package-info", packageInfoCode);

	env.addClass(srcRoot, "p1", "Test1", test1Code);
	env.addClass(srcRoot, "p1", "Test2", test2Code);
	env.addClass(srcRoot, "p1", "package-info", packageInfoCode);
	
	fullBuild(projectPath);
	// default annotation present, so no problem
	expectingNoProblemsFor(srcRoot);

	// add package-info.java with default annotation
	packageInfoCode =
		"package p1;\n";
	env.addClass(srcRoot, "p1", "package-info", packageInfoCode);
	incrementalBuild(projectPath);
	expectingProblemsFor(projectPath,
			"Problem : A default nullness annotation has not been specified for the package p1 [ resource : </Project/src/p1/package-info.java> range : <8,10> category : <90> severity : <2>]");

	// verify that all package CU's were recompiled
	expectingUniqueCompiledClasses(new String[] { "p1.Test1", "p1.Test1$Test1Inner", "p1.Test2", "p1.package-info" });
}

/**
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=367836
 */
public void testBug367836() throws JavaModelException {
	IPath projectPath = env.addProject("Project"); //$NON-NLS-1$
	env.addExternalJars(projectPath, Util.getJavaClassLibs());
	env.removePackageFragmentRoot(projectPath, ""); //$NON-NLS-1$
	IPath src = env.addPackageFragmentRoot(projectPath, "src"); //$NON-NLS-1$
	env.setOutputFolder(projectPath, "bin"); //$NON-NLS-1$

	env.addClass(src, "p", "A", //$NON-NLS-1$ //$NON-NLS-2$
		"package p;\n"+ //$NON-NLS-1$
		"public class A {}" //$NON-NLS-1$
	);

	IPath bPath = env.addClass(src, "p", "package-info", //$NON-NLS-1$ //$NON-NLS-2$
		"" //$NON-NLS-1$
	);

	fullBuild();
	expectingOnlySpecificProblemFor(bPath,
		new Problem("", "The declared package \"\" does not match the expected package \"p\"", bPath, 0, 0, CategorizedProblem.CAT_INTERNAL, IMarker.SEVERITY_ERROR)); //$NON-NLS-1$ //$NON-NLS-2$
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=374063
// verify that markers are created on the correct resource
public void testBug374063() throws JavaModelException, IOException {
	
	IPath projectPath = env.addProject("Project", "1.5"); 
	env.addExternalJars(projectPath, Util.getJavaClassLibs());
	fullBuild(projectPath);

	// remove old package fragment root so that names don't collide
	env.removePackageFragmentRoot(projectPath, ""); 

	IPath srcRoot = env.addPackageFragmentRoot(projectPath, "src");
	env.setOutputFolder(projectPath, "bin"); 
	// prepare the project:
	setupProjectForNullAnnotations(projectPath);
	env.getJavaProject(projectPath).setOption(JavaCore.COMPILER_PB_MISSING_NONNULL_BY_DEFAULT_ANNOTATION, JavaCore.ERROR);
	env.getJavaProject(projectPath).setOption(JavaCore.COMPILER_PB_REDUNDANT_NULL_ANNOTATION, JavaCore.ERROR);
	String test1Code = "package p1;\n"	+
		"public class Test1 {\n" +
		"    public String output(List<Integer> integers) {\n" +
		"        return \"\";\n" +
		"    }\n" +
		"	 public void output(List<Double> doubles) {}\n" +
		"}";

	env.addClass(srcRoot, "p1", "Test1", test1Code);
	
	fullBuild(projectPath);
	// resource for compile errors should be Test1 and not p1
	expectingProblemsFor(projectPath, 
			"Problem : A default nullness annotation has not been specified for the package p1 [ resource : </Project/src/p1> range : <8,10> category : <90> severity : <2>]\n" + 
			"Problem : List cannot be resolved to a type [ resource : </Project/src/p1/Test1.java> range : <130,134> category : <40> severity : <2>]\n" + 
			"Problem : List cannot be resolved to a type [ resource : </Project/src/p1/Test1.java> range : <58,62> category : <40> severity : <2>]");

	// add package-info.java with default annotation
	String packageInfoCode = "@org.eclipse.jdt.annotation.NonNullByDefault\n" +
		"package p1;\n";
	env.addClass(srcRoot, "p1", "package-info", packageInfoCode);
	incrementalBuild(projectPath);
	expectingProblemsFor(projectPath,
			"Problem : List cannot be resolved to a type [ resource : </Project/src/p1/Test1.java> range : <130,134> category : <40> severity : <2>]\n" + 
			"Problem : List cannot be resolved to a type [ resource : </Project/src/p1/Test1.java> range : <58,62> category : <40> severity : <2>]");

	// verify that all package CU's were recompiled
	expectingUniqueCompiledClasses(new String[] { "p1.Test1", "p1.package-info" });
}
// 382960
public void testBug382960() throws JavaModelException, IOException, CoreException {
	IPath projectPath = env.addProject("Project", "1.5");
	env.addExternalJars(projectPath, Util.getJavaClassLibs());
	fullBuild(projectPath);

	env.removePackageFragmentRoot(projectPath, "");
	IPath srcRoot = env.addPackageFragmentRoot(projectPath, "src");
	env.setOutputFolder(projectPath, "bin");
	// prepare the project:
	setupProjectForNullAnnotations(projectPath);
	env.getJavaProject(projectPath).setOption(JavaCore.COMPILER_PB_MISSING_NONNULL_BY_DEFAULT_ANNOTATION, JavaCore.ERROR);
	String test1Code = "package p1;\n" +
						"public class Test1 {\n" +
						"    public String output(List<Integer> integers) {\n" +
						"        return \"\";\n" +
						"    }\n" +
						"	 public void output(List<Double> doubles) {}\n" +
						"}";

	env.addClass(srcRoot, "p1", "Test1", test1Code);
	String packageInfoCode = "package p1;\n";
	env.addClass(srcRoot, "p1", "package-info", packageInfoCode);
	fullBuild(projectPath);
	expectingProblemsFor(projectPath,
			"Problem : A default nullness annotation has not been specified for the package p1 [ resource : </Project/src/p1/package-info.java> range : <8,10> category : <90> severity : <2>]\n" + 
			"Problem : List cannot be resolved to a type [ resource : </Project/src/p1/Test1.java> range : <130,134> category : <40> severity : <2>]\n" + 
			"Problem : List cannot be resolved to a type [ resource : </Project/src/p1/Test1.java> range : <58,62> category : <40> severity : <2>]");

	packageInfoCode = "@org.eclipse.jdt.annotation.NonNullByDefault\n" +
					   "package p1;\n";
	env.addClass(srcRoot, "p1", "package-info", packageInfoCode);
	incrementalBuild(projectPath);
	expectingProblemsFor(projectPath,
			"Problem : List cannot be resolved to a type [ resource : </Project/src/p1/Test1.java> range : <130,134> category : <40> severity : <2>]\n" + 
			"Problem : List cannot be resolved to a type [ resource : </Project/src/p1/Test1.java> range : <58,62> category : <40> severity : <2>]");
	expectingUniqueCompiledClasses(new String[] { "p1.Test1", "p1.package-info" });

	IProject project = env.getProject(projectPath);
	IFile packageInfo = project.getFile("/src/p1/package-info.java");
	packageInfo.touch(null);

	incrementalBuild(projectPath);
	expectingProblemsFor(projectPath,
			"Problem : List cannot be resolved to a type [ resource : </Project/src/p1/Test1.java> range : <130,134> category : <40> severity : <2>]\n" + 
			"Problem : List cannot be resolved to a type [ resource : </Project/src/p1/Test1.java> range : <58,62> category : <40> severity : <2>]");

	// verify that only package-info was recompiled
	expectingUniqueCompiledClasses(new String[] { "p1.package-info" });
}
// test that when a package-info.java has been created, markers on the 
// package fragments in all source folders are removed.
public void testBug525469() throws JavaModelException, IOException {
	
	IPath projectPath = env.addProject("Project", "1.5"); 
	env.addExternalJars(projectPath, Util.getJavaClassLibs());
	fullBuild(projectPath);

	// remove old package fragment root so that names don't collide
	env.removePackageFragmentRoot(projectPath, ""); 

	IPath srcRoot1 = env.addPackageFragmentRoot(projectPath, "src1");
	IPath srcRoot2 = env.addPackageFragmentRoot(projectPath, "src2");
	env.setOutputFolder(projectPath, "bin"); 
	// prepare the project:
	setupProjectForNullAnnotations(projectPath);
	env.getJavaProject(projectPath).setOption(JavaCore.COMPILER_PB_MISSING_NONNULL_BY_DEFAULT_ANNOTATION, JavaCore.ERROR);
	env.getJavaProject(projectPath).setOption(JavaCore.COMPILER_PB_REDUNDANT_NULL_ANNOTATION, JavaCore.ERROR);

	String test1Code = "package p1;\n"	+
		"public class Test1 {\n" +
		"}";
	env.addClass(srcRoot1, "p1", "Test1", test1Code);

	String otherClassCode = "package p2;\n"	+
		"public class OtherClass {\n" +
		"}";
	env.addClass(srcRoot1, "p2", "OtherClass", otherClassCode);
	
	String packageInfoCode2 = "@org.eclipse.jdt.annotation.NonNullByDefault\n" +
		"package p2;\n";
	env.addClass(srcRoot1, "p2", "package-info", packageInfoCode2);

	fullBuild(projectPath);
	
	String test2Code = "package p1;\n"	+
		"public class Test2 {\n" +
		"}";

	env.addClass(srcRoot2, "p1", "Test2", test2Code);
	incrementalBuild(projectPath);
	
	// after the incremental build, as there is no package-info.java for p1, the error is visible in both source directories on the directory for the package p1
	expectingProblemsFor(projectPath, 
			"Problem : A default nullness annotation has not been specified for the package p1 [ resource : </Project/src1/p1> range : <8,10> category : <90> severity : <2>]\n" + 
			"Problem : A default nullness annotation has not been specified for the package p1 [ resource : </Project/src2/p1> range : <8,10> category : <90> severity : <2>]"); 

	// add package-info.java with default annotation
	String packageInfoCode1 = "@org.eclipse.jdt.annotation.NonNullByDefault\n" +
		"package p1;\n";
	env.addClass(srcRoot1, "p1", "package-info", packageInfoCode1);
	
	// an incremental build is requested, but it will switch to a full build
	incrementalBuild(projectPath);
	
	// verify the expected behaviour: the error marker in the src2 directory must be gone, too
	expectingProblemsFor(projectPath,
			"");

	// verify the implementation by doing a full build: all files have been recompiled
	expectingUniqueCompiledClasses(new String[] { "p1.Test1", "p1.Test2", "p1.package-info", "p2.OtherClass", "p2.package-info" });
}

void setupProjectForNullAnnotations(IPath projectPath) throws IOException, JavaModelException {
	// add the org.eclipse.jdt.annotation library (bin/ folder or jar) to the project:
	File bundleFile = FileLocator.getBundleFile(Platform.getBundle("org.eclipse.jdt.annotation"));
	String annotationsLib = bundleFile.isDirectory() ? bundleFile.getPath()+"/bin" : bundleFile.getPath();
	IJavaProject javaProject = env.getJavaProject(projectPath);
	IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
	int len = rawClasspath.length;
	System.arraycopy(rawClasspath, 0, rawClasspath = new IClasspathEntry[len+1], 0, len);
	rawClasspath[len] = JavaCore.newLibraryEntry(new Path(annotationsLib), null, null);
	javaProject.setRawClasspath(rawClasspath, null);

	javaProject.setOption(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, JavaCore.ENABLED);
}

protected void assertSourceEquals(String message, String expected, String actual) {
	if (actual == null) {
		assertEquals(message, expected, null);
		return;
	}
	actual = org.eclipse.jdt.core.tests.util.Util.convertToIndependantLineDelimiter(actual);
	if (!actual.equals(expected)) {
		System.out.print(org.eclipse.jdt.core.tests.util.Util.displayString(actual.toString(), 0));
	}
	assertEquals(message, expected, actual);
}
public static Class testClass() {
	return PackageInfoTest.class;
}
}

Back to the top