Search in sources :

Example 1 with TestDataSourceWrapper

use of org.springframework.jdbc.datasource.TestDataSourceWrapper in project spring-framework by spring-projects.

the class GenericStoredProcedureTests method testAddInvoices.

@Test
public void testAddInvoices() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource("org/springframework/jdbc/object/GenericStoredProcedureTests-context.xml"));
    Connection connection = mock(Connection.class);
    DataSource dataSource = mock(DataSource.class);
    given(dataSource.getConnection()).willReturn(connection);
    CallableStatement callableStatement = mock(CallableStatement.class);
    TestDataSourceWrapper testDataSource = (TestDataSourceWrapper) bf.getBean("dataSource");
    testDataSource.setTarget(dataSource);
    given(callableStatement.execute()).willReturn(false);
    given(callableStatement.getUpdateCount()).willReturn(-1);
    given(callableStatement.getObject(3)).willReturn(4);
    given(connection.prepareCall("{call " + "add_invoice" + "(?, ?, ?)}")).willReturn(callableStatement);
    StoredProcedure adder = (StoredProcedure) bf.getBean("genericProcedure");
    Map<String, Object> in = new HashMap<>(2);
    in.put("amount", 1106);
    in.put("custid", 3);
    Map<String, Object> out = adder.execute(in);
    Integer id = (Integer) out.get("newid");
    assertEquals(4, id.intValue());
    verify(callableStatement).setObject(1, 1106, Types.INTEGER);
    verify(callableStatement).setObject(2, 3, Types.INTEGER);
    verify(callableStatement).registerOutParameter(3, Types.INTEGER);
    verify(callableStatement).close();
}
Also used : HashMap(java.util.HashMap) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Connection(java.sql.Connection) ClassPathResource(org.springframework.core.io.ClassPathResource) DataSource(javax.sql.DataSource) CallableStatement(java.sql.CallableStatement) TestDataSourceWrapper(org.springframework.jdbc.datasource.TestDataSourceWrapper) Test(org.junit.Test)

Example 2 with TestDataSourceWrapper

use of org.springframework.jdbc.datasource.TestDataSourceWrapper in project spring-framework by spring-projects.

the class GenericSqlQueryTests method setUp.

@Before
public void setUp() throws Exception {
    this.beanFactory = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(new ClassPathResource("org/springframework/jdbc/object/GenericSqlQueryTests-context.xml"));
    DataSource dataSource = mock(DataSource.class);
    this.connection = mock(Connection.class);
    this.preparedStatement = mock(PreparedStatement.class);
    this.resultSet = mock(ResultSet.class);
    given(dataSource.getConnection()).willReturn(connection);
    TestDataSourceWrapper testDataSource = (TestDataSourceWrapper) beanFactory.getBean("dataSource");
    testDataSource.setTarget(dataSource);
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ClassPathResource(org.springframework.core.io.ClassPathResource) DataSource(javax.sql.DataSource) TestDataSourceWrapper(org.springframework.jdbc.datasource.TestDataSourceWrapper) Before(org.junit.Before)

Aggregations

Connection (java.sql.Connection)2 DataSource (javax.sql.DataSource)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 TestDataSourceWrapper (org.springframework.jdbc.datasource.TestDataSourceWrapper)2 CallableStatement (java.sql.CallableStatement)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 HashMap (java.util.HashMap)1 Before (org.junit.Before)1 Test (org.junit.Test)1