diff options
author | Gilberto Torrezan <gilberto-torrezan@users.noreply.github.com> | 2018-06-13 10:08:04 +0300 |
---|---|---|
committer | Gilberto Torrezan <gilberto-torrezan@users.noreply.github.com> | 2018-09-03 15:21:22 +0300 |
commit | 8d61288b6fd19a7f5284c05b1917d33eeb816d2a (patch) | |
tree | fe3072366d6bd768e9f3025e057a6b499ebae290 /server/src/test | |
parent | 328b057f4087f546a0a26302e23014aae09920d7 (diff) | |
download | vaadin-framework-8d61288b6fd19a7f5284c05b1917d33eeb816d2a.tar.gz vaadin-framework-8d61288b6fd19a7f5284c05b1917d33eeb816d2a.zip |
Add fallback resolvers for CurrentInstance (#10974)
This allow applications to inject custom default instances when the
current instances cannot be found by regular means.
For example, when VaadinServlet.getCurrent() would return null, a
fallback resolver could be invoked to properly create the servlet and
return it.
Diffstat (limited to 'server/src/test')
-rw-r--r-- | server/src/test/java/com/vaadin/util/CurrentInstanceTest.java | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/server/src/test/java/com/vaadin/util/CurrentInstanceTest.java b/server/src/test/java/com/vaadin/util/CurrentInstanceTest.java index 87d6245dcb..1beae2d3be 100644 --- a/server/src/test/java/com/vaadin/util/CurrentInstanceTest.java +++ b/server/src/test/java/com/vaadin/util/CurrentInstanceTest.java @@ -3,11 +3,13 @@ package com.vaadin.util; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; import java.lang.ref.WeakReference; import java.lang.reflect.Field; import java.util.Map; +import java.util.Properties; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; @@ -15,11 +17,17 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import org.easymock.EasyMock; +import org.hamcrest.CoreMatchers; +import org.junit.After; import org.junit.Before; import org.junit.Test; +import com.vaadin.server.DefaultDeploymentConfiguration; +import com.vaadin.server.ServiceException; import com.vaadin.server.VaadinRequest; import com.vaadin.server.VaadinService; +import com.vaadin.server.VaadinServlet; +import com.vaadin.server.VaadinServletService; import com.vaadin.server.VaadinSession; import com.vaadin.ui.UI; @@ -31,6 +39,17 @@ public class CurrentInstanceTest { CurrentInstance.clearAll(); } + @Before + @After + public void clearExistingFallbackResolvers() throws Exception { + // Removes all static fallback resolvers + Field field = CurrentInstance.class + .getDeclaredField("fallbackResolvers"); + field.setAccessible(true); + Map<?, ?> map = (Map<?, ?>) field.get(null); + map.clear(); + } + @Test public void testInitiallyCleared() throws Exception { assertCleared(); @@ -144,6 +163,62 @@ public class CurrentInstanceTest { assertNull(VaadinSession.getCurrent()); } + @Test + public void testFallbackResolvers() throws Exception { + TestFallbackResolver<UI> uiResolver = new TestFallbackResolver<UI>( + new FakeUI()); + CurrentInstance.defineFallbackResolver(UI.class, uiResolver); + + TestFallbackResolver<VaadinSession> sessionResolver = new TestFallbackResolver<VaadinSession>( + new FakeSession()); + CurrentInstance.defineFallbackResolver(VaadinSession.class, + sessionResolver); + + TestFallbackResolver<VaadinService> serviceResolver = new TestFallbackResolver<VaadinService>( + new FakeService(new FakeServlet())); + CurrentInstance.defineFallbackResolver(VaadinService.class, + serviceResolver); + + assertThat(UI.getCurrent(), CoreMatchers.instanceOf(FakeUI.class)); + assertThat(VaadinSession.getCurrent(), + CoreMatchers.instanceOf(FakeSession.class)); + assertThat(VaadinService.getCurrent(), + CoreMatchers.instanceOf(FakeService.class)); + + assertEquals( + "The UI fallback resolver should have been called exactly once", + 1, uiResolver.getCalled()); + + assertEquals( + "The VaadinSession fallback resolver should have been called exactly once", + 1, sessionResolver.getCalled()); + + assertEquals( + "The VaadinService fallback resolver should have been called exactly once", + 1, serviceResolver.getCalled()); + + // the VaadinServlet.getCurrent() resolution uses the VaadinService type + assertThat(VaadinServlet.getCurrent(), + CoreMatchers.instanceOf(FakeServlet.class)); + assertEquals( + "The VaadinService fallback resolver should have been called exactly twice", + 2, serviceResolver.getCalled()); + + } + + @Test(expected = IllegalArgumentException.class) + public void testFallbackResolversWithAlreadyDefinedResolver() { + TestFallbackResolver<UI> uiResolver = new TestFallbackResolver<UI>( + new FakeUI()); + CurrentInstance.defineFallbackResolver(UI.class, uiResolver); + CurrentInstance.defineFallbackResolver(UI.class, uiResolver); + } + + @Test(expected = IllegalArgumentException.class) + public void testFallbackResolversWithNullResolver() { + CurrentInstance.defineFallbackResolver(UI.class, null); + } + public static void waitUntilGarbageCollected(WeakReference<?> ref) throws InterruptedException { for (int i = 0; i < 50; i++) { @@ -172,4 +247,49 @@ public class CurrentInstanceTest { Future<Void> future = service.submit(runnable); future.get(); } + + private static class TestFallbackResolver<T> + implements CurrentInstanceFallbackResolver<T> { + + private int called; + private final T instance; + + public TestFallbackResolver(T instance) { + this.instance = instance; + } + + @Override + public T resolve() { + called++; + return instance; + } + + public int getCalled() { + return called; + } + } + + private static class FakeUI extends UI { + @Override + protected void init(VaadinRequest request) { + } + } + + private static class FakeServlet extends VaadinServlet { + } + + private static class FakeService extends VaadinServletService { + public FakeService(VaadinServlet servlet) throws ServiceException { + super(servlet, new DefaultDeploymentConfiguration(FakeService.class, + new Properties())); + } + } + + private static class FakeSession extends VaadinSession { + public FakeSession() { + super(null); + } + + } + } |