Search in sources :

Example 1 with ResourceProvider

use of com.vaadin.flow.di.ResourceProvider in project flow by vaadin.

the class LookupServletContainerInitializerTest method processLookupServletContainerInitializer_resourceProviderIsProvidedAsScannedClass_lookupReturnsTheProviderInstance.

@Test
public void processLookupServletContainerInitializer_resourceProviderIsProvidedAsScannedClass_lookupReturnsTheProviderInstance() throws ServletException {
    Lookup lookup = mockLookup(TestResourceProvider.class);
    ResourceProvider provider = lookup.lookup(ResourceProvider.class);
    Assert.assertNotNull(provider);
    Assert.assertEquals(TestResourceProvider.class, provider.getClass());
    Collection<ResourceProvider> allProviders = lookup.lookupAll(ResourceProvider.class);
    Assert.assertEquals(1, allProviders.size());
    Assert.assertEquals(TestResourceProvider.class, allProviders.iterator().next().getClass());
}
Also used : ResourceProvider(com.vaadin.flow.di.ResourceProvider) TestResourceProvider(com.vaadin.flow.server.startup.testdata.TestResourceProvider) Lookup(com.vaadin.flow.di.Lookup) Test(org.junit.Test)

Example 2 with ResourceProvider

use of com.vaadin.flow.di.ResourceProvider in project flow by vaadin.

the class DefaultApplicationConfigurationFactoryTest method create_propertiesAreReadFromContext.

@Test
public void create_propertiesAreReadFromContext() throws IOException {
    VaadinContext context = Mockito.mock(VaadinContext.class);
    VaadinConfig config = Mockito.mock(VaadinConfig.class);
    ResourceProvider resourceProvider = mockResourceProvider(config, context);
    Mockito.when(context.getContextParameterNames()).thenReturn(Collections.enumeration(Collections.singleton("foo")));
    Mockito.when(context.getContextParameter("foo")).thenReturn("bar");
    mockClassPathTokenFile(resourceProvider, "{}");
    DefaultApplicationConfigurationFactory factory = new DefaultApplicationConfigurationFactory();
    ApplicationConfiguration configuration = factory.create(context);
    List<String> propertyNames = Collections.list(configuration.getPropertyNames());
    Assert.assertEquals(1, propertyNames.size());
    Assert.assertEquals("foo", propertyNames.get(0));
    Assert.assertEquals("bar", configuration.getStringProperty("foo", null));
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) VaadinConfig(com.vaadin.flow.server.VaadinConfig) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Test(org.junit.Test)

Example 3 with ResourceProvider

use of com.vaadin.flow.di.ResourceProvider in project flow by vaadin.

the class AbstractDevModeTest method setup.

@Before
public void setup() throws Exception {
    baseDir = temporaryFolder.getRoot().getPath();
    npmFolder = temporaryFolder.getRoot();
    Boolean enablePnpm = Boolean.TRUE;
    appConfig = Mockito.mock(ApplicationConfiguration.class);
    servletContext = Mockito.mock(ServletContext.class);
    Mockito.when(servletContext.getAttribute(ApplicationConfiguration.class.getName())).thenReturn(appConfig);
    Mockito.when(servletContext.getClassLoader()).thenReturn(servletContext.getClass().getClassLoader());
    vaadinContext = new VaadinServletContext(servletContext);
    mockApplicationConfiguration(appConfig, enablePnpm);
    lookup = Mockito.mock(Lookup.class);
    Mockito.when(servletContext.getAttribute(Lookup.class.getName())).thenReturn(lookup);
    ResourceProvider resourceProvider = Mockito.mock(ResourceProvider.class);
    Mockito.when(lookup.lookup(ResourceProvider.class)).thenReturn(resourceProvider);
    devModeHandlerManager = new DevModeHandlerManagerImpl();
    Mockito.when(lookup.lookup(DevModeHandlerManager.class)).thenReturn(devModeHandlerManager);
    configuration = new MockDeploymentConfiguration();
    Mockito.when(lookup.lookup(DeploymentConfiguration.class)).thenReturn(configuration);
    Mockito.when(lookup.lookup(ApplicationConfiguration.class)).thenReturn(appConfig);
    Mockito.when(lookup.lookup(StaticFileHandlerFactory.class)).thenReturn(service -> new StaticFileServer(service));
    vaadinService = Mockito.mock(VaadinService.class);
    Mockito.when(vaadinService.getContext()).thenReturn(vaadinContext);
    Mockito.when(vaadinService.getDeploymentConfiguration()).thenReturn(configuration);
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) StaticFileServer(com.vaadin.flow.server.StaticFileServer) ResourceProvider(com.vaadin.flow.di.ResourceProvider) DevModeHandlerManagerImpl(com.vaadin.base.devserver.DevModeHandlerManagerImpl) MockDeploymentConfiguration(com.vaadin.base.devserver.MockDeploymentConfiguration) VaadinService(com.vaadin.flow.server.VaadinService) VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) ServletContext(javax.servlet.ServletContext) Lookup(com.vaadin.flow.di.Lookup) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) Before(org.junit.Before)

Example 4 with ResourceProvider

use of com.vaadin.flow.di.ResourceProvider in project flow by vaadin.

the class SpringInstantiatorTest method getService.

