diff options
Diffstat (limited to 'spring-jdbc/src/test/java/org/springframework')
9 files changed, 133 insertions, 19 deletions
diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java index 6615c885..d15f94b4 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 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. @@ -48,6 +48,7 @@ import static org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFacto * @author Juergen Hoeller * @author Chris Beams * @author Sam Brannen + * @author Stephane Nicoll */ public class JdbcNamespaceIntegrationTests { @@ -165,6 +166,16 @@ public class JdbcNamespaceIntegrationTests { assertBeanPropertyValueOf("databaseName", "secondDataSource", factory); } + @Test + public void initializeWithCustomSeparator() throws Exception { + assertCorrectSetupAndCloseContext("jdbc-initialize-custom-separator.xml", 2, "dataSource"); + } + + @Test + public void embeddedWithCustomSeparator() throws Exception { + assertCorrectSetupAndCloseContext("jdbc-config-custom-separator.xml", 2, "dataSource"); + } + private ClassPathXmlApplicationContext context(String file) { return new ClassPathXmlApplicationContext(file, getClass()); } diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateQueryTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateQueryTests.java index bbcc5657..f8fc598b 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateQueryTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateQueryTests.java @@ -150,7 +150,8 @@ public class JdbcTemplateQueryTests { this.thrown.expect(IncorrectResultSizeDataAccessException.class); try { this.template.queryForObject(sql, String.class); - } finally { + } + finally { verify(this.resultSet).close(); verify(this.statement).close(); } diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceJtaTransactionTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceJtaTransactionTests.java index c5247749..3cda75ea 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceJtaTransactionTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceJtaTransactionTests.java @@ -292,13 +292,14 @@ public class DataSourceJtaTransactionTests { assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); verify(userTransaction, times(6)).begin(); verify(transactionManager, times(5)).resume(transaction); - if(rollback) { + if (rollback) { verify(userTransaction, times(5)).commit(); verify(userTransaction).rollback(); - } else { + } + else { verify(userTransaction, times(6)).commit(); } - if(accessAfterResume && !openOuterConnection) { + if (accessAfterResume && !openOuterConnection) { verify(connection, times(7)).close(); } else { @@ -528,7 +529,7 @@ public class DataSourceJtaTransactionTests { assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); verify(userTransaction).begin(); - if(suspendException) { + if (suspendException) { verify(userTransaction).rollback(); } diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceTransactionManagerTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceTransactionManagerTests.java index e4ecd2fc..211445a1 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceTransactionManagerTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceTransactionManagerTests.java @@ -152,7 +152,7 @@ public class DataSourceTransactionManagerTests { assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse)); assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive()); - if(autoCommit && (!lazyConnection || createStatement)) { + if (autoCommit && (!lazyConnection || createStatement)) { InOrder ordered = inOrder(con); ordered.verify(con).setAutoCommit(false); ordered.verify(con).commit(); @@ -244,7 +244,7 @@ public class DataSourceTransactionManagerTests { assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds)); assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive()); - if(autoCommit && (!lazyConnection || createStatement)) { + if (autoCommit && (!lazyConnection || createStatement)) { InOrder ordered = inOrder(con); ordered.verify(con).setAutoCommit(false); ordered.verify(con).rollback(); diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/CompositeDatabasePopulatorTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/CompositeDatabasePopulatorTests.java new file mode 100644 index 00000000..d3c3b7f7 --- /dev/null +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/CompositeDatabasePopulatorTests.java @@ -0,0 +1,92 @@ +/* + * 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. + * 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.datasource.init; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.LinkedHashSet; +import java.util.Set; + +import org.junit.Test; + +import static org.mockito.Mockito.*; + +/** + * Unit tests for {@link CompositeDatabasePopulator}. + * + * @author Kazuki Shimizu + * @author Juergen Hoeller + * @since 4.3 + */ +public class CompositeDatabasePopulatorTests { + + private final Connection mockedConnection = mock(Connection.class); + + private final DatabasePopulator mockedDatabasePopulator1 = mock(DatabasePopulator.class); + + private final DatabasePopulator mockedDatabasePopulator2 = mock(DatabasePopulator.class); + + + @Test + public void addPopulators() throws SQLException { + CompositeDatabasePopulator populator = new CompositeDatabasePopulator(); + populator.addPopulators(mockedDatabasePopulator1, mockedDatabasePopulator2); + populator.populate(mockedConnection); + verify(mockedDatabasePopulator1,times(1)).populate(mockedConnection); + verify(mockedDatabasePopulator2, times(1)).populate(mockedConnection); + } + + @Test + public void setPopulatorsWithMultiple() throws SQLException { + CompositeDatabasePopulator populator = new CompositeDatabasePopulator(); + populator.setPopulators(mockedDatabasePopulator1, mockedDatabasePopulator2); // multiple + populator.populate(mockedConnection); + verify(mockedDatabasePopulator1, times(1)).populate(mockedConnection); + verify(mockedDatabasePopulator2, times(1)).populate(mockedConnection); + } + + @Test + public void setPopulatorsForOverride() throws SQLException { + CompositeDatabasePopulator populator = new CompositeDatabasePopulator(); + populator.setPopulators(mockedDatabasePopulator1); + populator.setPopulators(mockedDatabasePopulator2); // override + populator.populate(mockedConnection); + verify(mockedDatabasePopulator1, times(0)).populate(mockedConnection); + verify(mockedDatabasePopulator2, times(1)).populate(mockedConnection); + } + + @Test + public void constructWithVarargs() throws SQLException { + CompositeDatabasePopulator populator = + new CompositeDatabasePopulator(mockedDatabasePopulator1, mockedDatabasePopulator2); + populator.populate(mockedConnection); + verify(mockedDatabasePopulator1, times(1)).populate(mockedConnection); + verify(mockedDatabasePopulator2, times(1)).populate(mockedConnection); + } + + @Test + public void constructWithCollection() throws SQLException { + Set<DatabasePopulator> populators = new LinkedHashSet<>(); + populators.add(mockedDatabasePopulator1); + populators.add(mockedDatabasePopulator2); + CompositeDatabasePopulator populator = new CompositeDatabasePopulator(populators); + populator.populate(mockedConnection); + verify(mockedDatabasePopulator1, times(1)).populate(mockedConnection); + verify(mockedDatabasePopulator2, times(1)).populate(mockedConnection); + } + +} diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/lookup/BeanFactoryDataSourceLookupTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/lookup/BeanFactoryDataSourceLookupTests.java index a1b19afa..3cef22b8 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/lookup/BeanFactoryDataSourceLookupTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/lookup/BeanFactoryDataSourceLookupTests.java @@ -63,10 +63,11 @@ public class BeanFactoryDataSourceLookupTests { BeanFactoryDataSourceLookup lookup = new BeanFactoryDataSourceLookup(beanFactory); lookup.getDataSource(DATASOURCE_BEAN_NAME); fail("should have thrown DataSourceLookupFailureException"); - } catch (DataSourceLookupFailureException ex) { /* expected */ } + } + catch (DataSourceLookupFailureException ex) { /* expected */ } } - @Test(expected=IllegalStateException.class) + @Test(expected = IllegalStateException.class) public void testLookupWhereBeanFactoryHasNotBeenSupplied() throws Exception { BeanFactoryDataSourceLookup lookup = new BeanFactoryDataSourceLookup(); lookup.getDataSource(DATASOURCE_BEAN_NAME); diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/object/GenericSqlQueryTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/object/GenericSqlQueryTests.java index ed32b628..5a90e703 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/object/GenericSqlQueryTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/object/GenericSqlQueryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 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; @@ -43,11 +42,12 @@ import static org.mockito.BDDMockito.*; /** * @author Thomas Risberg + * @author Juergen Hoeller */ 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; @@ -57,6 +57,7 @@ public class GenericSqlQueryTests { private ResultSet resultSet; + @Before public void setUp() throws Exception { this.beanFactory = new DefaultListableBeanFactory(); @@ -72,17 +73,23 @@ 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 { + public void testCustomerQueryWithNamedParameters() throws SQLException { SqlQuery<?> query = (SqlQuery<?>) beanFactory.getBean("queryWithNamedParameters"); doTestCustomerQuery(query, true); } + @Test + public void testCustomerQueryWithRowMapperInstance() throws SQLException { + SqlQuery<?> query = (SqlQuery<?>) beanFactory.getBean("queryWithRowMapperBean"); + doTestCustomerQuery(query, true); + } + private void doTestCustomerQuery(SqlQuery<?> query, boolean namedParameters) throws SQLException { given(resultSet.next()).willReturn(true); given(resultSet.getInt("id")).willReturn(1); diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/object/StoredProcedureTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/object/StoredProcedureTests.java index 73427778..4c40aa99 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/object/StoredProcedureTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/object/StoredProcedureTests.java @@ -81,7 +81,7 @@ public class StoredProcedureTests { @After public void verifyClosed() throws Exception { - if(verifyClosedAfter) { + if (verifyClosedAfter) { verify(callableStatement).close(); verify(connection, atLeastOnce()).close(); } @@ -189,7 +189,7 @@ public class StoredProcedureTests { TestJdbcTemplate t = new TestJdbcTemplate(); t.setDataSource(dataSource); // Will fail without the following, because we're not able to get a connection - // from the DataSource here if we need to to create an ExceptionTranslator + // from the DataSource here if we need to create an ExceptionTranslator t.setExceptionTranslator(new SQLStateSQLExceptionTranslator()); StoredProcedureConfiguredViaJdbcTemplate sp = new StoredProcedureConfiguredViaJdbcTemplate(t); diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/support/rowset/ResultSetWrappingRowSetTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/support/rowset/ResultSetWrappingRowSetTests.java index 1637764d..0b048d08 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/support/rowset/ResultSetWrappingRowSetTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/support/rowset/ResultSetWrappingRowSetTests.java @@ -205,7 +205,8 @@ public class ResultSetWrappingRowSetTests { if (arg instanceof String) { given(rset.findColumn((String) arg)).willReturn(1); given(rsetMethod.invoke(rset, 1)).willReturn(ret).willThrow(new SQLException("test")); - } else { + } + else { given(rsetMethod.invoke(rset, arg)).willReturn(ret).willThrow(new SQLException("test")); } rowsetMethod.invoke(rowset, arg); |