Add missing author tags and sync SQL script support for JDBC & R2DBC

This commit is contained in:
Sam Brannen
2021-05-16 17:18:56 +02:00
parent 8da049b613
commit c80c4e001a
23 changed files with 176 additions and 171 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2021 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.
@@ -29,7 +29,7 @@ import org.springframework.core.io.support.EncodedResource;
public class CannotReadScriptException extends ScriptException {
/**
* Construct a new {@code CannotReadScriptException}.
* Create a new {@code CannotReadScriptException}.
* @param resource the resource that cannot be read from
* @param cause the underlying cause of the resource access failure
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2021 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.
@@ -23,6 +23,8 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.springframework.util.Assert;
/**
* Composite {@link DatabasePopulator} that delegates to a list of given
* {@code DatabasePopulator} implementations, executing all scripts.
@@ -52,6 +54,7 @@ public class CompositeDatabasePopulator implements DatabasePopulator {
* @since 4.3
*/
public CompositeDatabasePopulator(Collection<DatabasePopulator> populators) {
Assert.notNull(populators, "DatabasePopulators must not be null");
this.populators.addAll(populators);
}
@@ -61,6 +64,7 @@ public class CompositeDatabasePopulator implements DatabasePopulator {
* @since 4.3
*/
public CompositeDatabasePopulator(DatabasePopulator... populators) {
Assert.notNull(populators, "DatabasePopulators must not be null");
this.populators.addAll(Arrays.asList(populators));
}
@@ -69,6 +73,7 @@ public class CompositeDatabasePopulator implements DatabasePopulator {
* Specify one or more populators to delegate to.
*/
public void setPopulators(DatabasePopulator... populators) {
Assert.notNull(populators, "DatabasePopulators must not be null");
this.populators.clear();
this.populators.addAll(Arrays.asList(populators));
}
@@ -77,12 +82,13 @@ public class CompositeDatabasePopulator implements DatabasePopulator {
* Add one or more populators to the list of delegates.
*/
public void addPopulators(DatabasePopulator... populators) {
Assert.notNull(populators, "DatabasePopulators must not be null");
this.populators.addAll(Arrays.asList(populators));
}
@Override
public void populate(Connection connection) throws SQLException, ScriptException {
Assert.notNull(connection, "Connection must not be null");
for (DatabasePopulator populator : this.populators) {
populator.populate(connection);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2021 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.
@@ -30,7 +30,7 @@ import org.springframework.lang.Nullable;
public abstract class ScriptException extends DataAccessException {
/**
* Constructor for {@code ScriptException}.
* Create a new {@code ScriptException}.
* @param message the detail message
*/
public ScriptException(String message) {
@@ -38,7 +38,7 @@ public abstract class ScriptException extends DataAccessException {
}
/**
* Constructor for {@code ScriptException}.
* Create a new {@code ScriptException}.
* @param message the detail message
* @param cause the root cause
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2021 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.
@@ -29,7 +29,7 @@ import org.springframework.lang.Nullable;
public class ScriptParseException extends ScriptException {
/**
* Construct a new {@code ScriptParseException}.
* Create a new {@code ScriptParseException}.
* @param message detailed message
* @param resource the resource from which the SQL script was read
*/
@@ -38,7 +38,7 @@ public class ScriptParseException extends ScriptException {
}
/**
* Construct a new {@code ScriptParseException}.
* Create a new {@code ScriptParseException}.
* @param message detailed message
* @param resource the resource from which the SQL script was read
* @param cause the underlying cause of the failure

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2021 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.
@@ -28,7 +28,7 @@ package org.springframework.jdbc.datasource.init;
public class UncategorizedScriptException extends ScriptException {
/**
* Construct a new {@code UncategorizedScriptException}.
* Create a new {@code UncategorizedScriptException}.
* @param message detailed message
*/
public UncategorizedScriptException(String message) {
@@ -36,7 +36,7 @@ public class UncategorizedScriptException extends ScriptException {
}
/**
* Construct a new {@code UncategorizedScriptException}.
* Create a new {@code UncategorizedScriptException}.
* @param message detailed message
* @param cause the root cause
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
@@ -29,15 +29,13 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
import static org.assertj.core.api.Assertions.assertThat;
/**
* Abstract base class for integration tests involving database initialization.
*
* @author Sam Brannen
* @since 4.0.3
*/
public abstract class AbstractDatabaseInitializationTests {
abstract class AbstractDatabaseInitializationTests {
private final ClassRelativeResourceLoader resourceLoader = new ClassRelativeResourceLoader(getClass());
@@ -47,13 +45,13 @@ public abstract class AbstractDatabaseInitializationTests {
@BeforeEach
public void setUp() {
void setUp() {
db = new EmbeddedDatabaseBuilder().setType(getEmbeddedDatabaseType()).build();
jdbcTemplate = new JdbcTemplate(db);
}
@AfterEach
public void shutDown() {
void shutDown() {
if (TransactionSynchronizationManager.isSynchronizationActive()) {
TransactionSynchronizationManager.clear();
TransactionSynchronizationManager.unbindResource(db);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
@@ -34,7 +34,7 @@ import static org.mockito.Mockito.verify;
* @author Juergen Hoeller
* @since 4.3
*/
public class CompositeDatabasePopulatorTests {
class CompositeDatabasePopulatorTests {
private final Connection mockedConnection = mock(Connection.class);
@@ -44,49 +44,59 @@ public class CompositeDatabasePopulatorTests {
@Test
public void addPopulators() throws SQLException {
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 {
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
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 {
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 {
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);
}

View File

@@ -26,7 +26,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 4.0.3
*/
class H2DatabasePopulatorTests extends AbstractDatabasePopulatorTests {
class H2DatabasePopulatorIntegrationTests extends AbstractDatabasePopulatorTests {
@Override
protected EmbeddedDatabaseType getEmbeddedDatabaseType() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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,12 +17,12 @@
package org.springframework.jdbc.datasource.init;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.core.io.Resource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.BDDMockito.mock;
/**
* Unit tests for {@link ResourceDatabasePopulator}.
@@ -31,84 +31,84 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
* @since 4.1
* @see AbstractDatabasePopulatorTests
*/
public class ResourceDatabasePopulatorTests {
class ResourceDatabasePopulatorUnitTests {
private static final Resource script1 = Mockito.mock(Resource.class);
private static final Resource script2 = Mockito.mock(Resource.class);
private static final Resource script3 = Mockito.mock(Resource.class);
private static final Resource script1 = mock(Resource.class);
private static final Resource script2 = mock(Resource.class);
private static final Resource script3 = mock(Resource.class);
@Test
public void constructWithNullResource() {
void constructWithNullResource() {
assertThatIllegalArgumentException().isThrownBy(() ->
new ResourceDatabasePopulator((Resource) null));
}
@Test
public void constructWithNullResourceArray() {
void constructWithNullResourceArray() {
assertThatIllegalArgumentException().isThrownBy(() ->
new ResourceDatabasePopulator((Resource[]) null));
}
@Test
public void constructWithResource() {
void constructWithResource() {
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(script1);
assertThat(databasePopulator.scripts.size()).isEqualTo(1);
assertThat(databasePopulator.scripts).hasSize(1);
}
@Test
public void constructWithMultipleResources() {
void constructWithMultipleResources() {
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(script1, script2);
assertThat(databasePopulator.scripts.size()).isEqualTo(2);
assertThat(databasePopulator.scripts).hasSize(2);
}
@Test
public void constructWithMultipleResourcesAndThenAddScript() {
void constructWithMultipleResourcesAndThenAddScript() {
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(script1, script2);
assertThat(databasePopulator.scripts.size()).isEqualTo(2);
assertThat(databasePopulator.scripts).hasSize(2);
databasePopulator.addScript(script3);
assertThat(databasePopulator.scripts.size()).isEqualTo(3);
assertThat(databasePopulator.scripts).hasSize(3);
}
@Test
public void addScriptsWithNullResource() {
void addScriptsWithNullResource() {
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
assertThatIllegalArgumentException().isThrownBy(() ->
databasePopulator.addScripts((Resource) null));
}
@Test
public void addScriptsWithNullResourceArray() {
void addScriptsWithNullResourceArray() {
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
assertThatIllegalArgumentException().isThrownBy(() ->
databasePopulator.addScripts((Resource[]) null));
}
@Test
public void setScriptsWithNullResource() {
void setScriptsWithNullResource() {
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
assertThatIllegalArgumentException().isThrownBy(() ->
databasePopulator.setScripts((Resource) null));
}
@Test
public void setScriptsWithNullResourceArray() {
void setScriptsWithNullResourceArray() {
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
assertThatIllegalArgumentException().isThrownBy(() ->
databasePopulator.setScripts((Resource[]) null));
}
@Test
public void setScriptsAndThenAddScript() {
void setScriptsAndThenAddScript() {
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
assertThat(databasePopulator.scripts.size()).isEqualTo(0);
assertThat(databasePopulator.scripts).isEmpty();
databasePopulator.setScripts(script1, script2);
assertThat(databasePopulator.scripts.size()).isEqualTo(2);
assertThat(databasePopulator.scripts).hasSize(2);
databasePopulator.addScript(script3);
assertThat(databasePopulator.scripts.size()).isEqualTo(3);
assertThat(databasePopulator.scripts).hasSize(3);
}
}