Router implements Serializable { public int navigate(UI ui, Location location, NavigationTrigger trigger) { return navigate(ui, location, trigger, null); } Router(RouteRegistry registry); void initializeUI(UI ui, VaadinRequest initRequest); void initializeUI(UI ui, Location location); Optional<NavigationState> resolveNavigationTarget(String pathInfo,
            Map<String, String[]> parameterMap); Optional<NavigationState> resolveNavigationTarget(
            Location location); Optional<NavigationState> resolveRouteNotFoundNavigationTarget(); int navigate(UI ui, Location location, NavigationTrigger trigger); int navigate(UI ui, Location location, NavigationTrigger trigger,
            JsonValue state); RouteRegistry getRegistry(); Optional<ErrorTargetEntry> getErrorNavigationTarget(
            Exception exception);  }
@Test public void basic_navigation() throws InvalidRouteConfigurationException { setNavigationTargets(RootNavigationTarget.class, FooNavigationTarget.class, FooBarNavigationTarget.class); router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals(RootNavigationTarget.class, getUIComponent()); router.navigate(ui, new Location("foo"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals(FooNavigationTarget.class, getUIComponent()); router.navigate(ui, new Location("foo/bar"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals(FooBarNavigationTarget.class, getUIComponent()); }
@Test public void page_title_set_from_annotation() throws InvalidRouteConfigurationException { setNavigationTargets(NavigationTargetWithTitle.class); router.navigate(ui, new Location("navigation-target-with-title"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Custom Title", ui.getInternals().getTitle()); }
@Test public void page_title_not_set_from_annotation_in_parent() throws InvalidRouteConfigurationException { setNavigationTargets(ChildWithoutTitle.class); router.navigate(ui, new Location("parent-with-title/child"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("", ui.getInternals().getTitle()); }
@Test public void page_title_set_dynamically() throws InvalidRouteConfigurationException { setNavigationTargets(NavigationTargetWithDynamicTitle.class); router.navigate(ui, new Location("navigation-target-with-dynamic-title"), NavigationTrigger.PROGRAMMATIC); assertThat("Dynamic title is wrong", ui.getInternals().getTitle(), is(DYNAMIC_TITLE)); }
@Test public void page_title_set_dynamically_from_url_parameter() throws InvalidRouteConfigurationException { setNavigationTargets(NavigationTargetWithDynamicTitleFromUrl.class); router.navigate(ui, new Location("url/hello"), NavigationTrigger.PROGRAMMATIC); assertThat("Dynamic title is wrong", ui.getInternals().getTitle(), is("hello")); }
@Test public void page_title_set_dynamically_from_event_handler() throws InvalidRouteConfigurationException { setNavigationTargets( NavigationTargetWithDynamicTitleFromNavigation.class); router.navigate(ui, new Location("url"), NavigationTrigger.PROGRAMMATIC); assertThat("Dynamic title is wrong", ui.getInternals().getTitle(), is("ACTIVATING")); }
@Test public void before_navigation_event_is_triggered() throws InvalidRouteConfigurationException { FooBarNavigationTarget.events.clear(); setNavigationTargets(RootNavigationTarget.class, FooNavigationTarget.class, FooBarNavigationTarget.class); router.navigate(ui, new Location("foo/bar"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 1, FooBarNavigationTarget.events.size()); Assert.assertEquals("Unexpected event type", BeforeEnterEvent.class, FooBarNavigationTarget.events.get(0).getClass()); }
@Test public void leave_and_enter_listeners_only_receive_correct_state() throws InvalidRouteConfigurationException { setNavigationTargets(LeavingNavigationTarget.class, EnteringNavigationTarget.class, RootNavigationTarget.class); router.navigate(ui, new Location("enteringTarget"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("BeforeEnterObserver should have fired.", 1, EnteringNavigationTarget.events.size()); router.navigate(ui, new Location("leavingTarget"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("No leave or enter target should have fired.", 1, EnteringNavigationTarget.events.size()); Assert.assertEquals("No leave or enter target should have fired.", 0, LeavingNavigationTarget.events.size()); router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("BeforeLeaveObserver should have fired", 1, LeavingNavigationTarget.events.size()); }
@Test public void leave_navigate_and_enter_listeners_execute_in_correct_order() throws InvalidRouteConfigurationException { setNavigationTargets(CombinedObserverTarget.class, RootNavigationTarget.class); router.navigate(ui, new Location("combined"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("BeforeEnterObserver should have fired.", 1, Enter.events.size()); Assert.assertEquals("BeforeNavigationObserver should have fired.", 1, CombinedObserverTarget.Before.events.size()); router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("BeforeLeaveObserver target should have fired.", 1, Leave.events.size()); Assert.assertEquals( "BeforeNavigationObserver target should have fired.", 2, CombinedObserverTarget.Before.events.size()); Assert.assertEquals("LeaveListener got event", BeforeLeaveEvent.class, CombinedObserverTarget.Before.events.get(1).getClass()); }
@Test public void before_navigation_event_is_triggered_for_attach_and_detach() throws InvalidRouteConfigurationException { FooBarNavigationTarget.events.clear(); setNavigationTargets(RootNavigationTarget.class, FooNavigationTarget.class, FooBarNavigationTarget.class); router.navigate(ui, new Location("foo/bar"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 1, FooBarNavigationTarget.events.size()); Assert.assertEquals(BeforeEnterEvent.class, FooBarNavigationTarget.events.get(0).getClass()); router.navigate(ui, new Location("foo"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 2, FooBarNavigationTarget.events.size()); Assert.assertEquals(BeforeLeaveEvent.class, FooBarNavigationTarget.events.get(1).getClass()); }
@Test public void reroute_on_before_navigation_event() throws InvalidRouteConfigurationException { FooBarNavigationTarget.events.clear(); ReroutingNavigationTarget.events.clear(); RootNavigationTarget.events.clear(); setNavigationTargets(RootNavigationTarget.class, ReroutingNavigationTarget.class, FooBarNavigationTarget.class); router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC); Map<String, String> params = new HashMap<>(); params.put("foo", "bar"); QueryParameters queryParameters = QueryParameters.simple(params); router.navigate(ui, new Location("reroute", queryParameters), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 1, ReroutingNavigationTarget.events.size()); Assert.assertEquals("Expected event amount was wrong", 1, FooBarNavigationTarget.events.size()); Assert.assertEquals(FooBarNavigationTarget.class, getUIComponent()); Assert.assertEquals(BeforeEnterEvent.class, ReroutingNavigationTarget.events.get(0).getClass()); Assert.assertEquals(BeforeEnterEvent.class, FooBarNavigationTarget.events.get(0).getClass()); QueryParameters rerouteQueryParameters = FooBarNavigationTarget.events .get(0).getLocation().getQueryParameters(); Assert.assertNotNull(rerouteQueryParameters); List<String> foo = rerouteQueryParameters.getParameters().get("foo"); Assert.assertNotNull(foo); Assert.assertFalse(foo.isEmpty()); Assert.assertEquals(foo.get(0), "bar"); }
@Test public void before_and_after_event_fired_in_correct_order() throws InvalidRouteConfigurationException { NavigationEvents.events.clear(); setNavigationTargets(NavigationEvents.class); router.navigate(ui, new Location("navigationEvents"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 2, NavigationEvents.events.size()); Assert.assertEquals("Before navigation event was wrong.", BeforeEnterEvent.class, NavigationEvents.events.get(0).getClass()); Assert.assertEquals("After navigation event was wrong.", AfterNavigationEvent.class, NavigationEvents.events.get(1).getClass()); }
@Test public void after_event_not_fired_on_detach() throws InvalidRouteConfigurationException { NavigationEvents.events.clear(); setNavigationTargets(NavigationEvents.class, FooNavigationTarget.class); router.navigate(ui, new Location("navigationEvents"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 2, NavigationEvents.events.size()); Assert.assertEquals("Before navigation event was wrong.", BeforeEnterEvent.class, NavigationEvents.events.get(0).getClass()); router.navigate(ui, new Location("foo"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 3, NavigationEvents.events.size()); Assert.assertEquals("After navigation event was wrong.", BeforeLeaveEvent.class, NavigationEvents.events.get(2).getClass()); }
@Test public void reroute_with_url_parameter() throws InvalidRouteConfigurationException { RouteWithParameter.events.clear(); setNavigationTargets(GreetingNavigationTarget.class, RouteWithParameter.class, RerouteToRouteWithParam.class); router.navigate(ui, new Location("redirect/to/param"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 2, RouteWithParameter.events.size()); Assert.assertEquals("Before navigation event was wrong.", "hello", RouteWithParameter.param); }
@Test public void reroute_fails_with_no_url_parameter() throws InvalidRouteConfigurationException { setNavigationTargets(GreetingNavigationTarget.class, ParameterRouteNoParameter.class, RerouteToRouteWithParam.class); String locationString = "redirect/to/param"; int result = router.navigate(ui, new Location(locationString), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals( "Routing with mismatching parameters should have failed -", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, result); String message = "No route 'param' accepting the parameters [hello] was found."; String exceptionText = String.format(EXCEPTION_WRAPPER_MESSAGE, locationString, message); assertExceptionComponent(exceptionText); }
@Test public void reroute_fails_with_faulty_url_parameter() throws InvalidRouteConfigurationException { setNavigationTargets(GreetingNavigationTarget.class, RouteWithParameter.class, FailRerouteWithParam.class); String locationString = "fail/param"; router.navigate(ui, new Location(locationString), NavigationTrigger.PROGRAMMATIC); String message = "Given route parameter 'class java.lang.Boolean' is of the wrong type. Required 'class java.lang.String'."; String exceptionText = String.format(EXCEPTION_WRAPPER_MESSAGE, locationString, message); assertExceptionComponent(exceptionText); }
@Test public void reroute_with_multiple_route_parameters() throws InvalidRouteConfigurationException { setNavigationTargets(GreetingNavigationTarget.class, RouteWithMultipleParameters.class, RerouteToRouteWithMultipleParams.class); router.navigate(ui, new Location("redirect/to/params"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 2, RouteWithMultipleParameters.events.size()); Assert.assertEquals("Before navigation event was wrong.", "this/must/work", RouteWithMultipleParameters.param); }
@Test public void reroute_fails_with_faulty_route_parameters() throws InvalidRouteConfigurationException { setNavigationTargets(GreetingNavigationTarget.class, RouteWithMultipleParameters.class, FailRerouteWithParams.class); String locationString = "fail/params"; int result = router.navigate(ui, new Location(locationString), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals( "Routing with mismatching parameters should have failed -", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, result); String message = "Given route parameter 'class java.lang.Long' is of the wrong type. Required 'class java.lang.String'."; String exceptionText = String.format(EXCEPTION_WRAPPER_MESSAGE, locationString, message); assertExceptionComponent(exceptionText); }
@Test public void reroute_with_multiple_route_parameters_fails_to_parameterless_target() throws InvalidRouteConfigurationException { setNavigationTargets(GreetingNavigationTarget.class, ParameterRouteNoParameter.class, RerouteToRouteWithMultipleParams.class); String locationString = "redirect/to/params"; int result = router.navigate(ui, new Location(locationString), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals( "Routing with mismatching parameters should have failed -", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, result); String message = "No route 'param' accepting the parameters [this, must, work] was found."; String exceptionText = String.format(EXCEPTION_WRAPPER_MESSAGE, locationString, message); assertExceptionComponent(exceptionText); }
@Test public void reroute_with_multiple_route_parameters_fails_to_single_parameter_target() throws InvalidRouteConfigurationException { setNavigationTargets(GreetingNavigationTarget.class, RouteWithParameter.class, RerouteToRouteWithMultipleParams.class); String locationString = "redirect/to/params"; int result = router.navigate(ui, new Location(locationString), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals( "Routing with mismatching parameters should have failed -", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, result); String message = "No route 'param' accepting the parameters [this, must, work] was found."; String exceptionText = String.format(EXCEPTION_WRAPPER_MESSAGE, locationString, message); assertExceptionComponent(exceptionText); }
@Test public void route_precedence_when_one_has_parameter() throws InvalidRouteConfigurationException { RouteWithParameter.events.clear(); setNavigationTargets(RouteWithParameter.class, StaticParameter.class); router.navigate(ui, new Location("param/param"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals(RouteWithParameter.class, getUIComponent()); Assert.assertEquals("Expected event amount was wrong", 2, RouteWithParameter.events.size()); Assert.assertEquals("Before navigation event was wrong.", "param", RouteWithParameter.param); router.navigate(ui, new Location("param/static"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals( "Did not get correct class even though StaticParameter should have precedence over RouteWithParameter due to exact url match.", StaticParameter.class, getUIComponent()); }
@Test public void optional_parameter_gets_parameter() throws InvalidRouteConfigurationException { OptionalParameter.events.clear(); setNavigationTargets(OptionalParameter.class); router.navigate(ui, new Location("optional/parameter"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 1, OptionalParameter.events.size()); Assert.assertEquals("Before navigation event was wrong.", "parameter", OptionalParameter.param); }
@Test public void optional_parameter_matches_no_parameter() throws InvalidRouteConfigurationException { OptionalParameter.events.clear(); OptionalParameter.param = null; setNavigationTargets(OptionalParameter.class); router.navigate(ui, new Location("optional"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 1, OptionalParameter.events.size()); Assert.assertNull("Before navigation event was wrong.", OptionalParameter.param); }
@Test public void correctly_return_route_with_one_base_route_with_optionals() throws InvalidRouteConfigurationException { setNavigationTargets(RouteWithParameter.class, ParameterRouteNoParameter.class); router.navigate(ui, new Location("param/parameter"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Failed", RouteWithParameter.class, getUIComponent()); }
@Test public void navigateToRoot_errorCode_dontRedirect() throws InvalidRouteConfigurationException { setNavigationTargets(FooNavigationTarget.class); Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, router.navigate( ui, new Location(""), NavigationTrigger.PROGRAMMATIC)); }
@Test public void navigating_to_route_with_wildcard_parameter() throws InvalidRouteConfigurationException { WildParameter.events.clear(); WildParameter.param = null; setNavigationTargets(WildParameter.class); router.navigate(ui, new Location("wild"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 1, WildParameter.events.size()); Assert.assertEquals("Parameter should be empty", "", WildParameter.param); router.navigate(ui, new Location("wild/single"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 2, WildParameter.events.size()); Assert.assertEquals("Parameter should be empty", "single", WildParameter.param); router.navigate(ui, new Location("wild/multi/part/parameter"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 3, WildParameter.events.size()); Assert.assertEquals("Parameter should be empty", "multi/part/parameter", WildParameter.param); }
@Test public void route_with_wildcard_parameter_should_be_last_hit() throws InvalidRouteConfigurationException { WildParameter.events.clear(); WildParameter.param = null; setNavigationTargets(WildParameter.class, WildHasParameter.class, WildNormal.class); router.navigate(ui, new Location("wild"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 0, WildHasParameter.events.size()); router.navigate(ui, new Location("wild/parameter"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 1, WildHasParameter.events.size()); Assert.assertEquals("Parameter didn't match expected value", "parameter", WildHasParameter.param); router.navigate(ui, new Location("wild/multi/part/parameter"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 1, WildParameter.events.size()); Assert.assertEquals("Parameter didn't match expected value", "multi/part/parameter", WildParameter.param); }
@Test public void root_navigation_target_with_required_parameter() throws InvalidRouteConfigurationException { RootParameter.events.clear(); setNavigationTargets(RootParameter.class); router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals( "Has url with required parameter should not match to \"\"", 0, RootParameter.events.size()); }
@Test public void reroute_on_hasParameter_step() throws InvalidRouteConfigurationException { RootParameter.events.clear(); setNavigationTargets(RootParameter.class, RedirectOnSetParam.class); router.navigate(ui, new Location("param/reroute/hello"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 1, RootParameter.events.size()); Assert.assertEquals("Parameter should be empty", "hello", RootParameter.param); }
@Test public void has_url_with_supported_parameters_navigation() throws InvalidRouteConfigurationException { setNavigationTargets(IntegerParameter.class, LongParameter.class, BooleanParameter.class); router.navigate(ui, new Location("integer/5"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 1, IntegerParameter.events.size()); Assert.assertEquals("Parameter should be empty", 5, IntegerParameter.param.intValue()); router.navigate(ui, new Location("long/5"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 1, LongParameter.events.size()); Assert.assertEquals("Parameter should be empty", 5, LongParameter.param.longValue()); router.navigate(ui, new Location("boolean/true"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 1, BooleanParameter.events.size()); Assert.assertEquals("Parameter should be empty", true, BooleanParameter.param); }
@Test public void default_wildcard_support_only_for_string() throws InvalidRouteConfigurationException { setNavigationTargets(UnsupportedWildParameter.class); String locationString = "usupported/wildcard/3/4/1"; int result = router.navigate(ui, new Location(locationString), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Non existent route should have returned.", HttpServletResponse.SC_NOT_FOUND, result); String message = String.format( "Invalid wildcard parameter in class %s. Only String is supported for wildcard parameters.", UnsupportedWildParameter.class.getName()); String exceptionText1 = String.format("Could not navigate to '%s'", locationString); String exceptionText2 = String.format( "Reason: Failed to parse url parameter, exception: %s", new UnsupportedOperationException(message)); assertExceptionComponent(RouteNotFoundError.class, exceptionText1, exceptionText2); }
@Test public void unparsable_url_parameter() throws InvalidRouteConfigurationException { setNavigationTargets(LongParameter.class); String locationString = "long/unsupportedParam"; int result = router.navigate(ui, new Location(locationString), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Non existent route should have returned.", HttpServletResponse.SC_NOT_FOUND, result); String exceptionText1 = String.format("Could not navigate to '%s'", locationString); String exceptionText2 = String.format( "Reason: Couldn't find route for '%s'", locationString); assertExceptionComponent(RouteNotFoundError.class, exceptionText1, exceptionText2); }
@Test public void redirect_to_routeNotFound_error_view_when_no_route_found() throws InvalidRouteConfigurationException { ErrorTarget.events.clear(); setNavigationTargets(FooNavigationTarget.class); setErrorNavigationTargets(ErrorTarget.class); String locationString = "error"; int result = router.navigate(ui, new Location(locationString), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Non existent route should have returned.", HttpServletResponse.SC_NOT_FOUND, result); Assert.assertEquals("Expected event amount was wrong", 1, ErrorTarget.events.size()); String errorMessage = ErrorTarget.message; Assert.assertTrue(errorMessage.contains( String.format("Could not navigate to '%s'", locationString))); Assert.assertTrue(errorMessage.contains( String.format("Couldn't find route for '%s'", locationString))); }
@Test public void exception_during_navigation_is_caught_and_show_in_internalServerError() throws InvalidRouteConfigurationException { setNavigationTargets(FailOnException.class); int result = router.navigate(ui, new Location("exception"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Non existent route should have returned.", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, result); }
@Test public void fail_for_multiple_of_the_same_class() throws InvalidRouteConfigurationException { setErrorNavigationTargets(ErrorTarget.class, RouteNotFoundError.class); int result = router.navigate(ui, new Location("exception"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Non existent route should have returned.", HttpServletResponse.SC_NOT_FOUND, result); Assert.assertEquals( "Expected the extending class to be used instead of the super class", ErrorTarget.class, getUIComponent()); }
@Test public void custom_exception_target_should_override_default_ones() { setErrorNavigationTargets(NonExtendingNotFoundTarget.class, RouteNotFoundError.class); int result = router.navigate(ui, new Location("exception"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Non existent route should have returned.", HttpServletResponse.SC_NOT_FOUND, result); Assert.assertEquals( "Expected the extending class to be used instead of the super class", NonExtendingNotFoundTarget.class, getUIComponent()); assertExceptionComponent(NonExtendingNotFoundTarget.class, EXCEPTION_TEXT); }
@Test public void custom_exception_target_is_used() { setErrorNavigationTargets(CustomNotFoundTarget.class, RouteNotFoundError.class); int result = router.navigate(ui, new Location("exception"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Non existent route should have returned.", HttpServletResponse.SC_NOT_FOUND, result); Assert.assertEquals( "Expected the extending class to be used instead of the super class", CustomNotFoundTarget.class, getUIComponent()); assertExceptionComponent(CustomNotFoundTarget.class, EXCEPTION_TEXT); }
@Test public void error_target_has_parent_layout() throws InvalidRouteConfigurationException { setNavigationTargets(LoneRoute.class); setErrorNavigationTargets(ErrorTargetWithParent.class, RouteNotFoundError.class); int result = router.navigate(ui, new Location("exception"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Non existent route should have returned.", HttpServletResponse.SC_NOT_FOUND, result); Component parenComponent = ComponentUtil .findParentComponent(ui.getElement().getChild(0)).get(); Assert.assertEquals(RouteParent.class, parenComponent.getClass()); List<Class<?>> childClasses = parenComponent.getChildren() .map(Object::getClass).collect(Collectors.toList()); Assert.assertEquals( Arrays.asList(RouterLink.class, ErrorTargetWithParent.class), childClasses); }
@Test public void reroute_to_error_opens_expected_error_target() throws InvalidRouteConfigurationException { setNavigationTargets(RerouteToError.class); setErrorNavigationTargets(IllegalTarget.class); int result = router.navigate(ui, new Location("beforeToError/exception"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Target should have rerouted to exception target.", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, result); Assert.assertEquals(IllegalTarget.class, getUIComponent()); Optional<Component> visibleComponent = ui.getElement().getChild(0) .getComponent(); Assert.assertEquals("Illegal argument exception.", visibleComponent.get().getElement().getText()); }
@Test public void reroute_to_error_with_custom_message_message_is_used() throws InvalidRouteConfigurationException { IllegalTarget.events.clear(); setNavigationTargets(RerouteToErrorWithMessage.class); setErrorNavigationTargets(IllegalTarget.class); int result = router.navigate(ui, new Location("beforeToError/message/CustomMessage"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Target should have rerouted to exception target.", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, result); Assert.assertEquals(IllegalTarget.class, getUIComponent()); Optional<Component> visibleComponent = ui.getElement().getChild(0) .getComponent(); Assert.assertEquals("CustomMessage", visibleComponent.get().getElement().getText()); Assert.assertEquals("Expected only one event message from error view", 1, IllegalTarget.events.size()); BeforeEnterEvent event = (BeforeEnterEvent) IllegalTarget.events.get(0); Assert.assertEquals("Parameter should be empty", "beforeToError/message/CustomMessage", event.getLocation().getPath()); }
@Test public void reroute_to_error_from_has_param() throws InvalidRouteConfigurationException { setNavigationTargets(RedirectToNotFoundInHasParam.class); int result = router.navigate(ui, new Location("toNotFound/error"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Target should have rerouted to exception target.", HttpServletResponse.SC_NOT_FOUND, result); Assert.assertEquals(RouteNotFoundError.class, getUIComponent()); }
@Test public void forward_and_reroute_at_the_same_time_exception() throws InvalidRouteConfigurationException { String location = "forwardAndReroute/exception"; FooBarNavigationTarget.events.clear(); ForwardingAndReroutingNavigationTarget.events.clear(); RootNavigationTarget.events.clear(); setNavigationTargets(RootNavigationTarget.class, ForwardingAndReroutingNavigationTarget.class, FooBarNavigationTarget.class); router.navigate(ui, new Location(location), NavigationTrigger.PROGRAMMATIC); String validationMessage = "Error forward & reroute can not be set at the same time"; String errorMessage = String.format( "There was an exception while trying to navigate to '%s' with the exception message '%s'", location, validationMessage); assertExceptionComponent(InternalServerError.class, errorMessage); }
@Test public void faulty_error_response_code_should_throw_exception() throws InvalidRouteConfigurationException { setNavigationTargets(RerouteToError.class); setErrorNavigationTargets(FaultyErrorView.class); String location = "beforeToError/exception"; int result = router.navigate(ui, new Location(location), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals( "Target should have failed on an internal exception.", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, result); String validationMessage = String.format( "Error state code must be a valid HttpServletResponse value. Received invalid value of '%s' for '%s'", 0, FaultyErrorView.class.getName()); String errorMessage = String.format( "There was an exception while trying to navigate to '%s' with the exception message '%s'", location, validationMessage); assertExceptionComponent(InternalServerError.class, errorMessage); }
@Test public void repeatedly_navigating_to_same_ur_through_ui_navigate_should_not_loop() throws InvalidRouteConfigurationException { LoopByUINavigate.events.clear(); setNavigationTargets(LoopByUINavigate.class); ui.navigate("loop"); Assert.assertEquals("Expected only one request to loop", 1, LoopByUINavigate.events.size()); Assert.assertNull("Last handled location should have been cleared", ui.getInternals().getLastHandledLocation()); }
@Test public void ui_navigate_should_not_loop() throws InvalidRouteConfigurationException { LoopByUINavigate.events.clear(); RedirectToLoopByReroute.events.clear(); setNavigationTargets(LoopByUINavigate.class, RedirectToLoopByReroute.class); ui.navigate("redirect/loop"); Assert.assertEquals("Expected one events", 1, LoopByUINavigate.events.size()); Assert.assertEquals("Expected onve events", 1, RedirectToLoopByReroute.events.size()); }
@Test public void ui_navigate_should_only_have_one_history_marking_on_loop() throws InvalidRouteConfigurationException { setNavigationTargets(LoopByUINavigate.class); ui.navigate("loop"); long historyInvocations = ui.getInternals() .dumpPendingJavaScriptInvocations().stream() .filter(js -> js.getInvocation().getExpression() .contains("history.pushState")) .count(); assertEquals(1, historyInvocations); Assert.assertNull("Last handled location should have been cleared", ui.getInternals().getLastHandledLocation()); }
@Test public void router_navigate_should_not_loop() throws InvalidRouteConfigurationException { setNavigationTargets(LoopOnRouterNavigate.class); ui.navigate("loop"); Assert.assertEquals("Expected only one request", 1, LoopOnRouterNavigate.events.size()); Assert.assertNull("Last handled location should have been cleared", ui.getInternals().getLastHandledLocation()); }
@Test public void exception_while_navigating_should_succeed_and_clear_last_handled() throws InvalidRouteConfigurationException { setNavigationTargets(FailOnException.class); ui.navigate("exception"); Assert.assertNull("Last handled location should have been cleared", ui.getInternals().getLastHandledLocation()); }
@Test public void exception_in_exception_handler_while_navigating_should_clear_last_handled() throws InvalidRouteConfigurationException { setNavigationTargets(FailOnException.class); setErrorNavigationTargets(FailingErrorHandler.class); try { ui.navigate("exception"); Assert.fail("No runtime exception was thrown from navigation"); } catch (Exception re) { Assert.assertNull( "Last handled location should have been cleared even though navigation failed", ui.getInternals().getLastHandledLocation()); } }
@Test public void postpone_then_resume_on_before_navigation_event() throws InvalidRouteConfigurationException, InterruptedException { RootNavigationTarget.events.clear(); PostponingAndResumingNavigationTarget.events.clear(); setNavigationTargets(RootNavigationTarget.class, PostponingAndResumingNavigationTarget.class); int status1 = router.navigate(ui, new Location("postpone"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("First transition failed", HttpServletResponse.SC_OK, status1); Assert.assertEquals(PostponingAndResumingNavigationTarget.class, getUIComponent()); Assert.assertEquals("Expected event amount was wrong", 0, PostponingAndResumingNavigationTarget.events.size()); int status2 = router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Second transition failed", HttpServletResponse.SC_OK, status2); Assert.assertEquals(RootNavigationTarget.class, getUIComponent()); Assert.assertEquals( "Expected event in the first target amount was wrong", 1, PostponingAndResumingNavigationTarget.events.size()); Assert.assertEquals( "Expected event amount in the last target was wrong", 1, RootNavigationTarget.events.size()); }
@Test public void postpone_forever_on_before_navigation_event() throws InvalidRouteConfigurationException { RootNavigationTarget.events.clear(); PostponingAndResumingNavigationTarget.events.clear(); setNavigationTargets(RootNavigationTarget.class, PostponingForeverNavigationTarget.class); int status1 = router.navigate(ui, new Location("postpone"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("First transition failed", HttpServletResponse.SC_OK, status1); Assert.assertEquals(PostponingForeverNavigationTarget.class, getUIComponent()); int status2 = router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Second transition failed", HttpServletResponse.SC_OK, status2); Assert.assertEquals(PostponingForeverNavigationTarget.class, getUIComponent()); Assert.assertEquals("Expected event amount in the target was wrong", 1, PostponingForeverNavigationTarget.events.size()); Assert.assertEquals("Expected event amount in the root was wrong", 0, RootNavigationTarget.events.size()); }
@Test public void postpone_obsoleted_by_new_navigation_transition() throws InvalidRouteConfigurationException, InterruptedException { FooBarNavigationTarget.events.clear(); FooBarNavigationTarget.events.clear(); setNavigationTargets(FooNavigationTarget.class, FooBarNavigationTarget.class, PostponingFirstTimeNavigationTarget.class); int status1 = router.navigate(ui, new Location("postpone"), NavigationTrigger.PROGRAMMATIC); int status2 = router.navigate(ui, new Location("foo"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Expected event amount was wrong", 1, PostponingFirstTimeNavigationTarget.events.size()); BeforeLeaveEvent event = PostponingFirstTimeNavigationTarget.events .get(0); int status3 = router.navigate(ui, new Location("foo/bar"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("First transition failed", HttpServletResponse.SC_OK, status1); Assert.assertEquals(FooBarNavigationTarget.class, getUIComponent()); event.postpone().proceed(); Assert.assertEquals("Second transition failed", HttpServletResponse.SC_OK, status2); Assert.assertEquals("Third transition failed", HttpServletResponse.SC_OK, status3); Assert.assertEquals(FooBarNavigationTarget.class, getUIComponent()); Assert.assertEquals("Expected event amount was wrong", 2, PostponingFirstTimeNavigationTarget.events.size()); Assert.assertEquals("Expected event amount was wrong", 1, FooBarNavigationTarget.events.size()); }
@Test public void postpone_then_resume_with_multiple_listeners() throws InvalidRouteConfigurationException, InterruptedException { setNavigationTargets(RootNavigationTarget.class, PostponingAndResumingCompoundNavigationTarget.class); int status1 = router.navigate(ui, new Location("postpone"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("First transition failed", HttpServletResponse.SC_OK, status1); Assert.assertEquals(PostponingAndResumingCompoundNavigationTarget.class, getUIComponent()); int status2 = router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Second transition failed", HttpServletResponse.SC_OK, status2); Assert.assertNotNull( PostponingAndResumingCompoundNavigationTarget.postpone); PostponingAndResumingCompoundNavigationTarget.postpone.proceed(); Assert.assertEquals(RootNavigationTarget.class, getUIComponent()); Assert.assertEquals(1, PostponingAndResumingCompoundNavigationTarget.events.size()); Assert.assertEquals(2, ChildListener.events.size()); Assert.assertEquals(BeforeEnterEvent.class, ChildListener.events.get(0).getClass()); Assert.assertEquals(BeforeLeaveEvent.class, ChildListener.events.get(1).getClass()); }
@Test public void navigation_should_fire_locale_change_observer() throws InvalidRouteConfigurationException { Translations.events.clear(); setNavigationTargets(Translations.class); ui.navigate(""); Assert.assertEquals("Expected event amount was wrong", 1, Translations.events.size()); Assert.assertEquals(Locale.getDefault(), Translations.events.get(0).getLocale()); }
@Test public void away_navigation_should_not_inform_observer() throws InvalidRouteConfigurationException, InterruptedException { Translations.events.clear(); setNavigationTargets(FooNavigationTarget.class, Translations.class); ui.navigate(""); Assert.assertEquals("Expected event amount was wrong", 1, Translations.events.size()); Assert.assertEquals(Locale.getDefault(), Translations.events.get(0).getLocale()); ui.navigate("foo"); Assert.assertEquals("Recorded event amount should have stayed the same", 1, Translations.events.size()); }
@Test public void route_as_parent_layout_handles_as_expected() throws InvalidRouteConfigurationException { setNavigationTargets(BaseLayout.class, SubLayout.class); ui.navigate("base"); Assert.assertEquals(MainLayout.class, getUIComponent()); List<Component> children = ui.getChildren() .collect(Collectors.toList()); Assert.assertEquals(1, children.size()); Assert.assertEquals(MainLayout.class, children.get(0).getClass()); children = children.get(0).getChildren().collect(Collectors.toList()); Assert.assertEquals(1, children.size()); Assert.assertEquals(BaseLayout.class, children.get(0).getClass()); children = children.get(0).getChildren().collect(Collectors.toList()); Assert.assertTrue(children.isEmpty()); ui.navigate("sub"); Assert.assertEquals(MainLayout.class, getUIComponent()); children = ui.getChildren().collect(Collectors.toList()); Assert.assertEquals(1, children.size()); Assert.assertEquals(MainLayout.class, children.get(0).getClass()); children = children.get(0).getChildren().collect(Collectors.toList()); Assert.assertEquals(1, children.size()); Assert.assertEquals(BaseLayout.class, children.get(0).getClass()); children = children.get(0).getChildren().collect(Collectors.toList()); Assert.assertEquals(1, children.size()); Assert.assertEquals(SubLayout.class, children.get(0).getClass()); children = children.get(0).getChildren().collect(Collectors.toList()); Assert.assertTrue(children.isEmpty()); }
@Test public void proceedRightAfterPostpone_navigationIsDone() throws InvalidRouteConfigurationException { setNavigationTargets(ProceedRightAfterPospone.class, RootNavigationTarget.class); RootNavigationTarget.events.clear(); router.navigate(ui, new Location("foo"), NavigationTrigger.PROGRAMMATIC); router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals(1, RootNavigationTarget.events.size()); Assert.assertEquals(AfterNavigationEvent.class, RootNavigationTarget.events.get(0).getClass()); }
@Test public void navigateWithinOneParent_oneLeaveEventOneEnterEvent() throws InvalidRouteConfigurationException { setNavigationTargets(RouteChild.class, LoneRoute.class); router.navigate(ui, new Location("parent/child"), NavigationTrigger.PROGRAMMATIC); RouteChild.events.clear(); router.navigate(ui, new Location("single"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals(1, RouteChild.events.size()); Assert.assertEquals(BeforeLeaveEvent.class, RouteChild.events.get(0).getClass()); Assert.assertEquals(1, LoneRoute.events.size()); Assert.assertEquals(BeforeEnterEvent.class, LoneRoute.events.get(0).getClass()); }
@Test public void navigateWithinOneParent_oneAfterNavigationEventOneEventOnly() throws InvalidRouteConfigurationException { setNavigationTargets(AfterNavigationChild.class, AfterNavigationWithinSameParent.class, LoneRoute.class); router.navigate(ui, new Location("parent/after-navigation-child"), NavigationTrigger.PROGRAMMATIC); AfterNavigationChild.events.clear(); router.navigate(ui, new Location("parent/after-navigation-within-same-parent"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals( "After navigation event should not be fired for " + AfterNavigationChild.class.getSimpleName(), 0, AfterNavigationChild.events.size()); Assert.assertEquals( "Only one navigation event should be fired for " + AfterNavigationWithinSameParent.class.getSimpleName(), 1, AfterNavigationWithinSameParent.events.size()); Assert.assertEquals( "The fired event type should be " + AfterNavigationEvent.class.getSimpleName(), AfterNavigationEvent.class, AfterNavigationWithinSameParent.events.get(0).getClass()); }
@Test public void manually_registered_listeners_should_fire_for_every_navigation() throws InvalidRouteConfigurationException { setNavigationTargets(RootNavigationTarget.class, FooNavigationTarget.class, FooBarNavigationTarget.class); AtomicInteger leaveCount = new AtomicInteger(0); AtomicInteger enterCount = new AtomicInteger(0); AtomicInteger afterCount = new AtomicInteger(0); ui.addBeforeLeaveListener(event -> leaveCount.incrementAndGet()); ui.addBeforeEnterListener(event -> enterCount.incrementAndGet()); ui.addAfterNavigationListener(event -> afterCount.incrementAndGet()); Assert.assertEquals( "No event should have happened due to adding listener.", 0, leaveCount.get()); Assert.assertEquals( "No event should have happened due to adding listener.", 0, enterCount.get()); Assert.assertEquals( "No event should have happened due to adding listener.", 0, afterCount.get()); router.navigate(ui, new Location("foo/bar"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("BeforeLeaveListener should have been invoked.", 1, leaveCount.get()); Assert.assertEquals("BeforeEnterListener should have been invoked.", 1, enterCount.get()); Assert.assertEquals("AfterNavigationListener should have been invoked.", 1, afterCount.get()); router.navigate(ui, new Location("foo"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("BeforeLeaveListener should have been invoked.", 2, leaveCount.get()); Assert.assertEquals("BeforeEnterListener should have been invoked.", 2, enterCount.get()); Assert.assertEquals("AfterNavigationListener should have been invoked.", 2, afterCount.get()); }
@Test public void after_navigation_listener_is_only_invoked_once_for_redirect() throws InvalidRouteConfigurationException { setNavigationTargets(ReroutingNavigationTarget.class, FooBarNavigationTarget.class); AtomicInteger afterCount = new AtomicInteger(0); ui.addAfterNavigationListener(event -> afterCount.incrementAndGet()); router.navigate(ui, new Location("reroute"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals( "AfterNavigationListener should have been invoked only after redirect.", 1, afterCount.get()); }
@Test public void before_leave_listener_is_invoked_for_each_redirect() throws InvalidRouteConfigurationException { setNavigationTargets(ReroutingNavigationTarget.class, FooBarNavigationTarget.class); AtomicInteger leaveCount = new AtomicInteger(0); ui.addBeforeLeaveListener(event -> leaveCount.incrementAndGet()); router.navigate(ui, new Location("reroute"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals( "BeforeLeaveListener should have been invoked for initial navigation and redirect.", 2, leaveCount.get()); }
@Test public void before_enter_listener_is_invoked_for_each_redirect_when_redirecting_on_before_enter() throws InvalidRouteConfigurationException { setNavigationTargets(ReroutingNavigationTarget.class, FooBarNavigationTarget.class); AtomicInteger enterCount = new AtomicInteger(0); ui.addBeforeEnterListener(event -> enterCount.incrementAndGet()); router.navigate(ui, new Location("reroute"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals( "BeforeEnterListener should have been invoked for initial navigation and redirect.", 2, enterCount.get()); }
@Test public void before_enter_listener_is_invoked_once_and_before_leave_twice_when_redirecting_on_before_leave() throws InvalidRouteConfigurationException { ReroutingOnLeaveNavigationTarget.events.clear(); setNavigationTargets(ReroutingOnLeaveNavigationTarget.class, FooBarNavigationTarget.class, FooNavigationTarget.class); router.navigate(ui, new Location("reroute"), NavigationTrigger.PROGRAMMATIC); AtomicInteger leaveCount = new AtomicInteger(0); AtomicInteger enterCount = new AtomicInteger(0); ui.addBeforeLeaveListener(event -> leaveCount.incrementAndGet()); ui.addBeforeEnterListener(event -> enterCount.incrementAndGet()); router.navigate(ui, new Location("foo"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals( "BeforeLeaveListener should have been invoked for initial navigation and redirect.", 2, leaveCount.get()); Assert.assertEquals( "BeforeEnterListener should have been invoked for initial navigation and redirect.", 1, enterCount.get()); }
@Test public void manual_before_listeners_are_fired_before_observers() throws InvalidRouteConfigurationException { ManualNavigationTarget.events.clear(); setNavigationTargets(ManualNavigationTarget.class, FooNavigationTarget.class); Registration beforeEnter = ui.addBeforeEnterListener( event -> ManualNavigationTarget.events.add("Manual event")); router.navigate(ui, new Location("manual"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("not enough events", 2, ManualNavigationTarget.events.size()); Assert.assertEquals("Manual event", ManualNavigationTarget.events.get(0)); Assert.assertEquals("Before enter", ManualNavigationTarget.events.get(1)); beforeEnter.remove(); ui.addBeforeLeaveListener( event -> ManualNavigationTarget.events.add("Manual event")); router.navigate(ui, new Location("foo"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("not enough events", 4, ManualNavigationTarget.events.size()); Assert.assertEquals("Manual event", ManualNavigationTarget.events.get(2)); Assert.assertEquals("Before leave", ManualNavigationTarget.events.get(3)); }
@Test public void manual_after_listener_is_fired_before_observer() throws InvalidRouteConfigurationException { AfterNavigationTarget.events.clear(); setNavigationTargets(AfterNavigationTarget.class); ui.addAfterNavigationListener( event -> AfterNavigationTarget.events.add("Manual event")); router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("not enough events", 2, AfterNavigationTarget.events.size()); Assert.assertEquals("Manual event", AfterNavigationTarget.events.get(0)); Assert.assertEquals("AfterNavigation Observer", AfterNavigationTarget.events.get(1)); }
@Test public void navigating_with_class_gets_correct_component() throws InvalidRouteConfigurationException { setNavigationTargets(RootNavigationTarget.class, FooNavigationTarget.class, FooBarNavigationTarget.class); ui.navigate(RootNavigationTarget.class); Assert.assertEquals(RootNavigationTarget.class, getUIComponent()); ui.navigate(FooNavigationTarget.class); Assert.assertEquals(FooNavigationTarget.class, getUIComponent()); ui.navigate(FooBarNavigationTarget.class); Assert.assertEquals(FooBarNavigationTarget.class, getUIComponent()); }
@Test public void navigating_with_class_and_parameter_gets_correct_component() throws InvalidRouteConfigurationException { setNavigationTargets(RouteWithParameter.class, BooleanParameter.class, WildParameter.class, OptionalParameter.class); ui.navigate(RouteWithParameter.class, "Parameter"); Assert.assertEquals(RouteWithParameter.class, getUIComponent()); Assert.assertEquals("Before navigation event was wrong.", "Parameter", RouteWithParameter.param); ui.navigate(OptionalParameter.class, "optional"); Assert.assertEquals(OptionalParameter.class, getUIComponent()); Assert.assertEquals("Before navigation event was wrong.", "optional", OptionalParameter.param); ui.navigate(OptionalParameter.class); Assert.assertEquals(OptionalParameter.class, getUIComponent()); Assert.assertEquals("Before navigation event was wrong.", null, OptionalParameter.param); ui.navigate(OptionalParameter.class, (String) null); Assert.assertEquals(OptionalParameter.class, getUIComponent()); Assert.assertEquals("Before navigation event was wrong.", null, OptionalParameter.param); ui.navigate(BooleanParameter.class, false); Assert.assertEquals(BooleanParameter.class, getUIComponent()); Assert.assertEquals("Before navigation event was wrong.", false, BooleanParameter.param); ui.navigate(WildParameter.class); Assert.assertEquals(WildParameter.class, getUIComponent()); Assert.assertEquals("Before navigation event was wrong.", "", WildParameter.param); ui.navigate(WildParameter.class, (String) null); Assert.assertEquals(WildParameter.class, getUIComponent()); Assert.assertEquals("Before navigation event was wrong.", "", WildParameter.param); ui.navigate(WildParameter.class, ""); Assert.assertEquals(WildParameter.class, getUIComponent()); Assert.assertEquals("Before navigation event was wrong.", "", WildParameter.param); ui.navigate(WildParameter.class, "my/wild/param"); Assert.assertEquals(WildParameter.class, getUIComponent()); Assert.assertEquals("Before navigation event was wrong.", "my/wild/param", WildParameter.param); }
@Test public void exception_event_should_keep_original_trigger() { setErrorNavigationTargets(FileNotFound.class); int result = router.navigate(ui, new Location("programmatic"), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals("Non existent route should have returned.", HttpServletResponse.SC_NOT_FOUND, result); Assert.assertEquals(NavigationTrigger.PROGRAMMATIC, FileNotFound.trigger); JsonObject state = Json.createObject(); state.put("href", "router_link"); state.put("scrollPositionX", 0d); state.put("scrollPositionY", 0d); router.navigate(ui, new Location("router_link"), NavigationTrigger.ROUTER_LINK, state); Assert.assertEquals(NavigationTrigger.ROUTER_LINK, FileNotFound.trigger); router.navigate(ui, new Location("history"), NavigationTrigger.HISTORY); Assert.assertEquals(NavigationTrigger.HISTORY, FileNotFound.trigger); router.navigate(ui, new Location("page_load"), NavigationTrigger.PAGE_LOAD); Assert.assertEquals(NavigationTrigger.PAGE_LOAD, FileNotFound.trigger); }
@Test public void preserve_initial_ui_contents() throws InvalidRouteConfigurationException { setNavigationTargets(View.class); Element specialChild = new Element("div"); ui.getElement().appendChild(specialChild); router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals(ui.getElement(), specialChild.getParent()); }
@Test public void noRemoveLayout_oldContentRetained() { setNavigationTargets(NoRemoveContent1.class, NoRemoveContent2.class); ui.navigate(NoRemoveContent1.class); NoRemoveLayout layout = (NoRemoveLayout) ui.getChildren().findFirst() .get(); Assert.assertEquals(Arrays.asList(NoRemoveContent1.class), layout.getChildren().map(Component::getClass) .collect(Collectors.toList())); ui.navigate(NoRemoveContent2.class); Assert.assertEquals( Arrays.asList(NoRemoveContent1.class, NoRemoveContent2.class), layout.getChildren().map(Component::getClass) .collect(Collectors.toList())); }
@Test public void layout_chain_is_included_in_before_events() { setNavigationTargets(LoneRoute.class, RouteChildWithParameter.class); RouteChildWithParameter.events.clear(); ui.navigate(RouteChildWithParameter.class, "foobar"); BeforeEnterEvent beforeEnterEvent = (BeforeEnterEvent) RouteChildWithParameter.events .get(0); Assert.assertEquals( "There is not exactly one layout in the layout chain", 1, beforeEnterEvent.getLayouts().size()); Assert.assertTrue("RouteParent was not included in the layout chain", beforeEnterEvent.getLayouts().contains(RouteParent.class)); RouteChildWithParameter.events.clear(); ui.navigate(LoneRoute.class); BeforeLeaveEvent beforeLeaveEvent = (BeforeLeaveEvent) RouteChildWithParameter.events .get(0); Assert.assertEquals( "There is not exactly one layout in the layout chain", 1, beforeLeaveEvent.getLayouts().size()); Assert.assertTrue("RouteParent was not included in the layout chain", beforeLeaveEvent.getLayouts().contains(RouteParent.class)); }
@Test public void optional_parameter_non_existing_route() throws InvalidRouteConfigurationException { OptionalParameter.events.clear(); Mockito.when(configuration.isProductionMode()).thenReturn(false); setNavigationTargets(OptionalParameter.class); String locationString = "optional/doesnotExist/parameter"; router.navigate(ui, new Location(locationString), NavigationTrigger.PROGRAMMATIC); String exceptionText1 = String.format("Could not navigate to '%s'", locationString); String exceptionText2 = String .format("Couldn't find route for '%s'", locationString); String optionalTemplate = HasUrlParameterFormat .getTemplate("optional", OptionalParameter.class); String exceptionText3 = "<li>" + optionalTemplate + " (supports optional parameter)</li>"; assertExceptionComponent(RouteNotFoundError.class, exceptionText1, exceptionText2, exceptionText3); }
@Test public void without_optional_parameter() throws InvalidRouteConfigurationException { OptionalParameter.events.clear(); Mockito.when(configuration.isProductionMode()).thenReturn(false); setNavigationTargets(WithoutOptionalParameter.class); String locationString = "optional"; router.navigate(ui, new Location(locationString), NavigationTrigger.PROGRAMMATIC); String exceptionText1 = String.format("Could not navigate to '%s'", locationString); String exceptionText2 = String .format("Reason: Couldn't find route for '%s'", locationString); String template = HasUrlParameterFormat .getTemplate("optional", WithoutOptionalParameter.class); String exceptionText3 = "<li>" + template + " (requires parameter)</li>"; assertExceptionComponent(RouteNotFoundError.class, exceptionText1, exceptionText2, exceptionText3); }
@Test public void reroute_and_forward_from_parent_layout() { ProcessEventsBase.clear(); setNavigationTargets(SecurityDocument.class, SecurityLogin.class); final List<String> expectedInitially = Arrays.asList("SecurityParent", "SecurityParent", "SecurityLogin"); final List<String> expected = Arrays.asList("SecurityParent", "SecurityLogin"); router.navigate(ui, new Location("security/document"), NavigationTrigger.PROGRAMMATIC); assertEventOrder(expectedInitially, null, expectedInitially, expected); ProcessEventsBase.clear(); router.navigate(ui, new Location("security/login"), NavigationTrigger.PROGRAMMATIC); assertExistingChainEventOrder(expected); }
@Test public void event_listeners_are_invoked_starting_with_parent_component() throws InvalidRouteConfigurationException { ProcessEventsBase.clear(); setNavigationTargets(ProcessEventsFlower.class); router.navigate(ui, new Location("event/flower"), NavigationTrigger.PROGRAMMATIC); assertInitialChainEventOrder( getProcessEventsBranchChainNames("ProcessEventsFlower")); }
@Test public void event_listeners_are_invoked_starting_with_parent_component_when_preserved_on_refresh() throws InvalidRouteConfigurationException { ProcessEventsBase.clear(); ExtendedClientDetails previousClientDetails = ui.getInternals() .getExtendedClientDetails(); ExtendedClientDetails clientDetails = Mockito.mock(ExtendedClientDetails.class); ui.getInternals().setExtendedClientDetails(clientDetails); Mockito.when(clientDetails.getWindowName()).thenReturn("mock"); setNavigationTargets(ProcessEventsFruit.class); router.navigate(ui, new Location("event/fruit"), NavigationTrigger.PROGRAMMATIC); ProcessEventsBase.clear(); router.navigate(ui, new Location("event/fruit"), NavigationTrigger.PROGRAMMATIC); assertExistingChainEventOrder( getProcessEventsBranchChainNames("ProcessEventsFruit")); ui.getInternals().setExtendedClientDetails(previousClientDetails); }
@Test public void parent_layouts_are_reused_when_change_url() throws InvalidRouteConfigurationException { ProcessEventsBase.clear(); setNavigationTargets(ProcessEventsFlower.class, ProcessEventsLeaf.class); router.navigate(ui, new Location("event/flower"), NavigationTrigger.PROGRAMMATIC); ProcessEventsBase.clear(); final String parameter = "green"; router.navigate(ui, new Location("event/leaf/" + parameter), NavigationTrigger.PROGRAMMATIC); assertEventOrder(Arrays.asList("ProcessEventsLeaf", "leafChild"), getProcessEventsBranchChainNames("ProcessEventsFlower"), getProcessEventsBranchChainNames(parameter, "ProcessEventsLeaf", "leafChild"), getProcessEventsBranchChainNames("ProcessEventsLeaf", "leafChild")); }
@Test public void components_are_not_created_when_parent_layout_redirects() throws InvalidRouteConfigurationException { ProcessEventsBase.clear(); setNavigationTargets(ProcessEventsFlower.class, ProcessEventsTwig.class); router.navigate(ui, new Location("event/twig"), NavigationTrigger.PROGRAMMATIC); final List<String> expectedOnReroute = getProcessEventsBranchChainNames( "ProcessEventsFlower"); final List<String> expected = Stream .concat(getProcessEventsTrunkChainNames("ProcessEventsRotten") .stream(), expectedOnReroute.stream()) .collect(Collectors.toList()); assertEventOrder(expected, null, expected, expectedOnReroute); }
@Test public void url_parameter_is_invoked_right_before_enter_events() throws InvalidRouteConfigurationException { ProcessEventsBase.clear(); setNavigationTargets(ProcessEventsLeaf.class); final String parameter = "red"; router.navigate(ui, new Location("event/leaf/" + parameter), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals( "BeforeEnter events aren't triggered in correct order", getProcessEventsBranchChainNames(parameter, "ProcessEventsLeaf", "leafChild"), ProcessEventsBase.beforeEnter); }
@Test public void url_parameter_is_invoked_where_before_enter_is_not_observed() throws InvalidRouteConfigurationException { ProcessEventsBase.clear(); setNavigationTargets(ProcessEventsNeedle.class); final String parameter = "green"; router.navigate(ui, new Location("event/needle/" + parameter), NavigationTrigger.PROGRAMMATIC); Assert.assertEquals( "BeforeEnter events aren't triggered in correct order", getProcessEventsBranchChainNames(parameter, "needleChild"), ProcessEventsBase.beforeEnter); }
@Test public void forward_fromSetParameters_withoutBeforeEnterObserver() { ForwardSetParameterView.clear(); setNavigationTargets(ForwardSetParameterView.class, ForwardSetParameterBackView.class); navigate("forward/setParameter/test"); Assert.assertFalse( "afterNavigation must not be invoked after forwardTo in setParameter", ForwardSetParameterView.afterNavigationInvoked); Assert.assertTrue("forwardTo ForwardSetParameterBackView failed", ForwardSetParameterView.backBeforeEnterInvoked); }