Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@
<version>4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.tyrus</groupId>
<artifactId>tyrus-client</artifactId>
Expand Down Expand Up @@ -236,6 +242,12 @@
<profile>
<id>glassfish-remote-arquillian</id>
<dependencies>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.tyrus</groupId>
<artifactId>tyrus-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.websocket.ClientEndpoint;
import javax.websocket.EncodeException;
import javax.websocket.OnError;
Expand All @@ -57,6 +58,7 @@
decoders={MyMessageDecoder.class})
public class MyClient {
public static CountDownLatch latch= new CountDownLatch(3);
public static MyMessage response;

@OnOpen
public void onOpen(Session session) {
Expand All @@ -69,9 +71,9 @@ public void onOpen(Session session) {
}

@OnMessage
public MyMessage processMessage(MyMessage message) {
latch.countDown();
return message;
public void processMessage(MyMessage message) {
response = message;
latch.countDown();
}

@OnError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@

package org.javaee7.websocket.encoder.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;

import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.MessageHandler;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;

/**
Expand All @@ -47,16 +51,11 @@ public static WebArchive createDeployment() {

@Test
public void testEndpoint() throws URISyntaxException, DeploymentException, IOException, InterruptedException {
final String JSON = "{\"apple\" : \"red\", \"banana\": \"yellow\"}";
String JSON = "{\"apple\":\"red\",\"banana\":\"yellow\"}";
Session session = connectToServer(MyClient.class);
assertNotNull(session);
session.addMessageHandler(new MessageHandler.Whole<String>() {
@Override
public void onMessage(String text) {
assertEquals(JSON, text);
}
});
assertTrue(MyClient.latch.await(2, TimeUnit.SECONDS));
assertEquals(JSON, MyClient.response.toString());
}

public Session connectToServer(Class endpoint) throws DeploymentException, IOException, URISyntaxException {
Expand All @@ -67,9 +66,8 @@ public Session connectToServer(Class endpoint) throws DeploymentException, IOExc
+ base.getHost()
+ ":"
+ base.getPort()
+ "/"
+ base.getPath()
+ "/encoder-client");
+ "encoder-client");
return container.connectToServer(endpoint, uri);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
@ClientEndpoint(encoders = {MyMessageEncoder.class},
decoders={MyMessageDecoder.class})
public class MyClient {
public static CountDownLatch latch= new CountDownLatch(4);
public static CountDownLatch latch= new CountDownLatch(3);
public static MyMessage response;

@OnOpen
public void onOpen(Session session) {
Expand All @@ -30,9 +31,9 @@ public void onOpen(Session session) {
}

@OnMessage
public MyMessage processMessage(MyMessage message) {
latch.countDown();
return message;
public void processMessage(MyMessage message) {
response = message;
latch.countDown();
}

@OnError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
Expand All @@ -59,7 +60,6 @@ public void onOpen(final Session session, EndpointConfig ec) {
@Override
public void onMessage(String text) {
try {
MyClient.latch.countDown();
session.getBasicRemote().sendText(text);
} catch (IOException ex) {
Logger.getLogger(MyEndpoint.class.getName()).log(Level.SEVERE, null, ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.websocket.Decoder;
import javax.websocket.Encoder;
import javax.websocket.Endpoint;
Expand All @@ -65,14 +66,12 @@ public MyEndpointConfiguration() {

@Override
public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> set) {
return new HashSet<ServerEndpointConfig>() {
{
add(ServerEndpointConfig.Builder.create(MyEndpoint.class, "/encoder-programmatic")
Set<ServerEndpointConfig> config = new HashSet<ServerEndpointConfig>();
config.add(ServerEndpointConfig.Builder.create(MyEndpoint.class, "/encoder-programmatic")
.encoders(encoders)
.decoders(decoders)
.build());
}
};
return config;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package org.javaee7.websocket.encoder.programmatic;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;

import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.MessageHandler;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;

/**
Expand All @@ -31,37 +35,30 @@ public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class)
.addClasses(MyEndpoint.class,
MyEndpointConfiguration.class,
MyClient.class,
MyMessage.class,
MyMessageEncoder.class,
MyMessageDecoder.class);
}

@Test
public void testEndpoint() throws URISyntaxException, DeploymentException, IOException, InterruptedException {
final String JSON = "{\"apple\" : \"red\", \"banana\": \"yellow\"}";
final String JSON = "{\"apple\":\"red\",\"banana\":\"yellow\"}";
Session session = connectToServer(MyClient.class);
assertNotNull(session);
session.addMessageHandler(new MessageHandler.Whole<String>() {
@Override
public void onMessage(String text) {
assertEquals(JSON, text);
}
});
assertTrue(MyClient.latch.await(2, TimeUnit.SECONDS));
assertEquals(JSON, MyClient.response.toString());
}

public Session connectToServer(Class endpoint) throws DeploymentException, IOException, URISyntaxException {
public Session connectToServer(Class<?> endpoint) throws DeploymentException, IOException, URISyntaxException {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
assertNotNull(container);
assertNotNull(base);
URI uri = new URI("ws://"
+ base.getHost()
+ ":"
+ base.getPort()
+ "/"
+ base.getPath()
+ "/encoder-programmatic");
+ "encoder-programmatic");
return container.connectToServer(endpoint, uri);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
@ClientEndpoint
public class MyEndpointClientEmptyJSONArray {
public static String JSON = "{}";
public static CountDownLatch latch= new CountDownLatch(3);
public static CountDownLatch latch= new CountDownLatch(1);
public static String response;

@OnOpen
public void onOpen(Session session) {
Expand All @@ -27,8 +28,8 @@ public void onOpen(Session session) {
}

@OnMessage
public String processMessage(String message) {
public void processMessage(String message) {
response = message;
latch.countDown();
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
*/
@ClientEndpoint
public class MyEndpointClientJSONObject {
public static CountDownLatch latch = new CountDownLatch(3);
public static CountDownLatch latch = new CountDownLatch(1);
public static String JSON = "{\"apple\" : \"red\", \"banana\": \"yellow\"}";
public static String response;

@OnOpen
public void onOpen(Session session) {
Expand All @@ -27,8 +28,8 @@ public void onOpen(Session session) {
}

@OnMessage
public String processMessage(String message) {
public void processMessage(String message) {
response = message;
latch.countDown();
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ public class MyMessageDecoder implements Decoder.Text<MyMessage> {

@Override
public MyMessage decode(String string) throws DecodeException {
if (MyEndpointClientEmptyJSONArray.latch != null)
MyEndpointClientEmptyJSONArray.latch.countDown();

if (MyEndpointClientJSONObject.latch != null)
MyEndpointClientJSONObject.latch.countDown();

MyMessage myMessage = new MyMessage(Json.createReader(new StringReader(string)).readObject());
return myMessage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@
public class MyMessageEncoder implements Encoder.Text<MyMessage> {
@Override
public String encode(MyMessage myMessage) throws EncodeException {
if (MyEndpointClientEmptyJSONArray.latch != null)
MyEndpointClientEmptyJSONArray.latch.countDown();

if (MyEndpointClientJSONObject.latch != null)
MyEndpointClientJSONObject.latch.countDown();

return myMessage.getJsonObject().toString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package org.javaee7.websocket.encoder;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.MessageHandler;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

Expand Down Expand Up @@ -43,36 +42,24 @@ public static WebArchive createDeployment() {
.addClasses(MyEndpoint.class,
MyMessage.class,
MyMessageEncoder.class,
MyMessageDecoder.class,
MyEndpointClientEmptyJSONArray.class,
MyEndpointClientJSONObject.class);
MyMessageDecoder.class);
}

@Test
public void testEndpointEmptyJSONArray() throws URISyntaxException, DeploymentException, IOException, InterruptedException {
final Session session = connectToServer(MyEndpointClientEmptyJSONArray.class);
assertNotNull(session);
session.addMessageHandler(new MessageHandler.Whole<String>() {
@Override
public void onMessage(String text) {
assertEquals("{}", text);
}
});
assertTrue(MyEndpointClientEmptyJSONArray.latch.await(2, TimeUnit.SECONDS));
assertEquals("{}", MyEndpointClientEmptyJSONArray.response);
}

@Test
public void testEndpointEmptyJSONObject() throws URISyntaxException, DeploymentException, IOException, InterruptedException {
final String JSON = "{\"apple\" : \"red\", \"banana\": \"yellow\"}";
String JSON = "{\"apple\":\"red\",\"banana\":\"yellow\"}";
Session session = connectToServer(MyEndpointClientJSONObject.class);
assertNotNull(session);
session.addMessageHandler(new MessageHandler.Whole<String>() {
@Override
public void onMessage(String text) {
assertEquals(JSON, text);
}
});
assertTrue(MyEndpointClientJSONObject.latch.await(2, TimeUnit.SECONDS));
assertEquals(JSON, MyEndpointClientJSONObject.response);
}

/**
Expand All @@ -85,15 +72,14 @@ public void onMessage(String text) {
* @throws IOException
* @throws URISyntaxException
*/
public Session connectToServer(Class endpoint) throws DeploymentException, IOException, URISyntaxException {
public Session connectToServer(Class<?> endpoint) throws DeploymentException, IOException, URISyntaxException {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
URI uri = new URI("ws://"
+ base.getHost()
+ ":"
+ base.getPort()
+ "/"
+ base.getPath()
+ "/encoder");
+ "encoder");
return container.connectToServer(endpoint, uri);
}
}
Loading