|
|
|
|
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
|
|
|
|
/* |
|
|
|
|
* Copyright 2002-2020 the original author or authors. |
|
|
|
|
* Copyright 2002-2023 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. |
|
|
|
|
@ -28,6 +28,7 @@ import org.springframework.core.testfixture.io.SerializationTestUtils;
@@ -28,6 +28,7 @@ import org.springframework.core.testfixture.io.SerializationTestUtils;
|
|
|
|
|
import org.springframework.lang.Nullable; |
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
import static org.mockito.Mockito.mock; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @author Juergen Hoeller |
|
|
|
|
@ -35,6 +36,8 @@ import static org.assertj.core.api.Assertions.assertThat;
@@ -35,6 +36,8 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
*/ |
|
|
|
|
public class MethodMatchersTests { |
|
|
|
|
|
|
|
|
|
private final static Method TEST_METHOD = mock(Method.class); |
|
|
|
|
|
|
|
|
|
private final Method EXCEPTION_GETMESSAGE; |
|
|
|
|
|
|
|
|
|
private final Method ITESTBEAN_SETAGE; |
|
|
|
|
@ -112,16 +115,62 @@ public class MethodMatchersTests {
@@ -112,16 +115,62 @@ public class MethodMatchersTests {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testDynamicAndStaticMethodMatcherReversion() throws Exception { |
|
|
|
|
void negateMethodMatcher() { |
|
|
|
|
MethodMatcher getterMatcher = new StartsWithMatcher("get"); |
|
|
|
|
MethodMatcher reversion = MethodMatchers.reversion(getterMatcher); |
|
|
|
|
assertThat(reversion.isRuntime()).as("Reversion is a static matcher").isFalse(); |
|
|
|
|
assertThat(reversion.matches(ITESTBEAN_GETAGE, TestBean.class)).as("Didn't matched getAge method").isFalse(); |
|
|
|
|
assertThat(reversion.matches(IOTHER_ABSQUATULATE, TestBean.class)).as("Matched absquatulate method").isTrue(); |
|
|
|
|
reversion = MethodMatchers.reversion(new TestDynamicMethodMatcherWhichDoesNotMatch()); |
|
|
|
|
assertThat(reversion.isRuntime()).as("Intersection is a dynamic matcher").isTrue(); |
|
|
|
|
assertThat(reversion.matches(ITESTBEAN_SETAGE, TestBean.class)).as("Didn't matched setAge method").isFalse(); |
|
|
|
|
assertThat(reversion.matches(ITESTBEAN_SETAGE, TestBean.class, 5)).as("Matched setAge method").isTrue(); |
|
|
|
|
MethodMatcher negate = MethodMatchers.negate(getterMatcher); |
|
|
|
|
assertThat(negate.matches(ITESTBEAN_SETAGE, int.class)).isTrue(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void negateTrueMethodMatcher() { |
|
|
|
|
MethodMatcher negate = MethodMatchers.negate(MethodMatcher.TRUE); |
|
|
|
|
assertThat(negate.matches(TEST_METHOD, String.class)).isFalse(); |
|
|
|
|
assertThat(negate.matches(TEST_METHOD, Object.class)).isFalse(); |
|
|
|
|
assertThat(negate.matches(TEST_METHOD, Integer.class)).isFalse(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void negateTrueMethodMatcherAppliedTwice() { |
|
|
|
|
MethodMatcher negate = MethodMatchers.negate(MethodMatchers.negate(MethodMatcher.TRUE)); |
|
|
|
|
assertThat(negate.matches(TEST_METHOD, String.class)).isTrue(); |
|
|
|
|
assertThat(negate.matches(TEST_METHOD, Object.class)).isTrue(); |
|
|
|
|
assertThat(negate.matches(TEST_METHOD, Integer.class)).isTrue(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void negateIsNotEqualsToOriginalMatcher() { |
|
|
|
|
MethodMatcher original = MethodMatcher.TRUE; |
|
|
|
|
MethodMatcher negate = MethodMatchers.negate(original); |
|
|
|
|
assertThat(original).isNotEqualTo(negate); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void negateOnSameMatcherIsEquals() { |
|
|
|
|
MethodMatcher original = MethodMatcher.TRUE; |
|
|
|
|
MethodMatcher first = MethodMatchers.negate(original); |
|
|
|
|
MethodMatcher second = MethodMatchers.negate(original); |
|
|
|
|
assertThat(first).isEqualTo(second); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void negateHasNotSameHashCodeAsOriginalMatcher() { |
|
|
|
|
MethodMatcher original = MethodMatcher.TRUE; |
|
|
|
|
MethodMatcher negate = MethodMatchers.negate(original); |
|
|
|
|
assertThat(original).doesNotHaveSameHashCodeAs(negate); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void negateOnSameMatcherHasSameHashCode() { |
|
|
|
|
MethodMatcher original = MethodMatcher.TRUE; |
|
|
|
|
MethodMatcher first = MethodMatchers.negate(original); |
|
|
|
|
MethodMatcher second = MethodMatchers.negate(original); |
|
|
|
|
assertThat(first).hasSameHashCodeAs(second); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void toStringIncludesRepresentationOfOriginalMatcher() { |
|
|
|
|
MethodMatcher original = MethodMatcher.TRUE; |
|
|
|
|
assertThat(MethodMatchers.negate(original)).hasToString("Negate " + original); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|