Adding HubConnection Tests for the Java Client(#2550)
This commit is contained in:
parent
f4bf222099
commit
13a3dfe8dc
|
|
@ -5,18 +5,18 @@ import java.net.URISyntaxException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class HubConnection {
|
public class HubConnection {
|
||||||
private String _url;
|
private String url;
|
||||||
private Transport _transport;
|
private Transport transport;
|
||||||
private OnReceiveCallBack callback;
|
private OnReceiveCallBack callback;
|
||||||
private HashMap<String, Action> handlers = new HashMap<>();
|
private HashMap<String, Action> handlers = new HashMap<>();
|
||||||
private HubProtocol protocol;
|
private HubProtocol protocol;
|
||||||
|
|
||||||
public Boolean connected = false;
|
public Boolean connected = false;
|
||||||
|
|
||||||
public HubConnection(String url) {
|
public HubConnection(String url, Transport transport){
|
||||||
_url = url;
|
this.url = url;
|
||||||
protocol = new JsonHubProtocol();
|
this.protocol = new JsonHubProtocol();
|
||||||
callback = (payload) -> {
|
this.callback = (payload) -> {
|
||||||
|
|
||||||
InvocationMessage[] messages = protocol.parseMessages(payload);
|
InvocationMessage[] messages = protocol.parseMessages(payload);
|
||||||
|
|
||||||
|
|
@ -29,28 +29,36 @@ public class HubConnection {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
if (transport == null){
|
||||||
_transport = new WebSocketTransport(_url);
|
try {
|
||||||
} catch (URISyntaxException e) {
|
this.transport = new WebSocketTransport(this.url);
|
||||||
e.printStackTrace();
|
} catch (URISyntaxException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.transport = transport;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HubConnection(String url) {
|
||||||
|
this(url, null);
|
||||||
|
}
|
||||||
|
|
||||||
public void start() throws InterruptedException {
|
public void start() throws InterruptedException {
|
||||||
_transport.setOnReceive(this.callback);
|
transport.setOnReceive(this.callback);
|
||||||
_transport.start();
|
transport.start();
|
||||||
connected = true;
|
connected = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop(){
|
public void stop(){
|
||||||
_transport.stop();
|
transport.stop();
|
||||||
connected = false;
|
connected = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void send(String method, Object... args) {
|
public void send(String method, Object... args) {
|
||||||
InvocationMessage invocationMessage = new InvocationMessage(method, args);
|
InvocationMessage invocationMessage = new InvocationMessage(method, args);
|
||||||
String message = protocol.writeMessage(invocationMessage);
|
String message = protocol.writeMessage(invocationMessage);
|
||||||
_transport.send(message);
|
transport.send(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void On(String target, Action callback) {
|
public void On(String target, Action callback) {
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,86 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class HubConnectionTest {
|
public class HubConnectionTest {
|
||||||
// Coming soon!
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmptyCollection() {
|
public void checkHubConnectionState() throws InterruptedException {
|
||||||
|
Transport mockTransport = new MockEchoTransport();
|
||||||
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
|
||||||
|
hubConnection.start();
|
||||||
|
assertTrue(hubConnection.connected);
|
||||||
|
|
||||||
|
hubConnection.stop();
|
||||||
|
assertFalse(hubConnection.connected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void SendWithNoParamsTriggersOnHandler() throws InterruptedException {
|
||||||
|
AtomicInteger value = new AtomicInteger(0);
|
||||||
|
Transport mockTransport = new MockEchoTransport();
|
||||||
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
|
||||||
|
|
||||||
|
Action callback = (param) -> {
|
||||||
|
assertEquals(0, value.get());
|
||||||
|
value.incrementAndGet();
|
||||||
|
};
|
||||||
|
hubConnection.On("inc", callback);
|
||||||
|
|
||||||
|
hubConnection.start();
|
||||||
|
hubConnection.send("inc");
|
||||||
|
|
||||||
|
// Confirming that our handler was called and that the counter property was incremented.
|
||||||
|
assertEquals(1, value.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void SendWithParamTriggersOnHandler() throws InterruptedException {
|
||||||
|
AtomicReference<String> value = new AtomicReference<>();
|
||||||
|
Transport mockTransport = new MockEchoTransport();
|
||||||
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
|
||||||
|
|
||||||
|
Action callback = (param) -> {
|
||||||
|
assertNull(value.get());
|
||||||
|
value.set(((JsonArray) param).get(0).getAsString());
|
||||||
|
};
|
||||||
|
hubConnection.On("inc", callback);
|
||||||
|
|
||||||
|
hubConnection.start();
|
||||||
|
hubConnection.send("inc", "Hello World");
|
||||||
|
|
||||||
|
// Confirming that our handler was called and the correct message was passed in.
|
||||||
|
assertEquals("Hello World", value.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
private class MockEchoTransport implements Transport {
|
||||||
|
private OnReceiveCallBack onReceiveCallBack;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void send(String message) {
|
||||||
|
this.onReceive(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setOnReceive(OnReceiveCallBack callback) {
|
||||||
|
this.onReceiveCallBack = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive(String message) {
|
||||||
|
this.onReceiveCallBack.invoke(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop() {return;}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue