|
| 1 | +package org.javaee7.websocket.encoder.programmatic; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.URI; |
| 5 | +import java.net.URISyntaxException; |
| 6 | +import java.util.concurrent.TimeUnit; |
| 7 | +import javax.websocket.ContainerProvider; |
| 8 | +import javax.websocket.DeploymentException; |
| 9 | +import javax.websocket.MessageHandler; |
| 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 MyClientTest { |
| 26 | + @ArquillianResource |
| 27 | + URI base; |
| 28 | + |
| 29 | + @Deployment(testable = false) |
| 30 | + public static WebArchive createDeployment() { |
| 31 | + return ShrinkWrap.create(WebArchive.class) |
| 32 | + .addClasses(MyEndpoint.class, |
| 33 | + MyEndpointConfiguration.class, |
| 34 | + MyClient.class, |
| 35 | + MyMessage.class, |
| 36 | + MyMessageEncoder.class, |
| 37 | + MyMessageDecoder.class); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testEndpoint() throws URISyntaxException, DeploymentException, IOException, InterruptedException { |
| 42 | + final String JSON = "{\"apple\" : \"red\", \"banana\": \"yellow\"}"; |
| 43 | + Session session = connectToServer(MyClient.class); |
| 44 | + assertNotNull(session); |
| 45 | + session.addMessageHandler(new MessageHandler.Whole<String>() { |
| 46 | + @Override |
| 47 | + public void onMessage(String text) { |
| 48 | + assertEquals(JSON, text); |
| 49 | + } |
| 50 | + }); |
| 51 | + assertTrue(MyClient.latch.await(2, TimeUnit.SECONDS)); |
| 52 | + } |
| 53 | + |
| 54 | + public Session connectToServer(Class endpoint) throws DeploymentException, IOException, URISyntaxException { |
| 55 | + WebSocketContainer container = ContainerProvider.getWebSocketContainer(); |
| 56 | + assertNotNull(container); |
| 57 | + assertNotNull(base); |
| 58 | + URI uri = new URI("ws://" |
| 59 | + + base.getHost() |
| 60 | + + ":" |
| 61 | + + base.getPort() |
| 62 | + + "/" |
| 63 | + + base.getPath() |
| 64 | + + "/encoder-programmatic"); |
| 65 | + return container.connectToServer(endpoint, uri); |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments