Skip to content

Commit

Permalink
[java] Fixing compilation errors introduced by rebasing the previous …
Browse files Browse the repository at this point in the history
…commit and restoring backward compatibility.
  • Loading branch information
barancev committed Oct 5, 2019
1 parent bc1a581 commit 03d8d13
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 47 deletions.
9 changes: 6 additions & 3 deletions java/client/src/org/openqa/selenium/remote/ErrorHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ public Response throwIfResponseFailed(Response response, long duration) throws R
message += " (WARNING: The client has suppressed server-side stacktraces)";
} else {
cause = serverError;
if (cause.getStackTrace() == null || cause.getStackTrace().length == 0) {
message += " (WARNING: The server did not provide any stacktrace information)";
}
}

if (rawErrorData.get(SCREEN_SHOT) != null) {
Expand Down Expand Up @@ -220,7 +223,7 @@ private <T extends Throwable> T createThrowable(

private Throwable rebuildServerError(Map<String, Object> rawErrorData, int responseStatus) {

if (rawErrorData.get(CLASS) == null || rawErrorData.get(STACK_TRACE) == null) {
if (rawErrorData.get(CLASS) == null && rawErrorData.get(STACK_TRACE) == null) {
// Not enough information for us to try to rebuild an error.
return null;
}
Expand All @@ -230,7 +233,7 @@ private Throwable rebuildServerError(Map<String, Object> rawErrorData, int respo
Class<?> clazz = null;

// First: allow Remote Driver to specify the Selenium Server internal exception
if (rawErrorData.containsKey(CLASS)) {
if (rawErrorData.get(CLASS) != null) {
String className = (String) rawErrorData.get(CLASS);
try {
clazz = Class.forName(className);
Expand Down Expand Up @@ -262,7 +265,7 @@ private Throwable rebuildServerError(Map<String, Object> rawErrorData, int respo
// Note: if we have a class name above, we should always have a stack trace.
// The inverse is not always true.
StackTraceElement[] stackTrace = new StackTraceElement[0];
if (rawErrorData.containsKey(STACK_TRACE)) {
if (rawErrorData.get(STACK_TRACE) != null) {
@SuppressWarnings({"unchecked"})
List<Map<String, Object>> stackTraceInfo =
(List<Map<String, Object>>) rawErrorData.get(STACK_TRACE);
Expand Down
83 changes: 39 additions & 44 deletions java/client/test/org/openqa/selenium/remote/ErrorHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void testCauseShouldUseTheNamedClassIfAvailableOnTheClassPath() {
.isThrownBy(() -> handler.throwIfResponseFailed(
createResponse(ErrorCodes.UNHANDLED_ERROR,
ImmutableMap.of("message", "boom", "class", NullPointerException.class.getName())), 123))
.withMessage(new WebDriverException("boom\nCommand duration or timeout: 123 milliseconds").getMessage())
.withMessage(new WebDriverException("boom (WARNING: The server did not provide any stacktrace information)\nCommand duration or timeout: 123 milliseconds").getMessage())
.withCauseInstanceOf(NullPointerException.class)
.satisfies(expected -> assertThat(expected.getCause()).hasMessage("boom"));
}
Expand All @@ -142,7 +142,7 @@ public void testCauseStackTraceShouldBeEmptyIfTheServerDidNotProvideThatInformat
.isThrownBy(() -> handler.throwIfResponseFailed(
createResponse(ErrorCodes.UNHANDLED_ERROR,
ImmutableMap.of("message", "boom", "class", NullPointerException.class.getName())), 1234))
.withMessage(new WebDriverException("boom\nCommand duration or timeout: 1.23 seconds").getMessage())
.withMessage(new WebDriverException("boom (WARNING: The server did not provide any stacktrace information)\nCommand duration or timeout: 1.23 seconds").getMessage())
.withCauseInstanceOf(NullPointerException.class)
.satisfies(expected -> {
assertThat(expected.getCause()).hasMessage("boom");
Expand Down Expand Up @@ -239,58 +239,53 @@ public void testShouldStillTryToBuildWebDriverExceptionIfClassIsNotProvidedAndSt
});
}

@SuppressWarnings("ThrowableInstanceNeverThrown")
@Test
public void testShoulNotBuildWebDriverExceptionIfClassAndStackTraceIsNull() {
Map<String, ?> data = ImmutableMap.of(
"message", "some error message",
"class", null,
"stackTrace", null);

try {
handler.throwIfResponseFailed(createResponse(ErrorCodes.UNHANDLED_ERROR, data), 123);
fail("Should have thrown!");
} catch (WebDriverException expected) {
assertEquals(new WebDriverException("some error message\nCommand duration or timeout: 123 milliseconds",
new WebDriverException()).getMessage(),
expected.getMessage());
}
Map<String, Object> data = new HashMap<>();
data.put("message", "some error message");
data.put("class", null);
data.put("stackTrace", null);

assertThatExceptionOfType(WebDriverException.class)
.isThrownBy(() -> handler.throwIfResponseFailed(
createResponse(ErrorCodes.UNHANDLED_ERROR, data), 123))
.withMessageStartingWith(new WebDriverException(
"some error message (WARNING: The server did not provide any stacktrace information)\nCommand duration or timeout: 123 milliseconds",
new WebDriverException()).getMessage());
}

@SuppressWarnings("ThrowableInstanceNeverThrown")
@Test
public void testShoulNotBuildWebDriverExceptionIfClassNullAndStackTraceNotNull() {
Map<String, ?> data = ImmutableMap.of(
"message", "some error message",
"class", null,
"stackTrace", "a");

try {
handler.throwIfResponseFailed(createResponse(ErrorCodes.UNHANDLED_ERROR, data), 123);
fail("Should have thrown!");
} catch (WebDriverException expected) {
assertEquals(new WebDriverException("some error message\nCommand duration or timeout: 123 milliseconds",
new WebDriverException()).getMessage(),
expected.getMessage());
}
Map<String, Object> data = new HashMap<>();
data.put("message", "some error message");
data.put("class", null);
data.put("stackTrace", Collections.singletonList(
ImmutableMap.of("lineNumber", 1224,
"methodName", "someMethod",
"className", "MyClass",
"fileName", "Resource.m")));

assertThatExceptionOfType(WebDriverException.class)
.isThrownBy(() -> handler.throwIfResponseFailed(
createResponse(ErrorCodes.UNHANDLED_ERROR, data), 123))
.withMessageStartingWith(new WebDriverException(
"some error message\nCommand duration or timeout: 123 milliseconds",
new WebDriverException()).getMessage());
}

@SuppressWarnings("ThrowableInstanceNeverThrown")
@Test
public void testShoulNotBuildWebDriverExceptionIfClassNotNullAndStackTraceNull() {
Map<String, ?> data = ImmutableMap.of(
"message", "some error message",
"class", "a",
"stackTrace", null);

try {
handler.throwIfResponseFailed(createResponse(ErrorCodes.UNHANDLED_ERROR, data), 123);
fail("Should have thrown!");
} catch (WebDriverException expected) {
assertEquals(new WebDriverException("some error message\nCommand duration or timeout: 123 milliseconds",
new WebDriverException()).getMessage(),
expected.getMessage());
}
Map<String, Object> data = new HashMap<>();
data.put("message", "some error message");
data.put("class", "a");
data.put("stackTrace", null);

assertThatExceptionOfType(WebDriverException.class)
.isThrownBy(() -> handler.throwIfResponseFailed(
createResponse(ErrorCodes.UNHANDLED_ERROR, data), 123))
.withMessageStartingWith(new WebDriverException(
"some error message (WARNING: The server did not provide any stacktrace information)\nCommand duration or timeout: 123 milliseconds",
new WebDriverException()).getMessage());
}

@Test
Expand Down

0 comments on commit 03d8d13

Please sign in to comment.