diff --git a/spring-cloud-bus/src/main/java/org/springframework/cloud/bus/BusPathMatcher.java b/spring-cloud-bus/src/main/java/org/springframework/cloud/bus/BusPathMatcher.java index 340cae4..73ed246 100644 --- a/spring-cloud-bus/src/main/java/org/springframework/cloud/bus/BusPathMatcher.java +++ b/spring-cloud-bus/src/main/java/org/springframework/cloud/bus/BusPathMatcher.java @@ -26,12 +26,14 @@ import java.lang.annotation.Target; import org.springframework.beans.factory.annotation.Qualifier; /** + * Qualifier annotation for components to do with matching paths in the bus. + * * @author Dave Syer * */ @Qualifier -@Target({ ElementType.FIELD, ElementType.METHOD, - ElementType.ANNOTATION_TYPE, ElementType.PARAMETER }) +@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE, + ElementType.PARAMETER }) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented 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 446226d..98f2846 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 @@ -3,6 +3,7 @@ package org.springframework.cloud.bus.event; import java.util.UUID; import org.springframework.context.ApplicationEvent; +import org.springframework.util.StringUtils; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonTypeInfo; @@ -31,7 +32,8 @@ public abstract class RemoteApplicationEvent extends ApplicationEvent { if (destinationService == null) { destinationService = "*"; } - if (!destinationService.contains(":")) { + if (StringUtils.countOccurrencesOf(destinationService, ":") <= 1 + && !destinationService.contains("*")) { // All instances of the destination unless specifically requested destinationService = destinationService + ":**"; } 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 new file mode 100644 index 0000000..60fb199 --- /dev/null +++ b/spring-cloud-bus/src/test/java/org/springframework/cloud/bus/ServiceMatcherTests.java @@ -0,0 +1,119 @@ +/* + * Copyright 2012-2015 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.cloud.bus; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import java.util.Collections; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.cloud.bus.event.EnvironmentChangeRemoteApplicationEvent; +import org.springframework.context.support.StaticApplicationContext; +import org.springframework.util.AntPathMatcher; + +/** + * @author Dave Syer + * + */ +public class ServiceMatcherTests { + + private ServiceMatcher matcher = new ServiceMatcher(); + private StaticApplicationContext context = new StaticApplicationContext(); + + @Before + public void init() { + context.setId("one:two:8888"); + context.refresh(); + matcher.setMatcher(new AntPathMatcher(":")); + matcher.setApplicationContext(context); + } + + @Test + public void fromSelf() { + assertThat( + matcher.isFromSelf( + new EnvironmentChangeRemoteApplicationEvent(this, "one:two:8888", + "foo:bar:spam", Collections.emptyMap())), + is(true)); + } + + @Test + public void forSelf() { + assertThat( + matcher.isForSelf( + new EnvironmentChangeRemoteApplicationEvent(this, "foo:bar:spam", + "one:two:8888", Collections.emptyMap())), + is(true)); + } + + @Test + public void forSelfWithWildcard() { + assertThat( + matcher.isForSelf( + new EnvironmentChangeRemoteApplicationEvent(this, "foo:bar:spam", + "one:two:*", Collections.emptyMap())), + is(true)); + } + + @Test + public void forSelfWithDoubleWildcard() { + assertThat( + matcher.isForSelf( + new EnvironmentChangeRemoteApplicationEvent(this, "foo:bar:spam", + "one:**", Collections.emptyMap())), + is(true)); + } + + @Test + public void forSelfWithNoWildcard() { + assertThat( + matcher.isForSelf( + new EnvironmentChangeRemoteApplicationEvent(this, "foo:bar:spam", + "one", Collections.emptyMap())), + is(true)); + } + + @Test + public void forSelfWithProfileNoWildcard() { + assertThat( + matcher.isForSelf( + new EnvironmentChangeRemoteApplicationEvent(this, "foo:bar:spam", + "one:two", Collections.emptyMap())), + is(true)); + } + + @Test + public void notForSelf() { + assertThat( + matcher.isForSelf( + new EnvironmentChangeRemoteApplicationEvent(this, "foo:bar:spam", + "one:two:9999", Collections.emptyMap())), + is(false)); + } + + @Test + public void notFromSelf() { + assertThat( + matcher.isFromSelf( + new EnvironmentChangeRemoteApplicationEvent(this, "one:two:9999", + "foo:bar:spam", Collections.emptyMap())), + is(false)); + } + +}