incubator-nuvem-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jo...@apache.org
Subject svn commit: r1086394 - in /incubator/nuvem/trunk/nuvem-google/src: main/java/org/apache/nuvem/cloud/xmpp/impl/ test/java/org/apache/nuvem/cloud/xmpp/impl/
Date Mon, 28 Mar 2011 20:40:05 GMT
Author: johnp
Date: Mon Mar 28 20:40:05 2011
New Revision: 1086394

URL: http://svn.apache.org/viewvc?rev=1086394&view=rev
Log:
Added necessary javadocs and refactored the code to provide a XMPPConnector implementation
to the XMPPEndPoint which will abstract the process of establishing connection to the XMPPServer
so that there could be different strategies later.

Added:
    incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GoogleXMPPConnector.java
Modified:
    incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GoogleRecieverServlet.java
    incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GoogleXMPPEndPoint.java
    incubator/nuvem/trunk/nuvem-google/src/test/java/org/apache/nuvem/cloud/xmpp/impl/GoogleXMPPEndPointTestCase.java

Modified: incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GoogleRecieverServlet.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GoogleRecieverServlet.java?rev=1086394&r1=1086393&r2=1086394&view=diff
==============================================================================
--- incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GoogleRecieverServlet.java
(original)
+++ incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GoogleRecieverServlet.java
Mon Mar 28 20:40:05 2011
@@ -26,6 +26,7 @@ import javax.servlet.http.HttpServletReq
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.nuvem.cloud.xmpp.api.MessageListener;
 
 import com.google.appengine.api.xmpp.JID;
 import com.google.appengine.api.xmpp.Message;
@@ -34,22 +35,39 @@ import com.google.appengine.api.xmpp.XMP
 
 /**
  * A servlet to recieve XMPP messages from google cloud platform.
+ * <p>
+ * This servlet will recieve the messages posted in the HTTP POST request, parse
+ * the message using the APIs provided by GAE convert the message into nuvem
+ * specific message so that the
+ * {@link MessageListener#listen(org.apache.nuvem.cloud.xmpp.api.Message)} will
+ * be called for the <code>JID</code> the <code>MessageListener</code>
is
+ * registered for.
+ * </p>
  */
 public class GoogleRecieverServlet extends HttpServlet {
-    /**
-     * serial id.
-     */
-    private static final long serialVersionUID = -6839442887435183490L;
+	/**
+	 * serial id.
+	 */
+	private static final long serialVersionUID = -6839442887435183490L;
 
-    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException
{
-        XMPPService xmpp = XMPPServiceFactory.getXMPPService();
-        Message message = xmpp.parseMessage(req);
-        org.apache.nuvem.cloud.xmpp.api.Message nuvemMessage = GoogleXMPPMessageAdapter.toNuvemMessage(message);
-        JID from = message.getFromJid();
+	/**
+	 * Adapts the HTTP Post request into a call to the {@link MessageListener#listen(org.apache.nuvem.cloud.xmpp.api.Message)}.
+	 * @see org.apache.nuvem.cloud.xmpp.api.MessageListener
+	 * @see org.apache.nuvem.cloud.xmpp.api.XMPPEndPoint
+	 */
+	public void doPost(HttpServletRequest req, HttpServletResponse res)
+			throws IOException {
+		XMPPService xmpp = XMPPServiceFactory.getXMPPService();
+		Message message = xmpp.parseMessage(req);
+		org.apache.nuvem.cloud.xmpp.api.Message nuvemMessage = GoogleXMPPMessageAdapter
+				.toNuvemMessage(message);
+		JID from = message.getFromJid();
 
-        // for identifying the listeners, we exclude the resource.
-        String jidExcludingResource = StringUtils.substringBefore(from.getId(), "/");
-        GoogleXMPPEndPoint.getListenerFor(new org.apache.nuvem.cloud.xmpp.api.JID(jidExcludingResource))
-            .listen(nuvemMessage);
-    }
+		// for identifying the listeners, we exclude the resource.
+		String jidExcludingResource = StringUtils.substringBefore(from.getId(),
+				"/");
+		GoogleXMPPEndPoint.getListenerFor(
+				new org.apache.nuvem.cloud.xmpp.api.JID(jidExcludingResource))
+				.listen(nuvemMessage);
+	}
 }

