Skip to content

Commit f9a1da3

Browse files
committed
Adding a new test, still failing with handshake
1 parent 558ac5c commit f9a1da3

File tree

7 files changed

+142
-163
lines changed

7 files changed

+142
-163
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public void onOpen(final Session session, EndpointConfig ec) {
6262
@Override
6363
public void onMessage(String text) {
6464
try {
65+
MyEndpointTextClient.latch.countDown();
6566
session.getBasicRemote().sendText(text);
6667
} catch (IOException ex) {
6768
Logger.getLogger(MyEndpoint.class.getName()).log(Level.SEVERE, null, ex);
@@ -74,6 +75,7 @@ public void onMessage(String text) {
7475
@Override
7576
public void onMessage(ByteBuffer t) {
7677
try {
78+
MyEndpointBinaryClient.latch.countDown();
7779
session.getBasicRemote().sendBinary(t);
7880
} catch (IOException ex) {
7981
Logger.getLogger(MyEndpoint.class.getName()).log(Level.SEVERE, null, ex);
@@ -93,11 +95,11 @@ public void onMessage(PongMessage t) {
9395

9496
@Override
9597
public void onClose(Session session, CloseReason closeReason) {
96-
System.out.println("Closing: " + closeReason.getReasonPhrase());
98+
System.err.println("Closing: " + closeReason.getReasonPhrase());
9799
}
98100

99101
@Override
100102
public void onError(Session session, Throwable t) {
101-
System.out.println("Error: " + t.getLocalizedMessage());
103+
System.err.println("Error: " + t.getLocalizedMessage());
102104
}
103105
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.javaee7.websocket.endpoint.programmatic;
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.OnOpen;
9+
import javax.websocket.Session;
10+
11+
/**
12+
* @author Arun Gupta
13+
*/
14+
@ClientEndpoint
15+
public class MyEndpointBinaryClient {
16+
public static CountDownLatch latch= new CountDownLatch(2);
17+
public static byte[] response;
18+
19+
@OnOpen
20+
public void onOpen(Session session) {
21+
try {
22+
latch.countDown();
23+
session.getBasicRemote().sendBinary(ByteBuffer.wrap("Hello World!".getBytes()));
24+
} catch (IOException ioe) {
25+
ioe.printStackTrace();
26+
}
27+
}
28+
}

websocket/endpoint-programmatic/src/main/java/org/javaee7/websocket/endpoint/programmatic/MyApplicationConfig.java renamed to websocket/endpoint-programmatic/src/main/java/org/javaee7/websocket/endpoint/programmatic/MyEndpointConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
/**
5050
* @author Arun Gupta
5151
*/
52-
public class MyApplicationConfig implements ServerApplicationConfig {
52+
public class MyEndpointConfig implements ServerApplicationConfig {
5353

5454
@Override
5555
public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> set) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.javaee7.websocket.endpoint.programmatic;
2+
3+
import java.io.IOException;
4+
import java.util.concurrent.CountDownLatch;
5+
6+
import javax.websocket.ClientEndpoint;
7+
import javax.websocket.OnOpen;
8+
import javax.websocket.Session;
9+
10+
/**
11+
* @author Arun Gupta
12+
*/
13+
@ClientEndpoint
14+
public class MyEndpointTextClient {
15+
public static CountDownLatch latch= new CountDownLatch(2);
16+
public static byte[] response;
17+
18+
@OnOpen
19+
public void onOpen(Session session) {
20+
try {
21+
latch.countDown();
22+
session.getBasicRemote().sendText("Hello world!");
23+
} catch (IOException ioe) {
24+
ioe.printStackTrace();
25+
}
26+
}
27+
}

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

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

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

Lines changed: 0 additions & 90 deletions
This file was deleted.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.javaee7.websocket.endpoint.programmatic;
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.TimeUnit;
8+
import javax.websocket.ContainerProvider;
9+
import javax.websocket.DeploymentException;
10+
import javax.websocket.MessageHandler;
11+
import javax.websocket.Session;
12+
import javax.websocket.WebSocketContainer;
13+
import org.jboss.arquillian.container.test.api.Deployment;
14+
import org.jboss.arquillian.junit.Arquillian;
15+
import org.jboss.arquillian.test.api.ArquillianResource;
16+
import org.jboss.shrinkwrap.api.ShrinkWrap;
17+
import org.jboss.shrinkwrap.api.spec.WebArchive;
18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.Assert.assertTrue;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
24+
/**
25+
* @author Arun Gupta
26+
*/
27+
@RunWith(Arquillian.class)
28+
public class MyEndpointTest {
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+
MyEndpointConfig.class,
38+
MyEndpointTextClient.class,
39+
MyEndpointBinaryClient.class);
40+
}
41+
42+
@Test
43+
public void testTextEndpoint() throws URISyntaxException, DeploymentException, IOException, InterruptedException {
44+
final String TEXT = "Hello World!";
45+
Session session = connectToServer(MyEndpointTextClient.class);
46+
assertNotNull(session);
47+
session.addMessageHandler(new MessageHandler.Whole<String>() {
48+
@Override
49+
public void onMessage(String text) {
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+
final String TEXT = "Hello World!";
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+
assertEquals(TEXT, binary);
65+
}
66+
});
67+
assertTrue(MyEndpointBinaryClient.latch.await(2, TimeUnit.SECONDS));
68+
}
69+
70+
public Session connectToServer(Class endpoint) throws DeploymentException, IOException, URISyntaxException {
71+
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
72+
URI uri = new URI("ws://"
73+
+ base.getHost()
74+
+ ":"
75+
+ base.getPort()
76+
+ "/"
77+
+ base.getPath()
78+
+ "/websocket");
79+
System.out.println("Connecting to: " + uri);
80+
return container.connectToServer(endpoint, uri);
81+
}
82+
}

0 commit comments

Comments
 (0)