use of com.vaadin.flow.router.legacy.RouteLocation in project flow by vaadin.
the class RouterLink method buildUrl.
/**
* Creates a URL for a route by populating placeholder and wildcard slots
* with the given parameter values. The slots are populated starting from
* the beginning of the route. The number of parameters must match the
* number of slots in the route.
*
* @param route
* the route to use, not <code>null</code>
* @param parameters
* the parameter values to set in the route
* @return a URL with all placeholder and wildcard slots populated
*/
public static String buildUrl(String route, String... parameters) {
assert route != null;
assert parameters != null;
Iterator<String> parametersIterator = Arrays.asList(parameters).iterator();
RouteLocation location = new RouteLocation(new Location(route));
List<String> urlSegments = new ArrayList<>(location.getSegments().size());
location.visitSegments(new RouteSegmentVisitor() {
@Override
public void acceptPlaceholder(String placeholderName) {
if (!parametersIterator.hasNext()) {
throw new IllegalArgumentException(route + " has more placeholders than the number of given parameters: " + Arrays.toString(parameters));
}
urlSegments.add(parametersIterator.next());
}
@Override
public void acceptWildcard() {
if (parametersIterator.hasNext()) {
urlSegments.add(parametersIterator.next());
} else {
urlSegments.add("");
}
}
@Override
public void acceptSegment(String segment) {
urlSegments.add(segment);
}
});
if (parametersIterator.hasNext()) {
throw new IllegalArgumentException(route + " has fewer placeholders than the number of given parameters: " + Arrays.toString(parameters));
}
return new Location(urlSegments).getPath();
}
use of com.vaadin.flow.router.legacy.RouteLocation in project flow by vaadin.
the class RouteLocationTest method walkRouteWithInvalidWildcard.
@Test(expected = IllegalArgumentException.class)
public void walkRouteWithInvalidWildcard() {
RouteLocation location = new RouteLocation(new Location("foo/*/*"));
RouteSegmentVisitor visitor = EasyMock.createMock(RouteSegmentVisitor.class);
location.visitSegments(visitor);
}
use of com.vaadin.flow.router.legacy.RouteLocation in project flow by vaadin.
the class RouteLocationTest method testWalkRoute.
@Test
public void testWalkRoute() {
RouteLocation location = new RouteLocation(new Location("foo/{bar}/*"));
AtomicInteger calls = new AtomicInteger();
location.visitSegments(new RouteSegmentVisitor() {
@Override
public void acceptSegment(String segmentName) {
Assert.assertEquals("foo", segmentName);
Assert.assertEquals(1, calls.incrementAndGet());
}
@Override
public void acceptPlaceholder(String placeholderName) {
Assert.assertEquals("bar", placeholderName);
Assert.assertEquals(2, calls.incrementAndGet());
}
@Override
public void acceptWildcard() {
Assert.assertEquals(3, calls.incrementAndGet());
}
});
Assert.assertEquals(3, calls.get());
}
Aggregations