Added: incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GoogleXMPPConnector.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GoogleXMPPConnector.java?rev=1086394&view=auto
==============================================================================
--- incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GoogleXMPPConnector.java
(added)
+++ incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GoogleXMPPConnector.java
Mon Mar 28 20:40:05 2011
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.nuvem.cloud.xmpp.impl;
+
+import java.util.Map;
+import java.util.logging.Logger;
+
+import org.apache.nuvem.cloud.xmpp.api.XMPPConnector;
+
+import com.google.appengine.api.xmpp.XMPPService;
+import com.google.appengine.api.xmpp.XMPPServiceFactory;
+
+/**
+ * Connects to the google XMPP Server inside the GAE platform.
+ * 
+ */
+public class GoogleXMPPConnector implements XMPPConnector<XMPPService> {
+
+	/**
+	 * Logger.
+	 */
+	private static final Logger log = Logger.getLogger(XMPPConnector.class
+			.getName());
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public XMPPService connect() {
+		return XMPPServiceFactory.getXMPPService();
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public XMPPService connect(Map<String, String> connectionProperties) {
+		log.warning("Google App engine doesnt support overriding any connection properties, the
conneciton properties passed as parameters are ignored!");
+		return connect();
+	}
+
+	/**
+	 * {@inheritDoc}}
+	 */
+	public String getDescription() {
+		return "Google Connector";
+	}
+}

Modified: incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GoogleXMPPEndPoint.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GoogleXMPPEndPoint.java?rev=1086394&r1=1086393&r2=1086394&view=diff
==============================================================================
--- incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GoogleXMPPEndPoint.java
(original)
+++ incubator/nuvem/trunk/nuvem-google/src/main/java/org/apache/nuvem/cloud/xmpp/impl/GoogleXMPPEndPoint.java
Mon Mar 28 20:40:05 2011
@@ -22,12 +22,15 @@ package org.apache.nuvem.cloud.xmpp.impl
 
 import java.util.logging.Logger;
 
+import org.apache.commons.lang.Validate;
 import org.apache.nuvem.cloud.xmpp.api.Error;
 import org.apache.nuvem.cloud.xmpp.api.ErrorCode;
 import org.apache.nuvem.cloud.xmpp.api.Status;
+import org.apache.nuvem.cloud.xmpp.api.XMPPConnector;
 import org.apache.nuvem.cloud.xmpp.api.XMPPEndPoint;
 import org.apache.nuvem.cloud.xmpp.common.AbstractXMPPEndPoint;
 import org.oasisopen.sca.annotation.Init;
+import org.oasisopen.sca.annotation.Reference;
 import org.oasisopen.sca.annotation.Scope;
 import org.oasisopen.sca.annotation.Service;
 
@@ -35,92 +38,119 @@ import com.google.appengine.api.xmpp.JID
 import com.google.appengine.api.xmpp.Message;
 import com.google.appengine.api.xmpp.SendResponse;
 import com.google.appengine.api.xmpp.XMPPService;
-import com.google.appengine.api.xmpp.XMPPServiceFactory;
 
+/**
+ * XMPPEndPoint IMplementation for the GAE Platform.
+ * 
+ */
 @Service(XMPPEndPoint.class)
 @Scope("COMPOSITE")
-public class GoogleXMPPEndPoint extends AbstractXMPPEndPoint implements XMPPEndPoint {
+public class GoogleXMPPEndPoint extends AbstractXMPPEndPoint implements
+		XMPPEndPoint {
 
-    private static final Logger log = Logger.getLogger(XMPPEndPoint.class.getName());
-
-    /**
-     * Google's XMPP service.
-     */
-    private XMPPService xmpp;
-
-    @Init
-    public void init() {
-        xmpp = XMPPServiceFactory.getXMPPService();
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public Status sendMessage(org.apache.nuvem.cloud.xmpp.api.Message message) {
-        if (message == null || message.recipient() == null) {
-            throw new IllegalArgumentException("invalid input");
-        }
-
-        Status deliveryStatus = new Status();
-        JID jid = new JID(message.recipient().asString());
-        Message msg = GoogleXMPPMessageAdapter.toGoogleMessage(message);
-
-        SendResponse status = null;
-        if (xmpp.getPresence(jid).isAvailable()) {
-            log.info("user online...");
-            try {
-                status = xmpp.sendMessage(msg);
-            } catch (RuntimeException e) {
-                deliveryStatus.addError(new Error(ErrorCode.UNKNOWN_ERROR,
-                                                  "unknown runtime error while sending message
over XMPP"));
-                return deliveryStatus;
-            }
-            log.info(status.toString());
-        } else {
-            log.info("user offline...");
-            deliveryStatus.addError(new org.apache.nuvem.cloud.xmpp.api.Error(ErrorCode.USER_OFFLINE));
-            return deliveryStatus;
-        }
-        return GoogleStatusAdapter.toStatus(status);
-    }
-
-    @Override
-    public Status invite(org.apache.nuvem.cloud.xmpp.api.JID jid) {
-        if (jid == null)
-            throw new IllegalArgumentException("jid cannot be null");
-        Status deliveryStatus = new Status();
-        JID googleJID = new JID(jid.asString());
-        try {
-            xmpp.sendInvitation(googleJID);
-        } catch (RuntimeException e) {
-            deliveryStatus.addError(new Error(ErrorCode.UNKNOWN_ERROR,
-                                              "unknown runtime error while sending invitiation
over XMPP"));
-            return deliveryStatus;
-        }
-        return deliveryStatus;
-    }
-
-    /**
-     * {@inheritDoc} validation of the input will be done within
-     * {@link org.apache.nuvem.cloud.xmpp.api.JID}
-     */
-    @Override
-    public Status invite(String jid) {
-        return invite(new org.apache.nuvem.cloud.xmpp.api.JID(jid));
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public boolean isPresent(org.apache.nuvem.cloud.xmpp.api.JID jid) {
-        return xmpp.getPresence(new JID(jid.asString())).isAvailable();
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    public boolean isPresent(String id) {
-        return isPresent(new org.apache.nuvem.cloud.xmpp.api.JID(id));
-    }
+	/**
+	 * Logger.
+	 */
+	private static final Logger log = Logger.getLogger(XMPPEndPoint.class
+			.getName());
+
+	@Reference(required = false)
+	protected XMPPConnector<XMPPService> connector;
+
+
+	@Init
+	public void init() {
+		if (connector == null) {
+			log
+					.info("Google XMPPEndPoint is getting initialized with a default connector...");
+			connector = new GoogleXMPPConnector();
+		}
+	}
+
+	/**
+	 * Default constructor.
+	 */
+	public GoogleXMPPEndPoint() {
+
+	}
+
+	/**
+	 * Constructor to allow injection of xmpp service.
+	 * 
+	 * @param xmpp
+	 */
+	public GoogleXMPPEndPoint(XMPPConnector<XMPPService> xmppConnector) {
+		this.connector = xmppConnector;
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public Status sendMessage(org.apache.nuvem.cloud.xmpp.api.Message message) {
+		XMPPService xmpp = connector.connect();
+		if (message == null || message.recipient() == null) {
+			throw new IllegalArgumentException("invalid input");
+		}
+
+		Status deliveryStatus = new Status();
+		JID jid = new JID(message.recipient().asString());
+		Message msg = GoogleXMPPMessageAdapter.toGoogleMessage(message);
+
+		SendResponse status = null;
+		if (xmpp.getPresence(jid).isAvailable()) {
+			log.info("user online...");
+			try {
+				status = xmpp.sendMessage(msg);
+			} catch (RuntimeException e) {
+				deliveryStatus
+						.addError(new Error(ErrorCode.UNKNOWN_ERROR,
+								"unknown runtime error while sending message over XMPP"));
+				return deliveryStatus;
+			}
+			log.info(status.toString());
+		} else {
+			log.info("user offline...");
+			deliveryStatus.addError(new org.apache.nuvem.cloud.xmpp.api.Error(
+					ErrorCode.USER_OFFLINE));
+			return deliveryStatus;
+		}
+		return GoogleStatusAdapter.toStatus(status);
+	}
+
+	private Status invite(org.apache.nuvem.cloud.xmpp.api.JID jid) {
+		if (jid == null)
+			throw new IllegalArgumentException("jid cannot be null");
+		
+		XMPPService xmpp = connector.connect();
+		Status deliveryStatus = new Status();
+		JID googleJID = new JID(jid.asString());
+		try {
+			xmpp.sendInvitation(googleJID);
+		} catch (RuntimeException e) {
+			deliveryStatus
+					.addError(new Error(ErrorCode.UNKNOWN_ERROR,
+							"unknown runtime error while sending invitiation over XMPP"));
+			return deliveryStatus;
+		}
+		return deliveryStatus;
+	}
+
+	/**
+	 * {@inheritDoc} validation of the input will be done within
+	 * {@link org.apache.nuvem.cloud.xmpp.api.JID}
+	 */
+	@Override
+	public Status invite(String jid) {
+		return invite(new org.apache.nuvem.cloud.xmpp.api.JID(jid));
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public boolean isPresent(String id) {
+		Validate.notNull(id);
+		XMPPService xmpp = connector.connect();
+		return xmpp.getPresence(new JID(id)).isAvailable();
+	}
 
 }

Modified: incubator/nuvem/trunk/nuvem-google/src/test/java/org/apache/nuvem/cloud/xmpp/impl/GoogleXMPPEndPointTestCase.java
URL: http://svn.apache.org/viewvc/incubator/nuvem/trunk/nuvem-google/src/test/java/org/apache/nuvem/cloud/xmpp/impl/GoogleXMPPEndPointTestCase.java?rev=1086394&r1=1086393&r2=1086394&view=diff
==============================================================================
--- incubator/nuvem/trunk/nuvem-google/src/test/java/org/apache/nuvem/cloud/xmpp/impl/GoogleXMPPEndPointTestCase.java
(original)
+++ incubator/nuvem/trunk/nuvem-google/src/test/java/org/apache/nuvem/cloud/xmpp/impl/GoogleXMPPEndPointTestCase.java
Mon Mar 28 20:40:05 2011
@@ -19,8 +19,6 @@
 
 package org.apache.nuvem.cloud.xmpp.impl;
 
-import java.lang.reflect.Field;
-
 import junit.framework.Assert;
 
 import org.apache.nuvem.cloud.xmpp.api.ErrorCode;
@@ -28,6 +26,7 @@ import org.apache.nuvem.cloud.xmpp.api.M
 import org.apache.nuvem.cloud.xmpp.api.MessageBuilder;
 import org.apache.nuvem.cloud.xmpp.api.MessageListener;
 import org.apache.nuvem.cloud.xmpp.api.Status;
+import org.apache.nuvem.cloud.xmpp.api.XMPPConnector;
 import org.easymock.EasyMock;
 import org.easymock.IArgumentMatcher;
 import org.junit.Before;
@@ -40,137 +39,148 @@ import com.google.appengine.api.xmpp.XMP
 
 public class GoogleXMPPEndPointTestCase {
 
-    /**
-     * Mocked service.
-     */
-    private XMPPService mockXMPPService;
-
-    private GoogleXMPPEndPoint endPoint = new GoogleXMPPEndPoint();
-
-    @Before
-    public void setUp() throws IllegalArgumentException, IllegalAccessException {
-        mockXMPPService = EasyMock.createMock(XMPPService.class);
-
-        for (Field field : endPoint.getClass().getDeclaredFields()) {
-            if (field.getName().equals("xmpp")) {
-                field.setAccessible(true);
-                field.set(endPoint, mockXMPPService);
-            }
-        }
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testWithNullMessage() {
-        endPoint.sendMessage(null);
-    }
-
-    @Test
-    public void testWithValidMessageButToOfflineRecipient() {
-        Presence presence = new Presence(false);
-        JID jid = new JID("test@test.com");
-        Message message =
-            new MessageBuilder().containing("content").toRecipient("test@test.com").from("from@test.com").build();
-        EasyMock.expect(mockXMPPService.getPresence(jidMatcher(jid))).andReturn(presence);
-        EasyMock.replay(mockXMPPService);
-
-        Status status = endPoint.sendMessage(message);
-        Assert.assertTrue(status.hasErrors());
-        Assert.assertTrue(ErrorCode.USER_OFFLINE == status.errors().get(0).code());
-
-    }
-
-    @Test
-    public void testWithValidMessageAndForOnlineRecipient() {
-        Presence presence = new Presence(true);
-        JID jid = new JID("test@test.com");
-        Message message =
-            new MessageBuilder().containing("content").toRecipient("test@test.com").from("from@test.com").build();
-        EasyMock.expect(mockXMPPService.getPresence(jidMatcher(jid))).andReturn(presence);
-        EasyMock.expect(mockXMPPService.sendMessage(EasyMock.<com.google.appengine.api.xmpp.Message>
anyObject()))
-            .andReturn(new SendResponse());
-        EasyMock.replay(mockXMPPService);
-        Status status = endPoint.sendMessage(message);
-        Assert.assertTrue(!status.hasErrors());
-    }
-
-    private JID jidMatcher(JID jid) {
-        EasyMock.reportMatcher(new GoogleJIDMatcher(jid));
-        return null;
-    }
-
-    /**
-     * Using custom JID matcher as google's JID equals method is not working as
-     * expected.
-     */
-    private static class GoogleJIDMatcher implements IArgumentMatcher {
-        private JID expected;
-
-        public GoogleJIDMatcher(JID expected) {
-            this.expected = expected;
-        }
-
-        public boolean matches(Object obj) {
-            if (!(obj instanceof JID)) {
-                return false;
-            }
-            JID actual = (JID)obj;
-            return actual.getId().equals(expected.getId());
-        }
-
-        @Override
-        public void appendTo(StringBuffer buffer) {
-            buffer.append("eqException(");
-            buffer.append(expected.getClass().getName());
-            buffer.append(" with id:");
-            buffer.append(expected.getId());
-
-        }
-    }
-
-    @Test(expected = IllegalArgumentException.class)
-    public void testInviteWithInvalidJID() {
-        endPoint.invite("");
-    }
-
-    @Test
-    public void testInviteWithValidJID() {
-        JID jid = new JID("test@test.com");
-        mockXMPPService.sendInvitation(jidMatcher(jid));
-        EasyMock.expectLastCall();
-        EasyMock.replay(mockXMPPService);
-        endPoint.invite("test@test.com");
-    }
-
-    @Test
-    public void testRegisterAndRetrieveValidListener() {
-        MessageListener listener = new MessageListener() {
-
-            @Override
-            public void listen(Message message) {
-
-            }
-        };
-        endPoint.registerListner(new org.apache.nuvem.cloud.xmpp.api.JID("test@domain.com"),
listener);
-        MessageListener registeredListener =
-            GoogleXMPPEndPoint.getListenerFor(new org.apache.nuvem.cloud.xmpp.api.JID("test@domain.com"));
-        Assert.assertNotNull(registeredListener);
-    }
-
-    @Test
-    public void testListnerClearing() {
-        MessageListener listener = new MessageListener() {
-
-            @Override
-            public void listen(Message message) {
-
-            }
-        };
-        org.apache.nuvem.cloud.xmpp.api.JID jid = new org.apache.nuvem.cloud.xmpp.api.JID("test@domain.com");
-        endPoint.registerListner(jid, listener);
-        MessageListener registeredListener =
-            GoogleXMPPEndPoint.getListenerFor(new org.apache.nuvem.cloud.xmpp.api.JID("test@domain.com"));
-        Assert.assertNotNull(registeredListener);
-        Assert.assertTrue(endPoint.clearListenersFor(jid));
-        Assert.assertEquals(MessageListener.LOGGING_LISTENER, GoogleXMPPEndPoint.getListenerFor(jid));
-    }
+	/**
+	 * Mocked service.
+	 */
+	private XMPPService mockXMPPService;
+
+	private XMPPConnector<XMPPService> mockedConnector;
+
+	private GoogleXMPPEndPoint endPoint;
+
+	@Before
+	public void setUp() throws IllegalArgumentException, IllegalAccessException {
+		mockedConnector = EasyMock.createMock(XMPPConnector.class);
+		mockXMPPService = EasyMock.createMock(XMPPService.class);
+		EasyMock.expect(mockedConnector.connect()).andReturn(mockXMPPService);
+		endPoint = new GoogleXMPPEndPoint(mockedConnector);
+	}
+
+	@Test(expected = IllegalArgumentException.class)
+	public void testWithNullMessage() {
+		endPoint.sendMessage(null);
+	}
+
+	@Test
+	public void testWithValidMessageButToOfflineRecipient() {
+		Presence presence = new Presence(false);
+		JID jid = new JID("test@test.com");
+		Message message = new MessageBuilder().containing("content")
+				.toRecipient("test@test.com").from("from@test.com").build();
+		EasyMock.expect(mockXMPPService.getPresence(jidMatcher(jid)))
+				.andReturn(presence);
+		EasyMock.replay(mockXMPPService);
+		EasyMock.replay(mockedConnector);
+
+		Status status = endPoint.sendMessage(message);
+		Assert.assertTrue(status.hasErrors());
+		Assert.assertTrue(ErrorCode.USER_OFFLINE == status.errors().get(0)
+				.code());
+
+	}
+
+	@Test
+	public void testWithValidMessageAndForOnlineRecipient() {
+		Presence presence = new Presence(true);
+		JID jid = new JID("test@test.com");
+		Message message = new MessageBuilder().containing("content")
+				.toRecipient("test@test.com").from("from@test.com").build();
+		EasyMock.expect(mockXMPPService.getPresence(jidMatcher(jid)))
+				.andReturn(presence);
+		EasyMock.expect(
+				mockXMPPService.sendMessage(EasyMock
+						.<com.google.appengine.api.xmpp.Message> anyObject()))
+				.andReturn(new SendResponse());
+		EasyMock.replay(mockXMPPService);
+		EasyMock.replay(mockedConnector);
+		Status status = endPoint.sendMessage(message);
+		Assert.assertTrue(!status.hasErrors());
+	}
+
+	private JID jidMatcher(JID jid) {
+		EasyMock.reportMatcher(new GoogleJIDMatcher(jid));
+		return null;
+	}
+
+	/**
+	 * Using custom JID matcher as google's JID equals method is not working as
+	 * expected.
+	 */
+	private static class GoogleJIDMatcher implements IArgumentMatcher {
+		private JID expected;
+
+		public GoogleJIDMatcher(JID expected) {
+			this.expected = expected;
+		}
+
+		public boolean matches(Object obj) {
+			if (!(obj instanceof JID)) {
+				return false;
+			}
+			JID actual = (JID) obj;
+			return actual.getId().equals(expected.getId());
+		}
+
+		@Override
+		public void appendTo(StringBuffer buffer) {
+			buffer.append("eqException(");
+			buffer.append(expected.getClass().getName());
+			buffer.append(" with id:");
+			buffer.append(expected.getId());
+
+		}
+	}
+
+	@Test(expected = IllegalArgumentException.class)
+	public void testInviteWithInvalidJID() {
+		endPoint.invite("");
+	}
+
+	@Test
+	public void testInviteWithValidJID() {
+		JID jid = new JID("test@test.com");
+		mockXMPPService.sendInvitation(jidMatcher(jid));
+		EasyMock.expectLastCall();
+		EasyMock.replay(mockXMPPService);
+		EasyMock.replay(mockedConnector);
+		endPoint.invite("test@test.com");
+	}
+
+	@Test
+	public void testRegisterAndRetrieveValidListener() {
+		MessageListener listener = new MessageListener() {
+
+			@Override
+			public void listen(Message message) {
+
+			}
+		};
+		endPoint.registerListner(new org.apache.nuvem.cloud.xmpp.api.JID(
+				"test@domain.com"), listener);
+		MessageListener registeredListener = GoogleXMPPEndPoint
+				.getListenerFor(new org.apache.nuvem.cloud.xmpp.api.JID(
+						"test@domain.com"));
+		Assert.assertNotNull(registeredListener);
+	}
+
+	@Test
+	public void testListnerClearing() {
+		MessageListener listener = new MessageListener() {
+
+			@Override
+			public void listen(Message message) {
+
+			}
+		};
+		org.apache.nuvem.cloud.xmpp.api.JID jid = new org.apache.nuvem.cloud.xmpp.api.JID(
+				"test@domain.com");
+		endPoint.registerListner(jid, listener);
+		MessageListener registeredListener = GoogleXMPPEndPoint
+				.getListenerFor(new org.apache.nuvem.cloud.xmpp.api.JID(
+						"test@domain.com"));
+		Assert.assertNotNull(registeredListener);
+		Assert.assertTrue(endPoint.clearListenersFor(jid));
+		Assert.assertEquals(MessageListener.LOGGING_LISTENER,
+				GoogleXMPPEndPoint.getListenerFor(jid));
+	}
 }



Mime
View raw message