Search in sources :

Example 11 with ClassPathXmlApplicationContext

use of org.springframework.context.support.ClassPathXmlApplicationContext in project druid by alibaba.

the class SqlMapClientWrapperTest method setUp.

protected void setUp() throws Exception {
    context = new ClassPathXmlApplicationContext("com/alibaba/druid/pool/ibatis/spring-config-ibatis-1.xml");
    DataSource dataSource = (DataSource) context.getBean("dataSource");
    {
        Connection conn = dataSource.getConnection();
        Statement stmt = conn.createStatement();
        stmt.execute("CREATE TABLE sequence_seed (value INTEGER, name VARCHAR(50))");
        stmt.close();
        conn.close();
    }
    {
        Connection conn = dataSource.getConnection();
        Statement stmt = conn.createStatement();
        stmt.execute("CREATE TABLE t_User (id BIGINT, name VARCHAR(50))");
        stmt.close();
        conn.close();
    }
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Statement(java.sql.Statement) Connection(java.sql.Connection) DataSource(javax.sql.DataSource)

Example 12 with ClassPathXmlApplicationContext

use of org.springframework.context.support.ClassPathXmlApplicationContext in project druid by alibaba.

the class DruidStatServiceTest method test_statService_getResetAll.

public void test_statService_getResetAll() throws Exception {
    // data source mock
    String sql = "select 1";
    Connection conn = dataSource.getConnection();
    PreparedStatement stmt = conn.prepareStatement(sql);
    ResultSet rs = stmt.executeQuery();
    rs.next();
    rs.close();
    stmt.close();
    conn.close();
    String resultSQL = DruidStatService.getInstance().service("/sql.json");
    Map<String, Object> resultSQLMap = (Map<String, Object>) JSONUtils.parse(resultSQL);
    List<Map<String, Object>> sqlList = (List<Map<String, Object>>) resultSQLMap.get("Content");
    assertThat(sqlList.size(), equalTo(1));
    Map<String, Object> sqlStat = sqlList.get(0);
    assertThat((Integer) sqlStat.get("RunningCount"), equalTo(0));
    // http request mock
    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 resultWebSession = DruidStatService.getInstance().service("/websession.json");
    Map<String, Object> resultWebSessionMap = (Map<String, Object>) JSONUtils.parse(resultWebSession);
    List<Map<String, Object>> contentWebSessionList = (List<Map<String, Object>>) resultWebSessionMap.get("Content");
    assertThat(contentWebSessionList.size(), equalTo(1));
    Map<String, Object> contentWebSessionMap = contentWebSessionList.get(0);
    assertThat((String) contentWebSessionMap.get("SESSIONID"), equalTo(sessionId));
    // spring mock
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/alibaba/druid/stat/spring-config-stat.xml");
    UserService userService = (UserService) context.getBean("userService");
    userService.save();
    String resultSpring = DruidStatService.getInstance().service("/spring.json");
    Map<String, Object> resultSpringMap = (Map<String, Object>) JSONUtils.parse(resultSpring);
    List<Map<String, Object>> contentSpringList = (List<Map<String, Object>>) resultSpringMap.get("Content");
    assertThat(contentSpringList.size(), equalTo(1));
    Map<String, Object> contentMap = contentSpringList.get(0);
    assertThat((String) contentMap.get("Class"), is(not(nullValue())));
    assertThat((Integer) contentMap.get("ExecuteCount"), equalTo(1));
    // reset all test
    String result = DruidStatService.getInstance().service("/reset-all.json");
    Map<String, Object> resultMap = (Map<String, Object>) JSONUtils.parse(result);
    assertThat(resultMap.get("content"), is(nullValue()));
    // assert sql
    resultSQL = DruidStatService.getInstance().service("/sql.json");
    resultSQLMap = (Map<String, Object>) JSONUtils.parse(resultSQL);
    sqlList = (List<Map<String, Object>>) resultSQLMap.get("Content");
    assertThat(sqlList, is(nullValue()));
    // assert web session
    resultWebSession = DruidStatService.getInstance().service("/websession.json");
    resultWebSessionMap = (Map<String, Object>) JSONUtils.parse(resultWebSession);
    contentWebSessionList = (List<Map<String, Object>>) resultWebSessionMap.get("Content");
    assertThat(contentWebSessionList, is(nullValue()));
    // assert spring
    resultSpring = DruidStatService.getInstance().service("/spring.json");
    resultSpringMap = (Map<String, Object>) JSONUtils.parse(resultSpring);
    contentSpringList = (List<Map<String, Object>>) resultSpringMap.get("Content");
    assertThat(contentSpringList, is(nullValue()));
}
Also used : UserService(com.alibaba.druid.stat.spring.UserService) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) MockServletContext(org.springframework.mock.web.MockServletContext) WebStatFilter(com.alibaba.druid.support.http.WebStatFilter) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ResultSet(java.sql.ResultSet) 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 13 with ClassPathXmlApplicationContext

