Author: markt
Date: Thu Jun 27 14:47:36 2013
New Revision: 1497386

URL: http://svn.apache.org/r1497386
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=55143
Add a test case.
Patch provided by Niki Dokovski

Modified:
    tomcat/trunk/test/org/apache/tomcat/websocket/pojo/TestEncodingDecoding.java

Modified: 
tomcat/trunk/test/org/apache/tomcat/websocket/pojo/TestEncodingDecoding.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/websocket/pojo/TestEncodingDecoding.java?rev=1497386&r1=1497385&r2=1497386&view=diff
==============================================================================
--- 
tomcat/trunk/test/org/apache/tomcat/websocket/pojo/TestEncodingDecoding.java 
(original)
+++ 
tomcat/trunk/test/org/apache/tomcat/websocket/pojo/TestEncodingDecoding.java 
Thu Jun 27 14:47:36 2013
@@ -16,23 +16,36 @@
  */
 package org.apache.tomcat.websocket.pojo;
 
+import java.io.IOException;
 import java.net.URI;
 import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.Queue;
 import java.util.concurrent.ConcurrentLinkedQueue;
 
+import javax.servlet.ServletContextEvent;
 import javax.websocket.ClientEndpoint;
 import javax.websocket.ContainerProvider;
 import javax.websocket.DecodeException;
 import javax.websocket.Decoder;
+import javax.websocket.DeploymentException;
 import javax.websocket.EncodeException;
 import javax.websocket.Encoder;
+import javax.websocket.Endpoint;
 import javax.websocket.EndpointConfig;
+import javax.websocket.Extension;
+import javax.websocket.MessageHandler;
 import javax.websocket.OnMessage;
 import javax.websocket.Session;
 import javax.websocket.WebSocketContainer;
+import javax.websocket.server.ServerContainer;
 import javax.websocket.server.ServerEndpoint;
+import javax.websocket.server.ServerEndpointConfig;
+
 
 import org.junit.Assert;
 import org.junit.Test;
@@ -44,14 +57,64 @@ import org.apache.catalina.startup.Tomca
 import org.apache.catalina.startup.TomcatBaseTest;
 import org.apache.tomcat.websocket.pojo.TesterUtil.ServerConfigListener;
 import org.apache.tomcat.websocket.pojo.TesterUtil.SingletonConfigurator;
