Skip to content

Commit

Permalink
move error msgs to constants
Browse files Browse the repository at this point in the history
  • Loading branch information
tepi committed Jan 15, 2025
1 parent a4fc03e commit 1a2668b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -791,9 +791,9 @@ private RuntimeException ambigousTarget(Class<? extends Component> target) {

String messageFormat;
if (isParameter()) {
messageFormat = "Navigation target paths (considering @Route, @RouteAlias and @RoutePrefix values) must be unique, found navigation targets '%s' and '%s' with parameter having the same route.";
messageFormat = RouteUtil.ROUTE_CONFLICT_WITH_PARAMS;
} else {
messageFormat = "Navigation target paths (considering @Route, @RouteAlias and @RoutePrefix values) must be unique, found navigation targets '%s' and '%s' with the same route.";
messageFormat = RouteUtil.ROUTE_CONFLICT;
}

String message = String.format(messageFormat,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
*/
public class RouteUtil {

private static final String ROUTE_CONFLICT_PREFIX = "Navigation target paths (considering @Route, @RouteAlias and @RoutePrefix values) must be unique, found navigation targets '%s' and '%s' ";
public static final String ROUTE_CONFLICT = ROUTE_CONFLICT_PREFIX
+ "having the same route.";
public static final String ROUTE_CONFLICT_WITH_PARAMS = ROUTE_CONFLICT_PREFIX
+ "with parameter having the same route.";

protected RouteUtil() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@
import java.util.List;
import java.util.concurrent.CountDownLatch;

import com.vaadin.flow.router.BeforeEvent;
import com.vaadin.flow.router.HasUrlParameter;
import com.vaadin.flow.router.NotFoundException;
import com.vaadin.flow.router.OptionalParameter;
import com.vaadin.flow.router.RouteParameterRegex;
import com.vaadin.flow.router.WildcardParameter;
import com.vaadin.flow.server.InvalidRouteConfigurationException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.router.BeforeEvent;
import com.vaadin.flow.router.HasUrlParameter;
import com.vaadin.flow.router.Layout;
import com.vaadin.flow.router.NotFoundException;
import com.vaadin.flow.router.OptionalParameter;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouteBaseData;
import com.vaadin.flow.router.RouteParameterRegex;
import com.vaadin.flow.router.RouterLayout;
import com.vaadin.flow.router.RoutesChangedEvent;
import com.vaadin.flow.router.Layout;
import com.vaadin.flow.router.WildcardParameter;
import com.vaadin.flow.server.InvalidRouteConfigurationException;
import com.vaadin.flow.shared.Registration;
import org.junit.rules.ExpectedException;

public class AbstractRouteRegistryTest {

Expand Down Expand Up @@ -620,8 +620,7 @@ private void assertHasUrlAndWildcard() {
public void multiple_normal_routes_throw_exception()
throws InvalidRouteConfigurationException {
expectedEx.expect(InvalidRouteConfigurationException.class);
expectedEx.expectMessage(String.format(
"Navigation targets must have unique routes, found navigation targets '%s' and '%s' with the same route.",
expectedEx.expectMessage(String.format(RouteUtil.ROUTE_CONFLICT,
NormalRoute.class.getName(),
SecondNormalRoute.class.getName()));

Expand All @@ -648,10 +647,10 @@ public void normal_and_optional_throws_exception()
public void two_optionals_throw_exception()
throws InvalidRouteConfigurationException {
expectedEx.expect(InvalidRouteConfigurationException.class);
expectedEx.expectMessage(String.format(
"Navigation targets must have unique routes, found navigation targets '%s' and '%s' with parameter have the same route.",
OptionalRoute.class.getName(),
SecondOptionalRoute.class.getName()));
expectedEx.expectMessage(
String.format(RouteUtil.ROUTE_CONFLICT_WITH_PARAMS,
OptionalRoute.class.getName(),
SecondOptionalRoute.class.getName()));

addTarget(OptionalRoute.class);
addTarget(SecondOptionalRoute.class);
Expand All @@ -675,10 +674,10 @@ public void optional_and_normal_throws_exception()
public void two_has_route_parameters_throw_exception()
throws InvalidRouteConfigurationException {
expectedEx.expect(InvalidRouteConfigurationException.class);
expectedEx.expectMessage(String.format(
"Navigation targets must have unique routes, found navigation targets '%s' and '%s' with parameter have the same route.",
HasUrlRoute.class.getName(),
SecondHasUrlRoute.class.getName()));
expectedEx.expectMessage(
String.format(RouteUtil.ROUTE_CONFLICT_WITH_PARAMS,
HasUrlRoute.class.getName(),
SecondHasUrlRoute.class.getName()));

addTarget(HasUrlRoute.class);
addTarget(SecondHasUrlRoute.class);
Expand All @@ -689,10 +688,10 @@ public void two_has_route_parameters_throw_exception()
public void two_wildcard_parameters_throw_exception()
throws InvalidRouteConfigurationException {
expectedEx.expect(InvalidRouteConfigurationException.class);
expectedEx.expectMessage(String.format(
"Navigation targets must have unique routes, found navigation targets '%s' and '%s' with parameter have the same route.",
WildcardRoute.class.getName(),
SecondWildcardRoute.class.getName()));
expectedEx.expectMessage(
String.format(RouteUtil.ROUTE_CONFLICT_WITH_PARAMS,
WildcardRoute.class.getName(),
SecondWildcardRoute.class.getName()));

addTarget(WildcardRoute.class);
addTarget(SecondWildcardRoute.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.vaadin.flow.router.internal;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.router.BeforeEnterEvent;
Expand All @@ -8,10 +13,6 @@
import com.vaadin.flow.router.HasErrorParameter;
import com.vaadin.flow.router.HasUrlParameter;
import com.vaadin.flow.server.AmbiguousRouteConfigurationException;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class ConfigureRoutesTest {

Expand Down Expand Up @@ -109,8 +110,9 @@ public void duplicateRootPathRegistration_throwsException() {
exceptionRule.expect(AmbiguousRouteConfigurationException.class);
exceptionRule.reportMissingExceptionWithMessage(
"Duplicate routes shouldn't be accepted.");
exceptionRule.expectMessage(
"Navigation targets must have unique routes, found navigation targets 'com.vaadin.flow.router.internal.ConfigureRoutesTest$BaseTarget' and 'com.vaadin.flow.router.internal.ConfigureRoutesTest$BaseTarget' with the same route.");
exceptionRule.expectMessage(String.format(RouteUtil.ROUTE_CONFLICT,
"com.vaadin.flow.router.internal.ConfigureRoutesTest$BaseTarget",
"com.vaadin.flow.router.internal.ConfigureRoutesTest$BaseTarget"));
mutable.setRoute("", BaseTarget.class);
}

Expand All @@ -126,8 +128,10 @@ public void duplicateParameterPathRegistration_throwsException() {
exceptionRule.expect(AmbiguousRouteConfigurationException.class);
exceptionRule.reportMissingExceptionWithMessage(
"Duplicate parameter routes shouldn't be accepted.");
exceptionRule.expectMessage(
"Navigation targets must have unique routes, found navigation targets 'com.vaadin.flow.router.internal.ConfigureRoutesTest$ParamTarget' and 'com.vaadin.flow.router.internal.ConfigureRoutesTest$ParamTarget' with parameter have the same route.");
exceptionRule.expectMessage(String.format(
RouteUtil.ROUTE_CONFLICT_WITH_PARAMS,
"com.vaadin.flow.router.internal.ConfigureRoutesTest$ParamTarget",
"com.vaadin.flow.router.internal.ConfigureRoutesTest$ParamTarget"));
mutable.setRoute(":param", ParamTarget.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.vaadin.flow.router.RouterLayout;
import com.vaadin.flow.router.RoutesChangedEvent;
import com.vaadin.flow.router.internal.HasUrlParameterFormat;
import com.vaadin.flow.router.internal.RouteUtil;
import com.vaadin.flow.server.startup.ApplicationConfiguration;
import com.vaadin.flow.server.startup.ApplicationRouteRegistry;
import com.vaadin.flow.shared.Registration;
Expand Down Expand Up @@ -488,8 +489,7 @@ public void setSameRouteValueFromDifferentThreads_ConcurrencyTest()
Assert.assertEquals(
"Expected 4 route already exists exceptions due to route target validation",
THREADS - 1, exceptions.size());
String expected = String.format(
"Navigation targets must have unique routes, found navigation targets '%s' and '%s' with the same route.",
String expected = String.format(RouteUtil.ROUTE_CONFLICT,
MyRoute.class.getName(), MyRoute.class.getName());
for (String exception : exceptions) {
Assert.assertEquals(expected, exception);
Expand Down Expand Up @@ -540,8 +540,7 @@ public void useRouteResolutionFromDifferentThreads_ConcurrencyTest()
Assert.assertEquals(
"Expected 4 route already exists exceptions due to route target validation",
THREADS - 1, exceptions.size());
String expected = String.format(
"Navigation targets must have unique routes, found navigation targets '%s' and '%s' with the same route.",
String expected = String.format(RouteUtil.ROUTE_CONFLICT,
MyRoute.class.getName(), MyRoute.class.getName());
for (String exception : exceptions) {
Assert.assertEquals(expected, exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import com.vaadin.flow.router.internal.ErrorTargetEntry;
import com.vaadin.flow.router.internal.HasUrlParameterFormat;
import com.vaadin.flow.router.internal.PathUtil;
import com.vaadin.flow.router.internal.RouteUtil;
import com.vaadin.flow.server.InvalidRouteConfigurationException;
import com.vaadin.flow.server.InvalidRouteLayoutConfigurationException;
import com.vaadin.flow.server.MockVaadinContext;
Expand Down Expand Up @@ -169,10 +170,9 @@ public void process_duplicate_routesViaAlias_throws()
@Test
public void routeRegistry_fails_for_multiple_registration_of_same_route() {
expectedEx.expect(InvalidRouteConfigurationException.class);
expectedEx.expectMessage(
"Navigation targets must have unique routes, found navigation targets "
+ "'com.vaadin.flow.server.startup.RouteRegistryInitializerTest$NavigationTargetFoo' and "
+ "'com.vaadin.flow.server.startup.RouteRegistryInitializerTest$NavigationTargetFoo2' with the same route.");
expectedEx.expectMessage(String.format(RouteUtil.ROUTE_CONFLICT,
"com.vaadin.flow.server.startup.RouteRegistryInitializerTest$NavigationTargetFoo",
"com.vaadin.flow.server.startup.RouteRegistryInitializerTest$NavigationTargetFoo2"));

RouteConfiguration.forRegistry(registry)
.setAnnotatedRoute(NavigationTargetFoo.class);
Expand Down

0 comments on commit 1a2668b

Please sign in to comment.