1 /*******************************************************************************
2  * Copyright (c) 2008, 2018 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.pde.api.tools.builder.tests.leak;
15 
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.pde.api.tools.internal.problems.ApiProblemFactory;
18 import org.eclipse.pde.api.tools.internal.provisional.descriptors.IElementDescriptor;
19 import org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblem;
20 
21 import junit.framework.Test;
22 
23 /**
24  * Tests that an API type that implements an internal type
25  * is properly detected
26  *
27  * @since 1.0
28  */
29 public class ClassImplementsLeak extends LeakTest {
30 
31 	private int pid = -1;
32 
33 	/**
34 	 * Constructor
35 	 * @param name
36 	 */
ClassImplementsLeak(String name)37 	public ClassImplementsLeak(String name) {
38 		super(name);
39 	}
40 
41 	@Override
getDefaultProblemId()42 	protected int getDefaultProblemId() {
43 		if(pid == -1) {
44 			pid = ApiProblemFactory.createProblemId(
45 					IApiProblem.CATEGORY_USAGE,
46 					IElementDescriptor.TYPE,
47 					IApiProblem.API_LEAK,
48 					IApiProblem.LEAK_IMPLEMENTS);
49 		}
50 		return pid;
51 	}
52 
53 	@Override
getTestSourcePath()54 	protected IPath getTestSourcePath() {
55 		return super.getTestSourcePath().append("class"); //$NON-NLS-1$
56 	}
57 
58 	/**
59 	 * @return the test for this class
60 	 */
suite()61 	public static Test suite() {
62 		return buildTestSuite(ClassImplementsLeak.class);
63 	}
64 
65 	/**
66 	 * Tests that a class that implements an internal interface is properly flagged
67 	 * using a full build
68 	 */
testClassImplementsLeak1F()69 	public void testClassImplementsLeak1F() {
70 		x1(false);
71 	}
72 
73 	/**
74 	 * Tests that a class that implements an internal interface is properly flagged
75 	 * using an incremental build
76 	 */
testClassImplementsLeak1I()77 	public void testClassImplementsLeak1I() {
78 		x1(true);
79 	}
80 
x1(boolean inc)81 	private void x1(boolean inc) {
82 		setExpectedProblemIds(new int[] {getDefaultProblemId()});
83 		String typename = "test8"; //$NON-NLS-1$
84 		setExpectedMessageArgs(new String[][] {{TESTING_INTERNAL_INTERFACE_NAME, typename}});
85 		deployLeakTest(typename+".java", inc); //$NON-NLS-1$
86 	}
87 
88 	/**
89 	 * Tests that an outer class that implements an internal interface is not flagged
90 	 * using a full build
91 	 */
testClassImplementsLeak2F()92 	public void testClassImplementsLeak2F() {
93 		x2(false);
94 	}
95 
96 	/**
97 	 * Tests that an outer class that implements an internal interface is not flagged
98 	 * using an incremental build
99 	 */
testClassImplementsLeak2I()100 	public void testClassImplementsLeak2I() {
101 		x2(true);
102 	}
103 
x2(boolean inc)104 	private void x2(boolean inc) {
105 		expectingNoProblems();
106 		String typename = "test9"; //$NON-NLS-1$
107 		deployLeakTest(typename+".java", inc); //$NON-NLS-1$
108 	}
109 
110 	/**
111 	 * Tests that an inner class that implements an internal interface is not flagged
112 	 * using a full build
113 	 */
testClassImplementsLeak3F()114 	public void testClassImplementsLeak3F() {
115 		x3(false);
116 	}
117 
118 	/**
119 	 * Tests that an inner class that implements an internal interface is not flagged
120 	 * using an incremental build
121 	 */
testClassImplementsLeak3I()122 	public void testClassImplementsLeak3I() {
123 		x3(true);
124 	}
125 
x3(boolean inc)126 	private void x3(boolean inc) {
127 		expectingNoProblems();
128 		String typename = "test10"; //$NON-NLS-1$
129 		deployLeakTest(typename+".java", inc); //$NON-NLS-1$
130 	}
131 
132 	/**
133 	 * Tests that a static inner class that implements an internal interface is not flagged
134 	 * using a full build
135 	 */
testClassImplementsLeak4F()136 	public void testClassImplementsLeak4F() {
137 		x4(false);
138 	}
139 
140 	/**
141 	 * Tests that a static inner class that implements an internal interface is not flagged
142 	 * using an incremental build
143 	 */
testClassImplementsLeak4I()144 	public void testClassImplementsLeak4I() {
145 		x4(true);
146 	}
147 
x4(boolean inc)148 	private void x4(boolean inc) {
149 		expectingNoProblems();
150 		String typename = "test11"; //$NON-NLS-1$
151 		deployLeakTest(typename+".java", inc); //$NON-NLS-1$
152 	}
153 
154 	/**
155 	 * Tests that a class that implements an internal interface is properly flagged
156 	 * using a full build even with an @noextend tag
157 	 */
testClassImplementsLeak5F()158 	public void testClassImplementsLeak5F() {
159 		x5(false);
160 	}
161 
162 	/**
163 	 * Tests that a class that implements an internal interface is properly flagged
164 	 * using an incremental build even with an @noextend tag
165 	 */
testClassImplementsLeak5I()166 	public void testClassImplementsLeak5I() {
167 		x5(true);
168 	}
169 
x5(boolean inc)170 	private void x5(boolean inc) {
171 		setExpectedProblemIds(new int[] {getDefaultProblemId()});
172 		String typename = "test12"; //$NON-NLS-1$
173 		setExpectedMessageArgs(new String[][] {{TESTING_INTERNAL_INTERFACE_NAME, typename}});
174 		deployLeakTest(typename+".java", inc); //$NON-NLS-1$
175 	}
176 
177 	/**
178 	 * Tests that a class that implements an internal interface is properly flagged
179 	 * using a full build even with an @noinstantiate tag
180 	 */
testClassImplementsLeak6F()181 	public void testClassImplementsLeak6F() {
182 		x6(false);
183 	}
184 
185 	/**
186 	 * Tests that a class that implements an internal interface is properly flagged
187 	 * using an incremental build even with an @noinstantiate tag
188 	 */
testClassImplementsLeak6I()189 	public void testClassImplementsLeak6I() {
190 		x6(true);
191 	}
192 
x6(boolean inc)193 	private void x6(boolean inc) {
194 		setExpectedProblemIds(new int[] {getDefaultProblemId()});
195 		String typename = "test13"; //$NON-NLS-1$
196 		setExpectedMessageArgs(new String[][] {{TESTING_INTERNAL_INTERFACE_NAME, typename}});
197 		deployLeakTest(typename+".java", inc); //$NON-NLS-1$
198 	}
199 
200 	/**
201 	 * Tests that a class that implements an internal interface is properly flagged
202 	 * using a full build even with an @noinstantiate and an @noextend tag
203 	 */
testClassImplementsLeak7F()204 	public void testClassImplementsLeak7F() {
205 		x7(false);
206 	}
207 
208 	/**
209 	 * Tests that a class that implements an internal interface is properly flagged
210 	 * using an incremental build even with an @noinstantiate and an @noextend tag
211 	 */
testClassImplementsLeak7I()212 	public void testClassImplementsLeak7I() {
213 		x7(true);
214 	}
215 
x7(boolean inc)216 	private void x7(boolean inc) {
217 		setExpectedProblemIds(new int[] {getDefaultProblemId()});
218 		String typename = "test14"; //$NON-NLS-1$
219 		setExpectedMessageArgs(new String[][] {{TESTING_INTERNAL_INTERFACE_NAME, typename}});
220 		deployLeakTest(typename+".java", inc); //$NON-NLS-1$
221 	}
222 
223 	/**
224 	 * Tests that an @noextend tag(s) does not prevent an implement leak problem from being reported
225 	 * using a full build
226 	 */
testClassImplementsLeak8F()227 	public void testClassImplementsLeak8F() {
228 		x8(false);
229 	}
230 
231 	/**
232 	 * Tests that an @noextend tag(s) does not prevent an implement leak problem from being reported
233 	 * using an incremental build
234 	 */
testClassImplementsLeak8I()235 	public void testClassImplementsLeak8I() {
236 		x8(true);
237 	}
238 
x8(boolean inc)239 	private void x8(boolean inc) {
240 		setExpectedProblemIds(new int[] {getDefaultProblemId(), getDefaultProblemId(), getDefaultProblemId()});
241 		String typename = "test20"; //$NON-NLS-1$
242 		String innertype = "inner"; //$NON-NLS-1$
243 		String innertype2 = "inner2"; //$NON-NLS-1$
244 		setExpectedMessageArgs(new String[][] {{TESTING_INTERNAL_INTERFACE_NAME, typename},
245 				{TESTING_INTERNAL_INTERFACE_NAME, innertype},
246 				{TESTING_INTERNAL_INTERFACE_NAME, innertype2}});
247 		deployLeakTest(typename+".java", inc); //$NON-NLS-1$
248 	}
249 
250 	/**
251 	 * Tests that an @noinstantiate tag(s) does not prevent an implement leak problem from being reported
252 	 * using a full build
253 	 */
testClassImplementsLeak9F()254 	public void testClassImplementsLeak9F() {
255 		x9(false);
256 	}
257 
258 	/**
259 	 * Tests that an @noinstantiate tag(s) does not prevent an implement leak problem from being reported
260 	 * using an incremental build
261 	 */
testClassImplementsLeak9I()262 	public void testClassImplementsLeak9I() {
263 		x9(true);
264 	}
265 
x9(boolean inc)266 	private void x9(boolean inc) {
267 		setExpectedProblemIds(new int[] {getDefaultProblemId(), getDefaultProblemId(), getDefaultProblemId()});
268 		String typename = "test21"; //$NON-NLS-1$
269 		String innertype = "inner"; //$NON-NLS-1$
270 		String innertype2 = "inner2"; //$NON-NLS-1$
271 		setExpectedMessageArgs(new String[][] {{TESTING_INTERNAL_INTERFACE_NAME, typename},
272 				{TESTING_INTERNAL_INTERFACE_NAME, innertype},
273 				{TESTING_INTERNAL_INTERFACE_NAME, innertype2}});
274 		deployLeakTest(typename+".java", inc); //$NON-NLS-1$
275 	}
276 
277 	/**
278 	 * Tests that an @noinstantiate and @noextend tag(s) does not prevent an implement leak problem from being reported
279 	 * using a full build
280 	 */
testClassImplementsLeak10F()281 	public void testClassImplementsLeak10F() {
282 		x10(false);
283 	}
284 
285 	/**
286 	 * Tests that an @noinstantiate and @noextend tag(s) does not prevent an implement leak problem from being reported
287 	 * using an incremental build
288 	 */
testClassImplementsLeak10I()289 	public void testClassImplementsLeak10I() {
290 		x10(true);
291 	}
292 
x10(boolean inc)293 	private void x10(boolean inc) {
294 		setExpectedProblemIds(new int[] {getDefaultProblemId(), getDefaultProblemId(), getDefaultProblemId()});
295 		String typename = "test22"; //$NON-NLS-1$
296 		String innertype = "inner"; //$NON-NLS-1$
297 		String innertype2 = "inner2"; //$NON-NLS-1$
298 		setExpectedMessageArgs(new String[][] {{TESTING_INTERNAL_INTERFACE_NAME, typename},
299 				{TESTING_INTERNAL_INTERFACE_NAME, innertype},
300 				{TESTING_INTERNAL_INTERFACE_NAME, innertype2}});
301 		deployLeakTest(typename+".java", inc); //$NON-NLS-1$
302 	}
303 
304 	/**
305 	 * Tests that an API class that implements a top level non public type is a leak.
306 	 */
testClassImplementsLeak11F()307 	public void testClassImplementsLeak11F() {
308 		x11(false);
309 	}
310 
311 	/**
312 	 * Tests that an API class that implements a top level non public type is a leak.
313 	 */
testClassImplementsLeak11I()314 	public void testClassImplementsLeak11I() {
315 		x11(true);
316 	}
317 
x11(boolean inc)318 	private void x11(boolean inc) {
319 		setExpectedProblemIds(new int[] {getDefaultProblemId()});
320 		String typename = "test31"; //$NON-NLS-1$
321 		setExpectedMessageArgs(new String[][] {{"Iouter", typename}}); //$NON-NLS-1$
322 		deployLeakTest(typename+".java", inc); //$NON-NLS-1$
323 	}
324 
325 	/**
326 	 * Tests that an API class that implements noimplement interface is a leak
327 	 */
testClassImplementsNoImplementInterface12F()328 	public void testClassImplementsNoImplementInterface12F() {
329 		x12(false);
330 	}
331 
332 
testClassImplementsNoImplementInterface12I()333 	public void testClassImplementsNoImplementInterface12I() {
334 		x12(true);
335 	}
336 
x12(boolean inc)337 	private void x12(boolean inc) {
338 		setExpectedProblemIds(new int[] { getDefaultProblemId() });
339 		String typename = "test33"; //$NON-NLS-1$
340 		setExpectedMessageArgs(new String[][] { { "interfaceNoImplement", typename } }); //$NON-NLS-1$
341 		deployLeakTest(typename + ".java", inc); //$NON-NLS-1$
342 	}
343 
344 
345 }
346