Search in sources :

Example 51 with VaadinService

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

the class TemplateInitializerTest method createService.

@Override
protected VaadinService createService() {
    VaadinService service = mock(VaadinService.class);
    DeploymentConfiguration configuration = mock(DeploymentConfiguration.class);
    when(configuration.isProductionMode()).thenReturn(false);
    when(service.getDeploymentConfiguration()).thenReturn(configuration);
    return service;
}
Also used : VaadinService(com.vaadin.flow.server.VaadinService) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Example 52 with VaadinService

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

the class NpmTemplateParser method getTemplateContent.

@Override
public TemplateData getTemplateContent(Class<? extends PolymerTemplate<?>> clazz, String tag, VaadinService service) {
    List<Dependency> dependencies = AnnotationReader.getAnnotationsFor(clazz, JsModule.class).stream().map(jsModule -> new Dependency(Dependency.Type.JS_MODULE, jsModule.value(), // load mode doesn't
    LoadMode.EAGER)).collect(Collectors.toList());
    for (DependencyFilter filter : service.getDependencyFilters()) {
        dependencies = filter.filter(new ArrayList<>(dependencies), service);
    }
    Pair<Dependency, String> chosenDep = null;
    for (Dependency dependency : dependencies) {
        if (dependency.getType() != Dependency.Type.JS_MODULE) {
            continue;
        }
        String url = dependency.getUrl();
        String source = getSourcesFromTemplate(service, tag, url);
        if (source == null) {
            continue;
        }
        if (chosenDep == null) {
            chosenDep = new Pair<>(dependency, source);
        }
        if (dependencyHasTagName(dependency, tag)) {
            chosenDep = new Pair<>(dependency, source);
            break;
        }
    }
    if (chosenDep != null) {
        Element templateElement = BundleParser.parseTemplateElement(chosenDep.getFirst().getUrl(), chosenDep.getSecond());
        if (!JsoupUtils.getDomModule(templateElement, null).isPresent()) {
            // Template needs to be wrapped in an element with id, to look
            // like a P2 template
            Element parent = new Element(tag);
            parent.attr("id", tag);
            templateElement.appendTo(parent);
        }
        return new TemplateData(chosenDep.getFirst().getUrl(), templateElement);
    }
    throw new IllegalStateException(String.format("Couldn't find the " + "definition of the element with tag '%s' " + "in any template file declared using '@%s' annotations. " + "In a Spring Boot project, please ensure that the template's " + "groupId is added to the vaadin.whitelisted-packages " + "property. Otherwise, please Check the availability of the " + "template files in your WAR file or provide alternative " + "implementation of the method getTemplateContent() which " + "should return an element representing the content of the " + "template file", tag, JsModule.class.getSimpleName()));
}
Also used : Logger(org.slf4j.Logger) URL(java.net.URL) Pair(com.vaadin.flow.internal.Pair) AnnotationReader(com.vaadin.flow.internal.AnnotationReader) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Dependency(com.vaadin.flow.shared.ui.Dependency) Collectors(java.util.stream.Collectors) FrontendUtils(com.vaadin.flow.server.frontend.FrontendUtils) ArrayList(java.util.ArrayList) List(java.util.List) Locale(java.util.Locale) Element(org.jsoup.nodes.Element) VaadinService(com.vaadin.flow.server.VaadinService) LoadMode(com.vaadin.flow.shared.ui.LoadMode) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Lookup(com.vaadin.flow.di.Lookup) Constants(com.vaadin.flow.server.Constants) JsModule(com.vaadin.flow.component.dependency.JsModule) FilenameUtils(org.apache.commons.io.FilenameUtils) InputStream(java.io.InputStream) JsModule(com.vaadin.flow.component.dependency.JsModule) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Dependency(com.vaadin.flow.shared.ui.Dependency)

Example 53 with VaadinService

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

the class PolymerTemplateTest method parseCachedTemplate_twoTemplatesWithInjetions_injectionsAreRegisteredInFeature.

