Skip to content

Commit

Permalink
fix: find file-routes.json in dev bundle (#20324) (#20345)
Browse files Browse the repository at this point in the history
Fixes #20290

Co-authored-by: Teppo Kurki <[email protected]>
  • Loading branch information
vaadin-bot and tepi authored Oct 28, 2024
1 parent df7458a commit 6d21696
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
Expand All @@ -34,7 +32,6 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -56,6 +53,7 @@
import com.vaadin.flow.server.VaadinRequest;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.server.frontend.DevBundleUtils;
import com.vaadin.flow.server.menu.AvailableViewInfo;
import com.vaadin.flow.server.menu.RouteParamType;

Expand Down Expand Up @@ -471,10 +469,12 @@ public static URL getViewsJsonAsResource(
if (fileRoutes.toFile().exists()) {
return fileRoutes.toUri().toURL();
}
return null;
} catch (MalformedURLException e) {
return DevBundleUtils.findBundleFile(
configuration.getProjectFolder(),
configuration.getBuildFolder(), FILE_ROUTES_JSON_NAME);
} catch (IOException e) {
LoggerFactory.getLogger(MenuRegistry.class).warn(
"Failed to find {} under frontend/generated",
"Failed to find {} in frontend/generated or dev-bundle folder",
FILE_ROUTES_JSON_NAME, e);
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ public VaadinContext getContext() {
.mock(DeploymentConfiguration.class);
Mockito.when(config.isProductionMode()).thenReturn(false);
Mockito.when(config.getFrontendFolder()).thenReturn(new File("front"));
Mockito.when(config.getProjectFolder()).thenReturn(new File("./"));
Mockito.when(config.getBuildFolder()).thenReturn("build");

session.lock();
session.setConfiguration(config);
((MockVaadinServletService) service).setConfiguration(config);
CurrentInstance.set(VaadinSession.class, session);

ui.getInternals().setSession(session);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,12 @@ public VaadinContext getContext() {
.mock(DeploymentConfiguration.class);
Mockito.when(config.isProductionMode()).thenReturn(false);
Mockito.when(config.getFrontendFolder()).thenReturn(new File("front"));
Mockito.when(config.getProjectFolder()).thenReturn(new File("./"));
Mockito.when(config.getBuildFolder()).thenReturn("build");

session.lock();
session.setConfiguration(config);

((MockVaadinServletService) service).setConfiguration(config);
ui.getInternals().setSession(session);

RouteConfiguration routeConfiguration = RouteConfiguration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package com.vaadin.flow.component.internal;

import static com.vaadin.flow.component.UI.CLIENT_NAVIGATE_TO;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.Serializable;
import java.util.Collections;
import java.util.Optional;
Expand Down Expand Up @@ -33,6 +28,7 @@
import com.vaadin.flow.internal.CurrentInstance;
import com.vaadin.flow.internal.StateNode;
import com.vaadin.flow.internal.StateTree;
import com.vaadin.flow.internal.menu.MenuRegistry;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.BeforeEnterObserver;
import com.vaadin.flow.router.BeforeLeaveEvent;
Expand All @@ -46,9 +42,13 @@
import com.vaadin.flow.server.MockServletServiceSessionSetup;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.internal.menu.MenuRegistry;
import com.vaadin.tests.util.MockDeploymentConfiguration;

import static com.vaadin.flow.component.UI.CLIENT_NAVIGATE_TO;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class JavaScriptBootstrapUITest {

private static final String CLIENT_PUSHSTATE_TO = "setTimeout(() => { window.history.pushState($0, '', $1); window.dispatchEvent(new CustomEvent('vaadin-navigated')); })";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3770,6 +3770,9 @@ public void optional_parameter_non_existing_route()
Mockito.when(configuration.isProductionMode()).thenReturn(false);
Mockito.when(configuration.getFrontendFolder())
.thenReturn(new File("front"));
Mockito.when(configuration.getProjectFolder())
.thenReturn(new File("./"));
Mockito.when(configuration.getBuildFolder()).thenReturn("build");
setNavigationTargets(OptionalParameter.class);

String locationString = "optional/doesnotExist/parameter";
Expand Down Expand Up @@ -3799,6 +3802,9 @@ public void without_optional_parameter()
Mockito.when(configuration.isProductionMode()).thenReturn(false);
Mockito.when(configuration.getFrontendFolder())
.thenReturn(new File("front"));
Mockito.when(configuration.getProjectFolder())
.thenReturn(new File("./"));
Mockito.when(configuration.getBuildFolder()).thenReturn("build");
setNavigationTargets(WithoutOptionalParameter.class);

String locationString = "optional";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
package com.vaadin.flow.server;

import com.vaadin.flow.di.Instantiator;
import com.vaadin.flow.di.Lookup;
import com.vaadin.flow.di.ResourceProvider;
import com.vaadin.flow.function.DeploymentConfiguration;
import com.vaadin.flow.internal.CurrentInstance;
import com.vaadin.flow.internal.ResponseWriterTest.CapturingServletOutputStream;
import com.vaadin.flow.router.DefaultRoutePathProvider;
import com.vaadin.flow.router.QueryParameters;
import com.vaadin.flow.router.RoutePathProvider;
import com.vaadin.flow.router.Router;
import com.vaadin.flow.router.TestRouteRegistry;
import com.vaadin.flow.server.AppShellRegistry.AppShellRegistryWrapper;
import com.vaadin.flow.server.communication.IndexHtmlRequestListener;
import com.vaadin.flow.server.communication.IndexHtmlResponse;
import com.vaadin.flow.shared.ApplicationConstants;
import com.vaadin.tests.util.MockDeploymentConfiguration;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
Expand All @@ -42,6 +23,27 @@
import java.util.concurrent.LinkedBlockingDeque;
import java.util.function.Supplier;

import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import com.vaadin.flow.di.Instantiator;
import com.vaadin.flow.di.Lookup;
import com.vaadin.flow.di.ResourceProvider;
import com.vaadin.flow.function.DeploymentConfiguration;
import com.vaadin.flow.internal.CurrentInstance;
import com.vaadin.flow.internal.ResponseWriterTest.CapturingServletOutputStream;
import com.vaadin.flow.router.DefaultRoutePathProvider;
import com.vaadin.flow.router.QueryParameters;
import com.vaadin.flow.router.RoutePathProvider;
import com.vaadin.flow.router.Router;
import com.vaadin.flow.router.TestRouteRegistry;
import com.vaadin.flow.server.AppShellRegistry.AppShellRegistryWrapper;
import com.vaadin.flow.server.communication.IndexHtmlRequestListener;
import com.vaadin.flow.server.communication.IndexHtmlResponse;
import com.vaadin.flow.shared.ApplicationConstants;
import com.vaadin.tests.util.MockDeploymentConfiguration;

public class MockServletServiceSessionSetup {

public class TestVaadinServletService extends VaadinServletService {
Expand Down Expand Up @@ -321,6 +323,7 @@ public MockServletServiceSessionSetup(boolean sessionAvailable)
servlet = createVaadinServlet();

deploymentConfiguration.setXsrfProtectionEnabled(false);
deploymentConfiguration.setProjectFolder(new File("./"));
Mockito.doAnswer(
invocation -> servletContext.getClass().getClassLoader())
.when(servletContext).getClassLoader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ public void init() {

Mockito.when(deploymentConfiguration.getFrontendFolder())
.thenReturn(tmpDir.getRoot());
Mockito.when(deploymentConfiguration.getProjectFolder())
.thenReturn(tmpDir.getRoot());
Mockito.when(deploymentConfiguration.getBuildFolder())
.thenReturn("build");

VaadinService.setCurrent(vaadinService);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public void init() {

Mockito.when(deploymentConfiguration.getFrontendFolder())
.thenReturn(tmpDir.getRoot());
Mockito.when(deploymentConfiguration.getProjectFolder())
.thenReturn(tmpDir.getRoot());
Mockito.when(deploymentConfiguration.getBuildFolder())
.thenReturn("build");

frontendUtils = Mockito.mockStatic(FrontendUtils.class);
frontendUtils.when(() -> FrontendUtils.isHillaUsed(Mockito.any()))
Expand Down

0 comments on commit 6d21696

Please sign in to comment.