directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject svn commit: r642969 - in /directory/sandbox/slp/src/test: ./ java/ java/org/ java/org/apache/ java/org/apache/directory/ java/org/apache/directory/server/ java/org/apache/directory/server/slp/ java/org/apache/directory/server/slp/messages/
Date Mon, 31 Mar 2008 11:54:27 GMT
Author: rjan
Date: Mon Mar 31 04:54:25 2008
New Revision: 642969

URL: http://svn.apache.org/viewvc?rev=642969&view=rev
Log:
added the message test. Resolves JIRA issue DIRSERVER-1133.

Added:
    directory/sandbox/slp/src/test/
    directory/sandbox/slp/src/test/java/
    directory/sandbox/slp/src/test/java/org/
    directory/sandbox/slp/src/test/java/org/apache/
    directory/sandbox/slp/src/test/java/org/apache/directory/
    directory/sandbox/slp/src/test/java/org/apache/directory/server/
    directory/sandbox/slp/src/test/java/org/apache/directory/server/slp/
    directory/sandbox/slp/src/test/java/org/apache/directory/server/slp/messages/
    directory/sandbox/slp/src/test/java/org/apache/directory/server/slp/messages/MessageTest.java

Added: directory/sandbox/slp/src/test/java/org/apache/directory/server/slp/messages/MessageTest.java
URL: http://svn.apache.org/viewvc/directory/sandbox/slp/src/test/java/org/apache/directory/server/slp/messages/MessageTest.java?rev=642969&view=auto
==============================================================================
--- directory/sandbox/slp/src/test/java/org/apache/directory/server/slp/messages/MessageTest.java
(added)
+++ directory/sandbox/slp/src/test/java/org/apache/directory/server/slp/messages/MessageTest.java
Mon Mar 31 04:54:25 2008
@@ -0,0 +1,412 @@
+package org.apache.directory.server.slp.messages;
+
+import java.net.InetSocketAddress;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+
+import org.apache.directory.server.slp.ServiceLocationException;
+import org.apache.directory.server.slp.ServiceType;
+import org.apache.directory.server.slp.ServiceURL;
+import org.apache.directory.server.slp.codec.SLPProtocolCodecFactory;
+import org.apache.mina.common.ConnectFuture;
+import org.apache.mina.common.IdleStatus;
+import org.apache.mina.common.IoHandler;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.filter.LoggingFilter;
+import org.apache.mina.filter.codec.ProtocolCodecFilter;
+import org.apache.mina.transport.socket.nio.DatagramAcceptor;
+import org.apache.mina.transport.socket.nio.DatagramConnector;
+import org.apache.mina.transport.socket.nio.SocketAcceptor;
+import org.apache.mina.transport.socket.nio.SocketConnector;
+
+
+import junit.framework.TestCase;
+
+public class MessageTest extends TestCase implements IoHandler {
+
+    private SocketAcceptor acceptor;
+
+    private DatagramAcceptor udpAcceptor;
+
+    private SocketConnector connector;
+
+    private DatagramConnector udpConnector;
+
+    private IoSession clientSession;
+
+    private final Locale locale = new Locale("en");
+
+    private final String[] attrs = new String[] { "one", "two", "three" };
+
+    private final String[] responders = new String[] { "192.168.1.1",
+            "10.1.9.201" };
+
+    private final String[] scopes = new String[] { "default", "special",
+            "hidden" };
+
+    private final String[] tags = new String[] { "attr1", "attr3" };
+
+    private final ServiceURL url;
+
+    private final ServiceURL[] urls;
+
+    private final String predicate = "(&(resolution>300)(color=true))";
+
+    private final ServiceType[] serviceTypes;
+
+    private List queue = new ArrayList();
+
+    private boolean udp = false;
+
+    public MessageTest() throws ServiceLocationException {
+        url = new ServiceURL("service:test://localhost",
+                ServiceURL.LIFETIME_DEFAULT);
+        urls = new ServiceURL[] {
+                new ServiceURL("service:test://localhost",
+                        ServiceURL.LIFETIME_DEFAULT),
+                new ServiceURL("service:test2://localhost",
+                        ServiceURL.LIFETIME_MAXIMUM),
+                new ServiceURL("service:test3://localhost",
+                        ServiceURL.LIFETIME_PERMANENT) };
+
+        serviceTypes = new ServiceType[] {
+                new ServiceType("service:osgi:test"),
+                new ServiceType("service:one:two"),
+                new ServiceType("service:dummy"),
+                new ServiceType("service:none") };
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        acceptor = new SocketAcceptor();
+        acceptor.getFilterChain().addLast("logger", new LoggingFilter());
+        acceptor.getFilterChain().addLast("protocol",
+                new ProtocolCodecFilter(new SLPProtocolCodecFactory()));
+        acceptor.bind(new InetSocketAddress(10000), this);
+
+        udpAcceptor = new DatagramAcceptor();
+        udpAcceptor.getFilterChain().addLast("logger", new LoggingFilter());
+        udpAcceptor.getFilterChain().addLast("protocol",
+                new ProtocolCodecFilter(new SLPProtocolCodecFactory()));
+        udpAcceptor.bind(new InetSocketAddress(10000), this);
+
+        connector = new SocketConnector();
+        connector.getFilterChain().addLast("logger", new LoggingFilter());
+        connector.getFilterChain().addLast("protocol",
+                new ProtocolCodecFilter(new SLPProtocolCodecFactory()));
+        udpConnector = new DatagramConnector();
+        udpConnector.getFilterChain().addLast("logger", new LoggingFilter());
+        udpConnector.getFilterChain().addLast("protocol",
+                new ProtocolCodecFilter(new SLPProtocolCodecFactory()));
+
+        final ConnectFuture connectFuture;
+        if (udp) {
+            connectFuture = udpConnector.connect(new InetSocketAddress(
+                    "localhost", 10000), this);
+        } else {
+            connectFuture = connector.connect(new InetSocketAddress(
+                    "localhost", 10000), this);
+        }
+        while (!connectFuture.isConnected()) {
+
+        }
+        clientSession = connectFuture.getSession();
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        acceptor.unbindAll();
+    }
+
+    public void testAttributeReplyMessage() throws InterruptedException {
+        final AttributeReplyMessage msg = new AttributeReplyMessage();
+        msg.setLocale(locale);
+        msg.setXid((short) 100);
+        msg.setAttributes(attrs);
+        msg.setErrorCode((short) 1);
+        clientSession.write(msg);
+
+        final AbstractSLPMessage msg2 = waitForMessage();
+
+        assertTrue(msg2 instanceof AttributeReplyMessage);
+        final AttributeReplyMessage rcv = (AttributeReplyMessage) msg2;
+
+        assertEquals(rcv.getLocale(), msg.getLocale());
+        assertEquals(rcv.xid, 100);
+        assertEquals(rcv.getAttributes(), msg.getAttributes());
+        assertEquals(rcv.getErrorCode(), 1);
+    }
+
+    public void testAttributeRequestMessage() throws Exception {
+        final AttributeRequestMessage msg = new AttributeRequestMessage();
+        msg.setLocale(locale);
+        msg.setXid((short) 100);
+        msg.setPrevResponders(responders);
+        msg.setServiceURL(url);
+        msg.setScopes(scopes);
+        msg.setTags(tags);
+        clientSession.write(msg);
+
+        final AbstractSLPMessage msg2 = waitForMessage();
+        assertTrue(msg2 instanceof AttributeRequestMessage);
+        final AttributeRequestMessage rcv = (AttributeRequestMessage) msg2;
+
+        assertEquals(rcv.getLocale(), msg.getLocale());
+        assertEquals(rcv.xid, 100);
+        assertEquals(rcv.getPrevResponders(), responders);
+        assertEquals(rcv.getServiceUrl(), url);
+        assertEquals(rcv.getScopes(), scopes);
+        assertEquals(rcv.getTags(), tags);
+    }
+
+    public void testServiceAcknowledgementMessage() throws Exception {
+        final ServiceAcknowledgementMessage msg = new ServiceAcknowledgementMessage();
+        msg.setLocale(locale);
+        msg.setXid((short) 1000);
+        msg.setErrorCode((short) 10);
+
+        clientSession.write(msg);
+
+        final AbstractSLPMessage msg2 = waitForMessage();
+        assertTrue(msg2 instanceof ServiceAcknowledgementMessage);
+        final ServiceAcknowledgementMessage rcv = (ServiceAcknowledgementMessage) msg2;
+
+        assertEquals(rcv.getLocale(), msg.getLocale());
+        assertEquals(rcv.xid, 1000);
+        assertEquals(rcv.getErrorCode(), 10);
+    }
+
+    public void testDAAdvertisementMessage() throws Exception {
+        final DAAdvertisementMessage msg = new DAAdvertisementMessage();
+        msg.setLocale(locale);
+        msg.setXid((short) 1000);
+        msg.setErrorCode((short) 10);
+        msg.setAttributes(attrs);
+        msg.setScopes(scopes);
+        msg.setServiceURL(url);
+        msg.setStatelessBootTimestamp(111);
+
+        clientSession.write(msg);
+
+        final AbstractSLPMessage msg2 = waitForMessage();
+        assertTrue(msg2 instanceof DAAdvertisementMessage);
+        final DAAdvertisementMessage rcv = (DAAdvertisementMessage) msg2;
+
+        assertEquals(rcv.getLocale(), msg.getLocale());
+        assertEquals(rcv.xid, 1000);
+        assertEquals(rcv.getErrorCode(), 10);
+        assertEquals(rcv.getAttributes(), attrs);
+        assertEquals(rcv.getScopes(), scopes);
+        assertEquals(rcv.getServiceURL(), url);
+        assertEquals(rcv.getStatelessBootTimestamp(), 111);
+    }
+
+    public void testServiceDeregistrationMessage() throws Exception {
+        final ServiceDeregistrationMessage msg = new ServiceDeregistrationMessage();
+        msg.setLocale(locale);
+        msg.setXid((short) 1000);
+        msg.setScopes(scopes);
+        msg.setServiceURL(url);
+        msg.setTags(tags);
+
+        clientSession.write(msg);
+
+        final AbstractSLPMessage msg2 = waitForMessage();
+        assertTrue(msg2 instanceof ServiceDeregistrationMessage);
+        final ServiceDeregistrationMessage rcv = (ServiceDeregistrationMessage) msg2;
+
+        assertEquals(rcv.getLocale(), msg.getLocale());
+        assertEquals(rcv.xid, 1000);
+        assertEquals(rcv.getScopes(), scopes);
+        assertEquals(rcv.getServiceURL(), url);
+        assertEquals(rcv.getTags(), tags);
+    }
+
+    public void testServiceRegistrationMessage() throws Exception {
+        final ServiceRegistrationMessage msg = new ServiceRegistrationMessage();
+
+        msg.setLocale(locale);
+        msg.setXid((short) 1000);
+        msg.setScopes(scopes);
+        msg.setAttrList(attrs);
+        msg.setServiceType(url.getServiceType());
+        msg.setServiceURL(url);
+
+        clientSession.write(msg);
+
+        final AbstractSLPMessage msg2 = waitForMessage();
+        assertTrue(msg2 instanceof ServiceRegistrationMessage);
+        final ServiceRegistrationMessage rcv = (ServiceRegistrationMessage) msg2;
+
+        assertEquals(rcv.getLocale(), msg.getLocale());
+        assertEquals(rcv.xid, 1000);
+        assertEquals(rcv.getScopes(), scopes);
+        assertEquals(rcv.getAttrList(), attrs);
+        assertEquals(rcv.getServiceType(), url.getServiceType());
+        assertEquals(rcv.getServiceURL(), url);
+    }
+
+    public void testServiceReplyMessage() throws Exception {
+        final ServiceReplyMessage msg = new ServiceReplyMessage();
+
+        msg.setLocale(locale);
+        msg.setXid((short) 1000);
+        msg.setErrorCode((short) 1);
+        msg.setServiceURLs(urls);
+
+        clientSession.write(msg);
+
+        final AbstractSLPMessage msg2 = waitForMessage();
+        assertTrue(msg2 instanceof ServiceReplyMessage);
+        final ServiceReplyMessage rcv = (ServiceReplyMessage) msg2;
+
+        assertEquals(rcv.getLocale(), msg.getLocale());
+        assertEquals(rcv.xid, 1000);
+        assertEquals(rcv.getErrorCode(), 1);
+        assertEquals(rcv.getServiceURLs(), urls);
+    }
+
+    public void testServiceRequestMessage() throws Exception {
+        final ServiceRequestMessage msg = new ServiceRequestMessage();
+
+        msg.setLocale(locale);
+        msg.setXid((short) 1000);
+        msg.setPredicate(predicate);
+        msg.setPrevResponders(responders);
+        msg.setScopes(scopes);
+        msg.setServiceType(url.getServiceType());
+
+        clientSession.write(msg);
+
+        final AbstractSLPMessage msg2 = waitForMessage();
+        assertTrue(msg2 instanceof ServiceRequestMessage);
+        final ServiceRequestMessage rcv = (ServiceRequestMessage) msg2;
+
+        assertEquals(rcv.getLocale(), msg.getLocale());
+        assertEquals(rcv.xid, 1000);
+        assertEquals(rcv.getPredicate(), predicate);
+        assertEquals(rcv.getPrevResponders(), responders);
+        assertEquals(rcv.getScopes(), scopes);
+        assertEquals(rcv.getServiceType(), url.getServiceType());
+    }
+
+    public void testServiceTypeReplyMessage() throws Exception {
+        final ServiceTypeReplyMessage msg = new ServiceTypeReplyMessage();
+
+        msg.setLocale(locale);
+        msg.setXid((short) 1000);
+        msg.setErrorCode((short) 1);
+        msg.setServiceTypes(serviceTypes);
+
+        clientSession.write(msg);
+
+        final AbstractSLPMessage msg2 = waitForMessage();
+        assertTrue(msg2 instanceof ServiceTypeReplyMessage);
+        final ServiceTypeReplyMessage rcv = (ServiceTypeReplyMessage) msg2;
+
+        assertEquals(rcv.getLocale(), msg.getLocale());
+        assertEquals(rcv.xid, 1000);
+        assertEquals(rcv.getErrorCode(), 1);
+        assertEquals(rcv.getServiceTypes(), serviceTypes);
+    }
+
+    public void testServiceTypeRequestMessage() throws Exception {
+        final ServiceTypeRequestMessage msg = new ServiceTypeRequestMessage();
+
+        msg.setLocale(locale);
+        msg.setXid((short) 1000);
+        msg.setPrevResponders(responders);
+        msg.setScopes(scopes);
+        msg.setNamingAuthority("IANA");
+
+        clientSession.write(msg);
+
+        final AbstractSLPMessage msg2 = waitForMessage();
+        assertTrue(msg2 instanceof ServiceTypeRequestMessage);
+        final ServiceTypeRequestMessage rcv = (ServiceTypeRequestMessage) msg2;
+
+        assertEquals(rcv.getLocale(), msg.getLocale());
+        assertEquals(rcv.xid, 1000);
+        assertEquals(rcv.getPrevResponders(), responders);
+        assertEquals(rcv.getScopes(), scopes);
+        assertEquals(rcv.getNamingAuthority(), "IANA");
+    }
+
+    public static void assertEquals(String[] sa1, String[] sa2) {
+        assertEquals(sa1.length, sa2.length);
+        for (int i = 0; i < sa1.length; i++) {
+            assertEquals(sa1[i], sa2[i]);
+        }
+    }
+
+    public static void assertEquals(ServiceURL url1, ServiceURL url2) {
+        assertEquals(url1.toString(), url2.toString());
+    }
+
+    public static void assertEquals(ServiceURL[] urls1, ServiceURL[] urls2) {
+        assertEquals(urls1.length, urls2.length);
+        for (int i = 0; i < urls1.length; i++) {
+            assertEquals(urls1[i], urls2[i]);
+        }
+    }
+
+    public static void assertEquals(ServiceType[] st1, ServiceType[] st2) {
+        assertEquals(st1.length, st2.length);
+        for (int i = 0; i < st1.length; i++) {
+            assertEquals(st1[i].toString(), st2[i].toString());
+        }
+    }
+
+    public void exceptionCaught(IoSession session, Throwable cause)
+            throws Exception {
+        cause.printStackTrace();
+        fail(cause.getMessage());
+    }
+
+    public void messageReceived(IoSession session, Object message)
+            throws Exception {
+        System.out.println("RECEIVED " + message);
+        synchronized (queue) {
+            queue.add(message);
+            queue.notifyAll();
+        }
+
+    }
+
+    public void messageSent(IoSession session, Object message) throws Exception {
+        System.out.println("SENT MESSAGE " + message);
+    }
+
+    public void sessionClosed(IoSession session) throws Exception {
+        // TODO Auto-generated method stub
+
+    }
+
+    public void sessionCreated(IoSession session) throws Exception {
+        // TODO Auto-generated method stub
+
+    }
+
+    public void sessionIdle(IoSession session, IdleStatus status)
+            throws Exception {
+        // TODO Auto-generated method stub
+
+    }
+
+    public void sessionOpened(IoSession session) throws Exception {
+        // TODO Auto-generated method stub
+
+    }
+
+    private final AbstractSLPMessage waitForMessage()
+            throws InterruptedException {
+        synchronized (queue) {
+            while (queue.isEmpty()) {
+                queue.wait();
+            }
+            return (AbstractSLPMessage) queue.remove(0);
+        }
+    }
+
+}



Mime
View raw message