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