public Route append(String path, Map<String, PathParam.Builder> params) { return append(path, RouteKind.MATCH, params); }
private void findAncestorOrSelfParams(List<PathParam> params) { findParams(params); if (parent != null) { parent.findAncestorOrSelfParams(params); } }
@Test public void testComponent() throws Exception { Map<String, String> expectedParameters = new HashMap<String, String>(); expectedParameters.put(Names.GTN_SITENAME, "classic"); expectedParameters.put(Names.GTN_SITETYPE, "portal"); expectedParameters.put(Names.GTN_PATH, "/"); // router.assertRoute(portal, expectedParameters, "/private/portal/classic/"); assertEquals("/private/portal/classic/", portal.matches(expectedParameters).render()); }
private void assertEquals(Route expectedRoute, Route route) { assertEquals(expectedRoute.getClass(), route.getClass()); assertEquals(expectedRoute.getSegmentNames(), route.getSegmentNames()); for (String segmentName : expectedRoute.getSegmentNames()) { assertEquals(expectedRoute.getSegmentSize(segmentName), route.getSegmentSize(segmentName)); for (int segmentIndex = 0;segmentIndex < expectedRoute.getSegmentSize(segmentName);segmentIndex++) { SegmentRoute expectedSegmentRoute = expectedRoute.getSegment(segmentName, segmentIndex); SegmentRoute segmentRoute = route.getSegment(segmentName, segmentIndex); assertEquals(expectedSegmentRoute, segmentRoute); assertEquals(expectedRoute.getPatternSize(), route.getPatternSize()); for (int i = 0;i < expectedRoute.getPatternSize();i++) { assertEquals(expectedRoute.getPattern(i), route.getPattern(i));
@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 testClear() throws Exception { Router router = new Router(); Route foo = router.append("/foo"); RouteMatch match = router.route("/foo"); assertNotNull(match); assertSame(foo, match.getRoute()); Route bar = foo.append("/bar"); // /*RouteMatch*/ match = router.route("/foo/bar"); assertNotNull(match); assertSame(bar, match.getRoute()); // foo.clearChildren(); match = router.route("/foo/bar"); assertNull(match); match = router.route("/foo"); assertNotNull(match); assertSame(foo, match.getRoute()); }
findAncestorOrSelfParams(ancestorParams); LinkedList<PathParam> descendantParams = new LinkedList<PathParam>(); for (PathParam param : ancestorParams) { route.findDescendantOrSelfParams(param.name, descendantParams); if (descendantParams.size() > 0) { throw new MalformedRouteException("Duplicate parameter " + param.name);
public final RouteMatch matches(Map<String, String> parameters) { // HashMap<String, String> unmatched = new HashMap<String, String>(parameters); HashMap<PathParam, String> matched = new HashMap<PathParam, String>(); // if (_matches(unmatched, matched)) { return new RouteMatch(this, unmatched, matched); } else { return null; } }
/** * Find the params having the specified <code>name</code> among this route or its descendants. * * @param name the name * @param params the list collecting the found params */ private void findDescendantOrSelfParams(String name, List<PathParam> params) { PathParam param = getParam(name); if (param != null) { params.add(param); } }
@Test public void testPrivateClassicSlash() throws Exception { Map<String, String> expectedParameters = new HashMap<String, String>(); expectedParameters.put(Names.GTN_SITENAME, "classic"); expectedParameters.put(Names.GTN_SITETYPE, "portal"); expectedParameters.put(Names.GTN_PATH, "/"); // router.assertRoute(portal, expectedParameters, "/private/portal/classic/"); assertEquals("/private/portal/classic/", portal.matches(expectedParameters).render()); }
private void assertEquals(Route expectedRoute, Route route) { assertEquals(expectedRoute.getClass(), route.getClass()); assertEquals(expectedRoute.getSegmentNames(), route.getSegmentNames()); for (String segmentName : expectedRoute.getSegmentNames()) { assertEquals(expectedRoute.getSegmentSize(segmentName), route.getSegmentSize(segmentName)); for (int segmentIndex = 0;segmentIndex < expectedRoute.getSegmentSize(segmentName);segmentIndex++) { SegmentRoute expectedSegmentRoute = expectedRoute.getSegment(segmentName, segmentIndex); SegmentRoute segmentRoute = route.getSegment(segmentName, segmentIndex); assertEquals(expectedSegmentRoute, segmentRoute); assertEquals(expectedRoute.getPatternSize(), route.getPatternSize()); for (int i = 0;i < expectedRoute.getPatternSize();i++) { assertEquals(expectedRoute.getPattern(i), route.getPattern(i));
@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 testClear() throws Exception { Router router = new Router(); Route foo = router.append("/foo"); RouteMatch match = router.route("/foo"); assertNotNull(match); assertSame(foo, match.getRoute()); Route bar = foo.append("/bar"); // /*RouteMatch*/ match = router.route("/foo/bar"); assertNotNull(match); assertSame(bar, match.getRoute()); // foo.clearChildren(); match = router.route("/foo/bar"); assertNull(match); match = router.route("/foo"); assertNotNull(match); assertSame(foo, match.getRoute()); }
findAncestorOrSelfParams(ancestorParams); LinkedList<PathParam> descendantParams = new LinkedList<PathParam>(); for (PathParam param : ancestorParams) { route.findDescendantOrSelfParams(param.name, descendantParams); if (descendantParams.size() > 0) { throw new MalformedRouteException("Duplicate parameter " + param.name);
public final RouteMatch matches(Map<String, String> parameters) { // HashMap<String, String> unmatched = new HashMap<String, String>(parameters); HashMap<PathParam, String> matched = new HashMap<PathParam, String>(); // if (_matches(unmatched, matched)) { return new RouteMatch(this, unmatched, matched); } else { return null; } }
/** * Find the params having the specified <code>name</code> among this route or its descendants. * * @param name the name * @param params the list collecting the found params */ private void findDescendantOrSelfParams(String name, List<PathParam> params) { PathParam param = getParam(name); if (param != null) { params.add(param); } }
@Test public void testComponent() throws Exception { Map<String, String> expectedParameters = new HashMap<String, String>(); expectedParameters.put(Names.GTN_SITENAME, "classic"); expectedParameters.put(Names.GTN_SITETYPE, "portal"); expectedParameters.put(Names.GTN_PATH, "/"); // router.assertRoute(portal, expectedParameters, "/private/portal/classic/"); assertEquals("/private/portal/classic/", portal.matches(expectedParameters).render()); }