Use instanceof pattern matching in select classes in spring-test

This commit is contained in:
Sam Brannen 2021-10-13 20:41:49 +02:00
parent 887ecffaa9
commit 1ef47fa369
3 changed files with 19 additions and 21 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -54,8 +54,8 @@ public abstract class AopTestUtils {
public static <T> T getTargetObject(Object candidate) {
Assert.notNull(candidate, "Candidate must not be null");
try {
if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised) {
Object target = ((Advised) candidate).getTargetSource().getTarget();
if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised advised) {
Object target = advised.getTargetSource().getTarget();
if (target != null) {
return (T) target;
}
@ -86,8 +86,8 @@ public abstract class AopTestUtils {
public static <T> T getUltimateTargetObject(Object candidate) {
Assert.notNull(candidate, "Candidate must not be null");
try {
if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised) {
Object target = ((Advised) candidate).getTargetSource().getTarget();
if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised advised) {
Object target = advised.getTargetSource().getTarget();
if (target != null) {
return (T) getUltimateTargetObject(target);
}

View File

@ -87,11 +87,11 @@ public class ExceptionCollector {
if (this.exceptions.size() == 1) {
Throwable exception = this.exceptions.get(0);
if (exception instanceof Error) {
throw (Error) exception;
if (exception instanceof Error error) {
throw error;
}
if (exception instanceof Exception) {
throw (Exception) exception;
if (exception instanceof Exception ex) {
throw ex;
}
AssertionError assertionError = new AssertionError(exception.getMessage());
assertionError.initCause(exception);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -97,9 +97,7 @@ public class JsonPathExpectationsHelper {
*/
public void assertValue(String content, @Nullable Object expectedValue) {
Object actualValue = evaluateJsonPath(content);
if ((actualValue instanceof List) && !(expectedValue instanceof List)) {
@SuppressWarnings("rawtypes")
List actualValueList = (List) actualValue;
if ((actualValue instanceof List<?> actualValueList) && !(expectedValue instanceof List)) {
if (actualValueList.isEmpty()) {
AssertionErrors.fail("No matching value at JSON path \"" + this.expression + "\"");
}
@ -209,8 +207,8 @@ public class JsonPathExpectationsHelper {
return;
}
String reason = failureReason("no value", value);
if (pathIsIndefinite() && value instanceof List) {
AssertionErrors.assertTrue(reason, ((List<?>) value).isEmpty());
if (pathIsIndefinite() && value instanceof List<?> list) {
AssertionErrors.assertTrue(reason, list.isEmpty());
}
else {
AssertionErrors.assertTrue(reason, (value == null));
@ -252,9 +250,9 @@ public class JsonPathExpectationsHelper {
*/
public void hasJsonPath(String content) {
Object value = evaluateJsonPath(content);
if (pathIsIndefinite() && value instanceof List) {
if (pathIsIndefinite() && value instanceof List<?> list) {
String message = "No values for JSON path \"" + this.expression + "\"";
AssertionErrors.assertTrue(message, !((List<?>) value).isEmpty());
AssertionErrors.assertTrue(message, !list.isEmpty());
}
}
@ -276,8 +274,8 @@ public class JsonPathExpectationsHelper {
catch (AssertionError ex) {
return;
}
if (pathIsIndefinite() && value instanceof List) {
AssertionErrors.assertTrue(failureReason("no values", value), ((List<?>) value).isEmpty());
if (pathIsIndefinite() && value instanceof List<?> list) {
AssertionErrors.assertTrue(failureReason("no values", value), list.isEmpty());
}
else {
AssertionErrors.fail(failureReason("no value", value));
@ -328,8 +326,8 @@ public class JsonPathExpectationsHelper {
Object value = evaluateJsonPath(content);
String reason = "No value at JSON path \"" + this.expression + "\"";
AssertionErrors.assertTrue(reason, value != null);
if (pathIsIndefinite() && value instanceof List) {
AssertionErrors.assertTrue(reason, !((List<?>) value).isEmpty());
if (pathIsIndefinite() && value instanceof List<?> list) {
AssertionErrors.assertTrue(reason, !list.isEmpty());
}
return value;
}