Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Borkowski2015-05-19 13:01:53 +0000
committerAxel RICHARD2015-05-27 08:06:27 +0000
commitdb9f81ea7ff7f799e51218520c9d250efb715834 (patch)
tree6c33878d45a2f429e9f8190e8dc61759c26d2aca /plugins
parentb058efcc035c6c85437c30ed1833187cac84e152 (diff)
downloadorg.eclipse.emf.compare-db9f81ea7ff7f799e51218520c9d250efb715834.tar.gz
org.eclipse.emf.compare-db9f81ea7ff7f799e51218520c9d250efb715834.tar.xz
org.eclipse.emf.compare-db9f81ea7ff7f799e51218520c9d250efb715834.zip
DependencyGraphUpdaterTest using Mockito
The unit test has been re-written to use Mockito, which is more concise and easier to read. Furthermore, the missing test for recordRemoval(..) has been added. Coverage is 60% and the missing 40% are accountable to LOGGER.isDebugEnabled() (resulting in deficient branch and instruction coverage). Change-Id: I5f555929a1e3b01243796dee074270754fc0446d Signed-off-by: Michael Borkowski <mborkowski@eclipsesource.com>
Diffstat (limited to 'plugins')
-rw-r--r--plugins/org.eclipse.emf.compare.ide.ui.tests/src/org/eclipse/emf/compare/ide/ui/tests/logical/resolver/DependencyGraphUpdaterTest.java188
1 files changed, 66 insertions, 122 deletions
diff --git a/plugins/org.eclipse.emf.compare.ide.ui.tests/src/org/eclipse/emf/compare/ide/ui/tests/logical/resolver/DependencyGraphUpdaterTest.java b/plugins/org.eclipse.emf.compare.ide.ui.tests/src/org/eclipse/emf/compare/ide/ui/tests/logical/resolver/DependencyGraphUpdaterTest.java
index 96534c6e6..3c517ec5b 100644
--- a/plugins/org.eclipse.emf.compare.ide.ui.tests/src/org/eclipse/emf/compare/ide/ui/tests/logical/resolver/DependencyGraphUpdaterTest.java
+++ b/plugins/org.eclipse.emf.compare.ide.ui.tests/src/org/eclipse/emf/compare/ide/ui/tests/logical/resolver/DependencyGraphUpdaterTest.java
@@ -7,30 +7,44 @@
*
* Contributors:
* Philip Langer - initial API and implementation
+ * Michael Borkowski - rewrite using Mockito
*******************************************************************************/
package org.eclipse.emf.compare.ide.ui.tests.logical.resolver;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.verifyZeroInteractions;
import com.google.common.base.Optional;
+import com.google.common.collect.Sets;
import com.google.common.eventbus.EventBus;
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
+import java.util.Collections;
import org.eclipse.emf.compare.ide.ui.internal.logical.resolver.DependencyFoundEvent;
import org.eclipse.emf.compare.ide.ui.internal.logical.resolver.DependencyGraphUpdater;
import org.eclipse.emf.compare.ide.ui.internal.logical.resolver.ResolvedEvent;
+import org.eclipse.emf.compare.ide.ui.internal.logical.resolver.ResourceRemovedEvent;
import org.eclipse.emf.compare.internal.utils.Graph;
+import org.junit.Before;
import org.junit.Test;
-@SuppressWarnings({"nls", "restriction" })
+@SuppressWarnings({"nls", "restriction", "cast", "unchecked" })
public class DependencyGraphUpdaterTest {
+ Graph<String> graph;
+
+ DependencyGraphUpdater<String> sut;
+
+ @Before
+ public void setUp() {
+ graph = (Graph<String>)mock(Graph.class);
+ sut = new DependencyGraphUpdater<String>(graph, new EventBus());
+ }
+
@Test(expected = NullPointerException.class)
public void testInstantiationWithNullGraph() {
new DependencyGraphUpdater<String>(null, new EventBus());
@@ -46,141 +60,71 @@ public class DependencyGraphUpdaterTest {
new DependencyGraphUpdater<String>(new Graph<String>(), new EventBus());
}
+ @Test
public void testInstantiationDoesNotModifyGraph() {
- MockStringGraph graph = new MockStringGraph();
- new DependencyGraphUpdater<String>(graph, new EventBus());
- assertTrue(graph.addedElements.isEmpty());
- assertTrue(graph.addedChildRelationships.isEmpty());
- assertTrue(graph.addedParentRelationships.isEmpty());
- assertFalse(graph.didRemove);
+ verifyZeroInteractions(graph);
}
@Test
public void testRecordNodeAddsNodeToGraph() {
- MockStringGraph graph = new MockStringGraph();
- DependencyGraphUpdater<String> updater = new DependencyGraphUpdater<String>(graph, new EventBus());
-
- ResolvedEvent<String> resolvedEvent = new ResolvedEvent<String>("1");
- updater.recordNode(resolvedEvent);
- assertEquals(1, graph.addedElements.size());
- assertEquals("1", graph.addedElements.get(0));
-
- resolvedEvent = new ResolvedEvent<String>("2");
- updater.recordNode(resolvedEvent);
- assertEquals(2, graph.addedElements.size());
- assertEquals("2", graph.addedElements.get(1));
- }
-
- @Test
- public void testRecordEdgeWithoutParent() {
- MockStringGraph graph = new MockStringGraph();
- DependencyGraphUpdater<String> updater = new DependencyGraphUpdater<String>(graph, new EventBus());
-
- DependencyFoundEvent<String> event;
+ ResolvedEvent<String> event1, event2;
- event = new DependencyFoundEvent<String>("from", "to");
- updater.recordEdge(event);
- assertAddedChildrenAtIndex(graph, "from", "to", 0);
+ event1 = new ResolvedEvent<String>("1");
+ sut.recordNode(event1);
- event = new DependencyFoundEvent<String>("from2", "to2");
- updater.recordEdge(event);
- assertAddedChildrenAtIndex(graph, "from2", "to2", 1);
- }
+ event2 = new ResolvedEvent<String>("2");
+ sut.recordNode(event2);
- private void assertAddedChildrenAtIndex(MockStringGraph graph, String expectedFrom, String expectedTo,
- int index) {
- assertEquals(index + 1, graph.addedChildRelationships.size());
- assertEquals(expectedFrom, graph.addedChildRelationships.get(index).element);
- assertEquals(1, graph.addedChildRelationships.get(index).children.size());
- assertEquals(expectedTo, graph.addedChildRelationships.get(index).children.iterator().next());
+ verify(graph).add(event1.getNode());
+ verify(graph).add(event2.getNode());
+ verifyNoMoreInteractions(graph);
}
@Test
- public void testRecordEdgeWithParent() {
- MockStringGraph graph = new MockStringGraph();
- DependencyGraphUpdater<String> updater = new DependencyGraphUpdater<String>(graph, new EventBus());
-
- DependencyFoundEvent<String> event;
+ public void testRecordEdgeWithoutParent() {
+ DependencyFoundEvent<String> event1, event2;
- event = new DependencyFoundEvent<String>("from", "to", Optional.of("parent"));
- updater.recordEdge(event);
- assertAddedChildrenAtIndex(graph, "from", "to", 0);
- assertAddedParentAtIndex(graph, "to", "parent", 0);
+ event1 = new DependencyFoundEvent<String>("from1", "to1");
+ sut.recordEdge(event1);
- event = new DependencyFoundEvent<String>("from2", "to2", Optional.of("parent2"));
- updater.recordEdge(event);
- assertAddedChildrenAtIndex(graph, "from2", "to2", 1);
- assertAddedParentAtIndex(graph, "to2", "parent2", 1);
- }
+ event2 = new DependencyFoundEvent<String>("from2", "to2");
+ sut.recordEdge(event2);
- private void assertAddedParentAtIndex(MockStringGraph graph, String to, String parent, int index) {
- assertEquals(index + 1, graph.addedParentRelationships.size());
- assertEquals(to, graph.addedParentRelationships.get(index).element);
- assertEquals(parent, graph.addedParentRelationships.get(index).parent);
+ verify(graph).addChildren(event1.getFrom(), Collections.singleton(event1.getTo()));
+ verify(graph).addChildren(event2.getFrom(), Collections.singleton(event2.getTo()));
+ verifyNoMoreInteractions(graph);
}
- private class MockStringGraph extends Graph<String> {
-
- List<String> addedElements = new LinkedList<String>();
-
- List<ChildRelationShip> addedChildRelationships = new LinkedList<ChildRelationShip>();
-
- List<ParentRelationShip> addedParentRelationships = new LinkedList<ParentRelationShip>();
-
- boolean didRemove = false;
-
- @Override
- public boolean add(String element) {
- addedElements.add(element);
- return super.add(element);
- }
-
- @Override
- public void addChildren(String element, Set<String> newChildren) {
- addedChildRelationships.add(new ChildRelationShip(element, newChildren));
- super.addChildren(element, newChildren);
- }
-
- @Override
- public void addParentData(String element, String parentData) {
- addedParentRelationships.add(new ParentRelationShip(element, parentData));
- super.addParentData(element, parentData);
- }
-
- @Override
- public void remove(String element) {
- didRemove = true;
- super.remove(element);
- }
-
- @Override
- public void removeAll(Collection<String> elements) {
- didRemove = true;
- super.removeAll(elements);
- }
+ @Test
+ public void testRecordEdgeWithParent() {
+ DependencyFoundEvent<String> event1, event2;
- }
+ event1 = new DependencyFoundEvent<String>("from1", "to1", Optional.of("parent"));
+ sut.recordEdge(event1);
- private class ChildRelationShip {
- String element;
+ verify(graph).addChildren(event1.getFrom(), Collections.singleton(event1.getTo()));
+ verify(graph).addParentData(event1.getTo(), event1.getParent().get());
- Set<String> children;
+ event2 = new DependencyFoundEvent<String>("from2", "to2", Optional.of("parent2"));
+ sut.recordEdge(event2);
- protected ChildRelationShip(String element, Set<String> children) {
- this.element = element;
- this.children = children;
- }
+ verify(graph).addChildren(event2.getFrom(), Collections.singleton(event2.getTo()));
+ verify(graph).addParentData(event2.getTo(), event2.getParent().get());
}
- private class ParentRelationShip {
- String element;
-
- String parent;
-
- protected ParentRelationShip(String element, String parent) {
- this.element = element;
- this.parent = parent;
- }
+ @Test
+ public void testRemoval() {
+ ResourceRemovedEvent<String> event;
+
+ sut.recordNode(new ResolvedEvent<String>("a"));
+ sut.recordNode(new ResolvedEvent<String>("b"));
+ sut.recordNode(new ResolvedEvent<String>("c"));
+ sut.recordNode(new ResolvedEvent<String>("d"));
+ event = new ResourceRemovedEvent<String>(Sets.newHashSet("a", "b", "c"));
+ sut.recordRemoval(event);
+
+ verify(graph, times(4)).add(anyString());
+ verify(graph).removeAll(event.getElements());
+ verifyNoMoreInteractions(graph);
}
-
}

Back to the top