Skip to content

Commit eea7018

Browse files
committed
Tests are working again, WAR can be deployed on WildFly and GlassFish. Clients are working with Tyrys/Grizzly dependencies but need to be clearly resolved for WildFly. Waiting for Undertow bug (number??) to be fixed.
1 parent 3ef5d3f commit eea7018

File tree

6 files changed

+38
-30
lines changed

6 files changed

+38
-30
lines changed

websocket/binary/src/main/java/org/javaee7/websocket/binary/MyEndpointByteArray.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@
4040
package org.javaee7.websocket.binary;
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
/**
@@ -51,8 +49,7 @@
5149
@ServerEndpoint("/bytearray")
5250
public class MyEndpointByteArray {
5351
@OnMessage
54-
public void echoBinary(byte[] data, Session session) throws IOException {
55-
System.out.println("echoBinary (byte[]): " + data);
56-
session.getBasicRemote().sendBinary(ByteBuffer.wrap(data));
52+
public byte[] echoBinary(byte[] data) throws IOException {
53+
return data;
5754
}
5855
}

websocket/binary/src/main/java/org/javaee7/websocket/binary/MyEndpointByteBuffer.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import java.io.IOException;
4343
import java.nio.ByteBuffer;
4444
import javax.websocket.OnMessage;
45-
import javax.websocket.Session;
4645
import javax.websocket.server.ServerEndpoint;
4746

4847
/**
@@ -51,8 +50,8 @@
5150
@ServerEndpoint("/bytebuffer")
5251
public class MyEndpointByteBuffer {
5352
@OnMessage
54-
public void echoBinary(ByteBuffer data, Session session) throws IOException {
55-
System.out.println("echoBinary (ByteBuffer): " + data);
56-
session.getBasicRemote().sendBinary(data);
53+
public ByteBuffer echoBinary(ByteBuffer data) throws IOException {
54+
System.out.println("echoBinary");
55+
return data;
5756
}
5857
}

websocket/binary/src/main/java/org/javaee7/websocket/binary/MyEndpointClient.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.nio.ByteBuffer;
88

99
import javax.websocket.ClientEndpoint;
10+
import javax.websocket.OnMessage;
1011
import javax.websocket.OnOpen;
1112
import javax.websocket.Session;
1213

@@ -16,14 +17,19 @@
1617
*/
1718
@ClientEndpoint
1819
public class MyEndpointClient {
20+
public static byte[] response;
1921

2022
@OnOpen
2123
public void onOpen(Session session) {
22-
System.out.println("[Action]->Invokint method onOpen of the class:" + this.getClass().getCanonicalName());
2324
try {
2425
session.getBasicRemote().sendBinary(ByteBuffer.wrap("Hello World!".getBytes()));
2526
} catch (IOException ioe) {
2627
ioe.printStackTrace();
2728
}
2829
}
30+
31+
@OnMessage
32+
public void processMessage(byte[] message) {
33+
MyEndpointClient.response = message;
34+
}
2935
}

websocket/binary/src/main/java/org/javaee7/websocket/binary/MyEndpointInputStream.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ public class MyEndpointInputStream {
5555
@OnMessage
5656
public void echoStream(InputStream stream, Session session) throws IOException {
5757
System.out.println("echoStream: " + stream);
58-
byte[] b = new byte[8];
59-
int n = stream.read(b);
60-
System.out.println("read " + n + " bytes");
58+
byte[] b = new byte[12];
59+
stream.read(b);
6160
session.getBasicRemote().sendBinary(ByteBuffer.wrap(b));
6261
}
6362
}

websocket/binary/src/main/webapp/websocket.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,14 @@ var output = document.getElementById("output");
6868

6969
function onOpenByteArray() {
7070
console.log("onOpen (byte])");
71-
writeToScreen("CONNECTED (byte[])");
7271
}
7372

7473
function onOpenByteBuffer() {
7574
console.log("onOpen (ByteBuffer)");
76-
writeToScreen("CONNECTED (ByteBuffer)");
7775
}
7876

7977
function onOpenInputStream() {
8078
console.log("onOpen (InputStream)");
81-
writeToScreen("CONNECTED (InputStream)");
8279
}
8380

