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;
}
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()));
}
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);
}
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);
}
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;
}
Aggregations