diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/DeltaChainFlatteningTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/DeltaChainFlatteningTest.java index 4036e62b1e0..0d92ef21574 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/DeltaChainFlatteningTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/DeltaChainFlatteningTest.java @@ -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(); - } } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/DeltaFlatteningTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/DeltaFlatteningTest.java index ed3a06347c2..695c03cbfa3 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/DeltaFlatteningTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/DeltaFlatteningTest.java @@ -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 @@ -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); } } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeDeltaChainTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeDeltaChainTest.java index 54f8038600b..2112773b9a0 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeDeltaChainTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeDeltaChainTest.java @@ -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(); @@ -64,38 +48,16 @@ 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); + }); } /** @@ -103,6 +65,7 @@ public void testIllegalMerges() { */ @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"); @@ -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++) { @@ -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); @@ -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); } } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeHasChangesTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeHasChangesTest.java index b6416248445..78c93b9c687 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeHasChangesTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeHasChangesTest.java @@ -13,14 +13,14 @@ *******************************************************************************/ package org.eclipse.core.tests.internal.watson; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.eclipse.core.internal.events.ResourceComparator; import org.eclipse.core.internal.resources.ResourceInfo; import org.eclipse.core.internal.watson.ElementTree; import org.eclipse.core.runtime.IPath; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Tests for @@ -40,7 +40,7 @@ public void testNoChanges() { ElementTree newTree = newTree(oldTree); boolean hasChanges = hasChanges(oldTree, newTree); - assertFalse("expected no changes to be detected if the tree was not changed", hasChanges); + assertFalse(hasChanges, "expected no changes to be detected if the tree was not changed"); } /** @@ -54,7 +54,7 @@ public void testAddElement() { add(newTree, folder1); boolean hasChanges = hasChanges(oldTree, newTree); - assertTrue("expected changes to be detected after adding an element to the tree", hasChanges); + assertTrue(hasChanges, "expected changes to be detected after adding an element to the tree"); } /** @@ -68,7 +68,7 @@ public void testRemoveElement() { remove(newTree, file1); boolean hasChanges = hasChanges(oldTree, newTree); - assertTrue("expected changes to be detected after removing an element from the tree", hasChanges); + assertTrue(hasChanges, "expected changes to be detected after removing an element from the tree"); } /** @@ -84,8 +84,8 @@ public void testAddAndRemoveElement() { remove(newTree, folder1); boolean hasChanges = hasChanges(oldTree, newTree); - assertTrue("expected changes to be detected after adding and removing the same element to and from the tree", - hasChanges); + assertTrue(hasChanges, + "expected changes to be detected after adding and removing the same element to and from the tree"); } /** @@ -100,7 +100,7 @@ public void testChangeElementData() { newTree.setElementData(file1, "different data"); boolean hasChanges = hasChanges(oldTree, newTree); - assertTrue("expected changes to be detected after changing the data of a tree element", hasChanges); + assertTrue(hasChanges, "expected changes to be detected after changing the data of a tree element"); } /** @@ -117,7 +117,7 @@ public void testChangeTreeData() { incrementCharsetGenerationCount(newTree); boolean hasChanges = hasChanges(oldTree, newTree); - assertTrue("expected changes to be detected after changing the data of the tree", hasChanges); + assertTrue(hasChanges, "expected changes to be detected after changing the data of the tree"); } private static boolean hasChanges(ElementTree oldTree, ElementTree newTree) { diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeIteratorTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeIteratorTest.java index c21df871673..b23c256a670 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeIteratorTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeIteratorTest.java @@ -14,7 +14,7 @@ *******************************************************************************/ package org.eclipse.core.tests.internal.watson; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; import java.util.ArrayList; import java.util.Stack; @@ -23,7 +23,7 @@ import org.eclipse.core.internal.watson.IElementContentVisitor; import org.eclipse.core.internal.watson.IElementTreeData; import org.eclipse.core.runtime.IPath; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Unit tests for ElementTreeIterator. @@ -105,12 +105,12 @@ public void testContentIterator() { return true; }; new ElementTreeIterator(tree, IPath.ROOT).iterate(elementVisitor); - assertEquals("1", 2 + n + n * n + n * n * n, elts.size()); + assertThat(elts).hasSize(2 + n + n * n + n * n * n); elts.clear(); IPath innerElement = IPath.ROOT.append("sol").append("proj1"); new ElementTreeIterator(tree, innerElement).iterate(elementVisitor); - assertEquals("2", 1 + n + n * n, elts.size()); + assertThat(elts).hasSize(1 + n + n * n); } /** diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeSerializationTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeSerializationTest.java deleted file mode 100644 index 76cad4eca16..00000000000 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeSerializationTest.java +++ /dev/null @@ -1,280 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2000, 2017 IBM Corporation and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.core.tests.internal.watson; - -import static org.junit.Assert.fail; - -import java.io.*; -import org.eclipse.core.internal.watson.*; -import org.eclipse.core.runtime.IPath; -import org.junit.After; -import org.junit.Before; - -public abstract class ElementTreeSerializationTest implements IPathConstants { - - protected ElementTree fTree; - - String root = System.getProperty("java.io.tmpdir"); - protected File tempFile = new File(root + "/temp/TestFlattening"); - - class WriterThread implements Runnable { - DataOutputStream fDataOutputStream; - ElementTreeWriter fWriter; - - public void setStream(DataOutputStream stream) { - fDataOutputStream = stream; - } - - public void setWriter(ElementTreeWriter writer) { - fWriter = writer; - } - - @Override - public void run() { - try { - doWrite(fWriter, fDataOutputStream); - // inform reader, writing is finished: - fDataOutputStream.flush(); // DeltaChainFlatteningTest 30s -> 0s - } catch (IOException e) { - e.printStackTrace(); - fail("Error writing delta"); - } - } - } - - class ReaderThread implements Runnable { - DataInputStream fDataInputStream; - ElementTreeReader fReader; - Object fRefried; - - public void setStream(DataInputStream stream) { - fDataInputStream = stream; - } - - public void setReader(ElementTreeReader reader) { - fReader = reader; - } - - public Object getReconstitutedObject() { - return fRefried; - } - - @Override - public void run() { - try { - fRefried = doRead(fReader, fDataInputStream); - } catch (IOException e) { - e.printStackTrace(); - fail("Error reading delta"); - } - } - } - - WriterThread writerThread = new WriterThread(); - ReaderThread readerThread = new ReaderThread(); - - /** - * The subtree to write - */ - protected IPath fSubtreePath; - - /** - * The depth of the tree to write. - */ - protected int fDepth; - - /** - * Performs exhaustive tests for all subtrees and all depths - */ - protected void doExhaustiveTests() { - IPath[] paths = TestUtil.getTreePaths(); - int[] depths = getTreeDepths(); - - for (IPath path : paths) { - for (int depth : depths) { - doTest(path, depth); - } - } - } - - /** - * Write a flattened element tree to a file and read it back. - */ - public Object doFileTest() { - IElementInfoFlattener fac = getFlattener(); - - Object newTree = null; - ElementTreeWriter writer = new ElementTreeWriter(fac); - ElementTreeReader reader = new ElementTreeReader(fac); - FileOutputStream fos = null; - FileInputStream fis = null; - DataOutputStream dos = null; - DataInputStream dis = null; - - /* Write the element tree. */ - try { - File dir = tempFile.getParentFile(); - dir.mkdirs(); - fos = new FileOutputStream(tempFile); - dos = new DataOutputStream(fos); - } catch (IOException e) { - e.printStackTrace(); - fail("Unable to open ouput file"); - } - try { - doWrite(writer, dos); - } catch (IOException e) { - e.printStackTrace(); - fail("Error writing tree to file"); - } finally { - try { - dos.flush(); - fos.close(); - } catch (IOException e) { - e.printStackTrace(); - fail("Unable to close output file!"); - } - } - - /* Read the element tree. */ - try { - fis = new FileInputStream(tempFile); - dis = new DataInputStream(fis); - } catch (IOException e) { - e.printStackTrace(); - fail("Unable to open input file"); - } - try { - newTree = doRead(reader, dis); - } catch (IOException e) { - e.printStackTrace(); - fail("Error reading tree from file"); - } finally { - try { - fis.close(); - } catch (IOException e) { - e.printStackTrace(); - fail("Unable to close input file!"); - } - } - return newTree; - } - - /** Pipe a flattened element tree from writer to reader threads. - */ - public Object doPipeTest() { - IElementInfoFlattener fac = getFlattener(); - Object refried = null; - ElementTreeWriter w = new ElementTreeWriter(fac); - ElementTreeReader r = new ElementTreeReader(fac); - PipedOutputStream pout; - PipedInputStream pin; - DataOutputStream oos; - DataInputStream ois; - - /* Pipe the element tree from writer to reader threads. */ - try { - pout = new PipedOutputStream(); - pin = new PipedInputStream(pout); - oos = new DataOutputStream(pout); //new FileOutputStream(FILE_NAME)); - ois = new DataInputStream(pin); - writerThread.setStream(oos); - readerThread.setStream(ois); - writerThread.setWriter(w); - readerThread.setReader(r); - Thread thread1 = new Thread(writerThread, "testwriter"); - Thread thread2 = new Thread(readerThread, "testreader"); - thread1.start(); - thread2.start(); - while (thread2.isAlive()) { - try { - thread2.join(); - } catch (InterruptedException excp) { - } - } - refried = readerThread.getReconstitutedObject(); - ois.close(); - oos.close(); - } catch (IOException e) { - e.printStackTrace(); - fail("Unable to open stream:"); - } - return refried; - } - - /** - * Performs the serialization activity for this test - */ - public abstract Object doRead(ElementTreeReader reader, DataInputStream input) throws IOException; - - /** - * Runs a test for this class at a certain depth and path - */ - public abstract void doTest(IPath path, int depth); - - /** - * Performs the serialization activity for this test - */ - public abstract void doWrite(ElementTreeWriter writer, DataOutputStream output) throws IOException; - - /** - * Tests the reading and writing of element deltas - */ - public IElementInfoFlattener getFlattener() { - return new IElementInfoFlattener() { - @Override - public void writeElement(IPath path, Object data, DataOutput output) throws IOException { - if (data == null) { - output.writeUTF("null"); - } else { - output.writeUTF((String) data); - } - } - - @Override - public Object readElement(IPath path, DataInput input) throws IOException { - String data = input.readUTF(); - if ("null".equals(data)) { - return null; - } - return data; - } - }; - } - - /** - * Returns all the different possible depth values for this tree. - * To be conservative, it is okay if some of the returned depths - * are not possible. - */ - protected int[] getTreeDepths() { - return new int[] {-1, 0, 1, 2, 3, 4}; - } - - @Before - public void setUp() throws Exception { - fTree = TestUtil.createTestElementTree(); - /* default subtree we are interested in */ - fSubtreePath = solution; - - /* default depth is the whole tree */ - fDepth = ElementTreeWriter.D_INFINITE; - } - - @After - public void tearDown() throws Exception { - //ElementTree tests don't use the CoreTest infrastructure - tempFile.delete(); - } -} diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeSerializationTestHelper.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeSerializationTestHelper.java new file mode 100644 index 00000000000..855e0f6bd4c --- /dev/null +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/ElementTreeSerializationTestHelper.java @@ -0,0 +1,221 @@ +/******************************************************************************* + * Copyright (c) 2000, 2017 IBM Corporation and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.core.tests.internal.watson; + +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.io.DataInput; +import java.io.DataInputStream; +import java.io.DataOutput; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; +import org.eclipse.core.internal.watson.ElementTreeReader; +import org.eclipse.core.internal.watson.ElementTreeWriter; +import org.eclipse.core.internal.watson.IElementInfoFlattener; +import org.eclipse.core.runtime.IPath; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; + +public final class ElementTreeSerializationTestHelper implements ArgumentsProvider { + + private ElementTreeSerializationTestHelper() { + } + + interface StreamReader { + Object doRead(ElementTreeReader reader, DataInputStream input) throws IOException; + } + + interface StreamWriter { + void doWrite(ElementTreeWriter writer, DataOutputStream output) throws IOException; + } + + private static class WriterThread implements Runnable { + private final StreamWriter streamWriter; + private DataOutputStream dataOutputStream; + private ElementTreeWriter treeWriter; + private volatile IOException exceptionDuringRun; + + WriterThread(StreamWriter streamWriter, DataOutputStream stream, ElementTreeWriter treeWriter) { + this.streamWriter = streamWriter; + this.dataOutputStream = stream; + this.treeWriter = treeWriter; + } + + public IOException getExceptionDuringRun() { + return exceptionDuringRun; + } + + @Override + public void run() { + try { + streamWriter.doWrite(treeWriter, dataOutputStream); + // inform reader, writing is finished: + dataOutputStream.flush(); // DeltaChainFlatteningTest 30s -> 0s + } catch (IOException e) { + exceptionDuringRun = e; + } + } + } + + private static class ReaderThread implements Runnable { + private final StreamReader streamReader; + private final DataInputStream dataInputStream; + private final ElementTreeReader treeReader; + private Object refried; + private volatile IOException exceptionDuringRun; + + ReaderThread(StreamReader streamReader, DataInputStream stream, ElementTreeReader reader) { + this.streamReader = streamReader; + this.dataInputStream = stream; + this.treeReader = reader; + } + + public Object getReconstitutedObject() { + return refried; + } + + public IOException getExceptionDuringRun() { + return exceptionDuringRun; + } + + @Override + public void run() { + try { + refried = streamReader.doRead(treeReader, dataInputStream); + } catch (IOException e) { + exceptionDuringRun = e; + } + } + } + + /* + * Arguments for exhaustive test cases across the whole element tree + */ + @Override + public Stream provideArguments(ExtensionContext context) { + List arguments = new ArrayList<>(); + IPath[] paths = TestUtil.getTreePaths(); + int[] depths = getTreeDepths(); + + for (IPath path : paths) { + for (int depth : depths) { + arguments.add(Arguments.of(path, depth)); + } + } + return arguments.stream(); + } + + /** + * Returns all the different possible depth values for this tree. To be + * conservative, it is okay if some of the returned depths are not possible. + */ + private static int[] getTreeDepths() { + return new int[] { -1, 0, 1, 2, 3, 4 }; + } + + /** + * Write a flattened element tree to a file and read it back. + */ + public static Object doFileTest(Path tempDir, StreamReader reading, StreamWriter writing) throws IOException { + IElementInfoFlattener fac = getFlattener(); + + Object newTree = null; + ElementTreeWriter writer = new ElementTreeWriter(fac); + ElementTreeReader reader = new ElementTreeReader(fac); + + /* Write the element tree. */ + Files.createDirectories(tempDir); + File tempFile = tempDir.resolve("TestFlattening").toFile(); + try (FileOutputStream fos = new FileOutputStream(tempFile)) { + DataOutputStream dos = new DataOutputStream(fos); + writing.doWrite(writer, dos); + } + + /* Read the element tree. */ + try (FileInputStream fis = new FileInputStream(tempFile); DataInputStream dis = new DataInputStream(fis)) { + newTree = reading.doRead(reader, dis); + } + return newTree; + } + + /** Pipe a flattened element tree from writer to reader threads. + */ + public static Object doPipeTest(StreamWriter streamWriter, StreamReader streamReader) throws IOException { + IElementInfoFlattener fac = getFlattener(); + Object refried = null; + ElementTreeWriter w = new ElementTreeWriter(fac); + ElementTreeReader r = new ElementTreeReader(fac); + + /* Pipe the element tree from writer to reader threads. */ + try (PipedOutputStream pout = new PipedOutputStream(); + PipedInputStream pin = new PipedInputStream(pout); + DataOutputStream oos = new DataOutputStream(pout); // new FileOutputStream(FILE_NAME)); + DataInputStream ois = new DataInputStream(pin)) { + WriterThread writerThread = new WriterThread(streamWriter, oos, w); + ReaderThread readerThread = new ReaderThread(streamReader, ois, r); + Thread thread1 = new Thread(writerThread, "testwriter"); + Thread thread2 = new Thread(readerThread, "testreader"); + thread1.start(); + thread2.start(); + while (thread2.isAlive()) { + try { + thread2.join(); + } catch (InterruptedException excp) { + } + } + assertNull(writerThread.getExceptionDuringRun(), "exception occurred during write"); + assertNull(readerThread.getExceptionDuringRun(), "exception occurred during read"); + refried = readerThread.getReconstitutedObject(); + } + return refried; + } + + + /** + * Tests the reading and writing of element deltas + */ + private static IElementInfoFlattener getFlattener() { + return new IElementInfoFlattener() { + @Override + public void writeElement(IPath path, Object data, DataOutput output) throws IOException { + if (data == null) { + output.writeUTF("null"); + } else { + output.writeUTF((String) data); + } + } + + @Override + public Object readElement(IPath path, DataInput input) throws IOException { + String data = input.readUTF(); + if ("null".equals(data)) { + return null; + } + return data; + } + }; + } + +} diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/IPathConstants.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/IPathConstants.java index de2bee0f885..bb5b02795ed 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/IPathConstants.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/IPathConstants.java @@ -24,17 +24,17 @@ interface IPathConstants { * The following paths are used in the tree created by * ElementTreeTestUtilities.createTestElementTree() */ - IPath root = IPath.ROOT; - IPath solution = root.append("solution"); - IPath project1 = solution.append("project1"); - IPath project2 = solution.append("project2"); - IPath file1 = project2.append("file1"); - IPath folder1 = project2.append("folder1"); - IPath folder2 = project2.append("folder2"); + final IPath root = IPath.ROOT; + final IPath solution = root.append("solution"); + final IPath project1 = solution.append("project1"); + final IPath project2 = solution.append("project2"); + final IPath file1 = project2.append("file1"); + final IPath folder1 = project2.append("folder1"); + final IPath folder2 = project2.append("folder2"); - IPath file2 = folder1.append("file2"); - IPath folder3 = folder1.append("folder3"); - IPath folder4 = folder1.append("folder4"); + final IPath file2 = folder1.append("file2"); + final IPath folder3 = folder1.append("folder3"); + final IPath folder4 = folder1.append("folder4"); - IPath file3 = folder3.append("file3"); + final IPath file3 = folder3.append("file3"); } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/TestUtil.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/TestUtil.java index 44a8bee1479..9df2a7982ee 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/TestUtil.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/TestUtil.java @@ -15,9 +15,9 @@ package org.eclipse.core.tests.internal.watson; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -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.assertTrue; import java.util.ArrayList; import java.util.Random; @@ -65,9 +65,9 @@ static protected void assertEqualTrees(String message, ElementTree expected, Ele */ static protected void assertEqualTrees(String msg, ElementTree expected, ElementTree actual, IPath path, int depth) { /* check node at current path */ - assertTrue(msg, expected.includes(path)); - assertTrue(msg, actual.includes(path)); - assertEquals(msg, expected.getElementData(path), actual.getElementData(path)); + assertTrue(expected.includes(path), msg); + assertTrue(actual.includes(path), msg); + assertEquals(expected.getElementData(path), actual.getElementData(path), msg); if (depth != 0) { /* get the children */ @@ -80,7 +80,7 @@ static protected void assertEqualTrees(String msg, ElementTree expected, Element --newDepth; } for (int i = 0; i < expectedChildren.length; i++) { - assertEquals("children IDs", expectedChildren[i], actualChildren[i]); + assertEquals(expectedChildren[i], actualChildren[i], "children IDs"); assertEqualTrees("Recursive call", expected, actual, expectedChildren[i], newDepth); } } @@ -91,7 +91,7 @@ static protected void assertEqualTrees(String msg, ElementTree expected, Element */ static protected void assertHasPaths(ElementTree tree, IPath[] paths) { for (int i = 0; i < paths.length; i++) { - assertTrue("assertHasPaths" + i, tree.includes(paths[i])); + assertTrue(tree.includes(paths[i]), "assertHasPaths" + i); } } @@ -100,7 +100,7 @@ static protected void assertHasPaths(ElementTree tree, IPath[] paths) { */ static protected void assertNoPaths(ElementTree tree, IPath[] paths) { for (IPath path : paths) { - assertFalse("assertNoPaths: " + path, tree.includes(path)); + assertFalse(tree.includes(path), "assertNoPaths: " + path); } } diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/TreeFlatteningTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/TreeFlatteningTest.java index 4bf4aa16b33..3cd83dd7eac 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/TreeFlatteningTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/internal/watson/TreeFlatteningTest.java @@ -13,74 +13,52 @@ *******************************************************************************/ package org.eclipse.core.tests.internal.watson; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.eclipse.core.tests.internal.watson.ElementTreeSerializationTestHelper.doFileTest; +import static org.eclipse.core.tests.internal.watson.ElementTreeSerializationTestHelper.doPipeTest; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -import java.util.Arrays; +import java.nio.file.Path; import org.eclipse.core.internal.resources.SaveManager; import org.eclipse.core.internal.watson.ElementTree; import org.eclipse.core.internal.watson.ElementTreeReader; import org.eclipse.core.internal.watson.ElementTreeWriter; import org.eclipse.core.runtime.IPath; -import org.junit.Before; -import org.junit.Test; +import org.eclipse.core.tests.internal.watson.ElementTreeSerializationTestHelper.StreamReader; +import org.eclipse.core.tests.internal.watson.ElementTreeSerializationTestHelper.StreamWriter; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ArgumentsSource; /** * Unit tests for ElementTreeWriter and * ElementTreeReader. */ -public class TreeFlatteningTest extends ElementTreeSerializationTest { +public class TreeFlatteningTest implements IPathConstants { - /** - * Performs the serialization activity for this test - */ - @Override - public Object doRead(ElementTreeReader reader, DataInputStream input) throws IOException { - return reader.readTree(input); - } - - /** - * Runs a test for this class at a certain depth and path - */ - @Override - public void doTest(IPath path, int depth) { - /* 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); + private StreamReader getReader() throws IOException { + return (ElementTreeReader reader, DataInputStream input) -> reader.readTree(input); } - /** - * Performs the serialization activity for this test - */ - @Override - public void doWrite(ElementTreeWriter writer, DataOutputStream output) throws IOException { - writer.writeTree(fTree, fSubtreePath, fDepth, output); - } - - @Override - @Before - public void setUp() throws Exception { - super.setUp(); - fTree = TestUtil.createTestElementTree(); + private StreamWriter getWriter(ElementTree tree, IPath path, int depth) throws IOException { + return (ElementTreeWriter writer, DataOutputStream output) -> writer.writeTree(tree, path, depth, output); } @Test - public void test0() { - /* Get an element tree from somewhere. */ - fTree = TestUtil.createTestElementTree(); - ElementTree newTree = (ElementTree) doFileTest(); + public void test0(@TempDir Path tempDir) throws IOException { + ElementTree tree = TestUtil.createTestElementTree(); + IPath testTreeRootPath = IPathConstants.solution; + ElementTree newTree = (ElementTree) doFileTest(tempDir, getReader(), + getWriter(tree, testTreeRootPath, ElementTreeWriter.D_INFINITE)); - TestUtil.assertEqualTrees(this.getClass() + "test0", fTree, newTree); + TestUtil.assertEqualTrees(this.getClass() + "test0", tree, newTree); } @Test @@ -128,7 +106,7 @@ public void testSortTrees() { System.out.println("oldest=" + trees12[oldest12].toDebugString()); ElementTree[] sorted12 = SaveManager.sortTrees(trees12); ElementTree[] sorted21 = SaveManager.sortTrees(trees21); - assertTrue(Arrays.equals(sorted12, sorted21)); + assertArrayEquals(sorted12, sorted21); assertSame(tree1111, sorted12[0]); // sorted by creation time desc } { // trees with duplicates @@ -144,7 +122,7 @@ public void testSortTrees() { System.out.println("oldest=" + trees12[oldest12].toDebugString()); ElementTree[] sorted12 = SaveManager.sortTrees(trees12); ElementTree[] sorted21 = SaveManager.sortTrees(trees21); - assertTrue(Arrays.equals(sorted12, sorted21)); + assertArrayEquals(sorted12, sorted21); assertSame(tree1111, sorted12[0]); // sorted by creation time desc } { // sparse (without all intermediate trees) @@ -159,7 +137,7 @@ public void testSortTrees() { System.out.println("oldest=" + trees12[oldest12].toDebugString()); ElementTree[] sorted12 = SaveManager.sortTrees(trees12); ElementTree[] sorted21 = SaveManager.sortTrees(trees21); - assertTrue(Arrays.equals(sorted12, sorted21)); + assertArrayEquals(sorted12, sorted21); assertSame(tree1111, sorted12[0]); // sorted by creation time desc } { // without newest @@ -174,7 +152,7 @@ public void testSortTrees() { System.out.println("oldest=" + trees12[oldest12].toDebugString()); ElementTree[] sorted12 = SaveManager.sortTrees(trees12); ElementTree[] sorted21 = SaveManager.sortTrees(trees21); - assertTrue(Arrays.equals(sorted12, sorted21)); + assertArrayEquals(sorted12, sorted21); assertSame(tree111, sorted12[0]); // sorted by creation time desc } { // without oldest @@ -189,7 +167,7 @@ public void testSortTrees() { System.out.println("oldest=" + trees12[oldest12].toDebugString()); ElementTree[] sorted12 = SaveManager.sortTrees(trees12); ElementTree[] sorted21 = SaveManager.sortTrees(trees21); - assertTrue(Arrays.equals(sorted12, sorted21)); + assertArrayEquals(sorted12, sorted21); assertSame(tree1111, sorted12[0]); // sorted by creation time desc } { // trees with odd duplicates @@ -205,7 +183,7 @@ public void testSortTrees() { System.out.println("oldest=" + trees12[oldest12].toDebugString()); ElementTree[] sorted12 = SaveManager.sortTrees(trees12); ElementTree[] sorted21 = SaveManager.sortTrees(trees21); - assertTrue(Arrays.equals(sorted12, sorted21)); + assertArrayEquals(sorted12, sorted21); assertSame(tree1111, sorted12[0]); // sorted by creation time desc } } @@ -221,35 +199,40 @@ private ElementTree[] reversed(ElementTree[] trees) { /** * Tests the reading and writing of element deltas */ - @Test - public void testExhaustive() { - doExhaustiveTests(); + @ParameterizedTest + @ArgumentsSource(ElementTreeSerializationTestHelper.class) + public void testExhaustive(IPath path, int depth) throws IOException { + ElementTree tree = TestUtil.createTestElementTree(); + ElementTree newTree = (ElementTree) doPipeTest(getWriter(tree, path, depth), + getReader()); + + TestUtil.assertEqualTrees(this.getClass() + "test0", tree, newTree, path, depth); } @Test - public void testNullData() { - /* Get an element tree from somewhere. */ - fTree = TestUtil.createTestElementTree(); - fTree = fTree.newEmptyDelta(); + public void testNullData() throws IOException { + ElementTree tree = TestUtil.createTestElementTree(); + tree = tree.newEmptyDelta(); + IPath testTreeRootPath = IPathConstants.solution; /* set some elements to have null data */ - fTree.setElementData(solution, null); - fTree.setElementData(folder2, null); - fTree.immutable(); + tree.setElementData(IPathConstants.solution, null); + tree.setElementData(IPathConstants.folder2, null); + tree.immutable(); - ElementTree newTree = (ElementTree) doPipeTest(); + ElementTree newTree = (ElementTree) doPipeTest(getWriter(tree, testTreeRootPath, ElementTreeWriter.D_INFINITE), + getReader()); - TestUtil.assertEqualTrees(this.getClass() + "test0", fTree, newTree); + TestUtil.assertEqualTrees(this.getClass() + "test0", tree, newTree); } @Test - public void testWriteRoot() { - /* Get an element tree from somewhere. */ - fTree = TestUtil.createTestElementTree(); - fSubtreePath = IPath.ROOT; - - ElementTree newTree = (ElementTree) doPipeTest(); + public void testWriteRoot() throws IOException { + ElementTree tree = TestUtil.createTestElementTree(); + IPath path = IPath.ROOT; + ElementTree newTree = (ElementTree) doPipeTest(getWriter(tree, path, ElementTreeWriter.D_INFINITE), + getReader()); - TestUtil.assertEqualTrees(this.getClass() + "test0", fTree, newTree, fSubtreePath); + TestUtil.assertEqualTrees(this.getClass() + "test0", tree, newTree, path); } }