Search in sources :

Example 86 with DataSource

use of javax.sql.DataSource in project spring-framework by spring-projects.

the class JdbcDaoSupportTests method testJdbcDaoSupportWithDataSource.

@Test
public void testJdbcDaoSupportWithDataSource() throws Exception {
    DataSource ds = mock(DataSource.class);
    final List<String> test = new ArrayList<>();
    JdbcDaoSupport dao = new JdbcDaoSupport() {

        @Override
        protected void initDao() {
            test.add("test");
        }
    };
    dao.setDataSource(ds);
    dao.afterPropertiesSet();
    assertEquals("Correct DataSource", ds, dao.getDataSource());
    assertEquals("Correct JdbcTemplate", ds, dao.getJdbcTemplate().getDataSource());
    assertEquals("initDao called", test.size(), 1);
}
Also used : ArrayList(java.util.ArrayList) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 87 with DataSource

use of javax.sql.DataSource in project spring-framework by spring-projects.

the class RdbmsOperationTests method parameterPropagation.

@Test
public void parameterPropagation() {
    SqlOperation operation = new SqlOperation() {
    };
    DataSource ds = new DriverManagerDataSource();
    operation.setDataSource(ds);
    operation.setFetchSize(10);
    operation.setMaxRows(20);
    JdbcTemplate jt = operation.getJdbcTemplate();
    assertEquals(ds, jt.getDataSource());
    assertEquals(10, jt.getFetchSize());
    assertEquals(20, jt.getMaxRows());
}
Also used : DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataSource(javax.sql.DataSource) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) Test(org.junit.Test)

Example 88 with DataSource

use of javax.sql.DataSource in project spring-framework by spring-projects.

the class SqlQueryTests method testFancyCustomerQuery.

@Test
public void testFancyCustomerQuery() throws SQLException {
    given(resultSet.next()).willReturn(true, false);
    given(resultSet.getInt("id")).willReturn(1);
    given(resultSet.getString("forename")).willReturn("rod");
    given(connection.prepareStatement(SELECT_ID_FORENAME_WHERE, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY)).willReturn(preparedStatement);
    class CustomerQuery extends MappingSqlQuery<Customer> {

        public CustomerQuery(DataSource ds) {
            super(ds, SELECT_ID_FORENAME_WHERE);
            setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
            declareParameter(new SqlParameter(Types.NUMERIC));
            compile();
        }

        @Override
        protected Customer mapRow(ResultSet rs, int rownum) throws SQLException {
            Customer cust = new Customer();
            cust.setId(rs.getInt(COLUMN_NAMES[0]));
            cust.setForename(rs.getString(COLUMN_NAMES[1]));
            return cust;
        }

        public Customer findCustomer(int id) {
            return findObject(id);
        }
    }
    CustomerQuery query = new CustomerQuery(dataSource);
    Customer cust = query.findCustomer(1);
    assertTrue("Customer id was assigned correctly", cust.getId() == 1);
    assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
    verify(preparedStatement).setObject(1, 1, Types.NUMERIC);
    verify(resultSet).close();
    verify(preparedStatement).close();
    verify(connection).close();
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) Customer(org.springframework.jdbc.Customer) ResultSet(java.sql.ResultSet) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 89 with DataSource

use of javax.sql.DataSource in project spring-framework by spring-projects.

the class SqlQueryTests method testFindCustomerMixed.

