Skip to content

Commit eda9298

Browse files
committed
Fixes issue #88 - Add tests for all WebSocket examples
1 parent 478bb95 commit eda9298

File tree

23 files changed

+732
-101
lines changed

23 files changed

+732
-101
lines changed

pom.xml

+10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@
4848
<artifactId>httpclient</artifactId>
4949
<version>4.5.5</version>
5050
</dependency>
51+
<dependency>
52+
<groupId>org.glassfish.tyrus</groupId>
53+
<artifactId>tyrus-client</artifactId>
54+
<version>1.13.1</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.glassfish.tyrus</groupId>
58+
<artifactId>tyrus-container-grizzly-client</artifactId>
59+
<version>1.13.1</version>
60+
</dependency>
5161
</dependencies>
5262
</dependencyManagement>
5363
<dependencies>

websocket/README.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# WebSocket Examples
22

3-
1. [An annotated ServerEndpoint example](annotatedServerEndpoint/README.md)
4-
2. [An Encoder/Decoder example](encoderDecoder/README.md)
5-
3. [An @OnClose example](onClose/README.md)
6-
4. [An @OnError example](onError/README.md)
7-
5. [An @OnOpen example](onOpen/README.md)
3+
1. [An annotated ClientEndpoint example](annotatedClientEndpoint/README.md)
4+
2. [An annotated ServerEndpoint example](annotatedServerEndpoint/README.md)
5+
3. [An Encoder/Decoder example](encoderDecoder/README.md)
6+
4. [An @OnClose example](onClose/README.md)
7+
5. [An @OnError example](onError/README.md)
8+
6. [An @OnOpen example](onOpen/README.md)

websocket/annotatedClientEndpoint/pom.xml

-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@
2121
<dependency>
2222
<groupId>org.glassfish.tyrus</groupId>
2323
<artifactId>tyrus-client</artifactId>
24-
<version>1.13.1</version>
2524
<scope>test</scope>
2625
</dependency>
2726
<dependency>
2827
<groupId>org.glassfish.tyrus</groupId>
2928
<artifactId>tyrus-container-grizzly-client</artifactId>
30-
<version>1.13.1</version>
3129
<scope>test</scope>
3230
</dependency>
3331
</dependencies>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# An annotated ServerEndpoint example
2+
3+
This example demonstrates how to use an annotated ServerEndpoint.

websocket/annotatedServerEndpoint/pom.xml

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
<artifactId>javaee-web-api</artifactId>
1919
<scope>provided</scope>
2020
</dependency>
21+
<dependency>
22+
<groupId>org.glassfish.tyrus</groupId>
23+
<artifactId>tyrus-client</artifactId>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.glassfish.tyrus</groupId>
28+
<artifactId>tyrus-container-grizzly-client</artifactId>
29+
<scope>test</scope>
30+
</dependency>
2131
</dependencies>
2232
<properties>
2333
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
1111
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1212
*/
13-
package jakartaee.examples.websocket.annotatedServerEndpoint;
13+
package jakartaee.examples.websocket.annotatedserverendpoint;
1414

