Routing { public static <T extends Route> T build(Class<T> routeType, Consumer<String> consumer) { PathImplementor implementor = RoutingImpl.getImplementorByClass(routeType); if (implementor == null) { throw new IllegalArgumentException("Invalid route type: " + routeType); } @SuppressWarnings("unchecked") T result = (T) implementor.write(consumer); if (result == null) { throw new IllegalArgumentException("Invalid route type: " + routeType); } return result; } private  Routing(); static T build(Class<T> routeType, Consumer<String> consumer); static T open(Window window, Class<T> routeType); static T open(Class<T> routeType);  }
@Test public void writes() { new UsersApp().parse(""); SavingConsumer consumer = new SavingConsumer(); UsersRoute route = Routing.build(UsersRoute.class, consumer); route.users(); assertEquals("users", consumer.consumed); route.viewUser(23); assertEquals("users/23", consumer.consumed); route.editUser(42); assertEquals("users/42/edit", consumer.consumed); }
@Test public void writesDateParam() { new DateRouteImpl().parse(); SavingConsumer consumer = new SavingConsumer(); DateRoute route = Routing.build(DateRoute.class, consumer); route.path(new Date(1445810760000L)); assertEquals("date/2015-10-25T22:06:00", consumer.consumed); }
@Test public void writesStringParam() { new StringRouteImpl().parse(); SavingConsumer consumer = new SavingConsumer(); StringRoute route = Routing.build(StringRoute.class, consumer); route.path("baz"); assertEquals("foo/baz/bar", consumer.consumed); route.nondeterminatePath("bazz"); assertEquals("foo/bazzz", consumer.consumed); route.path("/"); assertEquals("foo/%2F/bar", consumer.consumed); }