Skip to content

Commit 1016b36

Browse files
committed
Migrate and restructure resources watson tests to JUnit 5 #903
Migrates the tests in org.eclipse.core.tests.internal.watson to JUnit 5. - Exchange JUnit 4 test annotations - Replace JUnit 4 assertions with JUnit 5 or AssertJ assertions - Restructure the tests: replace misused inheritance with helper class, replace polymorphic with imperative behavior specification, and parameterize tests Contributes to #903
1 parent 007ee10 commit 1016b36

File tree

10 files changed

+400
-557
lines changed

10 files changed

+400
-557
lines changed

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/DeltaChainFlatteningTest.java

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,65 +13,36 @@
1313
*******************************************************************************/
1414
package org.eclipse.core.tests.internal.watson;
1515

16-
import java.io.*;
17-
import org.eclipse.core.internal.watson.*;
18-
import org.eclipse.core.runtime.IPath;
19-
import org.junit.Before;
20-
import org.junit.Test;
21-
22-
public class DeltaChainFlatteningTest extends ElementTreeSerializationTest {
23-
protected ElementTree[] fDeltaChain;
24-
protected ElementTree[] fRefriedDeltaChain;
16+
import static org.eclipse.core.tests.internal.watson.ElementTreeSerializationTestHelper.doPipeTest;
2517

26-
/**
27-
* doRead method comment.
28-
*/
29-
@Override
30-
public Object doRead(ElementTreeReader reader, DataInputStream input) throws IOException {
31-
return reader.readDeltaChain(input);
32-
}
18+
import java.io.IOException;
19+
import org.eclipse.core.internal.watson.DefaultElementComparator;
20+
import org.eclipse.core.internal.watson.ElementTree;
21+
import org.eclipse.core.internal.watson.ElementTreeReader;
22+
import org.eclipse.core.runtime.IPath;
23+
import org.eclipse.core.tests.internal.watson.ElementTreeSerializationTestHelper.StreamReader;
24+
import org.eclipse.core.tests.internal.watson.ElementTreeSerializationTestHelper.StreamWriter;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.ArgumentsSource;
3327

28+
public class DeltaChainFlatteningTest implements IPathConstants {
3429
/**
35-
* Runs a test for this class at a certain depth and path
30+
* Tests the reading and writing of element deltas
3631
*/
37-
@Override
38-
public void doTest(IPath path, int depth) {
39-
fSubtreePath = path;
40-
fDepth = depth;
41-
fDeltaChain = TestUtil.doRoutineOperations(fTree, project1);
32+
@ParameterizedTest
33+
@ArgumentsSource(ElementTreeSerializationTestHelper.class)
34+
public void test0(IPath path, int depth) throws IOException {
35+
ElementTree tree = TestUtil.createTestElementTree();
36+
ElementTree[] fDeltaChain = TestUtil.doRoutineOperations(tree, project1);
4237
TestUtil.scramble(fDeltaChain);
4338

44-
ElementTree[] refried = (ElementTree[]) doPipeTest();
39+
StreamWriter streamWriter = (writer, output) -> writer.writeDeltaChain(fDeltaChain, path, depth, output,
40+
DefaultElementComparator.getComparator());
41+
StreamReader streamReader = ElementTreeReader::readDeltaChain;
42+
ElementTree[] refried = (ElementTree[]) doPipeTest(streamWriter, streamReader);
43+
4544
for (int j = 0; j < refried.length; j++) {
46-
TestUtil.assertEqualTrees("Same after delta chain serialize", fDeltaChain[j], refried[j], fSubtreePath, fDepth);
45+
TestUtil.assertEqualTrees("Same after delta chain serialize", fDeltaChain[j], refried[j], path, depth);
4746
}
4847
}
49-
50-
/**
51-
* doWrite method comment.
52-
*/
53-
@Override
54-
public void doWrite(ElementTreeWriter writer, DataOutputStream output) throws IOException {
55-
writer.writeDeltaChain(fDeltaChain, fSubtreePath, fDepth, output, DefaultElementComparator.getComparator());
56-
}
57-
58-
/**
59-
* Sets up the delta chain to be serialized
60-
*/
61-
@Override
62-
@Before
63-
public void setUp() throws Exception {
64-
super.setUp();
65-
fTree = TestUtil.createTestElementTree();
66-
/* do a bunch of operations on fTree to build a delta chain */
67-
fDeltaChain = TestUtil.doManyRoutineOperations(fTree, project1);
68-
}
69-
70-
/**
71-
* Tests the reading and writing of element deltas
72-
*/
73-
@Test
74-
public void test0() {
75-
doExhaustiveTests();
76-
}
7748
}

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/DeltaFlatteningTest.java

Lines changed: 44 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,22 @@
1313
*******************************************************************************/
1414
package org.eclipse.core.tests.internal.watson;
1515

16-
import java.io.*;
17-
import org.eclipse.core.internal.watson.*;
18-
import org.eclipse.core.runtime.IPath;
19-
import org.junit.Before;
20-
import org.junit.Test;
21-
22-
public class DeltaFlatteningTest extends ElementTreeSerializationTest {
23-
protected ElementTree fNewTree;
24-
protected IPath project3, folder5, file4, file5;
25-
26-
/**
27-
* Performs the serialization activity for this test
28-
*/
29-
@Override
30-
public Object doRead(ElementTreeReader reader, DataInputStream input) throws IOException {
31-
return reader.readDelta(fNewTree, input);
32-
}
33-
34-
/**
35-
* Runs a test for this class at a certain depth and path
36-
*/
37-
@Override
38-
public void doTest(IPath path, int depth) {
16+
import static org.eclipse.core.tests.internal.watson.ElementTreeSerializationTestHelper.doPipeTest;
3917

40-
/* Get an element tree from somewhere. */
41-
fTree = TestUtil.createTestElementTree();
42-
fSubtreePath = path;
43-
fDepth = depth;
44-
ElementTree newTree = (ElementTree) doPipeTest();
45-
TestUtil.assertEqualTrees(this.getClass() + "test0", fTree, newTree, fSubtreePath, fDepth);
46-
}
18+
import java.io.IOException;
19+
import org.eclipse.core.internal.watson.DefaultElementComparator;
20+
import org.eclipse.core.internal.watson.ElementTree;
21+
import org.eclipse.core.internal.watson.ElementTreeWriter;
22+
import org.eclipse.core.runtime.IPath;
23+
import org.eclipse.core.tests.internal.watson.ElementTreeSerializationTestHelper.StreamReader;
24+
import org.eclipse.core.tests.internal.watson.ElementTreeSerializationTestHelper.StreamWriter;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.ArgumentsSource;
4727

48-
/**
49-
* Performs the serialization activity for this test
50-
*/
51-
@Override
52-
public void doWrite(ElementTreeWriter writer, DataOutputStream output) throws IOException {
53-
writer.writeDelta(fTree, fNewTree, fSubtreePath, fDepth, output, DefaultElementComparator.getComparator());
54-
}
28+
public class DeltaFlatteningTest implements IPathConstants {
5529

56-
@Override
57-
@Before
58-
public void setUp() throws Exception {
59-
super.setUp();
60-
fTree = TestUtil.createTestElementTree();
30+
private ElementTree prepareTreeForChange() {
31+
ElementTree tree = TestUtil.createTestElementTree();
6132
/**
6233
* The following changes will be made to the base tree:
6334
* - add project3
@@ -69,32 +40,45 @@ public void setUp() throws Exception {
6940
* - delete folder3
7041
*/
7142

72-
fNewTree = fTree.newEmptyDelta();
43+
ElementTree newTree = tree.newEmptyDelta();
7344

74-
project3 = solution.append("project3");
75-
folder5 = project3.append("folder5");
76-
file4 = project2.append("file4");
77-
file5 = folder1.append("file5");
45+
IPath project3 = solution.append("project3");
46+
IPath folder5 = project3.append("folder5");
47+
IPath file4 = project2.append("file4");
48+
IPath file5 = folder1.append("file5");
7849

79-
fNewTree.createElement(project3, "project3");
80-
fNewTree.createElement(folder5, "folder5");
81-
fNewTree.deleteElement(file1);
82-
fNewTree.createElement(folder2, "ChangedData");
83-
fNewTree.createElement(file4, "file4");
84-
fNewTree.createElement(file5, "file5");
85-
fNewTree.deleteElement(folder3);
86-
fNewTree.immutable();
50+
newTree.createElement(project3, "project3");
51+
newTree.createElement(folder5, "folder5");
52+
newTree.deleteElement(file1);
53+
newTree.createElement(folder2, "ChangedData");
54+
newTree.createElement(file4, "file4");
55+
newTree.createElement(file5, "file5");
56+
newTree.deleteElement(folder3);
57+
newTree.immutable();
8758

8859
/* assert the new structure */
89-
TestUtil.assertHasPaths(fNewTree, new IPath[] {solution, project1, project2, project3, file2, file4, file5, folder1, folder2, folder4, folder5});
90-
TestUtil.assertNoPaths(fNewTree, new IPath[] {file1, file3, folder3});
60+
TestUtil.assertHasPaths(newTree, new IPath[] { solution, project1, project2, project3, file2, file4, file5,
61+
folder1, folder2, folder4, folder5 });
62+
TestUtil.assertNoPaths(newTree, new IPath[] { file1, file3, folder3 });
63+
64+
return newTree;
9165
}
9266

9367
/**
9468
* Tests the reading and writing of element deltas
9569
*/
96-
@Test
97-
public void test0() {
98-
doExhaustiveTests();
70+
@ParameterizedTest
71+
@ArgumentsSource(ElementTreeSerializationTestHelper.class)
72+
public void test0(IPath path, int depth) throws IOException {
73+
ElementTree tree = TestUtil.createTestElementTree();
74+
IPath testTreeRootPath = solution;
75+
ElementTree treeForChange = prepareTreeForChange();
76+
77+
StreamReader streamReader = (reader, input) -> reader.readDelta(treeForChange, input);
78+
StreamWriter streamWriter = (writer, output) -> writer.writeDelta(tree, treeForChange, testTreeRootPath,
79+
ElementTreeWriter.D_INFINITE, output, DefaultElementComparator.getComparator());
80+
ElementTree newTree = (ElementTree) doPipeTest(streamWriter, streamReader);
81+
82+
TestUtil.assertEqualTrees(this.getClass() + "test0", tree, newTree, path, depth);
9983
}
10084
}

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeDeltaChainTest.java

Lines changed: 23 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,30 @@
1313
*******************************************************************************/
1414
package org.eclipse.core.tests.internal.watson;
1515

16-
import static org.junit.Assert.assertFalse;
17-
import static org.junit.Assert.assertNotEquals;
18-
import static org.junit.Assert.assertTrue;
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertFalse;
18+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
19+
import static org.junit.jupiter.api.Assertions.assertThrows;
1920

2021
import org.eclipse.core.internal.watson.ElementTree;
2122
import org.eclipse.core.runtime.IPath;
22-
import org.junit.Before;
23-
import org.junit.Test;
23+
import org.junit.jupiter.api.Test;
2424

2525
/**
2626
* Tests the ElementTree.mergeDeltaChain() method.
2727
*/
2828
public class ElementTreeDeltaChainTest implements IPathConstants {
29-
protected ElementTree fTree;
30-
protected IPath project3;
31-
32-
@Before
33-
public void setUp() throws Exception {
34-
fTree = TestUtil.createTestElementTree();
35-
project3 = solution.append("project3");
36-
}
29+
private static final IPath project3 = solution.append("project3");
3730

3831
/**
3932
* Tries some bogus merges and makes sure an exception is thrown.
4033
*/
4134
@Test
4235
public void testIllegalMerges() {
43-
fTree = fTree.newEmptyDelta();
36+
ElementTree tree = TestUtil.createTestElementTree().newEmptyDelta();
4437

4538
/* null trees */
46-
boolean caught = false;
47-
Exception ex = null;
48-
try {
49-
fTree.mergeDeltaChain(solution, null);
50-
} catch (RuntimeException e) {
51-
caught = true;
52-
ex = e;
53-
} finally {
54-
assertTrue("", caught);
55-
}
39+
assertThrows(RuntimeException.class, () -> tree.mergeDeltaChain(solution, null));
5640

5741
/* create a tree with a whole bunch of operations in project3 */
5842
ElementTree projectTree = new ElementTree();
@@ -64,45 +48,24 @@ public void testIllegalMerges() {
6448
TestUtil.scramble(trees);
6549

6650
/* null handle */
67-
caught = false;
68-
try {
69-
fTree.mergeDeltaChain(null, trees);
70-
} catch (RuntimeException e) {
71-
caught = true;
72-
ex = e;
73-
} finally {
74-
assertTrue(ex.getMessage(), caught);
75-
}
51+
assertThrows(RuntimeException.class, () -> tree.mergeDeltaChain(null, trees));
7652

7753
/* non-existent handle */
78-
caught = false;
79-
try {
80-
fTree.mergeDeltaChain(solution.append("bogosity"), trees);
81-
} catch (RuntimeException e) {
82-
caught = true;
83-
ex = e;
84-
} finally {
85-
assertTrue(ex.getMessage(), caught);
86-
}
54+
assertThrows(RuntimeException.class, () -> tree.mergeDeltaChain(solution.append("bogosity"), trees));
8755

8856
/* immutable receiver */
89-
caught = false;
90-
try {
91-
fTree.immutable();
92-
fTree.mergeDeltaChain(solution, trees);
93-
} catch (RuntimeException e) {
94-
caught = true;
95-
ex = e;
96-
} finally {
97-
assertTrue(ex.getMessage(), caught);
98-
}
57+
assertThrows(RuntimeException.class, () -> {
58+
tree.immutable();
59+
tree.mergeDeltaChain(solution, trees);
60+
});
9961
}
10062

10163
/**
10264
* Tests the mergeDeltaChain method
10365
*/
10466
@Test
10567
public void testMergeDeltaChain() {
68+
ElementTree tree = TestUtil.createTestElementTree();
10669
/* create a tree with a whole bunch of operations in project3 */
10770
ElementTree projectTree = new ElementTree();
10871
projectTree.createElement(solution, "Dummy");
@@ -119,13 +82,13 @@ public void testMergeDeltaChain() {
11982
TestUtil.scramble(trees, copies);
12083

12184
/* do a bunch of operations on fTree to build a delta chain */
122-
TestUtil.doRoutineOperations(fTree, solution);
123-
fTree = fTree.newEmptyDelta();
85+
TestUtil.doRoutineOperations(tree, solution);
86+
tree = tree.newEmptyDelta();
12487

12588
/* merge the delta chains */
126-
ElementTree newTree = fTree.mergeDeltaChain(project3, trees);
127-
assertNotEquals("returned tree should be different", newTree, fTree);
128-
assertFalse("returned tree should be open", newTree.isImmutable());
89+
ElementTree newTree = tree.mergeDeltaChain(project3, trees);
90+
assertNotEquals(newTree, tree);
91+
assertFalse(newTree.isImmutable());
12992

13093
/* make sure old and new trees have same structure */
13194
for (int i = 0; i < trees.length; i++) {
@@ -142,13 +105,14 @@ public void testMergeDeltaChain() {
142105
*/
143106
@Test
144107
public void testMergeOverwrite() {
108+
ElementTree tree = TestUtil.createTestElementTree();
145109
/* create a tree with a whole bunch of operations in project3 */
146110
ElementTree projectTree = new ElementTree();
147111
projectTree.createElement(solution, "Dummy");
148112
projectTree.createElement(project3, "project3");
149113

150114
/* form a delta chain on fTree */
151-
ElementTree[] trees = TestUtil.doManyRoutineOperations(fTree, solution);
115+
ElementTree[] trees = TestUtil.doManyRoutineOperations(tree, solution);
152116

153117
/* scramble the order of the project trees */
154118
TestUtil.scramble(trees);
@@ -157,7 +121,7 @@ public void testMergeOverwrite() {
157121
ElementTree newTree = projectTree.mergeDeltaChain(solution, trees);
158122

159123
assertNotEquals(newTree, projectTree);
160-
assertTrue(newTree.getElementData(solution).equals("solution"));
124+
assertEquals("solution", newTree.getElementData(solution));
161125
TestUtil.assertTreeStructure(newTree);
162126
}
163127
}

0 commit comments

Comments
 (0)