Search in sources :

Example 6 with DefaultDeploymentConfiguration

use of com.vaadin.flow.server.DefaultDeploymentConfiguration in project flow by vaadin.

the class AbstractDnDUnitTest method setup.

@Before
public void setup() {
    ApplicationConfiguration appConfig = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(appConfig.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
    Mockito.when(appConfig.getBuildFolder()).thenReturn(".");
    VaadinContext context = Mockito.mock(VaadinContext.class);
    Mockito.when(appConfig.getContext()).thenReturn(context);
    Lookup lookup = Mockito.mock(Lookup.class);
    Mockito.when(context.getAttribute(Lookup.class)).thenReturn(lookup);
    DefaultDeploymentConfiguration configuration = new DefaultDeploymentConfiguration(appConfig, VaadinServlet.class, new Properties());
    VaadinService service = Mockito.mock(VaadinService.class);
    Mockito.when(service.resolveResource(Mockito.anyString())).thenReturn("");
    VaadinSession session = Mockito.mock(VaadinSession.class);
    Mockito.when(session.getConfiguration()).thenReturn(configuration);
    Mockito.when(session.getService()).thenReturn(service);
    ui = new MockUI(session);
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) VaadinSession(com.vaadin.flow.server.VaadinSession) VaadinService(com.vaadin.flow.server.VaadinService) Lookup(com.vaadin.flow.di.Lookup) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration) Properties(java.util.Properties) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) Before(org.junit.Before)

Example 7 with DefaultDeploymentConfiguration

use of com.vaadin.flow.server.DefaultDeploymentConfiguration in project flow by vaadin.

the class WebComponentProviderTest method webComponentGenerator_responseGetsResult.

@Test
public void webComponentGenerator_responseGetsResult() throws IOException {
    registry = setupConfigurations(MyComponentExporter.class);
    ByteArrayOutputStream out = Mockito.mock(ByteArrayOutputStream.class);
    DefaultDeploymentConfiguration configuration = Mockito.mock(DefaultDeploymentConfiguration.class);
    Mockito.when(response.getOutputStream()).thenReturn(out);
    Mockito.when(session.getConfiguration()).thenReturn(configuration);
    Mockito.when(request.getPathInfo()).thenReturn("/web-component/my-component.js");
    Assert.assertTrue("Provider should handle web-component request", provider.synchronizedHandleRequest(session, request, response));
    Mockito.verify(response).getOutputStream();
    Mockito.verify(out).write(Mockito.any());
}
Also used : DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 8 with DefaultDeploymentConfiguration

use of com.vaadin.flow.server.DefaultDeploymentConfiguration in project flow by vaadin.

the class ApplicationRunnerServlet method findDeploymentConfiguration.

private DeploymentConfiguration findDeploymentConfiguration(DeploymentConfiguration originalConfiguration) throws Exception {
    // First level of cache
    DeploymentConfiguration configuration = CurrentInstance.get(DeploymentConfiguration.class);
    if (configuration == null) {
        // Not in cache, try to find a VaadinSession to get it from
        VaadinSession session = VaadinSession.getCurrent();
        if (session == null) {
            /*
                 * There's no current session, request or response when serving
                 * static resources, but there's still the current request
                 * maintained by ApplicationRunnerServlet, and there's most
                 * likely also a HttpSession containing a VaadinSession for that
                 * request.
                 */
            HttpServletRequest currentRequest = VaadinServletService.getCurrentServletRequest();
            if (currentRequest != null) {
                HttpSession httpSession = currentRequest.getSession(false);
                if (httpSession != null) {
                    Map<Class<?>, CurrentInstance> oldCurrent = CurrentInstance.setCurrent((VaadinSession) null);
                    try {
                        VaadinServletService service = (VaadinServletService) VaadinService.getCurrent();
                        session = service.findVaadinSession(new VaadinServletRequest(currentRequest, service));
                    } finally {
                        /*
                             * Clear some state set by findVaadinSession to
                             * avoid accidentally depending on it when coding on
                             * e.g. static request handling.
                             */
                        CurrentInstance.restoreInstances(oldCurrent);
                        currentRequest.removeAttribute(VaadinSession.class.getName());
                    }
                }
            }
        }
        if (session != null) {
            String name = ApplicationRunnerServlet.class.getName() + ".deploymentConfiguration";
            try {
                session.getLockInstance().lock();
                /*
                     * Read attribute using reflection to bypass
                     * VaadinSesison.getAttribute which would cause an infinite
                     * loop when checking the production mode setting for
                     * determining whether to check that the session is locked.
                     */
                Field attributesField = VaadinSession.class.getDeclaredField("attributes");
                attributesField.setAccessible(true);
                Attributes sessionAttributes = (Attributes) attributesField.get(session);
                configuration = (DeploymentConfiguration) sessionAttributes.getAttribute(name);
                if (configuration == null) {
                    ApplicationRunnerServlet servlet = (ApplicationRunnerServlet) VaadinServlet.getCurrent();
                    Class<?> classToRun;
                    try {
                        classToRun = servlet.getClassToRun();
                    } catch (ClassNotFoundException e) {
                        /*
                             * This happens e.g. if the UI class defined in the
                             * URL is not found or if this servlet just serves
                             * static resources while there's some other servlet
                             * that serves the UI (e.g. when using /run-push/).
                             */
                        return originalConfiguration;
                    }
                    CustomDeploymentConfiguration customDeploymentConfiguration = classToRun.getAnnotation(CustomDeploymentConfiguration.class);
                    if (customDeploymentConfiguration != null) {
                        Properties initParameters = new Properties(originalConfiguration.getInitParameters());
                        for (Conf entry : customDeploymentConfiguration.value()) {
                            initParameters.put(entry.name(), entry.value());
                        }
                        initParameters.put(VaadinSession.UI_PARAMETER, getApplicationRunnerApplicationClassName(request.get()));
                        configuration = new DefaultDeploymentConfiguration(ApplicationConfiguration.get(getService().getContext()), servlet.getClass(), initParameters);
                    } else {
                        configuration = originalConfiguration;
                    }
                    sessionAttributes.setAttribute(name, configuration);
                }
            } finally {
                session.getLockInstance().unlock();
            }
            CurrentInstance.set(DeploymentConfiguration.class, configuration);
        } else {
            configuration = originalConfiguration;
        }
    }
    return configuration;
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) Conf(com.vaadin.flow.uitest.servlet.CustomDeploymentConfiguration.Conf) HttpSession(javax.servlet.http.HttpSession) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Attributes(com.vaadin.flow.server.Attributes) VaadinServletService(com.vaadin.flow.server.VaadinServletService) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration) Properties(java.util.Properties) HttpServletRequest(javax.servlet.http.HttpServletRequest) Field(java.lang.reflect.Field) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration)

