Skip to content

Commit

Permalink
Migrate all tests in o.e.c.tests.internal.resources to JUnit 4 #903
Browse files Browse the repository at this point in the history
* Remove ResourceTest class hierarchy
* Add @test annotations
* Remove obsolete assertion methods

Contributes to
#903
  • Loading branch information
HeikoKlare committed Dec 11, 2023
1 parent de0295e commit 4709fe2
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@
* in this test package.
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({ ModelObjectReaderWriterTest.class, ProjectPreferencesTest.class,
ResourceInfoTest.class,
WorkspaceConcurrencyTest.class, WorkspacePreferencesTest.class, ProjectReferencesTest.class,
ProjectDynamicReferencesTest.class, ProjectBuildConfigsTest.class, Bug544975Test.class, })
@Suite.SuiteClasses({ //
Bug544975Test.class, //
ModelObjectReaderWriterTest.class, //
ProjectBuildConfigsTest.class, //
ProjectDynamicReferencesTest.class, //
ProjectPreferencesTest.class, //
ProjectReferencesTest.class, //
ResourceInfoTest.class, //
WorkspaceConcurrencyTest.class, //
WorkspacePreferencesTest.class, })
public class AllInternalResourcesTests {
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import static org.eclipse.core.tests.resources.ResourceTestUtil.createInWorkspace;
import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.nio.file.Files;
Expand All @@ -30,10 +32,16 @@
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.core.tests.resources.ResourceTest;
import org.eclipse.core.tests.resources.WorkspaceTestRule;
import org.junit.Rule;
import org.junit.Test;

public class Bug544975Test extends ResourceTest {
public class Bug544975Test {

@Rule
public WorkspaceTestRule workspaceRule = new WorkspaceTestRule();

@Test
public void testBug544975ProjectOpenBackgroundRefresh() throws Exception {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("Bug544975");
Expand Down Expand Up @@ -73,6 +81,7 @@ public void testBug544975ProjectOpenBackgroundRefresh() throws Exception {
}
}

@Test
public void testBug544975ProjectOpenWithoutBackgroundRefresh() throws Exception {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("Bug544975");
Expand Down Expand Up @@ -111,4 +120,5 @@ private IFile createFile(IProject project, String fileName, String initialConten
file.setContents(stream, IResource.FORCE, new NullProgressMonitor());
return file;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.eclipse.core.tests.internal.resources;

import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace;
import static org.eclipse.core.tests.harness.FileSystemHelper.getRandomLocation;
import static org.eclipse.core.tests.harness.FileSystemHelper.getTempDir;
import static org.eclipse.core.tests.resources.ResourceTestUtil.createInputStream;
import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor;
import static org.eclipse.core.tests.resources.ResourceTestUtil.removeFromFileSystem;
Expand Down Expand Up @@ -57,10 +59,16 @@
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Platform.OS;
import org.eclipse.core.tests.resources.ResourceTest;
import org.eclipse.core.tests.resources.WorkspaceTestRule;
import org.junit.Rule;
import org.junit.Test;
import org.xml.sax.InputSource;

public class ModelObjectReaderWriterTest extends ResourceTest {
public class ModelObjectReaderWriterTest {

@Rule
public WorkspaceTestRule workspaceRule = new WorkspaceTestRule();

static final IPath LONG_LOCATION = IPath.fromOSString("/eclipse/dev/i0218/eclipse/pffds/fds//fds///fdsfsdfsd///fdsfdsf/fsdfsdfsd/lugi/dsds/fsd//f/ffdsfdsf/fsdfdsfsd/fds//fdsfdsfdsf/fdsfdsfds/fdsfdsfdsf/fdsfdsfdsds/ns/org.eclipse.help.ui_2.1.0/contexts.xml").setDevice(OS.isWindows() ? "D:" : null);
static final URI LONG_LOCATION_URI = LONG_LOCATION.toFile().toURI();
private static final String PATH_STRING = IPath.fromOSString("/abc/def").setDevice(OS.isWindows() ? "D:" : null).toString();
Expand Down Expand Up @@ -282,14 +290,15 @@ private ProjectDescription readDescription(IFileStore store) throws CoreExceptio
* Verifies that project description file is written in a consistent way.
* (bug 177148)
*/
@Test
public void testConsistentWrite() throws Throwable {
String locationA = getTempDir().append("testPath1").toPortableString();
String locationB = getTempDir().append("testPath1").toPortableString();
String newline = System.lineSeparator();
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + newline + "<projectDescription>" + newline + " <name>MyProjectDescription</name>" + newline + " <comment></comment>" + newline + " <projects>" + newline + " </projects>" + newline + " <buildSpec>" + newline + " <buildCommand>" + newline + " <name>MyCommand</name>" + newline + " <arguments>" + newline + " <dictionary>" + newline + " <key>aA</key>" + newline + " <value>2 x ARGH!</value>" + newline + " </dictionary>" + newline + " <dictionary>" + newline + " <key>b</key>" + newline + " <value>ARGH!</value>" + newline + " </dictionary>" + newline
+ " </arguments>" + newline + " </buildCommand>" + newline + " </buildSpec>" + newline + " <natures>" + newline + " </natures>" + newline + " <linkedResources>" + newline + " <link>" + newline + " <name>pathA</name>" + newline + " <type>2</type>" + newline + " <location>" + locationA + "</location>" + newline + " </link>" + newline + " <link>" + newline + " <name>pathB</name>" + newline + " <type>2</type>" + newline + " <location>" + locationB + "</location>" + newline + " </link>" + newline + " </linkedResources>" + newline + "</projectDescription>" + newline;

IFileStore tempStore = getTempStore();
IFileStore tempStore = workspaceRule.getTempStore();
URI location = tempStore.toURI();

ProjectDescription description = new ProjectDescription();
Expand Down Expand Up @@ -320,13 +329,14 @@ public void testConsistentWrite() throws Throwable {
assertThat(result, is(expected));
}

@Test
public void testInvalidProjectDescription1() throws Throwable {
String invalidProjectDescription = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<homeDescription>\n" + " <name>abc</name>\n" + " <comment></comment>\n" + " <projects>\n" + " </projects>\n" + " <buildSpec>\n" + " <buildCommand>\n" + " <name>org.eclipse.jdt.core.javabuilder</name>\n" + " <arguments>\n" + " </arguments>\n" + " </buildCommand>\n" + " </buildSpec>\n" + " <natures>\n" + " <nature>org.eclipse.jdt.core.javanature</nature>\n" + " </natures>\n" + " <linkedResources>\n" + " <link>\n" + " <name>newLink</name>\n" + " <type>2</type>\n" + " <location>" + PATH_STRING + "</location>\n" + " </link>\n" + " </linkedResources>\n" + "</homeDescription>";

IWorkspace workspace = getWorkspace();
IPath root = workspace.getRoot().getLocation();
IPath location = root.append("ModelObjectReaderWriterTest.txt");
deleteOnTearDown(location);
workspaceRule.deleteOnTearDown(location);

ProjectDescriptionReader reader = new ProjectDescriptionReader(workspace);
// Write out the project description file
Expand All @@ -338,10 +348,11 @@ public void testInvalidProjectDescription1() throws Throwable {
assertThat(projDesc, nullValue());
}

@Test
public void testInvalidProjectDescription2() throws Throwable {
String invalidProjectDescription = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<projectDescription>\n" + " <bogusname>abc</bogusname>\n" + "</projectDescription>";

IFileStore store = getTempStore();
IFileStore store = workspaceRule.getTempStore();
// Write out the project description file
try (OutputStream output = store.openOutputStream(EFS.NONE, null)) {
createInputStream(invalidProjectDescription).transferTo(output);
Expand All @@ -357,10 +368,11 @@ public void testInvalidProjectDescription2() throws Throwable {
assertThat(projDesc.getLinks(), nullValue());
}

@Test
public void testInvalidProjectDescription3() throws Throwable {
String invalidProjectDescription = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<projectDescription>\n" + " <name>abc</name>\n" + " <comment></comment>\n" + " <projects>\n" + " </projects>\n" + " <buildSpec>\n" + " <badBuildCommand>\n" + " <name>org.eclipse.jdt.core.javabuilder</name>\n" + " <arguments>\n" + " </arguments>\n" + " </badBuildCommand>\n" + " </buildSpec>\n" + "</projectDescription>";

IFileStore store = getTempStore();
IFileStore store = workspaceRule.getTempStore();
// Write out the project description file
try (OutputStream output = store.openOutputStream(EFS.NONE, null)) {
createInputStream(invalidProjectDescription).transferTo(output);
Expand All @@ -377,10 +389,11 @@ public void testInvalidProjectDescription3() throws Throwable {
assertThat(projDesc.getLinks(), nullValue());
}

@Test
public void testInvalidProjectDescription4() throws Throwable {
String invalidProjectDescription = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<projectDescription>\n" + " <name>abc</name>\n" + " <comment></comment>\n" + " <projects>\n" + " </projects>\n" + " <buildSpec>\n" + " </buildSpec>\n" + " <natures>\n" + " </natures>\n" + " <linkedResources>\n" + " <link>\n" + " <name>newLink</name>\n" + " <type>foobar</type>\n" + " <location>" + PATH_STRING + "</location>\n" + " </link>\n" + " </linkedResources>\n" + "</projectDescription>";

IFileStore store = getTempStore();
IFileStore store = workspaceRule.getTempStore();
// Write out the project description file
try (OutputStream output = store.openOutputStream(EFS.NONE, null)) {
createInputStream(invalidProjectDescription).transferTo(output);
Expand All @@ -401,11 +414,12 @@ public void testInvalidProjectDescription4() throws Throwable {
/**
* Tests a project description with a very long local location for a linked resource.
*/
@Test
public void testLongProjectDescription() throws Throwable {
String longProjectDescription = getLongDescription();

IPath location = getRandomLocation();
deleteOnTearDown(location);
workspaceRule.deleteOnTearDown(location);

ProjectDescriptionReader reader = new ProjectDescriptionReader(getWorkspace());
// Write out the project description file
Expand All @@ -424,10 +438,11 @@ public void testLongProjectDescription() throws Throwable {
/**
* Tests a project description with a very long URI location for linked resource.
*/
@Test
public void testLongProjectDescriptionURI() throws Throwable {
String longProjectDescription = getLongDescriptionURI();
IPath location = getRandomLocation();
deleteOnTearDown(location);
workspaceRule.deleteOnTearDown(location);

ProjectDescriptionReader reader = new ProjectDescriptionReader(ResourcesPlugin.getWorkspace());
// Write out the project description file
Expand All @@ -443,6 +458,7 @@ public void testLongProjectDescriptionURI() throws Throwable {
}
}

@Test
public void testMultiLineCharFields() throws Throwable {
String multiLineProjectDescription = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<projectDescription>\n" + " <name>\n" + " abc\n" + " </name>\n" + " <charset>\n" + " ISO-8859-1\n" + " </charset>\n" + " <comment>This is the comment.</comment>\n" + " <projects>\n" + " <project>\n" + " org.eclipse.core.boot\n" + " </project>\n" + " </projects>\n" + " <buildSpec>\n" + " <buildCommand>\n" + " <name>\n" + " org.eclipse.jdt.core.javabuilder\n" + " </name>\n" + " <arguments>\n" + " <key>thisIsTheKey</key>\n" + " <value>thisIsTheValue</value>\n" + " </arguments>\n" + " </buildCommand>\n" + " </buildSpec>\n" + " <natures>\n" + " <nature>\n"
+ " org.eclipse.jdt.core.javanature\n" + " </nature>\n" + " </natures>\n" + " <linkedResources>\n" + " <link>\n" + " <name>" + "newLink" + "</name>\n" + " <type>\n" + " 2\n" + " </type>\n" + " <location>" + PATH_STRING + "</location>\n" + " </link>\n" + " </linkedResources>\n" + "</projectDescription>";
Expand All @@ -453,9 +469,9 @@ public void testMultiLineCharFields() throws Throwable {
IWorkspace workspace = getWorkspace();
IPath root = workspace.getRoot().getLocation();
IPath multiLocation = root.append("multiLineTest.txt");
deleteOnTearDown(multiLocation);
workspaceRule.deleteOnTearDown(multiLocation);
IPath singleLocation = root.append("singleLineTest.txt");
deleteOnTearDown(singleLocation);
workspaceRule.deleteOnTearDown(singleLocation);

ProjectDescriptionReader reader = new ProjectDescriptionReader(workspace);
// Write out the project description file
Expand All @@ -472,6 +488,7 @@ public void testMultiLineCharFields() throws Throwable {
compareProjectDescriptions(1, multiDesc, singleDesc);
}

@Test
public void testMultipleProjectDescriptions() throws Throwable {
URL whereToLook = Platform.getBundle("org.eclipse.core.tests.resources").getEntry("MultipleProjectTestFiles/");
String[] members = {"abc.project", "def.project", "org.apache.lucene.project", "org.eclipse.ant.core.project"};
Expand All @@ -489,9 +506,9 @@ public void testMultipleProjectDescriptions() throws Throwable {
}
}

@Test
public void testProjectDescription() throws Throwable {

IFileStore tempStore = getTempStore();
IFileStore tempStore = workspaceRule.getTempStore();
URI location = tempStore.toURI();
/* test write */
ProjectDescription description = new ProjectDescription();
Expand Down Expand Up @@ -532,13 +549,14 @@ public void testProjectDescription() throws Throwable {
assertThat(commands2[0].getArguments().get("EmptyArg"), emptyString());
}

@Test
public void testProjectDescription2() throws Throwable {
// Use ModelObject2 to read the project description

/* initialize common objects */
ModelObjectWriter writer = new ModelObjectWriter();
ProjectDescriptionReader reader = new ProjectDescriptionReader(getWorkspace());
IFileStore tempStore = getTempStore();
IFileStore tempStore = workspaceRule.getTempStore();
URI location = tempStore.toURI();
/* test write */
ProjectDescription description = new ProjectDescription();
Expand Down Expand Up @@ -589,8 +607,9 @@ public void testProjectDescription2() throws Throwable {
}

// see bug 274437
@Test
public void testProjectDescription3() throws Throwable {
IFileStore tempStore = getTempStore();
IFileStore tempStore = workspaceRule.getTempStore();
URI location = tempStore.toURI();
/* test write */
ProjectDescription description = new ProjectDescription();
Expand All @@ -615,9 +634,9 @@ public void testProjectDescription3() throws Throwable {
assertThat(commands2[0].getArguments(), anEmptyMap());
}

@Test
public void testProjectDescriptionWithSpaces() throws Throwable {

IFileStore store = getTempStore();
IFileStore store = workspaceRule.getTempStore();
IPath path = IPath.fromOSString("link");
URI location = store.toURI();
URI locationWithSpaces = store.getChild("With some spaces").toURI();
Expand Down Expand Up @@ -650,6 +669,7 @@ private void writeDescription(IFileStore store, ProjectDescription description)
}

// Regression for Bug 300669
@Test
public void testProjectDescriptionWithFiltersAndNullProject() throws Exception {
String projectDescription = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + //
"<projectDescription>\n" + //
Expand Down Expand Up @@ -679,7 +699,7 @@ public void testProjectDescriptionWithFiltersAndNullProject() throws Exception {

IPath root = getWorkspace().getRoot().getLocation();
IPath location = root.append("ModelObjectReaderWriterTest.txt");
deleteOnTearDown(location);
workspaceRule.deleteOnTearDown(location);

ProjectDescriptionReader reader = new ProjectDescriptionReader(getWorkspace());
// Write out the project description file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.tests.resources.ResourceTest;
import org.eclipse.core.tests.resources.WorkspaceTestRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

/**
* Tests configuration project build configuration on the project description
*/
public class ProjectBuildConfigsTest extends ResourceTest {
public class ProjectBuildConfigsTest {

@Rule
public WorkspaceTestRule workspaceRule = new WorkspaceTestRule();

private IProject project;
private static final String variantId0 = "Variant0";
Expand All @@ -41,9 +47,8 @@ public class ProjectBuildConfigsTest extends ResourceTest {
private IBuildConfiguration variant2;
private IBuildConfiguration defaultVariant;

@Override
@Before
public void setUp() throws Exception {
super.setUp();
project = getWorkspace().getRoot().getProject("ProjectBuildConfigsTest_Project");
createInWorkspace(new IProject[] {project});
variant0 = new BuildConfiguration(project, variantId0);
Expand All @@ -52,6 +57,7 @@ public void setUp() throws Exception {
defaultVariant = new BuildConfiguration(project, IBuildConfiguration.DEFAULT_CONFIG_NAME);
}

@Test
public void testBasics() throws CoreException {
IProjectDescription desc = project.getDescription();
String[] configs = new String[] {variantId0, variantId1};
Expand Down Expand Up @@ -84,13 +90,15 @@ public void testBasics() throws CoreException {
assertThat(variant.getName(), is(variantId0));
}

@Test
public void testDuplicates() throws CoreException {
IProjectDescription desc = project.getDescription();
desc.setBuildConfigs(new String[] {variantId0, variantId1, variantId0});
project.setDescription(desc, createTestMonitor());
assertThat(project.getBuildConfigs(), arrayContaining(variant0, variant1));
}

@Test
public void testDefaultVariant() throws CoreException {
IProjectDescription desc = project.getDescription();
desc.setBuildConfigs(new String[] {});
Expand All @@ -107,6 +115,7 @@ public void testDefaultVariant() throws CoreException {
assertThat(project.getActiveBuildConfig(), is(defaultVariant));
}

@Test
public void testRemoveActiveVariant() throws CoreException {
IProjectDescription desc = project.getDescription();
desc.setBuildConfigs(new String[0]);
Expand All @@ -127,6 +136,7 @@ public void testRemoveActiveVariant() throws CoreException {
/**
* Tests that build configuration references are correct after moving a project
*/
@Test
public void testProjectMove() throws CoreException {
IProjectDescription desc = project.getDescription();
IBuildConfiguration[] configs = new IBuildConfiguration[] {variant0, variant1};
Expand All @@ -148,4 +158,5 @@ public void testProjectMove() throws CoreException {
assertThat("unexpected project name at index " + i, newConfigs[i].getName(), is(configs[i].getName()));
}
}

}
Loading

0 comments on commit 4709fe2

Please sign in to comment.