[Java] Cleanup exceptions (#3110)

This commit is contained in:
BrennanConroy 2018-10-10 16:59:29 -07:00 committed by GitHub
parent 286e4bebf7
commit 9049bf709c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 227 additions and 226 deletions

View File

@ -3,7 +3,6 @@
package com.microsoft.signalr;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Date;
@ -197,12 +196,7 @@ public class HubConnection {
if (response.getStatusCode() != 200) {
throw new RuntimeException(String.format("Unexpected status code returned from negotiate: %d %s.", response.getStatusCode(), response.getStatusText()));
}
NegotiateResponse negotiateResponse;
try {
negotiateResponse = new NegotiateResponse(response.getContent());
} catch (IOException e) {
throw new RuntimeException(e);
}
NegotiateResponse negotiateResponse = new NegotiateResponse(response.getContent());
if (negotiateResponse.getError() != null) {
throw new RuntimeException(negotiateResponse.getError());
@ -409,18 +403,18 @@ public class HubConnection {
*
* @param method The name of the server method to invoke.
* @param args The arguments to be passed to the method.
* @throws Exception If there was an error while sending.
*/
public void send(String method, Object... args) throws Exception {
public void send(String method, Object... args) {
if (hubConnectionState != HubConnectionState.CONNECTED) {
throw new HubException("The 'send' method cannot be called if the connection is not active");
throw new RuntimeException("The 'send' method cannot be called if the connection is not active");
}
InvocationMessage invocationMessage = new InvocationMessage(null, method, args);
sendHubMessage(invocationMessage);
}
public <T> Single<T> invoke(Class<T> returnType, String method, Object... args) throws Exception {
@SuppressWarnings("unchecked")
public <T> Single<T> invoke(Class<T> returnType, String method, Object... args) {
String id = connectionState.getNextInvocationId();
InvocationMessage invocationMessage = new InvocationMessage(id, method, args);
@ -451,7 +445,7 @@ public class HubConnection {
return Single.fromFuture(future);
}
private void sendHubMessage(HubMessage message) throws Exception {
private void sendHubMessage(HubMessage message) {
String serializedMessage = protocol.writeMessage(message);
if (message.getMessageType() == HubMessageType.INVOCATION) {
logger.log(LogLevel.Debug, "Sending %s message '%s'.", message.getMessageType().name(), ((InvocationMessage)message).getInvocationId());
@ -777,7 +771,7 @@ public class HubConnection {
}
@Override
public List<Class<?>> getParameterTypes(String methodName) throws Exception {
public List<Class<?>> getParameterTypes(String methodName) {
List<InvocationHandler> handlers = connection.handlers.get(methodName);
if (handlers == null) {
logger.log(LogLevel.Warning, "Failed to find handler for '%s' method.", methodName);
@ -785,7 +779,7 @@ public class HubConnection {
}
if (handlers.isEmpty()) {
throw new Exception(String.format("There are no callbacks registered for the method '%s'.", methodName));
throw new RuntimeException(String.format("There are no callbacks registered for the method '%s'.", methodName));
}
return handlers.get(0).getClasses();

View File

@ -3,7 +3,7 @@
package com.microsoft.signalr;
public class HubException extends Exception {
public class HubException extends RuntimeException {
private static final long serialVersionUID = -572019264269821519L;
public HubException() {

View File

@ -16,7 +16,7 @@ interface HubProtocol {
* @param message A string representation of one or more {@link HubMessage}s.
* @return A list of {@link HubMessage}s.
*/
HubMessage[] parseMessages(String message, InvocationBinder binder) throws Exception;
HubMessage[] parseMessages(String message, InvocationBinder binder);
/**
* Writes the specified {@link HubMessage} to a String.

View File

@ -7,5 +7,5 @@ import java.util.List;
interface InvocationBinder {
Class<?> getReturnType(String invocationId);
List<Class<?>> getParameterTypes(String methodName) throws Exception;
List<Class<?>> getParameterTypes(String methodName);
}

View File

@ -36,105 +36,109 @@ class JsonHubProtocol implements HubProtocol {
}
@Override
public HubMessage[] parseMessages(String payload, InvocationBinder binder) throws Exception {
public HubMessage[] parseMessages(String payload, InvocationBinder binder) {
if (payload != null && !payload.substring(payload.length() - 1).equals(RECORD_SEPARATOR)) {
throw new RuntimeException("Message is incomplete.");
}
String[] messages = payload.split(RECORD_SEPARATOR);
List<HubMessage> hubMessages = new ArrayList<>();
for (String str : messages) {
HubMessageType messageType = null;
String invocationId = null;
String target = null;
String error = null;
ArrayList<Object> arguments = null;
JsonArray argumentsToken = null;
Object result = null;
JsonElement resultToken = null;
JsonReader reader = new JsonReader(new StringReader(str));
reader.beginObject();
try {
for (String str : messages) {
HubMessageType messageType = null;
String invocationId = null;
String target = null;
String error = null;
ArrayList<Object> arguments = null;
JsonArray argumentsToken = null;
Object result = null;
JsonElement resultToken = null;
JsonReader reader = new JsonReader(new StringReader(str));
reader.beginObject();
do {
String name = reader.nextName();
switch (name) {
case "type":
messageType = HubMessageType.values()[reader.nextInt() - 1];
break;
case "invocationId":
invocationId = reader.nextString();
break;
case "target":
target = reader.nextString();
break;
case "error":
error = reader.nextString();
break;
case "result":
if (invocationId == null) {
resultToken = jsonParser.parse(reader);
} else {
result = gson.fromJson(reader, binder.getReturnType(invocationId));
}
break;
case "item":
reader.skipValue();
break;
case "arguments":
if (target != null) {
do {
String name = reader.nextName();
switch (name) {
case "type":
messageType = HubMessageType.values()[reader.nextInt() - 1];
break;
case "invocationId":
invocationId = reader.nextString();
break;
case "target":
target = reader.nextString();
break;
case "error":
error = reader.nextString();
break;
case "result":
if (invocationId == null) {
resultToken = jsonParser.parse(reader);
} else {
result = gson.fromJson(reader, binder.getReturnType(invocationId));
}
break;
case "item":
reader.skipValue();
break;
case "arguments":
if (target != null) {
List<Class<?>> types = binder.getParameterTypes(target);
arguments = bindArguments(reader, types);
} else {
argumentsToken = (JsonArray)jsonParser.parse(reader);
}
break;
case "headers":
throw new RuntimeException("Headers not implemented yet.");
default:
// Skip unknown property, allows new clients to still work with old protocols
reader.skipValue();
break;
}
} while (reader.hasNext());
reader.endObject();
reader.close();
switch (messageType) {
case INVOCATION:
if (argumentsToken != null) {
List<Class<?>> types = binder.getParameterTypes(target);
arguments = bindArguments(reader, types);
arguments = bindArguments(argumentsToken, types);
}
if (arguments == null) {
hubMessages.add(new InvocationMessage(invocationId, target, new Object[0]));
} else {
argumentsToken = (JsonArray)jsonParser.parse(reader);
hubMessages.add(new InvocationMessage(invocationId, target, arguments.toArray()));
}
break;
case COMPLETION:
if (resultToken != null) {
result = gson.fromJson(resultToken, binder.getReturnType(invocationId));
}
hubMessages.add(new CompletionMessage(invocationId, result, error));
break;
case STREAM_INVOCATION:
case STREAM_ITEM:
case CANCEL_INVOCATION:
throw new UnsupportedOperationException(String.format("The message type %s is not supported yet.", messageType));
case PING:
hubMessages.add(PingMessage.getInstance());
break;
case CLOSE:
if (error != null) {
hubMessages.add(new CloseMessage(error));
} else {
hubMessages.add(new CloseMessage());
}
break;
case "headers":
throw new HubException("Headers not implemented yet.");
default:
// Skip unknown property, allows new clients to still work with old protocols
reader.skipValue();
break;
}
} while (reader.hasNext());
reader.endObject();
reader.close();
switch (messageType) {
case INVOCATION:
if (argumentsToken != null) {
List<Class<?>> types = binder.getParameterTypes(target);
arguments = bindArguments(argumentsToken, types);
}
if (arguments == null) {
hubMessages.add(new InvocationMessage(invocationId, target, new Object[0]));
} else {
hubMessages.add(new InvocationMessage(invocationId, target, arguments.toArray()));
}
break;
case COMPLETION:
if (resultToken != null) {
result = gson.fromJson(resultToken, binder.getReturnType(invocationId));
}
hubMessages.add(new CompletionMessage(invocationId, result, error));
break;
case STREAM_INVOCATION:
case STREAM_ITEM:
case CANCEL_INVOCATION:
throw new UnsupportedOperationException(String.format("The message type %s is not supported yet.", messageType));
case PING:
hubMessages.add(PingMessage.getInstance());
break;
case CLOSE:
if (error != null) {
hubMessages.add(new CloseMessage(error));
} else {
hubMessages.add(new CloseMessage());
}
break;
default:
break;
}
} catch (IOException ex) {
throw new RuntimeException("Error reading JSON.", ex);
}
return hubMessages.toArray(new HubMessage[hubMessages.size()]);

View File

@ -17,60 +17,64 @@ class NegotiateResponse {
private String accessToken;
private String error;
public NegotiateResponse(String negotiatePayload) throws IOException {
JsonReader reader = new JsonReader(new StringReader(negotiatePayload));
reader.beginObject();
public NegotiateResponse(String negotiatePayload) {
try {
JsonReader reader = new JsonReader(new StringReader(negotiatePayload));
reader.beginObject();
do {
String name = reader.nextName();
switch (name) {
case "error":
this.error = reader.nextString();
break;
case "url":
this.redirectUrl = reader.nextString();
break;
case "accessToken":
this.accessToken = reader.nextString();
break;
case "availableTransports":
reader.beginArray();
while (reader.hasNext()) {
reader.beginObject();
do {
String name = reader.nextName();
switch (name) {
case "error":
this.error = reader.nextString();
break;
case "url":
this.redirectUrl = reader.nextString();
break;
case "accessToken":
this.accessToken = reader.nextString();
break;
case "availableTransports":
reader.beginArray();
while (reader.hasNext()) {
String transport = null;
String property = reader.nextName();
switch (property) {
case "transport":
transport = reader.nextString();
break;
case "transferFormats":
// transfer formats aren't supported currently
reader.skipValue();
break;
default:
// Skip unknown property, allows new clients to still work with old protocols
reader.skipValue();
break;
reader.beginObject();
while (reader.hasNext()) {
String transport = null;
String property = reader.nextName();
switch (property) {
case "transport":
transport = reader.nextString();
break;
case "transferFormats":
// transfer formats aren't supported currently
reader.skipValue();
break;
default:
// Skip unknown property, allows new clients to still work with old protocols
reader.skipValue();
break;
}
this.availableTransports.add(transport);
}
this.availableTransports.add(transport);
reader.endObject();
}
reader.endObject();
}
reader.endArray();
break;
case "connectionId":
this.connectionId = reader.nextString();
break;
default:
// Skip unknown property, allows new clients to still work with old protocols
reader.skipValue();
break;
}
} while (reader.hasNext());
reader.endArray();
break;
case "connectionId":
this.connectionId = reader.nextString();
break;
default:
// Skip unknown property, allows new clients to still work with old protocols
reader.skipValue();
break;
}
} while (reader.hasNext());
reader.endObject();
reader.close();
reader.endObject();
reader.close();
} catch (IOException ex) {
throw new RuntimeException("Error reading NegotiateResponse", ex);
}
}
public String getConnectionId() {

View File

@ -4,5 +4,5 @@
package com.microsoft.signalr;
interface OnReceiveCallBack {
void invoke(String message) throws Exception;
void invoke(String message);
}

View File

@ -10,7 +10,7 @@ interface Transport {
CompletableFuture<Void> start(String url);
CompletableFuture<Void> send(String message);
void setOnReceive(OnReceiveCallBack callback);
void onReceive(String message) throws Exception;
void onReceive(String message);
void setOnClose(Consumer<String> onCloseCallback);
CompletableFuture<Void> stop();
}

View File

@ -68,7 +68,7 @@ class WebSocketTransport implements Transport {
}
@Override
public void onReceive(String message) throws Exception {
public void onReceive(String message) {
this.onReceiveCallBack.invoke(message);
}

View File

@ -24,7 +24,7 @@ class HubConnectionTest {
private static final String RECORD_SEPARATOR = "\u001e";
@Test
public void checkHubConnectionState() throws Exception {
public void checkHubConnectionState() {
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com");
hubConnection.start().blockingAwait(1000, TimeUnit.MILLISECONDS);
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
@ -34,7 +34,7 @@ class HubConnectionTest {
}
@Test
public void transportCloseTriggersStopInHubConnection() throws Exception {
public void transportCloseTriggersStopInHubConnection() {
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
hubConnection.start().blockingAwait(1000, TimeUnit.MILLISECONDS);
@ -45,7 +45,7 @@ class HubConnectionTest {
}
@Test
public void transportCloseWithErrorTriggersStopInHubConnection() throws Exception {
public void transportCloseWithErrorTriggersStopInHubConnection() {
MockTransport mockTransport = new MockTransport();
AtomicReference<String> message = new AtomicReference<>();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -90,7 +90,7 @@ class HubConnectionTest {
}
@Test
public void hubConnectionClosesAfterCloseMessage() throws Exception {
public void hubConnectionClosesAfterCloseMessage() {
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -104,7 +104,7 @@ class HubConnectionTest {
}
@Test
public void invalidHandShakeResponse() throws Exception {
public void invalidHandShakeResponse() {
MockTransport mockTransport = new MockTransport(false);
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -126,7 +126,7 @@ class HubConnectionTest {
}
@Test
public void registeringMultipleHandlersAndBothGetTriggered() throws Exception {
public void registeringMultipleHandlersAndBothGetTriggered() {
AtomicReference<Double> value = new AtomicReference<>(0.0);
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -151,7 +151,7 @@ class HubConnectionTest {
}
@Test
public void removeHandlerByName() throws Exception {
public void removeHandlerByName() {
AtomicReference<Double> value = new AtomicReference<>(0.0);
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -177,7 +177,7 @@ class HubConnectionTest {
}
@Test
public void addAndRemoveHandlerImmediately() throws Exception {
public void addAndRemoveHandlerImmediately() {
AtomicReference<Double> value = new AtomicReference<>(0.0);
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -201,7 +201,7 @@ class HubConnectionTest {
}
@Test
public void removingMultipleHandlersWithOneCallToRemove() throws Exception {
public void removingMultipleHandlersWithOneCallToRemove() {
AtomicReference<Double> value = new AtomicReference<>(0.0);
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -232,7 +232,7 @@ class HubConnectionTest {
}
@Test
public void removeHandlerWithUnsubscribe() throws Exception {
public void removeHandlerWithUnsubscribe() {
AtomicReference<Double> value = new AtomicReference<>(0.0);
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -264,7 +264,7 @@ class HubConnectionTest {
}
@Test
public void unsubscribeTwice() throws Exception {
public void unsubscribeTwice() {
AtomicReference<Double> value = new AtomicReference<>(0.0);
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -297,7 +297,7 @@ class HubConnectionTest {
}
@Test
public void removeSingleHandlerWithUnsubscribe() throws Exception {
public void removeSingleHandlerWithUnsubscribe() {
AtomicReference<Double> value = new AtomicReference<>(0.0);
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -326,7 +326,7 @@ class HubConnectionTest {
}
@Test
public void addAndRemoveHandlerImmediatelyWithSubscribe() throws Exception {
public void addAndRemoveHandlerImmediatelyWithSubscribe() {
AtomicReference<Double> value = new AtomicReference<>(0.0);
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -350,7 +350,7 @@ class HubConnectionTest {
}
@Test
public void registeringMultipleHandlersThatTakeParamsAndBothGetTriggered() throws Exception {
public void registeringMultipleHandlersThatTakeParamsAndBothGetTriggered() {
AtomicReference<Double> value = new AtomicReference<>(0.0);
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -369,7 +369,7 @@ class HubConnectionTest {
}
@Test
public void invokeWaitsForCompletionMessage() throws Exception {
public void invokeWaitsForCompletionMessage() {
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -387,7 +387,7 @@ class HubConnectionTest {
}
@Test
public void multipleInvokesWaitForOwnCompletionMessage() throws Exception {
public void multipleInvokesWaitForOwnCompletionMessage() {
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -413,7 +413,7 @@ class HubConnectionTest {
}
@Test
public void invokeWorksForPrimitiveTypes() throws Exception {
public void invokeWorksForPrimitiveTypes() {
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -432,7 +432,7 @@ class HubConnectionTest {
}
@Test
public void completionMessageCanHaveError() throws Exception {
public void completionMessageCanHaveError() {
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -457,7 +457,7 @@ class HubConnectionTest {
}
@Test
public void stopCancelsActiveInvokes() throws Exception {
public void stopCancelsActiveInvokes() {
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -482,7 +482,7 @@ class HubConnectionTest {
}
@Test
public void sendWithNoParamsTriggersOnHandler() throws Exception {
public void sendWithNoParamsTriggersOnHandler() {
AtomicReference<Integer> value = new AtomicReference<>(0);
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -500,7 +500,7 @@ class HubConnectionTest {
}
@Test
public void sendWithParamTriggersOnHandler() throws Exception {
public void sendWithParamTriggersOnHandler() {
AtomicReference<String> value = new AtomicReference<>();
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -519,7 +519,7 @@ class HubConnectionTest {
}
@Test
public void sendWithTwoParamsTriggersOnHandler() throws Exception {
public void sendWithTwoParamsTriggersOnHandler() {
AtomicReference<String> value1 = new AtomicReference<>();
AtomicReference<Double> value2 = new AtomicReference<>();
@ -544,7 +544,7 @@ class HubConnectionTest {
}
@Test
public void sendWithThreeParamsTriggersOnHandler() throws Exception {
public void sendWithThreeParamsTriggersOnHandler() {
AtomicReference<String> value1 = new AtomicReference<>();
AtomicReference<String> value2 = new AtomicReference<>();
AtomicReference<String> value3 = new AtomicReference<>();
@ -573,7 +573,7 @@ class HubConnectionTest {
}
@Test
public void sendWithFourParamsTriggersOnHandler() throws Exception {
public void sendWithFourParamsTriggersOnHandler() {
AtomicReference<String> value1 = new AtomicReference<>();
AtomicReference<String> value2 = new AtomicReference<>();
AtomicReference<String> value3 = new AtomicReference<>();
@ -605,7 +605,7 @@ class HubConnectionTest {
}
@Test
public void sendWithFiveParamsTriggersOnHandler() throws Exception {
public void sendWithFiveParamsTriggersOnHandler() {
AtomicReference<String> value1 = new AtomicReference<>();
AtomicReference<String> value2 = new AtomicReference<>();
AtomicReference<String> value3 = new AtomicReference<>();
@ -641,7 +641,7 @@ class HubConnectionTest {
}
@Test
public void sendWithSixParamsTriggersOnHandler() throws Exception {
public void sendWithSixParamsTriggersOnHandler() {
AtomicReference<String> value1 = new AtomicReference<>();
AtomicReference<String> value2 = new AtomicReference<>();
AtomicReference<String> value3 = new AtomicReference<>();
@ -681,7 +681,7 @@ class HubConnectionTest {
}
@Test
public void sendWithSevenParamsTriggersOnHandler() throws Exception {
public void sendWithSevenParamsTriggersOnHandler() {
AtomicReference<String> value1 = new AtomicReference<>();
AtomicReference<String> value2 = new AtomicReference<>();
AtomicReference<String> value3 = new AtomicReference<>();
@ -725,7 +725,7 @@ class HubConnectionTest {
}
@Test
public void sendWithEightParamsTriggersOnHandler() throws Exception {
public void sendWithEightParamsTriggersOnHandler() {
AtomicReference<String> value1 = new AtomicReference<>();
AtomicReference<String> value2 = new AtomicReference<>();
AtomicReference<String> value3 = new AtomicReference<>();
@ -778,7 +778,7 @@ class HubConnectionTest {
}
@Test
public void sendWithCustomObjectTriggersOnHandler() throws Exception {
public void sendWithCustomObjectTriggersOnHandler() {
AtomicReference<Custom> value1 = new AtomicReference<>();
MockTransport mockTransport = new MockTransport();
@ -803,7 +803,7 @@ class HubConnectionTest {
}
@Test
public void receiveHandshakeResponseAndMessage() throws Exception {
public void receiveHandshakeResponseAndMessage() {
AtomicReference<Double> value = new AtomicReference<Double>(0.0);
MockTransport mockTransport = new MockTransport(false);
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
@ -825,7 +825,7 @@ class HubConnectionTest {
}
@Test
public void onClosedCallbackRunsWhenStopIsCalled() throws Exception {
public void onClosedCallbackRunsWhenStopIsCalled() {
AtomicReference<String> value1 = new AtomicReference<>();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com");
hubConnection.start();
@ -840,7 +840,7 @@ class HubConnectionTest {
}
@Test
public void multipleOnClosedCallbacksRunWhenStopIsCalled() throws Exception {
public void multipleOnClosedCallbacksRunWhenStopIsCalled() {
AtomicReference<String> value1 = new AtomicReference<>();
AtomicReference<String> value2 = new AtomicReference<>();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com");
@ -866,7 +866,7 @@ class HubConnectionTest {
}
@Test
public void hubConnectionClosesAndRunsOnClosedCallbackAfterCloseMessageWithError() throws Exception {
public void hubConnectionClosesAndRunsOnClosedCallbackAfterCloseMessageWithError() {
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
hubConnection.onClosed((ex) -> {
@ -882,7 +882,7 @@ class HubConnectionTest {
}
@Test
public void callingStartOnStartedHubConnectionNoOps() throws Exception {
public void callingStartOnStartedHubConnectionNoOps() {
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com");
hubConnection.start();
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
@ -895,16 +895,16 @@ class HubConnectionTest {
}
@Test
public void cannotSendBeforeStart() throws Exception {
public void cannotSendBeforeStart() {
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com");
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
Throwable exception = assertThrows(HubException.class, () -> hubConnection.send("inc"));
Throwable exception = assertThrows(RuntimeException.class, () -> hubConnection.send("inc"));
assertEquals("The 'send' method cannot be called if the connection is not active", exception.getMessage());
}
@Test
public void errorWhenReceivingInvokeWithIncorrectArgumentLength() throws Exception {
public void errorWhenReceivingInvokeWithIncorrectArgumentLength() {
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
hubConnection.on("Send", (s) -> {
@ -938,7 +938,7 @@ class HubConnectionTest {
}
@Test
public void negotiateThatRedirectsForeverFailsAfter100Tries() throws InterruptedException, TimeoutException, Exception {
public void negotiateThatRedirectsForeverFailsAfter100Tries() {
TestHttpClient client = new TestHttpClient().on("POST", "http://example.com/negotiate",
(req) -> CompletableFuture.completedFuture(new HttpResponse(200, "", "{\"url\":\"http://example.com\"}")));
@ -953,7 +953,7 @@ class HubConnectionTest {
}
@Test
public void afterSuccessfulNegotiateConnectsWithTransport() throws InterruptedException, TimeoutException, Exception {
public void afterSuccessfulNegotiateConnectsWithTransport() {
TestHttpClient client = new TestHttpClient().on("POST", "http://example.com/negotiate",
(req) -> CompletableFuture.completedFuture(new HttpResponse(200, "",
"{\"connectionId\":\"bVOiRPG8-6YiJ6d7ZcTOVQ\",\""
@ -991,7 +991,7 @@ class HubConnectionTest {
}
@Test
public void negotiateRedirectIsFollowed() throws Exception {
public void negotiateRedirectIsFollowed() {
TestHttpClient client = new TestHttpClient().on("POST", "http://example.com/negotiate",
(req) -> CompletableFuture.completedFuture(new HttpResponse(200, "", "{\"url\":\"http://testexample.com/\"}")))
.on("POST", "http://testexample.com/negotiate",
@ -1011,8 +1011,7 @@ class HubConnectionTest {
}
@Test
public void accessTokenProviderIsUsedForNegotiate()
throws InterruptedException, ExecutionException, TimeoutException, Exception {
public void accessTokenProviderIsUsedForNegotiate() {
AtomicReference<String> token = new AtomicReference<>();
TestHttpClient client = new TestHttpClient()
.on("POST", "http://example.com/negotiate",
@ -1038,7 +1037,7 @@ class HubConnectionTest {
}
@Test
public void accessTokenProviderIsOverriddenFromRedirectNegotiate() throws Exception {
public void accessTokenProviderIsOverriddenFromRedirectNegotiate() {
AtomicReference<String> token = new AtomicReference<>();
TestHttpClient client = new TestHttpClient()
.on("POST", "http://example.com/negotiate", (req) -> CompletableFuture.completedFuture(new HttpResponse(200, "", "{\"url\":\"http://testexample.com/\",\"accessToken\":\"newToken\"}")))
@ -1065,7 +1064,7 @@ class HubConnectionTest {
}
@Test
public void connectionTimesOutIfServerDoesNotSendMessage() throws Exception {
public void connectionTimesOutIfServerDoesNotSendMessage() throws InterruptedException, ExecutionException, TimeoutException {
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com");
hubConnection.setServerTimeout(Duration.ofMillis(1));
hubConnection.setTickRate(Duration.ofMillis(1));
@ -1080,7 +1079,7 @@ class HubConnectionTest {
}
@Test
public void connectionSendsPingsRegularly() throws InterruptedException, ExecutionException, TimeoutException, Exception {
public void connectionSendsPingsRegularly() throws InterruptedException {
MockTransport mockTransport = new MockTransport(true, false);
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
hubConnection.setKeepAliveInterval(Duration.ofMillis(1));
@ -1099,7 +1098,7 @@ class HubConnectionTest {
}
@Test
public void hubConnectionCanBeStartedAfterBeingStopped() throws Exception {
public void hubConnectionCanBeStartedAfterBeingStopped() {
MockTransport transport = new MockTransport();
HubConnection hubConnection = HubConnectionBuilder
.create("http://example.com")
@ -1118,7 +1117,7 @@ class HubConnectionTest {
}
@Test
public void hubConnectionCanBeStartedAfterBeingStoppedAndRedirected() throws Exception {
public void hubConnectionCanBeStartedAfterBeingStoppedAndRedirected() {
MockTransport mockTransport = new MockTransport();
TestHttpClient client = new TestHttpClient()
.on("POST", "http://example.com/negotiate", (req) -> CompletableFuture

View File

@ -39,7 +39,7 @@ class JsonHubProtocolTest {
}
@Test
public void parsePingMessage() throws Exception {
public void parsePingMessage() {
String stringifiedMessage = "{\"type\":6}\u001E";
TestBinder binder = new TestBinder(PingMessage.getInstance());
@ -51,7 +51,7 @@ class JsonHubProtocolTest {
}
@Test
public void parseCloseMessage() throws Exception {
public void parseCloseMessage() {
String stringifiedMessage = "{\"type\":7}\u001E";
TestBinder binder = new TestBinder(new CloseMessage());
@ -69,7 +69,7 @@ class JsonHubProtocolTest {
}
@Test
public void parseCloseMessageWithError() throws Exception {
public void parseCloseMessageWithError() {
String stringifiedMessage = "{\"type\":7,\"error\": \"There was an error\"}\u001E";
TestBinder binder = new TestBinder(new CloseMessage("There was an error"));
@ -87,7 +87,7 @@ class JsonHubProtocolTest {
}
@Test
public void parseSingleMessage() throws Exception {
public void parseSingleMessage() {
String stringifiedMessage = "{\"type\":1,\"target\":\"test\",\"arguments\":[42]}\u001E";
TestBinder binder = new TestBinder(new InvocationMessage("1", "test", new Object[] { 42 }));
@ -109,7 +109,7 @@ class JsonHubProtocolTest {
}
@Test
public void parseSingleUnsupportedStreamItemMessage() throws Exception {
public void parseSingleUnsupportedStreamItemMessage() {
String stringifiedMessage = "{\"type\":2,\"Id\":1,\"Item\":42}\u001E";
TestBinder binder = new TestBinder(null);
@ -118,7 +118,7 @@ class JsonHubProtocolTest {
}
@Test
public void parseSingleUnsupportedStreamInvocationMessage() throws Exception {
public void parseSingleUnsupportedStreamInvocationMessage() {
String stringifiedMessage = "{\"type\":4,\"Id\":1,\"target\":\"test\",\"arguments\":[42]}\u001E";
TestBinder binder = new TestBinder(new StreamInvocationMessage("1", "test", new Object[] { 42 }));
@ -127,7 +127,7 @@ class JsonHubProtocolTest {
}
@Test
public void parseSingleUnsupportedCancelInvocationMessage() throws Exception {
public void parseSingleUnsupportedCancelInvocationMessage() {
String stringifiedMessage = "{\"type\":5,\"invocationId\":123}\u001E";
TestBinder binder = new TestBinder(null);
@ -136,7 +136,7 @@ class JsonHubProtocolTest {
}
@Test
public void parseTwoMessages() throws Exception {
public void parseTwoMessages() {
String twoMessages = "{\"type\":1,\"target\":\"one\",\"arguments\":[42]}\u001E{\"type\":1,\"target\":\"two\",\"arguments\":[43]}\u001E";
TestBinder binder = new TestBinder(new InvocationMessage("1", "one", new Object[] { 42 }));
@ -167,7 +167,7 @@ class JsonHubProtocolTest {
}
@Test
public void parseSingleMessageMutipleArgs() throws Exception {
public void parseSingleMessageMutipleArgs() {
String stringifiedMessage = "{\"type\":1,\"target\":\"test\",\"arguments\":[42, 24]}\u001E";
TestBinder binder = new TestBinder(new InvocationMessage("1", "test", new Object[] { 42, 24 }));
@ -186,7 +186,7 @@ class JsonHubProtocolTest {
}
@Test
public void parseMessageWithOutOfOrderProperties() throws Exception {
public void parseMessageWithOutOfOrderProperties() {
String stringifiedMessage = "{\"arguments\":[42, 24],\"type\":1,\"target\":\"test\"}\u001E";
TestBinder binder = new TestBinder(new InvocationMessage("1", "test", new Object[] { 42, 24 }));
@ -205,7 +205,7 @@ class JsonHubProtocolTest {
}
@Test
public void parseCompletionMessageWithOutOfOrderProperties() throws Exception {
public void parseCompletionMessageWithOutOfOrderProperties() {
String stringifiedMessage = "{\"type\":3,\"result\":42,\"invocationId\":\"1\"}\u001E";
TestBinder binder = new TestBinder(new CompletionMessage("1", 42, null));
@ -220,7 +220,7 @@ class JsonHubProtocolTest {
}
@Test
public void errorWhileParsingTooManyArgumentsWithOutOfOrderProperties() throws Exception {
public void errorWhileParsingTooManyArgumentsWithOutOfOrderProperties() {
String stringifiedMessage = "{\"arguments\":[42, 24],\"type\":1,\"target\":\"test\"}\u001E";
TestBinder binder = new TestBinder(new InvocationMessage(null, "test", new Object[] { 42 }));
@ -229,7 +229,7 @@ class JsonHubProtocolTest {
}
@Test
public void errorWhileParsingTooManyArguments() throws Exception {
public void errorWhileParsingTooManyArguments() {
String stringifiedMessage = "{\"type\":1,\"target\":\"test\",\"arguments\":[42, 24]}\u001E";
TestBinder binder = new TestBinder(new InvocationMessage(null, "test", new Object[] { 42 }));
@ -238,7 +238,7 @@ class JsonHubProtocolTest {
}
@Test
public void errorWhileParsingTooFewArguments() throws Exception {
public void errorWhileParsingTooFewArguments() {
String stringifiedMessage = "{\"type\":1,\"target\":\"test\",\"arguments\":[42]}\u001E";
TestBinder binder = new TestBinder(new InvocationMessage(null, "test", new Object[] { 42, 24 }));
@ -247,7 +247,7 @@ class JsonHubProtocolTest {
}
@Test
public void errorWhileParsingIncompleteMessage() throws Exception {
public void errorWhileParsingIncompleteMessage() {
String stringifiedMessage = "{\"type\":1,\"target\":\"test\",\"arguments\":";
TestBinder binder = new TestBinder(new InvocationMessage(null, "test", new Object[] { 42, 24 }));

View File

@ -31,7 +31,7 @@ class MockTransport implements Transport {
}
@Override
public CompletableFuture start(String url) {
public CompletableFuture<Void> start(String url) {
this.url = url;
if (autoHandshake) {
try {
@ -44,7 +44,7 @@ class MockTransport implements Transport {
}
@Override
public CompletableFuture send(String message) {
public CompletableFuture<Void> send(String message) {
if (!(ignorePings && message.equals("{\"type\":6}" + RECORD_SEPARATOR))) {
sentMessages.add(message);
}
@ -57,7 +57,7 @@ class MockTransport implements Transport {
}
@Override
public void onReceive(String message) throws Exception {
public void onReceive(String message) {
this.onReceiveCallBack.invoke(message);
}
@ -67,7 +67,7 @@ class MockTransport implements Transport {
}
@Override
public CompletableFuture stop() {
public CompletableFuture<Void> stop() {
onClose.accept(null);
return CompletableFuture.completedFuture(null);
}
@ -76,7 +76,7 @@ class MockTransport implements Transport {
onClose.accept(errorMessage);
}
public void receiveMessage(String message) throws Exception {
public void receiveMessage(String message) {
this.onReceive(message);
}

View File

@ -12,7 +12,7 @@ import org.junit.jupiter.api.Test;
class NegotiateResponseTest {
@Test
public void VerifyNegotiateResponse() throws IOException {
public void VerifyNegotiateResponse() {
String stringNegotiateResponse = "{\"connectionId\":\"bVOiRPG8-6YiJ6d7ZcTOVQ\",\"" +
"availableTransports\":[{\"transport\":\"WebSockets\",\"transferFormats\":[\"Text\",\"Binary\"]}," +
"{\"transport\":\"ServerSentEvents\",\"transferFormats\":[\"Text\"]}," +
@ -27,7 +27,7 @@ class NegotiateResponseTest {
}
@Test
public void VerifyRedirectNegotiateResponse() throws IOException {
public void VerifyRedirectNegotiateResponse() {
String stringNegotiateResponse = "{\"url\":\"www.example.com\"," +
"\"accessToken\":\"some_access_token\"," +
"\"availableTransports\":[]}";
@ -40,7 +40,7 @@ class NegotiateResponseTest {
}
@Test
public void NegotiateResponseIgnoresExtraProperties() throws IOException {
public void NegotiateResponseIgnoresExtraProperties() {
String stringNegotiateResponse = "{\"connectionId\":\"bVOiRPG8-6YiJ6d7ZcTOVQ\"," +
"\"extra\":\"something\"}";
NegotiateResponse negotiateResponse = new NegotiateResponse(stringNegotiateResponse);
@ -48,7 +48,7 @@ class NegotiateResponseTest {
}
@Test
public void NegotiateResponseIgnoresExtraComplexProperties() throws IOException {
public void NegotiateResponseIgnoresExtraComplexProperties() {
String stringNegotiateResponse = "{\"connectionId\":\"bVOiRPG8-6YiJ6d7ZcTOVQ\"," +
"\"extra\":[\"something\"]}";
NegotiateResponse negotiateResponse = new NegotiateResponse(stringNegotiateResponse);

View File

@ -13,7 +13,7 @@ import org.junit.jupiter.api.Test;
class WebSocketTransportTest {
@Test
public void WebsocketThrowsIfItCantConnect() throws Exception {
public void WebsocketThrowsIfItCantConnect() {
Transport transport = new WebSocketTransport(new HashMap<>(), new DefaultHttpClient(new NullLogger()), new NullLogger());
ExecutionException exception = assertThrows(ExecutionException.class, () -> transport.start("http://www.example.com").get(1, TimeUnit.SECONDS));
assertEquals("There was an error starting the Websockets transport.", exception.getCause().getMessage());

View File

@ -10,7 +10,7 @@ import com.microsoft.signalr.HubConnectionBuilder;
import com.microsoft.signalr.LogLevel;
public class Chat {
public static void main(String[] args) throws Exception {
public static void main(String[] args) {
System.out.println("Enter the URL of the SignalR Chat you want to join");
Scanner reader = new Scanner(System.in); // Reading from System.in
String input = reader.nextLine();