Permalink
Browse files

KAT-465 Cleaned up some of the code.

Make sure close() still happens on exceptions.
  • Loading branch information...
1 parent 2d29bfc commit 54dae060c2c7345431b771704bf685c9371775c0 Leon committed Oct 7, 2011
View
17 pom.xml
@@ -53,11 +53,18 @@
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
- <dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- <version>r06</version>
- </dependency>
+
+ <dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ <version>r06</version>
+ </dependency>
+
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>1.2.14</version>
+ </dependency>
</dependencies>
View
63 src/main/java/com/aconex/scrutineer/IdAndVersionStreamVerifier.java
@@ -1,44 +1,63 @@
package com.aconex.scrutineer;
+import org.apache.log4j.Logger;
+
import java.util.Iterator;
public class IdAndVersionStreamVerifier {
+ private static final Logger LOG = LogUtils.loggerForThisClass();
+
public void verify(IdAndVersionStream primaryStream, IdAndVersionStream secondayStream, IdAndVersionStreamVerifierListener idAndVersionStreamVerifierListener) {
- primaryStream.open();
- secondayStream.open();
try {
+ primaryStream.open();
+ secondayStream.open();
+
Iterator<IdAndVersion> primaryIterator = primaryStream.iterator();
Iterator<IdAndVersion> secondaryIterator = secondayStream.iterator();
while (primaryIterator.hasNext() && secondaryIterator.hasNext()) {
- IdAndVersion primaryItem = primaryIterator.next();
- IdAndVersion secondaryItem = secondaryIterator.next();
-
- if(!primaryItem.equals(secondaryItem)) {
- if (primaryItem.getId().equals(secondaryItem.getId())) {
- idAndVersionStreamVerifierListener.onVersionMisMatch(primaryItem, secondaryItem);
- }
- else if (primaryItem.compareTo(secondaryItem) < 0) {
- idAndVersionStreamVerifierListener.onMissingInSecondaryStream(primaryItem);
- primaryIterator.next();
- }
- else {
- idAndVersionStreamVerifierListener.onMissingInPrimaryStream(secondaryItem);
- secondaryIterator.next();
- }
- }
+ compareStreams(idAndVersionStreamVerifierListener, primaryIterator, secondaryIterator);
}
- handleDifferencesAtEndOfStreams(idAndVersionStreamVerifierListener, primaryIterator, secondaryIterator);
+ logDifferencesAtEndOfStreams(idAndVersionStreamVerifierListener, primaryIterator, secondaryIterator);
}
finally {
- primaryStream.close();
- secondayStream.close();
+ closeWithoutThrowingException(primaryStream);
+ closeWithoutThrowingException(secondayStream);
+ }
+ }
+
+ private void closeWithoutThrowingException(IdAndVersionStream idAndVersionStream) {
+ try {
+ idAndVersionStream.close();
+ }
+ catch(Exception e) {
+ LogUtils.warn(LOG,"Unable to close IdAndVersionStream",e);
+ }
+ }
+
+ private void compareStreams(IdAndVersionStreamVerifierListener idAndVersionStreamVerifierListener, Iterator<IdAndVersion> primaryIterator, Iterator<IdAndVersion> secondaryIterator) {
+
+ IdAndVersion primaryItem = primaryIterator.next();
+ IdAndVersion secondaryItem = secondaryIterator.next();
+
+ if(!primaryItem.equals(secondaryItem)) {
+ if (primaryItem.getId().equals(secondaryItem.getId())) {
+ idAndVersionStreamVerifierListener.onVersionMisMatch(primaryItem, secondaryItem);
+ }
+ else if (primaryItem.compareTo(secondaryItem) < 0) {
+ idAndVersionStreamVerifierListener.onMissingInSecondaryStream(primaryItem);
+ primaryIterator.next();
+ }
+ else {
+ idAndVersionStreamVerifierListener.onMissingInPrimaryStream(secondaryItem);
+ secondaryIterator.next();
+ }
}
}
- private void handleDifferencesAtEndOfStreams(IdAndVersionStreamVerifierListener idAndVersionStreamVerifierListener, Iterator<IdAndVersion> primaryIterator, Iterator<IdAndVersion> secondaryIterator) {
+ private void logDifferencesAtEndOfStreams(IdAndVersionStreamVerifierListener idAndVersionStreamVerifierListener, Iterator<IdAndVersion> primaryIterator, Iterator<IdAndVersion> secondaryIterator) {
while (primaryIterator.hasNext()) {
idAndVersionStreamVerifierListener.onMissingInSecondaryStream(primaryIterator.next());
View
68 src/main/java/com/aconex/scrutineer/LogUtils.java
@@ -0,0 +1,68 @@
+package com.aconex.scrutineer;
+
+
+import org.apache.log4j.Logger;
+
+public final class LogUtils {
+
+ private static final int MILLIES_PER_SECOND = 1000;
+
+ private LogUtils() {
+ // For Checkstyle
+ }
+
+ public static void debug(Logger log, String message, Object... args) {
+ if (!log.isDebugEnabled()) {
+ return;
+ }
+ log.debug(getFormattedMessage(message, args));
+ }
+
+ public static void debug(Logger log, String message, Throwable throwable, Object... args) {
+ if (!log.isDebugEnabled()) {
+ return;
+ }
+ log.debug(getFormattedMessage(message, args), throwable);
+ }
+
+ public static void warn(Logger log, String message, Object... args) {
+ log.warn(getFormattedMessage(message, args));
+ }
+
+ public static void warn(Logger log, String message, Throwable throwable, Object... args) {
+ log.warn(getFormattedMessage(message, args), throwable);
+ }
+
+ public static void error(Logger log, String message, Object... args) {
+ log.error(getFormattedMessage(message, args));
+ }
+
+ public static void error(Logger log, String message, Throwable throwable, Object... args) {
+ log.error(getFormattedMessage(message, args), throwable);
+ }
+
+ public static void info(Logger log, String message, Object... args) {
+ log.info(getFormattedMessage(message, args));
+ }
+
+ public static void info(Logger log, String message, Throwable throwable, Object... args) {
+ log.info(getFormattedMessage(message, args), throwable);
+ }
+
+ private static String getFormattedMessage(String message, Object... args) {
+ String formattedMessage = message;
+ if (args != null && args.length > 0) {
+ formattedMessage = String.format(message, args);
+ }
+ return formattedMessage;
+ }
+
+ public static Logger loggerForThisClass() {
+ // We use the third stack element; second is this method, first is .getStackTrace()
+ StackTraceElement myCaller = Thread.currentThread().getStackTrace()[2];
+ if(!"<clinit>".equals(myCaller.getMethodName())) {
+ throw new RuntimeException("Logger must be static");
+ }
+ return Logger.getLogger(myCaller.getClassName());
+ }
+}
View
35 src/test/java/com/aconex/scrutineer/IdAndVersionStreamVerifierTest.java
@@ -12,6 +12,7 @@
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.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
@@ -57,6 +58,32 @@ public void shouldCloseBothStreams() {
}
@Test
+ public void shouldCloseWhenOpenThrowsAnException() {
+ doThrow(new RuntimeException()).when(secondayStream).open();
+ try {
+ idAndVersionStreamVerifier.verify(primaryStream, secondayStream, idAndVersionStreamVerifierListener);
+ }
+ catch (RuntimeException e) {
+ //Expected
+ }
+ verify(primaryStream).close();
+ verify(secondayStream).close();
+ }
+
+ @Test
+ public void shouldCloseSecondaryWhenPrimaryCloseThrowsException() {
+ doThrow(new RuntimeException()).when(primaryStream).close();
+ try {
+ idAndVersionStreamVerifier.verify(primaryStream, secondayStream, idAndVersionStreamVerifierListener);
+ }
+ catch (RuntimeException e) {
+ //Expected
+ }
+ verify(secondayStream).close();
+ }
+
+
+ @Test
public void shouldNotReportErrorsIfStreamsAreEqual() {
idAndVersionStreamVerifier.verify(
streamOf(item(1), item(2), item(3)),
@@ -80,7 +107,7 @@ public void shouldReportMissingItemsAtTheEndOfThePrimaryStream() {
streamOf(item(1), item(2), item(3)),
streamOf(item(1), item(2), item(3), item(4)),
idAndVersionStreamVerifierListener);
- verify(idAndVersionStreamVerifierListener).onMissingInPrimaryStream(argThat(hasIdAndVersion("4",4)));
+ verify(idAndVersionStreamVerifierListener).onMissingInPrimaryStream(argThat(hasIdAndVersion("4", 4)));
}
@Test
@@ -89,7 +116,7 @@ public void shouldReportMissingItemsAtTheStartOfTheSecondaryStream() {
streamOf(item(1), item(2), item(3), item(4)),
streamOf(item(2), item(3), item(4)),
idAndVersionStreamVerifierListener);
- verify(idAndVersionStreamVerifierListener).onMissingInSecondaryStream(argThat(hasIdAndVersion("1",1)));
+ verify(idAndVersionStreamVerifierListener).onMissingInSecondaryStream(argThat(hasIdAndVersion("1", 1)));
}
@Test
@@ -98,7 +125,7 @@ public void shouldReportMissingItemsAtTheStartOfThePrimaryStream() {
streamOf(item(2), item(3), item(4)),
streamOf(item(1), item(2), item(3), item(4)),
idAndVersionStreamVerifierListener);
- verify(idAndVersionStreamVerifierListener).onMissingInPrimaryStream(argThat(hasIdAndVersion("1",1)));
+ verify(idAndVersionStreamVerifierListener).onMissingInPrimaryStream(argThat(hasIdAndVersion("1", 1)));
}
@Test
@@ -125,7 +152,7 @@ public void shouldReportVersionMisMatches() {
streamOf(item(1), item("2",2), item(3), item(4)),
streamOf(item(1), item("2",5), item(3), item(4)),
idAndVersionStreamVerifierListener);
- verify(idAndVersionStreamVerifierListener).onVersionMisMatch(argThat(hasIdAndVersion("2",2)), argThat(hasIdAndVersion("2",5)));
+ verify(idAndVersionStreamVerifierListener).onVersionMisMatch(argThat(hasIdAndVersion("2", 2)), argThat(hasIdAndVersion("2", 5)));
}
private static JavaIteratorIdAndVersionStream streamOf(IdAndVersion ... items) {

0 comments on commit 54dae06

Please sign in to comment.