+import org.apache.tomcat.websocket.server.WsListener;
 
 public class TestEncodingDecoding extends TomcatBaseTest {
 
     private static final String MESSAGE_ONE = "message-one";
+    private static final String PATH_PROGRAMMATIC_EP = "/echoProgrammaticEP";
+    private static final String PATH_ANNOTATED_EP = "/echoAnnotatedEP";
+
 
     @Test
-    public void test() throws Exception {
+    public void testProgrammaticEndPoints() throws Exception{
+        Tomcat tomcat = getTomcatInstance();
+        // Must have a real docBase - just use temp
+        Context ctx =
+            tomcat.addContext("", System.getProperty("java.io.tmpdir"));
+        ctx.addApplicationListener(new ApplicationListener(
+                ProgramaticServerEndpointConfig.class.getName(), false));
+        Tomcat.addServlet(ctx, "default", new DefaultServlet());
+        ctx.addServletMapping("/", "default");
+
+        WebSocketContainer wsContainer =
+                ContainerProvider.getWebSocketContainer();
+
+        tomcat.start();
+
+        Client client = new Client();
+        URI uri = new URI("ws://localhost:" + getPort() + 
PATH_PROGRAMMATIC_EP);
+        Session session = wsContainer.connectToServer(client, uri);
+
+        MsgString msg1 = new MsgString();
+        msg1.setData(MESSAGE_ONE);
+        session.getBasicRemote().sendObject(msg1);
+        // Should not take very long
+        int i = 0;
+        while (i < 20) {
+            if (MsgStringMessageHandler.received.size() > 0 &&
+                    client.received.size() > 0) {
+                break;
+            }
+            Thread.sleep(100);
+            i++;
+        }
+
+        // Check messages were received
+        Assert.assertEquals(1, MsgStringMessageHandler.received.size());
+        Assert.assertEquals(1, client.received.size());
+
+        // Check correct messages were received
+        Assert.assertEquals(MESSAGE_ONE,
+                ((MsgString) 
MsgStringMessageHandler.received.peek()).getData());
+        Assert.assertEquals(MESSAGE_ONE,
+                ((MsgString) client.received.peek()).getData());
+        session.close();
+    }
 
+
+    @Test
+    public void testAnnotatedEndPoints() throws Exception {
         // Set up utility classes
         Server server = new Server();
         SingletonConfigurator.setInstance(server);
@@ -69,11 +132,10 @@ public class TestEncodingDecoding extend
         WebSocketContainer wsContainer =
                 ContainerProvider.getWebSocketContainer();
 
-
         tomcat.start();
 
         Client client = new Client();
-        URI uri = new URI("ws://localhost:" + getPort() + "/");
+        URI uri = new URI("ws://localhost:" + getPort() + PATH_ANNOTATED_EP);
         Session session = wsContainer.connectToServer(client, uri);
 
         MsgString msg1 = new MsgString();
@@ -100,20 +162,29 @@ public class TestEncodingDecoding extend
                 ((MsgString) client.received.peek()).getData());
         session.close();
         Thread.sleep(100);
-        
Assert.assertTrue(Server.isLifeCycleEventCalled(MsgStringEncoder.class.getName()+":init"));
-        
Assert.assertTrue(Server.isLifeCycleEventCalled(MsgStringDecoder.class.getName()+":init"));
-        
Assert.assertTrue(Server.isLifeCycleEventCalled(MsgByteEncoder.class.getName()+":init"));
-        
Assert.assertTrue(Server.isLifeCycleEventCalled(MsgByteDecoder.class.getName()+":init"));
-        
Assert.assertTrue(Server.isLifeCycleEventCalled(MsgStringEncoder.class.getName()+":destroy"));
-        
Assert.assertTrue(Server.isLifeCycleEventCalled(MsgStringDecoder.class.getName()+":destroy"));
-        
Assert.assertTrue(Server.isLifeCycleEventCalled(MsgByteEncoder.class.getName()+":destroy"));
-        
Assert.assertTrue(Server.isLifeCycleEventCalled(MsgByteDecoder.class.getName()+":destroy"));
-
+        Assert.assertTrue(Server.isLifeCycleEventCalled(
+                MsgStringEncoder.class.getName()+":init"));
+        Assert.assertTrue(Server.isLifeCycleEventCalled(
+                MsgStringDecoder.class.getName()+":init"));
+        Assert.assertTrue(Server.isLifeCycleEventCalled(
+                MsgByteEncoder.class.getName()+":init"));
+        Assert.assertTrue(Server.isLifeCycleEventCalled(
+                MsgByteDecoder.class.getName()+":init"));
+        Assert.assertTrue(Server.isLifeCycleEventCalled(
+                MsgStringEncoder.class.getName()+":destroy"));
+        Assert.assertTrue(Server.isLifeCycleEventCalled(
+                MsgStringDecoder.class.getName()+":destroy"));
+        Assert.assertTrue(Server.isLifeCycleEventCalled(
+                MsgByteEncoder.class.getName()+":destroy"));
+        Assert.assertTrue(Server.isLifeCycleEventCalled(
+                MsgByteDecoder.class.getName()+":destroy"));
     }
 
+
     @ClientEndpoint(decoders={MsgStringDecoder.class, MsgByteDecoder.class},
             encoders={MsgStringEncoder.class, MsgByteEncoder.class})
     public static class Client {
+
         private Queue<Object> received = new ConcurrentLinkedQueue<>();
 
         @OnMessage
@@ -128,11 +199,12 @@ public class TestEncodingDecoding extend
     }
 
 
-    @ServerEndpoint(value="/",
+    @ServerEndpoint(value=PATH_ANNOTATED_EP,
             decoders={MsgStringDecoder.class, MsgByteDecoder.class},
             encoders={MsgStringEncoder.class, MsgByteEncoder.class},
             configurator=SingletonConfigurator.class)
     public static class Server {
+
         private Queue<Object> received = new ConcurrentLinkedQueue<>();
         static HashMap<String, Boolean> lifeCyclesCalled = new HashMap<>(8);
 
@@ -161,9 +233,65 @@ public class TestEncodingDecoding extend
     }
 
 
+    public static class MsgByteMessageHandler implements
+            MessageHandler.Whole<MsgByte> {
+
+        public static Queue<Object> received = new ConcurrentLinkedQueue<>();
+        private final Session session;
+
+        public MsgByteMessageHandler(Session session) {
+            this.session = session;
+        }
+
+        @Override
+        public void onMessage(MsgByte in) {
+            System.out.println(getClass() + " received");
+            received.add(in);
+            try {
+                MsgByte msg = new MsgByte();
+                msg.setData("got it".getBytes());
+                session.getBasicRemote().sendObject(msg);
+            } catch (IOException | EncodeException e) {
+                throw new IllegalStateException(e);
+            }
+        }
+    }
+
+
+    public static class MsgStringMessageHandler
+            implements MessageHandler.Whole<MsgString>{
+
+        public static Queue<Object> received = new ConcurrentLinkedQueue<>();
+        private final Session session;
+
+        public MsgStringMessageHandler(Session session) {
+            this.session = session;
+        }
+
+        @Override
+        public void onMessage(MsgString in) {
+            received.add(in);
+            try {
+                MsgString msg = new MsgString();
+                msg.setData(MESSAGE_ONE);
+                session.getBasicRemote().sendObject(msg);
+            } catch (IOException | EncodeException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+
+    public static class ProgrammaticEndpoint extends Endpoint {
+       @Override
+       public void onOpen(Session session, EndpointConfig config) {
+           session.addMessageHandler(new MsgStringMessageHandler(session));
+       }
+    }
+
+
     public static class MsgString {
         private String data;
-
         public String getData() { return data; }
         public void setData(String data) { this.data = data; }
     }
@@ -216,7 +344,6 @@ public class TestEncodingDecoding extend
 
     public static class MsgByte {
         private byte[] data;
-
         public byte[] getData() { return data; }
         public void setData(byte[] data) { this.data = data; }
     }
@@ -278,4 +405,62 @@ public class TestEncodingDecoding extend
             return false;
         }
     }
+
+
+    public static class ProgramaticServerEndpointConfig extends WsListener {
+
+        @Override
+        public void contextInitialized(ServletContextEvent sce) {
+            super.contextInitialized(sce);
+            ServerContainer sc =
+                    (ServerContainer) sce.getServletContext().getAttribute(
+                            org.apache.tomcat.websocket.server.Constants.
+                            SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE);
+            try {
+                sc.addEndpoint(new ServerEndpointConfig() {
+                    @Override
+                    public Map<String, Object> getUserProperties() {
+                        return Collections.emptyMap();
+                    }
+                    @Override
+                    public List<Class<? extends Encoder>> getEncoders() {
+                        List<Class<? extends Encoder>> encoders = new 
ArrayList<>(2);
+                        encoders.add(MsgStringEncoder.class);
+                        encoders.add(MsgByteEncoder.class);
+                        return encoders;
+                    }
+                    @Override
+                    public List<Class<? extends Decoder>> getDecoders() {
+                        List<Class<? extends Decoder>> decoders = new 
ArrayList<>(2);
+                        decoders.add(MsgStringDecoder.class);
+                        decoders.add(MsgByteDecoder.class);
+                        return decoders;
+                    }
+                    @Override
+                    public List<String> getSubprotocols() {
+                        return Collections.emptyList();
+                    }
+                    @Override
+                    public String getPath() {
+                        return PATH_PROGRAMMATIC_EP;
+                    }
+                    @Override
+                    public List<Extension> getExtensions() {
+                        return Collections.emptyList();
+                    }
+                    @Override
+                    public Class<?> getEndpointClass() {
+                        return ProgrammaticEndpoint.class;
+                    }
+                    @Override
+                    public Configurator getConfigurator() {
+                        return new ServerEndpointConfig.Configurator() {
+                        };
+                    }
+                });
+            } catch (DeploymentException e) {
+                throw new IllegalStateException(e);
+            }
+        }
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to