8481
function echoBinaryByteArray() {

websocket/binary/src/test/java/org/javaee7/websocket/binary/test/WebsocketBinaryEndpointTest.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*/
44
package org.javaee7.websocket.binary.test;
55

6-
76
import java.io.File;
87
import java.io.IOException;
98
import java.net.URI;
@@ -14,8 +13,6 @@
1413
import javax.websocket.Session;
1514
import javax.websocket.WebSocketContainer;
1615

17-
import static junit.framework.Assert.assertNull;
18-
1916
import org.javaee7.websocket.binary.MyEndpointByteArray;
2017
import org.javaee7.websocket.binary.MyEndpointByteBuffer;
2118
import org.javaee7.websocket.binary.MyEndpointClient;
@@ -25,17 +22,19 @@
2522
import org.jboss.arquillian.junit.Arquillian;
2623
import org.jboss.shrinkwrap.api.ShrinkWrap;
2724
import org.jboss.shrinkwrap.api.spec.WebArchive;
25+
import static org.junit.Assert.*;
2826
import org.junit.Test;
2927
import org.junit.runner.RunWith;
3028

3129
/**
3230
* @author Nikos Ballas
3331
* @author Arun Gupta
3432
*/
35-
@RunWith(Arquillian.class)
33+
//@RunWith(Arquillian.class)
3634
public class WebsocketBinaryEndpointTest {
3735

3836
private static final String WEBAPP_SRC = "src/main/webapp";
37+
private static final String RESPONSE = "Hello World!";
3938

4039
/**
4140
* Arquillian specific method for creating a file which can be deployed
@@ -45,14 +44,13 @@ public class WebsocketBinaryEndpointTest {
4544
* arquillian.xml file.
4645
*/
4746
@Deployment(testable = false)
48-
@TargetsContainer("wildfly-arquillian")
47+
// @TargetsContainer("wildfly-arquillian")
4948
public static WebArchive createDeployment() {
5049
WebArchive war = ShrinkWrap.create(WebArchive.class)
5150
.addClass(MyEndpointByteBuffer.class)
5251
.addClass(MyEndpointByteArray.class)
5352
.addClass(MyEndpointInputStream.class)
5453
.addClass(MyEndpointClient.class)
55-
.addAsWebResource(new File(WEBAPP_SRC, "index.jsp"))
5654
.addAsWebResource(new File(WEBAPP_SRC, "websocket.js"));
5755
return war;
5856
}
@@ -65,9 +63,13 @@ public static WebArchive createDeployment() {
6563
* @throws IOException
6664
*/
6765
@Test
68-
public void testEndpointByteBuffer() throws URISyntaxException, DeploymentException, IOException {
66+
public void testEndpointByteBuffer() throws URISyntaxException, DeploymentException, IOException, InterruptedException {
6967
Session session = connectToServer("bytebuffer");
70-
assertNull(session);
68+
assertNotNull(session);
69+
System.out.println("Waiting for 2 seconds to receive response");
70+
Thread.sleep(2000);
71+
assertNotNull(MyEndpointClient.response);
72+
assertArrayEquals(RESPONSE.getBytes(), MyEndpointClient.response);
7173
}
7274

7375
/**
@@ -80,9 +82,13 @@ public void testEndpointByteBuffer() throws URISyntaxException, DeploymentExcept
8082
* @throws URISyntaxException
8183
*/
8284
@Test
83-
public void testEndpointByteArray() throws DeploymentException, IOException, URISyntaxException {
85+
public void testEndpointByteArray() throws DeploymentException, IOException, URISyntaxException, InterruptedException {
8486
Session session = connectToServer("bytearray");
85-
assertNull(session);
87+
assertNotNull(session);
88+
System.out.println("Waiting for 2 seconds to receive response");
89+
Thread.sleep(2000);
90+
assertNotNull(MyEndpointClient.response);
91+
assertArrayEquals(RESPONSE.getBytes(), MyEndpointClient.response);
8692
}
8793

8894
/**
@@ -95,9 +101,13 @@ public void testEndpointByteArray() throws DeploymentException, IOException, URI
95101
* @throws URISyntaxException
96102
*/
97103
@Test
98-
public void testEndpointInputStream() throws DeploymentException, IOException, URISyntaxException {
104+
public void testEndpointInputStream() throws DeploymentException, IOException, URISyntaxException, InterruptedException {
99105
Session session = connectToServer("inputstream");
100-
assertNull(session);
106+
assertNotNull(session);
107+
System.out.println("Waiting for 2 seconds to receive response");
108+
Thread.sleep(2000);
109+
assertNotNull(MyEndpointClient.response);
110+
assertArrayEquals(RESPONSE.getBytes(), MyEndpointClient.response);
101111
}
102112

103113
/**
@@ -111,7 +121,7 @@ public void testEndpointInputStream() throws DeploymentException, IOException, U
111121
* @throws URISyntaxException
112122
*/
113123
public Session connectToServer(String endpoint) throws DeploymentException, IOException, URISyntaxException {
114-
WebSocketContainer wSocketContainer = ContainerProvider.getWebSocketContainer();
115-
return wSocketContainer.connectToServer(MyEndpointClient.class, new URI("ws://localhost:8080/binary/" + endpoint));
124+
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
125+
return container.connectToServer(MyEndpointClient.class, new URI("ws://localhost:8080/binary/" + endpoint));
116126
}
117127
}

0 commit comments

Comments
 (0)