You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by cm...@apache.org on 2012/01/06 11:24:36 UTC
svn commit: r1228060 [2/2] - in /camel/trunk/components/camel-websocket: ./
src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/
src/main/java/org/apache/camel/ src/main/java/org/apache/camel/component/
src/main/java/org/apache/ca...
Added: camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketComponentServletTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketComponentServletTest.java?rev=1228060&view=auto
==============================================================================
--- camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketComponentServletTest.java (added)
+++ camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketComponentServletTest.java Fri Jan 6 10:24:34 2012
@@ -0,0 +1,112 @@
+/**
+ * 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.camel.component.websocket;
+
+import javax.servlet.http.HttpServletRequest;
+import org.eclipse.jetty.websocket.WebSocket;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InOrder;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Mockito.inOrder;
+import static org.mockito.Mockito.times;;
+
+/**
+ *
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class WebsocketComponentServletTest {
+
+ private static final String PROTOCOL = "ws";
+ private static final String MESSAGE = "message";
+ private static final String CONNECTION_KEY = "random-connection-key";
+
+ @Mock
+ private WebsocketConsumer consumer;
+
+ @Mock
+ private NodeSynchronization sync;
+
+ @Mock
+ private HttpServletRequest request;
+
+ private WebsocketComponentServlet websocketComponentServlet;
+
+ /**
+ * @throws Exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ websocketComponentServlet = new WebsocketComponentServlet(sync);
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketComponentServlet#getConsumer()} .
+ */
+ @Test
+ public void testGetConsumer() {
+ assertNull(websocketComponentServlet.getConsumer());
+ websocketComponentServlet.setConsumer(consumer);
+ assertEquals(consumer, websocketComponentServlet.getConsumer());
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketComponentServlet#setConsumer(org.apache.camel.component.websocket.WebsocketConsumer)} .
+ */
+ @Test
+ public void testSetConsumer() {
+ testGetConsumer();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketComponentServlet#doWebSocketConnect(javax.servlet.http.HttpServletRequest, String)} .
+ */
+ @Test
+ public void testDoWebSocketConnect() {
+ websocketComponentServlet.setConsumer(consumer);
+ WebSocket webSocket = websocketComponentServlet.doWebSocketConnect(request, PROTOCOL);
+ assertNotNull(webSocket);
+ assertEquals(DefaultWebsocket.class, webSocket.getClass());
+ DefaultWebsocket defaultWebsocket = (DefaultWebsocket) webSocket;
+ defaultWebsocket.setConnectionKey(CONNECTION_KEY);
+ defaultWebsocket.onMessage(MESSAGE);
+ InOrder inOrder = inOrder(consumer, sync, request);
+ inOrder.verify(consumer, times(1)).sendExchange(CONNECTION_KEY, MESSAGE);
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketComponentServlet#doWebSocketConnect(javax.servlet.http.HttpServletRequest, String)} .
+ */
+ @Test
+ public void testDoWebSocketConnectConsumerIsNull() {
+ WebSocket webSocket = websocketComponentServlet.doWebSocketConnect(request, PROTOCOL);
+ assertNotNull(webSocket);
+ assertEquals(DefaultWebsocket.class, webSocket.getClass());
+ DefaultWebsocket defaultWebsocket = (DefaultWebsocket) webSocket;
+ defaultWebsocket.setConnectionKey(CONNECTION_KEY);
+ defaultWebsocket.onMessage(MESSAGE);
+ InOrder inOrder = inOrder(consumer, sync, request);
+ inOrder.verifyNoMoreInteractions();
+ }
+}
Added: camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketComponentTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketComponentTest.java?rev=1228060&view=auto
==============================================================================
--- camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketComponentTest.java (added)
+++ camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketComponentTest.java Fri Jan 6 10:24:34 2012
@@ -0,0 +1,265 @@
+/**
+ * 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.camel.component.websocket;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.eclipse.jetty.servlet.ServletHolder;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.InOrder;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.inOrder;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.when;
+
+/**
+ *
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class WebsocketComponentTest {
+
+ private static final String PATH_ONE = "foo";
+ private static final String PATH_TWO = "bar";
+ private static final String PATH_SPEC_ONE = "/" + PATH_ONE + "/*";
+ private static final String PATH_SPEC_TWO = "/" + PATH_TWO + "/*";
+
+ @Mock
+ private WebsocketConsumer consumer;
+
+ @Mock
+ private NodeSynchronization sync;
+
+ @Mock
+ private WebsocketComponentServlet servlet;
+
+ @Mock
+ private Map<String, WebsocketComponentServlet> servlets;
+
+ @Mock
+ private ServletContextHandler handler;
+
+ @Mock
+ private CamelContext camelContext;
+
+ private WebsocketComponent component;
+
+ /**
+ * @throws Exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ component = new WebsocketComponent();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketComponent#createContext()} .
+ */
+ @Test
+ public void testCreateContext() {
+ ServletContextHandler handler = component.createContext();
+ assertNotNull(handler);
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketComponent#createServer(org.eclipse.jetty.servlet.ServletContextHandler, String, int, String)} .
+ */
+ @Test
+ public void testCreateServerWithoutStaticContent() {
+ ServletContextHandler handler = component.createContext();
+ Server server = component.createServer(handler, "localhost", 1988, null);
+ assertEquals(1, server.getConnectors().length);
+ assertEquals("localhost", server.getConnectors()[0].getHost());
+ assertEquals(1988, server.getConnectors()[0].getPort());
+ assertFalse(server.getConnectors()[0].isStarted());
+ assertEquals(handler, server.getHandler());
+ assertEquals(1, server.getHandlers().length);
+ assertEquals(handler, server.getHandlers()[0]);
+ assertEquals("/", handler.getContextPath());
+ assertNotNull(handler.getSessionHandler());
+ assertNull(handler.getResourceBase());
+ assertNull(handler.getServletHandler().getHolderEntry("/"));
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketComponent#createServer(org.eclipse.jetty.servlet.ServletContextHandler, String, int, String)} .
+ */
+ @Test
+ public void testCreateServerWithStaticContent() {
+ ServletContextHandler handler = component.createContext();
+ Server server = component.createServer(handler, "localhost", 1988, "public/");
+ assertEquals(1, server.getConnectors().length);
+ assertEquals("localhost", server.getConnectors()[0].getHost());
+ assertEquals(1988, server.getConnectors()[0].getPort());
+ assertFalse(server.getConnectors()[0].isStarted());
+ assertEquals(handler, server.getHandler());
+ assertEquals(1, server.getHandlers().length);
+ assertEquals(handler, server.getHandlers()[0]);
+ assertEquals("/", handler.getContextPath());
+ assertNotNull(handler.getSessionHandler());
+ assertNotNull(handler.getResourceBase());
+ assertTrue(handler.getResourceBase().endsWith("public"));
+ assertNotNull(handler.getServletHandler().getHolderEntry("/"));
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketComponent#createEndpoint(String, String, java.util.Map)} .
+ */
+ @Test
+ public void testCreateEndpoint() throws Exception {
+ Map<String, Object> parameters = new HashMap<String, Object>();
+
+ component.setCamelContext(camelContext);
+
+ Endpoint e1 = component.createEndpoint("websocket://foo", "foo", parameters);
+ Endpoint e2 = component.createEndpoint("websocket://foo", "foo", parameters);
+ Endpoint e3 = component.createEndpoint("websocket://bar", "bar", parameters);
+ assertNotNull(e1);
+ assertNotNull(e1);
+ assertNotNull(e1);
+ assertEquals(e1, e2);
+ assertNotSame(e1, e3);
+ assertNotSame(e2, e3);
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketComponent#setServletConsumer(WebsocketComponentServlet, WebsocketConsumer)} .
+ */
+ @Test
+ public void testSetServletConsumer() throws Exception {
+ when(servlet.getConsumer()).thenReturn(null, null, consumer);
+ InOrder inOrder = inOrder(servlet, consumer, sync);
+ component.setServletConsumer(servlet, null); // null && null
+ inOrder.verify(servlet, times(0)).setConsumer(null);
+ component.setServletConsumer(servlet, consumer); // null && not null
+ inOrder.verify(servlet, times(1)).setConsumer(consumer);
+ component.setServletConsumer(servlet, null); // not null && null
+ inOrder.verify(servlet, times(0)).setConsumer(consumer);
+ component.setServletConsumer(servlet, consumer); // not null && not null
+ inOrder.verify(servlet, times(0)).setConsumer(consumer);
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketComponent#createServlet(WebsocketStore, String, java.util.Map, ServletContextHandler)} .
+ */
+ @Test
+ public void testCreateServlet() throws Exception {
+ component.createServlet(sync, PATH_SPEC_ONE, servlets, handler);
+ InOrder inOrder = inOrder(servlet, consumer, sync, servlets, handler);
+ ArgumentCaptor<WebsocketComponentServlet> servletCaptor = ArgumentCaptor.forClass(WebsocketComponentServlet.class);
+ inOrder.verify(servlets, times(1)).put(eq(PATH_SPEC_ONE), servletCaptor.capture());
+ ArgumentCaptor<ServletHolder> holderCaptor = ArgumentCaptor.forClass(ServletHolder.class);
+ inOrder.verify(handler, times(1)).addServlet(holderCaptor.capture(), eq(PATH_SPEC_ONE));
+ inOrder.verifyNoMoreInteractions();
+ assertEquals(servletCaptor.getValue(), holderCaptor.getValue().getServlet());
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketComponent#createPathSpec(String)} .
+ */
+ @Test
+ public void testCreatePathSpec() {
+ assertEquals(PATH_SPEC_ONE, component.createPathSpec(PATH_ONE));
+ assertEquals(PATH_SPEC_TWO, component.createPathSpec(PATH_TWO));
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketComponent#addServlet(WebsocketStore, WebsocketConsumer, String)} .
+ */
+ @Test
+ public void testAddServletProducersOnly() throws Exception {
+ component.setCamelContext(camelContext);
+ component.setPort(0);
+ component.doStart();
+ WebsocketComponentServlet s1 = component.addServlet(sync, null, PATH_ONE);
+ WebsocketComponentServlet s2 = component.addServlet(sync, null, PATH_TWO);
+ assertNotNull(s1);
+ assertNotNull(s2);
+ assertNotSame(s1, s2);
+ assertNull(s1.getConsumer());
+ assertNull(s2.getConsumer());
+ component.doStop();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketComponent#addServlet(WebsocketStore, WebsocketConsumer, String)} .
+ */
+ @Test
+ public void testAddServletConsumersOnly() throws Exception {
+ component.setCamelContext(camelContext);
+ component.setPort(0);
+ component.doStart();
+ WebsocketComponentServlet s1 = component.addServlet(sync, consumer, PATH_ONE);
+ WebsocketComponentServlet s2 = component.addServlet(sync, consumer, PATH_TWO);
+ assertNotNull(s1);
+ assertNotNull(s2);
+ assertNotSame(s1, s2);
+ assertEquals(consumer, s1.getConsumer());
+ assertEquals(consumer, s2.getConsumer());
+ component.doStop();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketComponent#addServlet(WebsocketStore, WebsocketConsumer, String)} .
+ */
+ @Test
+ public void testAddServletProducerAndConsumer() throws Exception {
+ component.setCamelContext(camelContext);
+ component.setPort(0);
+ component.doStart();
+ WebsocketComponentServlet s1 = component.addServlet(sync, null, PATH_ONE);
+ WebsocketComponentServlet s2 = component.addServlet(sync, consumer, PATH_ONE);
+ assertNotNull(s1);
+ assertNotNull(s2);
+ assertEquals(s1, s2);
+ assertEquals(consumer, s1.getConsumer());
+ component.doStop();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketComponent#addServlet(WebsocketStore, WebsocketConsumer, String)} .
+ */
+ @Test
+ public void testAddServletConsumerAndProducer() throws Exception {
+ component.setCamelContext(camelContext);
+ component.setPort(0);
+ component.doStart();
+ WebsocketComponentServlet s1 = component.addServlet(sync, consumer, PATH_ONE);
+ WebsocketComponentServlet s2 = component.addServlet(sync, null, PATH_ONE);
+ assertNotNull(s1);
+ assertNotNull(s2);
+ assertEquals(s1, s2);
+ assertEquals(consumer, s1.getConsumer());
+ component.doStop();
+ }
+}
Added: camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketConfigurationTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketConfigurationTest.java?rev=1228060&view=auto
==============================================================================
--- camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketConfigurationTest.java (added)
+++ camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketConfigurationTest.java Fri Jan 6 10:24:34 2012
@@ -0,0 +1,71 @@
+/**
+ * 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.camel.component.websocket;
+
+import org.apache.camel.CamelContext;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertEquals;
+
+public class WebsocketConfigurationTest {
+
+ private static final String REMAINING = "foo";
+ private static final String URI = "websocket://" + REMAINING;
+ private static final String PARAMETERS = "org.apache.camel.component.websocket.MemoryWebsocketStore";
+
+ @Mock
+ private WebsocketComponent component;
+
+ @Mock
+ private CamelContext camelContext;
+
+ private WebsocketEndpoint websocketEndpoint;
+
+ private WebsocketConfiguration wsConfig = new WebsocketConfiguration();
+
+ @Before
+ public void setUp() throws Exception {
+ component = new WebsocketComponent();
+ component.setCamelContext(camelContext);
+ }
+
+ @Test
+ public void testParameters() throws Exception {
+
+ assertNull(wsConfig.getGlobalStore());
+
+ wsConfig.setGlobalStore(PARAMETERS);
+
+ assertNotNull(wsConfig.getGlobalStore());
+
+ websocketEndpoint = new WebsocketEndpoint(URI, component, REMAINING, wsConfig);
+
+ assertNotNull(websocketEndpoint);
+ assertNotNull(REMAINING);
+ assertNotNull(wsConfig.getGlobalStore());
+ // System.out.println(URI);
+ // System.out.println(component);
+ // System.out.println(REMAINING);
+ // System.out.println(wsConfig.getGlobalStore());
+
+ }
+
+}
Added: camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketConsumerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketConsumerTest.java?rev=1228060&view=auto
==============================================================================
--- camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketConsumerTest.java (added)
+++ camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketConsumerTest.java Fri Jan 6 10:24:34 2012
@@ -0,0 +1,141 @@
+/**
+ * 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.camel.component.websocket;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.spi.ExceptionHandler;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InOrder;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.inOrder;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.when;
+
+/**
+ *
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class WebsocketConsumerTest {
+
+ private static final String CONNECTION_KEY = "random-connection-key";
+ private static final String MESSAGE = "message";
+
+ @Mock
+ private Endpoint endpoint;
+
+ @Mock
+ private ExceptionHandler exceptionHandler;
+
+ @Mock
+ private Processor processor;
+
+ @Mock
+ private Exchange exchange;
+
+ @Mock
+ private Message outMessage;
+
+ private Exception exception = new Exception("BAD NEWS EVERYONE!");
+
+ private WebsocketConsumer websocketConsumer;
+
+ /**
+ * @throws Exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ websocketConsumer = new WebsocketConsumer(endpoint, processor);
+ websocketConsumer.setExceptionHandler(exceptionHandler);
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketConsumer#sendExchange(String, String)} .
+ */
+ @Test
+ public void testSendExchange() throws Exception {
+ when(endpoint.createExchange()).thenReturn(exchange);
+ when(exchange.getIn()).thenReturn(outMessage);
+
+ websocketConsumer.sendExchange(CONNECTION_KEY, MESSAGE);
+
+ InOrder inOrder = inOrder(endpoint, exceptionHandler, processor, exchange, outMessage);
+ inOrder.verify(endpoint, times(1)).createExchange();
+ inOrder.verify(exchange, times(1)).getIn();
+ inOrder.verify(outMessage, times(1)).setHeader(WebsocketConstants.CONNECTION_KEY, CONNECTION_KEY);
+ inOrder.verify(exchange, times(1)).getIn();
+ inOrder.verify(outMessage, times(1)).setBody(MESSAGE);
+ inOrder.verify(processor, times(1)).process(exchange);
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketConsumer#sendExchange(String, String)} .
+ */
+ @Test
+ public void testSendExchangeWithException() throws Exception {
+ when(endpoint.createExchange()).thenReturn(exchange);
+ when(exchange.getIn()).thenReturn(outMessage);
+ doThrow(exception).when(processor).process(exchange);
+ when(exchange.getException()).thenReturn(exception);
+
+ websocketConsumer.sendExchange(CONNECTION_KEY, MESSAGE);
+
+ InOrder inOrder = inOrder(endpoint, exceptionHandler, processor, exchange, outMessage);
+ inOrder.verify(endpoint, times(1)).createExchange();
+ inOrder.verify(exchange, times(1)).getIn();
+ inOrder.verify(outMessage, times(1)).setHeader(WebsocketConstants.CONNECTION_KEY, CONNECTION_KEY);
+ inOrder.verify(exchange, times(1)).getIn();
+ inOrder.verify(outMessage, times(1)).setBody(MESSAGE);
+ inOrder.verify(processor, times(1)).process(exchange);
+ inOrder.verify(exchange, times(2)).getException();
+ inOrder.verify(exceptionHandler, times(1)).handleException(any(String.class), eq(exchange), eq(exception));
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketConsumer#sendExchange(String, String)} .
+ */
+ @Test
+ public void testSendExchangeWithExchangeExceptionIsNull() throws Exception {
+ when(endpoint.createExchange()).thenReturn(exchange);
+ when(exchange.getIn()).thenReturn(outMessage);
+ doThrow(exception).when(processor).process(exchange);
+ when(exchange.getException()).thenReturn(null);
+
+ websocketConsumer.sendExchange(CONNECTION_KEY, MESSAGE);
+
+ InOrder inOrder = inOrder(endpoint, exceptionHandler, processor, exchange, outMessage);
+ inOrder.verify(endpoint, times(1)).createExchange();
+ inOrder.verify(exchange, times(1)).getIn();
+ inOrder.verify(outMessage, times(1)).setHeader(WebsocketConstants.CONNECTION_KEY, CONNECTION_KEY);
+ inOrder.verify(exchange, times(1)).getIn();
+ inOrder.verify(outMessage, times(1)).setBody(MESSAGE);
+ inOrder.verify(processor, times(1)).process(exchange);
+ inOrder.verify(exchange, times(1)).getException();
+ inOrder.verifyNoMoreInteractions();
+ }
+}
Added: camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketEndpointTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketEndpointTest.java?rev=1228060&view=auto
==============================================================================
--- camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketEndpointTest.java (added)
+++ camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketEndpointTest.java Fri Jan 6 10:24:34 2012
@@ -0,0 +1,105 @@
+/**
+ * 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.camel.component.websocket;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.InOrder;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Matchers.isNull;
+import static org.mockito.Mockito.inOrder;
+import static org.mockito.Mockito.times;
+
+/**
+ *
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class WebsocketEndpointTest {
+
+ private static final String REMAINING = "foo/bar";
+ private static final String URI = "websocket://" + REMAINING;
+
+ @Mock
+ private WebsocketComponent component;
+
+ @Mock
+ private Processor processor;
+
+ private WebsocketEndpoint websocketEndpoint;
+
+ /**
+ * @throws Exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ websocketEndpoint = new WebsocketEndpoint(URI, component, REMAINING, new WebsocketConfiguration());
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketEndpoint#createConsumer(org.apache.camel.Processor)} .
+ */
+ @Test
+ public void testCreateConsumer() throws Exception {
+ Consumer consumer = websocketEndpoint.createConsumer(processor);
+ assertNotNull(consumer);
+ assertEquals(WebsocketConsumer.class, consumer.getClass());
+ InOrder inOrder = inOrder(component, processor);
+ ArgumentCaptor<NodeSynchronization> synchronizationCaptor = ArgumentCaptor.forClass(NodeSynchronization.class);
+ ArgumentCaptor<WebsocketConsumer> consumerCaptor = ArgumentCaptor.forClass(WebsocketConsumer.class);
+ inOrder.verify(component, times(1)).addServlet(synchronizationCaptor.capture(), consumerCaptor.capture(), eq(REMAINING));
+ inOrder.verifyNoMoreInteractions();
+
+ assertEquals(NodeSynchronizationImpl.class, synchronizationCaptor.getValue().getClass());
+
+ assertEquals(consumer, consumerCaptor.getValue());
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketEndpoint#createProducer()} .
+ */
+ @Test
+ public void testCreateProducer() throws Exception {
+ Producer producer = websocketEndpoint.createProducer();
+ assertNotNull(producer);
+ assertEquals(WebsocketProducer.class, producer.getClass());
+ InOrder inOrder = inOrder(component, processor);
+ ArgumentCaptor<NodeSynchronization> synchronizationCaptor = ArgumentCaptor.forClass(NodeSynchronization.class);
+ inOrder.verify(component, times(1)).addServlet(synchronizationCaptor.capture(), (WebsocketConsumer) isNull(), eq(REMAINING));
+ inOrder.verifyNoMoreInteractions();
+
+ assertEquals(NodeSynchronizationImpl.class, synchronizationCaptor.getValue().getClass());
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketEndpoint#isSingleton()} .
+ */
+ @Test
+ public void testIsSingleton() {
+ assertTrue(websocketEndpoint.isSingleton());
+ }
+}
Added: camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketProducerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketProducerTest.java?rev=1228060&view=auto
==============================================================================
--- camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketProducerTest.java (added)
+++ camel/trunk/components/camel-websocket/src/test/java/org/apache/camel/component/websocket/WebsocketProducerTest.java Fri Jan 6 10:24:34 2012
@@ -0,0 +1,397 @@
+/**
+ * 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.camel.component.websocket;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.eclipse.jetty.websocket.WebSocket.Connection;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InOrder;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.inOrder;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.when;
+
+/**
+ *
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class WebsocketProducerTest {
+
+ private static final String MESSAGE = "MESSAGE";
+ private static final String SESSION_KEY = "random-session-key";
+
+ @Mock
+ private Endpoint endpoint;
+
+ @Mock
+ private WebsocketStore store;
+
+ @Mock
+ private Connection connection;
+
+ @Mock
+ private DefaultWebsocket defaultWebsocket1;
+
+ @Mock
+ private DefaultWebsocket defaultWebsocket2;
+
+ @Mock
+ private Exchange exchange;
+
+ @Mock
+ private Message inMessage;
+
+ private IOException exception = new IOException("BAD NEWS EVERYONE!");
+
+ private WebsocketProducer websocketProducer;
+
+ private Collection<DefaultWebsocket> sockets;
+
+ /**
+ * @throws Exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ websocketProducer = new WebsocketProducer(endpoint, store);
+ sockets = Arrays.asList(defaultWebsocket1, defaultWebsocket2);
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketProducer#process(org.apache.camel.Exchange)} .
+ */
+ @Test
+ public void testProcessSingleMessage() throws Exception {
+ when(exchange.getIn()).thenReturn(inMessage);
+ when(inMessage.getBody(String.class)).thenReturn(MESSAGE);
+ when(inMessage.getHeader(WebsocketConstants.SEND_TO_ALL)).thenReturn(null);
+ when(inMessage.getHeader(WebsocketConstants.CONNECTION_KEY, String.class)).thenReturn(SESSION_KEY);
+ when(store.get(SESSION_KEY)).thenReturn(defaultWebsocket1);
+ when(defaultWebsocket1.getConnection()).thenReturn(connection);
+ when(connection.isOpen()).thenReturn(true);
+
+ websocketProducer.process(exchange);
+
+ InOrder inOrder = inOrder(endpoint, store, connection, defaultWebsocket1, defaultWebsocket2, exchange, inMessage);
+ inOrder.verify(exchange, times(1)).getIn();
+ inOrder.verify(inMessage, times(1)).getBody(String.class);
+ inOrder.verify(inMessage, times(1)).getHeader(WebsocketConstants.SEND_TO_ALL);
+ inOrder.verify(inMessage, times(1)).getHeader(WebsocketConstants.CONNECTION_KEY, String.class);
+ inOrder.verify(store, times(1)).get(SESSION_KEY);
+ inOrder.verify(defaultWebsocket1, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).isOpen();
+ inOrder.verify(defaultWebsocket1, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).sendMessage(MESSAGE);
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketProducer#process(org.apache.camel.Exchange)} .
+ */
+ @Test
+ public void testProcessSingleMessageWithException() throws Exception {
+ when(exchange.getIn()).thenReturn(inMessage);
+ when(inMessage.getBody(String.class)).thenReturn(MESSAGE);
+ when(inMessage.getHeader(WebsocketConstants.SEND_TO_ALL)).thenReturn(false);
+ when(inMessage.getHeader(WebsocketConstants.CONNECTION_KEY, String.class)).thenReturn(SESSION_KEY);
+ when(store.get(SESSION_KEY)).thenReturn(defaultWebsocket1);
+ when(defaultWebsocket1.getConnection()).thenReturn(connection);
+ when(connection.isOpen()).thenReturn(true);
+ doThrow(exception).when(connection).sendMessage(MESSAGE);
+
+ try {
+ websocketProducer.process(exchange);
+ fail("Exception expected");
+ } catch (IOException ioe) {
+ assertEquals(exception, ioe);
+ }
+
+ InOrder inOrder = inOrder(endpoint, store, connection, defaultWebsocket1, defaultWebsocket2, exchange, inMessage);
+ inOrder.verify(exchange, times(1)).getIn();
+ inOrder.verify(inMessage, times(1)).getBody(String.class);
+ inOrder.verify(inMessage, times(1)).getHeader(WebsocketConstants.SEND_TO_ALL);
+ inOrder.verify(inMessage, times(1)).getHeader(WebsocketConstants.CONNECTION_KEY, String.class);
+ inOrder.verify(store, times(1)).get(SESSION_KEY);
+ inOrder.verify(defaultWebsocket1, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).isOpen();
+ inOrder.verify(defaultWebsocket1, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).sendMessage(MESSAGE);
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketProducer#process(org.apache.camel.Exchange)} .
+ */
+ @Test
+ public void testProcessMultipleMessages() throws Exception {
+ when(exchange.getIn()).thenReturn(inMessage);
+ when(inMessage.getBody(String.class)).thenReturn(MESSAGE);
+ when(inMessage.getHeader(WebsocketConstants.SEND_TO_ALL)).thenReturn(true);
+ when(store.getAll()).thenReturn(sockets);
+ when(defaultWebsocket1.getConnection()).thenReturn(connection);
+ when(defaultWebsocket2.getConnection()).thenReturn(connection);
+ when(connection.isOpen()).thenReturn(true);
+
+ websocketProducer.process(exchange);
+
+ InOrder inOrder = inOrder(endpoint, store, connection, defaultWebsocket1, defaultWebsocket2, exchange, inMessage);
+ inOrder.verify(exchange, times(1)).getIn();
+ inOrder.verify(inMessage, times(1)).getBody(String.class);
+ inOrder.verify(inMessage, times(1)).getHeader(WebsocketConstants.SEND_TO_ALL);
+ inOrder.verify(store, times(1)).getAll();
+ inOrder.verify(defaultWebsocket1, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).isOpen();
+ inOrder.verify(defaultWebsocket1, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).sendMessage(MESSAGE);
+ inOrder.verify(defaultWebsocket2, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).isOpen();
+ inOrder.verify(defaultWebsocket2, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).sendMessage(MESSAGE);
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketProducer#process(org.apache.camel.Exchange)} .
+ */
+ @Test
+ public void testProcessMultipleMessagesWithExcpetion() throws Exception {
+ when(exchange.getIn()).thenReturn(inMessage);
+ when(inMessage.getBody(String.class)).thenReturn(MESSAGE);
+ when(inMessage.getHeader(WebsocketConstants.SEND_TO_ALL)).thenReturn(true);
+ when(store.getAll()).thenReturn(sockets);
+ when(defaultWebsocket1.getConnection()).thenReturn(connection);
+ when(defaultWebsocket2.getConnection()).thenReturn(connection);
+ doThrow(exception).when(connection).sendMessage(MESSAGE);
+ when(connection.isOpen()).thenReturn(true);
+
+ try {
+ websocketProducer.process(exchange);
+ fail("Exception expected");
+ } catch (Exception e) {
+ assertEquals(exception, e.getCause());
+ }
+
+ InOrder inOrder = inOrder(endpoint, store, connection, defaultWebsocket1, defaultWebsocket2, exchange, inMessage);
+ inOrder.verify(exchange, times(1)).getIn();
+ inOrder.verify(inMessage, times(1)).getBody(String.class);
+ inOrder.verify(inMessage, times(1)).getHeader(WebsocketConstants.SEND_TO_ALL);
+ inOrder.verify(store, times(1)).getAll();
+ inOrder.verify(defaultWebsocket1, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).isOpen();
+ inOrder.verify(defaultWebsocket1, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).sendMessage(MESSAGE);
+ inOrder.verify(defaultWebsocket2, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).isOpen();
+ inOrder.verify(defaultWebsocket2, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).sendMessage(MESSAGE);
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketProducer#process(org.apache.camel.Exchange)} .
+ */
+ @Test
+ public void testProcessSingleMessageNoConnectionKey() throws Exception {
+ when(exchange.getIn()).thenReturn(inMessage);
+ when(inMessage.getBody(String.class)).thenReturn(MESSAGE);
+ when(inMessage.getHeader(WebsocketConstants.SEND_TO_ALL)).thenReturn(false);
+ when(inMessage.getHeader(WebsocketConstants.CONNECTION_KEY, String.class)).thenReturn(null);
+
+ try {
+ websocketProducer.process(exchange);
+ fail("Exception expected");
+ } catch (Exception e) {
+ assertEquals(IllegalArgumentException.class, e.getClass());
+ assertNotNull(e.getMessage());
+ assertNull(e.getCause());
+ }
+
+ InOrder inOrder = inOrder(endpoint, store, connection, defaultWebsocket1, defaultWebsocket2, exchange, inMessage);
+ inOrder.verify(exchange, times(1)).getIn();
+ inOrder.verify(inMessage, times(1)).getBody(String.class);
+ inOrder.verify(inMessage, times(1)).getHeader(WebsocketConstants.SEND_TO_ALL);
+ inOrder.verify(inMessage, times(1)).getHeader(WebsocketConstants.CONNECTION_KEY, String.class);
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketProducer#sendMessage(org.apache.camel.component.websocket.DefaultWebsocket, String)} .
+ */
+ @Test
+ public void testSendMessage() throws Exception {
+ when(defaultWebsocket1.getConnection()).thenReturn(connection);
+ when(connection.isOpen()).thenReturn(true);
+
+ websocketProducer.sendMessage(defaultWebsocket1, MESSAGE);
+
+ InOrder inOrder = inOrder(endpoint, store, connection, defaultWebsocket1, defaultWebsocket2, exchange, inMessage);
+ inOrder.verify(defaultWebsocket1, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).isOpen();
+ inOrder.verify(defaultWebsocket1, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).sendMessage(MESSAGE);
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketProducer#sendMessage(org.apache.camel.component.websocket.DefaultWebsocket, String)} .
+ */
+ @Test
+ public void testSendMessageWebsocketIsNull() throws Exception {
+
+ websocketProducer.sendMessage(null, MESSAGE);
+
+ InOrder inOrder = inOrder(endpoint, store, connection, defaultWebsocket1, defaultWebsocket2, exchange, inMessage);
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketProducer#sendMessage(org.apache.camel.component.websocket.DefaultWebsocket, String)} .
+ */
+ @Test
+ public void testSendMessageConnetionIsClosed() throws Exception {
+ when(defaultWebsocket1.getConnection()).thenReturn(connection);
+ when(connection.isOpen()).thenReturn(false);
+
+ websocketProducer.sendMessage(defaultWebsocket1, MESSAGE);
+
+ InOrder inOrder = inOrder(endpoint, store, connection, defaultWebsocket1, defaultWebsocket2, exchange, inMessage);
+ inOrder.verify(defaultWebsocket1, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).isOpen();
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketProducer#sendMessage(org.apache.camel.component.websocket.DefaultWebsocket, String)} .
+ */
+ @Test
+ public void testSendMessageWithException() throws Exception {
+ when(defaultWebsocket1.getConnection()).thenReturn(connection);
+ when(connection.isOpen()).thenReturn(true);
+ doThrow(exception).when(connection).sendMessage(MESSAGE);
+
+ try {
+ websocketProducer.sendMessage(defaultWebsocket1, MESSAGE);
+ fail("Exception expected");
+ } catch (IOException ioe) {
+ assertEquals(exception, ioe);
+ }
+
+ InOrder inOrder = inOrder(endpoint, store, connection, defaultWebsocket1, defaultWebsocket2, exchange, inMessage);
+ inOrder.verify(defaultWebsocket1, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).isOpen();
+ inOrder.verify(defaultWebsocket1, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).sendMessage(MESSAGE);
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketProducer#isSendToAllSet(Message)} .
+ */
+ @Test
+ public void testIsSendToAllSet() {
+ when(inMessage.getHeader(WebsocketConstants.SEND_TO_ALL)).thenReturn(true, false);
+ assertTrue(websocketProducer.isSendToAllSet(inMessage));
+ assertFalse(websocketProducer.isSendToAllSet(inMessage));
+ InOrder inOrder = inOrder(inMessage);
+ inOrder.verify(inMessage, times(2)).getHeader(WebsocketConstants.SEND_TO_ALL);
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketProducer#isSendToAllSet(Message)} .
+ */
+ @Test
+ public void testIsSendToAllSetHeaderNull() {
+ when(inMessage.getHeader(WebsocketConstants.SEND_TO_ALL)).thenReturn(null);
+ assertFalse(websocketProducer.isSendToAllSet(inMessage));
+ InOrder inOrder = inOrder(inMessage);
+ inOrder.verify(inMessage, times(1)).getHeader(WebsocketConstants.SEND_TO_ALL);
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketProducer#sendToAll(WebsocketStore, String)} .
+ */
+ @Test
+ public void testSendToAll() throws Exception {
+ when(store.getAll()).thenReturn(sockets);
+ when(defaultWebsocket1.getConnection()).thenReturn(connection);
+ when(defaultWebsocket2.getConnection()).thenReturn(connection);
+ when(connection.isOpen()).thenReturn(true);
+
+ websocketProducer.sendToAll(store, MESSAGE);
+
+ InOrder inOrder = inOrder(store, connection, defaultWebsocket1, defaultWebsocket2);
+ inOrder.verify(store, times(1)).getAll();
+ inOrder.verify(defaultWebsocket1, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).isOpen();
+ inOrder.verify(defaultWebsocket1, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).sendMessage(MESSAGE);
+ inOrder.verify(defaultWebsocket2, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).isOpen();
+ inOrder.verify(defaultWebsocket2, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).sendMessage(MESSAGE);
+ inOrder.verifyNoMoreInteractions();
+ }
+
+ /**
+ * Test method for {@link org.apache.camel.component.websocket.WebsocketProducer#sendToAll(WebsocketStore, String)} .
+ */
+ @Test
+ public void testSendToAllWithExcpetion() throws Exception {
+ when(store.getAll()).thenReturn(sockets);
+ when(defaultWebsocket1.getConnection()).thenReturn(connection);
+ when(defaultWebsocket2.getConnection()).thenReturn(connection);
+ doThrow(exception).when(connection).sendMessage(MESSAGE);
+ when(connection.isOpen()).thenReturn(true);
+
+ try {
+ websocketProducer.sendToAll(store, MESSAGE);
+ fail("Exception expected");
+ } catch (Exception e) {
+ assertEquals(exception, e.getCause());
+ }
+
+ InOrder inOrder = inOrder(store, connection, defaultWebsocket1, defaultWebsocket2);
+ inOrder.verify(store, times(1)).getAll();
+ inOrder.verify(defaultWebsocket1, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).isOpen();
+ inOrder.verify(defaultWebsocket1, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).sendMessage(MESSAGE);
+ inOrder.verify(defaultWebsocket2, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).isOpen();
+ inOrder.verify(defaultWebsocket2, times(1)).getConnection();
+ inOrder.verify(connection, times(1)).sendMessage(MESSAGE);
+ inOrder.verifyNoMoreInteractions();
+ }
+}
Added: camel/trunk/components/camel-websocket/src/test/resources/index.html
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-websocket/src/test/resources/index.html?rev=1228060&view=auto
==============================================================================
--- camel/trunk/components/camel-websocket/src/test/resources/index.html (added)
+++ camel/trunk/components/camel-websocket/src/test/resources/index.html Fri Jan 6 10:24:34 2012
@@ -0,0 +1,109 @@
+<html><head>
+ <title>WebSocket Chat</title>
+ <script type='text/javascript'>
+
+ if (!window.WebSocket)
+ alert("WebSocket not supported by this browser");
+
+ function $() { return document.getElementById(arguments[0]); }
+ function $F() { return document.getElementById(arguments[0]).value; }
+
+ function getKeyCode(ev) { if (window.event) return window.event.keyCode; return ev.keyCode; }
+
+ var room = {
+ join: function(name) {
+ this._username=name;
+ var location = "ws://localhost:1989/foo/";
+ this._ws=new WebSocket(location);
+ this._ws.onopen=this._onopen;
+ this._ws.onmessage=this._onmessage;
+ this._ws.onclose=this._onclose;
+ },
+
+ _onopen: function(){
+ $('join').className='hidden';
+ $('joined').className='';
+ $('phrase').focus();
+ room._send(room._username,'has joined!');
+ },
+
+ _send: function(user,message){
+ user=user.replace(':','_');
+ if (this._ws)
+ this._ws.send(user+':'+message);
+ },
+
+ chat: function(text) {
+ if (text != null && text.length>0 )
+ room._send(room._username,text);
+ },
+
+ _onmessage: function(m) {
+ if (m.data){
+ var c=m.data.indexOf(':');
+ var from=m.data.substring(0,c).replace('<','<').replace('>','>');
+ var text=m.data.substring(c+1).replace('<','<').replace('>','>');
+
+ var chat=$('chat');
+ var spanFrom = document.createElement('span');
+ spanFrom.className='from';
+ spanFrom.innerHTML=from+': ';
+ var spanText = document.createElement('span');
+ spanText.className='text';
+ spanText.innerHTML=text;
+ var lineBreak = document.createElement('br');
+ chat.appendChild(spanFrom);
+ chat.appendChild(spanText);
+ chat.appendChild(lineBreak);
+ chat.scrollTop = chat.scrollHeight - chat.clientHeight;
+ }
+ },
+
+ _onclose: function(m) {
+ this._ws=null;
+ $('join').className='';
+ $('joined').className='hidden';
+ $('username').focus();
+ $('chat').innerHTML='';
+ }
+
+ };
+
+ </script>
+ <style type='text/css'>
+ div { border: 0px solid black; }
+ div#chat { clear: both; width: 40em; height: 20ex; overflow: auto; background-color: #f0f0f0; padding: 4px; border: 1px solid black; }
+ div#input { clear: both; width: 40em; padding: 4px; background-color: #e0e0e0; border: 1px solid black; border-top: 0px }
+ input#phrase { width:30em; background-color: #e0f0f0; }
+ input#username { width:14em; background-color: #e0f0f0; }
+ div.hidden { display: none; }
+ span.from { font-weight: bold; }
+ span.alert { font-style: italic; }
+ </style>
+</head><body>
+<div id='chat'></div>
+<div id='input'>
+ <div id='join' >
+ Username: <input id='username' type='text'/><input id='joinB' class='button' type='submit' name='join' value='Join'/>
+ </div>
+ <div id='joined' class='hidden'>
+ Chat: <input id='phrase' type='text'/>
+ <input id='sendB' class='button' type='submit' name='join' value='Send'/>
+ </div>
+</div>
+<script type='text/javascript'>
+$('username').setAttribute('autocomplete','OFF');
+$('username').onkeyup = function(ev) { var keyc=getKeyCode(ev); if (keyc==13 || keyc==10) { room.join($F('username')); return false; } return true; } ;
+$('joinB').onclick = function(event) { room.join($F('username')); return false; };
+$('phrase').setAttribute('autocomplete','OFF');
+$('phrase').onkeyup = function(ev) { var keyc=getKeyCode(ev); if (keyc==13 || keyc==10) { room.chat($F('phrase')); $('phrase').value=''; return false; } return true; };
+$('sendB').onclick = function(event) { room.chat($F('phrase')); $('phrase').value=''; return false; };
+</script>
+
+<p>
+This is a demonstration of the Jetty websocket server.
+</p>
+</body></html>
+
+
+
\ No newline at end of file
Added: camel/trunk/components/camel-websocket/src/test/resources/producer-only.html
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-websocket/src/test/resources/producer-only.html?rev=1228060&view=auto
==============================================================================
--- camel/trunk/components/camel-websocket/src/test/resources/producer-only.html (added)
+++ camel/trunk/components/camel-websocket/src/test/resources/producer-only.html Fri Jan 6 10:24:34 2012
@@ -0,0 +1,52 @@
+<html>
+<head>
+ <title>WebSocket Chat</title>
+ <script type='text/javascript'>
+
+ if (!window.WebSocket) {
+ alert("WebSocket not supported by this browser");
+ }
+
+ function $() { return document.getElementById(arguments[0]); }
+
+ var room = {
+ join: function(name) {
+ this._username=name;
+ var location = "ws://localhost:1989/counter/";
+ this._ws=new WebSocket(location);
+ this._ws.onmessage=this._onmessage;
+ this._ws.onclose=this._onclose;
+ },
+
+ _onmessage: function(m) {
+ if (m.data){
+ var chat=$('chat');
+ var spanText = document.createElement('span');
+ spanText.className='text';
+ spanText.innerHTML=m.data;
+ var lineBreak = document.createElement('br');
+ chat.appendChild(spanText);
+ chat.appendChild(lineBreak);
+ chat.scrollTop = chat.scrollHeight - chat.clientHeight;
+ }
+ },
+
+ _onclose: function(m) {
+ this._ws=null;
+ }
+
+ };
+
+ </script>
+ <style type='text/css'>
+ div { border: 0px solid black; }
+ div#chat { clear: both; width: 40em; height: 20ex; overflow: auto; background-color: #f0f0f0; padding: 4px; border: 1px solid black; }
+ </style>
+</head>
+<body>
+ <div id='chat'></div>
+ <script type='text/javascript'>
+ room.join("Testbot");
+ </script>
+</body>
+</html>