|
| 1 | +package org.javaee7.websocket.encoder; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.net.URI; |
| 7 | +import java.net.URISyntaxException; |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | +import java.util.logging.Level; |
| 10 | +import java.util.logging.Logger; |
| 11 | + |
| 12 | +import javax.websocket.ContainerProvider; |
| 13 | +import javax.websocket.DeploymentException; |
| 14 | +import javax.websocket.MessageHandler; |
| 15 | +import javax.websocket.Session; |
| 16 | +import javax.websocket.WebSocketContainer; |
| 17 | + |
| 18 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 19 | +import org.jboss.arquillian.junit.Arquillian; |
| 20 | +import org.jboss.arquillian.test.api.ArquillianResource; |
| 21 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 22 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 23 | +import org.junit.Test; |
| 24 | +import org.junit.runner.RunWith; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Nikos Ballas |
| 28 | + * @author Arun Gupta |
| 29 | + */ |
| 30 | +@RunWith(Arquillian.class) |
| 31 | +public class EncoderEndpointTest { |
| 32 | + |
| 33 | + @ArquillianResource |
| 34 | + URI base; |
| 35 | + |
| 36 | + /** |
| 37 | + * Arquillian specific method for creating a file which can be deployed |
| 38 | + * while executing the test. |
| 39 | + */ |
| 40 | + @Deployment(testable = false) |
| 41 | + public static WebArchive createDeployment() { |
| 42 | + return ShrinkWrap.create(WebArchive.class) |
| 43 | + .addClasses(MyEndpoint.class, |
| 44 | + MyMessage.class, |
| 45 | + MyMessageEncoder.class, |
| 46 | + MyMessageDecoder.class, |
| 47 | + MyEndpointClientEmptyJSONArray.class, |
| 48 | + MyEndpointClientJSONObject.class); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testEndpointEmptyJSONArray() throws URISyntaxException, DeploymentException, IOException, InterruptedException { |
| 53 | + final Session session = connectToServer(MyEndpointClientEmptyJSONArray.class); |
| 54 | + assertNotNull(session); |
| 55 | + session.addMessageHandler(new MessageHandler.Whole<String>() { |
| 56 | + @Override |
| 57 | + public void onMessage(String text) { |
| 58 | + assertEquals("{}", text); |
| 59 | + } |
| 60 | + }); |
| 61 | + assertTrue(MyEndpointClientEmptyJSONArray.latch.await(2, TimeUnit.SECONDS)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testEndpointEmptyJSONObject() throws URISyntaxException, DeploymentException, IOException, InterruptedException { |
| 66 | + final String JSON = "{\"apple\" : \"red\", \"banana\": \"yellow\"}"; |
| 67 | + Session session = connectToServer(MyEndpointClientJSONObject.class); |
| 68 | + assertNotNull(session); |
| 69 | + session.addMessageHandler(new MessageHandler.Whole<String>() { |
| 70 | + @Override |
| 71 | + public void onMessage(String text) { |
| 72 | + assertEquals(JSON, text); |
| 73 | + } |
| 74 | + }); |
| 75 | + assertTrue(MyEndpointClientJSONObject.latch.await(2, TimeUnit.SECONDS)); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Method used to supply connection to the server by passing the naming of |
| 80 | + * the websocket endpoint |
| 81 | + * |
| 82 | + * @param endpoint |
| 83 | + * @return |
| 84 | + * @throws DeploymentException |
| 85 | + * @throws IOException |
| 86 | + * @throws URISyntaxException |
| 87 | + */ |
| 88 | + public Session connectToServer(Class endpoint) throws DeploymentException, IOException, URISyntaxException { |
| 89 | + WebSocketContainer container = ContainerProvider.getWebSocketContainer(); |
| 90 | + URI uri = new URI("ws://" |
| 91 | + + base.getHost() |
| 92 | + + ":" |
| 93 | + + base.getPort() |
| 94 | + + "/" |
| 95 | + + base.getPath() |
| 96 | + + "/encoder"); |
| 97 | + return container.connectToServer(endpoint, uri); |
| 98 | + |
| 99 | + } |
| 100 | +} |
0 commit comments