Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTill Brychcy2017-08-03 20:53:45 +0000
committerTill Brychcy2018-01-10 07:14:17 +0000
commit56091bab8ca0004cc67f7a12186fd7908110d6e9 (patch)
treedba48bbbfef26dd7ad3dcfb1159c93a15d99b99f /org.eclipse.jdt.core.tests.builder
parent41b609123705d8e31de5555a8c4ed6a6ac0229b6 (diff)
downloadeclipse.jdt.core-56091bab8ca0004cc67f7a12186fd7908110d6e9.tar.gz
eclipse.jdt.core-56091bab8ca0004cc67f7a12186fd7908110d6e9.tar.xz
eclipse.jdt.core-56091bab8ca0004cc67f7a12186fd7908110d6e9.zip
Bug 224708 - Ability to mark source folder as test sourcesI20180111-0530
Diffstat (limited to 'org.eclipse.jdt.core.tests.builder')
-rw-r--r--org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/BuilderTests.java1
-rw-r--r--org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/TestAttributeBuilderTests.java776
-rw-r--r--org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/TestingEnvironment.java68
3 files changed, 843 insertions, 2 deletions
diff --git a/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/BuilderTests.java b/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/BuilderTests.java
index ad3e562ced..0d77e2389e 100644
--- a/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/BuilderTests.java
+++ b/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/BuilderTests.java
@@ -530,6 +530,7 @@ public class BuilderTests extends TestCase {
StaticFinalTests.class,
GetResourcesTests.class,
FriendDependencyTests.class,
+ TestAttributeBuilderTests.class,
};
if ((AbstractCompilerTest.getPossibleComplianceLevels() & AbstractCompilerTest.F_1_5) != 0) {
diff --git a/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/TestAttributeBuilderTests.java b/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/TestAttributeBuilderTests.java
new file mode 100644
index 0000000000..53fd906d72
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/TestAttributeBuilderTests.java
@@ -0,0 +1,776 @@
+/*******************************************************************************
+ * Copyright (c) 2017 Till Brychcy and others.
+ * 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:
+ * Till Brychcy - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.jdt.core.tests.builder;
+
+import java.util.HashMap;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.tests.util.Util;
+import junit.framework.Test;
+
+public class TestAttributeBuilderTests extends BuilderTests {
+
+ public TestAttributeBuilderTests(String name) {
+ super(name);
+ }
+
+ public static Test suite() {
+ return buildTestSuite(TestAttributeBuilderTests.class);
+ }
+
+ public void testWithProjectAsMainDependency() throws JavaModelException {
+ IPath project1Path = env.addProject("Project1");
+ env.removePackageFragmentRoot(project1Path, "");
+ IPath src1 = env.addPackageFragmentRoot(project1Path, "src", null, "bin");
+ IPath tests1 = env.addTestPackageFragmentRoot(project1Path, "tests");
+ env.addExternalJars(project1Path, Util.getJavaClassLibs());
+
+ env.addClass(src1, "p1", "P1Class",
+ "package p1;\n" +
+ "\n" +
+ "public class P1Class {\n"+
+ "}\n"
+ );
+ env.addClass(src1, "p1", "Production1",
+ "package p1;\n" +
+ "\n" +
+ "public class Production1 {\n" +
+ " void p1() {\n" +
+ " new P1Class(); // ok\n" +
+ " new T1Class(); // forbidden\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+ env.addClass(tests1, "p1", "T1Class",
+ "package p1;\n" +
+ "\n" +
+ "public class T1Class {\n"+
+ "}\n"
+ );
+ env.addClass(tests1, "p1", "Test1",
+ "package p1;\n" +
+ "\n" +
+ "public class Test1 {\n" +
+ " void test1() {\n" +
+ " new P1Class(); // ok\n" +
+ " new T1Class(); // ok\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+
+ IPath project2Path = env.addProject("Project2");
+ env.removePackageFragmentRoot(project2Path, "");
+ IPath src2 = env.addPackageFragmentRoot(project2Path, "src", null, "bin");
+ IPath tests2 = env.addTestPackageFragmentRoot(project2Path, "tests");
+ env.addExternalJars(project2Path, Util.getJavaClassLibs());
+ env.addRequiredProject(project2Path, project1Path);
+ env.addClass(src2, "p2", "P2Class",
+ "package p2;\n" +
+ "\n" +
+ "public class P2Class {\n"+
+ "}\n"
+ );
+ env.addClass(src2, "p2", "Production2",
+ "package p2;\n" +
+ "\n" +
+ "import p1.P1Class;\n" +
+ "import p1.T1Class;\n" +
+ "\n" +
+ "public class Production2 {\n" +
+ " void p2() {\n" +
+ " new P1Class(); // ok\n" +
+ " new P2Class(); // ok\n" +
+ " new T1Class(); // forbidden\n" +
+ " new T2Class(); // forbidden\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+ env.addClass(tests2, "p2", "T2Class",
+ "package p2;\n" +
+ "\n" +
+ "public class T2Class {\n"+
+ "}\n"
+ );
+ env.addClass(tests2, "p2", "Test2",
+ "package p2;\n" +
+ "\n" +
+ "import p1.P1Class;\n" +
+ "import p1.T1Class;\n" +
+ "\n" +
+ "public class Test2 {\n" +
+ " void test2() {\n" +
+ " new P1Class(); // ok\n" +
+ " new P2Class(); // ok\n" +
+ " new T1Class(); // ok\n" +
+ " new T2Class(); // ok\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+
+ fullBuild();
+ expectingProblemsFor(env.getWorkspaceRootPath(), "Problem : T1Class cannot be resolved to a type [ resource : </Project1/src/p1/Production1.java> range : <82,89> category : <40> severity : <2>]\n" +
+ "Problem : T1Class cannot be resolved to a type [ resource : </Project2/src/p2/Production2.java> range : <144,151> category : <40> severity : <2>]\n" +
+ "Problem : T2Class cannot be resolved to a type [ resource : </Project2/src/p2/Production2.java> range : <174,181> category : <40> severity : <2>]\n" +
+ "Problem : The import p1.T1Class cannot be resolved [ resource : </Project2/src/p2/Production2.java> range : <39,49> category : <30> severity : <2>]");
+ }
+ public void testWithProjectAsTestDependency() throws JavaModelException {
+ IPath project1Path = env.addProject("Project1");
+ env.removePackageFragmentRoot(project1Path, "");
+ IPath src1 = env.addPackageFragmentRoot(project1Path, "src", null, "bin");
+ IPath tests1 = env.addTestPackageFragmentRoot(project1Path, "tests");
+ env.addExternalJars(project1Path, Util.getJavaClassLibs());
+
+ env.addClass(src1, "p1", "P1Class",
+ "package p1;\n" +
+ "\n" +
+ "public class P1Class {\n"+
+ "}\n"
+ );
+ env.addClass(src1, "p1", "Production1",
+ "package p1;\n" +
+ "\n" +
+ "public class Production1 {\n" +
+ " void p1() {\n" +
+ " new P1Class(); // ok\n" +
+ " new T1Class(); // forbidden\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+ env.addClass(tests1, "p1", "T1Class",
+ "package p1;\n" +
+ "\n" +
+ "public class T1Class {\n"+
+ "}\n"
+ );
+ env.addClass(tests1, "p1", "Test1",
+ "package p1;\n" +
+ "\n" +
+ "public class Test1 {\n" +
+ " void test1() {\n" +
+ " new P1Class(); // ok\n" +
+ " new T1Class(); // ok\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+
+ IPath project2Path = env.addProject("Project2");
+ env.removePackageFragmentRoot(project2Path, "");
+ IPath src2 = env.addPackageFragmentRoot(project2Path, "src", null, "bin");
+ IPath tests2 = env.addTestPackageFragmentRoot(project2Path, "tests");
+ env.addExternalJars(project2Path, Util.getJavaClassLibs());
+ env.addRequiredTestProject(project2Path, project1Path);
+ env.addClass(src2, "p2", "P2Class",
+ "package p2;\n" +
+ "\n" +
+ "public class P2Class {\n"+
+ "}\n"
+ );
+ env.addClass(src2, "p2", "Production2",
+ "package p2;\n" +
+ "\n" +
+ "import p1.P1Class;\n" +
+ "import p1.T1Class;\n" +
+ "\n" +
+ "public class Production2 {\n" +
+ " void p2() {\n" +
+ " new P1Class(); // forbidden\n" +
+ " new P2Class(); // ok\n" +
+ " new T1Class(); // forbidden\n" +
+ " new T2Class(); // forbidden\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+ env.addClass(tests2, "p2", "T2Class",
+ "package p2;\n" +
+ "\n" +
+ "public class T2Class {\n"+
+ "}\n"
+ );
+ env.addClass(tests2, "p2", "Test2",
+ "package p2;\n" +
+ "\n" +
+ "import p1.P1Class;\n" +
+ "import p1.T1Class;\n" +
+ "\n" +
+ "public class Test2 {\n" +
+ " void test2() {\n" +
+ " new P1Class(); // ok\n" +
+ " new P2Class(); // ok\n" +
+ " new T1Class(); // ok\n" +
+ " new T2Class(); // ok\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+
+ fullBuild();
+ expectingProblemsFor(env.getWorkspaceRootPath(), "Problem : P1Class cannot be resolved to a type [ resource : </Project2/src/p2/Production2.java> range : <98,105> category : <40> severity : <2>]\n" +
+ "Problem : T1Class cannot be resolved to a type [ resource : </Project1/src/p1/Production1.java> range : <82,89> category : <40> severity : <2>]\n" +
+ "Problem : T1Class cannot be resolved to a type [ resource : </Project2/src/p2/Production2.java> range : <151,158> category : <40> severity : <2>]\n" +
+ "Problem : T2Class cannot be resolved to a type [ resource : </Project2/src/p2/Production2.java> range : <181,188> category : <40> severity : <2>]\n" +
+ "Problem : The import p1 cannot be resolved [ resource : </Project2/src/p2/Production2.java> range : <20,22> category : <30> severity : <2>]\n" +
+ "Problem : The import p1 cannot be resolved [ resource : </Project2/src/p2/Production2.java> range : <39,41> category : <30> severity : <2>]");
+ }
+ public void testWithProjectAsMainDependencyWithoutTestCode() throws JavaModelException {
+ IPath project1Path = env.addProject("Project1");
+ env.removePackageFragmentRoot(project1Path, "");
+ IPath src1 = env.addPackageFragmentRoot(project1Path, "src", null, "bin");
+ IPath tests1 = env.addTestPackageFragmentRoot(project1Path, "tests");
+ env.addExternalJars(project1Path, Util.getJavaClassLibs());
+
+ env.addClass(src1, "p1", "P1Class",
+ "package p1;\n" +
+ "\n" +
+ "public class P1Class {\n"+
+ "}\n"
+ );
+ env.addClass(src1, "p1", "Production1",
+ "package p1;\n" +
+ "\n" +
+ "public class Production1 {\n" +
+ " void p1() {\n" +
+ " new P1Class(); // ok\n" +
+ " new T1Class(); // forbidden\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+ env.addClass(tests1, "p1", "T1Class",
+ "package p1;\n" +
+ "\n" +
+ "public class T1Class {\n"+
+ "}\n"
+ );
+ env.addClass(tests1, "p1", "Test1",
+ "package p1;\n" +
+ "\n" +
+ "public class Test1 {\n" +
+ " void test1() {\n" +
+ " new P1Class(); // ok\n" +
+ " new T1Class(); // ok\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+
+ IPath project2Path = env.addProject("Project2");
+ env.removePackageFragmentRoot(project2Path, "");
+ IPath src2 = env.addPackageFragmentRoot(project2Path, "src", null, "bin");
+ IPath tests2 = env.addTestPackageFragmentRoot(project2Path, "tests");
+ env.addExternalJars(project2Path, Util.getJavaClassLibs());
+ env.addRequiredProjectWithoutTestCode(project2Path, project1Path);
+ env.addClass(src2, "p2", "P2Class",
+ "package p2;\n" +
+ "\n" +
+ "public class P2Class {\n"+
+ "}\n"
+ );
+ env.addClass(src2, "p2", "Production2",
+ "package p2;\n" +
+ "\n" +
+ "import p1.P1Class;\n" +
+ "import p1.T1Class;\n" +
+ "\n" +
+ "public class Production2 {\n" +
+ " void p2() {\n" +
+ " new P1Class(); // ok\n" +
+ " new P2Class(); // ok\n" +
+ " new T1Class(); // forbidden\n" +
+ " new T2Class(); // forbidden\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+ env.addClass(tests2, "p2", "T2Class",
+ "package p2;\n" +
+ "\n" +
+ "public class T2Class {\n"+
+ "}\n"
+ );
+ env.addClass(tests2, "p2", "Test2",
+ "package p2;\n" +
+ "\n" +
+ "import p1.P1Class;\n" +
+ "import p1.T1Class;\n" +
+ "\n" +
+ "public class Test2 {\n" +
+ " void test2() {\n" +
+ " new P1Class(); // ok\n" +
+ " new P2Class(); // ok\n" +
+ " new T1Class(); // ok\n" +
+ " new T2Class(); // ok\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+
+ fullBuild();
+ expectingProblemsFor(env.getWorkspaceRootPath(), "Problem : T1Class cannot be resolved to a type [ resource : </Project1/src/p1/Production1.java> range : <82,89> category : <40> severity : <2>]\n" +
+ "Problem : T1Class cannot be resolved to a type [ resource : </Project2/src/p2/Production2.java> range : <144,151> category : <40> severity : <2>]\n" +
+ "Problem : T1Class cannot be resolved to a type [ resource : </Project2/tests/p2/Test2.java> range : <141,148> category : <40> severity : <2>]\n" +
+ "Problem : T2Class cannot be resolved to a type [ resource : </Project2/src/p2/Production2.java> range : <174,181> category : <40> severity : <2>]\n" +
+ "Problem : The import p1.T1Class cannot be resolved [ resource : </Project2/src/p2/Production2.java> range : <39,49> category : <30> severity : <2>]\n" +
+ "Problem : The import p1.T1Class cannot be resolved [ resource : </Project2/tests/p2/Test2.java> range : <39,49> category : <30> severity : <2>]");
+ }
+ public void testWithProjectAsTestDependencyWithoutTestCode() throws JavaModelException {
+ IPath project1Path = env.addProject("Project1");
+ env.removePackageFragmentRoot(project1Path, "");
+ IPath src1 = env.addPackageFragmentRoot(project1Path, "src", null, "bin");
+ IPath tests1 = env.addTestPackageFragmentRoot(project1Path, "tests");
+ env.addExternalJars(project1Path, Util.getJavaClassLibs());
+
+ env.addClass(src1, "p1", "P1Class",
+ "package p1;\n" +
+ "\n" +
+ "public class P1Class {\n"+
+ "}\n"
+ );
+ env.addClass(src1, "p1", "Production1",
+ "package p1;\n" +
+ "\n" +
+ "public class Production1 {\n" +
+ " void p1() {\n" +
+ " new P1Class(); // ok\n" +
+ " new T1Class(); // forbidden\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+ env.addClass(tests1, "p1", "T1Class",
+ "package p1;\n" +
+ "\n" +
+ "public class T1Class {\n"+
+ "}\n"
+ );
+ env.addClass(tests1, "p1", "Test1",
+ "package p1;\n" +
+ "\n" +
+ "public class Test1 {\n" +
+ " void test1() {\n" +
+ " new P1Class(); // ok\n" +
+ " new T1Class(); // ok\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+
+ IPath project2Path = env.addProject("Project2");
+ env.removePackageFragmentRoot(project2Path, "");
+ IPath src2 = env.addPackageFragmentRoot(project2Path, "src", null, "bin");
+ IPath tests2 = env.addTestPackageFragmentRoot(project2Path, "tests");
+ env.addExternalJars(project2Path, Util.getJavaClassLibs());
+ env.addRequiredTestProjectWithoutTestCode(project2Path, project1Path);
+ env.addClass(src2, "p2", "P2Class",
+ "package p2;\n" +
+ "\n" +
+ "public class P2Class {\n"+
+ "}\n"
+ );
+ env.addClass(src2, "p2", "Production2",
+ "package p2;\n" +
+ "\n" +
+ "import p1.P1Class;\n" +
+ "import p1.T1Class;\n" +
+ "\n" +
+ "public class Production2 {\n" +
+ " void p2() {\n" +
+ " new P1Class(); // forbidden\n" +
+ " new P2Class(); // ok\n" +
+ " new T1Class(); // forbidden\n" +
+ " new T2Class(); // forbidden\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+ env.addClass(tests2, "p2", "T2Class",
+ "package p2;\n" +
+ "\n" +
+ "public class T2Class {\n"+
+ "}\n"
+ );
+ env.addClass(tests2, "p2", "Test2",
+ "package p2;\n" +
+ "\n" +
+ "import p1.P1Class;\n" +
+ "import p1.T1Class;\n" +
+ "\n" +
+ "public class Test2 {\n" +
+ " void test2() {\n" +
+ " new P1Class(); // ok\n" +
+ " new P2Class(); // ok\n" +
+ " new T1Class(); // ok\n" +
+ " new T2Class(); // ok\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+
+ fullBuild();
+ expectingProblemsFor(env.getWorkspaceRootPath(), "Problem : P1Class cannot be resolved to a type [ resource : </Project2/src/p2/Production2.java> range : <98,105> category : <40> severity : <2>]\n" +
+ "Problem : T1Class cannot be resolved to a type [ resource : </Project1/src/p1/Production1.java> range : <82,89> category : <40> severity : <2>]\n" +
+ "Problem : T1Class cannot be resolved to a type [ resource : </Project2/src/p2/Production2.java> range : <151,158> category : <40> severity : <2>]\n" +
+ "Problem : T1Class cannot be resolved to a type [ resource : </Project2/tests/p2/Test2.java> range : <141,148> category : <40> severity : <2>]\n" +
+ "Problem : T2Class cannot be resolved to a type [ resource : </Project2/src/p2/Production2.java> range : <181,188> category : <40> severity : <2>]\n" +
+ "Problem : The import p1 cannot be resolved [ resource : </Project2/src/p2/Production2.java> range : <20,22> category : <30> severity : <2>]\n" +
+ "Problem : The import p1 cannot be resolved [ resource : </Project2/src/p2/Production2.java> range : <39,41> category : <30> severity : <2>]\n" +
+ "Problem : The import p1.T1Class cannot be resolved [ resource : </Project2/tests/p2/Test2.java> range : <39,49> category : <30> severity : <2>]");
+ }
+
+ public void testIncrementalBuildMainChange() throws JavaModelException {
+ IPath project1Path = env.addProject("Project1");
+ env.removePackageFragmentRoot(project1Path, "");
+ IPath src1 = env.addPackageFragmentRoot(project1Path, "src", null, "bin");
+ IPath tests1 = env.addTestPackageFragmentRoot(project1Path, "tests");
+ env.addExternalJars(project1Path, Util.getJavaClassLibs());
+
+ env.addClass(src1, "p1", "P1Class",
+ "package p1;\n" +
+ "\n" +
+ "class P1Class {\n"+
+ "}\n"
+ );
+ env.addClass(tests1, "p1", "T1Class",
+ "package p1;\n" +
+ "\n" +
+ "public class T1Class extends P1Class {\n"+
+ "}\n"
+ );
+
+ IPath project2Path = env.addProject("Project2");
+ env.removePackageFragmentRoot(project2Path, "");
+ IPath src2 = env.addPackageFragmentRoot(project2Path, "src", null, "bin");
+ IPath tests2 = env.addTestPackageFragmentRoot(project2Path, "tests");
+ env.addExternalJars(project2Path, Util.getJavaClassLibs());
+ env.addRequiredProject(project2Path, project1Path);
+ env.addClass(src2, "p2", "P2Class",
+ "package p2;\n" +
+ "\n" +
+ "public class P2Class extends p1.P1Class {\n"+
+ "}\n"
+ );
+ env.addClass(tests2, "p2", "T2Class",
+ "package p2;\n" +
+ "\n" +
+ "public class T2Class {\n"+
+ "}\n"
+ );
+ env.addClass(tests2, "p2", "Test2",
+ "package p2;\n" +
+ "\n" +
+ "public class Test2 {\n" +
+ " void test2() {\n" +
+ " new P2Class();\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+
+ fullBuild();
+ expectingProblemsFor(env.getWorkspaceRootPath(), "Problem : The type p1.P1Class is not visible [ resource : </Project2/src/p2/P2Class.java> range : <42,52> category : <40> severity : <2>]");
+
+ env.addClass(src1, "p1", "P1Class",
+ "package p1;\n" +
+ "\n" +
+ "public class P1Class {\n"+
+ "}\n"
+ );
+ incrementalBuild();
+ expectingNoProblems();
+ expectingCompiledClasses(new String[] { "p1.P1Class", "p1.T1Class", "p2.P2Class", "p2.Test2" });
+ expectingCompilingOrder(new String[] { "/Project1/src/p1/P1Class.java", "/Project1/tests/p1/T1Class.java",
+ "/Project2/src/p2/P2Class.java", "/Project2/tests/p2/Test2.java" });
+ }
+ public void testIncrementalBuildTestChange() throws JavaModelException {
+ IPath project1Path = env.addProject("Project1");
+ env.removePackageFragmentRoot(project1Path, "");
+ IPath src1 = env.addPackageFragmentRoot(project1Path, "src", null, "bin");
+ IPath tests1 = env.addTestPackageFragmentRoot(project1Path, "tests");
+ env.addExternalJars(project1Path, Util.getJavaClassLibs());
+
+ env.addClass(src1, "p1", "P1Class",
+ "package p1;\n" +
+ "\n" +
+ "public class P1Class {\n"+
+ "}\n"
+ );
+ env.addClass(tests1, "p1", "T1Class",
+ "package p1;\n" +
+ "\n" +
+ "public class T1Class {\n"+
+ "}\n"
+ );
+ env.addClass(tests1, "p1", "Test1",
+ "package p1;\n" +
+ "\n" +
+ "public class Test1 {\n" +
+ " void test1() {\n" +
+ " new P1Class();" +
+ " new T1Class();" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+
+ IPath project2Path = env.addProject("Project2");
+ env.removePackageFragmentRoot(project2Path, "");
+ IPath src2 = env.addPackageFragmentRoot(project2Path, "src", null, "bin");
+ IPath tests2 = env.addTestPackageFragmentRoot(project2Path, "tests");
+ env.addExternalJars(project2Path, Util.getJavaClassLibs());
+ env.addRequiredProject(project2Path, project1Path);
+ env.addClass(src2, "p2", "P2Class",
+ "package p2;\n" +
+ "\n" +
+ "public class P2Class extends p1.P1Class {\n"+
+ "}\n"
+ );
+ env.addClass(tests2, "p2", "T2Class",
+ "package p2;\n" +
+ "\n" +
+ "public class T2Class extends p1.T1Class {\n"+
+ "}\n"
+ );
+ env.addClass(tests2, "p2", "Test2",
+ "package p2;\n" +
+ "\n" +
+ "public class Test2 extends p2.T2Class {\n" +
+ " void test2(T2Class t) {\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+
+ fullBuild();
+ expectingNoProblems();
+
+ env.addClass(tests1, "p1", "T1Class",
+ "package p1;\n" +
+ "\n" +
+ "public class T1Class extends P1Class {\n"+
+ "}\n"
+ );
+ incrementalBuild();
+ expectingNoProblems();
+ expectingCompiledClasses(new String[] { "p1.T1Class", "p1.Test1", "p2.T2Class", "p2.Test2" });
+ expectingCompilingOrder(new String[] { "/Project1/tests/p1/T1Class.java", "/Project1/tests/p1/Test1.java",
+ "/Project2/tests/p2/T2Class.java", "/Project2/tests/p2/Test2.java" });
+ }
+
+ public void testClasspathEntryTestAttributeChanges() throws JavaModelException {
+ IPath project1Path = env.addProject("Project1");
+ env.removePackageFragmentRoot(project1Path, "");
+ IPath src1 = env.addPackageFragmentRoot(project1Path, "src", null, "bin");
+ IPath tests1 = env.addTestPackageFragmentRoot(project1Path, "tests");
+ env.addExternalJars(project1Path, Util.getJavaClassLibs());
+
+ env.addClass(src1, "p1", "P1Class",
+ "package p1;\n" +
+ "\n" +
+ "public class P1Class {\n"+
+ "}\n"
+ );
+ env.addClass(src1, "p1", "P1Unrelated",
+ "package p1;\n" +
+ "\n" +
+ "public class P1Unrelated {\n"+
+ "}\n"
+ );
+ env.addClass(src1, "p1", "Production1",
+ "package p1;\n" +
+ "\n" +
+ "public class Production1 {\n" +
+ " void p1() {\n" +
+ " new P1Class(); // ok\n" +
+ " new T1Class(); // forbidden\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+ env.addClass(tests1, "p1", "T1Class",
+ "package p1;\n" +
+ "\n" +
+ "public class T1Class {\n"+
+ "}\n"
+ );
+ env.addClass(tests1, "p1", "Test1",
+ "package p1;\n" +
+ "\n" +
+ "public class Test1 {\n" +
+ " void test1() {\n" +
+ " new P1Class(); // ok\n" +
+ " new T1Class(); // ok\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+
+ IPath project2Path = env.addProject("Project2");
+ env.removePackageFragmentRoot(project2Path, "");
+ IPath src2 = env.addPackageFragmentRoot(project2Path, "src", null, "bin");
+ IPath tests2 = env.addTestPackageFragmentRoot(project2Path, "tests");
+ env.addExternalJars(project2Path, Util.getJavaClassLibs());
+ env.addRequiredProject(project2Path, project1Path);
+ env.addClass(src2, "p2", "P2Class",
+ "package p2;\n" +
+ "\n" +
+ "public class P2Class {\n"+
+ "}\n"
+ );
+ env.addClass(src1, "p2", "P2Unrelated",
+ "package p2;\n" +
+ "\n" +
+ "public class P2Unrelated {\n"+
+ "}\n"
+ );
+ env.addClass(src2, "p2", "Production2",
+ "package p2;\n" +
+ "\n" +
+ "import p1.P1Class;\n" +
+ "import p1.T1Class;\n" +
+ "\n" +
+ "public class Production2 {\n" +
+ " void p2() {\n" +
+ " new P1Class(); // ok\n" +
+ " new P2Class(); // ok\n" +
+ " new T1Class(); // forbidden\n" +
+ " new T2Class(); // forbidden\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+ env.addClass(tests2, "p2", "T2Class",
+ "package p2;\n" +
+ "\n" +
+ "public class T2Class {\n"+
+ "}\n"
+ );
+ env.addClass(tests2, "p2", "Test2",
+ "package p2;\n" +
+ "\n" +
+ "import p1.P1Class;\n" +
+ "import p1.T1Class;\n" +
+ "\n" +
+ "public class Test2 {\n" +
+ " void test2() {\n" +
+ " new P1Class(); // ok\n" +
+ " new P2Class(); // ok\n" +
+ " new T1Class(); // ok\n" +
+ " new T2Class(); // ok\n" +
+ " }\n" +
+ "}\n" +
+ ""
+ );
+
+ fullBuild();
+ expectingProblemsFor(env.getWorkspaceRootPath(), "Problem : T1Class cannot be resolved to a type [ resource : </Project1/src/p1/Production1.java> range : <82,89> category : <40> severity : <2>]\n" +
+ "Problem : T1Class cannot be resolved to a type [ resource : </Project2/src/p2/Production2.java> range : <144,151> category : <40> severity : <2>]\n" +
+ "Problem : T2Class cannot be resolved to a type [ resource : </Project2/src/p2/Production2.java> range : <174,181> category : <40> severity : <2>]\n" +
+ "Problem : The import p1.T1Class cannot be resolved [ resource : </Project2/src/p2/Production2.java> range : <39,49> category : <30> severity : <2>]");
+
+ env.changePackageFragmentRootTestAttribute(project2Path, tests2, false);
+ incrementalBuild();
+ expectingProblemsFor(env.getWorkspaceRootPath(), "Problem : T1Class cannot be resolved to a type [ resource : </Project1/src/p1/Production1.java> range : <82,89> category : <40> severity : <2>]\n" +
+ "Problem : T1Class cannot be resolved to a type [ resource : </Project2/src/p2/Production2.java> range : <144,151> category : <40> severity : <2>]\n" +
+ "Problem : T1Class cannot be resolved to a type [ resource : </Project2/tests/p2/Test2.java> range : <141,148> category : <40> severity : <2>]\n" +
+ "Problem : The import p1.T1Class cannot be resolved [ resource : </Project2/src/p2/Production2.java> range : <39,49> category : <30> severity : <2>]\n" +
+ "Problem : The import p1.T1Class cannot be resolved [ resource : </Project2/tests/p2/Test2.java> range : <39,49> category : <30> severity : <2>]");
+ expectingCompiledClasses(new String[]{"p2.P2Class","p2.Production2","p2.T2Class","p2.Test2"});
+
+ env.changePackageFragmentRootTestAttribute(project1Path, tests1, false);
+ incrementalBuild();
+ expectingNoProblems();
+ expectingCompiledClasses(new String[]{"p1.P1Class", "p1.P1Unrelated","p1.Production1","p1.T1Class","p1.Test1","p2.P2Class","p2.P2Unrelated","p2.Production2","p2.T2Class","p2.Test2"});
+
+ env.changePackageFragmentRootTestAttribute(project2Path, tests2, true);
+ incrementalBuild();
+ expectingProblemsFor(env.getWorkspaceRootPath(), "Problem : T2Class cannot be resolved to a type [ resource : </Project2/src/p2/Production2.java> range : <174,181> category : <40> severity : <2>]");
+ expectingCompiledClasses(new String[]{"p2.P2Class","p2.Production2","p2.T2Class","p2.Test2"});
+
+ env.changePackageFragmentRootTestAttribute(project1Path, tests1, true);
+ incrementalBuild();
+ expectingProblemsFor(env.getWorkspaceRootPath(), "Problem : T1Class cannot be resolved to a type [ resource : </Project1/src/p1/Production1.java> range : <82,89> category : <40> severity : <2>]\n" +
+ "Problem : T1Class cannot be resolved to a type [ resource : </Project2/src/p2/Production2.java> range : <144,151> category : <40> severity : <2>]\n" +
+ "Problem : T2Class cannot be resolved to a type [ resource : </Project2/src/p2/Production2.java> range : <174,181> category : <40> severity : <2>]\n" +
+ "Problem : The import p1.T1Class cannot be resolved [ resource : </Project2/src/p2/Production2.java> range : <39,49> category : <30> severity : <2>]");
+ expectingCompiledClasses(new String[]{"p1.P1Class", "p1.P1Unrelated","p1.Production1","p1.T1Class","p1.Test1","p2.P2Class","p2.P2Unrelated","p2.Production2","p2.T2Class","p2.Test2"});
+
+ env.changePackageFragmentRootTestAttribute(project2Path, tests2, false);
+ env.changePackageFragmentRootTestAttribute(project1Path, tests1, false);
+ incrementalBuild();
+ expectingNoProblems();
+ expectingCompiledClasses(new String[]{"p1.P1Class", "p1.P1Unrelated","p1.Production1","p1.T1Class","p1.Test1","p2.P2Class","p2.P2Unrelated","p2.Production2","p2.T2Class","p2.Test2"});
+ }
+
+ public void testExternalTestJarChanged() throws CoreException, java.io.IOException {
+ IPath projectPath = env.addProject("Project");
+ env.removePackageFragmentRoot(projectPath, "");
+ env.addExternalJars(projectPath, Util.getJavaClassLibs());
+
+ IPath tests = env.addTestPackageFragmentRoot(projectPath, "tests");
+ IPath classTest = env.addClass(tests, "p", "X",
+ "package p;\n"+
+ "public class X {\n" +
+ " void foo() {\n" +
+ " new q.Y().bar();\n" +
+ " }\n" +
+ "}"
+ );
+ String externalJar = Util.getOutputDirectory() + java.io.File.separator + "test.jar";
+ Util.createJar(
+ new String[] {
+ "q/Y.java",
+ "package q;\n" +
+ "public class Y {\n" +
+ "}"
+ },
+ new HashMap<>(),
+ externalJar
+ );
+ fullBuild();
+ expectingProblemsFor(
+ classTest,
+ "Problem : q cannot be resolved to a type [ resource : </Project/tests/p/X.java> range : <51,52> category : <40> severity : <2>]"
+ );
+ env.addExternalTestJar(projectPath, externalJar, false);
+
+ incrementalBuild();
+ expectingProblemsFor(
+ classTest,
+ "Problem : The method bar() is undefined for the type Y [ resource : </Project/tests/p/X.java> range : <57,60> category : <50> severity : <2>]"
+ );
+
+ Util.createJar(
+ new String[] {
+ "q/Y.java",
+ "package q;\n" +
+ "public class Y {\n" +
+ " public void bar() {\n" +
+ " }\n" +
+ "}"
+ },
+ new HashMap<>(),
+ externalJar
+ );
+
+ env.getProject(projectPath).touch(null);
+
+ incrementalBuild();
+ expectingNoProblems();
+ }
+}
diff --git a/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/TestingEnvironment.java b/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/TestingEnvironment.java
index 32e8febc70..86a3ee2b17 100644
--- a/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/TestingEnvironment.java
+++ b/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/TestingEnvironment.java
@@ -138,6 +138,10 @@ public void addClassFolder(IPath projectPath, IPath classFolderPath, boolean isE
return addPackageFragmentRoot(projectPath, sourceFolderName, null, null); //$NON-NLS-1$
}
+ public IPath addTestPackageFragmentRoot(IPath projectPath, String sourceFolderName) throws JavaModelException {
+ return addPackageFragmentRoot(projectPath, sourceFolderName, null, null, "bin-"+sourceFolderName, true);
+ }
+
/** Adds a package fragment root to the workspace. If
* a package fragment root with the same name already
* exists, it is not replaced. A workspace must be open.
@@ -147,12 +151,15 @@ public void addClassFolder(IPath projectPath, IPath classFolderPath, boolean isE
return addPackageFragmentRoot(projectPath, sourceFolderName, null, exclusionPatterns, specificOutputLocation);
}
+ public IPath addPackageFragmentRoot(IPath projectPath, String sourceFolderName, IPath[] inclusionPatterns, IPath[] exclusionPatterns, String specificOutputLocation) throws JavaModelException {
+ return addPackageFragmentRoot(projectPath, sourceFolderName, inclusionPatterns, exclusionPatterns, specificOutputLocation, false);
+ }
/** Adds a package fragment root to the workspace. If
* a package fragment root with the same name already
* exists, it is not replaced. A workspace must be open.
* Returns the path of the added package fragment root.
*/
- public IPath addPackageFragmentRoot(IPath projectPath, String sourceFolderName, IPath[] inclusionPatterns, IPath[] exclusionPatterns, String specificOutputLocation) throws JavaModelException {
+ public IPath addPackageFragmentRoot(IPath projectPath, String sourceFolderName, IPath[] inclusionPatterns, IPath[] exclusionPatterns, String specificOutputLocation, boolean isTest) throws JavaModelException {
checkAssertion("a workspace must be open", this.isOpen); //$NON-NLS-1$
IPath path = getPackageFragmentRootPath(projectPath, sourceFolderName);
createFolder(path);
@@ -165,7 +172,8 @@ public void addClassFolder(IPath projectPath, IPath classFolderPath, boolean isE
path,
inclusionPatterns == null ? new Path[0] : inclusionPatterns,
exclusionPatterns == null ? new Path[0] : exclusionPatterns,
- outputPath);
+ outputPath,
+ isTest ? new IClasspathAttribute[] {JavaCore.newClasspathAttribute(IClasspathAttribute.TEST, "true")} : ClasspathEntry.NO_EXTRA_ATTRIBUTES);
addEntry(projectPath, entry);
return path;
}
@@ -222,6 +230,26 @@ public void addClassFolder(IPath projectPath, IPath classFolderPath, boolean isE
public void addRequiredProject(IPath projectPath, IPath requiredProjectPath) throws JavaModelException {
addRequiredProject(projectPath, requiredProjectPath, new IPath[]{}/*include all*/, new IPath[]{}/*exclude none*/, false);
}
+ public void addRequiredTestProject(IPath projectPath, IPath requiredProjectPath) throws JavaModelException {
+ checkAssertion("required project must not be in project", !projectPath.isPrefixOf(requiredProjectPath)); //$NON-NLS-1$
+ IAccessRule[] accessRules = ClasspathEntry.getAccessRules(new IPath[]{}, new IPath[]{});
+ addEntry(projectPath, JavaCore.newProjectEntry(requiredProjectPath, accessRules, true, new IClasspathAttribute[] {JavaCore.newClasspathAttribute(IClasspathAttribute.TEST, "true")}, false));
+ }
+ public void addRequiredProjectWithoutTestCode(IPath projectPath, IPath requiredProjectPath) throws JavaModelException {
+ checkAssertion("required project must not be in project", !projectPath.isPrefixOf(requiredProjectPath)); //$NON-NLS-1$
+ IAccessRule[] accessRules = ClasspathEntry.getAccessRules(new IPath[]{}, new IPath[]{});
+ addEntry(projectPath, JavaCore.newProjectEntry(requiredProjectPath, accessRules, true, new IClasspathAttribute[] {JavaCore.newClasspathAttribute(IClasspathAttribute.WITHOUT_TEST_CODE, "true")}, false));
+ }
+
+ public void addRequiredTestProjectWithoutTestCode(IPath projectPath, IPath requiredProjectPath) throws JavaModelException {
+ checkAssertion("required project must not be in project", !projectPath.isPrefixOf(requiredProjectPath)); //$NON-NLS-1$
+ IAccessRule[] accessRules = ClasspathEntry.getAccessRules(new IPath[]{}, new IPath[]{});
+ addEntry(projectPath,
+ JavaCore.newProjectEntry(requiredProjectPath, accessRules, true,
+ new IClasspathAttribute[] { JavaCore.newClasspathAttribute(IClasspathAttribute.TEST, "true"),
+ JavaCore.newClasspathAttribute(IClasspathAttribute.WITHOUT_TEST_CODE, "true") },
+ false));
+ }
/** Adds a project to the classpath of a project.
*/
@@ -277,6 +305,15 @@ public void addClassFolder(IPath projectPath, IPath classFolderPath, boolean isE
public void addExternalJar(IPath projectPath, String jar, boolean isExported) throws JavaModelException {
addEntry(projectPath, JavaCore.newLibraryEntry(new Path(jar), null, null, isExported));
}
+ public void addExternalTestJar(IPath projectPath, String jar, boolean isExported) throws JavaModelException {
+ addEntry(projectPath, JavaCore.newLibraryEntry(
+ new Path(jar),
+ null,
+ null,
+ ClasspathEntry.NO_ACCESS_RULES,
+ new IClasspathAttribute[] {JavaCore.newClasspathAttribute(IClasspathAttribute.TEST, "true")},
+ isExported));
+ }
public void addLibrary(IPath projectPath, IPath libraryPath, IPath sourceAttachmentPath, IPath sourceAttachmentRootPath)
throws JavaModelException {
@@ -981,6 +1018,33 @@ public void cleanBuild(String projectName) {
}
}
+ public void changePackageFragmentRootTestAttribute(IPath projectPath, IPath entryPath, boolean isTest) throws JavaModelException {
+ checkAssertion("a workspace must be open", this.isOpen); //$NON-NLS-1$
+ IClasspathEntry[] oldEntries = getClasspath(projectPath);
+ for (int i = 0; i < oldEntries.length; ++i) {
+ final IClasspathEntry oldEntry = oldEntries[i];
+ if (oldEntry.getPath().equals(entryPath)) {
+ IClasspathEntry[] newEntries = oldEntries.clone();
+ IClasspathAttribute[] classpathAttributes = Arrays.stream(oldEntry.getExtraAttributes())
+ .filter(e -> !e.getName().equals(IClasspathAttribute.TEST)).toArray(IClasspathAttribute[]::new);
+ if(isTest) {
+ int length=classpathAttributes.length;
+ System.arraycopy(classpathAttributes, 0, classpathAttributes = new IClasspathAttribute[length + 1], 0, length);
+ classpathAttributes[length] = JavaCore.newClasspathAttribute(IClasspathAttribute.TEST, "true");
+ }
+ IClasspathEntry entry = JavaCore.newSourceEntry(
+ entryPath,
+ oldEntry.getInclusionPatterns(),
+ oldEntry.getExclusionPatterns(),
+ oldEntry.getOutputLocation(),
+ classpathAttributes);
+ newEntries[i] = entry;
+ setClasspath(projectPath, newEntries);
+ return;
+ }
+ }
+ }
+
/** Remove a file
*/
public void removeFile(IPath filePath) {

Back to the top