Permalink
Browse files

KAT-466 Add functionality to report on missing items at the end of th…

…e secondary stream.
  • Loading branch information...
Leon
Leon committed Oct 6, 2011
1 parent b8c920d commit 077c54024be2ec8023b68d0297938c694aa274bc
@@ -1,12 +1,23 @@
package com.aconex.scrutineer;
+import java.util.Iterator;
+
public class IdAndVersionStreamVerifier {
public void verify(IdAndVersionStream primaryStream, IdAndVersionStream secondayStream, IdAndVersionStreamVerifierListener idAndVersionStreamVerifierListener) {
primaryStream.open();
secondayStream.open();
try {
+ Iterator<IdAndVersion> primaryIterator = primaryStream.iterator();
+ Iterator<IdAndVersion> secondaryIterator = secondayStream.iterator();
+ while (primaryIterator.hasNext() && secondaryIterator.hasNext()) {
+ primaryIterator.next();
+ secondaryIterator.next();
+ }
+ while (primaryIterator.hasNext()) {
+ idAndVersionStreamVerifierListener.onMissingInSecondaryStream(primaryIterator.next());
+ }
}
finally {
primaryStream.close();
@@ -1,5 +1,7 @@
package com.aconex.scrutineer;
public interface IdAndVersionStreamVerifierListener {
+
+ void onMissingInSecondaryStream(IdAndVersion idAndVersion);
}
@@ -23,7 +23,7 @@ public boolean matchesSafely(IdAndVersion idAndVersion) {
@Override
public void describeTo(Description description) {
- description.appendText(" had id and version "+id+":"+version);
+ description.appendText(" has id and version "+id+":"+version);
}
@Factory
@@ -1,23 +1,28 @@
package com.aconex.scrutineer;
import com.aconex.scrutineer.javautil.JavaIteratorIdAndVersionStream;
+import com.google.common.collect.Iterators;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
+import java.util.Collections;
import java.util.List;
+import static com.aconex.scrutineer.HasIdAndVersionMatcher.hasIdAndVersion;
import static com.google.common.collect.Lists.newArrayList;
+import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
public class IdAndVersionStreamVerifierTest {
- private static final List<IdAndVersion> LIST = newArrayList(
+ private static final List<IdAndVersion> LIST = Collections.unmodifiableList(newArrayList(
item("1", 1),
item("3", 3),
- item("2", 2));
+ item("2", 2)));
@Mock
private IdAndVersionStream primaryStream;
@@ -27,38 +32,56 @@
@Mock
private IdAndVersionStreamVerifierListener idAndVersionStreamVerifierListener;
-
+ private IdAndVersionStreamVerifier idAndVersionStreamVerifier;
+
@Before
public void setup() {
initMocks(this);
+ idAndVersionStreamVerifier = new IdAndVersionStreamVerifier();
+ when(primaryStream.iterator()).thenReturn(LIST.iterator());
+ when(secondayStream.iterator()).thenReturn(LIST.iterator());
}
@Test
public void shouldOpenBothStreams() {
- IdAndVersionStreamVerifier idAndVersionStreamVerifier = new IdAndVersionStreamVerifier();
idAndVersionStreamVerifier.verify(primaryStream, secondayStream, idAndVersionStreamVerifierListener);
verify(primaryStream).open();
verify(secondayStream).open();
}
@Test
public void shouldCloseBothStreams() {
- IdAndVersionStreamVerifier idAndVersionStreamVerifier = new IdAndVersionStreamVerifier();
idAndVersionStreamVerifier.verify(primaryStream, secondayStream, idAndVersionStreamVerifierListener);
verify(primaryStream).close();
verify(secondayStream).close();
}
@Test
public void shouldNotReportErrorsIfStreamsAreEqual() {
- IdAndVersionStreamVerifier idAndVersionStreamVerifier = new IdAndVersionStreamVerifier();
idAndVersionStreamVerifier.verify(
- new JavaIteratorIdAndVersionStream(LIST.iterator()),
- new JavaIteratorIdAndVersionStream(LIST.iterator()),
+ streamOf(item(1), item(2), item(3)),
+ streamOf(item(1), item(2), item(3)),
idAndVersionStreamVerifierListener);
verifyZeroInteractions(idAndVersionStreamVerifierListener);
}
+ @Test
+ public void shouldReportMissingItemsAtTheEndOfTheSecondaryStream() {
+ idAndVersionStreamVerifier.verify(
+ streamOf(item(1), item(2), item(3), item(4)),
+ streamOf(item(1), item(2), item(3)),
+ idAndVersionStreamVerifierListener);
+ verify(idAndVersionStreamVerifierListener).onMissingInSecondaryStream(argThat(hasIdAndVersion("4",4)));
+ }
+
+ private static JavaIteratorIdAndVersionStream streamOf(IdAndVersion ... items) {
+ return new JavaIteratorIdAndVersionStream(Iterators.forArray(items));
+ }
+
+ static IdAndVersion item(long version) {
+ return new IdAndVersion(""+version, version);
+ }
+
static IdAndVersion item(String id, long version) {
return new IdAndVersion(id, version);
}

0 comments on commit 077c540

Please sign in to comment.