Search in sources :

Example 21 with VaadinSession

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

the class StreamResourceRegistryTest method setUp.

@Before
public void setUp() throws ServletException {
    UI ui = Mockito.mock(UI.class);
    UI.setCurrent(ui);
    Mockito.when(ui.getUIId()).thenReturn(1);
    service = servlet.getService();
    session = new VaadinSession(service) {

        @Override
        public boolean hasLock() {
            return true;
        }
    };
}
Also used : UI(com.vaadin.flow.component.UI) VaadinSession(com.vaadin.flow.server.VaadinSession) Before(org.junit.Before)

Example 22 with VaadinSession

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

the class ComponentTest method testUIInitialAttach.

@Test
public void testUIInitialAttach() {
    AtomicBoolean initialAttach = new AtomicBoolean(false);
    UI ui = new UI();
    ui.addAttachListener(e -> {
        initialAttach.set(e.isInitialAttach());
    });
    ui.getInternals().setSession(new VaadinSession(null));
    Assert.assertTrue(initialAttach.get());
// UI is never detached and reattached
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VaadinSession(com.vaadin.flow.server.VaadinSession) Test(org.junit.Test)

Example 23 with VaadinSession

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

the class ComponentTest method setup.

@Before
public void setup() {
    divWithTextComponent = new TestComponent(ElementFactory.createDiv("Test component"));
    parentDivComponent = new TestComponent(ElementFactory.createDiv());
    child1SpanComponent = new TestComponent(ElementFactory.createSpan("Span"));
    child2InputComponent = new TestComponent(ElementFactory.createInput());
    parentDivComponent.getElement().appendChild(child1SpanComponent.getElement(), child2InputComponent.getElement());
    VaadinSession session = Mockito.mock(VaadinSession.class);
    UI ui = new UI() {

        @Override
        public VaadinSession getSession() {
            return session;
        }
    };
    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) VaadinSession(com.vaadin.flow.server.VaadinSession) VaadinService(com.vaadin.flow.server.VaadinService) Before(org.junit.Before)

Example 24 with VaadinSession

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

the class DefaultTemplateParserTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
    VaadinSession session = Mockito.mock(VaadinSession.class);
    factory = (VaadinUriResolverFactory) vaadinRequest -> resolver;
    Mockito.when(session.getAttribute(VaadinUriResolverFactory.class)).thenReturn(factory);
    Mockito.when(service.getDependencyFilters()).thenReturn(Collections.emptyList());
    WrappedHttpSession wrappedSession = Mockito.mock(WrappedHttpSession.class);
    HttpSession httpSession = Mockito.mock(HttpSession.class);
    Mockito.when(wrappedSession.getHttpSession()).thenReturn(httpSession);
    servlet = new VaadinServlet() {

        @Override
        protected VaadinServletService createServletService() throws ServletException, ServiceException {
            return service;
        }

        @Override
        public ServletContext getServletContext() {
            return servletContext;
        }
    };
    Mockito.when(service.getServlet()).thenReturn(servlet);
    Mockito.when(resolver.resolveVaadinUri("/bar.html")).thenReturn("bar.html");
    Mockito.when(resolver.resolveVaadinUri("/bar1.html")).thenReturn("bar1.html");
    Mockito.when(resolver.resolveVaadinUri("/bundle.html")).thenReturn("bundle.html");
    Mockito.when(request.getWrappedSession()).thenReturn(wrappedSession);
    Mockito.when(request.getServletPath()).thenReturn("");
    Mockito.when(servletContext.getResourceAsStream("/bar.html")).thenReturn(new ByteArrayInputStream("<dom-module id='bar'></dom-module>".getBytes(StandardCharsets.UTF_8)));
    Mockito.when(servletContext.getResourceAsStream("/bar1.html")).thenReturn(new ByteArrayInputStream("<dom-module id='foo'></dom-module>".getBytes(StandardCharsets.UTF_8)));
    Mockito.when(servletContext.getResourceAsStream("/bundle.html")).thenReturn(getBundle(), getBundle(), getBundle());
    CurrentInstance.set(VaadinRequest.class, request);
    CurrentInstance.set(VaadinSession.class, session);
    CurrentInstance.set(VaadinService.class, service);
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) TemplateData(com.vaadin.flow.component.polymertemplate.TemplateParser.TemplateData) WrappedHttpSession(com.vaadin.flow.server.WrappedHttpSession) ServletException(javax.servlet.ServletException) Mock(org.mockito.Mock) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) VaadinUriResolverFactory(com.vaadin.flow.server.VaadinUriResolverFactory) Comment(org.jsoup.nodes.Comment) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Dependency(com.vaadin.flow.shared.ui.Dependency) Type(com.vaadin.flow.shared.ui.Dependency.Type) Assert.assertThat(org.junit.Assert.assertThat) MockitoAnnotations(org.mockito.MockitoAnnotations) Tag(com.vaadin.flow.component.Tag) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Element(org.jsoup.nodes.Element) After(org.junit.After) DependencyFilter(com.vaadin.flow.server.DependencyFilter) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Before(org.junit.Before) HttpSession(javax.servlet.http.HttpSession) VaadinSession(com.vaadin.flow.server.VaadinSession) Matchers.empty(org.hamcrest.Matchers.empty) VaadinServlet(com.vaadin.flow.server.VaadinServlet) Test(org.junit.Test) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) NotThreadSafe(net.jcip.annotations.NotThreadSafe) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Node(org.jsoup.nodes.Node) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) VaadinService(com.vaadin.flow.server.VaadinService) LoadMode(com.vaadin.flow.shared.ui.LoadMode) ServletContext(javax.servlet.ServletContext) ModelClass(com.vaadin.flow.component.polymertemplate.PolymerTemplateTest.ModelClass) ServiceException(com.vaadin.flow.server.ServiceException) Assert(org.junit.Assert) Collections(java.util.Collections) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ServletException(javax.servlet.ServletException) VaadinSession(com.vaadin.flow.server.VaadinSession) ServiceException(com.vaadin.flow.server.ServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) WrappedHttpSession(com.vaadin.flow.server.WrappedHttpSession) HttpSession(javax.servlet.http.HttpSession) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinServlet(com.vaadin.flow.server.VaadinServlet) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ServletContext(javax.servlet.ServletContext) WrappedHttpSession(com.vaadin.flow.server.WrappedHttpSession) Before(org.junit.Before)

