Author: markt Date: Wed Dec 10 14:59:38 2014 New Revision: 1644443 URL: http://svn.apache.org/r1644443 Log: Tests cases for https://java.net/jira/browse/WEBSOCKET_SPEC-232
Modified: tomcat/trunk/test/org/apache/tomcat/websocket/TestWsRemoteEndpoint.java tomcat/trunk/test/org/apache/tomcat/websocket/TesterEchoServer.java Modified: tomcat/trunk/test/org/apache/tomcat/websocket/TestWsRemoteEndpoint.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/websocket/TestWsRemoteEndpoint.java?rev=1644443&r1=1644442&r2=1644443&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/tomcat/websocket/TestWsRemoteEndpoint.java (original) +++ tomcat/trunk/test/org/apache/tomcat/websocket/TestWsRemoteEndpoint.java Wed Dec 10 14:59:38 2014 @@ -71,6 +71,16 @@ public class TestWsRemoteEndpoint extend } @Test + public void testWriterZeroLengthAnnotation() throws Exception { + doTestWriter(TesterAnnotatedEndpoint.class, true, ""); + } + + @Test + public void testWriterZeroLengthProgrammatic() throws Exception { + doTestWriter(TesterProgrammaticEndpoint.class, true, ""); + } + + @Test public void testStreamAnnotation() throws Exception { doTestWriter(TesterAnnotatedEndpoint.class, false, TEST_MESSAGE_5K); } @@ -162,20 +172,77 @@ public class TestWsRemoteEndpoint extend int offset = 0; int i = 0; for (String result : results) { - // First may be a fragment - Assert.assertEquals(SEQUENCE.substring(offset, S_LEN), - result.substring(0, S_LEN - offset)); - i = S_LEN - offset; - while (i + S_LEN < result.length()) { - if (!SEQUENCE.equals(result.substring(i, i + S_LEN))) { + if (testMessage.length() == 0) { + Assert.assertEquals(0, result.length()); + } else { + // First may be a fragment + Assert.assertEquals(SEQUENCE.substring(offset, S_LEN), + result.substring(0, S_LEN - offset)); + i = S_LEN - offset; + while (i + S_LEN < result.length()) { + if (!SEQUENCE.equals(result.substring(i, i + S_LEN))) { + Assert.fail(); + } + i += S_LEN; + } + offset = result.length() - i; + if (!SEQUENCE.substring(0, offset).equals(result.substring(i))) { Assert.fail(); } - i += S_LEN; - } - offset = result.length() - i; - if (!SEQUENCE.substring(0, offset).equals(result.substring(i))) { - Assert.fail(); } } } + + @Test + public void testWriterErrorAnnotation() throws Exception { + doTestWriterError(TesterAnnotatedEndpoint.class); + } + + @Test + public void testWriterErrorProgrammatic() throws Exception { + doTestWriterError(TesterProgrammaticEndpoint.class); + } + + private void doTestWriterError(Class<?> clazz) throws Exception { + Tomcat tomcat = getTomcatInstance(); + // No file system docBase required + Context ctx = tomcat.addContext("", null); + ctx.addApplicationListener(TesterEchoServer.Config.class.getName()); + Tomcat.addServlet(ctx, "default", new DefaultServlet()); + ctx.addServletMapping("/", "default"); + + WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer(); + + tomcat.start(); + + Session wsSession; + URI uri = new URI("ws://localhost:" + getPort() + TesterEchoServer.Config.PATH_WRITER_ERROR); + if (Endpoint.class.isAssignableFrom(clazz)) { + @SuppressWarnings("unchecked") + Class<? extends Endpoint> endpointClazz = (Class<? extends Endpoint>) clazz; + wsSession = wsContainer.connectToServer(endpointClazz, Builder.create().build(), uri); + } else { + wsSession = wsContainer.connectToServer(clazz, uri); + } + + CountDownLatch latch = new CountDownLatch(1); + TesterEndpoint tep = (TesterEndpoint) wsSession.getUserProperties().get("endpoint"); + tep.setLatch(latch); + AsyncHandler<?> handler; + handler = new AsyncText(latch); + + wsSession.addMessageHandler(handler); + + // This should trigger the error + wsSession.getBasicRemote().sendText("Start"); + + boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS); + + Assert.assertTrue(latchResult); + + @SuppressWarnings("unchecked") + List<String> messages = (List<String>) handler.getMessages(); + + Assert.assertEquals(0, messages.size()); + } } Modified: tomcat/trunk/test/org/apache/tomcat/websocket/TesterEchoServer.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/websocket/TesterEchoServer.java?rev=1644443&r1=1644442&r2=1644443&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/tomcat/websocket/TesterEchoServer.java (original) +++ tomcat/trunk/test/org/apache/tomcat/websocket/TesterEchoServer.java Wed Dec 10 14:59:38 2014 @@ -37,6 +37,7 @@ public class TesterEchoServer { public static final String PATH_BASIC = "/echoBasic"; public static final String PATH_BASIC_LIMIT_LOW = "/echoBasicLimitLow"; public static final String PATH_BASIC_LIMIT_HIGH = "/echoBasicLimitHigh"; + public static final String PATH_WRITER_ERROR = "/echoWriterError"; @Override public void contextInitialized(ServletContextEvent sce) { @@ -49,12 +50,14 @@ public class TesterEchoServer { sc.addEndpoint(Basic.class); sc.addEndpoint(BasicLimitLow.class); sc.addEndpoint(BasicLimitHigh.class); + sc.addEndpoint(WriterError.class); } catch (DeploymentException e) { throw new IllegalStateException(e); } } } + @ServerEndpoint("/echoAsync") public static class Async { @@ -186,4 +189,24 @@ public class TesterEchoServer { } } + + @ServerEndpoint("/echoWriterError") + public static class WriterError { + + @OnMessage + public void echoTextMessage(Session session, @SuppressWarnings("unused") String msg) { + try { + session.getBasicRemote().getSendWriter(); + // Simulate an error + throw new RuntimeException(); + } catch (IOException e) { + // Should not happen + try { + session.close(); + } catch (IOException e1) { + // Ignore + } + } + } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org