Permalink
Browse files

Merge commit 'stuartf/master'

* commit 'stuartf/master':
  added unit tests for messaging and email
  • Loading branch information...
ieb committed Sep 13, 2009
2 parents 04359a6 + eb5e493 commit 01c5dc950237d22b8993904739f7197035622e14
@@ -117,5 +117,11 @@
<version>1.2</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.sakaiproject.kernel</groupId>
+ <artifactId>org.sakaiproject.kernel.utils</artifactId>
+ <version>0.1-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
@@ -82,4 +82,7 @@ public void send(MessageRoutes routes, Event event, Node n) {
}
}
+ protected void bindEventAdmin(EventAdmin eventAdmin) {
+ this.eventAdmin = eventAdmin;
+ }
}
@@ -0,0 +1,58 @@
+package org.sakaiproject.kernel.email.outgoing;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.expectLastCall;
+import static org.easymock.EasyMock.isA;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
+import org.sakaiproject.kernel.api.message.MessageConstants;
+import org.sakaiproject.kernel.api.message.MessageRoutes;
+import org.sakaiproject.kernel.message.listener.MessageRoutesImpl;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+
+public class EmailMessageHandlerTest {
+
+ private EmailMessageHandler emh;
+
+ @Before
+ public void setup() {
+ EventAdmin eventAdmin = createMock(EventAdmin.class);
+ eventAdmin.postEvent(isA(Event.class));
+ expectLastCall();
+
+ replay(eventAdmin);
+
+ emh = new EmailMessageHandler();
+ emh.bindEventAdmin(eventAdmin);
+ }
+
+ @Test
+ public void testSend() throws Exception {
+ Property prop = createMock(Property.class);
+ expect(prop.getString()).andReturn("smtp:foo@localhost");
+
+ Node node = createMock(Node.class);
+ expect(node.getProperty(MessageConstants.PROP_SAKAI_TO)).andReturn(prop);
+ expect(node.getPath()).andReturn("/foo");
+
+ replay(prop, node);
+ MessageRoutes routes = new MessageRoutesImpl(node);
+
+ emh.send(routes, null, node);
+ verify(prop, node);
+ }
+
+ @Test
+ public void testGetType() {
+ assertEquals(MessageConstants.TYPE_EMAIL, emh.getType());
+ }
+}
@@ -0,0 +1,37 @@
+package org.sakaiproject.kernel.message.listener;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import org.junit.Test;
+
+public class MessageRouteImplTest {
+
+ @Test
+ public void testNullRoute() {
+ MessageRouteImpl mri = new MessageRouteImpl(null);
+ assertNull(mri.getRcpt());
+ assertNull(mri.getTransport());
+ }
+
+ @Test
+ public void testEmptyRoute() {
+ MessageRouteImpl mri = new MessageRouteImpl("");
+ assertEquals("internal", mri.getTransport());
+ assertEquals("", mri.getRcpt());
+ }
+
+ @Test
+ public void testInternalRoute() {
+ MessageRouteImpl mri = new MessageRouteImpl(":admin");
+ assertEquals("internal", mri.getTransport());
+ assertEquals("admin", mri.getRcpt());
+ }
+
+ @Test
+ public void testExternalRoute() {
+ MessageRouteImpl mri = new MessageRouteImpl("smtp:admin@localhost");
+ assertEquals("smtp", mri.getTransport());
+ assertEquals("admin@localhost", mri.getRcpt());
+ }
+}
@@ -0,0 +1,28 @@
+package org.sakaiproject.kernel.message.listener;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.sakaiproject.kernel.api.message.MessageConstants;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+
+public class MessageRoutesImplTest {
+
+ @Test
+ public void testConstructWithNode() throws Exception {
+ Property prop = createMock(Property.class);
+ expect(prop.getString()).andReturn("smtp:foo@localhost,smtp:bar@localhost");
+
+ Node node = createMock(Node.class);
+ expect(node.getProperty(MessageConstants.PROP_SAKAI_TO)).andReturn(prop);
+
+ replay(node, prop);
+ MessageRoutesImpl mri = new MessageRoutesImpl(node);
+ assertEquals(2, mri.size());
+ }
+}
@@ -0,0 +1,77 @@
+package org.sakaiproject.kernel.message.listener;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.eq;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.expectLastCall;
+import static org.easymock.EasyMock.isA;
+import static org.easymock.EasyMock.replay;
+
+import org.apache.sling.jcr.resource.JcrResourceConstants;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.service.event.Event;
+import org.sakaiproject.kernel.api.message.MessageConstants;
+import org.sakaiproject.kernel.api.message.MessageRouterManager;
+import org.sakaiproject.kernel.api.message.MessageRoutes;
+import org.sakaiproject.kernel.api.message.MessageTransport;
+
+import java.util.Properties;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+
+public class MessageSentListenerTest {
+ private MessageSentListener msl;
+ private MessageRouterManager messageRouterManager;
+
+ @Before
+ public void setup() throws Exception {
+ Property prop = createMock(Property.class);
+ expect(prop.getString()).andReturn(":admin");
+
+ Node node = createMock(Node.class);
+ expect(node.getProperty(MessageConstants.PROP_SAKAI_TO)).andReturn(prop);
+
+ replay(prop, node);
+
+ messageRouterManager = createMock(MessageRouterManager.class);
+ expect(messageRouterManager.getMessageRouting(isA(Node.class))).andReturn(
+ new MessageRoutesImpl(node));
+
+ replay(messageRouterManager);
+
+ msl = new MessageSentListener();
+ msl.bindMessageRouterManager(messageRouterManager);
+ }
+
+ @After
+ public void cleanup() {
+ msl.unbindMessageRouterManager(messageRouterManager);
+ }
+
+ @Test
+ public void testHandleEvent() throws Exception {
+ Property prop = createMock(Property.class);
+ expect(prop.getString()).andReturn(MessageConstants.SAKAI_MESSAGE_RT);
+
+ Node node = createMock(Node.class);
+ expect(node.getProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY))
+ .andReturn(prop);
+
+ Properties eventProps = new Properties();
+ eventProps.put(MessageConstants.EVENT_LOCATION, node);
+ Event event = new Event("myTopic", eventProps);
+
+ MessageTransport transport = createMock(MessageTransport.class);
+ transport.send(isA(MessageRoutes.class), eq(event), eq(node));
+ expectLastCall();
+
+ replay(prop, node, transport);
+
+ msl.addTransport(transport);
+ msl.handleEvent(event);
+ msl.removeTransport(transport);
+ }
+}

0 comments on commit 01c5dc9

Please sign in to comment.