Browse files

If options doesn't have all significant keys, skip generation for the

route. ( #12 )
  • Loading branch information...
1 parent fcd6527 commit 8eee27d72a80e5ecca094ca7818991bda56dac42 @kawasima committed Sep 11, 2012
View
10 src/main/java/net/unit8/sastruts/routing/RouteSet.java
@@ -80,6 +80,8 @@ public String generate(Options options) {
}
List<Route> routes = routesByController(controller, action);
for(Route route : routes) {
+ if (!hasAllKey(route, options))
+ continue;
String results = route.generate(options, merged);
if (StringUtils.isNotEmpty(results)) {
return results;
@@ -88,6 +90,14 @@ public String generate(Options options) {
throw new RoutingException("No route matches " + options.toString());
}
+ private boolean hasAllKey(Route route, Options options) {
+ for (String key : route.significantKeys()) {
+ if (!options.containsKey(key))
+ return false;
+ }
+ return true;
+ }
+
private List<Route> routesByController(String controller, String action) {
Map<String, List<Route>> actionMap = routesByController.get(controller);
if (actionMap == null) {
View
2 src/test/java/net/unit8/sastruts/routing/NamespaceTest.java
@@ -15,7 +15,7 @@
public void test() {
Routes.load(ResourceUtil.getResourceAsFile("routes/namespace.xml"));
System.out.println(Routes.getRouteSet().toString());
- assertRecognizes("ns1.Blog#show?blogCd=01", "/ns1/blog/01");
+ assertRecognizes("ns1.ns2.Blog#show?blogCd=01", "/ns1/ns2/blog/01");
assertRecognizes("Blog#show?blogCd=01", "/ns2/blog/01");
assertRecognizes("ns3.Blog#show?blogCd=01", "/blog/01");
}
View
9 src/test/java/net/unit8/sastruts/routing/RequirementsTest.java
@@ -41,11 +41,20 @@ public void testParentheses() {
assertGenerates("/images/640x480", "Images#show?width=640&height=480");
assertRecognizes("Sample#detail?sampleCd=001", "/sample_001/detail");
assertRecognizes("Images#size?size=1024", "/images/1024");
+ assertRecognizes("Images#size?size=sacd", "/images/sacd");
}
@Test
public void generate() {
Routes.load(ResourceUtil.getResourceAsFile("routes/requirements.xml"));
String path = Routes.generate(new Options().$("controller", "Posts").$("action", "index").$("year", "2012").$("month","11"));
assertThat(path, is("/posts/2012/11"));
}
+
+ @Test
+ public void generateSameAction() {
+ Routes.load(ResourceUtil.getResourceAsFile("routes/requirements.xml"));
+ assertGenerates("/area_01/zip_03", "sample.Hoge#index?area=01&zip=03");
+ assertGenerates("/area_01/addr_04", "sample.Hoge#index?area=01&addr=04");
+ assertGenerates("/area_01/addr_04?zip=03", "sample.Hoge#index?area=01&addr=04&zip=03");
+ }
}
View
2 src/test/resources/routes/namespace.xml
@@ -1,5 +1,5 @@
<routes>
- <namespace name="ns1">
+ <namespace name="ns1/ns2">
<match path="blog/:blogCd" to="Blog#show"/>
</namespace>
View
12 src/test/resources/routes/requirements.xml
@@ -20,4 +20,16 @@
</requirements>
</match>
+ <match path="/area_(:area)/addr_(:addr)" controller="sample.Hoge" action="index">
+ <requirements>
+ <requirement name="area" value="\d+"/>
+ <requirement name="addr" value="\d+"/>
+ </requirements>
+ </match>
+ <match path="/area_(:area)/zip_(:zip)" controller="sample.Hoge" action="index">
+ <requirements>
+ <requirement name="area" value="\d+"/>
+ <requirement name="zip" value="\d+"/>
+ </requirements>
+ </match>
</routes>

0 comments on commit 8eee27d

Please sign in to comment.