Skip to content

Commit ad3d9b1

Browse files
committed
Arquillian tests for async websocket endpoint. Part of #86
1 parent 8b9e0ee commit ad3d9b1

File tree

6 files changed

+160
-5
lines changed

6 files changed

+160
-5
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@
113113
<version>20131018</version>
114114
<scope>test</scope>
115115
</dependency>
116+
<dependency>
117+
<groupId>com.jayway.awaitility</groupId>
118+
<artifactId>awaitility</artifactId>
119+
<version>1.3.5</version>
120+
<scope>test</scope>
121+
</dependency>
116122
</dependencies>
117123
<pluginRepositories>
118124
<pluginRepository>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.javaee7.websocket.endpoint.async;
2+
3+
import java.nio.ByteBuffer;
4+
import javax.websocket.OnMessage;
5+
import javax.websocket.Session;
6+
import javax.websocket.server.ServerEndpoint;
7+
8+
/**
9+
* @author Jacek Jackowiak
10+
*/
11+
@ServerEndpoint("/bytebuffer")
12+
public class MyAsyncEndpointByteBuffer {
13+
14+
@OnMessage
15+
public void echoByteBuffer(ByteBuffer data, Session session) {
16+
session.getAsyncRemote().sendBinary(data);
17+
}
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.javaee7.websocket.endpoint.async;
2+
3+
import java.nio.ByteBuffer;
4+
import javax.websocket.ClientEndpoint;
5+
import javax.websocket.OnMessage;
6+
7+
/**
8+
*
9+
* @author Jacek Jackowiak
10+
*/
11+
@ClientEndpoint
12+
public class MyAsyncEndpointByteBufferClient {
13+
14+
private ByteBuffer receivedMessage;
15+
16+
@OnMessage
17+
public void onMessage(ByteBuffer msg) {
18+
receivedMessage = msg;
19+
}
20+
21+
public ByteBuffer getReceivedMessage() {
22+
return receivedMessage;
23+
}
24+
}

websocket/endpoint-async/src/main/java/org/javaee7/websocket/endpoint/async/MyEndpoint.java renamed to websocket/endpoint-async/src/main/java/org/javaee7/websocket/endpoint/async/MyAsyncEndpointText.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@
4646
/**
4747
* @author Arun Gupta
4848
*/
49-
@ServerEndpoint("/websocket")
50-
public class MyEndpoint {
51-
49+
@ServerEndpoint("/text")
50+
public class MyAsyncEndpointText {
51+
5252
@OnMessage
53-
public void echoText(String test, Session session) {
54-
session.getAsyncRemote().sendText(test);
53+
public void echoText(String text, Session session) {
54+
session.getAsyncRemote().sendText(text);
5555
}
5656
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.javaee7.websocket.endpoint.async;
2+
3+
import javax.websocket.*;
4+
5+
/**
6+
*
7+
* @author Jacek Jackowiak
8+
*/
9+
@ClientEndpoint
10+
public class MyAsyncEndpointTextClient {
11+
12+
private String receivedMessage;
13+
14+
@OnMessage
15+
public void onMessage(String msg) {
16+
receivedMessage = msg;
17+
}
18+
19+
public String getReceivedMessage() {
20+
return receivedMessage;
21+
}
22+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.javaee7.websocket.endpoint.async;
2+
3+
import org.jboss.arquillian.container.test.api.Deployment;
4+
import org.jboss.arquillian.junit.Arquillian;
5+
import org.jboss.arquillian.test.api.ArquillianResource;
6+
import org.jboss.shrinkwrap.api.ShrinkWrap;
7+
import org.jboss.shrinkwrap.api.spec.WebArchive;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import javax.websocket.*;
12+
import java.io.IOException;
13+
import java.io.UnsupportedEncodingException;
14+
import java.net.URI;
15+
import java.net.URISyntaxException;
16+
import java.net.URL;
17+
import java.nio.ByteBuffer;
18+
19+
import static com.jayway.awaitility.Awaitility.*;
20+
import static org.hamcrest.Matchers.*;
21+
import static org.hamcrest.MatcherAssert.*;
22+
23+
/**
24+
*
25+
* @author Jacek Jackowiak
26+
*/
27+
@RunWith(Arquillian.class)
28+
public class MyAsyncEndpointTest {
29+
30+
private static final String TEST_MESSAGE = "test";
31+
32+
@ArquillianResource
33+
private URL base;
34+
35+
@Deployment(testable = false)
36+
public static WebArchive deploy() throws URISyntaxException {
37+
return ShrinkWrap.create(WebArchive.class)
38+
.addClass(MyAsyncEndpointText.class)
39+
.addClass(MyAsyncEndpointByteBuffer.class);
40+
}
41+
42+
@Test
43+
public void shouldReceiveAsyncTextMessage() throws URISyntaxException, IOException, DeploymentException {
44+
MyAsyncEndpointTextClient endpoint = new MyAsyncEndpointTextClient();
45+
Session session = connectToEndpoint(endpoint, "text");
46+
47+
session.getAsyncRemote().sendText(TEST_MESSAGE);
48+
49+
await().untilCall(to(endpoint).getReceivedMessage(), is(equalTo(TEST_MESSAGE)));
50+
}
51+
52+
@Test
53+
public void shouldReceiveAsyncByteBufferMessage() throws URISyntaxException, IOException, DeploymentException {
54+
final ByteBuffer buffer = ByteBuffer.wrap(TEST_MESSAGE.getBytes());
55+
MyAsyncEndpointByteBufferClient endpoint = new MyAsyncEndpointByteBufferClient();
56+
Session session = connectToEndpoint(endpoint, "bytebuffer");
57+
58+
session.getAsyncRemote().sendBinary(buffer);
59+
60+
await().untilCall(to(endpoint).getReceivedMessage(), is(notNullValue()));
61+
String receivedString = bufferToString(endpoint.getReceivedMessage());
62+
assertThat(receivedString, is(equalTo(TEST_MESSAGE)));
63+
}
64+
65+
private String bufferToString(ByteBuffer buffer) throws UnsupportedEncodingException {
66+
byte[] bytes = new byte[buffer.remaining()];
67+
buffer.duplicate().get(bytes);
68+
return new String(bytes, "UTF-8");
69+
}
70+
71+
private Session connectToEndpoint(Object endpoint, String uriPart) throws URISyntaxException, DeploymentException, IOException {
72+
URI uri = getURI(uriPart);
73+
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
74+
return container.connectToServer(endpoint, uri);
75+
}
76+
77+
private URI getURI(String uriPart) throws URISyntaxException {
78+
return new URI("ws://"
79+
+ base.getHost()
80+
+ ":"
81+
+ base.getPort()
82+
+ base.getPath()
83+
+ uriPart);
84+
}
85+
}

0 commit comments

Comments
 (0)