@Test
public void parseCachedTemplate_twoTemplatesWithInjetions_injectionsAreRegisteredInFeature() {
    Mockito.when(configuration.isProductionMode()).thenReturn(true);
    AtomicInteger parserCallCount = new AtomicInteger();
    TemplateParser parser = new TemplateParser() {

        @Override
        public TemplateData getTemplateContent(Class<? extends PolymerTemplate<?>> clazz, String tag, VaadinService service) {
            String content;
            parserCallCount.incrementAndGet();
            if (clazz.equals(TemplateInitialization.class)) {
                content = "<dom-module id='" + tag + "'><template>" + "<ffs id='foo' someattrtibute></ffs>" + "<child-template></child-template>" + "</template></dom-module>";
            } else {
                content = "<dom-module id='" + tag + "'><template>" + "<child-template id='bar'></child-template> <ffs></ffs>" + "</template></dom-module>";
            }
            return new TemplateData("", Jsoup.parse(content));
        }
    };
    // run in the production mode (with caching enabled) for the first time
    TemplateInitialization template1 = new TemplateInitialization(parser);
    assertEquals(1, parserCallCount.get());
    assertTemplateInitialization(template1);
    // run in the production mode (with caching enabled) for the second time
    template1 = new TemplateInitialization(parser);
    // parser shouldn't be called
    assertEquals(1, parserCallCount.get());
    assertTemplateInitialization(template1);
    parserCallCount.set(0);
    // Now initialize another template
    // run in the production mode (with caching enabled) for the first time
    AnotherTemplateInitialization template2 = new AnotherTemplateInitialization(parser);
    assertEquals(1, parserCallCount.get());
    assertAnotherTemplateInitialization(template2);
    // run in the production mode (with caching enabled) for the second time
    template2 = new AnotherTemplateInitialization(parser);
    // parser shouldn't be called
    assertEquals(1, parserCallCount.get());
    assertAnotherTemplateInitialization(template2);
}
Also used : TemplateData(com.vaadin.flow.component.polymertemplate.TemplateParser.TemplateData) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) VaadinService(com.vaadin.flow.server.VaadinService) Test(org.junit.Test)

Example 54 with VaadinService

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

the class PolymerTemplateTest method setUp.

@SuppressWarnings("serial")
@Before
public void setUp() throws SecurityException, IllegalArgumentException {
    executionOrder.clear();
    executionParams.clear();
    VaadinSession session = Mockito.mock(VaadinSession.class);
    ui = new UI() {

        private Page page = new Page(this) {

            @Override
            public PendingJavaScriptResult executeJs(String expression, Serializable... parameters) {
                executionOrder.add(expression);
                executionParams.add(parameters);
                return null;
            }
        };

        @Override
        public VaadinSession getSession() {
            return session;
        }

        @Override
        public Page getPage() {
            return page;
        }
    };
    VaadinService service = Mockito.mock(VaadinService.class);
    when(session.getService()).thenReturn(service);
    DefaultInstantiator instantiator = new DefaultInstantiator(service);
    when(service.getInstantiator()).thenReturn(instantiator);
    UI.setCurrent(ui);
}
Also used : DefaultInstantiator(com.vaadin.flow.di.DefaultInstantiator) Serializable(java.io.Serializable) VaadinSession(com.vaadin.flow.server.VaadinSession) UI(com.vaadin.flow.component.UI) VaadinService(com.vaadin.flow.server.VaadinService) Page(com.vaadin.flow.component.page.Page) Before(org.junit.Before)

Example 55 with VaadinService

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

the class TemplateModelWithEncodersTest method createService.

@Override
protected VaadinService createService() {
    VaadinService service = Mockito.mock(VaadinService.class);
    DeploymentConfiguration configuration = Mockito.mock(DeploymentConfiguration.class);
    Mockito.when(configuration.isProductionMode()).thenReturn(true);
    Mockito.when(service.getDeploymentConfiguration()).thenReturn(configuration);
    return service;
}
Also used : VaadinService(com.vaadin.flow.server.VaadinService) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Aggregations

VaadinService (com.vaadin.flow.server.VaadinService)86 Test (org.junit.Test)39 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)19 VaadinSession (com.vaadin.flow.server.VaadinSession)18 Properties (java.util.Properties)15 VaadinContext (com.vaadin.flow.server.VaadinContext)12 Before (org.junit.Before)11 SpringInstantiatorTest (com.vaadin.flow.spring.instantiator.SpringInstantiatorTest)10 UI (com.vaadin.flow.component.UI)9 ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)8 Lookup (com.vaadin.flow.di.Lookup)7 VaadinResponse (com.vaadin.flow.server.VaadinResponse)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 ServletContext (javax.servlet.ServletContext)6 MockVaadinContext (com.vaadin.flow.server.MockVaadinContext)5 DefaultInstantiator (com.vaadin.flow.di.DefaultInstantiator)4 Instantiator (com.vaadin.flow.di.Instantiator)4 BrowserLiveReload (com.vaadin.flow.internal.BrowserLiveReload)4 DefaultDeploymentConfiguration (com.vaadin.flow.server.DefaultDeploymentConfiguration)4 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)4