Author: markt
Date: Thu Mar 14 21:50:00 2013
New Revision: 1456691
URL: http://svn.apache.org/r1456691
Log:
Extract some reuseable code
Added:
tomcat/trunk/test/org/apache/tomcat/websocket/pojo/Util.java (with props)
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=1456691&r1=1456690&r2=1456691&view=diff
==============================================================================
---
tomcat/trunk/test/org/apache/tomcat/websocket/pojo/TestEncodingDecoding.java
(original)
+++
tomcat/trunk/test/org/apache/tomcat/websocket/pojo/TestEncodingDecoding.java
Thu Mar 14 21:50:00 2013
@@ -21,19 +21,16 @@ import java.nio.ByteBuffer;
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.OnMessage;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import javax.websocket.server.ServerEndpoint;
-import javax.websocket.server.ServerEndpointConfig.Configurator;
import org.junit.Assert;
import org.junit.Test;
@@ -41,8 +38,8 @@ import org.junit.Test;
import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
-import org.apache.tomcat.websocket.server.WsListener;
-import org.apache.tomcat.websocket.server.WsServerContainer;
+import org.apache.tomcat.websocket.pojo.Util.ServerConfigListener;
+import org.apache.tomcat.websocket.pojo.Util.SingletonConfigurator;
public class TestEncodingDecoding extends TomcatBaseTest {
@@ -50,6 +47,12 @@ public class TestEncodingDecoding extend
@Test
public void test() throws Exception {
+
+ // Set up utility classes
+ Server server = new Server();
+ SingletonConfigurator.setInstance(server);
+ ServerConfigListener.setPojoClazz(Server.class);
+
Tomcat tomcat = getTomcatInstance();
// Must have a real docBase - just use temp
Context ctx =
@@ -59,6 +62,7 @@ public class TestEncodingDecoding extend
WebSocketContainer wsContainer =
ContainerProvider.getWebSocketContainer();
+
tomcat.start();
Client client = new Client();
@@ -69,8 +73,6 @@ public class TestEncodingDecoding extend
msg1.setData(MESSAGE_ONE);
session.getBasicRemote().sendObject(msg1);
- Server server = ServerConfigurator.getServerInstance();
-
// Should not take very long
int i = 0;
while (i < 20) {
@@ -111,14 +113,10 @@ public class TestEncodingDecoding extend
@ServerEndpoint(value="/",
decoders={MsgStringDecoder.class, MsgByteDecoder.class},
encoders={MsgStringEncoder.class, MsgByteEncoder.class},
- configurator=ServerConfigurator.class)
+ configurator=SingletonConfigurator.class)
public static class Server {
private Queue<Object> received = new ConcurrentLinkedQueue<>();
- public Server() {
- System.out.println("Server created");
- }
-
@OnMessage
public MsgString rx(MsgString in) {
received.add(in);
@@ -135,38 +133,6 @@ public class TestEncodingDecoding extend
}
- public static class ServerConfigurator extends Configurator {
-
- private static final Server server = new Server();
-
- @Override
- public <T> T getEndpointInstance(Class<T> clazz)
- throws InstantiationException {
- @SuppressWarnings("unchecked")
- T result = (T) server;
- return result;
- }
-
- public static Server getServerInstance() {
- return server;
- }
- }
-
- public static class ServerConfigListener extends WsListener {
-
- @Override
- public void contextInitialized(ServletContextEvent sce) {
- super.contextInitialized(sce);
- WsServerContainer sc = WsServerContainer.getServerContainer();
- sc.setServletContext(sce.getServletContext());
- try {
- sc.addEndpoint(Server.class);
- } catch (DeploymentException e) {
- throw new IllegalStateException(e);
- }
- }
- }
-
public static class MsgString {
private String data;
Added: tomcat/trunk/test/org/apache/tomcat/websocket/pojo/Util.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/websocket/pojo/Util.java?rev=1456691&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/tomcat/websocket/pojo/Util.java (added)
+++ tomcat/trunk/test/org/apache/tomcat/websocket/pojo/Util.java Thu Mar 14
21:50:00 2013
@@ -0,0 +1,66 @@
+/*
+ * 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.tomcat.websocket.pojo;
+
+import javax.servlet.ServletContextEvent;
+import javax.websocket.DeploymentException;
+import javax.websocket.server.ServerEndpointConfig.Configurator;
+
+import org.apache.tomcat.websocket.server.WsListener;
+import org.apache.tomcat.websocket.server.WsServerContainer;
+
+public class Util {
+
+ public static class ServerConfigListener extends WsListener {
+
+ private static Class<?> pojoClazz;
+
+ public static void setPojoClazz(Class<?> pojoClazz) {
+ ServerConfigListener.pojoClazz = pojoClazz;
+ }
+
+ @Override
+ public void contextInitialized(ServletContextEvent sce) {
+ super.contextInitialized(sce);
+ WsServerContainer sc = WsServerContainer.getServerContainer();
+ sc.setServletContext(sce.getServletContext());
+ try {
+ sc.addEndpoint(pojoClazz);
+ } catch (DeploymentException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+ }
+
+
+ public static class SingletonConfigurator extends Configurator {
+
+ private static Object instance;
+
+ public static void setInstance(Object instance) {
+ SingletonConfigurator.instance = instance;
+ }
+
+ @Override
+ public <T> T getEndpointInstance(Class<T> clazz)
+ throws InstantiationException {
+ @SuppressWarnings("unchecked")
+ T result = (T) instance;
+ return result;
+ }
+ }
+}
Propchange: tomcat/trunk/test/org/apache/tomcat/websocket/pojo/Util.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]