Migrate JUnit 3 tests to JUnit 4
This commit migrates all remaining tests from JUnit 3 to JUnit 4, with the exception of Spring's legacy JUnit 3.8 based testing framework that is still in use in the spring-orm module. Issue: SPR-13514
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -24,7 +24,6 @@ import java.sql.Statement;
|
||||
import java.sql.Types;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -33,30 +32,33 @@ import org.springframework.jdbc.datasource.SingleConnectionDataSource;
|
||||
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
* @since 02.08.2004
|
||||
*/
|
||||
public class RowMapperTests extends TestCase {
|
||||
public class RowMapperTests {
|
||||
|
||||
private Connection connection;
|
||||
private Statement statement;
|
||||
private PreparedStatement preparedStatement;
|
||||
private ResultSet resultSet;
|
||||
private final Connection connection = mock(Connection.class);
|
||||
|
||||
private JdbcTemplate template;
|
||||
private final Statement statement = mock(Statement.class);
|
||||
|
||||
private final PreparedStatement preparedStatement = mock(PreparedStatement.class);
|
||||
|
||||
private final ResultSet resultSet = mock(ResultSet.class);
|
||||
|
||||
private final JdbcTemplate template = new JdbcTemplate();
|
||||
|
||||
private final RowMapper<TestBean> testRowMapper =
|
||||
(rs, rowNum) -> new TestBean(rs.getString(1), rs.getInt(2));
|
||||
|
||||
private List<TestBean> result;
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp() throws SQLException {
|
||||
connection = mock(Connection.class);
|
||||
statement = mock(Statement.class);
|
||||
preparedStatement = mock(PreparedStatement.class);
|
||||
resultSet = mock(ResultSet.class);
|
||||
given(connection.createStatement()).willReturn(statement);
|
||||
given(connection.prepareStatement(anyString())).willReturn(preparedStatement);
|
||||
given(statement.executeQuery(anyString())).willReturn(resultSet);
|
||||
@@ -64,7 +66,7 @@ public class RowMapperTests extends TestCase {
|
||||
given(resultSet.next()).willReturn(true, true, false);
|
||||
given(resultSet.getString(1)).willReturn("tb1", "tb2");
|
||||
given(resultSet.getInt(2)).willReturn(1, 2);
|
||||
template = new JdbcTemplate();
|
||||
|
||||
template.setDataSource(new SingleConnectionDataSource(connection, false));
|
||||
template.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
|
||||
template.afterPropertiesSet();
|
||||
@@ -73,75 +75,57 @@ public class RowMapperTests extends TestCase {
|
||||
@After
|
||||
public void verifyClosed() throws Exception {
|
||||
verify(resultSet).close();
|
||||
verify(connection).close();
|
||||
// verify(connection).close();
|
||||
}
|
||||
|
||||
@After
|
||||
public void verifyResults() {
|
||||
assertTrue(result != null);
|
||||
assertNotNull(result);
|
||||
assertEquals(2, result.size());
|
||||
assertEquals("tb1", result.get(0).getName());
|
||||
assertEquals("tb2", result.get(1).getName());
|
||||
assertEquals(1, result.get(0).getAge());
|
||||
assertEquals(2, result.get(1).getAge());
|
||||
TestBean testBean1 = result.get(0);
|
||||
TestBean testBean2 = result.get(1);
|
||||
assertEquals("tb1", testBean1.getName());
|
||||
assertEquals("tb2", testBean2.getName());
|
||||
assertEquals(1, testBean1.getAge());
|
||||
assertEquals(2, testBean2.getAge());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStaticQueryWithRowMapper() throws SQLException {
|
||||
result = template.query("some SQL", new TestRowMapper());
|
||||
public void staticQueryWithRowMapper() throws SQLException {
|
||||
result = template.query("some SQL", testRowMapper);
|
||||
verify(statement).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreparedStatementCreatorWithRowMapper() throws SQLException {
|
||||
result = template.query(new PreparedStatementCreator() {
|
||||
@Override
|
||||
public PreparedStatement createPreparedStatement(Connection con)
|
||||
throws SQLException {
|
||||
return preparedStatement;
|
||||
}
|
||||
}, new TestRowMapper());
|
||||
public void preparedStatementCreatorWithRowMapper() throws SQLException {
|
||||
result = template.query(con -> preparedStatement, testRowMapper);
|
||||
verify(preparedStatement).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreparedStatementSetterWithRowMapper() throws SQLException {
|
||||
result = template.query("some SQL", new PreparedStatementSetter() {
|
||||
@Override
|
||||
public void setValues(PreparedStatement ps) throws SQLException {
|
||||
ps.setString(1, "test");
|
||||
}
|
||||
}, new TestRowMapper());
|
||||
public void preparedStatementSetterWithRowMapper() throws SQLException {
|
||||
result = template.query("some SQL", ps -> ps.setString(1, "test"), testRowMapper);
|
||||
verify(preparedStatement).setString(1, "test");
|
||||
verify(preparedStatement).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueryWithArgsAndRowMapper() throws SQLException {
|
||||
result = template.query("some SQL",
|
||||
new Object[] { "test1", "test2" },
|
||||
new TestRowMapper());
|
||||
public void queryWithArgsAndRowMapper() throws SQLException {
|
||||
result = template.query("some SQL", new Object[] { "test1", "test2" }, testRowMapper);
|
||||
preparedStatement.setString(1, "test1");
|
||||
preparedStatement.setString(2, "test2");
|
||||
preparedStatement.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueryWithArgsAndTypesAndRowMapper() throws SQLException {
|
||||
public void queryWithArgsAndTypesAndRowMapper() throws SQLException {
|
||||
result = template.query("some SQL",
|
||||
new Object[] { "test1", "test2" },
|
||||
new int[] { Types.VARCHAR, Types.VARCHAR },
|
||||
new TestRowMapper());
|
||||
testRowMapper);
|
||||
verify(preparedStatement).setString(1, "test1");
|
||||
verify(preparedStatement).setString(2, "test2");
|
||||
verify(preparedStatement).close();
|
||||
}
|
||||
|
||||
private static class TestRowMapper implements RowMapper<TestBean> {
|
||||
@Override
|
||||
public TestBean mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
return new TestBean(rs.getString(1), rs.getInt(2));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -19,9 +19,12 @@ package org.springframework.jdbc.object;
|
||||
import java.sql.Types;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
@@ -30,119 +33,90 @@ import org.springframework.jdbc.core.SqlOutParameter;
|
||||
import org.springframework.jdbc.core.SqlParameter;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Trevor Cook
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class RdbmsOperationTests extends TestCase {
|
||||
public class RdbmsOperationTests {
|
||||
|
||||
public void testEmptySql() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
try {
|
||||
operation.compile();
|
||||
fail("Shouldn't allow compiling without sql statement");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException idaauex) {
|
||||
// OK
|
||||
}
|
||||
private final TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
|
||||
@Rule
|
||||
public final ExpectedException exception = ExpectedException.none();
|
||||
|
||||
|
||||
@Test
|
||||
public void emptySql() {
|
||||
exception.expect(InvalidDataAccessApiUsageException.class);
|
||||
operation.compile();
|
||||
}
|
||||
|
||||
public void testSetTypeAfterCompile() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
@Test
|
||||
public void setTypeAfterCompile() {
|
||||
operation.setDataSource(new DriverManagerDataSource());
|
||||
operation.setSql("select * from mytable");
|
||||
operation.compile();
|
||||
try {
|
||||
operation.setTypes(new int[] {Types.INTEGER });
|
||||
fail("Shouldn't allow setting parameters after compile");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException idaauex) {
|
||||
// OK
|
||||
}
|
||||
exception.expect(InvalidDataAccessApiUsageException.class);
|
||||
operation.setTypes(new int[] { Types.INTEGER });
|
||||
}
|
||||
|
||||
public void testDeclareParameterAfterCompile() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
@Test
|
||||
public void declareParameterAfterCompile() {
|
||||
operation.setDataSource(new DriverManagerDataSource());
|
||||
operation.setSql("select * from mytable");
|
||||
operation.compile();
|
||||
try {
|
||||
operation.declareParameter(new SqlParameter(Types.INTEGER));
|
||||
fail("Shouldn't allow setting parameters after compile");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException idaauex) {
|
||||
// OK
|
||||
}
|
||||
exception.expect(InvalidDataAccessApiUsageException.class);
|
||||
operation.declareParameter(new SqlParameter(Types.INTEGER));
|
||||
}
|
||||
|
||||
public void testTooFewParameters() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
@Test
|
||||
public void tooFewParameters() {
|
||||
operation.setSql("select * from mytable");
|
||||
operation.setTypes(new int[] { Types.INTEGER });
|
||||
try {
|
||||
operation.validateParameters((Object[]) null);
|
||||
fail("Shouldn't validate without enough parameters");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException idaauex) {
|
||||
// OK
|
||||
}
|
||||
exception.expect(InvalidDataAccessApiUsageException.class);
|
||||
operation.validateParameters((Object[]) null);
|
||||
}
|
||||
|
||||
public void testTooFewMapParameters() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
@Test
|
||||
public void tooFewMapParameters() {
|
||||
operation.setSql("select * from mytable");
|
||||
operation.setTypes(new int[] { Types.INTEGER });
|
||||
try {
|
||||
operation.validateNamedParameters((Map<String, String>) null);
|
||||
fail("Shouldn't validate without enough parameters");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException idaauex) {
|
||||
// OK
|
||||
}
|
||||
exception.expect(InvalidDataAccessApiUsageException.class);
|
||||
operation.validateNamedParameters((Map<String, String>) null);
|
||||
}
|
||||
|
||||
public void testOperationConfiguredViaJdbcTemplateMustGetDataSource() throws Exception {
|
||||
try {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
operation.setSql("foo");
|
||||
operation.compile();
|
||||
fail("Can't compile without providing a DataSource for the JdbcTemplate");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException ex) {
|
||||
// Check for helpful error message. Omit leading character
|
||||
// so as not to be fussy about case
|
||||
assertTrue(ex.getMessage().indexOf("ataSource") != -1);
|
||||
}
|
||||
@Test
|
||||
public void operationConfiguredViaJdbcTemplateMustGetDataSource() throws Exception {
|
||||
operation.setSql("foo");
|
||||
|
||||
exception.expect(InvalidDataAccessApiUsageException.class);
|
||||
exception.expectMessage(containsString("ataSource"));
|
||||
operation.compile();
|
||||
}
|
||||
|
||||
public void testTooManyParameters() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
@Test
|
||||
public void tooManyParameters() {
|
||||
operation.setSql("select * from mytable");
|
||||
try {
|
||||
operation.validateParameters(new Object[] {1, 2});
|
||||
fail("Shouldn't validate with too many parameters");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException idaauex) {
|
||||
// OK
|
||||
}
|
||||
exception.expect(InvalidDataAccessApiUsageException.class);
|
||||
operation.validateParameters(new Object[] { 1, 2 });
|
||||
}
|
||||
|
||||
public void testUnspecifiedMapParameters() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
@Test
|
||||
public void unspecifiedMapParameters() {
|
||||
operation.setSql("select * from mytable");
|
||||
try {
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("col1", "value");
|
||||
operation.validateNamedParameters(params);
|
||||
fail("Shouldn't validate with unspecified parameters");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException idaauex) {
|
||||
// OK
|
||||
}
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("col1", "value");
|
||||
exception.expect(InvalidDataAccessApiUsageException.class);
|
||||
operation.validateNamedParameters(params);
|
||||
}
|
||||
|
||||
public void testCompileTwice() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
@Test
|
||||
public void compileTwice() {
|
||||
operation.setDataSource(new DriverManagerDataSource());
|
||||
operation.setSql("select * from mytable");
|
||||
operation.setTypes(null);
|
||||
@@ -150,22 +124,17 @@ public class RdbmsOperationTests extends TestCase {
|
||||
operation.compile();
|
||||
}
|
||||
|
||||
public void testEmptyDataSource() {
|
||||
SqlOperation operation = new SqlOperation() {
|
||||
};
|
||||
@Test
|
||||
public void emptyDataSource() {
|
||||
SqlOperation operation = new SqlOperation() {};
|
||||
operation.setSql("select * from mytable");
|
||||
try {
|
||||
operation.compile();
|
||||
fail("Shouldn't allow compiling without data source");
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException idaauex) {
|
||||
// OK
|
||||
}
|
||||
exception.expect(InvalidDataAccessApiUsageException.class);
|
||||
operation.compile();
|
||||
}
|
||||
|
||||
public void testParameterPropagation() {
|
||||
SqlOperation operation = new SqlOperation() {
|
||||
};
|
||||
@Test
|
||||
public void parameterPropagation() {
|
||||
SqlOperation operation = new SqlOperation() {};
|
||||
DataSource ds = new DriverManagerDataSource();
|
||||
operation.setDataSource(ds);
|
||||
operation.setFetchSize(10);
|
||||
@@ -176,8 +145,8 @@ public class RdbmsOperationTests extends TestCase {
|
||||
assertEquals(20, jt.getMaxRows());
|
||||
}
|
||||
|
||||
public void testValidateInOutParameter() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
@Test
|
||||
public void validateInOutParameter() {
|
||||
operation.setDataSource(new DriverManagerDataSource());
|
||||
operation.setSql("DUMMY_PROC");
|
||||
operation.declareParameter(new SqlOutParameter("DUMMY_OUT_PARAM", Types.VARCHAR));
|
||||
@@ -185,8 +154,8 @@ public class RdbmsOperationTests extends TestCase {
|
||||
operation.validateParameters(new Object[] {"DUMMY_VALUE1", "DUMMY_VALUE2"});
|
||||
}
|
||||
|
||||
public void testParametersSetWithList() {
|
||||
TestRdbmsOperation operation = new TestRdbmsOperation();
|
||||
@Test
|
||||
public void parametersSetWithList() {
|
||||
DataSource ds = new DriverManagerDataSource();
|
||||
operation.setDataSource(ds);
|
||||
operation.setSql("select * from mytable where one = ? and two = ?");
|
||||
@@ -194,14 +163,8 @@ public class RdbmsOperationTests extends TestCase {
|
||||
new SqlParameter("one", Types.NUMERIC),
|
||||
new SqlParameter("two", Types.NUMERIC)});
|
||||
operation.afterPropertiesSet();
|
||||
try {
|
||||
operation.validateParameters(new Object[] {1, "2"});
|
||||
assertEquals(2, operation.getDeclaredParameters().size());
|
||||
// OK
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException idaauex) {
|
||||
fail("Should have validated with parameters set using List: " + idaauex.getMessage());
|
||||
}
|
||||
operation.validateParameters(new Object[] { 1, "2" });
|
||||
assertEquals(2, operation.getDeclaredParameters().size());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2015 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,28 +16,32 @@
|
||||
|
||||
package org.springframework.jdbc.support;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Unit tests for JdbcUtils.
|
||||
* Unit tests for {@link JdbcUtils}.
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
public class JdbcUtilsTests extends TestCase {
|
||||
public class JdbcUtilsTests {
|
||||
|
||||
public void testCommonDatabaseName() {
|
||||
assertEquals("Wrong db name", "Oracle", JdbcUtils.commonDatabaseName("Oracle"));
|
||||
assertEquals("Wrong db name", "DB2", JdbcUtils.commonDatabaseName("DB2-for-Spring"));
|
||||
assertEquals("Wrong db name", "Sybase", JdbcUtils.commonDatabaseName("Sybase SQL Server"));
|
||||
assertEquals("Wrong db name", "Sybase", JdbcUtils.commonDatabaseName("Adaptive Server Enterprise"));
|
||||
assertEquals("Wrong db name", "MySQL", JdbcUtils.commonDatabaseName("MySQL"));
|
||||
@Test
|
||||
public void commonDatabaseName() {
|
||||
assertEquals("Oracle", JdbcUtils.commonDatabaseName("Oracle"));
|
||||
assertEquals("DB2", JdbcUtils.commonDatabaseName("DB2-for-Spring"));
|
||||
assertEquals("Sybase", JdbcUtils.commonDatabaseName("Sybase SQL Server"));
|
||||
assertEquals("Sybase", JdbcUtils.commonDatabaseName("Adaptive Server Enterprise"));
|
||||
assertEquals("MySQL", JdbcUtils.commonDatabaseName("MySQL"));
|
||||
}
|
||||
|
||||
public void testConvertUnderscoreNameToPropertyName() {
|
||||
assertEquals("Wrong property name", "myName", JdbcUtils.convertUnderscoreNameToPropertyName("MY_NAME"));
|
||||
assertEquals("Wrong property name", "yourName", JdbcUtils.convertUnderscoreNameToPropertyName("yOUR_nAME"));
|
||||
assertEquals("Wrong property name", "AName", JdbcUtils.convertUnderscoreNameToPropertyName("a_name"));
|
||||
assertEquals("Wrong property name", "someoneElsesName", JdbcUtils.convertUnderscoreNameToPropertyName("someone_elses_name"));
|
||||
@Test
|
||||
public void convertUnderscoreNameToPropertyName() {
|
||||
assertEquals("myName", JdbcUtils.convertUnderscoreNameToPropertyName("MY_NAME"));
|
||||
assertEquals("yourName", JdbcUtils.convertUnderscoreNameToPropertyName("yOUR_nAME"));
|
||||
assertEquals("AName", JdbcUtils.convertUnderscoreNameToPropertyName("a_name"));
|
||||
assertEquals("someoneElsesName", JdbcUtils.convertUnderscoreNameToPropertyName("someone_elses_name"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -17,98 +17,87 @@
|
||||
package org.springframework.jdbc.support;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.dao.DataRetrievalFailureException;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.*;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Tests for the KeyHolder and GeneratedKeyHolder
|
||||
* and it appears that JdbcUtils doesn't work exactly as documented.
|
||||
* Tests for {@link KeyHolder} and {@link GeneratedKeyHolder}.
|
||||
*
|
||||
* @author trisberg
|
||||
* @since Jul 18, 2004
|
||||
* @author Thomas Risberg
|
||||
* @author Sam Brannen
|
||||
* @since July 18, 2004
|
||||
*/
|
||||
public class KeyHolderTests extends TestCase {
|
||||
private KeyHolder kh;
|
||||
@SuppressWarnings("serial")
|
||||
public class KeyHolderTests {
|
||||
|
||||
@Override
|
||||
public void setUp() {
|
||||
kh = new GeneratedKeyHolder();
|
||||
}
|
||||
private final KeyHolder kh = new GeneratedKeyHolder();
|
||||
|
||||
@Rule
|
||||
public final ExpectedException exception = ExpectedException.none();
|
||||
|
||||
|
||||
@Test
|
||||
public void singleKey() {
|
||||
kh.getKeyList().addAll(singletonList(singletonMap("key", 1)));
|
||||
|
||||
public void testSingleKey(){
|
||||
List<Map<String, Object>> l = new LinkedList<Map<String, Object>>();
|
||||
Map<String, Object> m = new HashMap<String, Object>(1);
|
||||
m.put("key", 1);
|
||||
l.add(m);
|
||||
kh.getKeyList().addAll(l);
|
||||
assertEquals("single key should be returned", 1, kh.getKey().intValue());
|
||||
}
|
||||
|
||||
public void testSingleKeyNonNumeric(){
|
||||
List<Map<String, Object>> l = new LinkedList<Map<String, Object>>();
|
||||
Map<String, Object> m = new HashMap<String, Object>(1);
|
||||
m.put("key", "1");
|
||||
l.add(m);
|
||||
kh.getKeyList().addAll(l);
|
||||
try {
|
||||
kh.getKey().intValue();
|
||||
}
|
||||
catch (DataRetrievalFailureException e) {
|
||||
assertTrue(e.getMessage().startsWith("The generated key is not of a supported numeric type."));
|
||||
}
|
||||
@Test
|
||||
public void singleKeyNonNumeric() {
|
||||
kh.getKeyList().addAll(singletonList(singletonMap("key", "1")));
|
||||
|
||||
exception.expect(DataRetrievalFailureException.class);
|
||||
exception.expectMessage(startsWith("The generated key is not of a supported numeric type."));
|
||||
kh.getKey().intValue();
|
||||
}
|
||||
|
||||
public void testNoKeyReturnedInMap(){
|
||||
List<Map<String, Object>> l = new LinkedList<Map<String, Object>>();
|
||||
Map<String, Object> m = new HashMap<String, Object>();
|
||||
l.add(m);
|
||||
kh.getKeyList().addAll(l);
|
||||
try {
|
||||
kh.getKey();
|
||||
}
|
||||
catch (DataRetrievalFailureException e) {
|
||||
assertTrue(e.getMessage().startsWith("Unable to retrieve the generated key."));
|
||||
}
|
||||
@Test
|
||||
public void noKeyReturnedInMap() {
|
||||
kh.getKeyList().addAll(singletonList(emptyMap()));
|
||||
|
||||
exception.expect(DataRetrievalFailureException.class);
|
||||
exception.expectMessage(startsWith("Unable to retrieve the generated key."));
|
||||
kh.getKey();
|
||||
}
|
||||
|
||||
public void testMultipleKeys(){
|
||||
List<Map<String, Object>> l = new LinkedList<Map<String, Object>>();
|
||||
Map<String, Object> m = new HashMap<String, Object>(2);
|
||||
m.put("key", 1);
|
||||
m.put("seq", 2);
|
||||
l.add(m);
|
||||
kh.getKeyList().addAll(l);
|
||||
Map<String, Object> keyMap = kh.getKeys();
|
||||
assertEquals("two keys should be in the map", 2, keyMap.size());
|
||||
try {
|
||||
kh.getKey();
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException e) {
|
||||
assertTrue(e.getMessage().startsWith("The getKey method should only be used when a single key is returned."));
|
||||
}
|
||||
@Test
|
||||
public void multipleKeys() {
|
||||
Map<String, Object> m = new HashMap<String, Object>() {{
|
||||
put("key", 1);
|
||||
put("seq", 2);
|
||||
}};
|
||||
kh.getKeyList().addAll(singletonList(m));
|
||||
|
||||
assertEquals("two keys should be in the map", 2, kh.getKeys().size());
|
||||
exception.expect(InvalidDataAccessApiUsageException.class);
|
||||
exception.expectMessage(startsWith("The getKey method should only be used when a single key is returned."));
|
||||
kh.getKey();
|
||||
}
|
||||
|
||||
public void testMultipleKeyRows(){
|
||||
List<Map<String, Object>> l = new LinkedList<Map<String, Object>>();
|
||||
Map<String, Object> m = new HashMap<String, Object>(2);
|
||||
m.put("key", 1);
|
||||
m.put("seq", 2);
|
||||
l.add(m);
|
||||
l.add(m);
|
||||
kh.getKeyList().addAll(l);
|
||||
@Test
|
||||
public void multipleKeyRows() {
|
||||
Map<String, Object> m = new HashMap<String, Object>() {{
|
||||
put("key", 1);
|
||||
put("seq", 2);
|
||||
}};
|
||||
kh.getKeyList().addAll(asList(m, m));
|
||||
|
||||
assertEquals("two rows should be in the list", 2, kh.getKeyList().size());
|
||||
try {
|
||||
kh.getKeys();
|
||||
}
|
||||
catch (InvalidDataAccessApiUsageException e) {
|
||||
assertTrue(e.getMessage().startsWith("The getKeys method should only be used when keys for a single row are returned."));
|
||||
}
|
||||
exception.expect(InvalidDataAccessApiUsageException.class);
|
||||
exception.expectMessage(startsWith("The getKeys method should only be used when keys for a single row are returned."));
|
||||
kh.getKeys();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -20,7 +20,9 @@ import java.sql.BatchUpdateException;
|
||||
import java.sql.DataTruncation;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.dao.CannotAcquireLockException;
|
||||
import org.springframework.dao.CannotSerializeTransactionException;
|
||||
@@ -32,11 +34,14 @@ import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.jdbc.BadSqlGrammarException;
|
||||
import org.springframework.jdbc.InvalidResultSetAccessException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class SQLErrorCodeSQLExceptionTranslatorTests extends TestCase {
|
||||
public class SQLErrorCodeSQLExceptionTranslatorTests {
|
||||
|
||||
private static SQLErrorCodes ERROR_CODES = new SQLErrorCodes();
|
||||
static {
|
||||
@@ -50,7 +55,12 @@ public class SQLErrorCodeSQLExceptionTranslatorTests extends TestCase {
|
||||
ERROR_CODES.setCannotSerializeTransactionCodes(new String[] { "9" });
|
||||
}
|
||||
|
||||
public void testErrorCodeTranslation() {
|
||||
@Rule
|
||||
public final ExpectedException exception = ExpectedException.none();
|
||||
|
||||
|
||||
@Test
|
||||
public void errorCodeTranslation() {
|
||||
SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
|
||||
|
||||
SQLException badSqlEx = new SQLException("", "", 1);
|
||||
@@ -90,7 +100,8 @@ public class SQLErrorCodeSQLExceptionTranslatorTests extends TestCase {
|
||||
assertTrue(ex.getCause() == sex);
|
||||
}
|
||||
|
||||
public void testBatchExceptionTranslation() {
|
||||
@Test
|
||||
public void batchExceptionTranslation() {
|
||||
SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
|
||||
|
||||
SQLException badSqlEx = new SQLException("", "", 1);
|
||||
@@ -101,7 +112,8 @@ public class SQLErrorCodeSQLExceptionTranslatorTests extends TestCase {
|
||||
assertEquals(badSqlEx, bsgex.getSQLException());
|
||||
}
|
||||
|
||||
public void testDataTruncationTranslation() {
|
||||
@Test
|
||||
public void dataTruncationTranslation() {
|
||||
SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
|
||||
|
||||
SQLException dataAccessEx = new SQLException("", "", 5);
|
||||
@@ -111,7 +123,8 @@ public class SQLErrorCodeSQLExceptionTranslatorTests extends TestCase {
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public void testCustomTranslateMethodTranslation() {
|
||||
@Test
|
||||
public void customTranslateMethodTranslation() {
|
||||
final String TASK = "TASK";
|
||||
final String SQL = "SQL SELECT *";
|
||||
final DataAccessException customDex = new DataAccessException("") {};
|
||||
@@ -135,7 +148,8 @@ public class SQLErrorCodeSQLExceptionTranslatorTests extends TestCase {
|
||||
assertEquals(intVioEx, diex.getCause());
|
||||
}
|
||||
|
||||
public void testCustomExceptionTranslation() {
|
||||
@Test
|
||||
public void customExceptionTranslation() {
|
||||
final String TASK = "TASK";
|
||||
final String SQL = "SQL SELECT *";
|
||||
final SQLErrorCodes customErrorCodes = new SQLErrorCodes();
|
||||
@@ -161,13 +175,8 @@ public class SQLErrorCodeSQLExceptionTranslatorTests extends TestCase {
|
||||
assertEquals(invResEx, diex.getCause());
|
||||
|
||||
// Shouldn't custom translate this - invalid class
|
||||
try {
|
||||
customTranslation.setExceptionClass(String.class);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
exception.expect(IllegalArgumentException.class);
|
||||
customTranslation.setExceptionClass(String.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2015 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,22 +18,22 @@ package org.springframework.jdbc.support;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.TransientDataAccessResourceException;
|
||||
import org.springframework.jdbc.BadSqlGrammarException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Class to test custom SQLException translation.
|
||||
* Unit tests for custom SQLException translation.
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@RunWith(JUnit4.class)
|
||||
public class SQLExceptionCustomTranslatorTests extends TestCase {
|
||||
public class SQLExceptionCustomTranslatorTests {
|
||||
|
||||
private static SQLErrorCodes ERROR_CODES = new SQLErrorCodes();
|
||||
|
||||
@@ -43,22 +43,23 @@ public class SQLExceptionCustomTranslatorTests extends TestCase {
|
||||
ERROR_CODES.setCustomSqlExceptionTranslatorClass(CustomSqlExceptionTranslator.class);
|
||||
}
|
||||
|
||||
private final SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
|
||||
|
||||
|
||||
@Test
|
||||
public void testCustomErrorCodeTranslation() {
|
||||
|
||||
SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
|
||||
|
||||
SQLException dataIntegrityViolationEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 1);
|
||||
DataAccessException daeex = sext.translate("task", "SQL", dataIntegrityViolationEx);
|
||||
assertEquals(dataIntegrityViolationEx, daeex.getCause());
|
||||
assertTrue(daeex instanceof BadSqlGrammarException);
|
||||
|
||||
SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 2);
|
||||
DataAccessException darex = sext.translate("task", "SQL", dataAccessResourceEx);
|
||||
assertEquals(dataIntegrityViolationEx, daeex.getCause());
|
||||
assertTrue(darex instanceof TransientDataAccessResourceException);
|
||||
|
||||
public void badSqlGrammarException() {
|
||||
SQLException badSqlGrammarExceptionEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 1);
|
||||
DataAccessException dae = sext.translate("task", "SQL", badSqlGrammarExceptionEx);
|
||||
assertEquals(badSqlGrammarExceptionEx, dae.getCause());
|
||||
assertThat(dae, instanceOf(BadSqlGrammarException.class));
|
||||
}
|
||||
|
||||
}
|
||||
@Test
|
||||
public void dataAccessResourceException() {
|
||||
SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 2);
|
||||
DataAccessException dae = sext.translate("task", "SQL", dataAccessResourceEx);
|
||||
assertEquals(dataAccessResourceEx, dae.getCause());
|
||||
assertThat(dae, instanceOf(TransientDataAccessResourceException.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ package org.springframework.jdbc.support;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.dao.ConcurrencyFailureException;
|
||||
import org.springframework.dao.DataAccessResourceFailureException;
|
||||
@@ -30,10 +30,12 @@ import org.springframework.dao.RecoverableDataAccessException;
|
||||
import org.springframework.dao.TransientDataAccessResourceException;
|
||||
import org.springframework.jdbc.BadSqlGrammarException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
public class SQLExceptionSubclassTranslatorTests extends TestCase {
|
||||
public class SQLExceptionSubclassTranslatorTests {
|
||||
|
||||
private static SQLErrorCodes ERROR_CODES = new SQLErrorCodes();
|
||||
|
||||
@@ -42,7 +44,8 @@ public class SQLExceptionSubclassTranslatorTests extends TestCase {
|
||||
}
|
||||
|
||||
|
||||
public void testErrorCodeTranslation() {
|
||||
@Test
|
||||
public void errorCodeTranslation() {
|
||||
SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
|
||||
|
||||
SQLException dataIntegrityViolationEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 0);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2015 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,25 +18,27 @@ package org.springframework.jdbc.support;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.jdbc.BadSqlGrammarException;
|
||||
import org.springframework.jdbc.UncategorizedSQLException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @since 13-Jan-03
|
||||
*/
|
||||
public class SQLStateExceptionTranslatorTests extends TestCase {
|
||||
public class SQLStateExceptionTranslatorTests {
|
||||
|
||||
private SQLStateSQLExceptionTranslator trans = new SQLStateSQLExceptionTranslator();
|
||||
private static final String sql = "SELECT FOO FROM BAR";
|
||||
|
||||
private final SQLStateSQLExceptionTranslator trans = new SQLStateSQLExceptionTranslator();
|
||||
|
||||
// ALSO CHECK CHAIN of SQLExceptions!?
|
||||
// also allow chain of translators? default if can't do specific?
|
||||
|
||||
public void testBadSqlGrammar() {
|
||||
String sql = "SELECT FOO FROM BAR";
|
||||
@Test
|
||||
public void badSqlGrammar() {
|
||||
SQLException sex = new SQLException("Message", "42001", 1);
|
||||
try {
|
||||
throw this.trans.translate("task", sql, sex);
|
||||
@@ -48,8 +50,8 @@ public class SQLStateExceptionTranslatorTests extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testInvalidSqlStateCode() {
|
||||
String sql = "SELECT FOO FROM BAR";
|
||||
@Test
|
||||
public void invalidSqlStateCode() {
|
||||
SQLException sex = new SQLException("Message", "NO SUCH CODE", 1);
|
||||
try {
|
||||
throw this.trans.translate("task", sql, sex);
|
||||
@@ -62,11 +64,12 @@ public class SQLStateExceptionTranslatorTests extends TestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* PostgreSQL can return null
|
||||
* SAP DB can apparently return empty SQL code
|
||||
* PostgreSQL can return null.
|
||||
* SAP DB can apparently return empty SQL code.
|
||||
* Bug 729170
|
||||
*/
|
||||
public void testMalformedSqlStateCodes() {
|
||||
@Test
|
||||
public void malformedSqlStateCodes() {
|
||||
SQLException sex = new SQLException("Message", null, 1);
|
||||
testMalformedSqlStateCode(sex);
|
||||
|
||||
@@ -80,7 +83,6 @@ public class SQLStateExceptionTranslatorTests extends TestCase {
|
||||
|
||||
|
||||
private void testMalformedSqlStateCode(SQLException sex) {
|
||||
String sql = "SELECT FOO FROM BAR";
|
||||
try {
|
||||
throw this.trans.translate("task", sql, sex);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user