Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b1f7bbf5f7246799595776cc9e18ec86b207b4d7 (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
/*******************************************************************************
 * Copyright (c) 2005, 2007 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 - initial API and implementation
 *******************************************************************************/

package org.eclipse.jdt.apt.tests;

import java.io.File;

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

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.apt.core.internal.AptPlugin;
import org.eclipse.jdt.apt.core.util.AptConfig;
import org.eclipse.jdt.apt.core.util.IFactoryPath;
import org.eclipse.jdt.apt.tests.external.annotations.classloader.ColorAnnotationProcessor;
import org.eclipse.jdt.apt.tests.external.annotations.classloader.ColorTestCodeExample;
import org.eclipse.jdt.apt.tests.external.annotations.loadertest.LoaderTestAnnotationProcessor;
import org.eclipse.jdt.apt.tests.external.annotations.loadertest.LoaderTestCodeExample;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;

/**
 * 
 */
public class FactoryLoaderTests extends APTTestBase {
	
	private File _extJar; // external annotation jar
	private IPath _extVarJar; // external annotation jar, as a classpath-var-relative path
	private IPath _projectPath; // initialized in setUp(), cleared in tearDown()
	
	private final static String TEMPJARDIR_CPVAR = "FACTORYLOADERTEST_TEMP"; //$NON-NLS-1$
	
	public FactoryLoaderTests(String name)
	{
		super( name );
	}

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

	public void setUp() throws Exception {
		super.setUp();
		
		_projectPath = env.getProject( getProjectName() ).getFullPath();
		_extJar = TestUtil.createAndAddExternalAnnotationJar(
				env.getJavaProject( _projectPath ));
		
		// Create a classpath variable for the same jar file, so we can
		// refer to it that way.
		File canonicalJar = _extJar.getCanonicalFile();
		IPath jarDir = new Path( canonicalJar.getParent() );
		String extJarName = canonicalJar.getName();
		IPath varPath = new Path( TEMPJARDIR_CPVAR );
		_extVarJar = varPath.append( extJarName );
		JavaCore.setClasspathVariable( TEMPJARDIR_CPVAR, jarDir, null );

		IPath srcRoot = getSourcePath();
		String code = LoaderTestCodeExample.CODE;
		env.addClass(srcRoot, LoaderTestCodeExample.CODE_PACKAGE, LoaderTestCodeExample.CODE_CLASS_NAME, code);
		
		code = ColorTestCodeExample.CODE;
		env.addClass(srcRoot, ColorTestCodeExample.CODE_PACKAGE, ColorTestCodeExample.CODE_CLASS_NAME, code);
	}

	public void testExternalJarLoader() throws Exception {
		LoaderTestAnnotationProcessor.clearLoaded();
		IProject project = env.getProject( getProjectName() );
		fullBuild( project.getFullPath() );
		expectingNoProblems();
		assertFalse(LoaderTestAnnotationProcessor.isLoaded());
		
		IJavaProject jproj = env.getJavaProject( getProjectName() );
		IFactoryPath ifp = AptConfig.getFactoryPath(jproj);
		
		// add _extJar to the factory list as an external jar, and rebuild.
		ifp.addExternalJar(_extJar);
		AptConfig.setFactoryPath(jproj, ifp);
		
		// rebuild and verify that the processor was loaded
		LoaderTestAnnotationProcessor.clearLoaded();
		fullBuild( project.getFullPath() );
		expectingNoProblems();
		assertTrue(LoaderTestAnnotationProcessor.isLoaded());
		
		// Verify that we were able to run the ColorAnnotationProcessor successfully
		assertTrue(ColorAnnotationProcessor.wasSuccessful());
		
		// restore to the original
		ifp.removeExternalJar(_extJar);
		AptConfig.setFactoryPath(jproj, ifp);
		
		// rebuild and verify that the processor was not loaded.
		LoaderTestAnnotationProcessor.clearLoaded();
		fullBuild( project.getFullPath() );
		expectingNoProblems();
		assertFalse(LoaderTestAnnotationProcessor.isLoaded());
		
		// add _extJar to the factory list as a class-path-relative jar, and rebuild.
		ifp.addVarJar(_extVarJar);
		AptConfig.setFactoryPath(jproj, ifp);
		
		// rebuild and verify that the processor was loaded
		LoaderTestAnnotationProcessor.clearLoaded();
		fullBuild( project.getFullPath() );
		expectingNoProblems();
		assertTrue(LoaderTestAnnotationProcessor.isLoaded());
		
		// restore to the original
		ifp.removeVarJar(_extVarJar);
		AptConfig.setFactoryPath(jproj, ifp);
		
		// rebuild and verify that the processor was not loaded.
		LoaderTestAnnotationProcessor.clearLoaded();
		fullBuild( project.getFullPath() );
		expectingNoProblems();
		assertFalse(LoaderTestAnnotationProcessor.isLoaded());
	}

	// Test what happens when the factory path contains a jar file that can't be found.
	public void testNonexistentEntry() throws Exception {
		LoaderTestAnnotationProcessor.clearLoaded();
		IProject project = env.getProject( getProjectName() );
		fullBuild( project.getFullPath() );
		expectingNoProblems();
		assertFalse(LoaderTestAnnotationProcessor.isLoaded());
		
		IJavaProject jproj = env.getJavaProject( getProjectName() );
		IFactoryPath ifp = AptConfig.getFactoryPath(jproj);
		
		// add bogus entry to factory list, and rebuild.
		File bogusJar = new File("bogusJar.jar"); // assumed to not exist
		ifp.addExternalJar(bogusJar);
		
		// verify that a problem marker was added.
		AptConfig.setFactoryPath(jproj, ifp);
		fullBuild( project.getFullPath() );
		IMarker[] markers = getAllAPTMarkers(_projectPath);
		assertEquals(1, markers.length);
		assertEquals(AptPlugin.APT_LOADER_PROBLEM_MARKER, markers[0].getType());
		String message = markers[0].getAttribute(IMarker.MESSAGE, "");
		assertTrue(message.contains("bogusJar.jar"));
		
		// remove bogus entry, add _extJar to the factory list as an external jar, and rebuild.
		ifp.removeExternalJar(bogusJar);
		ifp.addExternalJar(_extJar);
		AptConfig.setFactoryPath(jproj, ifp);
		
		// rebuild and verify that the processor was loaded and the problems were removed.
		LoaderTestAnnotationProcessor.clearLoaded();
		fullBuild( project.getFullPath() );
		expectingNoProblems();
		assertTrue(LoaderTestAnnotationProcessor.isLoaded());
		
		// Verify that we were able to run the ColorAnnotationProcessor successfully
		assertTrue(ColorAnnotationProcessor.wasSuccessful());
		
		// restore to the original
		AptConfig.setFactoryPath(jproj, ifp);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jdt.core.tests.builder.Tests#tearDown()
	 */
	@Override
	protected void tearDown() throws Exception {
		JavaCore.removeClasspathVariable( TEMPJARDIR_CPVAR, null );
		_extJar = null;
		_extVarJar = null;
		_projectPath = null;
		super.tearDown();
	}
	

}

Back to the top