Skip to content

Commit d79b44f

Browse files
committed
Adding new Arquillian tests for encoder/decoder
1 parent 66618ef commit d79b44f

File tree

6 files changed

+184
-17
lines changed

6 files changed

+184
-17
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
encoders = {MyMessageEncoder.class},
5050
decoders = {MyMessageDecoder.class})
5151
public class MyEndpoint {
52-
5352
@OnMessage
5453
public MyMessage messageReceived(MyMessage message) {
5554
System.out.println("messageReceived: " + message);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.javaee7.websocket.encoder;
2+
3+
import java.io.IOException;
4+
import java.util.concurrent.CountDownLatch;
5+
6+
import javax.websocket.ClientEndpoint;
7+
import javax.websocket.OnMessage;
8+
import javax.websocket.OnOpen;
9+
import javax.websocket.Session;
10+
11+
/**
12+
* @author Arun Gupta
13+
*
14+
*/
15+
@ClientEndpoint
16+
public class MyEndpointClientEmptyJSONArray {
17+
public static String JSON = "{}";
18+
public static CountDownLatch latch= new CountDownLatch(3);
19+
20+
@OnOpen
21+
public void onOpen(Session session) {
22+
try {
23+
session.getBasicRemote().sendText(JSON);
24+
} catch (IOException ioe) {
25+
ioe.printStackTrace();
26+
}
27+
}
28+
29+
@OnMessage
30+
public String processMessage(String message) {
31+
latch.countDown();
32+
return message;
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.javaee7.websocket.encoder;
2+
3+
import java.io.IOException;
4+
import java.util.concurrent.CountDownLatch;
5+
6+
import javax.websocket.ClientEndpoint;
7+
import javax.websocket.OnMessage;
8+
import javax.websocket.OnOpen;
9+
import javax.websocket.Session;
10+
11+
/**
12+
* @author Arun Gupta
13+
*
14+
*/
15+
@ClientEndpoint
16+
public class MyEndpointClientJSONObject {
17+
public static CountDownLatch latch = new CountDownLatch(3);
18+
public static String JSON = "{\"apple\" : \"red\", \"banana\": \"yellow\"}";
19+
20+
@OnOpen
21+
public void onOpen(Session session) {
22+
try {
23+
session.getBasicRemote().sendText(JSON);
24+
} catch (IOException ioe) {
25+
ioe.printStackTrace();
26+
}
27+
}
28+
29+
@OnMessage
30+
public String processMessage(String message) {
31+
latch.countDown();
32+
return message;
33+
}
34+
}

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ public class MyMessageDecoder implements Decoder.Text<MyMessage> {
5252

5353
@Override
5454
public MyMessage decode(String string) throws DecodeException {
55-
System.out.println("decoding: " + string);
55+
if (MyEndpointClientEmptyJSONArray.latch != null)
56+
MyEndpointClientEmptyJSONArray.latch.countDown();
57+
58+
if (MyEndpointClientJSONObject.latch != null)
59+
MyEndpointClientJSONObject.latch.countDown();
60+
5661
MyMessage myMessage = new MyMessage(Json.createReader(new StringReader(string)).readObject());
57-
58-
System.out.println(myMessage.getJsonObject());
5962
return myMessage;
6063
}
6164

@@ -65,12 +68,8 @@ public boolean willDecode(String string) {
6568
}
6669

6770
@Override
68-
public void init(EndpointConfig ec) {
69-
System.out.println("init");
70-
}
71+
public void init(EndpointConfig ec) { }
7172

7273
@Override
73-
public void destroy() {
74-
System.out.println("desroy");
75-
}
74+
public void destroy() { }
7675
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,18 @@
4949
public class MyMessageEncoder implements Encoder.Text<MyMessage> {
5050
@Override
5151
public String encode(MyMessage myMessage) throws EncodeException {
52-
// return this.getClass().getName();
52+
if (MyEndpointClientEmptyJSONArray.latch != null)
53+
MyEndpointClientEmptyJSONArray.latch.countDown();
54+
55+
if (MyEndpointClientJSONObject.latch != null)
56+
MyEndpointClientJSONObject.latch.countDown();
57+
5358
return myMessage.getJsonObject().toString();
5459
}
5560

5661
@Override
57-
public void init(EndpointConfig ec) {
58-
System.out.println("init");
59-
}
62+
public void init(EndpointConfig ec) { }
6063

6164
@Override
62-
public void destroy() {
63-
System.out.println("destroy");
64-
}
65+
public void destroy() { }
6566
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)