Remove PathPatternRegistry

This commit is contained in:
Rossen Stoyanchev
2017-07-10 11:37:18 +02:00
parent e7b77cb2b6
commit fac35ebec2
9 changed files with 131 additions and 446 deletions

View File

@@ -1,133 +0,0 @@
/*
* Copyright 2002-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.
* 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.web.reactive.handler;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.http.server.reactive.PathContainer;
import org.springframework.web.util.pattern.PathPattern;
import org.springframework.web.util.pattern.PathPatternParser;
import org.springframework.web.util.pattern.PatternParseException;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link PathPatternRegistry}
*
* @author Brian Clozel
*/
public class PathPatternRegistryTests {
private final PathPatternRegistry<Object> registry = new PathPatternRegistry();
private final PathPatternParser parser = new PathPatternParser();
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void shouldPrependPatternsWithSlash() {
this.registry.register("foo/bar", new Object());
assertThat(this.registry.getPatternsMap().keySet(), contains(pattern("/foo/bar")));
}
@Test
public void shouldNotRegisterInvalidPatterns() {
this.thrown.expect(PatternParseException.class);
this.thrown.expectMessage(Matchers.containsString("Expected close capture character after variable name"));
this.registry.register("/{invalid", new Object());
}
@Test
public void registerPatternsWithSameSpecificity() {
PathPattern fooOne = this.parser.parse("/fo?");
PathPattern fooTwo = this.parser.parse("/f?o");
assertThat(fooOne.compareTo(fooTwo), is(0));
this.registry.register("/fo?", new Object());
this.registry.register("/f?o", new Object());
PathContainer path = PathContainer.parse("/foo", StandardCharsets.UTF_8);
Set<PathMatchResult<Object>> matches = this.registry.findMatches(path);
assertThat(toPatterns(matches), contains(pattern("/f?o"), pattern("/fo?")));
}
@Test
public void findNoMatch() {
this.registry.register("/foo/{bar}", new Object());
PathContainer path = PathContainer.parse("/other", StandardCharsets.UTF_8);
assertThat(this.registry.findMatches(path), hasSize(0));
}
@Test
public void orderMatchesBySpecificity() {
this.registry.register("/foo/{*baz}", new Object());
this.registry.register("/foo/bar/baz", new Object());
this.registry.register("/foo/bar/{baz}", new Object());
PathContainer path = PathContainer.parse("/foo/bar/baz", StandardCharsets.UTF_8);
Set<PathMatchResult<Object>> matches = this.registry.findMatches(path);
assertThat(toPatterns(matches), contains(pattern("/foo/bar/baz"), pattern("/foo/bar/{baz}"),
pattern("/foo/{*baz}")));
}
private List<PathPattern> toPatterns(Collection<PathMatchResult<Object>> results) {
return results.stream().map(PathMatchResult::getPattern).collect(Collectors.toList());
}
private static PathPatternMatcher pattern(String pattern) {
return new PathPatternMatcher(pattern);
}
private static class PathPatternMatcher extends BaseMatcher<PathPattern> {
private final String pattern;
public PathPatternMatcher(String pattern) {
this.pattern = pattern;
}
@Override
public boolean matches(Object item) {
if(item != null && item instanceof PathPattern) {
return ((PathPattern) item).getPatternString().equals(pattern);
}
return false;
}
@Override
public void describeTo(Description description) {
}
}
}

View File

@@ -55,7 +55,7 @@ public class AppCacheManifestTransformerTests {
ClassPathResource allowedLocation = new ClassPathResource("test/", getClass());
ResourceWebHandler resourceHandler = new ResourceWebHandler();
ResourceUrlProvider resourceUrlProvider = new ResourceUrlProvider();
resourceUrlProvider.setHandlerMap(Collections.singletonMap("/static/**", resourceHandler));
resourceUrlProvider.registerHandlers(Collections.singletonMap("/static/**", resourceHandler));
VersionResourceResolver versionResolver = new VersionResourceResolver();
versionResolver.setStrategyMap(Collections.singletonMap("/**", new ContentVersionStrategy()));

View File

@@ -54,7 +54,7 @@ public class CssLinkResourceTransformerTests {
ResourceWebHandler resourceHandler = new ResourceWebHandler();
ResourceUrlProvider resourceUrlProvider = new ResourceUrlProvider();
resourceUrlProvider.setHandlerMap(Collections.singletonMap("/static/**", resourceHandler));
resourceUrlProvider.registerHandlers(Collections.singletonMap("/static/**", resourceHandler));
VersionResourceResolver versionResolver = new VersionResourceResolver();
versionResolver.setStrategyMap(Collections.singletonMap("/**", new ContentVersionStrategy()));

View File

@@ -64,7 +64,7 @@ public class ResourceTransformerSupportTests {
handler.setLocations(Collections.singletonList(new ClassPathResource("test/", getClass())));
handler.setResourceResolvers(resolvers);
ResourceUrlProvider urlProvider = new ResourceUrlProvider();
urlProvider.setHandlerMap(Collections.singletonMap("/resources/**", handler));
urlProvider.registerHandlers(Collections.singletonMap("/resources/**", handler));
return urlProvider;
}

View File

@@ -69,7 +69,7 @@ public class ResourceUrlProviderTests {
this.handler.setLocations(locations);
this.handler.afterPropertiesSet();
this.handlerMap.put("/resources/**", this.handler);
this.urlProvider.setHandlerMap(this.handlerMap);
this.urlProvider.registerHandlers(this.handlerMap);
}
@@ -125,7 +125,7 @@ public class ResourceUrlProviderTests {
otherHandler.setResourceResolvers(resolvers);
this.handlerMap.put("/resources/*.css", otherHandler);
this.urlProvider.setHandlerMap(this.handlerMap);
this.urlProvider.registerHandlers(this.handlerMap);
PathContainer path = PathContainer.parse("/resources/foo.css", StandardCharsets.UTF_8);
String url = this.urlProvider.getForLookupPath(path).block(Duration.ofSeconds(5));