boolean preservePath = false; if (descriptor != null) { regex = descriptor.getPattern(); preservePath = descriptor.preservePath; } else { String[] templateSuffixes; try { REVisitor<RuntimeException> transformer = descriptor.getCaptureGroup() ? new CaptureGroupTransformation() : new NonCaptureGroupTransformation(); REParser parser = new REParser(regex);
@Test public void testLanguage3() throws Exception { RouterAssert router = new RouterAssert(); Route r = router. append("/public/{gtn:lang}", Collections.singletonMap(Names.GTN_LANG, PathParam.matching(LANG_PATTERN).preservePath(true))). append("{gtn:sitename}{gtn:path}", Collections.singletonMap(Names.GTN_PATH, PathParam.matching(".*").preservePath(true))); Map<String, String> expectedParameters = new HashMap<String, String>(); expectedParameters.put(Names.GTN_LANG, "fr"); expectedParameters.put(Names.GTN_SITENAME, "classic"); expectedParameters.put(Names.GTN_PATH, "/home"); // // assertEquals(Collections.<String, String>emptyMap(), router.route("/public")); router.assertRoute(expectedParameters, "/public/fr/classic/home"); expectedParameters.put(Names.GTN_PATH, ""); router.assertRoute(expectedParameters, "/public/fr/classic"); expectedParameters.put(Names.GTN_LANG, ""); expectedParameters.put(Names.GTN_PATH, "/home"); router.assertRoute(expectedParameters, "/public/classic/home"); }
public Route popupate(Route parent) { Map<String, PathParam.Builder> parameters; if (this.parameters != null && this.parameters.size() > 0) { parameters = new HashMap<String, PathParam.Builder>(this.parameters.size()); for (Map.Entry<String, ParamDescriptor> parameter : this.parameters.entrySet()) { ParamDescriptor paramDescriptor = parameter.getValue(); PathParam.Builder builder = PathParam.matching(paramDescriptor.pattern); if (paramDescriptor.preservePath != null) { builder.setPreservePath(paramDescriptor.preservePath); } if (paramDescriptor.captureGroup != null) { builder.setCaptureGroup(paramDescriptor.captureGroup); } parameters.put(parameter.getKey(), builder); } } else { parameters = Collections.emptyMap(); } return parent.append(path, parameters); } }
boolean preservePath = false; if (descriptor != null) { regex = descriptor.getPattern(); preservePath = descriptor.preservePath; } else { String[] templateSuffixes; try { REVisitor<RuntimeException> transformer = descriptor.getCaptureGroup() ? new CaptureGroupTransformation() : new NonCaptureGroupTransformation(); REParser parser = new REParser(regex);
public Route popupate(Route parent) { Map<String, PathParam.Builder> parameters; if (this.parameters != null && this.parameters.size() > 0) { parameters = new HashMap<String, PathParam.Builder>(this.parameters.size()); for (Map.Entry<String, ParamDescriptor> parameter : this.parameters.entrySet()) { ParamDescriptor paramDescriptor = parameter.getValue(); PathParam.Builder builder = PathParam.matching(paramDescriptor.pattern); if (paramDescriptor.preservePath != null) { builder.setPreservePath(paramDescriptor.preservePath); } if (paramDescriptor.captureGroup != null) { builder.setCaptureGroup(paramDescriptor.captureGroup); } parameters.put(parameter.getKey(), builder); } } else { parameters = Collections.emptyMap(); } return parent.append(path, parameters); } }
@Test public void testLanguage3() throws Exception { RouterAssert router = new RouterAssert(); Route r = router. append("/public/{gtn:lang}", Collections.singletonMap(Names.GTN_LANG, PathParam.matching(LANG_PATTERN).preservePath(true))). append("{gtn:sitename}{gtn:path}", Collections.singletonMap(Names.GTN_PATH, PathParam.matching(".*").preservePath(true))); Map<String, String> expectedParameters = new HashMap<String, String>(); expectedParameters.put(Names.GTN_LANG, "fr"); expectedParameters.put(Names.GTN_SITENAME, "classic"); expectedParameters.put(Names.GTN_PATH, "/home"); // // assertEquals(Collections.<String, String>emptyMap(), router.route("/public")); router.assertRoute(expectedParameters, "/public/fr/classic/home"); expectedParameters.put(Names.GTN_PATH, ""); router.assertRoute(expectedParameters, "/public/fr/classic"); expectedParameters.put(Names.GTN_LANG, ""); expectedParameters.put(Names.GTN_PATH, "/home"); router.assertRoute(expectedParameters, "/public/classic/home"); }
@Test public void testZeroOrOneFollowedBySubRoute() throws Exception { RouterAssert router = new RouterAssert(); Route a = router.append("/{a}", Collections.singletonMap(Names.A, PathParam.matching("a?").preservePath(true))); Route b = a.append("/b"); // Map<String, String> expectedParameters = new HashMap<String, String>(); expectedParameters.put(Names.A, "a"); router.assertRoute(expectedParameters, "/a/b"); RouteMatch resolve = b.matches(expectedParameters); assertNotNull(resolve); assertEquals("/a/b", resolve.render()); // router.assertRoute(b, "/b"); }
@Test public void testZeroOrOneFollowedBySubRoute() throws Exception { RouterAssert router = new RouterAssert(); Route a = router.append("/{a}", Collections.singletonMap(Names.A, PathParam.matching("a?").preservePath(true))); Route b = a.append("/b"); // Map<String, String> expectedParameters = new HashMap<String, String>(); expectedParameters.put(Names.A, "a"); router.assertRoute(expectedParameters, "/a/b"); RouteMatch resolve = b.matches(expectedParameters); assertNotNull(resolve); assertEquals("/a/b", resolve.render()); // router.assertRoute(b, "/b"); }
@Test public void testWildcardPathParamWithPreservePath() throws Exception { RouterAssert router = new RouterAssert(); Route a= router.append("/{p}", Collections.singletonMap(Names.P, PathParam.matching(".*").preservePath(true))); // Render assertEquals("/", a.matches(Collections.singletonMap(Names.P, "")).render()); assertEquals("//", a.matches(Collections.singletonMap(Names.P, "/")).render()); assertEquals("/a", a.matches(Collections.singletonMap(Names.P, "a")).render()); assertEquals("/a/b", a.matches(Collections.singletonMap(Names.P, "a/b")).render()); // Route router.assertRoute(Collections.singletonMap(Names.P, ""), "/"); router.assertRoute(Collections.singletonMap(Names.P, "/"), "//"); router.assertRoute(Collections.singletonMap(Names.P, "a"), "/a"); router.assertRoute(Collections.singletonMap(Names.P, "a/b"), "/a/b"); }
@Test public void testWildcardPathParamWithPreservePath() throws Exception { RouterAssert router = new RouterAssert(); Route a= router.append("/{p}", Collections.singletonMap(Names.P, PathParam.matching(".*").preservePath(true))); // Render assertEquals("/", a.matches(Collections.singletonMap(Names.P, "")).render()); assertEquals("//", a.matches(Collections.singletonMap(Names.P, "/")).render()); assertEquals("/a", a.matches(Collections.singletonMap(Names.P, "a")).render()); assertEquals("/a/b", a.matches(Collections.singletonMap(Names.P, "a/b")).render()); // Route router.assertRoute(Collections.singletonMap(Names.P, ""), "/"); router.assertRoute(Collections.singletonMap(Names.P, "/"), "//"); router.assertRoute(Collections.singletonMap(Names.P, "a"), "/a"); router.assertRoute(Collections.singletonMap(Names.P, "a/b"), "/a/b"); }
@Test public void testOptionalParameter() throws Exception { RouterAssert router = new RouterAssert(); Route a = router.append("/{a}/b", Collections.singletonMap(Names.A, PathParam.matching("a?").preservePath(true))); // Map<String, String> expectedParameters = new HashMap<String, String>(); expectedParameters.put(Names.A, "a"); router.assertRoute(a, Collections.singletonMap(Names.A, "a"), "/a/b"); assertEquals("/a/b", a.matches(expectedParameters).render()); // expectedParameters.put(Names.A, ""); router.assertRoute(expectedParameters, "/b"); assertEquals("/b", a.matches(expectedParameters).render()); }
@Test public void testJSMin() throws Exception { RouterAssert router = new RouterAssert(); Route r = router.append("/foo{gtn:min}.js", Collections.singletonMap(Names.GTN_MIN, PathParam.matching("-(min)|").captureGroup(true))); // router.assertRoute(Collections.singletonMap(Names.GTN_MIN, "min"), "/foo-min.js"); router.assertRoute(Collections.singletonMap(Names.GTN_MIN, ""), "/foo.js"); assertNull(router.route("/foo-max.js")); // assertEquals("/foo-min.js", r.matches(Collections.singletonMap(Names.GTN_MIN, "min")).render()); assertEquals("/foo.js", r.matches(Collections.singletonMap(Names.GTN_MIN, "")).render()); assertNull(r.matches(Collections.singletonMap(Names.GTN_MIN, "max"))); } }
@Test public void testJSMin() throws Exception { RouterAssert router = new RouterAssert(); Route r = router.append("/foo{gtn:min}.js", Collections.singletonMap(Names.GTN_MIN, PathParam.matching("-(min)|").captureGroup(true))); // router.assertRoute(Collections.singletonMap(Names.GTN_MIN, "min"), "/foo-min.js"); router.assertRoute(Collections.singletonMap(Names.GTN_MIN, ""), "/foo.js"); assertNull(router.route("/foo-max.js")); // assertEquals("/foo-min.js", r.matches(Collections.singletonMap(Names.GTN_MIN, "min")).render()); assertEquals("/foo.js", r.matches(Collections.singletonMap(Names.GTN_MIN, "")).render()); assertNull(r.matches(Collections.singletonMap(Names.GTN_MIN, "max"))); } }
@Test public void testParamPreservePath() throws Exception { RouterAssert router = new RouterAssert(); Route a = router.append("/{p}", Collections.singletonMap(Names.P, PathParam.matching("[^/]+").preservePath(true))); // Route router.assertRoute(Collections.singletonMap(Names.P, "_"), "/_"); assertNull(router.route("//")); // Render assertNull(a.matches(Collections.singletonMap(Names.P, "/"))); }
@Test public void testPreservePath() throws Exception { RouterAssert router = new RouterAssert(); router.append("/{a}", Collections.singletonMap(Names.A, PathParam.matching(".*").preservePath(true))); router.assertRoute(Collections.singletonMap(Names.A, "a"), "/a"); router.assertRoute(Collections.singletonMap(Names.A, "//"), "///"); router.assertRoute(Collections.singletonMap(Names.A, "a/"), "/a/"); } }
@Test public void testLang() throws Exception { Router router = new Router(); Route r = router.append("/{a}b", Collections.singletonMap(Names.A, PathParam.matching("(([A-Za-z]{2})/)?").preservePath(true))); // assertEquals("/fr/b", r.matches(Collections.singletonMap(Names.A, "fr/")).render()); assertEquals("/b", r.matches(Collections.singletonMap(Names.A, "")).render()); }
@Test public void testOptionalParameter() throws Exception { RouterAssert router = new RouterAssert(); Route a = router.append("/{a}/b", Collections.singletonMap(Names.A, PathParam.matching("a?").preservePath(true))); // Map<String, String> expectedParameters = new HashMap<String, String>(); expectedParameters.put(Names.A, "a"); router.assertRoute(a, Collections.singletonMap(Names.A, "a"), "/a/b"); assertEquals("/a/b", a.matches(expectedParameters).render()); // expectedParameters.put(Names.A, ""); router.assertRoute(expectedParameters, "/b"); assertEquals("/b", a.matches(expectedParameters).render()); }
@Test public void testLanguage1() throws Exception { RouterAssert router = new RouterAssert(); router.append("/public/{gtn:lang}", Collections.singletonMap(Names.GTN_LANG, PathParam.matching(LANG_PATTERN).preservePath(true))); // router.assertRoute(Collections.singletonMap(Names.GTN_LANG, ""), "/public"); router.assertRoute(Collections.singletonMap(Names.GTN_LANG, "fr"), "/public/fr"); router.assertRoute(Collections.singletonMap(Names.GTN_LANG, "fr-FR"), "/public/fr-FR"); }
@Test public void testAvoidMatchingPrefix() throws Exception { RouterAssert router = new RouterAssert(); Route a = router.append("/{a}/ab/c", Collections.singletonMap(Names.A, PathParam.matching("a?").preservePath(true))); // Map<String, String> expectedParameters = new HashMap<String, String>(); expectedParameters.put(Names.A, ""); router.assertRoute(a, expectedParameters, "/ab/c"); assertEquals("/ab/c", a.matches(expectedParameters).render()); }
@Test public void testCaptureGroup() throws Exception { Router router = new Router(); Route r = router.append("/{a}", Collections.singletonMap(Names.A, PathParam.matching("a(.)c").captureGroup(true))); // assertEquals("/abc", r.matches(Collections.singletonMap(Names.A, "b")).render()); } }