Created
January 20, 2011 20:48
-
-
Save CliveEvans/788628 to your computer and use it in GitHub Desktop.
Using Expectations in test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.blogspot.swishbob; | |
import static com.blogspot.swishbob.MessageCounter.COMPLETED; | |
import static com.blogspot.swishbob.MessageCounter.REASSIGNED; | |
import static com.blogspot.swishbob.MessageCounter.TIMED_OUT; | |
import static com.blogspot.swishbob.MessageEventType.MESSAGE_COMPLETED; | |
import static com.blogspot.swishbob.MessageEventType.MESSAGE_REASSIGNED; | |
import static com.blogspot.swishbob.MessageEventType.MESSAGE_TIMED_OUT; | |
import static com.blogspot.swishbob.Expectation.expect; | |
import org.junit.Test; | |
public class CountingEventsDemo { | |
private MessageEventHandler eventDrivenObject = new EventDrivenObject(); | |
private MessageEvent sourceEvent; | |
@Test | |
public void shouldOnlyIncrementMessageReassignedCountWhenMessageReassignedEventReceived() throws Exception { | |
givenSourceEvent(MESSAGE_REASSIGNED); | |
eventDrivenObject.onEvent(sourceEvent); | |
expect(REASSIGNED, 1).only(); | |
} | |
@Test | |
public void shouldOnlyIncrementMessageTimedOutCountWhenMessageTimedOutEventReceived() throws Exception { | |
givenSourceEvent(MESSAGE_TIMED_OUT); | |
eventDrivenObject.onEvent(sourceEvent); | |
expect(TIMED_OUT, 1).only(); | |
} | |
@Test | |
public void shouldOnlyIncrementMessageCompletedCountWhenMessageCompleteEventReceived() throws Exception { | |
givenSourceEvent(MESSAGE_COMPLETED); | |
eventDrivenObject.onEvent(sourceEvent); | |
expect(COMPLETED, 1).only(); | |
} | |
private void givenSourceEvent(MessageEventType type) { | |
sourceEvent = new MessageEvent(type); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.blogspot.swishbob; | |
public class EventDrivenObject implements MessageEventHandler { | |
@Override | |
public void onEvent(MessageEvent sourceEvent) { | |
} | |
@Override | |
public long getAssignedMessagesCount() { | |
return 0; | |
} | |
@Override | |
public long getCompletedMessagesCount() { | |
return 0; | |
} | |
@Override | |
public long getConsumedMessagesCount() { | |
return 0; | |
} | |
@Override | |
public long getReassignedMessagesCount() { | |
return 0; | |
} | |
@Override | |
public long getTimedOutMessagesCount() { | |
return 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.blogspot.swishbob; | |
import java.util.HashMap; | |
import java.util.Map; | |
class Expectation { | |
private final Map<MessageCounter, Integer> expectedCounts = new HashMap<MessageCounter, Integer>(); | |
private final MessageEventHandler eventHandler; | |
public static Expectation expect(final MessageCounter assignedMessages, final int count) { | |
return new Expectation(eventDrivenObject).and(assignedMessages, count); | |
} | |
private Expectation(MessageEventHandler eventHandler) { | |
this.eventHandler = eventDrivenObject; | |
for (MessageCounter type : MessageCounter.values()) { | |
expectedCounts.put(type, 0); | |
} | |
} | |
public Expectation and(final MessageCounter messageType, final int count) { | |
expectedCounts.put(messageType, count); | |
return this; | |
} | |
public void only() { | |
for (MessageCounter type : expectedCounts.keySet()) { | |
type.verifyExpected(expectedCounts.get(type), eventHandler); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void shouldOnlyIncrementMessageReassignedCountWhenMessageReassignedEventReceived() throws Exception { | |
givenSourceEvent(MESSAGE_REASSIGNED); | |
eventDrivenObject.onEvent(sourceEvent); | |
expect(MessageCounter.REASSIGNED, 1).only(); | |
} | |
private void givenSourceEvent(MessageEventType type) { | |
sourceEvent = new MessageEvent(type); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.blogspot.swishbob; | |
import static org.junit.Assert.assertEquals; | |
enum MessageCounter { | |
ASSIGNED { | |
@Override | |
protected long getCountFrom(MessageEventHandler eventHandler) { | |
return eventHandler.getAssignedMessagesCount(); | |
} | |
}, | |
COMPLETED { | |
@Override | |
protected long getCountFrom(MessageEventHandler eventHandler) { | |
return eventHandler.getCompletedMessagesCount(); | |
} | |
} , | |
CONSUMED { | |
@Override | |
protected long getCountFrom(MessageEventHandler eventHandler) { | |
return eventHandler.getConsumedMessagesCount(); | |
} | |
}, | |
REASSIGNED { | |
@Override | |
protected long getCountFrom(MessageEventHandler eventHandler) { | |
return eventHandler.getReassignedMessagesCount(); | |
} | |
}, | |
TIMED_OUT { | |
@Override | |
protected long getCountFrom(MessageEventHandler eventHandler) { | |
return eventHandler.getTimedOutMessagesCount(); | |
} | |
}; | |
public void verifyExpected(int count, MessageEventHandler eventHandler) { | |
assertEquals("Count for event type " + this + " was wrong", count, getCountFrom(eventHandler)); | |
} | |
protected abstract long getCountFrom(MessageEventHandler eventHandler); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.blogspot.swishbob; | |
public class MessageEvent { | |
private final MessageEventType type; | |
public MessageEvent(MessageEventType type) { | |
this.type = type; | |
} | |
public MessageEventType getType() { | |
return type; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.blogspot.swishbob; | |
public interface MessageEventHandler { | |
public abstract void onEvent(MessageEvent sourceEvent); | |
public abstract long getAssignedMessagesCount(); | |
public abstract long getCompletedMessagesCount(); | |
public abstract long getConsumedMessagesCount(); | |
public abstract long getReassignedMessagesCount(); | |
public abstract long getTimedOutMessagesCount(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.blogspot.swishbob; | |
public enum MessageEventType { | |
MESSAGE_REASSIGNED, | |
MESSAGE_ASSIGNED, | |
MESSAGE_TIMED_OUT, | |
MESSAGE_COMPLETED | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment