All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.mvysny.kaributesting.v10.spring.MockSpringServlet Maven / Gradle / Ivy

The newest version!
package com.github.mvysny.kaributesting.v10.spring;

import com.github.mvysny.kaributesting.v10.mock.MockVaadinHelper;
import com.github.mvysny.kaributesting.v10.Routes;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.function.DeploymentConfiguration;
import com.vaadin.flow.server.Constants;
import com.vaadin.flow.server.ServiceException;
import com.vaadin.flow.server.VaadinServletContext;
import com.vaadin.flow.server.VaadinServletService;
import com.vaadin.flow.server.frontend.FrontendUtils;
import com.vaadin.flow.spring.SpringServlet;
import kotlin.jvm.functions.Function0;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;

import java.io.File;

/**
 * Makes sure that the {@link #routes} are properly registered,
 * and that {@link MockSpringServletService} is used instead of vanilla {@link com.vaadin.flow.spring.SpringVaadinServletService}.
 * @author mavi
 */
public class MockSpringServlet extends SpringServlet {
    /**
     * The routes registered to the application.
     */
    @NotNull
    public final Routes routes;
    /**
     * The application context.
     */
    @NotNull
    public final ApplicationContext ctx;
    /**
     * Creates new UIs.
     */
    @NotNull
    public final Function0 uiFactory;

    /**
     * Creates new servlet.
     * @param routes The routes registered to the application.
     * @param ctx The application context.
     * @param uiFactory Creates new UIs.
     */
    public MockSpringServlet(@NotNull Routes routes, @NotNull ApplicationContext ctx, @NotNull Function0 uiFactory) {
        super(ctx, false);
        this.ctx = ctx;
        this.routes = routes;
        this.uiFactory = uiFactory;
    }

    @Override
    protected DeploymentConfiguration createDeploymentConfiguration() throws ServletException {
        MockVaadinHelper.mockFlowBuildInfo(this);
        // workaround for https://github.com/vaadin/flow/issues/18682
        System.setProperty(Constants.VAADIN_PREFIX + FrontendUtils.PARAM_FRONTEND_DIR, new File(FrontendUtils.DEFAULT_FRONTEND_DIR).getAbsolutePath());
        return super.createDeploymentConfiguration();
    }

    @Override
    protected VaadinServletService createServletService(DeploymentConfiguration deploymentConfiguration) throws ServiceException {
        final VaadinServletService service = new MockSpringServletService(this, deploymentConfiguration, ctx, uiFactory);
        service.init();
        routes.register((VaadinServletContext) service.getContext());
        return service;
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);
        // some apps retrieve Spring's WebApplicationContext from the ServletContext,
        // via WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE.
        servletConfig.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ctx);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy