diff --git a/spring-integration-core/src/main/java/org/springframework/integration/leader/event/OnFailedToAcquireMutexEvent.java b/spring-integration-core/src/main/java/org/springframework/integration/leader/event/OnFailedToAcquireMutexEvent.java index 9dbced97e7..51b124cbae 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/leader/event/OnFailedToAcquireMutexEvent.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/leader/event/OnFailedToAcquireMutexEvent.java @@ -26,6 +26,7 @@ import org.springframework.integration.leader.Context; * * @since 5.0 */ +@SuppressWarnings("serial") public class OnFailedToAcquireMutexEvent extends AbstractLeaderEvent { /** diff --git a/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/EqualsResultMatcher.java b/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/EqualsResultMatcher.java index 52952e24c2..2747f1d325 100644 --- a/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/EqualsResultMatcher.java +++ b/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/EqualsResultMatcher.java @@ -16,43 +16,46 @@ package org.springframework.integration.test.matcher; +import java.util.function.Supplier; + import org.hamcrest.Description; import org.hamcrest.DiagnosingMatcher; +import org.hamcrest.Factory; import org.springframework.util.ObjectUtils; /** * A matcher that evaluates against the result of invoking a function, - * wrapped by the {@link EqualsResultMatcher.Evaluator} + * wrapped by the {@link java.util.function.Supplier} * * The goal is to defer the computation until the matcher needs to be actually evaluated. * Mainly useful in conjunction with retrying matchers such as {@link EventuallyMatcher} * * @author Marius Bogoevici + * @author Artem Bilan + * * @since 4.2 */ public class EqualsResultMatcher extends DiagnosingMatcher { - private final Evaluator evaluator; + private final Supplier supplier; - public EqualsResultMatcher(Evaluator evaluator) { - this.evaluator = evaluator; + public EqualsResultMatcher(Supplier supplier) { + this.supplier = supplier; } @Override protected boolean matches(Object item, Description mismatchDescription) { - return ObjectUtils.nullSafeEquals(item, evaluator.evaluate()); + return ObjectUtils.nullSafeEquals(item, supplier.get()); } @Override public void describeTo(Description description) { } - public interface Evaluator { - U evaluate(); + @Factory + public static EqualsResultMatcher equalsResult(Supplier supplier) { + return new EqualsResultMatcher<>(supplier); } - public static EqualsResultMatcher equalsResult(Evaluator evaluator) { - return new EqualsResultMatcher(evaluator); - } } diff --git a/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/EventuallyMatcher.java b/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/EventuallyMatcher.java index 3b241cd2c5..f46d54d19c 100644 --- a/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/EventuallyMatcher.java +++ b/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/EventuallyMatcher.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2016 the original author or authors. + * Copyright 2013-2017 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. @@ -18,6 +18,7 @@ package org.springframework.integration.test.matcher; import org.hamcrest.Description; import org.hamcrest.DiagnosingMatcher; +import org.hamcrest.Factory; import org.hamcrest.Matcher; @@ -26,9 +27,9 @@ import org.hamcrest.Matcher; * * @param the type the wrapped matcher operates on * - * (Copied from {@code org.springframework.xd.test.fixtures.EventuallyMatcher}) - * * @author Eric Bottard + * @author Artem Bilan + * * @since 4.2 */ public class EventuallyMatcher extends DiagnosingMatcher { @@ -49,32 +50,36 @@ public class EventuallyMatcher extends DiagnosingMatcher { this.pause = pause; } + @Factory public static Matcher eventually(int nbAttempts, int pause, Matcher delegate) { - return new EventuallyMatcher(delegate, nbAttempts, pause); + return new EventuallyMatcher<>(delegate, nbAttempts, pause); } + @Factory public static Matcher eventually(Matcher delegate) { - return new EventuallyMatcher(delegate); + return new EventuallyMatcher<>(delegate); } @Override public void describeTo(Description description) { - description.appendDescriptionOf(delegate).appendText(String.format(", trying at most %d times", nbAttempts)); + description.appendDescriptionOf(this.delegate) + .appendText(String.format(", trying at most %d times", this.nbAttempts)); } @Override protected boolean matches(Object item, Description mismatchDescription) { - mismatchDescription.appendText(String.format("failed after %d*%d=%dms:%n", nbAttempts, pause, nbAttempts - * pause)); - for (int i = 0; i < nbAttempts; i++) { - boolean result = delegate.matches(item); + mismatchDescription.appendText( + String.format("failed after %d*%d=%dms:%n", this.nbAttempts, this.pause, this.nbAttempts * this.pause)); + + for (int i = 0; i < this.nbAttempts; i++) { + boolean result = this.delegate.matches(item); if (result) { return true; } - delegate.describeMismatch(item, mismatchDescription); + this.delegate.describeMismatch(item, mismatchDescription); mismatchDescription.appendText(", "); try { - Thread.sleep(pause); + Thread.sleep(this.pause); } catch (InterruptedException e) { Thread.currentThread().interrupt(); @@ -83,4 +88,5 @@ public class EventuallyMatcher extends DiagnosingMatcher { } return false; } + } diff --git a/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/HeaderMatcher.java b/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/HeaderMatcher.java index 217e09832b..b42d6b357a 100644 --- a/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/HeaderMatcher.java +++ b/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/HeaderMatcher.java @@ -23,6 +23,7 @@ import org.hamcrest.CoreMatchers; import org.hamcrest.Description; import org.hamcrest.Factory; import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; import org.junit.Assert; import org.springframework.messaging.Message; @@ -70,7 +71,7 @@ import org.springframework.messaging.MessageHeaders; * @author Artem Bilan * */ -public class HeaderMatcher extends TypeSafeMatcher> { +public class HeaderMatcher extends TypeSafeMatcher> { private final Matcher matcher; @@ -83,7 +84,7 @@ public class HeaderMatcher extends TypeSafeMatcher> { } @Override - public boolean matchesSafely(Message item) { + public boolean matchesSafely(Message item) { return this.matcher.matches(item.getHeaders()); } @@ -94,73 +95,73 @@ public class HeaderMatcher extends TypeSafeMatcher> { } @Factory - public static Matcher> hasHeader(String key, T value) { - return new HeaderMatcher(MapContentMatchers.hasEntry(key, value)); + public static HeaderMatcher

hasHeader(String key, V value) { + return new HeaderMatcher<>(MapContentMatchers.hasEntry(key, value)); } @Factory - public static Matcher> hasHeader(String key, Matcher valueMatcher) { - return new HeaderMatcher(MapContentMatchers.hasEntry(key, valueMatcher)); + public static HeaderMatcher

hasHeader(String key, Matcher valueMatcher) { + return new HeaderMatcher<>(MapContentMatchers.hasEntry(key, valueMatcher)); } @Factory - public static Matcher> hasHeaderKey(String key) { - return new HeaderMatcher(MapContentMatchers.hasKey(key)); + public static

HeaderMatcher

hasHeaderKey(String key) { + return new HeaderMatcher<>(MapContentMatchers.hasKey(key)); } @Factory - public static Matcher> hasAllHeaders(Map entries) { - return new HeaderMatcher(MapContentMatchers.hasAllEntries(entries)); + public static

HeaderMatcher

hasAllHeaders(Map entries) { + return new HeaderMatcher<>(MapContentMatchers.hasAllEntries(entries)); } @Factory - public static Matcher> hasMessageId(T value) { - return new HeaderMatcher(MapContentMatchers.hasEntry(MessageHeaders.ID, value)); + public static HeaderMatcher

hasMessageId(V value) { + return new HeaderMatcher<>(MapContentMatchers.hasEntry(MessageHeaders.ID, value)); } @Factory - public static Matcher> hasCorrelationId(T value) { - return new HeaderMatcher(MapContentMatchers.hasEntry("correlationId", value)); + public static HeaderMatcher

hasCorrelationId(V value) { + return new HeaderMatcher<>(MapContentMatchers.hasEntry("correlationId", value)); } @Factory - public static Matcher> hasSequenceNumber(Integer value) { + public static

HeaderMatcher

hasSequenceNumber(Integer value) { return hasSequenceNumber(CoreMatchers.is(value)); } @Factory - public static Matcher> hasSequenceNumber(Matcher matcher) { - return new HeaderMatcher(MapContentMatchers.hasEntry("sequenceNumber", matcher)); + public static

HeaderMatcher

hasSequenceNumber(Matcher matcher) { + return new HeaderMatcher<>(MapContentMatchers.hasEntry("sequenceNumber", matcher)); } @Factory - public static Matcher> hasSequenceSize(Integer value) { + public static

HeaderMatcher

hasSequenceSize(Integer value) { return hasSequenceSize(CoreMatchers.is(value)); } @Factory - public static Matcher> hasSequenceSize(Matcher value) { - return new HeaderMatcher(MapContentMatchers.hasEntry("sequenceSize", value)); + public static

HeaderMatcher

hasSequenceSize(Matcher value) { + return new HeaderMatcher<>(MapContentMatchers.hasEntry("sequenceSize", value)); } @Factory - public static Matcher> hasExpirationDate(Date value) { + public static

HeaderMatcher

hasExpirationDate(Date value) { return hasExpirationDate(CoreMatchers.is(value.getTime())); } @Factory - public static Matcher> hasExpirationDate(Matcher matcher) { - return new HeaderMatcher(MapContentMatchers.hasEntry("expirationDate", matcher)); + public static

HeaderMatcher

hasExpirationDate(Matcher matcher) { + return new HeaderMatcher<>(MapContentMatchers.hasEntry("expirationDate", matcher)); } @Factory - public static Matcher> hasTimestamp(Date value) { + public static

HeaderMatcher

hasTimestamp(Date value) { return hasTimestamp(CoreMatchers.is(value.getTime())); } @Factory - public static Matcher> hasTimestamp(Matcher matcher) { - return new HeaderMatcher(MapContentMatchers.hasEntry(MessageHeaders.TIMESTAMP, matcher)); + public static

HeaderMatcher

hasTimestamp(Matcher matcher) { + return new HeaderMatcher<>(MapContentMatchers.hasEntry(MessageHeaders.TIMESTAMP, matcher)); } } diff --git a/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/MapContentMatchers.java b/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/MapContentMatchers.java index 144abf3432..a94b6deab6 100644 --- a/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/MapContentMatchers.java +++ b/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/MapContentMatchers.java @@ -24,6 +24,7 @@ import org.hamcrest.Description; import org.hamcrest.Factory; import org.hamcrest.Matcher; import org.hamcrest.Matchers; +import org.hamcrest.TypeSafeMatcher; import org.hamcrest.core.AllOf; /** @@ -63,8 +64,7 @@ import org.hamcrest.core.AllOf; * @author Artem Bilan * */ -public class MapContentMatchers extends - TypeSafeMatcher> { +public class MapContentMatchers extends TypeSafeMatcher> { private final T key; diff --git a/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/MockitoMessageMatchers.java b/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/MockitoMessageMatchers.java index 21d6dd3cf4..f4b321f7e7 100644 --- a/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/MockitoMessageMatchers.java +++ b/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/MockitoMessageMatchers.java @@ -89,7 +89,7 @@ public class MockitoMessageMatchers { } public static Message messageWithHeaderEntry(String key, Matcher valueMatcher) { - return ArgumentMatchers.argThat(new HamcrestArgumentMatcher<>(HeaderMatcher.hasHeader(key, valueMatcher))); + return ArgumentMatchers.argThat(new HamcrestArgumentMatcher<>(HeaderMatcher.hasHeader(key, valueMatcher))); } public static Message messageWithHeaderEntries(Map entries) { diff --git a/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/PayloadAndHeaderMatcher.java b/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/PayloadAndHeaderMatcher.java index e81cc7ff79..fefe830de0 100644 --- a/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/PayloadAndHeaderMatcher.java +++ b/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/PayloadAndHeaderMatcher.java @@ -22,7 +22,6 @@ import java.util.Map; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; import org.hamcrest.Factory; -import org.hamcrest.Matcher; import org.springframework.messaging.Message; import org.springframework.messaging.MessageHeaders; @@ -50,20 +49,20 @@ import org.springframework.messaging.MessageHeaders; * @author Artem Bilan * */ -public class PayloadAndHeaderMatcher extends BaseMatcher> { +public class PayloadAndHeaderMatcher extends BaseMatcher> { - private final Object payload; + private final T payload; private final Map headers; private final String[] ignoreKeys; @Factory - public static Matcher> sameExceptIgnorableHeaders(Message expected, String... ignoreKeys) { - return new PayloadAndHeaderMatcher(expected, ignoreKeys); + public static

PayloadAndHeaderMatcher

sameExceptIgnorableHeaders(Message

expected, String... ignoreKeys) { + return new PayloadAndHeaderMatcher<>(expected, ignoreKeys); } - private PayloadAndHeaderMatcher(Message expected, String... ignoreKeys) { + private PayloadAndHeaderMatcher(Message expected, String... ignoreKeys) { this.ignoreKeys = ignoreKeys; this.payload = expected.getPayload(); this.headers = extractHeadersToAssert(expected); diff --git a/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/PayloadMatcher.java b/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/PayloadMatcher.java index de36257dda..e382ea15e6 100644 --- a/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/PayloadMatcher.java +++ b/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/PayloadMatcher.java @@ -19,6 +19,7 @@ package org.springframework.integration.test.matcher; import org.hamcrest.Description; import org.hamcrest.Factory; import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; import org.hamcrest.core.IsEqual; import org.junit.Assert; @@ -54,16 +55,17 @@ import org.springframework.messaging.Message; * * @author Alex Peters * @author Iwein Fuld + * @author Artem Bilan * */ -public class PayloadMatcher extends TypeSafeMatcher> { +public class PayloadMatcher extends TypeSafeMatcher> { - private final Matcher matcher; + private final Matcher matcher; /** * Create a PayloadMatcher that matches the payload of messages against the given matcher */ - private PayloadMatcher(Matcher matcher) { + private PayloadMatcher(Matcher matcher) { super(); this.matcher = matcher; } @@ -81,13 +83,13 @@ public class PayloadMatcher extends TypeSafeMatcher> { } @Factory - public static Matcher> hasPayload(T payload) { - return new PayloadMatcher(IsEqual.equalTo(payload)); + public static

PayloadMatcher

hasPayload(P payload) { + return new PayloadMatcher<>(IsEqual.equalTo(payload)); } @Factory - public static Matcher> hasPayload(Matcher payloadMatcher) { - return new PayloadMatcher(payloadMatcher); + public static

PayloadMatcher

hasPayload(Matcher

payloadMatcher) { + return new PayloadMatcher<>(payloadMatcher); } } diff --git a/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/TypeSafeMatcher.java b/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/TypeSafeMatcher.java deleted file mode 100644 index 03e58048ae..0000000000 --- a/spring-integration-test-support/src/main/java/org/springframework/integration/test/matcher/TypeSafeMatcher.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright 2002-2016 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.integration.test.matcher; - -import java.lang.reflect.Method; - -import org.hamcrest.BaseMatcher; - -/** - * This class was copied from JUnit to avoid using it from org.junit.internal (causing a backwards compatibility issue). - * If you want to extend this class use a recent version of JUnit, and extend - * org.junit.matchers.TypeSafeMatcher - *

- * Convenient base class for Matchers that require a non-null value of a specific type. - * This simply implements the null check, checks the type and then casts. - * - * @author Joe Walnes - */ -abstract class TypeSafeMatcher extends BaseMatcher { - - private final Class expectedType; - - /** - * Subclasses should implement this. The item will already have been checked for - * the specific type and will never be null. - * - * @param item The item. - * @return true if matches. - */ - public abstract boolean matchesSafely(T item); - - protected TypeSafeMatcher() { - expectedType = findExpectedType(getClass()); - } - - private static Class findExpectedType(Class fromClass) { - for (Class c = fromClass; c != Object.class; c = c.getSuperclass()) { - for (Method method : c.getDeclaredMethods()) { - if (isMatchesSafelyMethod(method)) { - return method.getParameterTypes()[0]; - } - } - } - - throw new Error("Cannot determine correct type for matchesSafely() method."); - } - - private static boolean isMatchesSafelyMethod(Method method) { - return method.getName().equals("matchesSafely") - && method.getParameterTypes().length == 1 - && !method.isSynthetic(); - } - - protected TypeSafeMatcher(Class expectedType) { - this.expectedType = expectedType; - } - - /** - * Method made final to prevent accidental override. - * If you need to override this, there's no point on extending TypeSafeMatcher. - * Instead, extend the {@link BaseMatcher}. - */ - @Override - @SuppressWarnings({ "unchecked" }) - public final boolean matches(Object item) { - return item != null - && expectedType.isInstance(item) - && matchesSafely((T) item); - } -} diff --git a/spring-integration-zookeeper/src/test/java/org/springframework/integration/zookeeper/metadata/ZookeeperMetadataStoreTests.java b/spring-integration-zookeeper/src/test/java/org/springframework/integration/zookeeper/metadata/ZookeeperMetadataStoreTests.java index 680505d2c6..2771aea4d2 100644 --- a/spring-integration-zookeeper/src/test/java/org/springframework/integration/zookeeper/metadata/ZookeeperMetadataStoreTests.java +++ b/spring-integration-zookeeper/src/test/java/org/springframework/integration/zookeeper/metadata/ZookeeperMetadataStoreTests.java @@ -47,7 +47,6 @@ import org.springframework.beans.DirectFieldAccessor; import org.springframework.integration.metadata.MetadataStoreListener; import org.springframework.integration.metadata.MetadataStoreListenerAdapter; import org.springframework.integration.support.utils.IntegrationUtils; -import org.springframework.integration.test.matcher.EqualsResultMatcher.Evaluator; import org.springframework.integration.zookeeper.ZookeeperTestSupport; /** @@ -115,22 +114,12 @@ public class ZookeeperMetadataStoreTests extends ZookeeperTestSupport { assertEquals("Integration", IntegrationUtils.bytesToString(client.getData().forPath(metadataStore.getPath(testKey)), "UTF-8")); assertEquals("Integration", metadataStore.get(testKey)); - assertThat("Integration", eventually(equalsResult(new Evaluator() { - @Override - public String evaluate() { - return otherMetadataStore.get(testKey); - } - }))); + assertThat("Integration", eventually(equalsResult(() -> otherMetadataStore.get(testKey)))); otherMetadataStore.putIfAbsent(testKey2, "Integration-2"); assertEquals("Integration-2", IntegrationUtils.bytesToString(client.getData().forPath(metadataStore.getPath(testKey2)), "UTF-8")); assertEquals("Integration-2", otherMetadataStore.get(testKey2)); - assertThat("Integration-2", eventually(equalsResult(new Evaluator() { - @Override - public String evaluate() { - return metadataStore.get(testKey2); - } - }))); + assertThat("Integration-2", eventually(equalsResult(() -> otherMetadataStore.get(testKey2)))); CloseableUtils.closeQuietly(otherClient); } @@ -148,21 +137,11 @@ public class ZookeeperMetadataStoreTests extends ZookeeperTestSupport { assertEquals("Integration", IntegrationUtils.bytesToString(client.getData().forPath(metadataStore.getPath(testKey)), "UTF-8")); assertEquals("Integration", metadataStore.get(testKey)); - assertThat("Integration", eventually(equalsResult(new Evaluator() { - @Override - public String evaluate() { - return otherMetadataStore.get(testKey); - } - }))); + assertThat("Integration", eventually(equalsResult(() -> otherMetadataStore.get(testKey)))); otherMetadataStore.replace(testKey, "Integration", "Integration-2"); assertEquals("Integration-2", IntegrationUtils.bytesToString(client.getData().forPath(metadataStore.getPath(testKey)), "UTF-8")); - assertThat("Integration-2", eventually(equalsResult(new Evaluator() { - @Override - public String evaluate() { - return metadataStore.get(testKey); - } - }))); + assertThat("Integration-2", eventually(equalsResult(() -> metadataStore.get(testKey)))); assertEquals("Integration-2", otherMetadataStore.get(testKey)); CloseableUtils.closeQuietly(otherClient); } @@ -246,6 +225,7 @@ public class ZookeeperMetadataStoreTests extends ZookeeperTestSupport { assertThat(e.getMessage(), containsString("'listener' must not be null")); } metadataStore.addListener(new MetadataStoreListenerAdapter() { + @Override public void onAdd(String key, String value) { notifiedChanges.add(Arrays.asList("add", key, value)); @@ -316,6 +296,7 @@ public class ZookeeperMetadataStoreTests extends ZookeeperTestSupport { barriers.put("remove", new CyclicBarrier(2)); barriers.put("update", new CyclicBarrier(2)); metadataStore.addListener(new MetadataStoreListenerAdapter() { + @Override public void onAdd(String key, String value) { notifiedChanges.add(Arrays.asList("add", key, value));