diff options
author | Stephan Herrmann | 2020-06-07 11:59:34 +0000 |
---|---|---|
committer | Stephan Herrmann | 2020-06-07 11:59:34 +0000 |
commit | 9a54c517896c043647214a8f30596386c6c0923d (patch) | |
tree | 77e4700b996127a5e4ae7c6f13a23058472800d5 | |
parent | 0a5864ce26232fac7f9b2f7cef7c7d6489fb6afe (diff) | |
download | org.eclipse.objectteams-9a54c517896c043647214a8f30596386c6c0923d.tar.gz org.eclipse.objectteams-9a54c517896c043647214a8f30596386c6c0923d.tar.xz org.eclipse.objectteams-9a54c517896c043647214a8f30596386c6c0923d.zip |
Bug 562557 - Report when implicit super team org.objectteams.Team is not
accessible
- includes some test cleanup
- mark StringBasedTest as an abstract super class for others
- remove copy-paste ModifyingResourceTests
- replace inheritance from ot.AbstractJavaModelTests with statics
11 files changed, 201 insertions, 356 deletions
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ExplicitConstructorCall.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ExplicitConstructorCall.java index 0b4c1f5e4..15322f475 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ExplicitConstructorCall.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ExplicitConstructorCall.java @@ -429,6 +429,11 @@ public class ExplicitConstructorCall extends Statement implements Invocation { if (superclassRef != null && superclassRef.resolvedType != null && !superclassRef.resolvedType.isValidBinding()) { rcvHasError = true; } +//{ObjectTeams: for inaccessible o.o.Team, relevant also for JDT? + else if ((receiverType.tagBits & TagBits.HasMissingType) != 0) { + rcvHasError = true; + } +// SH} } //{ObjectTeams: compiling org.objectteams.Team$__OT__Confined ? if (receiverType == null) { diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/LookupEnvironment.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/LookupEnvironment.java index b9a038f46..707e75f06 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/LookupEnvironment.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/LookupEnvironment.java @@ -1904,6 +1904,16 @@ public ReferenceBinding getResolvedType(char[][] compoundName, ModuleBinding mod ReferenceBinding type = getType(compoundName, moduleBinding); if (type != null) return type; +//{ObjectTeams: for inaccessible o.o.Team, relevant also for JDT? + if (this.problemReporter.referenceContext instanceof ASTNode) { + Binding problem = scope.problemType(compoundName, -1, null); + if (problem instanceof ProblemReferenceBinding) { + this.problemReporter.invalidType((ASTNode) this.problemReporter.referenceContext, (ReferenceBinding) problem); + return createMissingType(null, compoundName); + } + } +// SH} + // create a proxy for the missing BinaryType // report the missing class file first this.problemReporter.isClassPathCorrect( diff --git a/testplugins/org.eclipse.objectteams.otdt.test.builder/.classpath b/testplugins/org.eclipse.objectteams.otdt.test.builder/.classpath index 2c94f8a65..8a9cc2dac 100644 --- a/testplugins/org.eclipse.objectteams.otdt.test.builder/.classpath +++ b/testplugins/org.eclipse.objectteams.otdt.test.builder/.classpath @@ -5,7 +5,7 @@ <attribute name="test" value="true"/> </attributes> </classpathentry> - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> <classpathentry kind="con" path="OTRE"/> <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> <classpathentry kind="output" path="bin"/> diff --git a/testplugins/org.eclipse.objectteams.otdt.test.builder/.settings/org.eclipse.jdt.core.prefs b/testplugins/org.eclipse.objectteams.otdt.test.builder/.settings/org.eclipse.jdt.core.prefs index 2be88eefd..92a138b92 100644 --- a/testplugins/org.eclipse.objectteams.otdt.test.builder/.settings/org.eclipse.jdt.core.prefs +++ b/testplugins/org.eclipse.objectteams.otdt.test.builder/.settings/org.eclipse.jdt.core.prefs @@ -1,15 +1,18 @@ eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.compliance=1.8 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.7 +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 org.eclipse.objectteams.otdt.compiler.option.weaving_scheme=OTDRE org.eclipse.objectteams.otdt.compiler.problem.abstract_potential_relevant_role=warning org.eclipse.objectteams.otdt.compiler.problem.adapting_deprecated=error diff --git a/testplugins/org.eclipse.objectteams.otdt.test.builder/META-INF/MANIFEST.MF b/testplugins/org.eclipse.objectteams.otdt.test.builder/META-INF/MANIFEST.MF index 50977fc17..f2aa3f243 100644 --- a/testplugins/org.eclipse.objectteams.otdt.test.builder/META-INF/MANIFEST.MF +++ b/testplugins/org.eclipse.objectteams.otdt.test.builder/META-INF/MANIFEST.MF @@ -19,6 +19,7 @@ Require-Bundle: org.junit, org.eclipse.objectteams.otdt.tests, org.eclipse.objectteams.otequinox, org.eclipse.pde.core, - org.eclipse.objectteams.otre;bundle-version="2.3.0" + org.eclipse.objectteams.otre;bundle-version="2.3.0", + org.eclipse.jdt.core.tests.model Bundle-ActivationPolicy: lazy -Bundle-RequiredExecutionEnvironment: JavaSE-1.7 +Bundle-RequiredExecutionEnvironment: JavaSE-1.8 diff --git a/testplugins/org.eclipse.objectteams.otdt.test.builder/src/org/eclipse/objectteams/otdt/test/builder/ModuleBuilderTests.java b/testplugins/org.eclipse.objectteams.otdt.test.builder/src/org/eclipse/objectteams/otdt/test/builder/ModuleBuilderTests.java new file mode 100644 index 000000000..8d1a9aa30 --- /dev/null +++ b/testplugins/org.eclipse.objectteams.otdt.test.builder/src/org/eclipse/objectteams/otdt/test/builder/ModuleBuilderTests.java @@ -0,0 +1,121 @@ +/******************************************************************************* + * Copyright (c) 2020 Stephan Herrmann. + * + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.objectteams.otdt.test.builder; + +import java.util.Arrays; +import java.util.Comparator; + +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IncrementalProjectBuilder; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.Path; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IProblemRequestor; +import org.eclipse.jdt.core.WorkingCopyOwner; +import org.eclipse.jdt.core.tests.model.ModifyingResourceTests; +import org.eclipse.objectteams.otdt.core.ext.OTREContainer; +import org.eclipse.objectteams.otdt.tests.AbstractJavaModelTests; + +import junit.framework.Test; + +public class ModuleBuilderTests extends ModifyingResourceTests { + public ModuleBuilderTests(String name) { + super(name); + } + + static { +// TESTS_NAMES = new String[] { "testReleaseOption8" }; + } + private String sourceWorkspacePath = null; + protected ProblemRequestor problemRequestor; + public static Test suite() { + if (!isJRE9) { + // almost empty suite, since we need JRE9+ + Suite suite = new Suite(ModuleBuilderTests.class.getName()); + suite.addTest(new ModuleBuilderTests("thisSuiteRunsOnJRE9plus")); + return suite; + } + return buildModelTestSuite(ModuleBuilderTests.class, BYTECODE_DECLARATION_ORDER); + } + public void thisSuiteRunsOnJRE9plus() {} + + @Override + public String getSourceWorkspacePath() { + return this.sourceWorkspacePath == null ? super.getSourceWorkspacePath() : this.sourceWorkspacePath; + } + @Override + public void setUp() throws Exception { + super.setUp(); + this.problemRequestor = new ProblemRequestor(); + this.wcOwner = new WorkingCopyOwner() { + public IProblemRequestor getProblemRequestor(ICompilationUnit unit) { + return ModuleBuilderTests.this.problemRequestor; + } + }; + } + @Override + public void setUpSuite() throws Exception { + super.setUpSuite(); + // create general purpose project here? + } + + @Override + public void tearDownSuite() throws Exception { + super.tearDownSuite(); + // delete general purpose project here? + } + + // Test that OTRE being inaccessible is properly reported + public void test001() throws CoreException { + try { + IJavaProject project = createJava9Project("Test01", new String[]{"src"}); + AbstractJavaModelTests.addOTJavaNature(project.getProject()); + AbstractJavaModelTests.addContainerEntry(project, new Path(OTREContainer.OTRE_CONTAINER_NAME), true); + this.createFile("Test01/src/module-info.java", "module test01 {}\n"); + this.createFolder("Test01/src/com/greetings"); + this.createFile("Test01/src/com/greetings/MainTeam.java", + "package com.greetings;\n" + + "public team class MainTeam {}\n"); + waitForManualRefresh(); + waitForAutoBuild(); + project.getProject().build(IncrementalProjectBuilder.FULL_BUILD, null); + assertProblemMarkers("Unexpected markers", "The type org.objectteams.Team is not accessible", project.getProject()); + } finally { + deleteProject("Test01"); + } + } + + // --- for future use: --- + + protected void assertNoErrors() throws CoreException { + for (IProject p : getWorkspace().getRoot().getProjects()) { + int maxSeverity = p.findMaxProblemSeverity(null, true, IResource.DEPTH_INFINITE); + if (maxSeverity == IMarker.SEVERITY_ERROR) { + for (IMarker marker : p.findMarkers(null, true, IResource.DEPTH_INFINITE)) + System.err.println("Marker "+ marker.toString()); + } + assertFalse("Unexpected errors in project " + p.getName(), maxSeverity == IMarker.SEVERITY_ERROR); + } + } + + // sort by CHAR_START then MESSAGE + @Override + protected void sortMarkers(IMarker[] markers) { + Arrays.sort(markers, Comparator.comparingInt((IMarker a) -> a.getAttribute(IMarker.CHAR_START, 0)) + .thenComparing((IMarker a) -> a.getAttribute(IMarker.MESSAGE, ""))); + } +} diff --git a/testplugins/org.eclipse.objectteams.otdt.tests/model/org/eclipse/objectteams/otdt/tests/otmodel/DeclaredLiftingTest.java b/testplugins/org.eclipse.objectteams.otdt.tests/model/org/eclipse/objectteams/otdt/tests/otmodel/DeclaredLiftingTest.java index 3ab8543ca..5516addc8 100644 --- a/testplugins/org.eclipse.objectteams.otdt.tests/model/org/eclipse/objectteams/otdt/tests/otmodel/DeclaredLiftingTest.java +++ b/testplugins/org.eclipse.objectteams.otdt.tests/model/org/eclipse/objectteams/otdt/tests/otmodel/DeclaredLiftingTest.java @@ -26,13 +26,13 @@ import junit.framework.Test; import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaModelException; -import org.eclipse.objectteams.otdt.tests.StringBasedTest; +import org.eclipse.objectteams.otdt.tests.AbstractStringBasedTest; /** * @author kaschja */ //TODO (kaschja) create tests by the time declared lifting has been made visible to the OT-Model -public class DeclaredLiftingTest extends StringBasedTest +public class DeclaredLiftingTest extends AbstractStringBasedTest { public DeclaredLiftingTest() { diff --git a/testplugins/org.eclipse.objectteams.otdt.tests/model/org/eclipse/objectteams/otdt/tests/otmodel/JavaElementDeltaTests.java b/testplugins/org.eclipse.objectteams.otdt.tests/model/org/eclipse/objectteams/otdt/tests/otmodel/JavaElementDeltaTests.java index fb4146c20..fb3f2f9e1 100644 --- a/testplugins/org.eclipse.objectteams.otdt.tests/model/org/eclipse/objectteams/otdt/tests/otmodel/JavaElementDeltaTests.java +++ b/testplugins/org.eclipse.objectteams.otdt.tests/model/org/eclipse/objectteams/otdt/tests/otmodel/JavaElementDeltaTests.java @@ -18,9 +18,11 @@ **********************************************************************/ package org.eclipse.objectteams.otdt.tests.otmodel; +import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.jdt.core.*; -import org.eclipse.objectteams.otdt.tests.ModifyingResourceTests; +import org.eclipse.objectteams.otdt.tests.AbstractJavaModelTests; +import org.eclipse.jdt.core.tests.model.ModifyingResourceTests; import junit.framework.Test; @@ -52,10 +54,11 @@ public JavaElementDeltaTests(String name) { */ public void testAddOTJavaNature() throws CoreException { try { - createProject("P"); + IProject project = createProject("P"); addJavaNature("P"); startDeltas(); - addOTJavaNature("P"); + AbstractJavaModelTests.addOTJavaNature(project); + assertDeltas( "Unexpected delta", "P[*]: {CONTENT}\n" + @@ -74,8 +77,8 @@ public void testAddRoleToTeam() throws CoreException { ICompilationUnit copy = null; DeltaListener listener = new DeltaListener(ElementChangedEvent.POST_CHANGE); try { - createJavaProject("P", new String[] {"src"}, "bin"); - addOTJavaNature("P"); + IJavaProject javaProject = createJavaProject("P", new String[] {"src"}, "bin"); + AbstractJavaModelTests.addOTJavaNature(javaProject.getProject()); createFolder("P/src/p1"); createFile("/P/src/p1/MyTeam.java", "package p1;\n" + @@ -120,8 +123,8 @@ public void testAddCallinToRole1() throws CoreException { ICompilationUnit copy = null; DeltaListener listener = new DeltaListener(ElementChangedEvent.POST_CHANGE); try { - createJavaProject("P", new String[] {"src"}, "bin"); - addOTJavaNature("P"); + IJavaProject javaProject = createJavaProject("P", new String[] {"src"}, "bin"); + AbstractJavaModelTests.addOTJavaNature(javaProject.getProject()); createFolder("P/src/p1"); createFile("/P/src/p1/Base.java", "package p1;\n" + @@ -176,8 +179,8 @@ public void testAddCallinToRole1r() throws CoreException { ICompilationUnit copy = null; DeltaListener listener = new DeltaListener(ElementChangedEvent.POST_RECONCILE); try { - createJavaProject("P", new String[] {"src"}, "bin"); - addOTJavaNature("P"); + IJavaProject javaProject = createJavaProject("P", new String[] {"src"}, "bin"); + AbstractJavaModelTests.addOTJavaNature(javaProject.getProject()); createFolder("P/src/p1"); createFile("/P/src/p1/Base.java", "package p1;\n" + @@ -229,8 +232,8 @@ public void testAddCallinToRole2() throws CoreException { ICompilationUnit copy = null; DeltaListener listener = new DeltaListener(ElementChangedEvent.POST_CHANGE); try { - createJavaProject("P", new String[] {"src"}, "bin"); - addOTJavaNature("P"); + IJavaProject javaProject = createJavaProject("P", new String[] {"src"}, "bin"); + AbstractJavaModelTests.addOTJavaNature(javaProject.getProject()); createFolder("P/src/p1"); createFile("/P/src/p1/Base.java", "package p1;\n" + @@ -289,8 +292,8 @@ public void testModifyCallin1() throws CoreException { ICompilationUnit copy = null; DeltaListener listener = new DeltaListener(ElementChangedEvent.POST_CHANGE); try { - createJavaProject("P", new String[] {"src"}, "bin"); - addOTJavaNature("P"); + IJavaProject javaProject = createJavaProject("P", new String[] {"src"}, "bin"); + AbstractJavaModelTests.addOTJavaNature(javaProject.getProject()); createFolder("P/src/p1"); createFile("/P/src/p1/Base.java", "package p1;\n" + @@ -350,8 +353,8 @@ public void testModifyCallin2() throws CoreException { ICompilationUnit copy = null; DeltaListener listener = new DeltaListener(ElementChangedEvent.POST_CHANGE); try { - createJavaProject("P", new String[] {"src"}, "bin"); - addOTJavaNature("P"); + IJavaProject javaProject = createJavaProject("P", new String[] {"src"}, "bin"); + AbstractJavaModelTests.addOTJavaNature(javaProject.getProject()); createFolder("P/src/p1"); createFile("/P/src/p1/Base.java", "package p1;\n" + @@ -411,8 +414,8 @@ public void testModifyCallout1() throws CoreException { ICompilationUnit copy = null; DeltaListener listener = new DeltaListener(ElementChangedEvent.POST_CHANGE); try { - createJavaProject("P", new String[] {"src"}, "bin"); - addOTJavaNature("P"); + IJavaProject javaProject = createJavaProject("P", new String[] {"src"}, "bin"); + AbstractJavaModelTests.addOTJavaNature(javaProject.getProject()); createFolder("P/src/p1"); createFile("/P/src/p1/Base.java", "package p1;\n" + @@ -472,8 +475,8 @@ public void testModifyCallout2() throws CoreException { ICompilationUnit copy = null; DeltaListener listener = new DeltaListener(ElementChangedEvent.POST_CHANGE); try { - createJavaProject("P", new String[] {"src"}, "bin"); - addOTJavaNature("P"); + IJavaProject javaProject = createJavaProject("P", new String[] {"src"}, "bin"); + AbstractJavaModelTests.addOTJavaNature(javaProject.getProject()); createFolder("P/src/p1"); createFile("/P/src/p1/Base.java", "package p1;\n" + diff --git a/testplugins/org.eclipse.objectteams.otdt.tests/src/org/eclipse/objectteams/otdt/tests/AbstractJavaModelTests.java b/testplugins/org.eclipse.objectteams.otdt.tests/src/org/eclipse/objectteams/otdt/tests/AbstractJavaModelTests.java index 153ae5d2d..b9f5acceb 100644 --- a/testplugins/org.eclipse.objectteams.otdt.tests/src/org/eclipse/objectteams/otdt/tests/AbstractJavaModelTests.java +++ b/testplugins/org.eclipse.objectteams.otdt.tests/src/org/eclipse/objectteams/otdt/tests/AbstractJavaModelTests.java @@ -363,9 +363,12 @@ public abstract class AbstractJavaModelTests extends SuiteOfTestCases { project.setDescription(description, null); } -//{ObjectTeams: new method: +//{ObjectTeams: new methods: protected void addOTJavaNature(String projectName) throws CoreException { IProject project = getWorkspaceRoot().getProject(projectName); + addOTJavaNature(project); + } + public static void addOTJavaNature(IProject project) throws CoreException { IProjectDescription description = project.getDescription(); String[] oldNatures = description.getNatureIds(); int l = oldNatures.length; @@ -375,6 +378,30 @@ public abstract class AbstractJavaModelTests extends SuiteOfTestCases { description.setNatureIds(newNatures); project.setDescription(description, null); } + public static void addContainerEntry(IJavaProject project, IPath container, boolean isModular) throws JavaModelException { + IClasspathAttribute[] extraAttributes = isModular + ? new IClasspathAttribute[] { JavaCore.newClasspathAttribute(IClasspathAttribute.MODULE, "true") } + : null; + IClasspathEntry cpe = JavaCore.newContainerEntry(container, null, extraAttributes, false); + addToClasspath(project, cpe); + } + static final String JRE_CONTAINER_NAME = "org.eclipse.jdt.launching.JRE_CONTAINER"; + public static void addToClasspath(IJavaProject jproject, IClasspathEntry cpe) throws JavaModelException { + IClasspathEntry[] oldEntries= jproject.getRawClasspath(); + ArrayList<IClasspathEntry> entries = new ArrayList<>(oldEntries.length); + for (int i= 0; i < oldEntries.length; i++) { + if (oldEntries[i].equals(cpe)) { + return; + } + IPath oldpath = oldEntries[i].getPath(); + if(JRE_CONTAINER_NAME.equals(oldpath.segment(0)) && JRE_CONTAINER_NAME.equals(cpe.getPath().segment(0))) { + continue; + } + entries.add(oldEntries[i]); + } + entries.add(cpe); + jproject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null); + } // SH} protected void assertSearchResults(String expected, Object collector) { diff --git a/testplugins/org.eclipse.objectteams.otdt.tests/src/org/eclipse/objectteams/otdt/tests/StringBasedTest.java b/testplugins/org.eclipse.objectteams.otdt.tests/src/org/eclipse/objectteams/otdt/tests/AbstractStringBasedTest.java index 7d7d33fbd..8982c17a5 100644 --- a/testplugins/org.eclipse.objectteams.otdt.tests/src/org/eclipse/objectteams/otdt/tests/StringBasedTest.java +++ b/testplugins/org.eclipse.objectteams.otdt.tests/src/org/eclipse/objectteams/otdt/tests/AbstractStringBasedTest.java @@ -28,21 +28,19 @@ import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.objectteams.otdt.core.IOTType; import org.eclipse.objectteams.otdt.core.OTModelManager; +import org.eclipse.jdt.core.tests.model.ModifyingResourceTests; /** * @author jwloka - * @version $Id: StringBasedTest.java 23494 2010-02-05 23:06:44Z stephan $ * - * NOTE: Preliminary version of a test suite for testing the OTModel. The creation - * of test classes will be changed soon! */ -public class StringBasedTest extends ModifyingResourceTests +public class AbstractStringBasedTest extends ModifyingResourceTests { public static final String TEST_PROJECT = "OTModelTest"; private IJavaProject _testPrj; - public StringBasedTest(String name) + public AbstractStringBasedTest(String name) { super(name); } diff --git a/testplugins/org.eclipse.objectteams.otdt.tests/src/org/eclipse/objectteams/otdt/tests/ModifyingResourceTests.java b/testplugins/org.eclipse.objectteams.otdt.tests/src/org/eclipse/objectteams/otdt/tests/ModifyingResourceTests.java deleted file mode 100644 index 27faa98b3..000000000 --- a/testplugins/org.eclipse.objectteams.otdt.tests/src/org/eclipse/objectteams/otdt/tests/ModifyingResourceTests.java +++ /dev/null @@ -1,323 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2000, 2006 IBM Corporation and others. - * 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: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.objectteams.otdt.tests; - -import java.io.ByteArrayInputStream; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.StringTokenizer; - -import org.eclipse.core.resources.*; -import org.eclipse.core.runtime.*; -import org.eclipse.jdt.core.*; -import org.eclipse.jdt.core.tests.util.Util; -import org.eclipse.jdt.internal.compiler.batch.Main; -import org.eclipse.jdt.internal.core.JavaElement; -import org.eclipse.objectteams.otdt.tests.AbstractJavaModelTests; - -/* - * Tests that modify resources in the workspace. - */ -//{OT_COPY_PASTE: class copied from org.eclipse.jdt.core.tests.model -public class ModifyingResourceTests extends AbstractJavaModelTests { - -public ModifyingResourceTests(String name) { - super(name); -} -protected void assertDeltas(String message, String expected) { - StringBuffer buffer = new StringBuffer(); - IJavaElementDelta[] deltas = this.deltaListener.deltas; - for (int i=0, length= deltas.length; i<length; i++) { - IJavaElementDelta[] projects = deltas[i].getAffectedChildren(); - this.sortDeltas(projects); - for (int j=0, projectsLength=projects.length; j<projectsLength; j++) { - buffer.append(projects[j]); - if (j != projectsLength-1) { - buffer.append("\n"); - } - } - IResourceDelta[] nonJavaProjects = deltas[i].getResourceDeltas(); - if (nonJavaProjects != null) { - for (int j=0, nonJavaProjectsLength=nonJavaProjects.length; j<nonJavaProjectsLength; j++) { - if (j == 0 && buffer.length() != 0) { - buffer.append("\n"); - } - buffer.append(nonJavaProjects[j]); - if (j != nonJavaProjectsLength-1) { - buffer.append("\n"); - } - } - } - if (i != length-1) { - buffer.append("\n\n"); - } - } - String actual = buffer.toString(); - if (!expected.equals(actual)) { - System.out.println(displayString(actual, 2)); - } - assertEquals( - message, - expected, - actual); -} -protected void assertElementDescendants(String message, String expected, IJavaElement element) throws CoreException { - String actual = expandAll(element); - if (!expected.equals(actual)){ - System.out.println(Util.displayString(actual, 4)); - } - assertEquals( - message, - expected, - actual); -} -/** - * E.g. <code> - * org.eclipse.jdt.tests.core.ModifyingResourceTests.generateClassFile( - * "A", - * "public class A {\n" + - * "}") - */ -public static void generateClassFile(String className, String javaSource) throws IOException { - String cu = "d:/temp/" + className + ".java"; - FileOutputStream output = new FileOutputStream(cu); - try { - output.write(javaSource.getBytes()); - } finally { - output.close(); - } - Main.compile(cu + " -d d:/temp -classpath " + System.getProperty("java.home") + "/lib/rt.jar"); - FileInputStream input = new FileInputStream("d:/temp/" + className + ".class"); - try { - System.out.println("{"); - byte[] buffer = new byte[80]; - int read = 0; - while (read != -1) { - read = input.read(buffer); - if (read != -1) System.out.print("\t"); - for (int i = 0; i < read; i++) { - System.out.print(buffer[i]); - System.out.print(", "); - } - if (read != -1) System.out.println(); - } - System.out.print("}"); - } finally { - input.close(); - } -} - -protected IFile createFile(String path, InputStream content) throws CoreException { - IFile file = this.getFile(path); - file.create(content, true, null); - return file; -} - -protected IFile createFile(String path, byte[] content) throws CoreException { - IFile file = this.getFile(path); - InputStream input = new ByteArrayInputStream(content); - file.create(input, true, null); - return file; -} - -protected IFile createFile(String path, String content) throws CoreException { - IFile file = this.getFile(path); - InputStream input = new ByteArrayInputStream(content.getBytes()); - file.create(input, true, null); - return file; -} -protected IFolder createFolder(String path) throws CoreException { - final IFolder folder = this.getFolder(path); - getWorkspace().run(new IWorkspaceRunnable() { - public void run(IProgressMonitor monitor) throws CoreException { - IContainer parent = folder.getParent(); - if (parent instanceof IFolder && !parent.exists()) { - createFolder(parent.getFullPath().toString()); - } - folder.create(true, true, null); - } - }, - null); - - return folder; -} -protected void deleteFile(String filePath) throws CoreException { - deleteResource(this.getFile(filePath)); -} -protected void deleteFolder(String folderPath) throws CoreException { - deleteResource(this.getFolder(folderPath)); -} -protected IFile editFile(String path, String content) throws CoreException { - IFile file = this.getFile(path); - InputStream input = new ByteArrayInputStream(content.getBytes()); - file.setContents(input, IResource.FORCE, null); - return file; -} -/* - * Expands (i.e. open) the given element and returns a toString() representation - * of the tree. - */ -protected String expandAll(IJavaElement element) throws CoreException { - StringBuffer buffer = new StringBuffer(); - this.expandAll(element, 0, buffer); - return buffer.toString(); -} -private void expandAll(IJavaElement element, int tab, StringBuffer buffer) throws CoreException { - IJavaElement[] children = null; - // force opening of element by getting its children - if (element instanceof IParent) { - IParent parent = (IParent)element; - children = parent.getChildren(); - } - ((JavaElement)element).toStringInfo(tab, buffer); - if (children != null) { - for (int i = 0, length = children.length; i < length; i++) { - buffer.append("\n"); - this.expandAll(children[i], tab+1, buffer); - } - } -} -protected void renameProject(String project, String newName) throws CoreException { - this.getProject(project).move(new Path(newName), true, null); -} -protected void sortDeltas(IJavaElementDelta[] deltas) { - org.eclipse.jdt.internal.core.util.Util.Comparer comparer = new org.eclipse.jdt.internal.core.util.Util.Comparer() { - public int compare(Object a, Object b) { - IJavaElementDelta deltaA = (IJavaElementDelta)a; - IJavaElementDelta deltaB = (IJavaElementDelta)b; - return deltaA.getElement().getElementName().compareTo(deltaB.getElement().getElementName()); - } - }; - org.eclipse.jdt.internal.core.util.Util.sort(deltas, comparer); -} -protected IClassFile getClassFile(String path) { - return (IClassFile)JavaCore.create(getFile(path)); -} -protected IFolder getFolder(String path) { - return getWorkspaceRoot().getFolder(new Path(path)); -} -protected IPackageFragment getPackage(String path) { - if (path.indexOf('/', 1) != -1) { // if path as more than one segment - IJavaElement element = JavaCore.create(this.getFolder(path)); - if (element instanceof IPackageFragmentRoot) { - return ((IPackageFragmentRoot)element).getPackageFragment(""); - } - return (IPackageFragment)element; - } - IProject project = this.getProject(path); - return JavaCore.create(project).getPackageFragmentRoot(project).getPackageFragment(""); -} -protected IPackageFragmentRoot getPackageFragmentRoot(String path) { - if (path.indexOf('/', 1) != -1) { // if path as more than one segment - if (path.endsWith(".jar")) { - return (IPackageFragmentRoot)JavaCore.create(this.getFile(path)); - } - return (IPackageFragmentRoot)JavaCore.create(this.getFolder(path)); - } - IProject project = this.getProject(path); - return JavaCore.create(project).getPackageFragmentRoot(project); -} -protected String getSortedByProjectDeltas() { - StringBuffer buffer = new StringBuffer(); - for (int i=0, length = this.deltaListener.deltas.length; i<length; i++) { - IJavaElementDelta[] projects = this.deltaListener.deltas[i].getAffectedChildren(); - int projectsLength = projects.length; - - // sort by project - IJavaElementDelta[] sorted = new IJavaElementDelta[projectsLength]; - System.arraycopy(projects, 0, sorted, 0, projectsLength); - org.eclipse.jdt.internal.core.util.Util.sort( - sorted, - new org.eclipse.jdt.internal.core.util.Util.Comparer() { - public int compare(Object a, Object b) { - return a.toString().compareTo(b.toString()); - } - }); - - for (int j=0; j<projectsLength; j++) { - buffer.append(sorted[j]); - if (j != projectsLength-1) { - buffer.append("\n"); - } - } - if (i != length-1) { - buffer.append("\n\n"); - } - } - return buffer.toString(); -} -protected void moveFile(String sourcePath, String destPath) throws CoreException { - this.getFile(sourcePath).move(this.getFile(destPath).getFullPath(), false, null); -} -protected void moveFolder(String sourcePath, String destPath) throws CoreException { - this.getFolder(sourcePath).move(this.getFolder(destPath).getFullPath(), false, null); -} -protected void swapFiles(String firstPath, String secondPath) throws CoreException { - final IFile first = this.getFile(firstPath); - final IFile second = this.getFile(secondPath); - IWorkspaceRunnable runnable = new IWorkspaceRunnable( ) { - public void run(IProgressMonitor monitor) throws CoreException { - IPath tempPath = first.getParent().getFullPath().append("swappingFile.temp"); - first.move(tempPath, false, monitor); - second.move(first.getFullPath(), false, monitor); - getWorkspaceRoot().getFile(tempPath).move(second.getFullPath(), false, monitor); - } - }; - getWorkspace().run(runnable, null); -} -/* - * Returns a new classpath from the given source folders and their respective exclusion patterns. - * The given array as the following form: - * [<source folder>, "<pattern>[|<pattern]*"]* - * E.g. new String[] { - * "src1", "p/A.java", - * "src2", "*.txt|com.tests/**" - * } - */ -protected IClasspathEntry[] createClasspath(String[] sourceFoldersAndPatterns, boolean hasInclusionPatterns, boolean hasExclusionPatterns) { - int length = sourceFoldersAndPatterns.length; - int increment = 1; - if (hasInclusionPatterns) increment++; - if (hasExclusionPatterns) increment++; - IClasspathEntry[] classpath = new IClasspathEntry[length/increment]; - for (int i = 0; i < length; i+=increment) { - String src = sourceFoldersAndPatterns[i]; - IPath[] inclusionPatternPaths = new IPath[0]; - if (hasInclusionPatterns) { - String patterns = sourceFoldersAndPatterns[i+1]; - StringTokenizer tokenizer = new StringTokenizer(patterns, "|"); - int patternsCount = tokenizer.countTokens(); - inclusionPatternPaths = new IPath[patternsCount]; - for (int j = 0; j < patternsCount; j++) { - inclusionPatternPaths[j] = new Path(tokenizer.nextToken()); - } - } - IPath[] exclusionPatternPaths = new IPath[0]; - if (hasExclusionPatterns) { - String patterns = sourceFoldersAndPatterns[i+increment-1]; - StringTokenizer tokenizer = new StringTokenizer(patterns, "|"); - int patternsCount = tokenizer.countTokens(); - exclusionPatternPaths = new IPath[patternsCount]; - for (int j = 0; j < patternsCount; j++) { - exclusionPatternPaths[j] = new Path(tokenizer.nextToken()); - } - } - classpath[i/increment] = JavaCore.newSourceEntry(new Path(src), inclusionPatternPaths, exclusionPatternPaths, null); - } - return classpath; -} - -} -//ike}
\ No newline at end of file |