Example 25 with VaadinSession

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

the class PolymerTemplateTest method setUp.

@SuppressWarnings("serial")
@Before
public void setUp() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    executionOrder.clear();
    executionParams.clear();
    Field customElements = CustomElementRegistry.class.getDeclaredField("customElements");
    customElements.setAccessible(true);
    customElements.set(CustomElementRegistry.getInstance(), new AtomicReference<>());
    Map<String, Class<? extends Component>> map = new HashMap<>();
    map.put("child-template", TemplateChild.class);
    map.put("ffs", TestPolymerTemplate.class);
    map.put("execution-child", ExecutionChild.class);
    CustomElementRegistry.getInstance().setCustomElements(map);
    VaadinSession session = Mockito.mock(VaadinSession.class);
    UI ui = new UI() {

        private Page page = new Page(this) {

            @Override
            public ExecutionCanceler executeJavaScript(String expression, Serializable... parameters) {
                executionOrder.add(expression);
                executionParams.add(parameters);
                return () -> true;
            }
        };

        @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 : Serializable(java.io.Serializable) VaadinSession(com.vaadin.flow.server.VaadinSession) HashMap(java.util.HashMap) Page(com.vaadin.flow.component.page.Page) DefaultInstantiator(com.vaadin.flow.di.DefaultInstantiator) Field(java.lang.reflect.Field) UI(com.vaadin.flow.component.UI) VaadinService(com.vaadin.flow.server.VaadinService) Component(com.vaadin.flow.component.Component) Before(org.junit.Before)

Aggregations

VaadinSession (com.vaadin.flow.server.VaadinSession)26 UI (com.vaadin.flow.component.UI)9 VaadinService (com.vaadin.flow.server.VaadinService)9 Before (org.junit.Before)6 DefaultInstantiator (com.vaadin.flow.di.DefaultInstantiator)4 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)4 SpringVaadinSession (com.vaadin.flow.spring.SpringVaadinSession)4 Test (org.junit.Test)4 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)3 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)2 SessionExpiredException (com.vaadin.flow.server.SessionExpiredException)2 SystemMessages (com.vaadin.flow.server.SystemMessages)2 VaadinServlet (com.vaadin.flow.server.VaadinServlet)2 VaadinServletService (com.vaadin.flow.server.VaadinServletService)2 InvalidUIDLSecurityKeyException (com.vaadin.flow.server.communication.ServerRpcHandler.InvalidUIDLSecurityKeyException)2 PushMode (com.vaadin.flow.shared.communication.PushMode)2 JsonException (elemental.json.JsonException)2 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2 Field (java.lang.reflect.Field)2