001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.junit.tests;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.util.Arrays;
014: import java.util.HashSet;
015: import java.util.List;
016:
017: import junit.framework.TestCase;
018:
019: import org.eclipse.core.runtime.Path;
020:
021: import org.eclipse.jdt.core.ICompilationUnit;
022: import org.eclipse.jdt.core.IJavaElement;
023: import org.eclipse.jdt.core.IJavaProject;
024: import org.eclipse.jdt.core.IPackageFragment;
025: import org.eclipse.jdt.core.IPackageFragmentRoot;
026: import org.eclipse.jdt.core.IType;
027: import org.eclipse.jdt.core.JavaModelException;
028:
029: import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext;
030:
031: import org.eclipse.jdt.internal.junit.launcher.ITestKind;
032: import org.eclipse.jdt.internal.junit.launcher.JUnit4TestFinder;
033: import org.eclipse.jdt.internal.junit.launcher.TestKindRegistry;
034: import org.eclipse.jdt.internal.junit.util.TestSearchEngine;
035:
036: import org.eclipse.jdt.testplugin.JavaProjectHelper;
037:
038: public class TestTestSearchEngine extends TestCase {
039: private IJavaProject fProject;
040: private IPackageFragmentRoot fRoot;
041:
042: protected void setUp() throws Exception {
043: super .setUp();
044: fProject = JavaProjectHelper.createJavaProject("TestProject",
045: "bin");
046: JavaProjectHelper.addRTJar(fProject);
047: JavaProjectHelper.addVariableEntry(fProject, new Path(
048: "JUNIT_HOME/junit.jar"), null, null);
049: fRoot = JavaProjectHelper.addSourceContainer(fProject, "src");
050: }
051:
052: protected void tearDown() throws Exception {
053: JavaProjectHelper.delete(fProject);
054: super .tearDown();
055: }
056:
057: public void testOnePackage() throws Exception {
058: IPackageFragment p = fRoot.createPackageFragment("p", true,
059: null);
060: ICompilationUnit test1 = createCompilationUnit(p, 1);
061: ICompilationUnit test2 = createCompilationUnit(p, 2);
062:
063: IType[] result = findTests(p);
064: assertEqualTypes("Test case not found", new IType[] {
065: test1.getType("Test1"), test2.getType("Test2") },
066: result);
067: }
068:
069: public void testTwoPackages() throws Exception {
070: IPackageFragment p = fRoot.createPackageFragment("p", true,
071: null);
072: ICompilationUnit test1 = createCompilationUnit(p, 1);
073: ICompilationUnit test2 = createCompilationUnit(p, 2);
074:
075: IPackageFragment q = fRoot.createPackageFragment("q", true,
076: null);
077: ICompilationUnit test3 = createCompilationUnit(q, 3);
078:
079: IType[] result = findTests(new IJavaElement[] { p, q });
080: assertEqualTypes("Test case not found", new IType[] {
081: test1.getType("Test1"), test2.getType("Test2"),
082: test3.getType("Test3") }, result);
083: }
084:
085: public void testTwoPackagesSearchingInOne() throws Exception {
086: IPackageFragment p = fRoot.createPackageFragment("p", true,
087: null);
088: ICompilationUnit test1 = createCompilationUnit(p, 1);
089: ICompilationUnit test2 = createCompilationUnit(p, 2);
090:
091: IPackageFragment q = fRoot.createPackageFragment("q", true,
092: null);
093: createCompilationUnit(q, 3);
094:
095: IType[] result = findTests(p);
096: assertEqualTypes("Test case not found", new IType[] {
097: test1.getType("Test1"), test2.getType("Test2") },
098: result);
099: }
100:
101: public void testPackageFragmentRoot() throws Exception {
102: IPackageFragment p = fRoot.createPackageFragment("p", true,
103: null);
104: ICompilationUnit test1 = createCompilationUnit(p, 1);
105: ICompilationUnit test2 = createCompilationUnit(p, 2);
106:
107: IPackageFragment q = fRoot.createPackageFragment("q", true,
108: null);
109: ICompilationUnit test3 = createCompilationUnit(q, 3);
110:
111: IType[] result = findTests(fRoot);
112: assertEqualTypes("Test case not found", new IType[] {
113: test1.getType("Test1"), test2.getType("Test2"),
114: test3.getType("Test3") }, result);
115: }
116:
117: public void testTwoPackageFragmentRoots() throws Exception {
118: IPackageFragment p = fRoot.createPackageFragment("p", true,
119: null);
120: ICompilationUnit test1 = createCompilationUnit(p, 1);
121: ICompilationUnit test2 = createCompilationUnit(p, 2);
122:
123: IPackageFragment q = fRoot.createPackageFragment("q", true,
124: null);
125: ICompilationUnit test3 = createCompilationUnit(q, 3);
126:
127: IPackageFragmentRoot root2 = JavaProjectHelper
128: .addSourceContainer(fProject, "tests");
129:
130: IPackageFragment r = root2.createPackageFragment("r", true,
131: null);
132: ICompilationUnit test4 = createCompilationUnit(r, 4);
133: ICompilationUnit test5 = createCompilationUnit(r, 5);
134:
135: IType[] result = findTests(new IJavaElement[] { fRoot, root2 });
136: assertEqualTypes("Test case not found", new IType[] {
137: test1.getType("Test1"), test2.getType("Test2"),
138: test3.getType("Test3"), test4.getType("Test4"),
139: test5.getType("Test5") }, result);
140: }
141:
142: public void testTwoPackageFragmentRootsSearchingInOne()
143: throws Exception {
144: IPackageFragment p = fRoot.createPackageFragment("p", true,
145: null);
146: createCompilationUnit(p, 1);
147: createCompilationUnit(p, 2);
148:
149: IPackageFragment q = fRoot.createPackageFragment("q", true,
150: null);
151: createCompilationUnit(q, 3);
152:
153: IPackageFragmentRoot root2 = JavaProjectHelper
154: .addSourceContainer(fProject, "tests");
155:
156: IPackageFragment r = root2.createPackageFragment("r", true,
157: null);
158: ICompilationUnit test4 = createCompilationUnit(r, 4);
159: ICompilationUnit test5 = createCompilationUnit(r, 5);
160:
161: IType[] result = findTests(root2);
162: assertEqualTypes("Test case not found", new IType[] {
163: test4.getType("Test4"), test5.getType("Test5") },
164: result);
165: }
166:
167: public void testTwoPackageFragmentRootsSearchingInOneNoSupertype()
168: throws Exception {
169: // regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=139961
170: IPackageFragment p = fRoot.createPackageFragment("p", true,
171: null);
172: createCompilationUnit(p, 1);
173:
174: IPackageFragmentRoot root2 = JavaProjectHelper
175: .addSourceContainer(fProject, "tests");
176: IPackageFragment r = root2.createPackageFragment("r", true,
177: null);
178: ICompilationUnit testSub = r
179: .createCompilationUnit(
180: "TestSub.java",
181: "package r; import p.Test1; public class TestSub extends Test1 { }",
182: true, null);
183:
184: IType[] result = findTests(root2);
185: assertEqualTypes("Test case not found", new IType[] { testSub
186: .getType("TestSub") }, result);
187: }
188:
189: public void testProject() throws Exception {
190: IPackageFragment p = fRoot.createPackageFragment("p", true,
191: null);
192: ICompilationUnit test1 = createCompilationUnit(p, 1);
193: ICompilationUnit test2 = createCompilationUnit(p, 2);
194:
195: IPackageFragment q = fRoot.createPackageFragment("q", true,
196: null);
197: ICompilationUnit test3 = createCompilationUnit(q, 3);
198:
199: IPackageFragmentRoot root2 = JavaProjectHelper
200: .addSourceContainer(fProject, "tests");
201:
202: IPackageFragment r = root2.createPackageFragment("r", true,
203: null);
204: ICompilationUnit test4 = createCompilationUnit(r, 4);
205: ICompilationUnit test5 = createCompilationUnit(r, 5);
206:
207: IType[] result = findTests(fProject);
208: assertEqualTypes("Test case not found", new IType[] {
209: test1.getType("Test1"), test2.getType("Test2"),
210: test3.getType("Test3"), test4.getType("Test4"),
211: test5.getType("Test5") }, result);
212: }
213:
214: public void testSubPackage() throws Exception {
215: IPackageFragment p = fRoot.createPackageFragment("p", true,
216: null);
217: ICompilationUnit test1 = createCompilationUnit(p, 1);
218:
219: IPackageFragment q = fRoot.createPackageFragment("p.q", true,
220: null);
221: createCompilationUnit(q, 2);
222:
223: IType[] result = findTests(p);
224: assertEqualTypes("Test case not found", new IType[] { test1
225: .getType("Test1") }, result);
226: }
227:
228: private IType[] findTests(IJavaElement element)
229: throws InvocationTargetException, InterruptedException {
230: ITestKind testKind = TestKindRegistry
231: .getContainerTestKind(fProject);
232: return TestSearchEngine.findTests(
233: new BusyIndicatorRunnableContext(), element, testKind);
234: }
235:
236: private IType[] findTests(IJavaElement[] elements)
237: throws InvocationTargetException, InterruptedException {
238: HashSet res = new HashSet();
239: for (int i = 0; i < elements.length; i++) {
240: IType[] types = findTests(elements[i]);
241: for (int k = 0; k < types.length; k++) {
242: res.add(types[k]);
243: }
244: }
245: return (IType[]) res.toArray(new IType[res.size()]);
246: }
247:
248: public void testJUnit4NoSrc() throws Exception {
249: //regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=151003
250: IType noTest = fProject.findType("java.lang.Integer");
251: assertFalse(new JUnit4TestFinder().isTest(noTest));
252: }
253:
254: private ICompilationUnit createCompilationUnit(
255: IPackageFragment pack, int number)
256: throws JavaModelException {
257: return pack
258: .createCompilationUnit(
259: "Test" + number + ".java",
260: "package "
261: + pack.getElementName()
262: + "; import junit.framework.TestCase; public class Test"
263: + number + " extends TestCase { }",
264: true, null);
265: }
266:
267: private void assertEqualTypes(String message, IType[] expected,
268: IType[] actual) {
269: assertEquals("Wrong number of found tests", expected.length,
270: actual.length);
271: List list = Arrays.asList(expected);
272: for (int i = 0; i < actual.length; i++) {
273: if (!list.contains(expected[i])) {
274: fail(message + expected[i].getFullyQualifiedName());
275: }
276: }
277: }
278: }
|