Skip to content

Commit

Permalink
Migrate all tests in o.e.team.tests.ui.* to JUnit 4 #903
Browse files Browse the repository at this point in the history
* Replace the ResourceTest hierarchy with WorkspaceTestRule
* Add @test annotations
* Move assertions from asynchronous listener to main-thread test logic

Contributes to
#903
  • Loading branch information
HeikoKlare committed Dec 12, 2023
1 parent 182c0f7 commit 3bd68b7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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$
Expand All @@ -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<IStatus> 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 {
Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -249,6 +258,7 @@ private void verifyDirtyStateChanges(
assertFalse(compareEditorInput.isDirty());
}

@Test
public void testDirtyFlagOnLocalResourceTypedElement()
throws CoreException, InvocationTargetException,
InterruptedException, IllegalArgumentException, SecurityException,
Expand Down Expand Up @@ -277,6 +287,7 @@ public void testDirtyFlagOnLocalResourceTypedElement()
file1.getContents()));
}

@Test
public void testDirtyFlagOnCustomTypedElement() throws CoreException,
InvocationTargetException, InterruptedException,
IllegalArgumentException, SecurityException,
Expand All @@ -301,6 +312,7 @@ public void testDirtyFlagOnCustomTypedElement() throws CoreException,
*/
}

@Test
public void testDirtyFlagOnLocalResourceTypedElementAndEmptyRight()
throws CoreException, InvocationTargetException,
InterruptedException, IllegalArgumentException, SecurityException,
Expand Down Expand Up @@ -329,6 +341,7 @@ public void testDirtyFlagOnLocalResourceTypedElementAndEmptyRight()
file1.getContents()));
}

@Test
public void testDirtyFlagOnCustomTypedElementAndEmptyRight()
throws CoreException, InvocationTargetException,
InterruptedException, IllegalArgumentException, SecurityException,
Expand Down Expand Up @@ -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");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,37 @@
*******************************************************************************/
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;
import java.util.Iterator;
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 {

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

}



}

0 comments on commit 3bd68b7

Please sign in to comment.