use of com.vaadin.flow.server.VaadinServletContext in project flow by vaadin.
the class VaadinAppShellInitializerTest method setup.
@Before
public void setup() throws Exception {
logger = mockLog(VaadinAppShellInitializer.class);
mocks = new MockServletServiceSessionSetup();
servletContext = mocks.getServletContext();
appConfig = mockApplicationConfiguration();
Lookup lookup = (Lookup) servletContext.getAttribute(Lookup.class.getName());
Mockito.when(lookup.lookup(AppShellPredicate.class)).thenReturn(AppShellConfigurator.class::isAssignableFrom);
attributeMap.put(Lookup.class.getName(), servletContext.getAttribute(Lookup.class.getName()));
attributeMap.put(ApplicationConfiguration.class.getName(), appConfig);
service = mocks.getService();
Mockito.when(servletContext.getAttribute(Mockito.anyString())).then(invocationOnMock -> attributeMap.get(invocationOnMock.getArguments()[0].toString()));
Mockito.doAnswer(invocationOnMock -> attributeMap.put(invocationOnMock.getArguments()[0].toString(), invocationOnMock.getArguments()[1])).when(servletContext).setAttribute(Mockito.anyString(), Mockito.any());
ServletRegistration registration = Mockito.mock(ServletRegistration.class);
context = new VaadinServletContext(servletContext);
classes = new HashSet<>();
Map<String, ServletRegistration> registry = new HashMap<>();
registry.put("foo", registration);
Mockito.when(servletContext.getServletRegistrations()).thenReturn((Map) registry);
Mockito.when(servletContext.getInitParameterNames()).thenReturn(Collections.emptyEnumeration());
initializer = new VaadinAppShellInitializer();
document = Document.createShell("");
pushConfiguration = Mockito.mock(PushConfiguration.class);
}
use of com.vaadin.flow.server.VaadinServletContext in project flow by vaadin.
the class RouteRegistryInitializerTest method firstInitRouteRegistry.
private ApplicationRouteRegistry firstInitRouteRegistry() throws VaadinInitializerException {
vaadinContext = new VaadinServletContext(servletContext) {
private Map<Class<?>, Object> map = new HashMap<>();
@Override
public <T> T getAttribute(Class<T> type) {
if (Lookup.class.equals(type)) {
return type.cast(lookup);
} else {
return type.cast(map.get(type));
}
}
@Override
public <T> void setAttribute(Class<T> clazz, T value) {
map.put(clazz, value);
}
};
// first time initialization
routeRegistryInitializer.initialize(Collections.singleton(OldRouteTarget.class), vaadinContext);
ApplicationRouteRegistry registry = ApplicationRouteRegistry.getInstance(vaadinContext);
List<RouteData> routes = registry.getRegisteredRoutes();
// self check
Assert.assertEquals(1, routes.size());
RouteData data = routes.get(0);
Assert.assertEquals("foo-bar", data.getTemplate());
List<RouteAliasData> aliases = data.getRouteAliases();
Assert.assertEquals(1, aliases.size());
RouteAliasData alias = aliases.get(0);
Assert.assertEquals("baz", alias.getTemplate());
return registry;
}
use of com.vaadin.flow.server.VaadinServletContext in project flow by vaadin.
the class RouteRegistryInitializerTest method init.
@Before
public void init() {
pathProvider = Mockito.mock(RoutePathProvider.class);
routeRegistryInitializer = new RouteRegistryInitializer();
registry = new TestRouteRegistry();
servletContext = Mockito.mock(ServletContext.class);
lookup = Mockito.mock(Lookup.class);
Mockito.when(servletContext.getAttribute(Lookup.class.getName())).thenReturn(lookup);
vaadinContext = new VaadinServletContext(servletContext);
registry = ApplicationRouteRegistry.getInstance(vaadinContext);
Mockito.when(vaadinContext.getAttribute(ApplicationRouteRegistry.ApplicationRouteRegistryWrapper.class)).thenReturn(new ApplicationRouteRegistry.ApplicationRouteRegistryWrapper(registry));
Mockito.when(lookup.lookup(RoutePathProvider.class)).thenReturn(pathProvider);
Mockito.doAnswer(invocation -> {
Class clazz = invocation.getArgument(0, Class.class);
Annotation route = clazz.getAnnotation(Route.class);
return ((Route) route).value();
}).when(pathProvider).getRoutePath(Mockito.any());
}
use of com.vaadin.flow.server.VaadinServletContext in project flow by vaadin.
the class VaadinServletContextInitializerTest method errorParameterServletContextListenerEvent_hasCustomRouteNotFoundViewExtendingRouteNotFoundError_customRouteNotFoundViewIsRegistered.
@Test
public void errorParameterServletContextListenerEvent_hasCustomRouteNotFoundViewExtendingRouteNotFoundError_customRouteNotFoundViewIsRegistered() throws Exception {
// given
initDefaultMocks();
VaadinServletContextInitializer initializer = getStubbedVaadinServletContextInitializer();
Runnable when = initRouteNotFoundMocksAndGetContextInitializedMockCall(initializer);
class TestErrorView extends RouteNotFoundError {
}
Mockito.doAnswer(invocation -> Stream.of(TestErrorView.class)).when(initializer).findBySuperType(Mockito.anyCollection(), Mockito.eq(HasErrorParameter.class));
// when
when.run();
// then
ApplicationRouteRegistry registry = ApplicationRouteRegistry.getInstance(new VaadinServletContext(servletContext));
final Class<? extends Component> navigationTarget = registry.getErrorNavigationTarget(new NotFoundException()).get().getNavigationTarget();
Assert.assertEquals(TestErrorView.class, navigationTarget);
}
use of com.vaadin.flow.server.VaadinServletContext in project flow by vaadin.
the class DevModeEndpointTest method setup.
@Before
@SuppressWarnings({ "unchecked", "rawtypes" })
public void setup() throws Exception {
super.setup();
assertFalse("No DevModeHandler should be available at test start", DevModeHandlerManager.getDevModeHandler(new VaadinServletContext(servletContext)).isPresent());
createStubNode(false, true, baseDir);
createStubWebpackServer("Compiled", 500, baseDir, true);
// Prevent TaskRunNpmInstall#cleanUp from deleting node_modules
new File(baseDir, "node_modules/.modules.yaml").createNewFile();
ServletRegistration vaadinServletRegistration = Mockito.mock(ServletRegistration.class);
Mockito.doReturn(new EndpointGeneratorTaskFactoryImpl()).when(lookup).lookup(EndpointGeneratorTaskFactory.class);
ResourceProvider resourceProvider = Mockito.mock(ResourceProvider.class);
Mockito.when(lookup.lookup(ResourceProvider.class)).thenReturn(resourceProvider);
Mockito.when(vaadinServletRegistration.getClassName()).thenReturn(VaadinServletSubClass.class.getName());
classes = new HashSet<>();
classes.add(this.getClass());
Map registry = new HashMap();
// Adding extra registrations to make sure that
// DevModeInitializer picks
// the correct registration which is a VaadinServlet
// registration.
registry.put("extra1", Mockito.mock(ServletRegistration.class));
registry.put("foo", vaadinServletRegistration);
registry.put("extra2", Mockito.mock(ServletRegistration.class));
Mockito.when(servletContext.getServletRegistrations()).thenReturn(registry);
Mockito.when(servletContext.getInitParameterNames()).thenReturn(Collections.emptyEnumeration());
Mockito.when(servletContext.getClassLoader()).thenReturn(this.getClass().getClassLoader());
FileUtils.forceMkdir(new File(baseDir, DEFAULT_CONNECT_JAVA_SOURCE_FOLDER));
devModeStartupListener = new DevModeStartupListener();
}
Aggregations