public static VaadinService getService(ApplicationContext context, Properties configProperties, boolean rootMapping) throws ServletException {
    SpringServlet servlet = new SpringServlet(context, rootMapping) {

        @Override
        protected DeploymentConfiguration createDeploymentConfiguration(Properties initParameters) {
            if (configProperties != null) {
                configProperties.putAll(initParameters);
                return super.createDeploymentConfiguration(configProperties);
            }
            return super.createDeploymentConfiguration(initParameters);
        }
    };
    ServletConfig config = Mockito.mock(ServletConfig.class);
    ServletContext servletContext = Mockito.mock(ServletContext.class);
    Mockito.when(servletContext.getClassLoader()).thenReturn(servlet.getClass().getClassLoader());
    ApplicationConfiguration appConfig = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(appConfig.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
    Mockito.when(appConfig.getContext()).thenReturn(new VaadinServletContext(servletContext));
    Mockito.when(servletContext.getAttribute(ApplicationConfiguration.class.getName())).thenReturn(appConfig);
    Lookup lookup = Mockito.mock(Lookup.class);
    ResourceProvider provider = Mockito.mock(ResourceProvider.class);
    Mockito.when(lookup.lookup(ResourceProvider.class)).thenReturn(provider);
    StaticFileHandlerFactory staticFileHandlerFactory = vaadinService -> new StaticFileServer((VaadinServletService) vaadinService);
    Mockito.when(lookup.lookup(StaticFileHandlerFactory.class)).thenReturn(staticFileHandlerFactory);
    Mockito.when(servletContext.getAttribute(Lookup.class.getName())).thenReturn(lookup);
    Mockito.when(config.getServletContext()).thenReturn(servletContext);
    Mockito.when(config.getInitParameterNames()).thenReturn(Collections.emptyEnumeration());
    Mockito.when(servletContext.getInitParameterNames()).thenReturn(Collections.emptyEnumeration());
    servlet.init(config);
    return servlet.getService();
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) BeanInstantiationException(org.springframework.beans.BeanInstantiationException) ServletException(javax.servlet.ServletException) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) Div(com.vaadin.flow.component.html.Div) StaticFileServer(com.vaadin.flow.server.StaticFileServer) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Scope(org.springframework.context.annotation.Scope) Locale(java.util.Locale) Lookup(com.vaadin.flow.di.Lookup) SpringServlet(com.vaadin.flow.spring.SpringServlet) SpringRunner(org.springframework.test.context.junit4.SpringRunner) I18NProvider(com.vaadin.flow.i18n.I18NProvider) ServiceInitEvent(com.vaadin.flow.server.ServiceInitEvent) Instantiator(com.vaadin.flow.di.Instantiator) ServletConfig(javax.servlet.ServletConfig) Properties(java.util.Properties) SpringInstantiator(com.vaadin.flow.spring.SpringInstantiator) Set(java.util.Set) Import(org.springframework.context.annotation.Import) Test(org.junit.Test) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) ComponentScan(org.springframework.context.annotation.ComponentScan) VaadinServiceInitListener(com.vaadin.flow.server.VaadinServiceInitListener) Configuration(org.springframework.context.annotation.Configuration) Mockito(org.mockito.Mockito) List(java.util.List) Component(org.springframework.stereotype.Component) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) StaticFileHandlerFactory(com.vaadin.flow.server.StaticFileHandlerFactory) VaadinService(com.vaadin.flow.server.VaadinService) ServletContext(javax.servlet.ServletContext) Assert(org.junit.Assert) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) Collections(java.util.Collections) VaadinServletService(com.vaadin.flow.server.VaadinServletService) StaticFileHandlerFactory(com.vaadin.flow.server.StaticFileHandlerFactory) StaticFileServer(com.vaadin.flow.server.StaticFileServer) ResourceProvider(com.vaadin.flow.di.ResourceProvider) ServletConfig(javax.servlet.ServletConfig) VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) ServletContext(javax.servlet.ServletContext) Lookup(com.vaadin.flow.di.Lookup) Properties(java.util.Properties) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) SpringServlet(com.vaadin.flow.spring.SpringServlet)

Example 5 with ResourceProvider

use of com.vaadin.flow.di.ResourceProvider in project flow by vaadin.

the class WebComponentBootstrapHandlerTest method initLookup.

private void initLookup(VaadinServletService service) throws IOException {
    VaadinContext context = service.getContext();
    Lookup lookup = Mockito.mock(Lookup.class);
    context.setAttribute(Lookup.class, lookup);
    ResourceProvider provider = Mockito.mock(ResourceProvider.class);
    Mockito.when(lookup.lookup(ResourceProvider.class)).thenReturn(provider);
    Class<? extends VaadinServlet> servletClass = service.getServlet().getClass();
    Mockito.when(provider.getApplicationResource(Mockito.anyString())).thenAnswer(answer -> WebComponentBootstrapHandlerTest.class.getClassLoader().getResource(answer.getArgument(0)));
    Mockito.when(provider.getClientResourceAsStream("META-INF/resources/" + ApplicationConstants.CLIENT_ENGINE_PATH + "/compile.properties")).thenAnswer(invocation -> new ByteArrayInputStream("jsFile=foo".getBytes(StandardCharsets.UTF_8)));
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) ByteArrayInputStream(java.io.ByteArrayInputStream) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Lookup(com.vaadin.flow.di.Lookup)

Aggregations

ResourceProvider (com.vaadin.flow.di.ResourceProvider)30 Lookup (com.vaadin.flow.di.Lookup)22 Test (org.junit.Test)11 URL (java.net.URL)10 VaadinContext (com.vaadin.flow.server.VaadinContext)8 ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)5 File (java.io.File)5 IOException (java.io.IOException)5 Before (org.junit.Before)5 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)4 VaadinConfig (com.vaadin.flow.server.VaadinConfig)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)4 Properties (java.util.Properties)4 Instantiator (com.vaadin.flow.di.Instantiator)3 VaadinServletContext (com.vaadin.flow.server.VaadinServletContext)3 VaadinServletService (com.vaadin.flow.server.VaadinServletService)3 ServletContext (javax.servlet.ServletContext)3 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)2 StaticFileServer (com.vaadin.flow.server.StaticFileServer)2