use of org.springframework.mock.web.MockFilterConfig in project druid by alibaba.
the class WebStatFilterTest2 method test_lru.
public void test_lru() throws Exception {
MockServletContext servletContext = new MockServletContext();
MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
filterConfig.addInitParameter(WebStatFilter.PARAM_NAME_SESSION_STAT_MAX_COUNT, "3");
WebStatFilter filter = new WebStatFilter();
filter.init(filterConfig);
WebAppStat appStat = filter.getWebAppStat();
Assert.assertEquals(3, filter.getSessionStatMaxCount());
Assert.assertEquals(0, appStat.getSessionStatDataList().size());
final MockHttpSession session_0 = new MockHttpSession(servletContext);
final MockHttpSession session_1 = new MockHttpSession(servletContext);
final MockHttpSession session_2 = new MockHttpSession(servletContext);
final MockHttpSession session_3 = new MockHttpSession(servletContext);
final MockHttpSession session_4 = new MockHttpSession(servletContext);
// 第一个session请求2次
{
Assert.assertNull(appStat.getSessionStat(session_0.getId()));
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain() {
public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) {
((MockHttpServletRequest) request).setSession(session_0);
}
;
};
filter.doFilter(request, response, chain);
Assert.assertEquals(1, appStat.getSessionStatDataList().size());
Assert.assertEquals(1, appStat.getSessionStat(session_0.getId()).getRequestCount());
Assert.assertTrue(appStat.getSessionStat(session_0.getId()).getLastAccessTimeMillis() > 0);
}
{
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain() {
public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) {
((MockHttpServletRequest) request).setSession(session_0);
}
;
};
filter.doFilter(request, response, chain);
Assert.assertEquals(1, appStat.getSessionStatDataList().size());
Assert.assertEquals(2, appStat.getSessionStat(session_0.getId()).getRequestCount());
}
// 第2个sesion请求1次
{
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain() {
public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) {
((MockHttpServletRequest) request).setSession(session_1);
}
;
};
filter.doFilter(request, response, chain);
Assert.assertEquals(2, appStat.getSessionStatDataList().size());
Assert.assertEquals(2, appStat.getSessionStat(session_0.getId()).getRequestCount());
Assert.assertEquals(1, appStat.getSessionStat(session_1.getId()).getRequestCount());
}
// 第3个sesion请求1次
{
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain() {
public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) {
((MockHttpServletRequest) request).setSession(session_2);
}
;
};
filter.doFilter(request, response, chain);
Assert.assertEquals(3, appStat.getSessionStatDataList().size());
Assert.assertEquals(2, appStat.getSessionStat(session_0.getId()).getRequestCount());
Assert.assertEquals(1, appStat.getSessionStat(session_1.getId()).getRequestCount());
Assert.assertEquals(1, appStat.getSessionStat(session_2.getId()).getRequestCount());
}
// 第4个sesion请求1次
{
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain() {
public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) {
((MockHttpServletRequest) request).setSession(session_3);
}
;
};
filter.doFilter(request, response, chain);
Assert.assertEquals(3, appStat.getSessionStatDataList().size());
Assert.assertNull(appStat.getSessionStat(session_0.getId()));
Assert.assertEquals(1, appStat.getSessionStat(session_1.getId()).getRequestCount());
Assert.assertEquals(1, appStat.getSessionStat(session_2.getId()).getRequestCount());
Assert.assertEquals(1, appStat.getSessionStat(session_3.getId()).getRequestCount());
}
// 第5个sesion请求1次
{
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain() {
public void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) {
((MockHttpServletRequest) request).setSession(session_4);
}
;
};
filter.doFilter(request, response, chain);
Assert.assertEquals(3, appStat.getSessionStatDataList().size());
Assert.assertNull(appStat.getSessionStat(session_0.getId()));
Assert.assertNull(appStat.getSessionStat(session_1.getId()));
Assert.assertEquals(1, appStat.getSessionStat(session_2.getId()).getRequestCount());
Assert.assertEquals(1, appStat.getSessionStat(session_3.getId()).getRequestCount());
Assert.assertEquals(1, appStat.getSessionStat(session_4.getId()).getRequestCount());
}
}
use of org.springframework.mock.web.MockFilterConfig in project druid by alibaba.
the class WebStatFilterTest3_WebURIStatNull method test_sessionStatDisable.
public void test_sessionStatDisable() throws Exception {
MockServletContext servletContext = new MockServletContext();
MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
filterConfig.addInitParameter(WebStatFilter.PARAM_NAME_SESSION_STAT_ENABLE, "false");
WebStatFilter filter = new WebStatFilter();
WebAppStat appStat = new WebAppStat() {
public WebURIStat getURIStat(String uri, boolean create) {
return null;
}
};
filter.setWebAppStat(appStat);
filter.setProfileEnable(true);
Assert.assertNotNull(filter.getWebAppStat());
filter.init(filterConfig);
Assert.assertSame(appStat, filter.getWebAppStat());
Assert.assertFalse(filter.isSessionStatEnable());
Assert.assertTrue(WebAppStatManager.getInstance().getWebAppStatSet().contains(appStat));
Assert.assertTrue(StatFilterContext.getInstance().getListeners().contains(filter.getStatFilterContextListener()));
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain();
Assert.assertNull(filter.getSessionStat(request));
filter.doFilter(request, response, chain);
Assert.assertEquals(0, appStat.getSessionStatDataList().size());
filter.destroy();
Assert.assertFalse(WebAppStatManager.getInstance().getWebAppStatSet().contains(appStat));
Assert.assertFalse(StatFilterContext.getInstance().getListeners().contains(filter.getStatFilterContextListener()));
Map<String, Object> statData = appStat.getStatData();
Assert.assertEquals(1L, statData.get("RequestCount"));
}
use of org.springframework.mock.web.MockFilterConfig in project spring-boot by spring-projects.
the class ErrorPageFilterTests method oncePerRequest.
@Test
public void oncePerRequest() throws Exception {
this.chain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
((HttpServletResponse) response).sendError(400, "BAD");
assertThat(request.getAttribute("FILTER.FILTERED")).isNotNull();
super.doFilter(request, response);
}
};
this.filter.init(new MockFilterConfig("FILTER"));
this.filter.doFilter(this.request, this.response, this.chain);
}
use of org.springframework.mock.web.MockFilterConfig in project spring-boot by spring-projects.
the class ServletWebServerApplicationContextTests method delegatingFilterProxyRegistrationBeansSkipsTargetBeanNames.
@Test
public void delegatingFilterProxyRegistrationBeansSkipsTargetBeanNames() throws Exception {
addWebServerFactoryBean();
DelegatingFilterProxyRegistrationBean initializer = new DelegatingFilterProxyRegistrationBean("filterBean");
this.context.registerBeanDefinition("initializerBean", beanDefinition(initializer));
BeanDefinition filterBeanDefinition = beanDefinition(new IllegalStateException("Create FilterBean Failure"));
filterBeanDefinition.setLazyInit(true);
this.context.registerBeanDefinition("filterBean", filterBeanDefinition);
this.context.refresh();
ServletContext servletContext = getWebServerFactory().getServletContext();
verify(servletContext, atMost(1)).addFilter(anyString(), this.filterCaptor.capture());
// Up to this point the filterBean should not have been created, calling
// the delegate proxy will trigger creation and an exception
this.thrown.expect(BeanCreationException.class);
this.thrown.expectMessage("Create FilterBean Failure");
this.filterCaptor.getValue().init(new MockFilterConfig());
this.filterCaptor.getValue().doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), new MockFilterChain());
}
use of org.springframework.mock.web.MockFilterConfig in project opennms by OpenNMS.
the class AbstractSpringJerseyRestTestCase method setUp.
@Before
public void setUp() throws Throwable {
beforeServletStart();
setUser("admin", new String[] { "ROLE_ADMIN" });
DaoTestConfigBean bean = new DaoTestConfigBean();
bean.afterPropertiesSet();
MockDatabase db = new MockDatabase(true);
DataSourceFactory.setInstance(db);
XADataSourceFactory.setInstance(db);
try {
MockFilterConfig filterConfig = new MockFilterConfig(servletContext, "openSessionInViewFilter");
setFilter(new OpenSessionInViewFilter());
getFilter().init(filterConfig);
// Jersey
/*
setServletConfig(new MockServletConfig(servletContext, "dispatcher"));
getServletConfig().addInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
getServletConfig().addInitParameter("com.sun.jersey.config.property.packages", "org.codehaus.jackson.jaxrs;org.opennms.web.rest;org.opennms.web.rest.config");
getServletConfig().addInitParameter("com.sun.jersey.spi.container.ContainerRequestFilters", "com.sun.jersey.api.container.filter.GZIPContentEncodingFilter");
getServletConfig().addInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters", "com.sun.jersey.api.container.filter.GZIPContentEncodingFilter");
setDispatcher(new SpringServlet());
getDispatcher().init(getServletConfig());
*/
// Apache CXF
setServletConfig(new MockServletConfig(servletContext, "dispatcher"));
getServletConfig().addInitParameter("config-location", m_cxfContextPath);
CXFServlet servlet = new CXFServlet();
setDispatcher(servlet);
getDispatcher().init(getServletConfig());
} catch (ServletException se) {
throw se.getRootCause();
}
afterServletStart();
System.err.println("------------------------------------------------------------------------------");
}
Aggregations