Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6b968ad865fd9de252a0a7cdc570da9129d4b518 (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
/*******************************************************************************
 * Copyright (c) 2005, 2014 BEA Systems, Inc.
 * 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:
 *   wharley@bea.com - initial API and implementation
 *   het@google.com - Bug 423254 - There is no way to tell if a project's factory path is different from the workspace default
 *******************************************************************************/

package org.eclipse.jdt.apt.tests;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.apt.core.util.AptConfig;
import org.eclipse.jdt.apt.core.util.IFactoryPath;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.tests.util.Util;

/**
 * 
 */
public class RegressionTests extends APTTestBase {

	public RegressionTests(String name) {
		super(name);
	}

	public static Test suite()
	{
		return new TestSuite( RegressionTests.class );
	}

	public void setUp() throws Exception
	{
		super.setUp();
	}
	
	/**
	 * Bugzilla 104032: NPE when deleting project that has APT settings.
	 */
	public void testBugzilla104032() throws Exception
	{
		// set up project with unique name
		final String projName = RegressionTests.class.getName() + "104032.Project"; //$NON-NLS-1$
		IPath projectPath = env.addProject( projName, "1.5" ); //$NON-NLS-1$
		env.addExternalJars( projectPath, Util.getJavaClassLibs() );

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

		TestUtil.createAndAddAnnotationJar( env
			.getJavaProject( projectPath ) );
		IProject project = env.getProject( projName );
		IFolder srcFolder = project.getFolder( "src" );
		IPath srcRoot = srcFolder.getFullPath();

		String a1Code = "package p1; " + "\n"
			+ "import org.eclipse.jdt.apt.tests.annotations.helloworld.HelloWorldAnnotation;" + "\n"
			+ "@HelloWorldAnnotation" + "\n"
			+ "public class A1 {}";
		String a2Code = "package p1; " + "\n"
			+ "import org.eclipse.jdt.apt.tests.annotations.helloworld.HelloWorldAnnotation;" + "\n"
			+ "@HelloWorldAnnotation" + "\n"
			+ "public class A2 {}";
		String bCode = "package p1; " + "\n"
			+ "public class B { generatedfilepackage.GeneratedFileTest gft; }";
		env.addClass( srcRoot, "p1", "A1", a1Code ); //$NON-NLS-1$ //$NON-NLS-2$
		env.addClass( srcRoot, "p1", "A2", a2Code ); //$NON-NLS-1$ //$NON-NLS-2$
		env.addClass( srcRoot, "p1", "B", bCode ); //$NON-NLS-1$ //$NON-NLS-2$
		
		// Set some per-project preferences
		IJavaProject jproj = env.getJavaProject( projName );
		AptConfig.addProcessorOption(jproj, "test.104032.a", "foo");
		AptConfig.setEnabled(jproj, true);
		
		fullBuild( project.getFullPath() );
		expectingNoProblems();
		
		// Now delete the project!
		ResourcesPlugin.getWorkspace().delete(new IResource[] { project }, true, null);

	}
    
	/**
	 * Tests annotation proxies
	 */
    public void testBugzilla106541() throws Exception
    {
        final String projName = RegressionTests.class.getName() + "104032.Project"; //$NON-NLS-1$
        IPath projectPath = env.addProject( projName, "1.5" ); //$NON-NLS-1$
        env.addExternalJars( projectPath, Util.getJavaClassLibs() );

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

        IJavaProject javaProject = env.getJavaProject( projectPath ) ;
		AptConfig.setEnabled(javaProject, true);
        TestUtil.createAndAddAnnotationJar(javaProject);
        IProject project = env.getProject( projName );
        IFolder srcFolder = project.getFolder( "src" );
        IPath srcRoot = srcFolder.getFullPath();

        String code = "package p1; " + "\n"
        + "import org.eclipse.jdt.apt.tests.annotations.readAnnotationType.SimpleAnnotation;" + "\n"
        + "@SimpleAnnotation(SimpleAnnotation.Name.HELLO)" + "\n"
        + "public class MyClass { \n"
        + " public test.HELLOGen _gen;"
        + " }";
        
        env.addClass( srcRoot, "p1", "MyClass", code );
        
        fullBuild( project.getFullPath() );
        expectingNoProblems();
        
        Util.delete(project);
    }
    
    // doesn't work because of a jdt.core type system universe problem.
    public void testBugzilla120255() throws Exception{
    	final String projName = RegressionTests.class.getName() + "120255.Project"; //$NON-NLS-1$
		IPath projectPath = env.addProject( projName, "1.5" ); //$NON-NLS-1$
		env.addExternalJars( projectPath, Util.getJavaClassLibs() );

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

		TestUtil.createAndAddAnnotationJar( env
			.getJavaProject( projectPath ) );
		IProject project = env.getProject( projName );
		IFolder srcFolder = project.getFolder( "src" );
		IPath srcRoot = srcFolder.getFullPath();

		String a1Code = "package pkg; " + "\n"
			+ "import org.eclipse.jdt.apt.tests.annotations.apitest.Common;\n" 
			+ "import java.util.*;\n\n"
			+ "@Common\n"
			+ "public class A1<T> {\n "
			+ "    @Common\n" 
			+ "    Collection<String> collectionOfString;\n\n" 
			+ "    @Common\n"
			+ "    Collection<List> collectionOfList;\n"
			+ "    public static class inner{}"
			+ "}";
	
		final IPath a1Path = env.addClass( srcRoot, "pkg", "A1", a1Code ); //$NON-NLS-1$ //$NON-NLS-2$
		
		// Set some per-project preferences
		IJavaProject jproj = env.getJavaProject( projName );
		AptConfig.setEnabled(jproj, true);
		fullBuild( project.getFullPath() );				
		expectingSpecificProblemsFor(a1Path, new ExpectedProblem[]{
				new ExpectedProblem("", "java.util.List is assignable to java.util.Collection", a1Path),
				new ExpectedProblem("", "java.lang.String is not assignable to java.util.Collection", a1Path),
				new ExpectedProblem("", "Type parameter 'T' belongs to org.eclipse.jdt.apt.core.internal.declaration.ClassDeclarationImpl A1", a1Path)
				}
		);
    }
	
    /**
     * Test the Types.isSubtype() API, in various inheritance scenarios
     */
    public void testBugzilla206591A() throws Exception {
    	final String projName = RegressionTests.class.getName() + "206591.Project"; //$NON-NLS-1$
		IPath projectPath = env.addProject( projName, "1.5" ); //$NON-NLS-1$
		env.addExternalJars( projectPath, Util.getJavaClassLibs() );

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

		TestUtil.createAndAddAnnotationJar( env
			.getJavaProject( projectPath ) );
		IProject project = env.getProject( projName );
		IFolder srcFolder = project.getFolder( "src" );
		IPath srcRoot = srcFolder.getFullPath();

		String a1Code = "package pkg; " + "\n"
			+ "import org.eclipse.jdt.apt.tests.annotations.apitest.SubtypeOf;\n" 
			+ "public interface A1 {\n "
			+ "}\n" 
			+ "class A2 implements A1 {\n"
			+ "}\n"
			+ "class A3 extends A2 {\n"
			+ "    @SubtypeOf(A1.class) // yes\n"
			+ "    A2 _foo;\n"
			+ "    @SubtypeOf(A1.class) // yes\n"
			+ "    A3 _bar;\n"
			+ "    @SubtypeOf(A2.class) // yes\n"
			+ "    A3 _baz;\n"
			+ "    @SubtypeOf(A1.class) // yes\n"
			+ "    A1 _quux;\n"
			+ "    @SubtypeOf(A2.class) // no\n"
			+ "    A1 _yuzz;\n"
			+ "    @SubtypeOf(String.class) // no\n"
			+ "    A2 _wum;\n"
			+ "}\n"
			+ "class A4 extends A2 implements A1 {\n"
			+ "    @SubtypeOf(A1.class) // yes\n"
			+ "    A4 _humpf;\n"
			+ "    @SubtypeOf(A2.class) // yes\n"
			+ "    A4 _fuddle;\n"
			+ "    @SubtypeOf(A5.class) // no\n"
			+ "    A4 _snee;\n"
			+ "}\n"
			+ "class A5 {\n"
			+ "}\n";
	
		final IPath a1Path = env.addClass( srcRoot, "pkg", "A1", a1Code ); //$NON-NLS-1$ //$NON-NLS-2$
		
		// Set some per-project preferences
		IJavaProject jproj = env.getJavaProject( projName );
		AptConfig.setEnabled(jproj, true);
		fullBuild( project.getFullPath() );				
		expectingSpecificProblemsFor(a1Path, new ExpectedProblem[]{
				new ExpectedProblem("", "pkg.A2 is a subtype of pkg.A1", a1Path),
				new ExpectedProblem("", "pkg.A3 is a subtype of pkg.A1", a1Path),
				new ExpectedProblem("", "pkg.A3 is a subtype of pkg.A2", a1Path),
				new ExpectedProblem("", "pkg.A1 is a subtype of pkg.A1", a1Path),
				new ExpectedProblem("", "pkg.A1 is not a subtype of pkg.A2", a1Path),
				new ExpectedProblem("", "pkg.A2 is not a subtype of java.lang.String", a1Path),
				new ExpectedProblem("", "pkg.A4 is a subtype of pkg.A1", a1Path),
				new ExpectedProblem("", "pkg.A4 is a subtype of pkg.A2", a1Path),
				new ExpectedProblem("", "pkg.A4 is not a subtype of pkg.A5", a1Path),
				}
		);
    }
	
    /**
     * Test the Types.isAssignable() API, in various inheritance scenarios
     * @throws Exception
     */
    public void testBugzilla206591B() throws Exception {
    	final String projName = RegressionTests.class.getName() + "206591.Project"; //$NON-NLS-1$
		IPath projectPath = env.addProject( projName, "1.5" ); //$NON-NLS-1$
		env.addExternalJars( projectPath, Util.getJavaClassLibs() );

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

		TestUtil.createAndAddAnnotationJar( env
			.getJavaProject( projectPath ) );
		IProject project = env.getProject( projName );
		IFolder srcFolder = project.getFolder( "src" );
		IPath srcRoot = srcFolder.getFullPath();
		
		String a1Code = "package pkg; " + "\n"
			+ "import org.eclipse.jdt.apt.tests.annotations.apitest.AssignableTo;\n" 
			+ "public interface A1 {\n "
			+ "}\n" 
			+ "class A2 implements A1 {\n"
			+ "}\n"
			+ "class A3 extends A2 {\n"
			+ "    @AssignableTo(A1.class) // yes\n"
			+ "    A2 _foo;\n"
			+ "    @AssignableTo(int.class) // yes\n"
			+ "    byte _bar;\n"
			+ "    @AssignableTo(A1.class) // yes\n"
			+ "    A3 _baz;\n"
			+ "    @AssignableTo(A2.class) // no\n"
			+ "    A1 _quux;\n"
			+ "}";
	
		final IPath a1Path = env.addClass( srcRoot, "pkg", "A1", a1Code ); //$NON-NLS-1$ //$NON-NLS-2$
		
		// Set some per-project preferences
		IJavaProject jproj = env.getJavaProject( projName );
		AptConfig.setEnabled(jproj, true);
		fullBuild( project.getFullPath() );				
		expectingSpecificProblemsFor(a1Path, new ExpectedProblem[]{
				new ExpectedProblem("", "pkg.A2 is assignable to pkg.A1", a1Path),
				new ExpectedProblem("", "byte is assignable to int", a1Path),
				new ExpectedProblem("", "pkg.A3 is assignable to pkg.A1", a1Path),
				new ExpectedProblem("", "pkg.A1 is not assignable to pkg.A2", a1Path),
				}
		);
    }

	/**
	 * Tests that a
	 * {@link AptConfig#hasProjectSpecificFactoryPath(IJavaProject)} checks if
	 * the project's factory path is equivalent to the default factory path, not
	 * just that it has created a factory path.
	 *
	 * @throws Exception
	 */
	public void testBugzilla423254() throws Exception {
		final String projName = RegressionTests.class.getName()
				+ "423254.Project"; //$NON-NLS-1$
		IPath projectPath = env.addProject(projName, "1.5"); //$NON-NLS-1$
		env.addExternalJars(projectPath, Util.getJavaClassLibs());

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

		IJavaProject jproj = env.getJavaProject(projName);
		assertFalse(AptConfig.hasProjectSpecificFactoryPath(jproj));

		IFactoryPath fp = AptConfig.getFactoryPath(jproj);
		fp.addVarJar(Path.fromOSString("/some_phony.jar"));
		AptConfig.setFactoryPath(jproj, fp);
		assertTrue(AptConfig.hasProjectSpecificFactoryPath(jproj));

		fp = AptConfig.getFactoryPath(jproj);
		fp.removeVarJar(Path.fromOSString("/some_phony.jar"));
		AptConfig.setFactoryPath(jproj, fp);
		assertFalse(AptConfig.hasProjectSpecificFactoryPath(jproj));
	}
}

Back to the top