Skip to content

Commit 47e1cd7

Browse files
committed
Added tests for poison-pull pattern
1 parent b3d1c2b commit 47e1cd7

File tree

6 files changed

+258
-0
lines changed

6 files changed

+258
-0
lines changed

poison-pill/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@
1414
<artifactId>junit</artifactId>
1515
<scope>test</scope>
1616
</dependency>
17+
<dependency>
18+
<groupId>org.mockito</groupId>
19+
<artifactId>mockito-core</artifactId>
20+
<scope>test</scope>
21+
</dependency>
1722
</dependencies>
1823
</project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.iluwatar.poison.pill;
2+
3+
import org.junit.Test;
4+
import org.mockito.InOrder;
5+
6+
import java.time.LocalDateTime;
7+
8+
import static org.mockito.Mockito.inOrder;
9+
10+
/**
11+
* Date: 12/27/15 - 9:45 PM
12+
*
13+
* @author Jeroen Meulemeester
14+
*/
15+
public class ConsumerTest extends StdOutTest {
16+
17+
@Test
18+
public void testConsume() throws Exception {
19+
final Message[] messages = new Message[]{
20+
createMessage("you", "Hello!"),
21+
createMessage("me", "Hi!"),
22+
Message.POISON_PILL,
23+
createMessage("late_for_the_party", "Hello? Anyone here?"),
24+
};
25+
26+
final MessageQueue queue = new SimpleMessageQueue(messages.length);
27+
for (final Message message : messages) {
28+
queue.put(message);
29+
}
30+
31+
new Consumer("NSA", queue).consume();
32+
33+
final InOrder inOrder = inOrder(getStdOutMock());
34+
inOrder.verify(getStdOutMock()).println("Message [Hello!] from [you] received by [NSA]");
35+
inOrder.verify(getStdOutMock()).println("Message [Hi!] from [me] received by [NSA]");
36+
inOrder.verify(getStdOutMock()).println("Consumer NSA receive request to terminate.");
37+
inOrder.verifyNoMoreInteractions();
38+
39+
}
40+
41+
/**
42+
* Create a new message from the given sender with the given message body
43+
*
44+
* @param sender The sender's name
45+
* @param message The message body
46+
* @return The message instance
47+
*/
48+
private static Message createMessage(final String sender, final String message) {
49+
final SimpleMessage msg = new SimpleMessage();
50+
msg.addHeader(Message.Headers.SENDER, sender);
51+
msg.addHeader(Message.Headers.DATE, LocalDateTime.now().toString());
52+
msg.setBody(message);
53+
return msg;
54+
}
55+
56+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.iluwatar.poison.pill;
2+
3+
import org.junit.Test;
4+
5+
import static com.iluwatar.poison.pill.Message.Headers;
6+
import static com.iluwatar.poison.pill.Message.POISON_PILL;
7+
8+
/**
9+
* Date: 12/27/15 - 10:30 PM
10+
*
11+
* @author Jeroen Meulemeester
12+
*/
13+
public class PoisonMessageTest {
14+
15+
@Test(expected = UnsupportedOperationException.class)
16+
public void testAddHeader() throws Exception {
17+
POISON_PILL.addHeader(Headers.SENDER, "sender");
18+
}
19+
20+
@Test(expected = UnsupportedOperationException.class)
21+
public void testGetHeader() throws Exception {
22+
POISON_PILL.getHeader(Headers.SENDER);
23+
}
24+
25+
@Test(expected = UnsupportedOperationException.class)
26+
public void testGetHeaders() throws Exception {
27+
POISON_PILL.getHeaders();
28+
}
29+
30+
@Test(expected = UnsupportedOperationException.class)
31+
public void testSetBody() throws Exception {
32+
POISON_PILL.setBody("Test message.");
33+
}
34+
35+
@Test(expected = UnsupportedOperationException.class)
36+
public void testGetBody() throws Exception {
37+
POISON_PILL.getBody();
38+
}
39+
40+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.iluwatar.poison.pill;
2+
3+
import org.junit.Test;
4+
import org.mockito.ArgumentCaptor;
5+
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertNotNull;
8+
import static org.junit.Assert.fail;
9+
import static org.mockito.Matchers.eq;
10+
import static org.mockito.Mockito.mock;
11+
import static org.mockito.Mockito.verify;
12+
import static org.mockito.Mockito.verifyNoMoreInteractions;
13+
import static org.mockito.Mockito.verifyZeroInteractions;
14+
15+
/**
16+
* Date: 12/27/15 - 10:32 PM
17+
*
18+
* @author Jeroen Meulemeester
19+
*/
20+
public class ProducerTest {
21+
22+
@Test
23+
public void testSend() throws Exception {
24+
final MqPublishPoint publishPoint = mock(MqPublishPoint.class);
25+
final Producer producer = new Producer("producer", publishPoint);
26+
verifyZeroInteractions(publishPoint);
27+
28+
producer.send("Hello!");
29+
30+
final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
31+
verify(publishPoint).put(messageCaptor.capture());
32+
33+
final Message message = messageCaptor.getValue();
34+
assertNotNull(message);
35+
assertEquals("producer", message.getHeader(Message.Headers.SENDER));
36+
assertNotNull(message.getHeader(Message.Headers.DATE));
37+
assertEquals("Hello!", message.getBody());
38+
39+
verifyNoMoreInteractions(publishPoint);
40+
}
41+
42+
@Test
43+
public void testStop() throws Exception {
44+
final MqPublishPoint publishPoint = mock(MqPublishPoint.class);
45+
final Producer producer = new Producer("producer", publishPoint);
46+
verifyZeroInteractions(publishPoint);
47+
48+
producer.stop();
49+
verify(publishPoint).put(eq(Message.POISON_PILL));
50+
51+
try {
52+
producer.send("Hello!");
53+
fail("Expected 'IllegalStateException' at this point, since the producer has stopped!");
54+
} catch (IllegalStateException e) {
55+
assertNotNull(e);
56+
assertNotNull(e.getMessage());
57+
assertEquals("Producer Hello! was stopped and fail to deliver requested message [producer].",
58+
e.getMessage());
59+
}
60+
61+
verifyNoMoreInteractions(publishPoint);
62+
}
63+
64+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.iluwatar.poison.pill;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Map;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertFalse;
9+
import static org.junit.Assert.assertNotNull;
10+
import static org.junit.Assert.assertTrue;
11+
12+
/**
13+
* Date: 12/27/15 - 10:25 PM
14+
*
15+
* @author Jeroen Meulemeester
16+
*/
17+
public class SimpleMessageTest {
18+
19+
@Test
20+
public void testGetHeaders() {
21+
final SimpleMessage message = new SimpleMessage();
22+
assertNotNull(message.getHeaders());
23+
assertTrue(message.getHeaders().isEmpty());
24+
25+
final String senderName = "test";
26+
message.addHeader(Message.Headers.SENDER, senderName);
27+
assertNotNull(message.getHeaders());
28+
assertFalse(message.getHeaders().isEmpty());
29+
assertEquals(senderName, message.getHeaders().get(Message.Headers.SENDER));
30+
}
31+
32+
@Test(expected = UnsupportedOperationException.class)
33+
public void testUnModifiableHeaders() {
34+
final SimpleMessage message = new SimpleMessage();
35+
final Map<Message.Headers, String> headers = message.getHeaders();
36+
headers.put(Message.Headers.SENDER, "test");
37+
}
38+
39+
40+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.iluwatar.poison.pill;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
6+
import java.io.PrintStream;
7+
8+
import static org.mockito.Mockito.mock;
9+
10+
/**
11+
* Date: 12/10/15 - 8:37 PM
12+
*
13+
* @author Jeroen Meulemeester
14+
*/
15+
public abstract class StdOutTest {
16+
17+
/**
18+
* The mocked standard out {@link PrintStream}, required since some actions don't have any
19+
* influence on accessible objects, except for writing to std-out using {@link System#out}
20+
*/
21+
private final PrintStream stdOutMock = mock(PrintStream.class);
22+
23+
/**
24+
* Keep the original std-out so it can be restored after the test
25+
*/
26+
private final PrintStream stdOutOrig = System.out;
27+
28+
/**
29+
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
30+
*/
31+
@Before
32+
public void setUp() {
33+
System.setOut(this.stdOutMock);
34+
}
35+
36+
/**
37+
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
38+
*/
39+
@After
40+
public void tearDown() {
41+
System.setOut(this.stdOutOrig);
42+
}
43+
44+
/**
45+
* Get the mocked stdOut {@link PrintStream}
46+
*
47+
* @return The stdOut print stream mock, renewed before each test
48+
*/
49+
final PrintStream getStdOutMock() {
50+
return this.stdOutMock;
51+
}
52+
53+
}

0 commit comments

Comments
 (0)