1515
import java.io.IOException;
1616
import javax.websocket.OnMessage;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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.annotatedserverendpoint;
14+
15+
import jakartaee.examples.websocket.annotatedserverendpoint.AnnotatedServerEndpoint;
16+
import java.io.File;
17+
import java.io.IOException;
18+
import java.net.URI;
19+
import java.net.URL;
20+
import java.util.concurrent.CountDownLatch;
21+
import java.util.concurrent.TimeUnit;
22+
import javax.websocket.ClientEndpoint;
23+
import javax.websocket.OnMessage;
24+
import javax.websocket.OnOpen;
25+
import javax.websocket.Session;
26+
import org.glassfish.tyrus.client.ClientManager;
27+
import org.jboss.arquillian.container.test.api.Deployment;
28+
import org.jboss.arquillian.container.test.api.RunAsClient;
29+
import org.jboss.arquillian.junit.Arquillian;
30+
import org.jboss.arquillian.test.api.ArquillianResource;
31+
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
32+
import org.jboss.shrinkwrap.api.spec.WebArchive;
33+
import static org.junit.Assert.assertEquals;
34+
import org.junit.Test;
35+
import org.junit.runner.RunWith;
36+
37+
/**
38+
* An annotated ClientEndpoint for the annotated ClientEndpoint example.
39+
*
40+
* @author Manfred Riem ([email protected])
41+
*/
42+
@ClientEndpoint
43+
@RunWith(Arquillian.class)
44+
public class AnnotatedServerEndpointTest {
45+
46+
/**
47+
* Stores the base URL.
48+
*/
49+
@ArquillianResource
50+
private URL baseUrl;
51+
52+
/**
53+
* Stores our buffer.
54+
*/
55+
private final StringBuilder buffer = new StringBuilder();
56+
57+
/**
58+
* Stores our countdown latch.
59+
*/
60+
private CountDownLatch countDown = new CountDownLatch(1);
61+
62+
/**
63+
* Create the deployment web archive.
64+
*
65+
* @return the deployment web archive.
66+
*/
67+
@Deployment
68+
public static WebArchive createDeployment() {
69+
return create(WebArchive.class).addClasses(
70+
AnnotatedServerEndpoint.class).
71+
addAsWebResource(new File("src/main/webapp/index.xhtml")).
72+
addAsWebInfResource(new File("src/main/webapp/WEB-INF/web.xml"));
73+
}
74+
75+
/**
76+
* Get the buffer.
77+
*
78+
* @return the buffer.
79+
*/
80+
public String getBuffer() {
81+
return buffer.toString();
82+
}
83+
84+
/**
85+
* Handle the on open event.
86+
*
87+
* @param session the session.
88+
*/
89+
@OnOpen
90+
public void onOpen(Session session) {
91+
try {
92+
session.getBasicRemote().sendText("ECHO");
93+
} catch (IOException ioe) {
94+
buffer.append(ioe.getMessage());
95+
}
96+
}
97+
98+
/**
99+
* Handle the text message.
100+
*
101+
* @param session the session.
102+
* @param message the message.
103+
*/
104+
@OnMessage
105+
public void onMessage(Session session, String message) {
106+
buffer.append(message);
107+
countDown.countDown();
108+
}
109+
110+
/**
111+
* Test the client endpoint.
112+
*
113+
* @throws Exception when a serious error occurs.
114+
*/
115+
@Test
116+
@RunAsClient
117+
public void testClientEndpoint() throws Exception {
118+
countDown = new CountDownLatch(1);
119+
ClientManager client = ClientManager.createClient();
120+
StringBuilder wsUrl = new StringBuilder();
121+
wsUrl.append("ws://").append(baseUrl.getHost()).append(":").
122+
append(baseUrl.getPort()).append(baseUrl.getPath()).append("echo");
123+
client.connectToServer(this, new URI(wsUrl.toString()));
124+
countDown.await(100, TimeUnit.SECONDS);
125+
assertEquals("ECHO", buffer.toString());
126+
}
127+
}

websocket/encoderDecoder/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# An Encoder/Decoder example
2+
3+
This example demonstrates how to use encoders and decoders usinsg WebSockets.

websocket/encoderDecoder/pom.xml

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
<artifactId>javaee-web-api</artifactId>
1919
<scope>provided</scope>
2020
</dependency>
21+
<dependency>
22+
<groupId>org.glassfish.tyrus</groupId>
23+
<artifactId>tyrus-client</artifactId>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.glassfish.tyrus</groupId>
28+
<artifactId>tyrus-container-grizzly-client</artifactId>
29+
<scope>test</scope>
30+
</dependency>
2131
</dependencies>
2232
<properties>
2333
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
}

websocket/onClose/pom.xml

+10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
<artifactId>javaee-web-api</artifactId>
2222
<scope>provided</scope>
2323
</dependency>
24+
<dependency>
25+
<groupId>org.glassfish.tyrus</groupId>
26+
<artifactId>tyrus-client</artifactId>
27+
<scope>test</scope>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.glassfish.tyrus</groupId>
31+
<artifactId>tyrus-container-grizzly-client</artifactId>
32+
<scope>test</scope>
33+
</dependency>
2434
</dependencies>
2535
<properties>
2636
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

0 commit comments

Comments
 (0)