INT-1580 cleaned up Roster endpoint, added tests

This commit is contained in:
Oleg Zhurakousky
2010-11-03 23:20:05 -04:00
parent 50d97f12cc
commit b1e1aa2460
21 changed files with 117 additions and 78 deletions

View File

@@ -123,7 +123,7 @@ public class XmppNamespaceHandler extends NamespaceHandlerSupport {
Assert.hasText(connectionName, "'xmpp-connection' must be defined");
builder.addPropertyReference("xmppConnection", connectionName);
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "channel", "requestChannel");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "extract-payload");
//IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "extract-payload");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "auto-startup");
}
}

View File

@@ -112,7 +112,6 @@ public class XmppMessageDrivenEndpoint extends AbstractEndpoint {
@Override
protected void doStart() {
Assert.isTrue(this.initialized, this.getComponentType() + " must be initialized");
logger.debug("start: " + xmppConnection.isConnected() + ":" + xmppConnection.isAuthenticated());
xmppConnection.addPacketListener(this.packetListener, null);
}

View File

@@ -13,9 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.xmpp.presence;
import java.util.Collection;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
@@ -23,14 +24,12 @@ import org.apache.commons.logging.LogFactory;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;
import org.springframework.context.Lifecycle;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.mapping.InboundMessageMapper;
import java.util.Collection;
import org.springframework.util.Assert;
/**
@@ -41,11 +40,10 @@ import java.util.Collection;
* @author Josh Long
* @since 2.0
*/
public class XmppRosterEventMessageDrivenEndpoint extends AbstractEndpoint implements Lifecycle {
public class XmppRosterEventMessageDrivenEndpoint extends AbstractEndpoint {
private static final Log logger = LogFactory.getLog(XmppRosterEventMessageDrivenEndpoint.class);
private volatile MessageChannel requestChannel;
private volatile XMPPConnection xmppConnection;
@@ -53,14 +51,17 @@ public class XmppRosterEventMessageDrivenEndpoint extends AbstractEndpoint imple
private InboundMessageMapper<Presence> messageMapper;
private final MessagingTemplate messagingTemplate = new MessagingTemplate();
private final EventForwardingRosterListener rosterListener = new EventForwardingRosterListener();
private volatile boolean initialized;
/**
* This will be injected or configured via a <em>xmpp-connection-factory</em> element.
*
* @param xmppConnection the connection
*/
public void setXmppConnection(final XMPPConnection xmppConnection) {
public void setXmppConnection(XMPPConnection xmppConnection) {
this.xmppConnection = xmppConnection;
}
@@ -71,20 +72,20 @@ public class XmppRosterEventMessageDrivenEndpoint extends AbstractEndpoint imple
this.messagingTemplate.setDefaultChannel(requestChannel);
this.requestChannel = requestChannel;
}
public void setMessageMapper(InboundMessageMapper<Presence> messageMapper) {
this.messageMapper = messageMapper;
}
@Override
protected void doStart() {
logger.debug("start: " + xmppConnection.isConnected() + ":" +
xmppConnection.isAuthenticated());
Assert.isTrue(this.initialized, this.getComponentType() + " must be initialized");
this.xmppConnection.getRoster().addRosterListener(rosterListener);
}
@Override
protected void doStop() {
if (this.xmppConnection.isConnected()) {
logger.debug("shutting down " + XmppRosterEventMessageDrivenEndpoint.class.getName() +
".");
this.xmppConnection.disconnect();
}
this.xmppConnection.getRoster().removeRosterListener(rosterListener);
}
@Override
@@ -94,7 +95,7 @@ public class XmppRosterEventMessageDrivenEndpoint extends AbstractEndpoint imple
}
this.messagingTemplate.afterPropertiesSet();
this.xmppConnection.getRoster().addRosterListener(new EventForwardingRosterListener());
this.initialized = true;
}
/**
@@ -112,10 +113,6 @@ public class XmppRosterEventMessageDrivenEndpoint extends AbstractEndpoint imple
}
}
public void setMessageMapper(InboundMessageMapper<Presence> messageMapper) {
this.messageMapper = messageMapper;
}
/**
* Subscribes to a given {@link org.jivesoftware.smack.Roster}s events and forwards them to components on the bus.
*/

View File

@@ -1,19 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2010 the original author or authors
~
~ Licensed 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.
-->
<beans:beans
xmlns="http://www.springframework.org/schema/integration"
xmlns:beans="http://www.springframework.org/schema/beans"
@@ -30,8 +15,7 @@
<channel id="input"/>
<channel id="output"/>
<xmpp:header-enricher input-channel="input" output-channel="output">
<xmpp:presence-from value="test@example.org"/>
<xmpp:presence-mode value="available"/>

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.xmpp.messages;
package org.springframework.integration.xmpp.ignore;
import org.junit.Ignore;
import org.junit.Test;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.xmpp.messages;
package org.springframework.integration.xmpp.ignore;
import org.junit.Ignore;
import org.junit.Test;

View File

@@ -1,15 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2010 the original author or authors ~ ~ Licensed 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.
-->
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
@@ -26,12 +15,8 @@
http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd">
<beans:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:ignoreUnresolvablePlaceholders="false">
<beans:property name="location">
<beans:bean class="org.springframework.core.io.FileSystemResource">
<beans:constructor-arg value="#{ systemProperties['user.home'] }/Desktop/test.properties"/>
</beans:bean>
</beans:property>
<beans:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<beans:property name="location" value="classpath:test.properties"/>
</beans:bean>
<beans:bean id="xmppRosterEventConsumer" class="org.springframework.integration.xmpp.presence.XmppRosterEventConsumer"/>
@@ -41,10 +26,6 @@
user="${user.1.login}"
password="${user.1.password}"
host="${user.1.host}"
port="${user.1.port}"
resource="${user.1.resource}"
sasl-mechanism-supported="${user.1.sasl.mechanism}"
sasl-mechanism-supported-index="${user.1.sasl.index}"
service-name="${user.1.service}"
/>

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.xmpp.presence;
package org.springframework.integration.xmpp.ignore;
import org.junit.Ignore;
import org.junit.Test;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.xmpp.messages;
package org.springframework.integration.xmpp.ignore;
import org.junit.Ignore;
import org.junit.Test;

View File

@@ -14,11 +14,12 @@
* limitations under the License.
*/
package org.springframework.integration.xmpp.presence;
package org.springframework.integration.xmpp.ignore;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.integration.xmpp.presence.XmppRosterEventMessageSendingHandler;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.xmpp.presence;
package org.springframework.integration.xmpp.ignore;
import org.junit.Ignore;
import org.junit.Test;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.xmpp.messages;
package org.springframework.integration.xmpp.ignore;
import org.jivesoftware.smack.packet.Message;
import org.springframework.integration.annotation.ServiceActivator;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.xmpp.messages;
package org.springframework.integration.xmpp.ignore;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.xmpp.presence;
package org.springframework.integration.xmpp.ignore;
import org.apache.commons.lang.StringUtils;
import org.springframework.integration.Message;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.xmpp.presence;
package org.springframework.integration.xmpp.ignore;
import org.apache.commons.lang.StringUtils;
import org.jivesoftware.smack.packet.Presence;
@@ -25,7 +25,7 @@ import org.springframework.integration.xmpp.XmppHeaders;
/**
* This is used in
* {@link org.springframework.integration.xmpp.presence.OutboundXmppRosterEventsEndpointTests}
* {@link org.springframework.integration.xmpp.ignore.OutboundXmppRosterEventsEndpointTests}
* to produce fake status / presence updates.
*
* @author Josh Long

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed 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.springframework.integration.xmpp.presence;
import static junit.framework.Assert.assertEquals;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.HashSet;
import java.util.Set;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
/**
* @author Oleg Zhurakousky
*
*/
public class XmppRosterEventMessageDrivenEndpointTests {
@Test
public void testEndpointLifecycle(){
final Set<RosterListener> rosterSet = new HashSet<RosterListener>();
XMPPConnection connection = mock(XMPPConnection.class);
Roster roster = mock(Roster.class);
when(connection.getRoster()).thenReturn(roster);
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
rosterSet.add((RosterListener) invocation.getArguments()[0]);
return null;
}
}).when(roster).addRosterListener(Mockito.any(RosterListener.class));
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
rosterSet.remove((RosterListener) invocation.getArguments()[0]);
return null;
}
}).when(roster).removeRosterListener(Mockito.any(RosterListener.class));
XmppRosterEventMessageDrivenEndpoint rosterEndpoint = new XmppRosterEventMessageDrivenEndpoint();
rosterEndpoint.setXmppConnection(connection);
rosterEndpoint.afterPropertiesSet();
assertEquals(0, rosterSet.size());
rosterEndpoint.start();
assertEquals(1, rosterSet.size());
rosterEndpoint.stop();
assertEquals(0, rosterSet.size());
}
@Test(expected=IllegalArgumentException.class)
public void testNonInitializedFailure(){
XmppRosterEventMessageDrivenEndpoint rosterEndpoint = new XmppRosterEventMessageDrivenEndpoint();
rosterEndpoint.start();
}
}

View File

@@ -14,11 +14,11 @@
# limitations under the License.
#
# to be able to run these tests, put this file on your desktop and configure as appropriate
user.2.login=springintegration.eip@gmail.com
user.2.password=spr1ng1p
user.2.host=talk.google.com
user.2.service=gmail.com
#user.2.sasl.mechanism=PLAIN
#user.2.sasl.index=0
#user.2.resource=resource
#user.2.port=5222
user.1.login=user@gmail.com
user.1.password=password
user.1.host=talk.google.com
user.1.service=gmail.com
#user.1.sasl.mechanism=PLAIN
#user.1.sasl.index=0
#user.1.resource=resource
#user.1.port=5222