Skip to content

Commit 9a37867

Browse files
committed
Clean up WebSocket tests
* Remove recursive send/receive. * Correct the CountDownLatch count numbers. Mixes between Client and Server Encoder/Decoders and rely on multiple messages being echoed. * GlassFish is more sensible to double slashes in WS URL's. Remove the extra forward slashes. * Pr spec you can only register a single Message Handler pr type. Change to store Message in static to verify content.
1 parent 36a8957 commit 9a37867

File tree

15 files changed

+83
-101
lines changed

15 files changed

+83
-101
lines changed

pom.xml

+12
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@
207207
<version>4.0</version>
208208
<scope>test</scope>
209209
</dependency>
210+
<dependency>
211+
<groupId>org.glassfish</groupId>
212+
<artifactId>javax.json</artifactId>
213+
<version>1.0.4</version>
214+
<scope>test</scope>
215+
</dependency>
210216
<dependency>
211217
<groupId>org.glassfish.tyrus</groupId>
212218
<artifactId>tyrus-client</artifactId>
@@ -236,6 +242,12 @@
236242
<profile>
237243
<id>glassfish-remote-arquillian</id>
238244
<dependencies>
245+
<dependency>
246+
<groupId>org.glassfish</groupId>
247+
<artifactId>javax.json</artifactId>
248+
<version>1.0.4</version>
249+
<scope>test</scope>
250+
</dependency>
239251
<dependency>
240252
<groupId>org.glassfish.tyrus</groupId>
241253
<artifactId>tyrus-client</artifactId>

