From 3bd68b72c29f993f8c41a195af45f5b99f1e9ae7 Mon Sep 17 00:00:00 2001 From: Heiko Klare Date: Tue, 12 Dec 2023 11:57:02 +0100 Subject: [PATCH] Migrate all tests in o.e.team.tests.ui.* to JUnit 4 #903 * Replace the ResourceTest hierarchy with WorkspaceTestRule * Add @Test annotations * Move assertions from asynchronous listener to main-thread test logic Contributes to https://github.com/eclipse-platform/eclipse.platform/issues/903 --- .../ui/SaveableCompareEditorInputTest.java | 67 ++++++++++++------- .../ui/synchronize/ResourceContentTests.java | 62 +++++++---------- 2 files changed, 67 insertions(+), 62 deletions(-) diff --git a/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/SaveableCompareEditorInputTest.java b/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/SaveableCompareEditorInputTest.java index c292cef1d2c..3a4c6377a74 100644 --- a/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/SaveableCompareEditorInputTest.java +++ b/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/SaveableCompareEditorInputTest.java @@ -13,14 +13,22 @@ *******************************************************************************/ package org.eclipse.team.tests.ui; +import static java.util.Collections.synchronizedList; +import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace; +import static org.eclipse.core.tests.resources.ResourceTestUtil.compareContent; +import static org.eclipse.core.tests.resources.ResourceTestUtil.createInWorkspace; +import static org.eclipse.core.tests.resources.ResourceTestUtil.createInputStream; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.empty; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import java.io.ByteArrayInputStream; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; -import junit.framework.Test; - import org.eclipse.compare.CompareConfiguration; import org.eclipse.compare.CompareViewerSwitchingPane; import org.eclipse.compare.ITypedElement; @@ -39,6 +47,7 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; +import org.eclipse.core.tests.resources.WorkspaceTestRule; import org.eclipse.jface.viewers.Viewer; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.graphics.Image; @@ -47,16 +56,17 @@ import org.eclipse.team.internal.ui.mapping.CompareInputChangeNotifier; import org.eclipse.team.internal.ui.synchronize.LocalResourceTypedElement; import org.eclipse.team.internal.ui.synchronize.SaveablesCompareEditorInput; -import org.eclipse.team.tests.core.TeamTest; import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput; import org.eclipse.ui.PlatformUI; -import static org.eclipse.core.tests.resources.ResourceTestUtil.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; -public class SaveableCompareEditorInputTest extends TeamTest { +public class SaveableCompareEditorInputTest { - public static Test suite() { - return suite(SaveableCompareEditorInputTest.class); - } + @Rule + public WorkspaceTestRule workspaceRule = new WorkspaceTestRule(); private static final String COMPARE_EDITOR = CompareUIPlugin.PLUGIN_ID + ".CompareEditor"; //$NON-NLS-1$ @@ -67,30 +77,28 @@ public static Test suite() { private final String fileContents1 = "FileContents"; private final String fileContents2 = "FileContents2"; private final TestLogListener logListener = new TestLogListener(); + private List errorsInListener = synchronizedList(new ArrayList<>()); private IProject project; - @Override - protected void setUp() throws Exception { - super.setUp(); - - project = createProject("Project_", new String[] { - "File1.txt", "File2.txt" }); - + @Before + public void setUp() throws Exception { + project = getWorkspace().getRoot().getProject("Project"); + createInWorkspace(project); file1 = project.getFile("File1.txt"); file2 = project.getFile("File2.txt"); - file1.setContents(new ByteArrayInputStream(fileContents1.getBytes()), - true, true, null); - file2.setContents(new ByteArrayInputStream(fileContents2.getBytes()), - true, true, null); + createInWorkspace(file1); + createInWorkspace(file2); + file1.setContents(createInputStream(fileContents1), true, true, null); + file2.setContents(createInputStream(fileContents2), true, true, null); Platform.addLogListener(logListener); } - @Override - protected void tearDown() throws Exception { + @After + public void tearDown() throws Exception { // remove log listener Platform.removeLogListener(logListener); - super.tearDown(); + assertThat("Unexpected errors in log listener", errorsInListener, empty()); } private class TestFileElement implements ITypedElement { @@ -125,8 +133,9 @@ public String getType() { private class TestLogListener implements ILogListener { @Override public void logging(IStatus status, String plugin) { - if (status.getSeverity() == IStatus.ERROR) - fail(status.toString()); + if (status.getSeverity() == IStatus.ERROR) { + errorsInListener.add(status); + } } } @@ -249,6 +258,7 @@ private void verifyDirtyStateChanges( assertFalse(compareEditorInput.isDirty()); } + @Test public void testDirtyFlagOnLocalResourceTypedElement() throws CoreException, InvocationTargetException, InterruptedException, IllegalArgumentException, SecurityException, @@ -277,6 +287,7 @@ public void testDirtyFlagOnLocalResourceTypedElement() file1.getContents())); } + @Test public void testDirtyFlagOnCustomTypedElement() throws CoreException, InvocationTargetException, InterruptedException, IllegalArgumentException, SecurityException, @@ -301,6 +312,7 @@ public void testDirtyFlagOnCustomTypedElement() throws CoreException, */ } + @Test public void testDirtyFlagOnLocalResourceTypedElementAndEmptyRight() throws CoreException, InvocationTargetException, InterruptedException, IllegalArgumentException, SecurityException, @@ -329,6 +341,7 @@ public void testDirtyFlagOnLocalResourceTypedElementAndEmptyRight() file1.getContents())); } + @Test public void testDirtyFlagOnCustomTypedElementAndEmptyRight() throws CoreException, InvocationTargetException, InterruptedException, IllegalArgumentException, SecurityException, @@ -412,23 +425,29 @@ private void verifyModifyAndSaveBothSidesOfCompareEditor(String extention) file2.getContents())); } + @Test public void testModifyAndSaveBothSidesOfCompareEditorHtml() throws Exception { verifyModifyAndSaveBothSidesOfCompareEditor("html"); } + @Test public void testModifyAndSaveBothSidesOfCompareEditorTxt() throws Exception { verifyModifyAndSaveBothSidesOfCompareEditor("txt"); } + @Test public void testModifyAndSaveBothSidesOfCompareEditorJava() throws Exception { verifyModifyAndSaveBothSidesOfCompareEditor("java"); } + @Test public void testModifyAndSaveBothSidesOfCompareEditorXml() throws Exception { verifyModifyAndSaveBothSidesOfCompareEditor("xml"); } + @Test public void testModifyAndSaveBothSidesOfCompareEditorProperties() throws Exception { verifyModifyAndSaveBothSidesOfCompareEditor("properties"); } + } diff --git a/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/synchronize/ResourceContentTests.java b/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/synchronize/ResourceContentTests.java index aa74bb8e56e..79015265dac 100644 --- a/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/synchronize/ResourceContentTests.java +++ b/team/tests/org.eclipse.team.tests.core/src/org/eclipse/team/tests/ui/synchronize/ResourceContentTests.java @@ -13,6 +13,11 @@ *******************************************************************************/ package org.eclipse.team.tests.ui.synchronize; +import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace; +import static org.eclipse.core.tests.resources.ResourceTestUtil.buildResources; +import static org.eclipse.core.tests.resources.ResourceTestUtil.createInWorkspace; +import static org.junit.Assert.assertFalse; + import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -20,20 +25,25 @@ import java.util.List; import java.util.Set; -import junit.framework.Test; - import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.tests.resources.WorkspaceTestRule; import org.eclipse.jface.viewers.TreePath; import org.eclipse.team.core.mapping.ISynchronizationContext; import org.eclipse.team.core.mapping.ISynchronizationScope; import org.eclipse.team.internal.ui.mapping.ResourceModelContentProvider; -import org.eclipse.team.tests.core.TeamTest; import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; -public class ResourceContentTests extends TeamTest { +public class ResourceContentTests { + + @Rule + public WorkspaceTestRule workspaceRule = new WorkspaceTestRule(); public static class TestableResourceModelContentProvider extends ResourceModelContentProvider { @@ -63,30 +73,16 @@ public ISynchronizationScope getScope() { } } - public static Test suite() { - return suite(ResourceContentTests.class); - } - private ResourceModelContentProvider provider; - public ResourceContentTests() { - super(); - } - - public ResourceContentTests(String name) { - super(name); - } - - @Override - protected void setUp() throws Exception { + @Before + public void setUp() throws Exception { initializeProvider(null, null, null); - super.setUp(); } - @Override - protected void tearDown() throws Exception { + @After + public void tearDown() throws Exception { cleanupProvider(); - super.tearDown(); } private void initializeProvider(ISynchronizationScope scope, ISynchronizationContext context, ISynchronizePageConfiguration configuration) { @@ -116,12 +112,8 @@ private void assertContentsMatch(IResource[] resources) { iterator.remove(); } } - if (!resourceSet.isEmpty()) { - fail("Tree entries were missing for " + toString(resourceSet)); - } - if (!paths.isEmpty()) { - fail("Tree entries were found for " + toString(paths)); - } + assertFalse("Tree entries were missing for " + toString(resourceSet), resourceSet.isEmpty()); + assertFalse("Tree entries were found for " + toString(paths), paths.isEmpty()); } private Set getPaths(Object root) { @@ -183,19 +175,13 @@ private IResource[] asResources(IProject project, String[] resourcePaths) { return (IResource[]) resources.toArray(new IResource[resources.size()]); } + @Test public void testFileContent() throws CoreException { + IProject project = getWorkspace().getRoot().getProject("Project"); + createInWorkspace(project); String[] files = new String[] {"file.txt", "file2.txt", "folder1/file3.txt", "folder1/folder2/file4.txt"}; - IProject project = createProject(files); - files = new String[] {".project", "file.txt", "file2.txt", "folder1/file3.txt", "folder1/folder2/file4.txt"}; + buildResources(project, files); assertContentMatches(project, files); } - public void testFileChange() throws CoreException { -// String[] files = new String[] {"file.txt", "file2.txt", "folder1/file3.txt", "folder1/folder2/file4.txt"}; -// IProject project = createProject(files); - - } - - - }