From 9ed1fa14036639d505e61fcc6a9e0c96b7dbbc88 Mon Sep 17 00:00:00 2001 From: Marius Bogoevici Date: Wed, 5 Oct 2016 07:27:32 -0400 Subject: [PATCH] Refine handling of wildcards (#44) Fixes #43 - Use a global wildcard if `destinationService` is null; - Append `:**` if the destinationService is not a global wildcard already, it is in the form `foo` or `foo:bar` and does not end in a global wildcard --- .../bus/event/RemoteApplicationEvent.java | 14 +++--- .../cloud/bus/ServiceMatcherTests.java | 45 +++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/spring-cloud-bus/src/main/java/org/springframework/cloud/bus/event/RemoteApplicationEvent.java b/spring-cloud-bus/src/main/java/org/springframework/cloud/bus/event/RemoteApplicationEvent.java index 98f2846..ce658a5 100644 --- a/spring-cloud-bus/src/main/java/org/springframework/cloud/bus/event/RemoteApplicationEvent.java +++ b/spring-cloud-bus/src/main/java/org/springframework/cloud/bus/event/RemoteApplicationEvent.java @@ -30,12 +30,16 @@ public abstract class RemoteApplicationEvent extends ApplicationEvent { super(source); this.originService = originService; if (destinationService == null) { - destinationService = "*"; + destinationService = "**"; } - if (StringUtils.countOccurrencesOf(destinationService, ":") <= 1 - && !destinationService.contains("*")) { - // All instances of the destination unless specifically requested - destinationService = destinationService + ":**"; + // If the destinationService is not already a wildcard, match everything that follows + // if there at most two path elements, and last element is not a global wildcard already + if (!"**".equals(destinationService)) { + if (StringUtils.countOccurrencesOf(destinationService, ":") <= 1 + && !StringUtils.endsWithIgnoreCase(destinationService, ":**")) { + // All instances of the destination unless specifically requested + destinationService = destinationService + ":**"; + } } this.destinationService = destinationService; this.id = UUID.randomUUID().toString(); diff --git a/spring-cloud-bus/src/test/java/org/springframework/cloud/bus/ServiceMatcherTests.java b/spring-cloud-bus/src/test/java/org/springframework/cloud/bus/ServiceMatcherTests.java index 60fb199..7ccf23a 100644 --- a/spring-cloud-bus/src/test/java/org/springframework/cloud/bus/ServiceMatcherTests.java +++ b/spring-cloud-bus/src/test/java/org/springframework/cloud/bus/ServiceMatcherTests.java @@ -71,6 +71,51 @@ public class ServiceMatcherTests { is(true)); } + @Test + public void forSelfWithGlobalWildcard() { + assertThat( + matcher.isForSelf( + new EnvironmentChangeRemoteApplicationEvent(this, "foo:bar:spam", + "**", Collections.emptyMap())), + is(true)); + } + + @Test + public void forSelfWithWildcardName() { + assertThat( + matcher.isForSelf( + new EnvironmentChangeRemoteApplicationEvent(this, "foo:bar:spam", + "o*", Collections.emptyMap())), + is(true)); + } + + @Test + public void forSelfWithWildcardNameAndProfile() { + assertThat( + matcher.isForSelf( + new EnvironmentChangeRemoteApplicationEvent(this, "foo:bar:spam", + "o*:t*", Collections.emptyMap())), + is(true)); + } + + @Test + public void forSelfWithWildcardString() { + assertThat( + matcher.isForSelf( + new EnvironmentChangeRemoteApplicationEvent(this, "foo:bar:spam", + "o*", Collections.emptyMap())), + is(true)); + } + + @Test + public void notForSelfWithWildCardNameAndMismatchingProfile() { + assertThat( + matcher.isForSelf( + new EnvironmentChangeRemoteApplicationEvent(this, "foo:bar:spam", + "o*:f*", Collections.emptyMap())), + is(false)); + } + @Test public void forSelfWithDoubleWildcard() { assertThat(