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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { public static <T> T getTargetObject(Object candidate) {
Assert.notNull(candidate, "Candidate must not be null"); Assert.notNull(candidate, "Candidate must not be null");
try { try {
if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised) { if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised advised) {
Object target = ((Advised) candidate).getTargetSource().getTarget(); Object target = advised.getTargetSource().getTarget();
if (target != null) { if (target != null) {
return (T) target; return (T) target;
} }
@ -86,8 +86,8 @@ public abstract class AopTestUtils {
public static <T> T getUltimateTargetObject(Object candidate) { public static <T> T getUltimateTargetObject(Object candidate) {
Assert.notNull(candidate, "Candidate must not be null"); Assert.notNull(candidate, "Candidate must not be null");
try { try {
if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised) { if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised advised) {
Object target = ((Advised) candidate).getTargetSource().getTarget(); Object target = advised.getTargetSource().getTarget();
if (target != null) { if (target != null) {
return (T) getUltimateTargetObject(target); return (T) getUltimateTargetObject(target);
} }

View File

@ -87,11 +87,11 @@ public class ExceptionCollector {
if (this.exceptions.size() == 1) { if (this.exceptions.size() == 1) {
Throwable exception = this.exceptions.get(0); Throwable exception = this.exceptions.get(0);
if (exception instanceof Error) { if (exception instanceof Error error) {
throw (Error) exception; throw error;
} }
if (exception instanceof Exception) { if (exception instanceof Exception ex) {
throw (Exception) exception; throw ex;
} }
AssertionError assertionError = new AssertionError(exception.getMessage()); AssertionError assertionError = new AssertionError(exception.getMessage());
assertionError.initCause(exception); 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { public void assertValue(String content, @Nullable Object expectedValue) {
Object actualValue = evaluateJsonPath(content); Object actualValue = evaluateJsonPath(content);
if ((actualValue instanceof List) && !(expectedValue instanceof List)) { if ((actualValue instanceof List<?> actualValueList) && !(expectedValue instanceof List)) {
@SuppressWarnings("rawtypes")
List actualValueList = (List) actualValue;
if (actualValueList.isEmpty()) { if (actualValueList.isEmpty()) {
AssertionErrors.fail("No matching value at JSON path \"" + this.expression + "\""); AssertionErrors.fail("No matching value at JSON path \"" + this.expression + "\"");
} }
@ -209,8 +207,8 @@ public class JsonPathExpectationsHelper {
return; return;
} }
String reason = failureReason("no value", value); String reason = failureReason("no value", value);
if (pathIsIndefinite() && value instanceof List) { if (pathIsIndefinite() && value instanceof List<?> list) {
AssertionErrors.assertTrue(reason, ((List<?>) value).isEmpty()); AssertionErrors.assertTrue(reason, list.isEmpty());
} }
else { else {
AssertionErrors.assertTrue(reason, (value == null)); AssertionErrors.assertTrue(reason, (value == null));
@ -252,9 +250,9 @@ public class JsonPathExpectationsHelper {
*/ */
public void hasJsonPath(String content) { public void hasJsonPath(String content) {
Object value = evaluateJsonPath(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 + "\""; 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) { catch (AssertionError ex) {
return; return;
} }
if (pathIsIndefinite() && value instanceof List) { if (pathIsIndefinite() && value instanceof List<?> list) {
AssertionErrors.assertTrue(failureReason("no values", value), ((List<?>) value).isEmpty()); AssertionErrors.assertTrue(failureReason("no values", value), list.isEmpty());
} }
else { else {
AssertionErrors.fail(failureReason("no value", value)); AssertionErrors.fail(failureReason("no value", value));
@ -328,8 +326,8 @@ public class JsonPathExpectationsHelper {
Object value = evaluateJsonPath(content); Object value = evaluateJsonPath(content);
String reason = "No value at JSON path \"" + this.expression + "\""; String reason = "No value at JSON path \"" + this.expression + "\"";
AssertionErrors.assertTrue(reason, value != null); AssertionErrors.assertTrue(reason, value != null);
if (pathIsIndefinite() && value instanceof List) { if (pathIsIndefinite() && value instanceof List<?> list) {
AssertionErrors.assertTrue(reason, !((List<?>) value).isEmpty()); AssertionErrors.assertTrue(reason, !list.isEmpty());
} }
return value; return value;
} }