Search in sources :

Example 6 with WebStatFilter

use of com.alibaba.druid.support.http.WebStatFilter 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"));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) WebAppStat(com.alibaba.druid.support.http.stat.WebAppStat) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockServletContext(org.springframework.mock.web.MockServletContext) WebStatFilter(com.alibaba.druid.support.http.WebStatFilter) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig)

Example 7 with WebStatFilter

use of com.alibaba.druid.support.http.WebStatFilter 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"));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) WebAppStat(com.alibaba.druid.support.http.stat.WebAppStat) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockServletContext(org.springframework.mock.web.MockServletContext) WebStatFilter(com.alibaba.druid.support.http.WebStatFilter) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig)

Example 8 with WebStatFilter

use of com.alibaba.druid.support.http.WebStatFilter 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();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.MockServletContext) WebStatFilter(com.alibaba.druid.support.http.WebStatFilter) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) MockHttpSession(org.springframework.mock.web.MockHttpSession) List(java.util.List) MockFilterChain(org.springframework.mock.web.MockFilterChain) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 9 with WebStatFilter

use of com.alibaba.druid.support.http.WebStatFilter 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();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) Map(java.util.Map) MockServletContext(org.springframework.mock.web.MockServletContext) WebStatFilter(com.alibaba.druid.support.http.WebStatFilter) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig)

Example 10 with WebStatFilter

use of com.alibaba.druid.support.http.WebStatFilter 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();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) List(java.util.List) MockFilterChain(org.springframework.mock.web.MockFilterChain) Map(java.util.Map) MockServletContext(org.springframework.mock.web.MockServletContext) WebStatFilter(com.alibaba.druid.support.http.WebStatFilter) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig)

Aggregations

WebStatFilter (com.alibaba.druid.support.http.WebStatFilter)20 FilterRegistrationBean (org.springframework.boot.web.servlet.FilterRegistrationBean)12 Bean (org.springframework.context.annotation.Bean)12 ServletRegistrationBean (org.springframework.boot.web.servlet.ServletRegistrationBean)11 MockFilterChain (org.springframework.mock.web.MockFilterChain)8 MockFilterConfig (org.springframework.mock.web.MockFilterConfig)8 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)8 MockServletContext (org.springframework.mock.web.MockServletContext)8 Map (java.util.Map)5 List (java.util.List)4 WebAppStat (com.alibaba.druid.support.http.stat.WebAppStat)3 MockHttpSession (org.springframework.mock.web.MockHttpSession)3 DruidStatProperties (com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties)1 UserService (com.alibaba.druid.stat.spring.UserService)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 HashMap (java.util.HashMap)1 SqlSessionFactoryBean (org.mybatis.spring.SqlSessionFactoryBean)1