Java Code Examples for com.vaadin.flow.server.VaadinSession#setCurrent()

The following examples show how to use com.vaadin.flow.server.VaadinSession#setCurrent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExtendedClientDetailsTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void isIOS_notIPadIsIPhone_returnsTrue() {
    ExtendedClientDetails details = Mockito
            .mock(ExtendedClientDetails.class);
    Mockito.doCallRealMethod().when(details).isIOS();

    VaadinSession session = Mockito.mock(VaadinSession.class);
    VaadinSession.setCurrent(session);

    WebBrowser browser = Mockito.mock(WebBrowser.class);
    Mockito.when(session.getBrowser()).thenReturn(browser);

    Mockito.when(browser.isIPhone()).thenReturn(true);

    Assert.assertTrue(details.isIOS());
}
 
Example 2
Source File: RouteConfigurationTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void configurationForSessionRegistry_buildsWithCorrectRegistry() {
    SessionRouteRegistry registry = getRegistry(session);
    registry.update(() -> {
        registry.setRoute("", MyRoute.class, Collections.emptyList());
        registry.setRoute("path", Secondary.class, Collections.emptyList());
    });

    try {
        VaadinSession.setCurrent(session);
        session.lock();
        RouteConfiguration routeConfiguration = RouteConfiguration
                .forSessionScope();

        Assert.assertEquals(
                "After unlock registry should be updated for others to configure with new data",
                2, routeConfiguration.getAvailableRoutes().size());
    } finally {
        session.unlock();
        CurrentInstance.clearAll();
    }
}
 
Example 3
Source File: CurrentInstanceTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void testRestoreWithGarbageCollectedValue()
        throws InterruptedException {
    VaadinSession session1 = new VaadinSession(new MockVaadinServletService()) {
        @Override
        public String toString() {
            return "First session";
        }
    };
    VaadinSession session2 = new VaadinSession(new MockVaadinServletService()) {
        @Override
        public String toString() {
            return "Second session";
        }
    };

    VaadinSession.setCurrent(session1);
    Map<Class<?>, CurrentInstance> previous = CurrentInstance
            .setCurrent(session2);

    // Use weak ref to verify object is collected
    WeakReference<VaadinSession> ref = new WeakReference<>(session1);

    session1 = null;
    Assert.assertTrue(TestUtil.isGarbageCollected(ref));

    CurrentInstance.restoreInstances(previous);

    Assert.assertNull(VaadinSession.getCurrent());
}
 
Example 4
Source File: ExtendedClientDetailsTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void isIOS_notIPad_deprecatedIsNotIOS_returnsFalse() {
    ExtendedClientDetails details = Mockito
            .mock(ExtendedClientDetails.class);
    Mockito.doCallRealMethod().when(details).isIOS();

    VaadinSession session = Mockito.mock(VaadinSession.class);
    VaadinSession.setCurrent(session);

    WebBrowser browser = Mockito.mock(WebBrowser.class);
    Mockito.when(session.getBrowser()).thenReturn(browser);

    Assert.assertFalse(details.isIOS());
}
 
Example 5
Source File: HierarchicalCommunicatorDataTest.java    From flow with Apache License 2.0 5 votes vote down vote up
private static VaadinSession findOrCreateSession() {
    VaadinSession session = VaadinSession.getCurrent();
    if (session == null) {
        session = new DataCommunicatorTest.AlwaysLockedVaadinSession(
                null);
        VaadinSession.setCurrent(session);
    }
    return session;
}
 
Example 6
Source File: DataCommunicatorTest.java    From flow with Apache License 2.0 5 votes vote down vote up
private static VaadinSession findOrcreateSession() {
    VaadinSession session = VaadinSession.getCurrent();
    if (session == null) {
        session = new AlwaysLockedVaadinSession(null);
        VaadinSession.setCurrent(session);
    }
    return session;
}
 
Example 7
Source File: AbstractListDataViewListenerTest.java    From flow with Apache License 2.0 5 votes vote down vote up
private static VaadinSession findOrcreateSession() {
    VaadinSession session = VaadinSession.getCurrent();
    if (session == null) {
        session = new AlwaysLockedVaadinSession(null);
        VaadinSession.setCurrent(session);
    }
    return session;
}
 
Example 8
Source File: MockUI.java    From flow with Apache License 2.0 5 votes vote down vote up
private static VaadinSession findOrCreateSession() {
    VaadinSession session = VaadinSession.getCurrent();
    if (session == null) {
        session = new AlwaysLockedVaadinSession(Mockito.mock(VaadinService.class));
        VaadinSession.setCurrent(session);
    }
    return session;
}
 
Example 9
Source File: ScopedEventBusIntegrationTest.java    From vaadin4spring with Apache License 2.0 5 votes vote down vote up
@Test
void testDifferentSessionsReturnDifferentSessionEventBuses() {
    EventBus.SessionEventBus sessionBus = applicationContext.getBean(EventBus.SessionEventBus.class);
    VaadinSession.setCurrent(new TestSession());
    EventBus.SessionEventBus sessionBus2 = applicationContext.getBean(EventBus.SessionEventBus.class);
    assertNotEquals(sessionBus, sessionBus2, "Different sessions should return different SessionEventBuses");
}
 
