use of javax.servlet.ServletConfig in project spring-framework by spring-projects.
the class ServletContextAwareProcessorTests method servletContextAwareWithServletContextAndServletConfig.
@Test
public void servletContextAwareWithServletContextAndServletConfig() {
ServletContext servletContext = new MockServletContext();
ServletConfig servletConfig = new MockServletConfig(servletContext);
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletContext, servletConfig);
ServletContextAwareBean bean = new ServletContextAwareBean();
assertNull(bean.getServletContext());
processor.postProcessBeforeInitialization(bean, "testBean");
assertNotNull("ServletContext should have been set", bean.getServletContext());
assertEquals(servletContext, bean.getServletContext());
}
use of javax.servlet.ServletConfig in project spring-framework by spring-projects.
the class ServletContextAwareProcessorTests method servletConfigAwareWithNullServletContextAndNonNullServletConfig.
@Test
public void servletConfigAwareWithNullServletContextAndNonNullServletConfig() {
ServletContext servletContext = new MockServletContext();
ServletConfig servletConfig = new MockServletConfig(servletContext);
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(null, servletConfig);
ServletConfigAwareBean bean = new ServletConfigAwareBean();
assertNull(bean.getServletConfig());
processor.postProcessBeforeInitialization(bean, "testBean");
assertNotNull("ServletConfig should have been set", bean.getServletConfig());
assertEquals(servletConfig, bean.getServletConfig());
}
use of javax.servlet.ServletConfig in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method parameterDispatchingController.
@Test
public void parameterDispatchingController() throws Exception {
final MockServletContext servletContext = new MockServletContext();
final MockServletConfig servletConfig = new MockServletConfig(servletContext);
WebApplicationContext webAppContext = initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext wac) {
wac.setServletContext(servletContext);
AnnotationConfigUtils.registerAnnotationConfigProcessors(wac);
wac.getBeanFactory().registerResolvableDependency(ServletConfig.class, servletConfig);
}
}, MyParameterDispatchingController.class);
MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
MockHttpServletResponse response = new MockHttpServletResponse();
HttpSession session = request.getSession();
getServlet().service(request, response);
assertEquals("myView", response.getContentAsString());
assertSame(servletContext, request.getAttribute("servletContext"));
assertSame(servletConfig, request.getAttribute("servletConfig"));
assertSame(session.getId(), request.getAttribute("sessionId"));
assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
assertSame(request.getLocale(), request.getAttribute("locale"));
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
response = new MockHttpServletResponse();
session = request.getSession();
getServlet().service(request, response);
assertEquals("myView", response.getContentAsString());
assertSame(servletContext, request.getAttribute("servletContext"));
assertSame(servletConfig, request.getAttribute("servletConfig"));
assertSame(session.getId(), request.getAttribute("sessionId"));
assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("view", "other");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("myOtherView", response.getContentAsString());
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("view", "my");
request.addParameter("lang", "de");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("myLangView", response.getContentAsString());
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("surprise", "!");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("mySurpriseView", response.getContentAsString());
MyParameterDispatchingController deserialized = (MyParameterDispatchingController) SerializationTestUtils.serializeAndDeserialize(webAppContext.getBean(MyParameterDispatchingController.class.getSimpleName()));
assertNotNull(deserialized.request);
assertNotNull(deserialized.session);
}
use of javax.servlet.ServletConfig in project swagger-core by swagger-api.
the class ApiListingResourceTest method shouldHandleErrorServletConfig_issue1691.
@Test
public void shouldHandleErrorServletConfig_issue1691() throws JsonProcessingException {
ServletConfig sc = new ServletConfig() {
@Override
public String getServletName() {
throw new RuntimeException("test_1691");
}
@Override
public ServletContext getServletContext() {
throw new RuntimeException("test_1691");
}
@Override
public String getInitParameter(String name) {
throw new RuntimeException("test_1691");
}
@Override
public Enumeration getInitParameterNames() {
throw new RuntimeException("test_1691");
}
};
ApiListingResource a = new ApiListingResource();
try {
a.getListing(null, sc, null, null, "json");
} catch (RuntimeException e) {
// test will fail before, no need to mock Response
if (e.getCause() instanceof ClassNotFoundException) {
return;
}
throw e;
}
}
use of javax.servlet.ServletConfig in project guice by google.
the class ServletDefinitionTest method testServletInitAndConfig.
public final void testServletInitAndConfig() throws ServletException {
Injector injector = createMock(Injector.class);
Binding binding = createMock(Binding.class);
expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
expect(injector.getBinding(Key.get(HttpServlet.class))).andReturn(binding);
final HttpServlet mockServlet = new HttpServlet() {
};
expect(injector.getInstance(Key.get(HttpServlet.class))).andReturn(mockServlet).anyTimes();
replay(injector, binding);
//some init params
//noinspection SSBasedInspection
final Map<String, String> initParams = new ImmutableMap.Builder<String, String>().put("ahsd", "asdas24dok").put("ahssd", "asdasd124ok").build();
String pattern = "/*";
final ServletDefinition servletDefinition = new ServletDefinition(Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, pattern), initParams, null);
ServletContext servletContext = createMock(ServletContext.class);
final String contextName = "thing__!@@44__SRV" + getClass();
expect(servletContext.getServletContextName()).andReturn(contextName);
replay(servletContext);
servletDefinition.init(servletContext, injector, Sets.<HttpServlet>newIdentityHashSet());
assertNotNull(mockServlet.getServletContext());
assertEquals(contextName, mockServlet.getServletContext().getServletContextName());
assertEquals(Key.get(HttpServlet.class).toString(), mockServlet.getServletName());
final ServletConfig servletConfig = mockServlet.getServletConfig();
final Enumeration names = servletConfig.getInitParameterNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
assertTrue(initParams.containsKey(name));
assertEquals(initParams.get(name), servletConfig.getInitParameter(name));
}
verify(injector, binding, servletContext);
}
Aggregations