Minor refact for test

This commit is contained in:
selva.andre
2012-07-17 22:34:28 +02:00
parent 949a00de14
commit 0e787e758a
@@ -1,5 +1,6 @@
package org.dna.mqtt.moquette.messaging.spi.impl; package org.dna.mqtt.moquette.messaging.spi.impl;
import org.junit.Before;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import java.util.List; import java.util.List;
import org.dna.mqtt.moquette.messaging.spi.INotifier; import org.dna.mqtt.moquette.messaging.spi.INotifier;
@@ -14,51 +15,34 @@ import static org.mockito.Mockito.*;
*/ */
public class SimpleMessagingTest { public class SimpleMessagingTest {
// public SimpleMessagingTest() { final static String FAKE_CLIENT_ID = "FAKE_123";
// } final static String FAKE_TOPIC = "/news";
// SimpleMessaging messaging;
// @BeforeClass
// public static void setUpClass() throws Exception { @Before
// } public void setUp() {
// messaging = new SimpleMessaging();
// @AfterClass }
// public static void tearDownClass() throws Exception {
// }
//
// @Before
// public void setUp() {
// }
//
// @After
// public void tearDown() {
// }
@Test @Test
public void testSubscribe() { public void testSubscribe() {
String clientID = "FAKE_123";
String topic = "/news";
SimpleMessaging messaging = new SimpleMessaging();
//Exercise //Exercise
messaging.subscribe(clientID, topic, QOSType.MOST_ONE); messaging.subscribe(FAKE_CLIENT_ID, FAKE_TOPIC, QOSType.MOST_ONE);
//Verify //Verify
Subscription expectedSubscription = new Subscription(clientID, topic, QOSType.MOST_ONE); Subscription expectedSubscription = new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC, QOSType.MOST_ONE);
assertTrue(messaging.getSubscriptions().contains(expectedSubscription)); assertTrue(messaging.getSubscriptions().contains(expectedSubscription));
} }
@Test @Test
public void testDoubleSubscribe() { public void testDoubleSubscribe() {
String clientID = "FAKE_123"; messaging.subscribe(FAKE_CLIENT_ID, FAKE_TOPIC, QOSType.MOST_ONE);
String topic = "/news";
SimpleMessaging messaging = new SimpleMessaging();
messaging.subscribe(clientID, topic, QOSType.MOST_ONE);
//Exercise //Exercise
messaging.subscribe(clientID, topic, QOSType.MOST_ONE); messaging.subscribe(FAKE_CLIENT_ID, FAKE_TOPIC, QOSType.MOST_ONE);
//Verify //Verify
Subscription subscription = new Subscription(clientID, topic, QOSType.MOST_ONE); Subscription subscription = new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC, QOSType.MOST_ONE);
assertEquals(1, countMatchingSubscriptions(messaging.getSubscriptions(), subscription)); assertEquals(1, countMatchingSubscriptions(messaging.getSubscriptions(), subscription));
} }
@@ -72,22 +56,18 @@ public class SimpleMessagingTest {
return count; return count;
} }
@Test @Test
public void testPublish() { public void testPublish() {
String clientID = "FAKE_123"; messaging.subscribe(FAKE_CLIENT_ID, FAKE_TOPIC, QOSType.MOST_ONE);
String topic = "/news";
SimpleMessaging messaging = new SimpleMessaging();
messaging.subscribe(clientID, topic, QOSType.MOST_ONE);
INotifier notifier = mock(INotifier.class); INotifier notifier = mock(INotifier.class);
messaging.setNotifier(notifier); messaging.setNotifier(notifier);
//Exercise //Exercise
messaging.publish(topic, "Hello".getBytes(), QOSType.MOST_ONE, false); messaging.publish(FAKE_TOPIC, "Hello".getBytes(), QOSType.MOST_ONE, false);
//Verify //Verify
ArgumentCaptor<byte[]> argument = ArgumentCaptor.forClass(byte[].class); ArgumentCaptor<byte[]> argument = ArgumentCaptor.forClass(byte[].class);
verify(notifier).notify(eq(clientID), eq(topic), any(QOSType.class), argument.capture()); verify(notifier).notify(eq(FAKE_CLIENT_ID), eq(FAKE_TOPIC), any(QOSType.class), argument.capture());
assertEquals("Hello", new String(argument.getValue())); assertEquals("Hello", new String(argument.getValue()));
} }
} }