Skip to content

Commit

Permalink
Migrate o.e.equinox.common.tests to JUnit 4
Browse files Browse the repository at this point in the history
* Apply JUnit 4 annotations @test, @before, @after
* Remove inheritance of TestCase and CoreTest
* Remove unnecessarily try-catch blocks by letting test method throw the
exception or by applying assertThrows()
  • Loading branch information
HeikoKlare authored and HannesWell committed Dec 21, 2023
1 parent ab23314 commit 69e4ff4
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,19 @@
*******************************************************************************/
package org.eclipse.equinox.common.tests;

import org.eclipse.core.runtime.*;
import org.eclipse.core.tests.harness.CoreTest;
import static org.junit.Assert.assertEquals;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.junit.Test;

/**
* Test cases for the Path class.
*/
public class CoreExceptionTest extends CoreTest {
/**
* Need a zero argument constructor to satisfy the test harness. This
* constructor should not do any real work nor should it be called by user code.
*/
public CoreExceptionTest() {
super(null);
}

public CoreExceptionTest(String name) {
super(name);
}
public class CoreExceptionTest {

@Test
public void testCoreException() {
final String MESSAGE_STRING = "An exception has occurred";
IStatus status = new Status(IStatus.ERROR, "org.eclipse.core.tests.runtime", 31415, MESSAGE_STRING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,17 @@
*******************************************************************************/
package org.eclipse.equinox.common.tests;

import static org.junit.Assert.assertEquals;

import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.tests.harness.CoreTest;
import org.junit.Test;

/**
* Test cases for the Path class.
*/
public class OperationCanceledExceptionTest extends CoreTest {
/**
* Need a zero argument constructor to satisfy the test harness. This
* constructor should not do any real work nor should it be called by user code.
*/
public OperationCanceledExceptionTest() {
super(null);
}

public OperationCanceledExceptionTest(String name) {
super(name);
}
public class OperationCanceledExceptionTest {

@Test
public void testCoreException() {
final String MESSAGE_STRING = "An exception has occurred";
OperationCanceledException e = new OperationCanceledException(MESSAGE_STRING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,28 @@
*******************************************************************************/
package org.eclipse.equinox.common.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.tests.harness.CoreTest;
import org.junit.Test;

/**
* Test cases for the Path class.
*/
public class PathTest extends CoreTest {
/**
* Need a zero argument constructor to satisfy the test harness. This
* constructor should not do any real work nor should it be called by user code.
*/
public PathTest() {
super(null);
}

public PathTest(String name) {
super(name);
}
public class PathTest {

@Test
public void testAddTrailingSeparator() {

IPath with = new Path("/first/second/third/");
Expand Down Expand Up @@ -73,6 +69,7 @@ public void testAddTrailingSeparator() {
assertEquals("6.1", IPath.ROOT, IPath.EMPTY.addTrailingSeparator());
}

@Test
public void testAppend() {

IPath fore = new Path("/first/second/third/");
Expand Down Expand Up @@ -237,6 +234,7 @@ public void testAppend() {
assertFalse("11.6.win", win4.isValidSegment(win4.segment(2)));
}

@Test
public void testSegmentCount() {

assertEquals("1.0", 0, IPath.ROOT.segmentCount());
Expand All @@ -255,6 +253,7 @@ public void testSegmentCount() {
assertEquals("3.4", 2, new Path("//first/second/").segmentCount());
}

@Test
public void testCanonicalize() {
// Test collapsing multiple separators
// double slashes at the beginning of a path
Expand All @@ -281,6 +280,7 @@ public void testCanonicalize() {
assertEquals("3.6", ".", new Path(".").toString());
}

@Test
public void testClone() {

IPath anyPath = new Path("/a/b/c");
Expand All @@ -293,6 +293,7 @@ public void testClone() {
assertEquals("1.3", IPath.ROOT, IPath.ROOT.clone());
}

@Test
public void testConstructors() {

assertEquals("1.0", "", new Path("").toString());
Expand All @@ -319,6 +320,7 @@ public void testConstructors() {
assertEquals("3.1.win", "D:/", IPath.forWindows("/D:/").toString());
}

@Test
public void testFactoryMethods() {

IPath win = IPath.forWindows("a:b\\c/d");
Expand All @@ -336,6 +338,7 @@ public void testFactoryMethods() {
assertNotEquals("3.1", win, posix);
}

@Test
public void testFirstSegment() {

assertNull("1.0", IPath.ROOT.segment(0));
Expand All @@ -355,6 +358,7 @@ public void testFirstSegment() {

}

@Test
public void testFromOSString() {
List<String> segments = List.of("first", "first/second/third");
for (String segment : segments) {
Expand All @@ -370,6 +374,7 @@ private static String osString(String pathname) {
return new java.io.File(pathname).toString();
}

@Test
public void testFromPortableString() {
assertEquals("1.0", "", IPath.fromPortableString("").toString());
assertEquals("1.1", "/", IPath.fromPortableString("/").toString());
Expand Down Expand Up @@ -404,6 +409,7 @@ public void testFromPortableString() {
assertEquals("3.12.posix", isLocalPosix, posix2.isValidSegment(":"));
}

@Test
public void testFromFile() {
List<String> segments = List.of("first", "first/second/third");
for (String segment : segments) {
Expand All @@ -418,6 +424,7 @@ public void testFromFile() {
}
}

@Test
public void testFromPath() {
List<String> segments = List.of("first", "first/second/third");
for (String segment : segments) {
Expand All @@ -432,6 +439,7 @@ public void testFromPath() {
}
}

@Test
public void testGetFileExtension() {

IPath anyPath = new Path("index.html");
Expand All @@ -450,6 +458,7 @@ public void testGetFileExtension() {

}

@Test
public void testHasTrailingSeparator() {

// positive
Expand Down Expand Up @@ -478,6 +487,7 @@ public void testHasTrailingSeparator() {

}

@Test
public void testIsAbsolute() {

// positive
Expand All @@ -501,6 +511,7 @@ public void testIsAbsolute() {

}

@Test
public void testIsEmpty() {

// positive
Expand All @@ -518,6 +529,7 @@ public void testIsEmpty() {
assertTrue("2.3", !new Path("c:/").isEmpty());
}

@Test
public void testIsPrefixOf() {

IPath prefix = new Path("/first/second");
Expand All @@ -536,6 +548,7 @@ public void testIsPrefixOf() {
assertTrue("3.3", !path.isPrefixOf(IPath.EMPTY));
}

@Test
public void testIsRoot() {

// negative
Expand All @@ -551,6 +564,7 @@ public void testIsRoot() {
assertTrue("2.3.posix", IPath.forPosix("/").isRoot());
}

@Test
public void testIsUNC() {

// negative
Expand Down Expand Up @@ -581,6 +595,7 @@ public void testIsUNC() {
assertTrue("6.2.win", IPath.forWindows("c:\\\\").setDevice(null).isUNC());
}

@Test
public void testIsValidPath() {
IPath test = IPath.ROOT;
// positive
Expand All @@ -607,6 +622,7 @@ public void testIsValidPath() {
assertTrue("3.4.posix", Path.isValidPosixPath("c:a/b:"));
}

@Test
public void testIsValidSegment() {
IPath test = IPath.ROOT;
// positive
Expand Down Expand Up @@ -635,6 +651,7 @@ public void testIsValidSegment() {
assertEquals("5.3", isLocalPosix, new Path("").isValidSegment(":"));
}

@Test
public void testLastSegment() {

assertEquals("1.0", "second", new Path("/first/second").lastSegment());
Expand All @@ -657,6 +674,7 @@ public void testLastSegment() {
assertEquals("5.1", "first", new Path("//first/").lastSegment());
}

@Test
public void testMakeAbsolute() {
IPath anyPath = new Path("first/second/third").makeAbsolute();
assertTrue("1.0", anyPath.isAbsolute());
Expand All @@ -667,6 +685,7 @@ public void testMakeAbsolute() {
assertEquals("2.1", IPath.ROOT, anyPath);
}

@Test
public void testMakeRelative() {
IPath anyPath = new Path("/first/second/third").makeRelative();
assertTrue("1.0", !anyPath.isAbsolute());
Expand All @@ -680,6 +699,7 @@ public void testMakeRelative() {
/**
* Tests for {@link Path#makeRelativeTo(IPath)}.
*/
@Test
public void testMakeRelativeTo() {
// valid cases
IPath[] bases = new IPath[] { new Path("/a/"), new Path("/a/b") };
Expand Down Expand Up @@ -717,6 +737,7 @@ public void testMakeRelativeTo() {
/**
* Tests for {@link Path#makeRelativeTo(IPath)}.
*/
@Test
public void testMakeRelativeToWindows() {
IPath[] bases = new IPath[] { IPath.forWindows("c:/a/"), IPath.forWindows("c:/a/b") };
IPath[] children = new IPath[] { IPath.forWindows("d:/a/"), IPath.forWindows("d:/a/b"),
Expand All @@ -733,6 +754,7 @@ public void testMakeRelativeToWindows() {

}

@Test
public void testMakeUNC() {
List<IPath> inputs = new ArrayList<>();
List<String> expected = new ArrayList<>();
Expand Down Expand Up @@ -815,6 +837,7 @@ public void testMakeUNC() {
/**
* This test is for bizarre cases that previously caused errors.
*/
@Test
public void testRegression() {
new Path("C:\\/eclipse");

Expand All @@ -824,6 +847,7 @@ public void testRegression() {
assertEquals("2.2.win", "ive", path.segment(0));
}

@Test
public void testRemoveFirstSegments() {
assertEquals("1.0", new Path("second"), new Path("/first/second").removeFirstSegments(1));
assertEquals("1.1", new Path("second/third/"), new Path("/first/second/third/").removeFirstSegments(1));
Expand Down Expand Up @@ -860,6 +884,7 @@ public void testRemoveFirstSegments() {
assertEquals("3.6", new Path("third/fourth"), new Path("//first/second/third/fourth").removeFirstSegments(2));
}

@Test
public void testRemoveLastSegments() {

assertEquals("1.0", new Path("/first"), new Path("/first/second").removeLastSegments(1));
Expand All @@ -881,6 +906,7 @@ public void testRemoveLastSegments() {
assertEquals("4.2", new Path("//"), new Path("//").removeLastSegments(1));
}

@Test
public void testRemoveTrailingSeparator() {

IPath with = new Path("/first/second/third/");
Expand Down Expand Up @@ -914,6 +940,7 @@ public void testRemoveTrailingSeparator() {
assertEquals("5.2", new Path("c:a/b"), new Path("c:a/b/").removeTrailingSeparator());
}

@Test
public void testSegments() {

IPath anyPath = null;
Expand Down Expand Up @@ -975,6 +1002,7 @@ public void testSegments() {
assertEquals("7.5", "c", segs[4]);
}

@Test
public void testToString() {

IPath anyPath = new Path("/first/second/third");
Expand All @@ -984,6 +1012,7 @@ public void testToString() {
assertEquals("1.2", "", IPath.EMPTY.toString());
}

@Test
public void testHash() {
// actual hash codes may depend on JDK implementation of String.hashCode()

Expand All @@ -1008,6 +1037,7 @@ public void testHash() {
new Path("p").addTrailingSeparator().hashCode());
}

@Test
public void testEquals() {
assertEquals("a", new Path("a"), new Path("a"));
assertEquals("b", new Path("a"), new Path("a"));
Expand All @@ -1031,6 +1061,7 @@ public void testEquals() {
assertNotEquals("leading\\ dependent", new Path("\\p"), new Path("p"));
}

@Test
public void testUptoSegment() {

// Case 1, absolute path with no trailing separator
Expand Down Expand Up @@ -1082,6 +1113,7 @@ public void testUptoSegment() {
assertEquals("5.4", new Path("//"), anyPath.uptoSegment(0));
}

@Test
public void testToPath() {

// Case 1, absolute path with no trailing separator
Expand Down
Loading

0 comments on commit 69e4ff4

Please sign in to comment.