use of com.vaadin.flow.server.VaadinServletContext in project flow by vaadin.
the class AbstractUIScopedTest method mockUI.
protected UI mockUI() {
VaadinSession session = mockSession();
Router router = mock(Router.class);
VaadinService service = session.getService();
when(service.getRouter()).thenReturn(router);
Properties initParameters = new Properties();
ApplicationConfiguration appConfig = Mockito.mock(ApplicationConfiguration.class);
Mockito.when(appConfig.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
when(service.getMainDivId(Mockito.any(), Mockito.any())).thenReturn(" - ");
final Map<String, Object> attributeMap = new HashMap<>();
ServletContext servletContext = Mockito.mock(ServletContext.class);
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());
VaadinServletContext context = new VaadinServletContext(servletContext);
Mockito.when(service.getContext()).thenReturn(context);
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(service.getDeploymentConfiguration()).thenReturn(config);
ui = new UI();
ui.getInternals().setSession(session);
ui.doInit(null, 1);
UI.setCurrent(ui);
return ui;
}
use of com.vaadin.flow.server.VaadinServletContext in project flow by vaadin.
the class VaadinRouteScopeTest method mockServletContext.
private void mockServletContext(UI ui) {
VaadinService service = ui.getSession().getService();
VaadinServletContext context = ((VaadinServletContext) service.getContext());
ServletContext servletContext = context.getContext();
WebApplicationContext appContext = Mockito.mock(WebApplicationContext.class);
Mockito.when(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)).thenReturn(appContext);
}
use of com.vaadin.flow.server.VaadinServletContext in project flow by vaadin.
the class VaadinServletContextInitializerTest method errorParameterServletContextListenerEvent_hasCustomRouteNotFoundViewImplementingHasErrorParameter_customRouteNotFoundViewIsRegistered.
@Test
public void errorParameterServletContextListenerEvent_hasCustomRouteNotFoundViewImplementingHasErrorParameter_customRouteNotFoundViewIsRegistered() throws Exception {
// given
initDefaultMocks();
VaadinServletContextInitializer initializer = getStubbedVaadinServletContextInitializer();
Runnable when = initRouteNotFoundMocksAndGetContextInitializedMockCall(initializer);
class TestErrorView extends Component implements HasErrorParameter<NotFoundException> {
@Override
public int setErrorParameter(BeforeEnterEvent event, ErrorParameter<NotFoundException> parameter) {
return 0;
}
}
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 UidlWriterTest method initializeUIForDependenciesTest.
private UI initializeUIForDependenciesTest(UI ui) throws Exception {
mocks = new MockServletServiceSessionSetup();
VaadinServletContext context = (VaadinServletContext) mocks.getService().getContext();
Lookup lookup = context.getAttribute(Lookup.class);
Mockito.when(lookup.lookup(RoutePathProvider.class)).thenReturn(new RoutePathProviderImpl());
VaadinSession session = mocks.getSession();
session.lock();
ui.getInternals().setSession(session);
RouteConfiguration routeConfiguration = RouteConfiguration.forRegistry(ui.getInternals().getRouter().getRegistry());
routeConfiguration.update(() -> {
routeConfiguration.getHandledRegistry().clean();
routeConfiguration.setAnnotatedRoute(BaseClass.class);
});
for (String type : new String[] { "html", "js", "css" }) {
mocks.getServlet().addServletContextResource("inline." + type, "inline." + type);
}
HttpServletRequest servletRequestMock = mock(HttpServletRequest.class);
VaadinServletRequest vaadinRequestMock = mock(VaadinServletRequest.class);
when(vaadinRequestMock.getHttpServletRequest()).thenReturn(servletRequestMock);
ui.doInit(vaadinRequestMock, 1);
ui.getInternals().getRouter().initializeUI(ui, BootstrapHandlerTest.requestToLocation(vaadinRequestMock));
return ui;
}
use of com.vaadin.flow.server.VaadinServletContext in project flow by vaadin.
the class ClassLoaderAwareServletContainerInitializer method onStartup.
/**
* Overridden to use different classloaders if needed.
* <p>
* {@inheritDoc}
*/
@Override
default void onStartup(Set<Class<?>> set, ServletContext context) throws ServletException {
// see DeferredServletContextIntializers
DeferredServletContextInitializers.Initializer deferredInitializer = ctx -> {
ClassLoader webClassLoader = ctx.getClassLoader();
ClassLoader classLoader = getClass().getClassLoader();
/*
* Hack is needed to make a workaround for weird behavior of WildFly
* with skinnywar See https://github.com/vaadin/flow/issues/7805
*/
boolean noHack = false;
while (classLoader != null) {
if (classLoader.equals(webClassLoader)) {
noHack = true;
break;
} else {
/*
* The classloader which has loaded this class ({@code
* classLoader}) should be either the {@code webClassLoader}
* or its child: in this case it knows how to handle the
* classes loaded by the {@code webClassLoader} : it either
* is able to load them itself or delegate to its parent
* (which is the {@code webClassLoader}): in this case hack
* is not needed and the {@link #process(Set,
* ServletContext)} method can be called directly.
*/
classLoader = classLoader.getParent();
}
}
if (noHack) {
process(set, ctx);
return;
}
try {
Class<?> initializer = ctx.getClassLoader().loadClass(getClass().getName());
String processMethodName = Stream.of(ClassLoaderAwareServletContainerInitializer.class.getDeclaredMethods()).filter(method -> !method.isDefault() && !method.isSynthetic()).findFirst().get().getName();
Method operation = Stream.of(initializer.getMethods()).filter(method -> method.getName().equals(processMethodName)).findFirst().get();
operation.invoke(initializer.newInstance(), new Object[] { set, ctx });
} catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
throw new ServletException(e);
}
};
if (requiresLookup()) {
VaadinServletContext vaadinContext = new VaadinServletContext(context);
synchronized (context) {
if (vaadinContext.getAttribute(Lookup.class) == null) {
DeferredServletContextInitializers initializers = vaadinContext.getAttribute(DeferredServletContextInitializers.class, () -> new DeferredServletContextInitializers());
initializers.addInitializer(ctx -> deferredInitializer.init(ctx));
return;
}
}
}
deferredInitializer.init(context);
}
Aggregations