Three matching patterns is enough to identify a service

So "foo" should match "foo:**" and "foo:bar" should match
"foo:bar:**". Fixes gh-38.
This commit is contained in:
Dave Syer
2016-08-16 15:51:46 +01:00
parent 8fb076fdd3
commit e3dd8b450c
3 changed files with 126 additions and 3 deletions

View File

@@ -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

View File

@@ -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 + ":**";
}

View File

@@ -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.<String, String>emptyMap())),
is(true));
}
@Test
public void forSelf() {
assertThat(
matcher.isForSelf(
new EnvironmentChangeRemoteApplicationEvent(this, "foo:bar:spam",
"one:two:8888", Collections.<String, String>emptyMap())),
is(true));
}
@Test
public void forSelfWithWildcard() {
assertThat(
matcher.isForSelf(
new EnvironmentChangeRemoteApplicationEvent(this, "foo:bar:spam",
"one:two:*", Collections.<String, String>emptyMap())),
is(true));
}
@Test
public void forSelfWithDoubleWildcard() {
assertThat(
matcher.isForSelf(
new EnvironmentChangeRemoteApplicationEvent(this, "foo:bar:spam",
"one:**", Collections.<String, String>emptyMap())),
is(true));
}
@Test
public void forSelfWithNoWildcard() {
assertThat(
matcher.isForSelf(
new EnvironmentChangeRemoteApplicationEvent(this, "foo:bar:spam",
"one", Collections.<String, String>emptyMap())),
is(true));
}
@Test
public void forSelfWithProfileNoWildcard() {
assertThat(
matcher.isForSelf(
new EnvironmentChangeRemoteApplicationEvent(this, "foo:bar:spam",
"one:two", Collections.<String, String>emptyMap())),
is(true));
}
@Test
public void notForSelf() {
assertThat(
matcher.isForSelf(
new EnvironmentChangeRemoteApplicationEvent(this, "foo:bar:spam",
"one:two:9999", Collections.<String, String>emptyMap())),
is(false));
}
@Test
public void notFromSelf() {
assertThat(
matcher.isFromSelf(
new EnvironmentChangeRemoteApplicationEvent(this, "one:two:9999",
"foo:bar:spam", Collections.<String, String>emptyMap())),
is(false));
}
}