Polish "Introduce reverse on ClassFilter and MethodFilter"

See gh-26725
This commit is contained in:
Stephane Nicoll
2023-08-28 15:01:17 +02:00
parent 13e62d0b8e
commit 1f6fd16dae
4 changed files with 165 additions and 66 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 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.
@@ -24,6 +24,9 @@ import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.core.NestedRuntimeException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Unit tests for {@link ClassFilters}.
@@ -67,12 +70,64 @@ class ClassFiltersTests {
}
@Test
void reversion() {
assertThat(exceptionFilter.matches(RuntimeException.class)).isTrue();
ClassFilter reversion = ClassFilters.reversion(exceptionFilter);
assertThat(reversion.matches(RuntimeException.class)).isFalse();
assertThat(reversion.toString())
.matches("^.+ReversionClassFilter: .+RootClassFilter: .+Exception$");
void negateClassFilter() {
ClassFilter filter = mock(ClassFilter.class);
given(filter.matches(String.class)).willReturn(true);
ClassFilter negate = ClassFilters.negate(filter);
assertThat(negate.matches(String.class)).isFalse();
verify(filter).matches(String.class);
}
@Test
void negateTrueClassFilter() {
ClassFilter negate = ClassFilters.negate(ClassFilter.TRUE);
assertThat(negate.matches(String.class)).isFalse();
assertThat(negate.matches(Object.class)).isFalse();
assertThat(negate.matches(Integer.class)).isFalse();
}
@Test
void negateTrueClassFilterAppliedTwice() {
ClassFilter negate = ClassFilters.negate(ClassFilters.negate(ClassFilter.TRUE));
assertThat(negate.matches(String.class)).isTrue();
assertThat(negate.matches(Object.class)).isTrue();
assertThat(negate.matches(Integer.class)).isTrue();
}
@Test
void negateIsNotEqualsToOriginalFilter() {
ClassFilter original = ClassFilter.TRUE;
ClassFilter negate = ClassFilters.negate(original);
assertThat(original).isNotEqualTo(negate);
}
@Test
void negateOnSameFilterIsEquals() {
ClassFilter original = ClassFilter.TRUE;
ClassFilter first = ClassFilters.negate(original);
ClassFilter second = ClassFilters.negate(original);
assertThat(first).isEqualTo(second);
}
@Test
void negateHasNotSameHashCodeAsOriginalFilter() {
ClassFilter original = ClassFilter.TRUE;
ClassFilter negate = ClassFilters.negate(original);
assertThat(original).doesNotHaveSameHashCodeAs(negate);
}
@Test
void negateOnSameFilterHasSameHashCode() {
ClassFilter original = ClassFilter.TRUE;
ClassFilter first = ClassFilters.negate(original);
ClassFilter second = ClassFilters.negate(original);
assertThat(first).hasSameHashCodeAs(second);
}
@Test
void toStringIncludesRepresentationOfOriginalFilter() {
ClassFilter original = ClassFilter.TRUE;
assertThat(ClassFilters.negate(original)).hasToString("Negate " + original);
}
}

View File

@@ -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;
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;
*/
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 {
}
@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);
}