Update support for using "." as path separator

Issue: SPR-11660
This commit is contained in:
Rossen Stoyanchev
2014-07-14 18:47:51 -04:00
parent 928a466b5d
commit ab2526a586
26 changed files with 405 additions and 254 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2012 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,42 +18,34 @@ package org.springframework.messaging.handler;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized.Parameter;
import org.springframework.util.AntPathMatcher;
import java.util.Arrays;
import static org.junit.Assert.*;
/**
* Unit tests for {@link DestinationPatternsMessageCondition}.
*
* @author Rossen Stoyanchev
* @author Sebastien Deleuze
*/
@RunWith(Parameterized.class)
public class DestinationPatternsMessageConditionTests {
@Parameter(0)
public String pathSeparator;
@Parameters
public static Iterable<Object[]> arguments() {
return Arrays.asList(new Object[][]{{"/"}, {"."}});
}
@Test
public void prependSlash() {
DestinationPatternsMessageCondition c = condition("foo");
assertEquals("/foo", c.getPatterns().iterator().next());
}
@Test
public void prependSlashWithCustomPathSeparator() {
DestinationPatternsMessageCondition c =
new DestinationPatternsMessageCondition(new String[] {"foo"}, new AntPathMatcher("."));
assertEquals("Pre-pending should be disabled when not using '/' as path separator",
"foo", c.getPatterns().iterator().next());
}
// SPR-8255
@Test
@@ -65,21 +57,20 @@ public class DestinationPatternsMessageConditionTests {
@Test
public void combineEmptySets() {
DestinationPatternsMessageCondition c1 = condition();
DestinationPatternsMessageCondition c2 = suffixCondition();
DestinationPatternsMessageCondition c2 = condition();
assertEquals(condition(""), c1.combine(c2));
}
@Test
public void combineOnePatternWithEmptySet() {
DestinationPatternsMessageCondition c1 = condition("/type1",
pathSeparator + "type2");
DestinationPatternsMessageCondition c2 = suffixCondition();
DestinationPatternsMessageCondition c1 = condition("/type1", "/type2");
DestinationPatternsMessageCondition c2 = condition();
assertEquals(condition("/type1", pathSeparator + "type2"), c1.combine(c2));
assertEquals(condition("/type1", "/type2"), c1.combine(c2));
c1 = condition();
c2 = suffixCondition("/method1", "/method2");
c2 = condition("/method1", "/method2");
assertEquals(condition("/method1", "/method2"), c1.combine(c2));
}
@@ -87,12 +78,10 @@ public class DestinationPatternsMessageConditionTests {
@Test
public void combineMultiplePatterns() {
DestinationPatternsMessageCondition c1 = condition("/t1", "/t2");
DestinationPatternsMessageCondition c2 = suffixCondition(pathSeparator + "m1",
pathSeparator + "m2");
DestinationPatternsMessageCondition c2 = condition("/m1", "/m2");
assertEquals(
condition("/t1" + pathSeparator + "m1", "/t1" + pathSeparator + "m2",
"/t2" + pathSeparator + "m1", "/t2" + pathSeparator + "m2"), c1.combine(c2));
assertEquals(new DestinationPatternsMessageCondition(
"/t1/m1", "/t1/m2", "/t2/m1", "/t2/m2"), c1.combine(c2));
}
@Test
@@ -105,40 +94,35 @@ public class DestinationPatternsMessageConditionTests {
@Test
public void matchPattern() {
DestinationPatternsMessageCondition condition = condition(
"/foo" + pathSeparator + "*");
DestinationPatternsMessageCondition match = condition.getMatchingCondition(messageTo("/foo" + pathSeparator + "bar"));
DestinationPatternsMessageCondition condition = condition("/foo/*");
DestinationPatternsMessageCondition match = condition.getMatchingCondition(messageTo("/foo/bar"));
assertNotNull(match);
}
@Test
public void matchSortPatterns() {
DestinationPatternsMessageCondition condition = suffixCondition(
pathSeparator + "**", pathSeparator + "foo" + pathSeparator + "bar",
pathSeparator + "foo" + pathSeparator + "*");
DestinationPatternsMessageCondition match = condition.getMatchingCondition(messageTo(pathSeparator + "foo" + pathSeparator + "bar"));
DestinationPatternsMessageCondition expected = suffixCondition(
pathSeparator + "foo" + pathSeparator + "bar",
pathSeparator + "foo" + pathSeparator + "*", pathSeparator + "**");
DestinationPatternsMessageCondition condition = condition("/**", "/foo/bar", "/foo/*");
DestinationPatternsMessageCondition match = condition.getMatchingCondition(messageTo("/foo/bar"));
DestinationPatternsMessageCondition expected = condition("/foo/bar", "/foo/*", "/**");
assertEquals(expected, match);
}
@Test
public void compareEqualPatterns() {
DestinationPatternsMessageCondition c1 = suffixCondition(pathSeparator + "foo*");
DestinationPatternsMessageCondition c2 = suffixCondition(pathSeparator + "foo*");
DestinationPatternsMessageCondition c1 = condition("/foo*");
DestinationPatternsMessageCondition c2 = condition("/foo*");
assertEquals(0, c1.compareTo(c2, messageTo(pathSeparator + "foo")));
assertEquals(0, c1.compareTo(c2, messageTo("/foo")));
}
@Test
public void comparePatternSpecificity() {
DestinationPatternsMessageCondition c1 = suffixCondition(pathSeparator + "fo*");
DestinationPatternsMessageCondition c2 = suffixCondition(pathSeparator + "foo");
DestinationPatternsMessageCondition c1 = condition("/fo*");
DestinationPatternsMessageCondition c2 = condition("/foo");
assertEquals(1, c1.compareTo(c2, messageTo(pathSeparator + "foo")));
assertEquals(1, c1.compareTo(c2, messageTo("/foo")));
}
@Test
@@ -156,11 +140,7 @@ public class DestinationPatternsMessageConditionTests {
private DestinationPatternsMessageCondition condition(String... patterns) {
return new DestinationPatternsMessageCondition(patterns, new AntPathMatcher(this.pathSeparator));
}
private DestinationPatternsMessageCondition suffixCondition(String... patterns) {
return new DestinationPatternsMessageCondition(patterns, new AntPathMatcher(this.pathSeparator), false);
return new DestinationPatternsMessageCondition(patterns);
}
private Message<?> messageTo(String destination) {

View File

@@ -16,6 +16,8 @@
package org.springframework.messaging.simp.annotation.support;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -38,6 +40,7 @@ import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.util.AntPathMatcher;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
@@ -148,7 +151,7 @@ public class SimpAnnotationMethodMessageHandlerTests {
@Test
public void simpScope() {
ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();
Map<String, Object> map = new ConcurrentHashMap<>();
map.put("name", "value");
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
headers.setSessionId("session1");
@@ -160,6 +163,33 @@ public class SimpAnnotationMethodMessageHandlerTests {
assertEquals("scope", this.testController.method);
}
@Test
public void dotPathSeparator() {
DotPathSeparatorController controller = new DotPathSeparatorController();
this.messageHandler.setPathMatcher(new AntPathMatcher("."));
this.messageHandler.registerHandler(controller);
this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/"));
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
headers.setSessionId("session1");
headers.setSessionAttributes(new HashMap<>());
headers.setDestination("/app1/pre.foo");
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
this.messageHandler.handleMessage(message);
assertEquals("handleFoo", controller.method);
headers = SimpMessageHeaderAccessor.create();
headers.setSessionId("session1");
headers.setSessionAttributes(new HashMap<>());
headers.setDestination("/app2/pre.foo");
message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
this.messageHandler.handleMessage(message);
assertEquals("handleFoo", controller.method);
}
private static class TestSimpAnnotationMethodMessageHandler extends SimpAnnotationMethodMessageHandler {
@@ -232,6 +262,20 @@ public class SimpAnnotationMethodMessageHandlerTests {
}
}
@Controller
@MessageMapping("pre")
private static class DotPathSeparatorController {
private String method;
@MessageMapping("foo")
public void handleFoo() {
this.method = "handleFoo";
}
}
private static class StringTestValidator implements Validator {
private final String invalidValue;

View File

@@ -79,7 +79,7 @@ public class MessageBrokerConfigurationTests {
private AnnotationConfigApplicationContext customChannelContext;
private AnnotationConfigApplicationContext customMatchingContext;
private AnnotationConfigApplicationContext customPathMatcherContext;
@Before
@@ -101,9 +101,9 @@ public class MessageBrokerConfigurationTests {
this.customChannelContext.register(CustomChannelConfig.class);
this.customChannelContext.refresh();
this.customMatchingContext = new AnnotationConfigApplicationContext();
this.customMatchingContext.register(CustomMatchingSimpleBrokerConfig.class);
this.customMatchingContext.refresh();
this.customPathMatcherContext = new AnnotationConfigApplicationContext();
this.customPathMatcherContext.register(CustomPathMatcherConfig.class);
this.customPathMatcherContext.refresh();
}
@@ -407,17 +407,13 @@ public class MessageBrokerConfigurationTests {
}
@Test
public void customMatching() {
SimpleBrokerMessageHandler brokerHandler = this.customMatchingContext.getBean(SimpleBrokerMessageHandler.class);
DefaultSubscriptionRegistry subscriptionRegistry = (DefaultSubscriptionRegistry)brokerHandler.getSubscriptionRegistry();
AntPathMatcher pathMatcher = (AntPathMatcher)subscriptionRegistry.getPathMatcher();
DirectFieldAccessor accessor = new DirectFieldAccessor(pathMatcher);
assertEquals(".", accessor.getPropertyValue("pathSeparator"));
public void customPathMatcher() {
SimpleBrokerMessageHandler broker = this.customPathMatcherContext.getBean(SimpleBrokerMessageHandler.class);
DefaultSubscriptionRegistry registry = (DefaultSubscriptionRegistry) broker.getSubscriptionRegistry();
assertEquals("a.a", registry.getPathMatcher().combine("a", "a"));
SimpAnnotationMethodMessageHandler messageHandler = customMatchingContext.getBean(SimpAnnotationMethodMessageHandler.class);
pathMatcher = (AntPathMatcher)messageHandler.getPathMatcher();
accessor = new DirectFieldAccessor(pathMatcher);
assertEquals(".", accessor.getPropertyValue("pathSeparator"));
SimpAnnotationMethodMessageHandler handler = this.customPathMatcherContext.getBean(SimpAnnotationMethodMessageHandler.class);
assertEquals("a.a", handler.getPathMatcher().combine("a", "a"));
}
@@ -504,11 +500,11 @@ public class MessageBrokerConfigurationTests {
}
@Configuration
static class CustomMatchingSimpleBrokerConfig extends SimpleBrokerConfig {
static class CustomPathMatcherConfig extends SimpleBrokerConfig {
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.defaultSeparator(".").enableSimpleBroker("/topic", "/queue");
registry.setPathMatcher(new AntPathMatcher(".")).enableSimpleBroker("/topic", "/queue");
}
}