Skip to content

Commit

Permalink
Migrate and restructure resources watson tests to JUnit 5 eclipse-pla…
Browse files Browse the repository at this point in the history
…tform#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
eclipse-platform#903
  • Loading branch information
HeikoKlare committed Sep 15, 2024
1 parent 133dfb3 commit 8ab816f
Show file tree
Hide file tree
Showing 10 changed files with 400 additions and 557 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,65 +13,36 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.watson;

import java.io.*;
import org.eclipse.core.internal.watson.*;
import org.eclipse.core.runtime.IPath;
import org.junit.Before;
import org.junit.Test;

public class DeltaChainFlatteningTest extends ElementTreeSerializationTest {
protected ElementTree[] fDeltaChain;
protected ElementTree[] fRefriedDeltaChain;
import static org.eclipse.core.tests.internal.watson.ElementTreeSerializationTestHelper.doPipeTest;

/**
* doRead method comment.
*/
@Override
public Object doRead(ElementTreeReader reader, DataInputStream input) throws IOException {
return reader.readDeltaChain(input);
}
import java.io.IOException;
import org.eclipse.core.internal.watson.DefaultElementComparator;
import org.eclipse.core.internal.watson.ElementTree;
import org.eclipse.core.internal.watson.ElementTreeReader;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.tests.internal.watson.ElementTreeSerializationTestHelper.StreamReader;
import org.eclipse.core.tests.internal.watson.ElementTreeSerializationTestHelper.StreamWriter;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;

public class DeltaChainFlatteningTest implements IPathConstants {
/**
* Runs a test for this class at a certain depth and path
* Tests the reading and writing of element deltas
*/
@Override
public void doTest(IPath path, int depth) {
fSubtreePath = path;
fDepth = depth;
fDeltaChain = TestUtil.doRoutineOperations(fTree, project1);
@ParameterizedTest
@ArgumentsSource(ElementTreeSerializationTestHelper.class)
public void test0(IPath path, int depth) throws IOException {
ElementTree tree = TestUtil.createTestElementTree();
ElementTree[] fDeltaChain = TestUtil.doRoutineOperations(tree, project1);
TestUtil.scramble(fDeltaChain);

ElementTree[] refried = (ElementTree[]) doPipeTest();
StreamWriter streamWriter = (writer, output) -> writer.writeDeltaChain(fDeltaChain, path, depth, output,
DefaultElementComparator.getComparator());
StreamReader streamReader = ElementTreeReader::readDeltaChain;
ElementTree[] refried = (ElementTree[]) doPipeTest(streamWriter, streamReader);

for (int j = 0; j < refried.length; j++) {
TestUtil.assertEqualTrees("Same after delta chain serialize", fDeltaChain[j], refried[j], fSubtreePath, fDepth);
TestUtil.assertEqualTrees("Same after delta chain serialize", fDeltaChain[j], refried[j], path, depth);
}
}

/**
* doWrite method comment.
*/
@Override
public void doWrite(ElementTreeWriter writer, DataOutputStream output) throws IOException {
writer.writeDeltaChain(fDeltaChain, fSubtreePath, fDepth, output, DefaultElementComparator.getComparator());
}

/**
* Sets up the delta chain to be serialized
*/
@Override
@Before
public void setUp() throws Exception {
super.setUp();
fTree = TestUtil.createTestElementTree();
/* do a bunch of operations on fTree to build a delta chain */
fDeltaChain = TestUtil.doManyRoutineOperations(fTree, project1);
}

/**
* Tests the reading and writing of element deltas
*/
@Test
public void test0() {
doExhaustiveTests();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,22 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.watson;

import java.io.*;
import org.eclipse.core.internal.watson.*;
import org.eclipse.core.runtime.IPath;
import org.junit.Before;
import org.junit.Test;

public class DeltaFlatteningTest extends ElementTreeSerializationTest {
protected ElementTree fNewTree;
protected IPath project3, folder5, file4, file5;

/**
* Performs the serialization activity for this test
*/
@Override
public Object doRead(ElementTreeReader reader, DataInputStream input) throws IOException {
return reader.readDelta(fNewTree, input);
}

/**
* Runs a test for this class at a certain depth and path
*/
@Override
public void doTest(IPath path, int depth) {
import static org.eclipse.core.tests.internal.watson.ElementTreeSerializationTestHelper.doPipeTest;

/* Get an element tree from somewhere. */
fTree = TestUtil.createTestElementTree();
fSubtreePath = path;
fDepth = depth;
ElementTree newTree = (ElementTree) doPipeTest();
TestUtil.assertEqualTrees(this.getClass() + "test0", fTree, newTree, fSubtreePath, fDepth);
}
import java.io.IOException;
import org.eclipse.core.internal.watson.DefaultElementComparator;
import org.eclipse.core.internal.watson.ElementTree;
import org.eclipse.core.internal.watson.ElementTreeWriter;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.tests.internal.watson.ElementTreeSerializationTestHelper.StreamReader;
import org.eclipse.core.tests.internal.watson.ElementTreeSerializationTestHelper.StreamWriter;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;

/**
* Performs the serialization activity for this test
*/
@Override
public void doWrite(ElementTreeWriter writer, DataOutputStream output) throws IOException {
writer.writeDelta(fTree, fNewTree, fSubtreePath, fDepth, output, DefaultElementComparator.getComparator());
}
public class DeltaFlatteningTest implements IPathConstants {

@Override
@Before
public void setUp() throws Exception {
super.setUp();
fTree = TestUtil.createTestElementTree();
private ElementTree prepareTreeForChange() {
ElementTree tree = TestUtil.createTestElementTree();
/**
* The following changes will be made to the base tree:
* - add project3
Expand All @@ -69,32 +40,45 @@ public void setUp() throws Exception {
* - delete folder3
*/

fNewTree = fTree.newEmptyDelta();
ElementTree newTree = tree.newEmptyDelta();

project3 = solution.append("project3");
folder5 = project3.append("folder5");
file4 = project2.append("file4");
file5 = folder1.append("file5");
IPath project3 = solution.append("project3");
IPath folder5 = project3.append("folder5");
IPath file4 = project2.append("file4");
IPath file5 = folder1.append("file5");

fNewTree.createElement(project3, "project3");
fNewTree.createElement(folder5, "folder5");
fNewTree.deleteElement(file1);
fNewTree.createElement(folder2, "ChangedData");
fNewTree.createElement(file4, "file4");
fNewTree.createElement(file5, "file5");
fNewTree.deleteElement(folder3);
fNewTree.immutable();
newTree.createElement(project3, "project3");
newTree.createElement(folder5, "folder5");
newTree.deleteElement(file1);
newTree.createElement(folder2, "ChangedData");
newTree.createElement(file4, "file4");
newTree.createElement(file5, "file5");
newTree.deleteElement(folder3);
newTree.immutable();

/* assert the new structure */
TestUtil.assertHasPaths(fNewTree, new IPath[] {solution, project1, project2, project3, file2, file4, file5, folder1, folder2, folder4, folder5});
TestUtil.assertNoPaths(fNewTree, new IPath[] {file1, file3, folder3});
TestUtil.assertHasPaths(newTree, new IPath[] { solution, project1, project2, project3, file2, file4, file5,
folder1, folder2, folder4, folder5 });
TestUtil.assertNoPaths(newTree, new IPath[] { file1, file3, folder3 });

return newTree;
}

/**
* Tests the reading and writing of element deltas
*/
@Test
public void test0() {
doExhaustiveTests();
@ParameterizedTest
@ArgumentsSource(ElementTreeSerializationTestHelper.class)
public void test0(IPath path, int depth) throws IOException {
ElementTree tree = TestUtil.createTestElementTree();
IPath testTreeRootPath = solution;
ElementTree treeForChange = prepareTreeForChange();

StreamReader streamReader = (reader, input) -> reader.readDelta(treeForChange, input);
StreamWriter streamWriter = (writer, output) -> writer.writeDelta(tree, treeForChange, testTreeRootPath,
ElementTreeWriter.D_INFINITE, output, DefaultElementComparator.getComparator());
ElementTree newTree = (ElementTree) doPipeTest(streamWriter, streamReader);

TestUtil.assertEqualTrees(this.getClass() + "test0", tree, newTree, path, depth);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,30 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.watson;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.eclipse.core.internal.watson.ElementTree;
import org.eclipse.core.runtime.IPath;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Test;

/**
* Tests the ElementTree.mergeDeltaChain() method.
*/
public class ElementTreeDeltaChainTest implements IPathConstants {
protected ElementTree fTree;
protected IPath project3;

@Before
public void setUp() throws Exception {
fTree = TestUtil.createTestElementTree();
project3 = solution.append("project3");
}
private static final IPath project3 = solution.append("project3");

/**
* Tries some bogus merges and makes sure an exception is thrown.
*/
@Test
public void testIllegalMerges() {
fTree = fTree.newEmptyDelta();
ElementTree tree = TestUtil.createTestElementTree().newEmptyDelta();

/* null trees */
boolean caught = false;
Exception ex = null;
try {
fTree.mergeDeltaChain(solution, null);
} catch (RuntimeException e) {
caught = true;
ex = e;
} finally {
assertTrue("", caught);
}
assertThrows(RuntimeException.class, () -> tree.mergeDeltaChain(solution, null));

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

/* null handle */
caught = false;
try {
fTree.mergeDeltaChain(null, trees);
} catch (RuntimeException e) {
caught = true;
ex = e;
} finally {
assertTrue(ex.getMessage(), caught);
}
assertThrows(RuntimeException.class, () -> tree.mergeDeltaChain(null, trees));

/* non-existent handle */
caught = false;
try {
fTree.mergeDeltaChain(solution.append("bogosity"), trees);
} catch (RuntimeException e) {
caught = true;
ex = e;
} finally {
assertTrue(ex.getMessage(), caught);
}
assertThrows(RuntimeException.class, () -> tree.mergeDeltaChain(solution.append("bogosity"), trees));

/* immutable receiver */
caught = false;
try {
fTree.immutable();
fTree.mergeDeltaChain(solution, trees);
} catch (RuntimeException e) {
caught = true;
ex = e;
} finally {
assertTrue(ex.getMessage(), caught);
}
assertThrows(RuntimeException.class, () -> {
tree.immutable();
tree.mergeDeltaChain(solution, trees);
});
}

/**
* Tests the mergeDeltaChain method
*/
@Test
public void testMergeDeltaChain() {
ElementTree tree = TestUtil.createTestElementTree();
/* create a tree with a whole bunch of operations in project3 */
ElementTree projectTree = new ElementTree();
projectTree.createElement(solution, "Dummy");
Expand All @@ -119,13 +82,13 @@ public void testMergeDeltaChain() {
TestUtil.scramble(trees, copies);

/* do a bunch of operations on fTree to build a delta chain */
TestUtil.doRoutineOperations(fTree, solution);
fTree = fTree.newEmptyDelta();
TestUtil.doRoutineOperations(tree, solution);
tree = tree.newEmptyDelta();

/* merge the delta chains */
ElementTree newTree = fTree.mergeDeltaChain(project3, trees);
assertNotEquals("returned tree should be different", newTree, fTree);
assertFalse("returned tree should be open", newTree.isImmutable());
ElementTree newTree = tree.mergeDeltaChain(project3, trees);
assertNotEquals(newTree, tree);
assertFalse(newTree.isImmutable());

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

/* form a delta chain on fTree */
ElementTree[] trees = TestUtil.doManyRoutineOperations(fTree, solution);
ElementTree[] trees = TestUtil.doManyRoutineOperations(tree, solution);

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

assertNotEquals(newTree, projectTree);
assertTrue(newTree.getElementData(solution).equals("solution"));
assertEquals("solution", newTree.getElementData(solution));
TestUtil.assertTreeStructure(newTree);
}
}
Loading

0 comments on commit 8ab816f

Please sign in to comment.