Example 9 with DefaultDeploymentConfiguration

use of com.vaadin.flow.server.DefaultDeploymentConfiguration in project flow by vaadin.

the class AbstractScopeTest method mockSession.

@SuppressWarnings("unchecked")
protected VaadinSession mockSession() {
    SpringVaadinSession session = Mockito.mock(TestSession.class, Mockito.withSettings().useConstructor());
    doCallRealMethod().when(session).setAttribute(Mockito.any(Class.class), Mockito.any());
    doCallRealMethod().when(session).getAttribute(Mockito.any(Class.class));
    doCallRealMethod().when(session).getAttribute(Mockito.any(String.class));
    doCallRealMethod().when(session).addUI(Mockito.any());
    doCallRealMethod().when(session).removeUI(Mockito.any());
    doCallRealMethod().when(session).getService();
    doCallRealMethod().when(session).getUIs();
    when(session.getState()).thenReturn(VaadinSessionState.OPEN);
    final Properties initParameters = new Properties();
    ApplicationConfiguration appConfig = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(appConfig.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
    VaadinContext context = Mockito.mock(VaadinContext.class);
    Lookup lookup = Mockito.mock(Lookup.class);
    Mockito.when(context.getAttribute(Lookup.class)).thenReturn(lookup);
    Mockito.when(appConfig.getContext()).thenReturn(context);
    DefaultDeploymentConfiguration config = new DefaultDeploymentConfiguration(appConfig, getClass(), initParameters);
    when(session.getConfiguration()).thenReturn(config);
    VaadinSession.setCurrent(session);
    when(session.hasLock()).thenReturn(true);
    // keep a reference to the session so that it cannot be GCed.
    this.session = session;
    return session;
}
Also used : SpringVaadinSession(com.vaadin.flow.spring.SpringVaadinSession) VaadinContext(com.vaadin.flow.server.VaadinContext) Lookup(com.vaadin.flow.di.Lookup) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration) Properties(java.util.Properties) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration)

Example 10 with DefaultDeploymentConfiguration

use of com.vaadin.flow.server.DefaultDeploymentConfiguration in project flow by vaadin.

the class SpringClassesSerializableTest method createStore.

private TestBeanStore createStore() {
    final Properties initParameters = new Properties();
    ApplicationConfiguration appConfig = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(appConfig.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
    VaadinContext context = Mockito.mock(VaadinContext.class);
    Mockito.when(context.getAttribute(Mockito.eq(ApplicationConfiguration.class), Mockito.any())).thenReturn(appConfig);
    Mockito.when(appConfig.getContext()).thenReturn(context);
    Lookup lookup = Mockito.mock(Lookup.class);
    Mockito.when(context.getAttribute(Lookup.class)).thenReturn(lookup);
    VaadinService service = new VaadinServletService(new VaadinServlet(), new DefaultDeploymentConfiguration(appConfig, getClass(), initParameters)) {

        @Override
        public VaadinContext getContext() {
            return context;
        }
    };
    VaadinSession session = new TestSession(service);
    TestBeanStore store = new TestBeanStore(session);
    return store;
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) TestBeanStore(com.vaadin.flow.spring.scopes.TestBeanStore) VaadinSession(com.vaadin.flow.server.VaadinSession) VaadinService(com.vaadin.flow.server.VaadinService) VaadinServlet(com.vaadin.flow.server.VaadinServlet) VaadinServletService(com.vaadin.flow.server.VaadinServletService) Lookup(com.vaadin.flow.di.Lookup) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration) Properties(java.util.Properties) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration)

Aggregations

DefaultDeploymentConfiguration (com.vaadin.flow.server.DefaultDeploymentConfiguration)10 Properties (java.util.Properties)8 ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)6 VaadinContext (com.vaadin.flow.server.VaadinContext)5 VaadinServletService (com.vaadin.flow.server.VaadinServletService)5 Lookup (com.vaadin.flow.di.Lookup)4 VaadinService (com.vaadin.flow.server.VaadinService)4 VaadinSession (com.vaadin.flow.server.VaadinSession)4 Test (org.junit.Test)4 Before (org.junit.Before)3 MockVaadinContext (com.vaadin.flow.server.MockVaadinContext)2 VaadinServlet (com.vaadin.flow.server.VaadinServlet)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ServletContext (javax.servlet.ServletContext)2 UI (com.vaadin.flow.component.UI)1 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)1 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)1 Router (com.vaadin.flow.router.Router)1 Attributes (com.vaadin.flow.server.Attributes)1 DependencyFilter (com.vaadin.flow.server.DependencyFilter)1