websocket/encoder-client/src/main/java/org/javaee7/websocket/encoder/client/MyClient.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.concurrent.CountDownLatch;
4444
import java.util.logging.Level;
4545
import java.util.logging.Logger;
46+
4647
import javax.websocket.ClientEndpoint;
4748
import javax.websocket.EncodeException;
4849
import javax.websocket.OnError;
@@ -57,6 +58,7 @@
5758
decoders={MyMessageDecoder.class})
5859
public class MyClient {
5960
public static CountDownLatch latch= new CountDownLatch(3);
61+
public static MyMessage response;
6062

6163
@OnOpen
6264
public void onOpen(Session session) {
@@ -69,9 +71,9 @@ public void onOpen(Session session) {
6971
}
7072

7173
@OnMessage
72-
public MyMessage processMessage(MyMessage message) {
73-
latch.countDown();
74-
return message;
74+
public void processMessage(MyMessage message) {
75+
response = message;
76+
latch.countDown();
7577
}
7678

7779
@OnError

websocket/encoder-client/src/test/java/org/javaee7/websocket/encoder/client/MyClientTest.java

+9-11
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,26 @@
66

77
package org.javaee7.websocket.encoder.client;
88

9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertNotNull;
11+
import static org.junit.Assert.assertTrue;
12+
913
import java.io.IOException;
1014
import java.net.URI;
1115
import java.net.URISyntaxException;
1216
import java.util.concurrent.TimeUnit;
17+
1318
import javax.websocket.ContainerProvider;
1419
import javax.websocket.DeploymentException;
15-
import javax.websocket.MessageHandler;
1620
import javax.websocket.Session;
1721
import javax.websocket.WebSocketContainer;
22+
1823
import org.jboss.arquillian.container.test.api.Deployment;
1924
import org.jboss.arquillian.junit.Arquillian;
2025
import org.jboss.arquillian.test.api.ArquillianResource;
2126
import org.jboss.shrinkwrap.api.ShrinkWrap;
2227
import org.jboss.shrinkwrap.api.spec.WebArchive;
2328
import org.junit.Test;
24-
import static org.junit.Assert.*;
2529
import org.junit.runner.RunWith;
2630

2731
/**
@@ -47,16 +51,11 @@ public static WebArchive createDeployment() {
4751

4852
@Test
4953
public void testEndpoint() throws URISyntaxException, DeploymentException, IOException, InterruptedException {
50-
final String JSON = "{\"apple\" : \"red\", \"banana\": \"yellow\"}";
54+
String JSON = "{\"apple\":\"red\",\"banana\":\"yellow\"}";
5155
Session session = connectToServer(MyClient.class);
5256
assertNotNull(session);
53-
session.addMessageHandler(new MessageHandler.Whole<String>() {
54-
@Override
55-
public void onMessage(String text) {
56-
assertEquals(JSON, text);
57-
}
58-
});
5957
assertTrue(MyClient.latch.await(2, TimeUnit.SECONDS));
58+
assertEquals(JSON, MyClient.response.toString());
6059
}
6160

6261
public Session connectToServer(Class endpoint) throws DeploymentException, IOException, URISyntaxException {
@@ -67,9 +66,8 @@ public Session connectToServer(Class endpoint) throws DeploymentException, IOExc
6766
+ base.getHost()
6867
+ ":"
6968
+ base.getPort()
70-
+ "/"
7169
+ base.getPath()
72-
+ "/encoder-client");
70+
+ "encoder-client");
7371
return container.connectToServer(endpoint, uri);
7472
}
7573

websocket/encoder-programmatic/src/main/java/org/javaee7/websocket/encoder/programmatic/MyClient.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
@ClientEndpoint(encoders = {MyMessageEncoder.class},
1818
decoders={MyMessageDecoder.class})
1919
public class MyClient {
20-
public static CountDownLatch latch= new CountDownLatch(4);
20+
public static CountDownLatch latch= new CountDownLatch(3);
21+
public static MyMessage response;
2122

2223
@OnOpen
2324
public void onOpen(Session session) {
@@ -30,9 +31,9 @@ public void onOpen(Session session) {
3031
}
3132

3233
@OnMessage
33-
public MyMessage processMessage(MyMessage message) {
34-
latch.countDown();
35-
return message;
34+
public void processMessage(MyMessage message) {
35+
response = message;
36+
latch.countDown();
3637
}
3738

3839
@OnError

websocket/encoder-programmatic/src/main/java/org/javaee7/websocket/encoder/programmatic/MyEndpoint.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.io.IOException;
4343
import java.util.logging.Level;
4444
import java.util.logging.Logger;
45+
4546
import javax.websocket.Endpoint;
4647
import javax.websocket.EndpointConfig;
4748
import javax.websocket.MessageHandler;
@@ -59,7 +60,6 @@ public void onOpen(final Session session, EndpointConfig ec) {
5960
@Override
6061
public void onMessage(String text) {
6162
try {
62-
MyClient.latch.countDown();
6363
session.getBasicRemote().sendText(text);
6464
} catch (IOException ex) {
6565
Logger.getLogger(MyEndpoint.class.getName()).log(Level.SEVERE, null, ex);

websocket/encoder-programmatic/src/main/java/org/javaee7/websocket/encoder/programmatic/MyEndpointConfiguration.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.HashSet;
4545
import java.util.List;
4646
import java.util.Set;
47+
4748
import javax.websocket.Decoder;
4849
import javax.websocket.Encoder;
4950
import javax.websocket.Endpoint;
@@ -65,14 +66,12 @@ public MyEndpointConfiguration() {
6566

6667
@Override
6768
public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> set) {
68-
return new HashSet<ServerEndpointConfig>() {
69-
{
70-
add(ServerEndpointConfig.Builder.create(MyEndpoint.class, "/encoder-programmatic")
69+
Set<ServerEndpointConfig> config = new HashSet<ServerEndpointConfig>();
70+
config.add(ServerEndpointConfig.Builder.create(MyEndpoint.class, "/encoder-programmatic")
7171
.encoders(encoders)
7272
.decoders(decoders)
7373
.build());
74-
}
75-
};
74+
return config;
7675
}
7776

7877
@Override

websocket/encoder-programmatic/src/test/java/org/javaee7/websocket/encoder/programmatic/MyClientTest.java

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
package org.javaee7.websocket.encoder.programmatic;
22

3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertTrue;
6+
37
import java.io.IOException;
48
import java.net.URI;
59
import java.net.URISyntaxException;
610
import java.util.concurrent.TimeUnit;
11+
712
import javax.websocket.ContainerProvider;
813
import javax.websocket.DeploymentException;
9-
import javax.websocket.MessageHandler;
1014
import javax.websocket.Session;
1115
import javax.websocket.WebSocketContainer;
16+
1217
import org.jboss.arquillian.container.test.api.Deployment;
1318
import org.jboss.arquillian.junit.Arquillian;
1419
import org.jboss.arquillian.test.api.ArquillianResource;
1520
import org.jboss.shrinkwrap.api.ShrinkWrap;
1621
import org.jboss.shrinkwrap.api.spec.WebArchive;
1722
import org.junit.Test;
18-
import static org.junit.Assert.*;
1923
import org.junit.runner.RunWith;
2024

2125
/**
@@ -31,37 +35,30 @@ public static WebArchive createDeployment() {
3135
return ShrinkWrap.create(WebArchive.class)
3236
.addClasses(MyEndpoint.class,
3337
MyEndpointConfiguration.class,
34-
MyClient.class,
3538
MyMessage.class,
3639
MyMessageEncoder.class,
3740
MyMessageDecoder.class);
3841
}
3942

4043
@Test
4144
public void testEndpoint() throws URISyntaxException, DeploymentException, IOException, InterruptedException {
42-
final String JSON = "{\"apple\" : \"red\", \"banana\": \"yellow\"}";
45+
final String JSON = "{\"apple\":\"red\",\"banana\":\"yellow\"}";
4346
Session session = connectToServer(MyClient.class);
4447
assertNotNull(session);
45-
session.addMessageHandler(new MessageHandler.Whole<String>() {
46-
@Override
47-
public void onMessage(String text) {
48-
assertEquals(JSON, text);
49-
}
50-
});
5148
assertTrue(MyClient.latch.await(2, TimeUnit.SECONDS));
49+
assertEquals(JSON, MyClient.response.toString());
5250
}
5351

54-
public Session connectToServer(Class endpoint) throws DeploymentException, IOException, URISyntaxException {
52+
public Session connectToServer(Class<?> endpoint) throws DeploymentException, IOException, URISyntaxException {
5553
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
5654
assertNotNull(container);
5755
assertNotNull(base);
5856
URI uri = new URI("ws://"
5957
+ base.getHost()
6058
+ ":"
6159
+ base.getPort()
62-
+ "/"
6360
+ base.getPath()
64-
+ "/encoder-programmatic");
61+
+ "encoder-programmatic");
6562
return container.connectToServer(endpoint, uri);
6663
}
6764

websocket/encoder/src/main/java/org/javaee7/websocket/encoder/MyEndpointClientEmptyJSONArray.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
@ClientEndpoint
1616
public class MyEndpointClientEmptyJSONArray {
1717
public static String JSON = "{}";
18-
public static CountDownLatch latch= new CountDownLatch(3);
18+
public static CountDownLatch latch= new CountDownLatch(1);
19+
public static String response;
1920

2021
@OnOpen
2122
public void onOpen(Session session) {
@@ -27,8 +28,8 @@ public void onOpen(Session session) {
2728
}
2829

2930
@OnMessage
30-
public String processMessage(String message) {
31+
public void processMessage(String message) {
32+
response = message;
3133
latch.countDown();
32-
return message;
3334
}
3435
}

websocket/encoder/src/main/java/org/javaee7/websocket/encoder/MyEndpointClientJSONObject.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
*/
1515
@ClientEndpoint
1616
public class MyEndpointClientJSONObject {
17-
public static CountDownLatch latch = new CountDownLatch(3);
17+
public static CountDownLatch latch = new CountDownLatch(1);
1818
public static String JSON = "{\"apple\" : \"red\", \"banana\": \"yellow\"}";
19+
public static String response;
1920

2021
@OnOpen
2122
public void onOpen(Session session) {
@@ -27,8 +28,8 @@ public void onOpen(Session session) {
2728
}
2829

2930
@OnMessage
30-
public String processMessage(String message) {
31+
public void processMessage(String message) {
32+
response = message;
3133
latch.countDown();
32-
return message;
3334
}
3435
}

websocket/encoder/src/main/java/org/javaee7/websocket/encoder/MyMessageDecoder.java

-6
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@ public class MyMessageDecoder implements Decoder.Text<MyMessage> {
5252

5353
@Override
5454
public MyMessage decode(String string) throws DecodeException {
55-
if (MyEndpointClientEmptyJSONArray.latch != null)
56-
MyEndpointClientEmptyJSONArray.latch.countDown();
57-
58-
if (MyEndpointClientJSONObject.latch != null)
59-
MyEndpointClientJSONObject.latch.countDown();
60-
6155
MyMessage myMessage = new MyMessage(Json.createReader(new StringReader(string)).readObject());
6256
return myMessage;
6357
}

websocket/encoder/src/main/java/org/javaee7/websocket/encoder/MyMessageEncoder.java

-6
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,6 @@
4949
public class MyMessageEncoder implements Encoder.Text<MyMessage> {
5050
@Override
5151
public String encode(MyMessage myMessage) throws EncodeException {
52-
if (MyEndpointClientEmptyJSONArray.latch != null)
53-
MyEndpointClientEmptyJSONArray.latch.countDown();
54-
55-
if (MyEndpointClientJSONObject.latch != null)
56-
MyEndpointClientJSONObject.latch.countDown();
57-
5852
return myMessage.getJsonObject().toString();
5953
}
6054

websocket/encoder/src/test/java/org/javaee7/websocket/encoder/EncoderEndpointTest.java

+9-23
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package org.javaee7.websocket.encoder;
22

3-
import static org.junit.Assert.*;
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertTrue;
46

57
import java.io.IOException;
68
import java.net.URI;
79
import java.net.URISyntaxException;
810
import java.util.concurrent.TimeUnit;
9-
import java.util.logging.Level;
10-
import java.util.logging.Logger;
1111

1212
import javax.websocket.ContainerProvider;
1313
import javax.websocket.DeploymentException;
14-
import javax.websocket.MessageHandler;
1514
import javax.websocket.Session;
1615
import javax.websocket.WebSocketContainer;
1716

@@ -43,36 +42,24 @@ public static WebArchive createDeployment() {
4342
.addClasses(MyEndpoint.class,
4443
MyMessage.class,
4544
MyMessageEncoder.class,
46-
MyMessageDecoder.class,
47-
MyEndpointClientEmptyJSONArray.class,
48-
MyEndpointClientJSONObject.class);
45+
MyMessageDecoder.class);
4946
}
5047

5148
@Test
5249
public void testEndpointEmptyJSONArray() throws URISyntaxException, DeploymentException, IOException, InterruptedException {
5350
final Session session = connectToServer(MyEndpointClientEmptyJSONArray.class);
5451
assertNotNull(session);
55-
session.addMessageHandler(new MessageHandler.Whole<String>() {
56-
@Override
57-
public void onMessage(String text) {
58-
assertEquals("{}", text);
59-
}
60-
});
6152
assertTrue(MyEndpointClientEmptyJSONArray.latch.await(2, TimeUnit.SECONDS));
53+
assertEquals("{}", MyEndpointClientEmptyJSONArray.response);
6254
}
6355

6456
@Test
6557
public void testEndpointEmptyJSONObject() throws URISyntaxException, DeploymentException, IOException, InterruptedException {
66-
final String JSON = "{\"apple\" : \"red\", \"banana\": \"yellow\"}";
58+
String JSON = "{\"apple\":\"red\",\"banana\":\"yellow\"}";
6759
Session session = connectToServer(MyEndpointClientJSONObject.class);
6860
assertNotNull(session);
69-
session.addMessageHandler(new MessageHandler.Whole<String>() {
70-
@Override
71-
public void onMessage(String text) {
72-
assertEquals(JSON, text);
73-
}
74-
});
7561
assertTrue(MyEndpointClientJSONObject.latch.await(2, TimeUnit.SECONDS));
62+
assertEquals(JSON, MyEndpointClientJSONObject.response);
7663
}
7764

7865
/**
@@ -85,15 +72,14 @@ public void onMessage(String text) {
8572
* @throws IOException
8673
* @throws URISyntaxException
8774
*/
88-
public Session connectToServer(Class endpoint) throws DeploymentException, IOException, URISyntaxException {
75+
public Session connectToServer(Class<?> endpoint) throws DeploymentException, IOException, URISyntaxException {
8976
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
9077
URI uri = new URI("ws://"
9178
+ base.getHost()
9279
+ ":"
9380
+ base.getPort()
94-
+ "/"
9581
+ base.getPath()
96-
+ "/encoder");
82+
+ "encoder");
9783
return container.connectToServer(endpoint, uri);
9884
}
9985
}

0 commit comments

Comments
 (0)