From 7ccb0b6e66ec3591209bd10c54d26f44a27bd0d8 Mon Sep 17 00:00:00 2001 From: Thomas Risberg Date: Thu, 12 Mar 2009 04:38:56 +0000 Subject: [PATCH] added a concrete GenericSqlQuery class to make it possible to configure in application context (SPR-3986) --- .../jdbc/object/CustomerMapper.java | 20 +++ .../jdbc/object/GenericSqlQueryTests.java | 133 ++++++++++++++++++ .../object/GenericStoredProcedureTests.java | 3 +- .../object/GenericSqlQueryTests-context.xml | 53 +++++++ 4 files changed, 207 insertions(+), 2 deletions(-) create mode 100644 org.springframework.jdbc/src/test/java/org/springframework/jdbc/object/CustomerMapper.java create mode 100644 org.springframework.jdbc/src/test/java/org/springframework/jdbc/object/GenericSqlQueryTests.java create mode 100644 org.springframework.jdbc/src/test/resources/org/springframework/jdbc/object/GenericSqlQueryTests-context.xml diff --git a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/object/CustomerMapper.java b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/object/CustomerMapper.java new file mode 100644 index 0000000000..50455e803a --- /dev/null +++ b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/object/CustomerMapper.java @@ -0,0 +1,20 @@ +package org.springframework.jdbc.object; + +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.Customer; + +import java.sql.ResultSet; +import java.sql.SQLException; + +public class CustomerMapper implements RowMapper { + + private static final String[] COLUMN_NAMES = new String[] {"id", "forename"}; + + public 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; + } + +} diff --git a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/object/GenericSqlQueryTests.java b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/object/GenericSqlQueryTests.java new file mode 100644 index 0000000000..71dca28139 --- /dev/null +++ b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/object/GenericSqlQueryTests.java @@ -0,0 +1,133 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.jdbc.object; + +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.expectLastCall; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Types; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.easymock.EasyMock; +import org.apache.commons.logging.LogFactory; + +import org.springframework.jdbc.AbstractJdbcTests; +import org.springframework.jdbc.Customer; +import org.springframework.jdbc.datasource.TestDataSourceWrapper; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.xml.XmlBeanFactory; +import org.springframework.core.io.ClassPathResource; + +/** + * @author Thomas Risberg + */ +public class GenericSqlQueryTests extends AbstractJdbcTests { + + private static final String SELECT_ID_FORENAME_NAMED_PARAMETERS_PARSED = + "select id, forename from custmr where id = ? and country = ?"; + + private final boolean debugEnabled = LogFactory.getLog(JdbcTemplate.class).isDebugEnabled(); + + private PreparedStatement mockPreparedStatement; + private ResultSet mockResultSet; + + private BeanFactory bf; + + + protected void setUp() throws Exception { + super.setUp(); + mockPreparedStatement = createMock(PreparedStatement.class); + mockResultSet = createMock(ResultSet.class); + this.bf = new XmlBeanFactory( + new ClassPathResource("org/springframework/jdbc/object/GenericSqlQueryTests-context.xml")); + TestDataSourceWrapper testDataSource = (TestDataSourceWrapper) bf.getBean("dataSource"); + testDataSource.setTarget(mockDataSource); + } + + protected void tearDown() throws Exception { + super.tearDown(); + if (shouldVerify()) { + EasyMock.verify(mockPreparedStatement); + EasyMock.verify(mockResultSet); + } + } + + protected void replay() { + super.replay(); + EasyMock.replay(mockPreparedStatement); + EasyMock.replay(mockResultSet); + } + + public void testPlaceHoldersCustomerQuery() throws SQLException { + SqlQuery query = (SqlQuery) bf.getBean("queryWithPlaceHolders"); + testCustomerQuery(query, false); + } + + public void testNamedParameterCustomerQuery() throws SQLException { + SqlQuery query = (SqlQuery) bf.getBean("queryWithNamedParameters"); + testCustomerQuery(query, true); + } + + private void testCustomerQuery(SqlQuery query, boolean namedParameters) throws SQLException { + expect(mockResultSet.next()).andReturn(true); + expect(mockResultSet.getInt("id")).andReturn(1); + expect(mockResultSet.getString("forename")).andReturn("rod"); + expect(mockResultSet.next()).andReturn(false); + mockResultSet.close(); + expectLastCall(); + + mockPreparedStatement.setObject(1, new Integer(1), Types.INTEGER); + expectLastCall(); + mockPreparedStatement.setString(2, "UK"); + expectLastCall(); + expect(mockPreparedStatement.executeQuery()).andReturn(mockResultSet); + if (debugEnabled) { + expect(mockPreparedStatement.getWarnings()).andReturn(null); + } + mockPreparedStatement.close(); + expectLastCall(); + + mockConnection.prepareStatement(SELECT_ID_FORENAME_NAMED_PARAMETERS_PARSED); + ctrlConnection.setReturnValue(mockPreparedStatement); + + replay(); + + List l; + if (namedParameters) { + Map params = new HashMap(2); + params.put("id", new Integer(1)); + params.put("country", "UK"); + l = query.executeByNamedParam(params); + } + else { + Object[] params = new Object[] {new Integer(1), "UK"}; + l = query.execute(params); + } + assertTrue("Customer was returned correctly", l.size() == 1); + Customer cust = (Customer) l.get(0); + assertTrue("Customer id was assigned correctly", cust.getId() == 1); + assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod")); + } + +} \ No newline at end of file diff --git a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/object/GenericStoredProcedureTests.java b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/object/GenericStoredProcedureTests.java index 6c4d19591a..3267a3189f 100644 --- a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/object/GenericStoredProcedureTests.java +++ b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/object/GenericStoredProcedureTests.java @@ -19,7 +19,6 @@ package org.springframework.jdbc.object; import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.expectLastCall; -import static org.easymock.EasyMock.verify; import java.sql.CallableStatement; import java.sql.Types; @@ -64,7 +63,7 @@ public class GenericStoredProcedureTests extends AbstractJdbcTests { protected void tearDown() throws Exception { super.tearDown(); if (shouldVerify()) { - verify(mockCallable); + EasyMock.verify(mockCallable); } } diff --git a/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/object/GenericSqlQueryTests-context.xml b/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/object/GenericSqlQueryTests-context.xml new file mode 100644 index 0000000000..eda2e44809 --- /dev/null +++ b/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/object/GenericSqlQueryTests-context.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file