|
| 1 | +package org.javaee7.websocket.chat; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.URI; |
| 5 | +import java.net.URISyntaxException; |
| 6 | +import java.util.concurrent.CountDownLatch; |
| 7 | +import java.util.concurrent.TimeUnit; |
| 8 | +import javax.websocket.ContainerProvider; |
| 9 | +import javax.websocket.DeploymentException; |
| 10 | +import javax.websocket.Session; |
| 11 | +import javax.websocket.WebSocketContainer; |
| 12 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 13 | +import org.jboss.arquillian.junit.Arquillian; |
| 14 | +import org.jboss.arquillian.test.api.ArquillianResource; |
| 15 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 16 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 17 | +import org.junit.Test; |
| 18 | +import static org.junit.Assert.*; |
| 19 | +import org.junit.runner.RunWith; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Arun Gupta |
| 23 | + */ |
| 24 | +@RunWith(Arquillian.class) |
| 25 | +public class ChatTest { |
| 26 | + |
| 27 | + @ArquillianResource |
| 28 | + URI base; |
| 29 | + |
| 30 | + @Deployment(testable = false) |
| 31 | + public static WebArchive createDeployment() { |
| 32 | + return ShrinkWrap.create(WebArchive.class) |
| 33 | + .addClasses(ChatEndpoint.class, |
| 34 | + ChatClientEndpoint1.class, |
| 35 | + ChatClientEndpoint2.class); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testConnect() throws URISyntaxException, DeploymentException, IOException, InterruptedException { |
| 40 | + ChatClientEndpoint1.latch = new CountDownLatch(1); |
| 41 | + final Session session1 = connectToServer(ChatClientEndpoint1.class); |
| 42 | + assertNotNull(session1); |
| 43 | + assertTrue(ChatClientEndpoint1.latch.await(2, TimeUnit.SECONDS)); |
| 44 | + |
| 45 | + assertEquals(ChatClientEndpoint1.TEXT, ChatClientEndpoint1.response); |
| 46 | + |
| 47 | + ChatClientEndpoint1.latch = new CountDownLatch(1); |
| 48 | + ChatClientEndpoint2.latch = new CountDownLatch(1); |
| 49 | + final Session session2 = connectToServer(ChatClientEndpoint2.class); |
| 50 | + assertNotNull(session2); |
| 51 | + assertTrue(ChatClientEndpoint1.latch.await(2, TimeUnit.SECONDS)); |
| 52 | + assertTrue(ChatClientEndpoint2.latch.await(2, TimeUnit.SECONDS)); |
| 53 | + assertEquals(ChatClientEndpoint2.TEXT, ChatClientEndpoint1.response); |
| 54 | + assertEquals(ChatClientEndpoint2.TEXT, ChatClientEndpoint2.response); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + public Session connectToServer(Class<?> endpoint) throws DeploymentException, IOException, URISyntaxException { |
| 59 | + WebSocketContainer container = ContainerProvider.getWebSocketContainer(); |
| 60 | + URI uri = new URI("ws://" |
| 61 | + + base.getHost() |
| 62 | + + ":" |
| 63 | + + base.getPort() |
| 64 | + + base.getPath() |
| 65 | + + "chat"); |
| 66 | + return container.connectToServer(endpoint, uri); |
| 67 | + } |
| 68 | +} |
0 commit comments