Polishing

This commit is contained in:
Juergen Hoeller
2016-08-17 20:43:41 +02:00
parent 52f21bdf54
commit b9a2d0af98
13 changed files with 217 additions and 156 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2016 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.
@@ -18,39 +18,44 @@ package org.springframework.jdbc.object;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.util.Assert;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
public class GenericSqlQuery extends SqlQuery {
/**
* A concrete variant of {@link SqlQuery} which can be configured
* with a {@link RowMapper}.
*
* @author Thomas Risberg
* @author Juergen Hoeller
* @since 3.0
* @see #setRowMapperClass
*/
public class GenericSqlQuery<T> extends SqlQuery<T> {
Class rowMapperClass;
@SuppressWarnings("rawtypes")
private Class<? extends RowMapper> rowMapperClass;
RowMapper rowMapper;
public void setRowMapperClass(Class rowMapperClass)
throws IllegalAccessException, InstantiationException {
/**
* Set a {@link RowMapper} class for this query, creating a fresh
* {@link RowMapper} instance per execution.
*/
@SuppressWarnings("rawtypes")
public void setRowMapperClass(Class<? extends RowMapper> rowMapperClass) {
this.rowMapperClass = rowMapperClass;
if (!RowMapper.class.isAssignableFrom(rowMapperClass))
throw new IllegalStateException("The specified class '" +
rowMapperClass.getName() + " is not a sub class of " +
"'org.springframework.jdbc.core.RowMapper'");
}
public void afterPropertiesSet() {
super.afterPropertiesSet();
Assert.notNull(rowMapperClass, "The 'rowMapperClass' property is required");
Assert.notNull(this.rowMapperClass, "'rowMapperClass' is required");
}
protected RowMapper newRowMapper(Object[] parameters, Map context) {
try {
return (RowMapper) rowMapperClass.newInstance();
}
catch (InstantiationException e) {
throw new InvalidDataAccessResourceUsageException("Unable to instantiate RowMapper", e);
}
catch (IllegalAccessException e) {
throw new InvalidDataAccessResourceUsageException("Unable to instantiate RowMapper", e);
}
@Override
@SuppressWarnings("unchecked")
protected RowMapper<T> newRowMapper(Object[] parameters, Map context) {
return BeanUtils.instantiateClass(this.rowMapperClass);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2016 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.
@@ -16,7 +16,6 @@
package org.springframework.jdbc.object;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@@ -25,13 +24,11 @@ import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
@@ -43,13 +40,14 @@ import static org.mockito.BDDMockito.*;
/**
* @author Thomas Risberg
* @author Juergen Hoeller
*/
public class GenericSqlQueryTests {
public class GenericSqlQueryTests {
private static final String SELECT_ID_FORENAME_NAMED_PARAMETERS_PARSED =
"select id, forename from custmr where id = ? and country = ?";
"select id, forename from custmr where id = ? and country = ?";
private BeanFactory beanFactory;
private DefaultListableBeanFactory beanFactory;
private Connection connection;
@@ -57,10 +55,11 @@ public class GenericSqlQueryTests {
private ResultSet resultSet;
@Before
public void setUp() throws Exception {
this.beanFactory = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader((BeanDefinitionRegistry) this.beanFactory).loadBeanDefinitions(
new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(
new ClassPathResource("org/springframework/jdbc/object/GenericSqlQueryTests-context.xml"));
DataSource dataSource = mock(DataSource.class);
this.connection = mock(Connection.class);
@@ -72,18 +71,18 @@ public class GenericSqlQueryTests {
}
@Test
public void testPlaceHoldersCustomerQuery() throws SQLException {
SqlQuery query = (SqlQuery) beanFactory.getBean("queryWithPlaceHolders");
public void testCustomerQueryWithPlaceholders() throws SQLException {
SqlQuery<?> query = (SqlQuery<?>) beanFactory.getBean("queryWithPlaceholders");
doTestCustomerQuery(query, false);
}
@Test
public void testNamedParameterCustomerQuery() throws SQLException {
SqlQuery query = (SqlQuery) beanFactory.getBean("queryWithNamedParameters");
public void testCustomerQueryWithNamedParameters() throws SQLException {
SqlQuery<?> query = (SqlQuery<?>) beanFactory.getBean("queryWithNamedParameters");
doTestCustomerQuery(query, true);
}
private void doTestCustomerQuery(SqlQuery query, boolean namedParameters) throws SQLException {
private void doTestCustomerQuery(SqlQuery<?> query, boolean namedParameters) throws SQLException {
given(resultSet.next()).willReturn(true);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
@@ -91,15 +90,15 @@ public class GenericSqlQueryTests {
given(preparedStatement.executeQuery()).willReturn(resultSet);
given(connection.prepareStatement(SELECT_ID_FORENAME_NAMED_PARAMETERS_PARSED)).willReturn(preparedStatement);
List queryResults;
List<?> queryResults;
if (namedParameters) {
Map<String, Object> params = new HashMap<String, Object>(2);
params.put("id", new Integer(1));
params.put("id", 1);
params.put("country", "UK");
queryResults = query.executeByNamedParam(params);
}
else {
Object[] params = new Object[] {new Integer(1), "UK"};
Object[] params = new Object[] {1, "UK"};
queryResults = query.execute(params);
}
assertTrue("Customer was returned correctly", queryResults.size() == 1);
@@ -108,7 +107,7 @@ public class GenericSqlQueryTests {
assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
verify(resultSet).close();
verify(preparedStatement).setObject(1, new Integer(1), Types.INTEGER);
verify(preparedStatement).setObject(1, 1, Types.INTEGER);
verify(preparedStatement).setString(2, "UK");
verify(preparedStatement).close();
}

View File

@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.TestDataSourceWrapper"/>
<bean id="queryWithPlaceHolders" class="org.springframework.jdbc.object.GenericSqlQuery">
<bean id="queryWithPlaceholders" class="org.springframework.jdbc.object.GenericSqlQuery">
<property name="dataSource" ref="dataSource"/>
<property name="sql" value="select id, forename from custmr where id = ? and country = ?"/>
<property name="parameters">
@@ -15,7 +15,7 @@
<constructor-arg index="0" value="amount"/>
<constructor-arg index="1">
<util:constant static-field="java.sql.Types.INTEGER"/>
</constructor-arg>
</constructor-arg>
</bean>
<bean class="org.springframework.jdbc.core.SqlParameter">
<constructor-arg index="0" value="custid"/>
@@ -47,7 +47,7 @@
</bean>
</list>
</property>
<property name="rowMapperClass" value="org.springframework.jdbc.object.CustomerMapper"/>
<property name="rowMapperClass" value="org.springframework.jdbc.object.CustomerMapper"/>
</bean>
</beans>