Search in sources :

Example 1 with SpringVaadinSession

use of com.vaadin.flow.spring.SpringVaadinSession in project flow by vaadin.

the class VaadinRouteScopeTest method destroySession_sessionAttributeIsCleanedAndDestructionCallbackIsCalled.

@Test
public void destroySession_sessionAttributeIsCleanedAndDestructionCallbackIsCalled() {
    UI ui = mockUI();
    mockServletContext(ui);
    SpringVaadinSession springSession = (SpringVaadinSession) VaadinSession.getCurrent();
    doCallRealMethod().when(springSession).addDestroyListener(Mockito.any());
    doCallRealMethod().when(springSession).fireSessionDestroy();
    VaadinRouteScope scope = initScope(ui);
    AtomicInteger count = new AtomicInteger();
    scope.registerDestructionCallback("foo", () -> count.getAndIncrement());
    ObjectFactory<?> factory = putObjectIntoScope(scope);
    String attribute = VaadinRouteScope.class.getName() + "$RouteStoreWrapper";
    // self control - the attribute name is used by the implementation
    Assert.assertNotNull(springSession.getAttribute(attribute));
    springSession.fireSessionDestroy();
    Assert.assertEquals(1, count.get());
    Assert.assertNull(springSession.getAttribute(attribute));
    // Destruction callbacks are not called anymore (they are removed)
    scope.getBeanStore().destroy();
    Assert.assertEquals(1, count.get());
    // object has been removed from the storage, so object factory is called
    // once again to create the bean
    scope.get("foo", factory);
    verify(factory, times(2)).getObject();
}
Also used : SpringVaadinSession(com.vaadin.flow.spring.SpringVaadinSession) UI(com.vaadin.flow.component.UI) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 2 with SpringVaadinSession

use of com.vaadin.flow.spring.SpringVaadinSession in project flow by vaadin.

the class AbstractScopeTest method mockSession.

@SuppressWarnings("unchecked")
protected VaadinSession mockSession() {
    SpringVaadinSession session = Mockito.mock(TestSession.class, Mockito.withSettings().useConstructor());
    doCallRealMethod().when(session).setAttribute(Mockito.any(Class.class), Mockito.any());
    doCallRealMethod().when(session).getAttribute(Mockito.any(Class.class));
    doCallRealMethod().when(session).getAttribute(Mockito.any(String.class));
    doCallRealMethod().when(session).addUI(Mockito.any());
    doCallRealMethod().when(session).removeUI(Mockito.any());
    doCallRealMethod().when(session).getService();
    doCallRealMethod().when(session).getUIs();
    when(session.getState()).thenReturn(VaadinSessionState.OPEN);
    final Properties initParameters = new Properties();
    ApplicationConfiguration appConfig = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(appConfig.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
    VaadinContext context = Mockito.mock(VaadinContext.class);
    Lookup lookup = Mockito.mock(Lookup.class);
    Mockito.when(context.getAttribute(Lookup.class)).thenReturn(lookup);
    Mockito.when(appConfig.getContext()).thenReturn(context);
    DefaultDeploymentConfiguration config = new DefaultDeploymentConfiguration(appConfig, getClass(), initParameters);
    when(session.getConfiguration()).thenReturn(config);
    VaadinSession.setCurrent(session);
    when(session.hasLock()).thenReturn(true);
    // keep a reference to the session so that it cannot be GCed.
    this.session = session;
    return session;
}
Also used : SpringVaadinSession(com.vaadin.flow.spring.SpringVaadinSession) VaadinContext(com.vaadin.flow.server.VaadinContext) Lookup(com.vaadin.flow.di.Lookup) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration) Properties(java.util.Properties) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration)

Example 3 with SpringVaadinSession

use of com.vaadin.flow.spring.SpringVaadinSession in project flow by vaadin.

the class VaadinSessionScopeTest method destroySession_sessionAttributeIsCleanedAndDestructionCallbackIsCalled.

