|
46 | 46 | import com.vaadin.flow.router.BeforeLeaveEvent.ContinueNavigationAction; |
47 | 47 | import com.vaadin.flow.router.RouterTest.CombinedObserverTarget.Enter; |
48 | 48 | import com.vaadin.flow.router.RouterTest.CombinedObserverTarget.Leave; |
| 49 | +import com.vaadin.flow.router.internal.DefaultErrorHandler; |
49 | 50 | import com.vaadin.flow.router.internal.HasUrlParameterFormat; |
50 | 51 | import com.vaadin.flow.router.internal.RouteUtil; |
51 | 52 | import com.vaadin.flow.server.InvalidRouteConfigurationException; |
@@ -2327,24 +2328,65 @@ public void exception_during_navigation_is_caught_and_show_in_internalServerErro |
2327 | 2328 | } |
2328 | 2329 |
|
2329 | 2330 | @Test |
2330 | | - public void fail_for_multiple_of_the_same_class() |
| 2331 | + public void fail_for_multiple_classes_extending_the_same_exception_class() |
2331 | 2332 | throws InvalidRouteConfigurationException { |
2332 | | - setErrorNavigationTargets(ErrorTarget.class, RouteNotFoundError.class); |
| 2333 | + expectedEx.expect(InvalidRouteConfigurationException.class); |
| 2334 | + setErrorNavigationTargets(ErrorTarget.class, |
| 2335 | + CustomNotFoundTarget.class); |
| 2336 | + } |
2333 | 2337 |
|
2334 | | - int result = router.navigate(ui, new Location("exception"), |
| 2338 | + @Route("npe") |
| 2339 | + @Tag(Tag.DIV) |
| 2340 | + public static class NpeNavigationTarget extends Component { |
| 2341 | + public NpeNavigationTarget() { |
| 2342 | + throw new NullPointerException("Null was found"); |
| 2343 | + } |
| 2344 | + } |
| 2345 | + |
| 2346 | + @Tag(Tag.DIV) |
| 2347 | + @DefaultErrorHandler |
| 2348 | + public static class DefaultNullPointerException extends Component |
| 2349 | + implements HasErrorParameter<NullPointerException> { |
| 2350 | + |
| 2351 | + @Override |
| 2352 | + public int setErrorParameter(BeforeEnterEvent event, |
| 2353 | + ErrorParameter<NullPointerException> parameter) { |
| 2354 | + return HttpServletResponse.SC_UNAUTHORIZED; |
| 2355 | + } |
| 2356 | + } |
| 2357 | + |
| 2358 | + @Tag(Tag.DIV) |
| 2359 | + public static class NullPointerExceptionHandler extends Component |
| 2360 | + implements HasErrorParameter<NullPointerException> { |
| 2361 | + |
| 2362 | + @Override |
| 2363 | + public int setErrorParameter(BeforeEnterEvent event, |
| 2364 | + ErrorParameter<NullPointerException> parameter) { |
| 2365 | + return HttpServletResponse.SC_INTERNAL_SERVER_ERROR; |
| 2366 | + } |
| 2367 | + } |
| 2368 | + |
| 2369 | + @Test // spring #661 |
| 2370 | + public void pick_custom_from_multiple_error_targets_when_other_is_default_annotated() { |
| 2371 | + setNavigationTargets(NpeNavigationTarget.class); |
| 2372 | + setErrorNavigationTargets(DefaultNullPointerException.class, |
| 2373 | + NullPointerExceptionHandler.class); |
| 2374 | + |
| 2375 | + int result = router.navigate(ui, new Location("npe"), |
2335 | 2376 | NavigationTrigger.PROGRAMMATIC); |
2336 | | - Assert.assertEquals("Non existent route should have returned.", |
2337 | | - HttpServletResponse.SC_NOT_FOUND, result); |
| 2377 | + Assert.assertEquals( |
| 2378 | + "Null pointer should return the server error of the custom implementation.", |
| 2379 | + HttpServletResponse.SC_INTERNAL_SERVER_ERROR, result); |
2338 | 2380 |
|
2339 | 2381 | Assert.assertEquals( |
2340 | 2382 | "Expected the extending class to be used instead of the super class", |
2341 | | - ErrorTarget.class, getUIComponent()); |
| 2383 | + NullPointerExceptionHandler.class, getUIComponent()); |
2342 | 2384 | } |
2343 | 2385 |
|
2344 | 2386 | @Test |
2345 | 2387 | public void do_not_accept_same_exception_targets() { |
2346 | 2388 |
|
2347 | | - expectedEx.expect(InvalidRouteLayoutConfigurationException.class); |
| 2389 | + expectedEx.expect(InvalidRouteConfigurationException.class); |
2348 | 2390 | expectedEx.expectMessage(startsWith( |
2349 | 2391 | "Only one target for an exception should be defined. Found ")); |
2350 | 2392 |
|
|
0 commit comments