Example 10
Source File: ScopedEventBusIntegrationTest.java    From vaadin4spring with Apache License 2.0 5 votes vote down vote up
@Test
void sameApplicationDifferentSessionReturnsSameApplicationEventBus() {
    EventBus.ApplicationEventBus applicationBus = applicationContext.getBean(EventBus.ApplicationEventBus.class);
    VaadinSession.setCurrent(new TestSession());
    EventBus.ApplicationEventBus applicationBus2 = applicationContext.getBean(EventBus.ApplicationEventBus.class);
    assertEquals(applicationBus, applicationBus2, "ApplicationEventBus should always be the same");
}
 
Example 11
Source File: PendingJavaScriptInvocationTest.java    From flow with Apache License 2.0 4 votes vote down vote up
@Test
public void blockFromOtherThread_doesNotThrow() throws Exception {
    MockVaadinSession session = new MockVaadinSession();
    VaadinSession.setCurrent(session);

    ExecutorService executor = Executors.newFixedThreadPool(3);

    session.lock();
    try {
        CompletableFuture<JsonValue> completableFuture = invocation
                .toCompletableFuture();

        List<Future<JsonValue>> futures = createBlockingActions(
                completableFuture).stream().map(executor::submit)
                        .collect(Collectors.toList());

        Assert.assertEquals("All futures should be pending", 0,
                futures.stream().filter(Future::isDone).count());

        JsonObject value = Json.createObject();
        invocation.complete(value);

        executor.shutdown();
        executor.awaitTermination(100, TimeUnit.MILLISECONDS);

        Assert.assertEquals("All futures should be done", futures.size(),
                futures.stream().filter(Future::isDone).count());

        futures.forEach(future -> {
            try {
                JsonValue futureValue = future.get();
                Assert.assertSame(value, futureValue);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });

    } finally {
        session.unlock();
        executor.shutdown();
    }
}
 
Example 12
Source File: RouteConfigurationTest.java    From flow with Apache License 2.0 4 votes vote down vote up
@Test
public void addListenerToApplicationScoped_noEventForSessionChange() {
    VaadinServlet servlet = Mockito.mock(VaadinServlet.class);
    Mockito.when(servlet.getServletContext()).thenReturn(servletContext);
    Mockito.when(vaadinService.getServlet()).thenReturn(servlet);

    try {
        CurrentInstance.set(VaadinService.class, vaadinService);
        VaadinSession.setCurrent(session);
        session.lock();
        RouteConfiguration routeConfiguration = RouteConfiguration
                .forApplicationScope();

        List<RouteBaseData> added = new ArrayList<>();
        List<RouteBaseData> removed = new ArrayList<>();

        routeConfiguration.addRoutesChangeListener(event -> {
            added.clear();
            removed.clear();
            added.addAll(event.getAddedRoutes());
            removed.addAll(event.getRemovedRoutes());
        });

        routeConfiguration.setRoute("old", MyRoute.class);

        Assert.assertEquals(1, added.size());
        Assert.assertEquals(0, removed.size());

        added.clear();
        removed.clear();

        routeConfiguration = RouteConfiguration.forSessionScope();

        routeConfiguration.setRoute("new", MyRoute.class);

        Assert.assertEquals(0, added.size());
        Assert.assertEquals(0, removed.size());
    } finally {
        session.unlock();
        CurrentInstance.clearAll();
    }
}
 
Example 13
Source File: RouteConfigurationTest.java    From flow with Apache License 2.0 4 votes vote down vote up
@Test
public void addListenerToSessionScoped_alsoEventsForApplicationScope() {
    VaadinServlet servlet = Mockito.mock(VaadinServlet.class);
    Mockito.when(servlet.getServletContext()).thenReturn(servletContext);
    Mockito.when(vaadinService.getServlet()).thenReturn(servlet);

    try {
        CurrentInstance.set(VaadinService.class, vaadinService);
        VaadinSession.setCurrent(session);
        session.lock();

        List<RouteBaseData> added = new ArrayList<>();
        List<RouteBaseData> removed = new ArrayList<>();

        RouteConfiguration routeConfiguration = RouteConfiguration
                .forSessionScope();

        routeConfiguration.addRoutesChangeListener(event -> {
            added.clear();
            removed.clear();
            added.addAll(event.getAddedRoutes());
            removed.addAll(event.getRemovedRoutes());
        });

        routeConfiguration.setRoute("old", MyRoute.class);

        Assert.assertEquals(1, added.size());
        Assert.assertEquals(0, removed.size());

        added.clear();
        removed.clear();

        routeConfiguration = RouteConfiguration.forApplicationScope();

        routeConfiguration.setRoute("new", MyRoute.class);

        Assert.assertEquals(1, added.size());
        Assert.assertEquals(0, removed.size());
    } finally {
        session.unlock();
        CurrentInstance.clearAll();
    }
}
 
Example 14
Source File: MockUI.java    From flow with Apache License 2.0 4 votes vote down vote up
private static VaadinSession createSession() {
    VaadinSession session = new AlwaysLockedVaadinSession(
            Mockito.mock(VaadinService.class));
    VaadinSession.setCurrent(session);
    return session;
}
 
Example 15
Source File: ScopedEventBusIntegrationTest.java    From vaadin4spring with Apache License 2.0 4 votes vote down vote up
@BeforeEach
public void setUp() {
    MockitoAnnotations.initMocks(this);
    VaadinSession.setCurrent(new TestSession());
    UI.setCurrent(createMockUI());
}