@SuppressWarnings("rawtypes")
@Test
public void destroySession_sessionAttributeIsCleanedAndDestructionCallbackIsCalled() {
    VaadinSession session = mockSession();
    SpringVaadinSession springSession = (SpringVaadinSession) session;
    doCallRealMethod().when(springSession).addDestroyListener(Mockito.any());
    doCallRealMethod().when(springSession).fireSessionDestroy();
    VaadinSessionScope scope = new VaadinSessionScope();
    AtomicInteger count = new AtomicInteger();
    scope.registerDestructionCallback("foo", () -> count.getAndIncrement());
    Object object = new Object();
    ObjectFactory factory = Mockito.mock(ObjectFactory.class);
    when(factory.getObject()).thenReturn(object);
    scope.get("foo", factory);
    springSession.fireSessionDestroy();
    Assert.assertEquals(1, count.get());
    Assert.assertNull(session.getAttribute(BeanStore.class));
    // Destruction callbacks are not called anymore (they are removed)
    scope.getBeanStore().destroy();
    Assert.assertEquals(1, count.get());
    // object has been removed from the storage, so object factory is called
    // once again to create the bean
    scope.get("foo", factory);
    verify(factory, times(2)).getObject();
}
Also used : SpringVaadinSession(com.vaadin.flow.spring.SpringVaadinSession) VaadinSession(com.vaadin.flow.server.VaadinSession) SpringVaadinSession(com.vaadin.flow.spring.SpringVaadinSession) ObjectFactory(org.springframework.beans.factory.ObjectFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 4 with SpringVaadinSession

use of com.vaadin.flow.spring.SpringVaadinSession in project flow by vaadin.

the class VaadinUIScopeTest method destroySession_sessionAttributeIsCleanedAndDestructionCallbackIsCalled.

@SuppressWarnings("rawtypes")
@Test
public void destroySession_sessionAttributeIsCleanedAndDestructionCallbackIsCalled() {
    mockUI();
    SpringVaadinSession springSession = (SpringVaadinSession) VaadinSession.getCurrent();
    doCallRealMethod().when(springSession).addDestroyListener(Mockito.any());
    doCallRealMethod().when(springSession).fireSessionDestroy();
    VaadinUIScope scope = new VaadinUIScope();
    AtomicInteger count = new AtomicInteger();
    scope.registerDestructionCallback("foo", () -> count.getAndIncrement());
    Object object = new Object();
    ObjectFactory factory = Mockito.mock(ObjectFactory.class);
    when(factory.getObject()).thenReturn(object);
    scope.get("foo", factory);
    String attribute = VaadinUIScope.class.getName() + "$UIStoreWrapper";
    // self control - the attribute name is used by the implementation
    Assert.assertNotNull(springSession.getAttribute(attribute));
    springSession.fireSessionDestroy();
    Assert.assertEquals(1, count.get());
    Assert.assertNull(springSession.getAttribute(attribute));
    // Destruction callbacks are not called anymore (they are removed)
    scope.getBeanStore().destroy();
    Assert.assertEquals(1, count.get());
    // object has been removed from the storage, so object factory is called
    // once again to create the bean
    scope.get("foo", factory);
    verify(factory, times(2)).getObject();
}
Also used : SpringVaadinSession(com.vaadin.flow.spring.SpringVaadinSession) ObjectFactory(org.springframework.beans.factory.ObjectFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

SpringVaadinSession (com.vaadin.flow.spring.SpringVaadinSession)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Test (org.junit.Test)3 ObjectFactory (org.springframework.beans.factory.ObjectFactory)2 UI (com.vaadin.flow.component.UI)1 Lookup (com.vaadin.flow.di.Lookup)1 DefaultDeploymentConfiguration (com.vaadin.flow.server.DefaultDeploymentConfiguration)1 VaadinContext (com.vaadin.flow.server.VaadinContext)1 VaadinSession (com.vaadin.flow.server.VaadinSession)1 ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)1 Properties (java.util.Properties)1