@Test
public void testFindCustomerMixed() throws SQLException {
    reset(connection);
    PreparedStatement preparedStatement2 = mock(PreparedStatement.class);
    ResultSet resultSet2 = mock(ResultSet.class);
    given(preparedStatement2.executeQuery()).willReturn(resultSet2);
    given(resultSet.next()).willReturn(true, false);
    given(resultSet.getInt("id")).willReturn(1);
    given(resultSet.getString("forename")).willReturn("rod");
    given(resultSet2.next()).willReturn(false);
    given(connection.prepareStatement(SELECT_ID_WHERE)).willReturn(preparedStatement, preparedStatement2);
    class CustomerQuery extends MappingSqlQuery<Customer> {

        public CustomerQuery(DataSource ds) {
            super(ds, SELECT_ID_WHERE);
            declareParameter(new SqlParameter(COLUMN_NAMES[0], COLUMN_TYPES[0]));
            declareParameter(new SqlParameter(COLUMN_NAMES[1], COLUMN_TYPES[1]));
            compile();
        }

        @Override
        protected Customer mapRow(ResultSet rs, int rownum) throws SQLException {
            Customer cust = new Customer();
            cust.setId(rs.getInt(COLUMN_NAMES[0]));
            cust.setForename(rs.getString(COLUMN_NAMES[1]));
            return cust;
        }

        public Customer findCustomer(int id, String name) {
            return findObject(new Object[] { id, name });
        }
    }
    CustomerQuery query = new CustomerQuery(dataSource);
    Customer cust1 = query.findCustomer(1, "rod");
    assertTrue("Found customer", cust1 != null);
    assertTrue("Customer id was assigned correctly", cust1.getId() == 1);
    Customer cust2 = query.findCustomer(1, "Roger");
    assertTrue("No customer found", cust2 == null);
    verify(preparedStatement).setObject(1, 1, Types.INTEGER);
    verify(preparedStatement).setString(2, "rod");
    verify(preparedStatement2).setObject(1, 1, Types.INTEGER);
    verify(preparedStatement2).setString(2, "Roger");
    verify(resultSet).close();
    verify(resultSet2).close();
    verify(preparedStatement).close();
    verify(preparedStatement2).close();
    verify(connection, times(2)).close();
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) Customer(org.springframework.jdbc.Customer) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 90 with DataSource

use of javax.sql.DataSource in project spring-framework by spring-projects.

the class SqlQueryTests method testFindCustomerIntInt.

@Test
public void testFindCustomerIntInt() throws SQLException {
    given(resultSet.next()).willReturn(true, false);
    given(resultSet.getInt("id")).willReturn(1);
    given(resultSet.getString("forename")).willReturn("rod");
    class CustomerQuery extends MappingSqlQuery<Customer> {

        public CustomerQuery(DataSource ds) {
            super(ds, SELECT_ID_WHERE);
            declareParameter(new SqlParameter(Types.NUMERIC));
            declareParameter(new SqlParameter(Types.NUMERIC));
            compile();
        }

        @Override
        protected Customer mapRow(ResultSet rs, int rownum) throws SQLException {
            Customer cust = new Customer();
            cust.setId(rs.getInt(COLUMN_NAMES[0]));
            cust.setForename(rs.getString(COLUMN_NAMES[1]));
            return cust;
        }

        public Customer findCustomer(int id, int otherNum) {
            return findObject(id, otherNum);
        }
    }
    CustomerQuery query = new CustomerQuery(dataSource);
    Customer cust = query.findCustomer(1, 1);
    assertTrue("Customer id was assigned correctly", cust.getId() == 1);
    assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
    verify(preparedStatement).setObject(1, 1, Types.NUMERIC);
    verify(preparedStatement).setObject(2, 1, Types.NUMERIC);
    verify(connection).prepareStatement(SELECT_ID_WHERE);
    verify(resultSet).close();
    verify(preparedStatement).close();
    verify(connection).close();
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) Customer(org.springframework.jdbc.Customer) ResultSet(java.sql.ResultSet) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Aggregations

DataSource (javax.sql.DataSource)546 Connection (java.sql.Connection)200 Test (org.junit.Test)192 SQLException (java.sql.SQLException)118 Context (javax.naming.Context)70 ResultSet (java.sql.ResultSet)59 Statement (java.sql.Statement)59 NamingException (javax.naming.NamingException)57 InitialContext (javax.naming.InitialContext)55 EJBException (javax.ejb.EJBException)40 HashMap (java.util.HashMap)38 PreparedStatement (java.sql.PreparedStatement)37 Properties (java.util.Properties)35 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)34 RemoteException (java.rmi.RemoteException)32 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)31 UserTransaction (javax.transaction.UserTransaction)30 IOException (java.io.IOException)29 ArrayList (java.util.ArrayList)26 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)21