Skip to content

Commit e8bd4d7

Browse files
committed
Moving binary tests to endpoint
1 parent eb8ad05 commit e8bd4d7

File tree

9 files changed

+284
-26
lines changed

9 files changed

+284
-26
lines changed

websocket/endpoint/src/main/java/org/javaee7/websocket/endpoint/MyEndpoint.java renamed to websocket/endpoint/src/main/java/org/javaee7/websocket/endpoint/MyEndpointByteArray.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,8 @@
4646
/**
4747
* @author Arun Gupta
4848
*/
49-
@ServerEndpoint("/websocket")
50-
public class MyEndpoint {
51-
52-
@OnMessage
53-
public String echoText(String name) {
54-
return name;
55-
}
56-
49+
@ServerEndpoint("/bytearray")
50+
public class MyEndpointByteArray {
5751
@OnMessage
5852
public byte[] echoBinary(byte[] data) throws IOException {
5953
return data;

websocket/endpoint/src/main/java/org/javaee7/websocket/endpoint/MyEndpointBinaryClient.java renamed to websocket/endpoint/src/main/java/org/javaee7/websocket/endpoint/MyEndpointByteArrayClient.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
* @author Arun Gupta
1414
*/
1515
@ClientEndpoint
16-
public class MyEndpointBinaryClient {
16+
public class MyEndpointByteArrayClient {
1717
public static CountDownLatch latch;
18+
public static byte[] response;
1819

1920
@OnOpen
2021
public void onOpen(Session session) {
@@ -27,6 +28,7 @@ public void onOpen(Session session) {
2728

2829
@OnMessage
2930
public void processMessage(byte[] message) {
31+
response = message;
3032
latch.countDown();
3133
}
3234
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.websocket.endpoint;
41+
42+
import java.io.IOException;
43+
import java.nio.ByteBuffer;
44+
import javax.websocket.OnMessage;
45+
import javax.websocket.server.ServerEndpoint;
46+
47+
/**
48+
* @author Arun Gupta
49+
*/
50+
@ServerEndpoint("/bytebuffer")
51+
public class MyEndpointByteBuffer {
52+
@OnMessage
53+
public ByteBuffer echoBinary(ByteBuffer data) throws IOException {
54+
System.out.println("echoBinary");
55+
return data;
56+
}
57+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 MyEndpointByteBufferClient {
17+
public static CountDownLatch latch;
18+
public static byte[] response;
19+
20+
@OnOpen
21+
public void onOpen(Session session) {
22+
try {
23+
session.getBasicRemote().sendBinary(ByteBuffer.wrap("Hello World!".getBytes()));
24+
} catch (IOException ioe) {
25+
ioe.printStackTrace();
26+
}
27+
}
28+
29+
@OnMessage
30+
public void processMessage(byte[] message) {
31+
response = message;
32+
latch.countDown();
33+
}
34+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.websocket.endpoint;
41+
42+
import java.io.IOException;
43+
import java.io.InputStream;
44+
import java.nio.ByteBuffer;
45+
import javax.websocket.OnMessage;
46+
import javax.websocket.Session;
47+
import javax.websocket.server.ServerEndpoint;
48+
49+
/**
50+
* @author Arun Gupta
51+
*/
52+
@ServerEndpoint("/inputstream")
53+
public class MyEndpointInputStream {
54+
55+
@OnMessage
56+
public void echoStream(InputStream stream, Session session) throws IOException {
57+
System.out.println("echoStream: " + stream);
58+
byte[] b = new byte[12];
59+
stream.read(b);
60+
session.getBasicRemote().sendBinary(ByteBuffer.wrap(b));
61+
}
62+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 MyEndpointInputStreamClient {
17+
public static CountDownLatch latch;
18+
public static byte[] response;
19+
20+
@OnOpen
21+
public void onOpen(Session session) {
22+
try {
23+
session.getBasicRemote().sendBinary(ByteBuffer.wrap("Hello World!".getBytes()));
24+
} catch (IOException ioe) {
25+
ioe.printStackTrace();
26+
}
27+
}
28+
29+
@OnMessage
30+
public void processMessage(byte[] message) {
31+
response = message;
32+
latch.countDown();
33+
}
34+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.websocket.endpoint;
41+
42+
import javax.websocket.OnMessage;
43+
import javax.websocket.server.ServerEndpoint;
44+
45+
/**
46+
* @author Arun Gupta
47+
*/
48+
@ServerEndpoint("/text")
49+
public class MyEndpointText {
50+
51+
@OnMessage
52+
public String echoText(String name) {
53+
return name;
54+
}
55+
}

websocket/endpoint/src/test/java/org/javaee7/websocket/endpoint/MyEndpointTest.java

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,20 @@ public class MyEndpointTest {
3333
@Deployment(testable=false)
3434
public static WebArchive createDeployment() {
3535
return ShrinkWrap.create(WebArchive.class)
36-
.addClasses(MyEndpoint.class,
36+
.addClasses(MyEndpointText.class,
3737
MyEndpointTextClient.class,
38-
MyEndpointBinaryClient.class);
38+
MyEndpointByteArray.class,
39+
MyEndpointByteArrayClient.class,
40+
MyEndpointByteBuffer.class,
41+
MyEndpointByteBufferClient.class,
42+
MyEndpointInputStream.class,
43+
MyEndpointInputStreamClient.class);
3944
}
4045

4146
@Test
4247
public void testTextEndpoint() throws URISyntaxException, DeploymentException, IOException, InterruptedException {
4348
MyEndpointTextClient.latch = new CountDownLatch(1);
44-
Session session = connectToServer(MyEndpointTextClient.class);
49+
Session session = connectToServer(MyEndpointTextClient.class, "/text");
4550
assertNotNull(session);
4651
session.addMessageHandler(new MessageHandler.Whole<String>() {
4752
@Override
@@ -54,29 +59,45 @@ public void onMessage(String text) {
5459
}
5560

5661
@Test
57-
public void testBinaryEndpoint() throws URISyntaxException, DeploymentException, IOException, InterruptedException {
58-
MyEndpointBinaryClient.latch = new CountDownLatch(1);
59-
Session session = connectToServer(MyEndpointBinaryClient.class);
62+
public void testEndpointByteBuffer() throws URISyntaxException, DeploymentException, IOException, InterruptedException {
63+
MyEndpointByteBufferClient.latch = new CountDownLatch(1);
64+
Session session = connectToServer(MyEndpointByteBufferClient.class, "bytebuffer");
6065
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));
66+
assertTrue(MyEndpointByteBufferClient.latch.await(2, TimeUnit.SECONDS));
67+
assertArrayEquals(TEXT.getBytes(), MyEndpointByteBufferClient.response);
68+
}
69+
70+
@Test
71+
public void testEndpointByteArray() throws DeploymentException, IOException, URISyntaxException, InterruptedException {
72+
MyEndpointByteArrayClient.latch = new CountDownLatch(1);
73+
Session session = connectToServer(MyEndpointByteArrayClient.class, "bytearray");
74+
assertNotNull(session);
75+
assertTrue(MyEndpointByteArrayClient.latch.await(2, TimeUnit.SECONDS));
76+
assertNotNull(MyEndpointByteArrayClient.response);
77+
assertArrayEquals(TEXT.getBytes(), MyEndpointByteArrayClient.response);
6978
}
7079

71-
public Session connectToServer(Class endpoint) throws DeploymentException, IOException, URISyntaxException {
80+
@Test
81+
public void testEndpointInputStream() throws DeploymentException, IOException, URISyntaxException, InterruptedException {
82+
MyEndpointInputStreamClient.latch = new CountDownLatch(1);
83+
Session session = connectToServer(MyEndpointInputStreamClient.class, "inputstream");
84+
assertNotNull(session);
85+
assertTrue(MyEndpointInputStreamClient.latch.await(2, TimeUnit.SECONDS));
86+
assertNotNull(MyEndpointInputStreamClient.response);
87+
assertArrayEquals(TEXT.getBytes(), MyEndpointInputStreamClient.response);
88+
}
89+
90+
91+
public Session connectToServer(Class endpoint, String uriPart) throws DeploymentException, IOException, URISyntaxException {
7292
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
7393
URI uri = new URI("ws://"
7494
+ base.getHost()
7595
+ ":"
7696
+ base.getPort()
7797
+ "/"
7898
+ base.getPath()
79-
+ "/websocket");
99+
+ "/"
100+
+ uriPart);
80101
System.out.println("Connecting to: " + uri);
81102
return container.connectToServer(endpoint, uri);
82103
}

websocket/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
<name>Java EE 7 WebSocket Samples</name>
1515

1616
<modules>
17-
<module>binary</module>
1817
<module>chat</module>
1918
<module>encoder</module>
2019
<module>encoder-client</module>

0 commit comments

Comments
 (0)