Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b5d9e7a952568768f5bbc80206573317a45a8992 (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
/*******************************************************************************
 * Copyright (c) 2005, 2007 BEA Systems, Inc. 
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    jgarms@bea.com - initial API and implementation
 *    
 *******************************************************************************/

package org.eclipse.jdt.apt.tests;

import java.util.ArrayList;
import java.util.List;

import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.ILogListener;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jdt.apt.core.internal.AptPlugin;
import org.eclipse.jdt.apt.core.util.AptUtil;
import org.eclipse.jdt.apt.tests.annotations.helloworld.HelloWorldAnnotation;
import org.eclipse.jdt.apt.tests.annotations.helloworld.HelloWorldAnnotationProcessorFactory;
import org.eclipse.jdt.apt.tests.annotations.helloworld.HelloWorldWildcardAnnotationProcessorFactory;
import org.eclipse.jdt.apt.tests.annotations.messager.MessagerAnnotationProcessor;
import org.eclipse.jdt.apt.tests.annotations.messager.MessagerCodeExample;
import org.eclipse.jdt.core.IJavaProject;

import com.sun.mirror.apt.AnnotationProcessorFactory;

public class APITests extends APTTestBase {
	
	private class LogListener implements ILogListener {
		private final List<IStatus> _messages = new ArrayList<IStatus>();
		
		public void logging(IStatus status, String plugin) {
			_messages.add(status);
		}
		
		public void clear() {
			_messages.clear();
		}
		
		public List<IStatus> getList() {
			return _messages;
		}
	}
	
	private LogListener _logListener;
	
	public APITests(final String name) {
		super( name );
	}

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

	@Override
	public void setUp() throws Exception {
		super.setUp();
		
		_logListener = new LogListener();
		AptPlugin.getPlugin().getLog().addLogListener(_logListener);
	}
	
	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		AptPlugin.getPlugin().getLog().removeLogListener(_logListener);
		_logListener = null;
	}
	
	public void testAptUtil() throws Exception {
		IJavaProject jproj = env.getJavaProject( getProjectName() );
		
		// Check getting a known annotation
		AnnotationProcessorFactory factory = 
			AptUtil.getFactoryForAnnotation(HelloWorldAnnotation.class.getName(), jproj);
		assertEquals(factory.getClass(), HelloWorldAnnotationProcessorFactory.class);
		
		// Check getting an annotation with a partial wildcard ("org.eclipse.jdt.apt.tests.*")
		factory = 
			AptUtil.getFactoryForAnnotation(HelloWorldAnnotation.class.getName() + "qwerty", jproj); //$NON-NLS-1$
		
		assertEquals(factory.getClass(), HelloWorldWildcardAnnotationProcessorFactory.class);
		
		// Check getting an annotation with a full wildcard ("*")
		// Note that these tests require that we do not cache what annotations
		// a factory claims to support. Specifically, the HelloWorldWildcard one
		// will swap out what it returns based on this static boolean.
		// If we change to cache the results, this test will need to be modified to work
		// in that scenario, probably by created a new external jar with 
		// a processor that claims *.
		HelloWorldWildcardAnnotationProcessorFactory.CLAIM_ALL_ANNOTATIONS = true;
		try {
			factory = 
				AptUtil.getFactoryForAnnotation("org.eclipse.Foo", jproj); //$NON-NLS-1$
			
			assertEquals(factory.getClass(), HelloWorldWildcardAnnotationProcessorFactory.class);
		}
		finally {
			HelloWorldWildcardAnnotationProcessorFactory.CLAIM_ALL_ANNOTATIONS = false;
		}
		
		// Make sure we've unset the wildcard behavior
		factory = 
			AptUtil.getFactoryForAnnotation("org.eclipse.Foo", jproj); //$NON-NLS-1$
		
		assertNull(factory);
		
	}
	
	public void testMessagerAPI() throws Exception {
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		IPath code = env.addClass(srcRoot, MessagerCodeExample.CODE_PACKAGE, MessagerCodeExample.CODE_CLASS_NAME, MessagerCodeExample.CODE1);
		ExpectedProblem prob1 = new ExpectedProblem("", MessagerAnnotationProcessor.PROBLEM_TEXT_WARNING, code, //$NON-NLS-1$ 
				MessagerCodeExample.WARNING_START,
				MessagerCodeExample.WARNING_END); 
		ExpectedProblem prob2 = new ExpectedProblem("", MessagerAnnotationProcessor.PROBLEM_TEXT_ERROR, code, //$NON-NLS-1$
				MessagerCodeExample.ERROR_START,
				MessagerCodeExample.ERROR_END); 
		ExpectedProblem[] problems = new ExpectedProblem[] { prob1, prob2 };
		
		// Code example with info, warning, and error messages
		_logListener.clear();
		fullBuild( project.getFullPath() );
		expectingOnlySpecificProblemsFor(code, problems);
		checkMessagerAnnotationLogEntry(
				MessagerAnnotationProcessor.PROBLEM_TEXT_INFO, 
				MessagerCodeExample.INFO_START,
				MessagerCodeExample.INFO_END);

		// Code example with info and warning messages
		env.removeClass(code, MessagerCodeExample.CODE_CLASS_NAME);
		code = env.addClass(srcRoot, MessagerCodeExample.CODE_PACKAGE, MessagerCodeExample.CODE_CLASS_NAME, MessagerCodeExample.CODE2);
		_logListener.clear();
		fullBuild( project.getFullPath() );
		problems = new ExpectedProblem[] { prob1 };
		expectingOnlySpecificProblemsFor(code, problems);
		checkMessagerAnnotationLogEntry(
				MessagerAnnotationProcessor.PROBLEM_TEXT_INFO, 
				MessagerCodeExample.INFO_START,
				MessagerCodeExample.INFO_END);
		
		// Code example with only a warning message
		env.removeClass(code, MessagerCodeExample.CODE_CLASS_NAME);
		code = env.addClass(srcRoot, MessagerCodeExample.CODE_PACKAGE, MessagerCodeExample.CODE_CLASS_NAME, MessagerCodeExample.CODE3);
		_logListener.clear();
		fullBuild( project.getFullPath() );
		expectingNoProblems();
		checkMessagerAnnotationLogEntry(
				MessagerAnnotationProcessor.PROBLEM_TEXT_INFO, 
				MessagerCodeExample.INFO_START,
				MessagerCodeExample.INFO_END);
		
		// Code example with no problems
		env.removeClass(code, MessagerCodeExample.CODE_CLASS_NAME);
		code = env.addClass(srcRoot, MessagerCodeExample.CODE_PACKAGE, MessagerCodeExample.CODE_CLASS_NAME, MessagerCodeExample.CODE4);
		_logListener.clear();
		fullBuild( project.getFullPath() );
		expectingNoProblems();
		assertTrue(_logListener.getList().isEmpty());
	}
	
	/**
	 * Check that there are exactly [targetCount] messages in the log that contain
	 * [targetMsg] and also contain "starting offset=[start]; ending offset=[end]".
	 */
	private void checkMessagerAnnotationLogEntry(String targetMsg, int start, int end) {
		int count = 0;
		final String offsetMsg = "starting offset=" + start + "; ending offset=" + end;
		for (IStatus status : _logListener.getList()) {
			String logMessage = status.getMessage();
			if (logMessage.contains(targetMsg) && logMessage.contains(offsetMsg)) {
				++count;
			}
		}
		assertEquals(1, count);
	}

	
}

Back to the top