use of org.springframework.mock.web.MockFilterChain in project druid by alibaba.
the class DruidStatServiceTest method test_statService_getWebURIById.
public void test_statService_getWebURIById() throws Exception {
String uri = "/";
MockServletContext servletContext = new MockServletContext();
MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
WebStatFilter filter = new WebStatFilter();
filter.init(filterConfig);
// first request test
MockHttpServletRequest request = new MockHttpServletRequest("GET", uri);
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain();
filter.doFilter(request, response, chain);
String result = DruidStatService.getInstance().service("/weburi-" + uri + ".json");
Map<String, Object> resultMap = (Map<String, Object>) JSONUtils.parse(result);
Map<String, Object> contentMap = (Map<String, Object>) resultMap.get("Content");
assertThat((Integer) contentMap.get("RequestCount"), equalTo(1));
// second request test
MockHttpServletRequest request2 = new MockHttpServletRequest("GET", uri);
MockHttpServletResponse response2 = new MockHttpServletResponse();
MockFilterChain chain2 = new MockFilterChain();
filter.doFilter(request2, response2, chain2);
result = DruidStatService.getInstance().service("/weburi-" + uri + ".json");
resultMap = (Map<String, Object>) JSONUtils.parse(result);
contentMap = (Map<String, Object>) resultMap.get("Content");
assertThat((Integer) contentMap.get("RequestCount"), equalTo(2));
filter.destroy();
}
use of org.springframework.mock.web.MockFilterChain in project druid by alibaba.
the class DruidStatServiceTest method test_statService_getWebApp.
public void test_statService_getWebApp() throws Exception {
String uri = "/";
MockServletContext servletContext = new MockServletContext();
MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
WebStatFilter filter = new WebStatFilter();
filter.init(filterConfig);
// first request test
MockHttpServletRequest request = new MockHttpServletRequest("GET", uri);
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain();
filter.doFilter(request, response, chain);
String result = DruidStatService.getInstance().service("/webapp.json");
Map<String, Object> resultMap = (Map<String, Object>) JSONUtils.parse(result);
List<Map<String, Object>> contentList = (List<Map<String, Object>>) resultMap.get("Content");
assertThat(contentList.size(), equalTo(1));
Map<String, Object> contentMap = contentList.get(0);
assertThat((Integer) contentMap.get("RequestCount"), equalTo(1));
// second request test
MockHttpServletRequest request2 = new MockHttpServletRequest("GET", uri);
MockHttpServletResponse response2 = new MockHttpServletResponse();
MockFilterChain chain2 = new MockFilterChain();
filter.doFilter(request2, response2, chain2);
result = DruidStatService.getInstance().service("/webapp.json");
resultMap = (Map<String, Object>) JSONUtils.parse(result);
contentList = (List<Map<String, Object>>) resultMap.get("Content");
assertThat(contentList.size(), equalTo(1));
contentMap = contentList.get(0);
assertThat((Integer) contentMap.get("RequestCount"), equalTo(2));
filter.destroy();
}
use of org.springframework.mock.web.MockFilterChain in project druid by alibaba.
the class DruidStatServiceTest method test_statService_getWebSession.
public void test_statService_getWebSession() throws Exception {
String uri = "/";
MockServletContext servletContext = new MockServletContext();
MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
WebStatFilter filter = new WebStatFilter();
filter.init(filterConfig);
// first request test
MockHttpServletRequest request = new MockHttpServletRequest("GET", uri);
MockHttpSession session = new MockHttpSession();
request.setSession(session);
String sessionId = session.getId();
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain();
filter.doFilter(request, response, chain);
String result = DruidStatService.getInstance().service("/websession.json");
Map<String, Object> resultMap = (Map<String, Object>) JSONUtils.parse(result);
List<Map<String, Object>> contentList = (List<Map<String, Object>>) resultMap.get("Content");
assertThat(contentList.size(), equalTo(1));
Map<String, Object> contentMap = contentList.get(0);
assertThat((String) contentMap.get("SESSIONID"), equalTo(sessionId));
// second request test
MockHttpServletRequest request2 = new MockHttpServletRequest("GET", uri);
MockHttpSession session2 = new MockHttpSession();
request2.setSession(session2);
String sessionId2 = session2.getId();
MockHttpServletResponse response2 = new MockHttpServletResponse();
MockFilterChain chain2 = new MockFilterChain();
filter.doFilter(request2, response2, chain2);
result = DruidStatService.getInstance().service("/websession.json");
resultMap = (Map<String, Object>) JSONUtils.parse(result);
contentList = (List<Map<String, Object>>) resultMap.get("Content");
assertThat(contentList.size(), equalTo(2));
contentMap = contentList.get(1);
assertThat((String) contentMap.get("SESSIONID"), equalTo(sessionId2));
filter.destroy();
}
use of org.springframework.mock.web.MockFilterChain in project druid by alibaba.
the class WebStatFilterTest 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();
Assert.assertNull(filter.getWebAppStat());
filter.init(filterConfig);
WebAppStat 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.MockFilterChain 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());
}
}
Aggregations