Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 22edf933f2d0522a4b5cf060400de7a352879b25 (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
/*******************************************************************************
 * Copyright (c) 2006, 2016 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:
 *    sbandow@bea.com - initial API and implementation
 *    
 *******************************************************************************/

package org.eclipse.jdt.apt.tests;

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

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.apt.tests.annotations.ProcessorTestStatus;

public class ExceptionHandlingTests extends APTTestBase {

	public ExceptionHandlingTests(final String name) {
		super(name);
	}

	public static Test suite() {
		return new TestSuite(ExceptionHandlingTests.class);
	}
	
	/**
	 * Annotation that expects a primitive but gets its wrapper class should not throw a ClassCastException
	 */
	public void testWrapperClassForPrimitiveValue() throws Exception {
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		
		IPath testPath = env.addClass(srcRoot, "test", "Test", getCodeForTest("booleanValue = Boolean.valueOf(true)"));

		fullBuild( project.getFullPath() );
		expectingOnlySpecificProblemFor(testPath, new ExpectedProblem("Test", "Type mismatch: cannot convert from Boolean to boolean", testPath));
		assertEquals(ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());
	}
	
	/**
	 * Annotation that expects one primitive but gets another should not throw a ClassCastException
	 */
	public void testOtherPrimitiveForBooleanValue() throws Exception
	{
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		
		IPath testPath = env.addClass(srcRoot, "test", "Test", getCodeForTest("booleanValue = 2"));

		fullBuild( project.getFullPath() );
		expectingOnlySpecificProblemFor(testPath, new ExpectedProblem("Test", "Type mismatch: cannot convert from int to boolean", testPath));
		assertEquals(ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());
	}

	/**
	 * Annotation that expects a primitive but gets a String should not throw a ClassCastException
	 */
	public void testStringForBooleanValue() throws Exception
	{
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		
		IPath testPath = env.addClass(srcRoot, "test", "Test", getCodeForTest("booleanValue = \"not a boolean\""));

		fullBuild( project.getFullPath() );
		expectingOnlySpecificProblemFor(testPath, new ExpectedProblem("Test", "Type mismatch: cannot convert from String to boolean", testPath));
		assertEquals(ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());
	}

	/**
	 * Annotation that expects a primitive but gets an array should not throw a ClassCastException
	 */
	public void testArrayForBooleanValue() throws Exception
	{
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
				
		IPath testPath = env.addClass(srcRoot, "test", "Test", getCodeForTest("booleanValue = {}"));

		fullBuild( project.getFullPath() );
		ExpectedProblem ep = new ExpectedProblem("Test", "Type mismatch: cannot convert from Object[] to boolean", testPath);
		expectingOnlySpecificProblemFor(testPath, ep);
		assertEquals(ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());
	}
	
	/**
	 * Annotation that expects a String but gets a primitive should not throw a ClassCastException
	 */
	public void testPrimitiveForStringValue() throws Exception
	{
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		
		IPath testPath = env.addClass(srcRoot, "test", "Test", getCodeForTest("strValue = true"));

		fullBuild( project.getFullPath() );
		expectingOnlySpecificProblemFor(testPath, new ExpectedProblem("Test", "Type mismatch: cannot convert from boolean to String", testPath));
		assertEquals(ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());
	}

	/**
	 * Annotation that expects a String but gets another class should not throw a ClassCastException
	 */
	public void testOtherClassForStringValue() throws Exception
	{
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		
		IPath testPath = env.addClass(srcRoot, "test", "Test", getCodeForTest("strValue = new Object()"));

		fullBuild( project.getFullPath() );
		expectingOnlySpecificProblemFor(testPath, new ExpectedProblem("Test", "Type mismatch: cannot convert from Object to String", testPath));
		assertEquals(ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());
	}

	/**
	 * Annotation that expects a String but gets an array should not throw a ClassCastException
	 */
	public void testArrayForStringValue() throws Exception
	{
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		
		IPath testPath = env.addClass(srcRoot, "test", "Test", getCodeForTest("strValue = {}"));

		fullBuild( project.getFullPath() );
		ExpectedProblem ep = new ExpectedProblem("Test", "Type mismatch: cannot convert from Object[] to String", testPath);
		expectingOnlySpecificProblemFor(testPath, ep); 
		assertEquals(ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());
	}

	/**
	 * Annotation that expects an array but gets a primitive should not throw a ClassCastException
	 */
	public void testPrimitiveForArrayValue() throws Exception
	{
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		
		IPath testPath = env.addClass(srcRoot, "test", "Test", getCodeForTest("arrValue = 'c'"));

		fullBuild( project.getFullPath() );
		expectingOnlySpecificProblemFor(testPath, new ExpectedProblem("Test", "Type mismatch: cannot convert from char to String[]", testPath));
		assertEquals(ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());
	}

	/**
	 * Annotation that expects an array but gets an object should not throw a ClassCastException
	 */
	public void testNonArrayForArrayValue() throws Exception
	{
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		
		IPath testPath = env.addClass(srcRoot, "test", "Test", getCodeForTest("arrValue = new Object()"));

		fullBuild( project.getFullPath() );
		expectingOnlySpecificProblemFor(testPath, new ExpectedProblem("Test", "Type mismatch: cannot convert from Object to String[]", testPath));
		assertEquals(ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());
	}

	/**
	 * Annotation that expects an array of enums but gets an annotation should not throw a ClassCastException
	 */
	public void testAnnotationForEnumArrayValue() throws Exception
	{
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		
		IPath testPath = env.addClass(srcRoot, "test", "Test", getCodeForTest("enumsValue = @ExceptionHandlingAnnotation()"));

		fullBuild( project.getFullPath() );
		expectingOnlySpecificProblemFor(testPath, new ExpectedProblem("Test", "Type mismatch: cannot convert from ExceptionHandlingAnnotation to ExceptionHandlingAnnotation.EHAEnum[]", testPath));
		assertEquals(ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());
	}

	/**
	 * Set up the test code for APT exception handling tests
	 * @param annoValue attribute values to pass to ExceptionHandlingAnnotation
	 * @return complete test code
	 */
	private static String getCodeForTest(String annoValue) {
		return "package test;" + "\n" +
			"import org.eclipse.jdt.apt.tests.annotations.exceptionhandling.ExceptionHandlingAnnotation;" + "\n" +
			"@ExceptionHandlingAnnotation(" +
			annoValue +
			")" + "\n" +
			"public class Test" + "\n" +
			"{" + "\n" +
			"}";
	}
}

Back to the top