use of org.springframework.context.support.ClassPathXmlApplicationContext in project druid by alibaba.

the class DruidStatServiceTest method test_statService_getSpring.

public void test_statService_getSpring() throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/alibaba/druid/stat/spring-config-stat.xml");
    UserService userService = (UserService) context.getBean("userService");
    userService.save();
    String result = DruidStatService.getInstance().service("/spring.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("Class"), is(not(nullValue())));
    assertThat((Integer) contentMap.get("ExecuteCount"), equalTo(1));
    // second test
    userService.save();
    result = DruidStatService.getInstance().service("/spring.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((String) contentMap.get("Class"), is(not(nullValue())));
    assertThat((Integer) contentMap.get("ExecuteCount"), equalTo(2));
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) UserService(com.alibaba.druid.stat.spring.UserService) List(java.util.List) Map(java.util.Map)

Example 14 with ClassPathXmlApplicationContext

use of org.springframework.context.support.ClassPathXmlApplicationContext in project druid by alibaba.

the class DruidStatServiceTest method test_statService_getSpringDetail.

public void test_statService_getSpringDetail() throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/alibaba/druid/stat/spring-config-stat.xml");
    UserService userService = (UserService) context.getBean("userService");
    userService.save();
    String result = DruidStatService.getInstance().service("/spring-detail.json?class=com.alibaba.druid.stat.spring.UserService&method=save()");
    Map<String, Object> resultMap = (Map<String, Object>) JSONUtils.parse(result);
    Map<String, Object> contentMap = (Map<String, Object>) resultMap.get("Content");
    assertThat((Integer) contentMap.get("ExecuteCount"), equalTo(1));
    // second test
    userService.save();
    result = DruidStatService.getInstance().service("/spring-detail.json?class=com.alibaba.druid.stat.spring.UserService&method=save()");
    resultMap = (Map<String, Object>) JSONUtils.parse(result);
    contentMap = (Map<String, Object>) resultMap.get("Content");
    assertThat((Integer) contentMap.get("ExecuteCount"), equalTo(2));
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) UserService(com.alibaba.druid.stat.spring.UserService) Map(java.util.Map)

Example 15 with ClassPathXmlApplicationContext

use of org.springframework.context.support.ClassPathXmlApplicationContext in project dubbo by alibaba.

the class ConfigTest method testSystemPropertyOverrideXmlDefault.

@SuppressWarnings("unchecked")
@Test
public void testSystemPropertyOverrideXmlDefault() throws Exception {
    System.setProperty("dubbo.application.name", "sysover");
    System.setProperty("dubbo.application.owner", "sysowner");
    System.setProperty("dubbo.registry.address", "N/A");
    System.setProperty("dubbo.protocol.name", "dubbo");
    System.setProperty("dubbo.protocol.port", "20819");
    ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/system-properties-override-default.xml");
    providerContext.start();
    try {
        ServiceConfig<DemoService> service = (ServiceConfig<DemoService>) providerContext.getBean("demoServiceConfig");
        assertEquals("sysover", service.getApplication().getName());
        assertEquals("sysowner", service.getApplication().getOwner());
        assertEquals("N/A", service.getRegistry().getAddress());
        assertEquals("dubbo", service.getProtocol().getName());
        assertEquals(20819, service.getProtocol().getPort().intValue());
    } finally {
        System.setProperty("dubbo.application.name", "");
        System.setProperty("dubbo.application.owner", "");
        System.setProperty("dubbo.registry.address", "");
        System.setProperty("dubbo.protocol.name", "");
        System.setProperty("dubbo.protocol.port", "");
        providerContext.stop();
        providerContext.close();
    }
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) DemoService(com.alibaba.dubbo.config.spring.api.DemoService) Test(org.junit.Test)

Aggregations

ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)480 Test (org.junit.Test)212 ApplicationContext (org.springframework.context.ApplicationContext)149 Before (org.junit.Before)47 ConfigurableMessenger (org.springframework.scripting.ConfigurableMessenger)33 ITestBean (org.springframework.tests.sample.beans.ITestBean)30 Messenger (org.springframework.scripting.Messenger)29 CamelContext (org.apache.camel.CamelContext)25 Refreshable (org.springframework.aop.target.dynamic.Refreshable)23 AbstractXmlApplicationContext (org.springframework.context.support.AbstractXmlApplicationContext)22 DataSource (javax.sql.DataSource)17 SpringCamelContext (org.apache.camel.spring.SpringCamelContext)16 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)13 AbstractApplicationContext (org.springframework.context.support.AbstractApplicationContext)13 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)12 DemoService (com.alibaba.dubbo.config.spring.api.DemoService)11 ProducerTemplate (org.apache.camel.ProducerTemplate)10 File (java.io.File)9 URL (com.alibaba.dubbo.common.URL)8 Service (org.apache.camel.Service)8