Skip to content

Commit 7e4b210

Browse files
committed
Converting to Arquillian-enabled tests
1 parent 918c6f2 commit 7e4b210

File tree

6 files changed

+148
-170
lines changed

6 files changed

+148
-170
lines changed

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@
4040
package org.javaee7.websocket.endpoint;
4141

4242
import java.io.IOException;
43-
import java.nio.ByteBuffer;
4443
import javax.websocket.OnMessage;
45-
import javax.websocket.Session;
4644
import javax.websocket.server.ServerEndpoint;
4745

4846
/**
@@ -57,13 +55,7 @@ public String echoText(String name) {
5755
}
5856

5957
@OnMessage
60-
public void echoBinary(byte[] data, Session session) throws IOException {
61-
System.out.println("echoBinary: " + data);
62-
StringBuilder builder = new StringBuilder();
63-
for (byte b : data) {
64-
builder.append(b);
65-
}
66-
System.out.println(builder);
67-
session.getBasicRemote().sendBinary(ByteBuffer.wrap(data));
58+
public byte[] echoBinary(byte[] data) throws IOException {
59+
return data;
6860
}
6961
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.javaee7.websocket.endpoint;
2+
3+
import java.io.IOException;
4+
import java.nio.ByteBuffer;
5+
import java.util.concurrent.CountDownLatch;
6+
7+
import javax.websocket.ClientEndpoint;
8+
import javax.websocket.OnMessage;
9+
import javax.websocket.OnOpen;
10+
import javax.websocket.Session;
11+
12+
/**
13+
* @author Arun Gupta
14+
*/
15+
@ClientEndpoint
16+
public class MyEndpointBinaryClient {
17+
public static CountDownLatch latch;
18+
19+
@OnOpen
20+
public void onOpen(Session session) {
21+
try {
22+
session.getBasicRemote().sendBinary(ByteBuffer.wrap("Hello World!".getBytes()));
23+
} catch (IOException ioe) {
24+
ioe.printStackTrace();
25+
}
26+
}
27+
28+
@OnMessage
29+
public void processMessage(byte[] message) {
30+
latch.countDown();
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.javaee7.websocket.endpoint;
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+
@ClientEndpoint
15+
public class MyEndpointTextClient {
16+
public static CountDownLatch latch;
17+
18+
@OnOpen
19+
public void onOpen(Session session) {
20+
try {
21+
session.getBasicRemote().sendText("Hello world!");
22+
} catch (IOException ioe) {
23+
ioe.printStackTrace();
24+
}
25+
}
26+
27+
@OnMessage
28+
public void processMessage(String message) {
29+
latch.countDown();
30+
}
31+
}

websocket/endpoint/src/main/webapp/index.jsp

Lines changed: 0 additions & 70 deletions
This file was deleted.

websocket/endpoint/src/main/webapp/websocket.js

Lines changed: 0 additions & 90 deletions
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package org.javaee7.websocket.endpoint;
2+
3+
import java.io.IOException;
4+
import java.net.URI;
5+
import java.net.URISyntaxException;
6+
import java.nio.ByteBuffer;
7+
import java.util.concurrent.CountDownLatch;
8+
import java.util.concurrent.TimeUnit;
9+
import javax.websocket.ContainerProvider;
10+
import javax.websocket.DeploymentException;
11+
import javax.websocket.MessageHandler;
12+
import javax.websocket.Session;
13+
import javax.websocket.WebSocketContainer;
14+
import org.jboss.arquillian.container.test.api.Deployment;
15+
import org.jboss.arquillian.junit.Arquillian;
16+
import org.jboss.arquillian.test.api.ArquillianResource;
17+
import org.jboss.shrinkwrap.api.ShrinkWrap;
18+
import org.jboss.shrinkwrap.api.spec.WebArchive;
19+
import org.junit.Test;
20+
import static org.junit.Assert.*;
21+
import org.junit.runner.RunWith;
22+
23+
/**
24+
* @author Arun Gupta
25+
*/
26+
@RunWith(Arquillian.class)
27+
public class MyEndpointTest {
28+
final String TEXT = "Hello World!";
29+
30+
@ArquillianResource
31+
URI base;
32+
33+
@Deployment(testable=false)
34+
public static WebArchive createDeployment() {
35+
return ShrinkWrap.create(WebArchive.class)
36+
.addClasses(MyEndpoint.class,
37+
MyEndpointTextClient.class,
38+
MyEndpointBinaryClient.class);
39+
}
40+
41+
@Test
42+
public void testTextEndpoint() throws URISyntaxException, DeploymentException, IOException, InterruptedException {
43+
MyEndpointTextClient.latch = new CountDownLatch(1);
44+
Session session = connectToServer(MyEndpointTextClient.class);
45+
assertNotNull(session);
46+
session.addMessageHandler(new MessageHandler.Whole<String>() {
47+
@Override
48+
public void onMessage(String text) {
49+
MyEndpointTextClient.latch.countDown();
50+
assertEquals(TEXT, text);
51+
}
52+
});
53+
assertTrue(MyEndpointTextClient.latch.await(2, TimeUnit.SECONDS));
54+
}
55+
56+
@Test
57+
public void testBinaryEndpoint() throws URISyntaxException, DeploymentException, IOException, InterruptedException {
58+
MyEndpointBinaryClient.latch = new CountDownLatch(1);
59+
Session session = connectToServer(MyEndpointBinaryClient.class);
60+
assertNotNull(session);
61+
session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() {
62+
@Override
63+
public void onMessage(ByteBuffer binary) {
64+
MyEndpointBinaryClient.latch.countDown();
65+
assertEquals(TEXT, binary);
66+
}
67+
});
68+
assertTrue(MyEndpointBinaryClient.latch.await(2, TimeUnit.SECONDS));
69+
}
70+
71+
public Session connectToServer(Class endpoint) throws DeploymentException, IOException, URISyntaxException {
72+
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
73+
URI uri = new URI("ws://"
74+
+ base.getHost()
75+
+ ":"
76+
+ base.getPort()
77+
+ "/"
78+
+ base.getPath()
79+
+ "/websocket");
80+
System.out.println("Connecting to: " + uri);
81+
return container.connectToServer(endpoint, uri);
82+
}
83+
}

0 commit comments

Comments
 (0)