Added tests for poison-pull pattern
This commit is contained in:
parent
b3d1c2b2ed
commit
47e1cd710c
@ -14,5 +14,10 @@
|
|||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
@ -0,0 +1,56 @@
|
|||||||
|
package com.iluwatar.poison.pill;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.InOrder;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.inOrder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/27/15 - 9:45 PM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public class ConsumerTest extends StdOutTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConsume() throws Exception {
|
||||||
|
final Message[] messages = new Message[]{
|
||||||
|
createMessage("you", "Hello!"),
|
||||||
|
createMessage("me", "Hi!"),
|
||||||
|
Message.POISON_PILL,
|
||||||
|
createMessage("late_for_the_party", "Hello? Anyone here?"),
|
||||||
|
};
|
||||||
|
|
||||||
|
final MessageQueue queue = new SimpleMessageQueue(messages.length);
|
||||||
|
for (final Message message : messages) {
|
||||||
|
queue.put(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
new Consumer("NSA", queue).consume();
|
||||||
|
|
||||||
|
final InOrder inOrder = inOrder(getStdOutMock());
|
||||||
|
inOrder.verify(getStdOutMock()).println("Message [Hello!] from [you] received by [NSA]");
|
||||||
|
inOrder.verify(getStdOutMock()).println("Message [Hi!] from [me] received by [NSA]");
|
||||||
|
inOrder.verify(getStdOutMock()).println("Consumer NSA receive request to terminate.");
|
||||||
|
inOrder.verifyNoMoreInteractions();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new message from the given sender with the given message body
|
||||||
|
*
|
||||||
|
* @param sender The sender's name
|
||||||
|
* @param message The message body
|
||||||
|
* @return The message instance
|
||||||
|
*/
|
||||||
|
private static Message createMessage(final String sender, final String message) {
|
||||||
|
final SimpleMessage msg = new SimpleMessage();
|
||||||
|
msg.addHeader(Message.Headers.SENDER, sender);
|
||||||
|
msg.addHeader(Message.Headers.DATE, LocalDateTime.now().toString());
|
||||||
|
msg.setBody(message);
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.iluwatar.poison.pill;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static com.iluwatar.poison.pill.Message.Headers;
|
||||||
|
import static com.iluwatar.poison.pill.Message.POISON_PILL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/27/15 - 10:30 PM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public class PoisonMessageTest {
|
||||||
|
|
||||||
|
@Test(expected = UnsupportedOperationException.class)
|
||||||
|
public void testAddHeader() throws Exception {
|
||||||
|
POISON_PILL.addHeader(Headers.SENDER, "sender");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = UnsupportedOperationException.class)
|
||||||
|
public void testGetHeader() throws Exception {
|
||||||
|
POISON_PILL.getHeader(Headers.SENDER);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = UnsupportedOperationException.class)
|
||||||
|
public void testGetHeaders() throws Exception {
|
||||||
|
POISON_PILL.getHeaders();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = UnsupportedOperationException.class)
|
||||||
|
public void testSetBody() throws Exception {
|
||||||
|
POISON_PILL.setBody("Test message.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = UnsupportedOperationException.class)
|
||||||
|
public void testGetBody() throws Exception {
|
||||||
|
POISON_PILL.getBody();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
package com.iluwatar.poison.pill;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
import static org.mockito.Matchers.eq;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||||
|
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/27/15 - 10:32 PM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public class ProducerTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSend() throws Exception {
|
||||||
|
final MqPublishPoint publishPoint = mock(MqPublishPoint.class);
|
||||||
|
final Producer producer = new Producer("producer", publishPoint);
|
||||||
|
verifyZeroInteractions(publishPoint);
|
||||||
|
|
||||||
|
producer.send("Hello!");
|
||||||
|
|
||||||
|
final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
|
||||||
|
verify(publishPoint).put(messageCaptor.capture());
|
||||||
|
|
||||||
|
final Message message = messageCaptor.getValue();
|
||||||
|
assertNotNull(message);
|
||||||
|
assertEquals("producer", message.getHeader(Message.Headers.SENDER));
|
||||||
|
assertNotNull(message.getHeader(Message.Headers.DATE));
|
||||||
|
assertEquals("Hello!", message.getBody());
|
||||||
|
|
||||||
|
verifyNoMoreInteractions(publishPoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStop() throws Exception {
|
||||||
|
final MqPublishPoint publishPoint = mock(MqPublishPoint.class);
|
||||||
|
final Producer producer = new Producer("producer", publishPoint);
|
||||||
|
verifyZeroInteractions(publishPoint);
|
||||||
|
|
||||||
|
producer.stop();
|
||||||
|
verify(publishPoint).put(eq(Message.POISON_PILL));
|
||||||
|
|
||||||
|
try {
|
||||||
|
producer.send("Hello!");
|
||||||
|
fail("Expected 'IllegalStateException' at this point, since the producer has stopped!");
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
assertNotNull(e.getMessage());
|
||||||
|
assertEquals("Producer Hello! was stopped and fail to deliver requested message [producer].",
|
||||||
|
e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
verifyNoMoreInteractions(publishPoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.iluwatar.poison.pill;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/27/15 - 10:25 PM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public class SimpleMessageTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetHeaders() {
|
||||||
|
final SimpleMessage message = new SimpleMessage();
|
||||||
|
assertNotNull(message.getHeaders());
|
||||||
|
assertTrue(message.getHeaders().isEmpty());
|
||||||
|
|
||||||
|
final String senderName = "test";
|
||||||
|
message.addHeader(Message.Headers.SENDER, senderName);
|
||||||
|
assertNotNull(message.getHeaders());
|
||||||
|
assertFalse(message.getHeaders().isEmpty());
|
||||||
|
assertEquals(senderName, message.getHeaders().get(Message.Headers.SENDER));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = UnsupportedOperationException.class)
|
||||||
|
public void testUnModifiableHeaders() {
|
||||||
|
final SimpleMessage message = new SimpleMessage();
|
||||||
|
final Map<Message.Headers, String> headers = message.getHeaders();
|
||||||
|
headers.put(Message.Headers.SENDER, "test");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
package com.iluwatar.poison.pill;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
|
||||||
|
import java.io.PrintStream;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/10/15 - 8:37 PM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public abstract class StdOutTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The mocked standard out {@link PrintStream}, required since some actions don't have any
|
||||||
|
* influence on accessible objects, except for writing to std-out using {@link System#out}
|
||||||
|
*/
|
||||||
|
private final PrintStream stdOutMock = mock(PrintStream.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Keep the original std-out so it can be restored after the test
|
||||||
|
*/
|
||||||
|
private final PrintStream stdOutOrig = System.out;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
|
||||||
|
*/
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
System.setOut(this.stdOutMock);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
|
||||||
|
*/
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
System.setOut(this.stdOutOrig);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the mocked stdOut {@link PrintStream}
|
||||||
|
*
|
||||||
|
* @return The stdOut print stream mock, renewed before each test
|
||||||
|
*/
|
||||||
|
final PrintStream getStdOutMock() {
|
||||||
|
return this.stdOutMock;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user