|
| 1 | +/* |
| 2 | + * Permission to use, copy, modify, and/or distribute this software for any |
| 3 | + * purpose with or without fee is hereby granted. |
| 4 | + * |
| 5 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIMS ALL WARRANTIES |
| 6 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 7 | + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR |
| 8 | + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 9 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 10 | + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 11 | + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 12 | + */ |
| 13 | +package jakartaee.examples.websocket.encoderdecoder; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.io.IOException; |
| 17 | +import java.net.URI; |
| 18 | +import java.net.URL; |
| 19 | +import java.util.concurrent.CountDownLatch; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | +import javax.websocket.ClientEndpoint; |
| 22 | +import javax.websocket.Decoder; |
| 23 | +import javax.websocket.EncodeException; |
| 24 | +import javax.websocket.Encoder; |
| 25 | +import javax.websocket.OnMessage; |
| 26 | +import javax.websocket.OnOpen; |
| 27 | +import javax.websocket.Session; |
| 28 | +import org.glassfish.tyrus.client.ClientManager; |
| 29 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 30 | +import org.jboss.arquillian.container.test.api.RunAsClient; |
| 31 | +import org.jboss.arquillian.junit.Arquillian; |
| 32 | +import org.jboss.arquillian.test.api.ArquillianResource; |
| 33 | +import static org.jboss.shrinkwrap.api.ShrinkWrap.create; |
| 34 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 35 | +import static org.junit.Assert.assertEquals; |
| 36 | +import org.junit.Test; |
| 37 | +import org.junit.runner.RunWith; |
| 38 | + |
| 39 | +/** |
| 40 | + * An annotated ClientEndpoint for the annotated ClientEndpoint example. |
| 41 | + * |
| 42 | + * @author Manfred Riem ([email protected]) |
| 43 | + */ |
| 44 | +@ClientEndpoint( |
| 45 | + encoders = {EncoderDecoderEncoder.class}, |
| 46 | + decoders = {EncoderDecoderDecoder.class} |
| 47 | +) |
| 48 | +@RunWith(Arquillian.class) |
| 49 | +public class EncoderDecoderEndpointTest { |
| 50 | + |
| 51 | + /** |
| 52 | + * Stores the base URL. |
| 53 | + */ |
| 54 | + @ArquillianResource |
| 55 | + private URL baseUrl; |
| 56 | + |
| 57 | + /** |
| 58 | + * Stores our buffer. |
| 59 | + */ |
| 60 | + private final StringBuilder buffer = new StringBuilder(); |
| 61 | + |
| 62 | + /** |
| 63 | + * Stores our countdown latch. |
| 64 | + */ |
| 65 | + private CountDownLatch countDown = new CountDownLatch(1); |
| 66 | + |
| 67 | + /** |
| 68 | + * Create the deployment web archive. |
| 69 | + * |
| 70 | + * @return the deployment web archive. |
| 71 | + */ |
| 72 | + @Deployment |
| 73 | + public static WebArchive createDeployment() { |
| 74 | + return create(WebArchive.class).addClasses( |
| 75 | + EncoderDecoderEndpoint.class, EncoderDecoder.class, |
| 76 | + EncoderDecoderDecoder.class, EncoderDecoderEncoder.class). |
| 77 | + addAsWebResource(new File("src/main/webapp/index.xhtml")). |
| 78 | + addAsWebInfResource(new File("src/main/webapp/WEB-INF/web.xml")); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Get the buffer. |
| 83 | + * |
| 84 | + * @return the buffer. |
| 85 | + */ |
| 86 | + public String getBuffer() { |
| 87 | + return buffer.toString(); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Handle the on open event. |
| 92 | + * |
| 93 | + * @param session the session. |
| 94 | + */ |
| 95 | + @OnOpen |
| 96 | + public void onOpen(Session session) { |
| 97 | + try { |
| 98 | + session.getBasicRemote().sendObject(new EncoderDecoder("ECHO")); |
| 99 | + } catch (IOException | EncodeException e) { |
| 100 | + buffer.append(e.getMessage()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Handle the text message. |
| 106 | + * |
| 107 | + * @param session the session. |
| 108 | + * @param encoderDecoder the message. |
| 109 | + */ |
| 110 | + @OnMessage |
| 111 | + public void onMessage(Session session, EncoderDecoder encoderDecoder) { |
| 112 | + buffer.append(encoderDecoder.toString()); |
| 113 | + countDown.countDown(); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Test the client endpoint. |
| 118 | + * |
| 119 | + * @throws Exception when a serious error occurs. |
| 120 | + */ |
| 121 | + @Test |
| 122 | + @RunAsClient |
| 123 | + public void testClientEndpoint() throws Exception { |
| 124 | + countDown = new CountDownLatch(1); |
| 125 | + ClientManager client = ClientManager.createClient(); |
| 126 | + StringBuilder wsUrl = new StringBuilder(); |
| 127 | + wsUrl.append("ws://").append(baseUrl.getHost()).append(":"). |
| 128 | + append(baseUrl.getPort()).append(baseUrl.getPath()).append("echo"); |
| 129 | + client.connectToServer(this, new URI(wsUrl.toString())); |
| 130 | + countDown.await(100, TimeUnit.SECONDS); |
| 131 | + assertEquals("ECHO", buffer.toString()); |
| 132 | + } |
| 133 | +} |
0 commit comments