Browse Source
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@438 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/head
31 changed files with 108 additions and 741 deletions
@ -1,85 +0,0 @@
@@ -1,85 +0,0 @@
|
||||
/* |
||||
* AbstractJdbcTests.java |
||||
* |
||||
* Copyright (C) 2002 by Interprise Software. All rights reserved. |
||||
*/ |
||||
/* |
||||
* Copyright 2002-2005 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; |
||||
|
||||
import java.sql.Connection; |
||||
|
||||
import javax.sql.DataSource; |
||||
|
||||
import junit.framework.TestCase; |
||||
import org.easymock.MockControl; |
||||
|
||||
/** |
||||
* @author Trevor D. Cook |
||||
*/ |
||||
public abstract class AbstractJdbcTests extends TestCase { |
||||
|
||||
protected MockControl ctrlDataSource; |
||||
protected DataSource mockDataSource; |
||||
protected MockControl ctrlConnection; |
||||
protected Connection mockConnection; |
||||
|
||||
/** |
||||
* Set to true if the user wants verification, indicated |
||||
* by a call to replay(). We need to make this optional, |
||||
* otherwise we setUp() will always result in verification failures |
||||
*/ |
||||
private boolean shouldVerify; |
||||
|
||||
protected void setUp() throws Exception { |
||||
this.shouldVerify = false; |
||||
super.setUp(); |
||||
|
||||
ctrlConnection = MockControl.createControl(Connection.class); |
||||
mockConnection = (Connection) ctrlConnection.getMock(); |
||||
mockConnection.getMetaData(); |
||||
ctrlConnection.setDefaultReturnValue(null); |
||||
mockConnection.close(); |
||||
ctrlConnection.setDefaultVoidCallable(); |
||||
|
||||
ctrlDataSource = MockControl.createControl(DataSource.class); |
||||
mockDataSource = (DataSource) ctrlDataSource.getMock(); |
||||
mockDataSource.getConnection(); |
||||
ctrlDataSource.setDefaultReturnValue(mockConnection); |
||||
} |
||||
|
||||
protected void replay() { |
||||
ctrlDataSource.replay(); |
||||
ctrlConnection.replay(); |
||||
this.shouldVerify = true; |
||||
} |
||||
|
||||
protected void tearDown() throws Exception { |
||||
super.tearDown(); |
||||
|
||||
// we shouldn't verify unless the user called replay()
|
||||
if (shouldVerify()) { |
||||
ctrlDataSource.verify(); |
||||
//ctrlConnection.verify();
|
||||
} |
||||
} |
||||
|
||||
protected boolean shouldVerify() { |
||||
return this.shouldVerify; |
||||
} |
||||
|
||||
} |
||||
@ -1,50 +0,0 @@
@@ -1,50 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2007 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; |
||||
|
||||
/** |
||||
* @author Juergen Hoeller |
||||
*/ |
||||
public class Customer { |
||||
|
||||
private int id; |
||||
|
||||
private String forename; |
||||
|
||||
|
||||
public int getId() { |
||||
return id; |
||||
} |
||||
|
||||
public void setId(int id) { |
||||
this.id = id; |
||||
} |
||||
|
||||
public String getForename() { |
||||
return forename; |
||||
} |
||||
|
||||
public void setForename(String forename) { |
||||
this.forename = forename; |
||||
} |
||||
|
||||
|
||||
public String toString() { |
||||
return "Customer: id=" + id + "; forename=" + forename; |
||||
} |
||||
|
||||
} |
||||
@ -1,145 +0,0 @@
@@ -1,145 +0,0 @@
|
||||
/* |
||||
* 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.core; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.sql.Connection; |
||||
import java.sql.ResultSet; |
||||
import java.sql.ResultSetMetaData; |
||||
import java.sql.SQLException; |
||||
import java.sql.Statement; |
||||
import java.sql.Timestamp; |
||||
import java.sql.Types; |
||||
|
||||
import junit.framework.TestCase; |
||||
import org.easymock.MockControl; |
||||
import org.apache.commons.logging.LogFactory; |
||||
|
||||
import org.springframework.jdbc.core.test.ConcretePerson; |
||||
import org.springframework.jdbc.core.test.Person; |
||||
import org.springframework.jdbc.datasource.SingleConnectionDataSource; |
||||
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; |
||||
|
||||
/** |
||||
* Mock object based abstract class for RowMapper tests. |
||||
* Initializes mock objects and verifies results. |
||||
* |
||||
* @author Thomas Risberg |
||||
*/ |
||||
public abstract class AbstractRowMapperTests extends TestCase { |
||||
|
||||
private final boolean debugEnabled = LogFactory.getLog(JdbcTemplate.class).isDebugEnabled(); |
||||
|
||||
protected MockControl conControl; |
||||
protected Connection con; |
||||
protected MockControl rsmdControl; |
||||
protected ResultSetMetaData rsmd; |
||||
protected MockControl rsControl; |
||||
protected ResultSet rs; |
||||
protected MockControl stmtControl; |
||||
protected Statement stmt; |
||||
protected JdbcTemplate jdbcTemplate; |
||||
|
||||
protected void setUp() throws SQLException { |
||||
conControl = MockControl.createControl(Connection.class); |
||||
con = (Connection) conControl.getMock(); |
||||
con.isClosed(); |
||||
conControl.setDefaultReturnValue(false); |
||||
|
||||
rsmdControl = MockControl.createControl(ResultSetMetaData.class); |
||||
rsmd = (ResultSetMetaData)rsmdControl.getMock(); |
||||
rsmd.getColumnCount(); |
||||
rsmdControl.setReturnValue(4, 1); |
||||
rsmd.getColumnLabel(1); |
||||
rsmdControl.setReturnValue("name", 1); |
||||
rsmd.getColumnLabel(2); |
||||
rsmdControl.setReturnValue("age", 1); |
||||
rsmd.getColumnLabel(3); |
||||
rsmdControl.setReturnValue("birth_date", 1); |
||||
rsmd.getColumnLabel(4); |
||||
rsmdControl.setReturnValue("balance", 1); |
||||
rsmdControl.replay(); |
||||
|
||||
rsControl = MockControl.createControl(ResultSet.class); |
||||
rs = (ResultSet) rsControl.getMock(); |
||||
rs.getMetaData(); |
||||
rsControl.setReturnValue(rsmd, 1); |
||||
rs.next(); |
||||
rsControl.setReturnValue(true, 1); |
||||
rs.getString(1); |
||||
rsControl.setReturnValue("Bubba", 1); |
||||
rs.wasNull(); |
||||
rsControl.setReturnValue(false, 1); |
||||
rs.getLong(2); |
||||
rsControl.setReturnValue(22, 1); |
||||
rs.getTimestamp(3); |
||||
rsControl.setReturnValue(new Timestamp(1221222L), 1); |
||||
rs.getBigDecimal(4); |
||||
rsControl.setReturnValue(new BigDecimal("1234.56"), 1); |
||||
rs.next(); |
||||
rsControl.setReturnValue(false, 1); |
||||
rs.close(); |
||||
rsControl.setVoidCallable(1); |
||||
rsControl.replay(); |
||||
|
||||
stmtControl = MockControl.createControl(Statement.class); |
||||
stmt = (Statement) stmtControl.getMock(); |
||||
|
||||
con.createStatement(); |
||||
conControl.setReturnValue(stmt, 1); |
||||
stmt.executeQuery("select name, age, birth_date, balance from people"); |
||||
stmtControl.setReturnValue(rs, 1); |
||||
if (debugEnabled) { |
||||
stmt.getWarnings(); |
||||
stmtControl.setReturnValue(null, 1); |
||||
} |
||||
stmt.close(); |
||||
stmtControl.setVoidCallable(1); |
||||
|
||||
conControl.replay(); |
||||
stmtControl.replay(); |
||||
|
||||
jdbcTemplate = new JdbcTemplate(); |
||||
jdbcTemplate.setDataSource(new SingleConnectionDataSource(con, false)); |
||||
jdbcTemplate.setExceptionTranslator(new SQLStateSQLExceptionTranslator()); |
||||
jdbcTemplate.afterPropertiesSet(); |
||||
} |
||||
|
||||
protected void verifyPerson(Person bean) { |
||||
verify(); |
||||
assertEquals("Bubba", bean.getName()); |
||||
assertEquals(22L, bean.getAge()); |
||||
assertEquals(new java.util.Date(1221222L), bean.getBirth_date()); |
||||
assertEquals(new BigDecimal("1234.56"), bean.getBalance()); |
||||
} |
||||
|
||||
protected void verifyConcretePerson(ConcretePerson bean) { |
||||
verify(); |
||||
assertEquals("Bubba", bean.getName()); |
||||
assertEquals(22L, bean.getAge()); |
||||
assertEquals(new java.util.Date(1221222L), bean.getBirth_date()); |
||||
assertEquals(new BigDecimal("1234.56"), bean.getBalance()); |
||||
} |
||||
|
||||
private void verify() { |
||||
conControl.verify(); |
||||
rsControl.verify(); |
||||
rsmdControl.verify(); |
||||
stmtControl.verify(); |
||||
} |
||||
|
||||
} |
||||
@ -1,42 +0,0 @@
@@ -1,42 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2006 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.core; |
||||
|
||||
import java.sql.ResultSet; |
||||
import java.sql.SQLException; |
||||
|
||||
/** |
||||
* Simple row count callback handler for testing purposes. |
||||
* Does not call any JDBC methods on the given ResultSet. |
||||
* |
||||
* @author Juergen Hoeller |
||||
* @since 2.0 |
||||
*/ |
||||
public class SimpleRowCountCallbackHandler implements RowCallbackHandler { |
||||
|
||||
private int count; |
||||
|
||||
|
||||
public void processRow(ResultSet rs) throws SQLException { |
||||
count++; |
||||
} |
||||
|
||||
public int getCount() { |
||||
return count; |
||||
} |
||||
|
||||
} |
||||
@ -1,57 +0,0 @@
@@ -1,57 +0,0 @@
|
||||
/* |
||||
* 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.core.test; |
||||
|
||||
import java.util.Date; |
||||
|
||||
/** |
||||
* @author Thomas Risberg |
||||
*/ |
||||
public abstract class AbstractPerson { |
||||
|
||||
private String name; |
||||
|
||||
private long age; |
||||
|
||||
private java.util.Date birth_date; |
||||
|
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public long getAge() { |
||||
return age; |
||||
} |
||||
|
||||
public void setAge(long age) { |
||||
this.age = age; |
||||
} |
||||
|
||||
public Date getBirth_date() { |
||||
return birth_date; |
||||
} |
||||
|
||||
public void setBirth_date(Date birth_date) { |
||||
this.birth_date = birth_date; |
||||
} |
||||
|
||||
} |
||||
@ -1,37 +0,0 @@
@@ -1,37 +0,0 @@
|
||||
/* |
||||
* 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.core.test; |
||||
|
||||
import java.math.BigDecimal; |
||||
|
||||
/** |
||||
* @author Thomas Risberg |
||||
*/ |
||||
public class ConcretePerson extends AbstractPerson { |
||||
|
||||
private BigDecimal balance; |
||||
|
||||
|
||||
public BigDecimal getBalance() { |
||||
return balance; |
||||
} |
||||
|
||||
public void setBalance(BigDecimal balance) { |
||||
this.balance = balance; |
||||
} |
||||
|
||||
} |
||||
@ -1,35 +0,0 @@
@@ -1,35 +0,0 @@
|
||||
/* |
||||
* 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.core.test; |
||||
|
||||
/** |
||||
* @author Juergen Hoeller |
||||
*/ |
||||
public class ExtendedPerson extends ConcretePerson { |
||||
|
||||
private Object someField; |
||||
|
||||
|
||||
public Object getSomeField() { |
||||
return someField; |
||||
} |
||||
|
||||
public void setSomeField(Object someField) { |
||||
this.someField = someField; |
||||
} |
||||
|
||||
} |
||||
@ -1,67 +0,0 @@
@@ -1,67 +0,0 @@
|
||||
/* |
||||
* 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.core.test; |
||||
|
||||
import java.math.BigDecimal; |
||||
|
||||
/** |
||||
* @author Thomas Risberg |
||||
*/ |
||||
public class Person { |
||||
|
||||
private String name; |
||||
|
||||
private long age; |
||||
|
||||
private java.util.Date birth_date; |
||||
|
||||
private BigDecimal balance; |
||||
|
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public long getAge() { |
||||
return age; |
||||
} |
||||
|
||||
public void setAge(long age) { |
||||
this.age = age; |
||||
} |
||||
|
||||
public java.util.Date getBirth_date() { |
||||
return birth_date; |
||||
} |
||||
|
||||
public void setBirth_date(java.util.Date birth_date) { |
||||
this.birth_date = birth_date; |
||||
} |
||||
|
||||
public BigDecimal getBalance() { |
||||
return balance; |
||||
} |
||||
|
||||
public void setBalance(BigDecimal balanace) { |
||||
this.balance = balanace; |
||||
} |
||||
|
||||
} |
||||
@ -1,34 +0,0 @@
@@ -1,34 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2005 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.support; |
||||
|
||||
import org.springframework.dao.DataAccessException; |
||||
|
||||
/** |
||||
* @author Thomas Risberg |
||||
*/ |
||||
public class CustomErrorCodeException extends DataAccessException { |
||||
|
||||
public CustomErrorCodeException(String msg) { |
||||
super(msg); |
||||
} |
||||
|
||||
public CustomErrorCodeException(String msg, Throwable ex) { |
||||
super(msg, ex); |
||||
} |
||||
|
||||
} |
||||
@ -1,78 +0,0 @@
@@ -1,78 +0,0 @@
|
||||
/* |
||||
* 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.support; |
||||
|
||||
import java.sql.SQLDataException; |
||||
import java.sql.SQLException; |
||||
import java.sql.SQLFeatureNotSupportedException; |
||||
import java.sql.SQLIntegrityConstraintViolationException; |
||||
import java.sql.SQLInvalidAuthorizationSpecException; |
||||
import java.sql.SQLNonTransientConnectionException; |
||||
import java.sql.SQLRecoverableException; |
||||
import java.sql.SQLSyntaxErrorException; |
||||
import java.sql.SQLTimeoutException; |
||||
import java.sql.SQLTransactionRollbackException; |
||||
import java.sql.SQLTransientConnectionException; |
||||
|
||||
/** |
||||
* Class to generate Java 6 SQLException subclasses for testing purposes. |
||||
* |
||||
* @author Thomas Risberg |
||||
*/ |
||||
public class SQLExceptionSubclassFactory { |
||||
|
||||
public static SQLException newSQLDataException(String reason, String SQLState, int vendorCode) { |
||||
return new SQLDataException(reason, SQLState, vendorCode); |
||||
} |
||||
|
||||
public static SQLException newSQLFeatureNotSupportedException(String reason, String SQLState, int vendorCode) { |
||||
return new SQLFeatureNotSupportedException(reason, SQLState, vendorCode); |
||||
} |
||||
|
||||
public static SQLException newSQLIntegrityConstraintViolationException(String reason, String SQLState, int vendorCode) { |
||||
return new SQLIntegrityConstraintViolationException(reason, SQLState, vendorCode); |
||||
} |
||||
|
||||
public static SQLException newSQLInvalidAuthorizationSpecException(String reason, String SQLState, int vendorCode) { |
||||
return new SQLInvalidAuthorizationSpecException(reason, SQLState, vendorCode); |
||||
} |
||||
|
||||
public static SQLException newSQLNonTransientConnectionException(String reason, String SQLState, int vendorCode) { |
||||
return new SQLNonTransientConnectionException(reason, SQLState, vendorCode); |
||||
} |
||||
|
||||
public static SQLException newSQLSyntaxErrorException(String reason, String SQLState, int vendorCode) { |
||||
return new SQLSyntaxErrorException(reason, SQLState, vendorCode); |
||||
} |
||||
|
||||
public static SQLException newSQLTransactionRollbackException(String reason, String SQLState, int vendorCode) { |
||||
return new SQLTransactionRollbackException(reason, SQLState, vendorCode); |
||||
} |
||||
|
||||
public static SQLException newSQLTransientConnectionException(String reason, String SQLState, int vendorCode) { |
||||
return new SQLTransientConnectionException(reason, SQLState, vendorCode); |
||||
} |
||||
|
||||
public static SQLException newSQLTimeoutException(String reason, String SQLState, int vendorCode) { |
||||
return new SQLTimeoutException(reason, SQLState, vendorCode); |
||||
} |
||||
|
||||
public static SQLException newSQLRecoverableException(String reason, String SQLState, int vendorCode) { |
||||
return new SQLRecoverableException(reason, SQLState, vendorCode); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue