Add missing author tags and sync SQL script support for JDBC & R2DBC
This commit is contained in:
parent
8da049b613
commit
c80c4e001a
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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() {
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -21,6 +21,8 @@ import org.springframework.core.io.support.EncodedResource;
|
|||
/**
|
||||
* Thrown by {@link ScriptUtils} if an SQL script cannot be read.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @author Sam Brannen
|
||||
* @author Mark Paluch
|
||||
* @since 5.3
|
||||
*/
|
||||
|
@ -29,8 +31,8 @@ public class CannotReadScriptException extends ScriptException {
|
|||
|
||||
/**
|
||||
* Create a new {@code CannotReadScriptException}.
|
||||
* @param resource the resource that cannot be read from.
|
||||
* @param cause the underlying cause of the resource access failure.
|
||||
* @param resource the resource that cannot be read from
|
||||
* @param cause the underlying cause of the resource access failure
|
||||
*/
|
||||
public CannotReadScriptException(EncodedResource resource, Throwable cause) {
|
||||
super("Cannot read SQL script from " + resource, cause);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2020 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,8 +29,12 @@ import org.springframework.util.Assert;
|
|||
|
||||
/**
|
||||
* Composite {@link DatabasePopulator} that delegates to a list of given
|
||||
* {@link DatabasePopulator} implementations, executing all scripts.
|
||||
* {@code DatabasePopulator} implementations, executing all scripts.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
* @author Kazuki Shimizu
|
||||
* @author Mark Paluch
|
||||
* @since 5.3
|
||||
*/
|
||||
|
@ -44,14 +48,15 @@ public class CompositeDatabasePopulator implements DatabasePopulator {
|
|||
* @see #setPopulators
|
||||
* @see #addPopulators
|
||||
*/
|
||||
public CompositeDatabasePopulator() {}
|
||||
public CompositeDatabasePopulator() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@code CompositeDatabasePopulator}. with the given populators.
|
||||
* @param populators one or more populators to delegate to.
|
||||
*/
|
||||
public CompositeDatabasePopulator(Collection<DatabasePopulator> populators) {
|
||||
Assert.notNull(populators, "Collection of DatabasePopulator must not be null");
|
||||
Assert.notNull(populators, "DatabasePopulators must not be null");
|
||||
this.populators.addAll(populators);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2020 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,6 +28,8 @@ import org.springframework.util.Assert;
|
|||
* initialization and {@link #setDatabaseCleaner clean up} a database during
|
||||
* destruction.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Sam Brannen
|
||||
* @author Mark Paluch
|
||||
* @since 5.3
|
||||
* @see DatabasePopulator
|
||||
|
@ -50,7 +52,7 @@ public class ConnectionFactoryInitializer implements InitializingBean, Disposabl
|
|||
* The {@link ConnectionFactory} for the database to populate when this
|
||||
* component is initialized and to clean up when this component is shut down.
|
||||
* <p>This property is mandatory with no default provided.
|
||||
* @param connectionFactory the R2DBC {@link ConnectionFactory}.
|
||||
* @param connectionFactory the R2DBC {@link ConnectionFactory}
|
||||
*/
|
||||
public void setConnectionFactory(ConnectionFactory connectionFactory) {
|
||||
this.connectionFactory = connectionFactory;
|
||||
|
@ -66,10 +68,9 @@ public class ConnectionFactoryInitializer implements InitializingBean, Disposabl
|
|||
}
|
||||
|
||||
/**
|
||||
* Set the {@link DatabasePopulator} to execute during the bean destruction phase, cleaning up the database and
|
||||
* leaving it in a known state for others.
|
||||
*
|
||||
* @param databaseCleaner the {@link DatabasePopulator} to use during destruction
|
||||
* Set the {@link DatabasePopulator} to execute during the bean destruction
|
||||
* phase, cleaning up the database and leaving it in a known state for others.
|
||||
* @param databaseCleaner the {@code DatabasePopulator} to use during destruction
|
||||
* @see #setDatabasePopulator
|
||||
*/
|
||||
public void setDatabaseCleaner(DatabasePopulator databaseCleaner) {
|
||||
|
@ -77,8 +78,8 @@ public class ConnectionFactoryInitializer implements InitializingBean, Disposabl
|
|||
}
|
||||
|
||||
/**
|
||||
* Flag to explicitly enable or disable the {@link #setDatabasePopulator database populator}
|
||||
* and {@link #setDatabaseCleaner database cleaner}.
|
||||
* Flag to explicitly enable or disable the {@linkplain #setDatabasePopulator
|
||||
* database populator} and {@linkplain #setDatabaseCleaner database cleaner}.
|
||||
* @param enabled {@code true} if the database populator and database cleaner
|
||||
* should be called on startup and shutdown, respectively
|
||||
*/
|
||||
|
@ -88,7 +89,8 @@ public class ConnectionFactoryInitializer implements InitializingBean, Disposabl
|
|||
|
||||
|
||||
/**
|
||||
* Use the {@link #setDatabasePopulator database populator} to set up the database.
|
||||
* Use the {@linkplain #setDatabasePopulator database populator} to set up
|
||||
* the database.
|
||||
*/
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
|
@ -96,7 +98,8 @@ public class ConnectionFactoryInitializer implements InitializingBean, Disposabl
|
|||
}
|
||||
|
||||
/**
|
||||
* Use the {@link #setDatabaseCleaner database cleaner} to clean up the database.
|
||||
* Use the {@linkplain #setDatabaseCleaner database cleaner} to clean up the
|
||||
* database.
|
||||
*/
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2020 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,6 +28,8 @@ import org.springframework.util.Assert;
|
|||
* Strategy used to populate, initialize, or clean up a database.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Keith Donald
|
||||
* @author Sam Brannen
|
||||
* @since 5.3
|
||||
* @see ResourceDatabasePopulator
|
||||
* @see ConnectionFactoryInitializer
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -20,8 +20,10 @@ import org.springframework.dao.DataAccessException;
|
|||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Root of the hierarchy of data access exceptions that are related to processing of SQL scripts.
|
||||
* Root of the hierarchy of data access exceptions that are related to processing
|
||||
* of SQL scripts.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @author Mark Paluch
|
||||
* @since 5.3
|
||||
*/
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -22,6 +22,7 @@ import org.springframework.lang.Nullable;
|
|||
/**
|
||||
* Thrown by {@link ScriptUtils} if an SQL script cannot be properly parsed.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @author Mark Paluch
|
||||
* @since 5.3
|
||||
*/
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2015 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.
|
||||
|
@ -22,6 +22,8 @@ import org.springframework.core.io.support.EncodedResource;
|
|||
* Thrown by {@link ScriptUtils} if a statement in an SQL script failed when
|
||||
* executing it against the target database.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
* @author Mark Paluch
|
||||
* @since 5.3
|
||||
*/
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2020 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,10 +17,11 @@
|
|||
package org.springframework.r2dbc.connection.init;
|
||||
|
||||
/**
|
||||
* Thrown when we cannot determine anything more specific than "something went wrong while
|
||||
* processing an SQL script": for example, a {@link io.r2dbc.spi.R2dbcException} from
|
||||
* R2DBC that we cannot pinpoint more precisely.
|
||||
* Thrown when we cannot determine anything more specific than "something went wrong
|
||||
* while processing an SQL script": for example, an {@link io.r2dbc.spi.R2dbcException}
|
||||
* from R2DBC that we cannot pinpoint more precisely.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @author Mark Paluch
|
||||
* @since 5.3
|
||||
*/
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -24,22 +24,23 @@ import org.springframework.core.io.ClassRelativeResourceLoader;
|
|||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.r2dbc.core.DatabaseClient;
|
||||
|
||||
|
||||
/**
|
||||
* Abstract test support for {@link DatabasePopulator}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Sam Brannen
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public abstract class AbstractDatabaseInitializationTests {
|
||||
abstract class AbstractDatabasePopulatorTests {
|
||||
|
||||
ClassRelativeResourceLoader resourceLoader = new ClassRelativeResourceLoader(
|
||||
getClass());
|
||||
ClassRelativeResourceLoader resourceLoader = new ClassRelativeResourceLoader(getClass());
|
||||
|
||||
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
|
||||
|
||||
|
||||
@Test
|
||||
public void scriptWithSingleLineCommentsAndFailedDrop() {
|
||||
void scriptWithSingleLineCommentsAndFailedDrop() {
|
||||
databasePopulator.addScript(resource("db-schema-failed-drop-comments.sql"));
|
||||
databasePopulator.addScript(resource("db-test-data.sql"));
|
||||
databasePopulator.setIgnoreFailedDrops(true);
|
||||
|
@ -56,7 +57,7 @@ public abstract class AbstractDatabaseInitializationTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void scriptWithStandardEscapedLiteral() {
|
||||
void scriptWithStandardEscapedLiteral() {
|
||||
databasePopulator.addScript(defaultSchema());
|
||||
databasePopulator.addScript(resource("db-test-data-escaped-literal.sql"));
|
||||
|
||||
|
@ -66,7 +67,7 @@ public abstract class AbstractDatabaseInitializationTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void scriptWithMySqlEscapedLiteral() {
|
||||
void scriptWithMySqlEscapedLiteral() {
|
||||
databasePopulator.addScript(defaultSchema());
|
||||
databasePopulator.addScript(resource("db-test-data-mysql-escaped-literal.sql"));
|
||||
|
||||
|
@ -76,7 +77,7 @@ public abstract class AbstractDatabaseInitializationTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void scriptWithMultipleStatements() {
|
||||
void scriptWithMultipleStatements() {
|
||||
databasePopulator.addScript(defaultSchema());
|
||||
databasePopulator.addScript(resource("db-test-data-multiple.sql"));
|
||||
|
||||
|
@ -86,7 +87,7 @@ public abstract class AbstractDatabaseInitializationTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void scriptWithMultipleStatementsAndLongSeparator() {
|
||||
void scriptWithMultipleStatementsAndLongSeparator() {
|
||||
databasePopulator.addScript(defaultSchema());
|
||||
databasePopulator.addScript(resource("db-test-data-endings.sql"));
|
||||
databasePopulator.setSeparator("@@");
|
||||
|
@ -120,15 +121,13 @@ public abstract class AbstractDatabaseInitializationTests {
|
|||
DatabaseClient client = DatabaseClient.create(connectionFactory);
|
||||
|
||||
for (String lastName : lastNames) {
|
||||
|
||||
client.sql("select count(0) from users where last_name = :name") //
|
||||
.bind("name", lastName) //
|
||||
.map((row, metadata) -> row.get(0)) //
|
||||
.first() //
|
||||
.map(number -> ((Number) number).intValue()) //
|
||||
.as(StepVerifier::create) //
|
||||
.expectNext(1).as(
|
||||
"Did not find user with last name [" + lastName + "].") //
|
||||
.expectNext(1).as("Did not find user with last name [" + lastName + "].") //
|
||||
.verifyComplete();
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -33,9 +33,11 @@ import static org.mockito.BDDMockito.when;
|
|||
/**
|
||||
* Unit tests for {@link CompositeDatabasePopulator}.
|
||||
*
|
||||
* @author Kazuki Shimizu
|
||||
* @author Juergen Hoeller
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class CompositeDatabasePopulatorTests {
|
||||
class CompositeDatabasePopulatorTests {
|
||||
|
||||
Connection mockedConnection = mock(Connection.class);
|
||||
|
||||
|
@ -45,15 +47,13 @@ public class CompositeDatabasePopulatorTests {
|
|||
|
||||
|
||||
@BeforeEach
|
||||
public void before() {
|
||||
when(mockedDatabasePopulator1.populate(mockedConnection)).thenReturn(
|
||||
Mono.empty());
|
||||
when(mockedDatabasePopulator2.populate(mockedConnection)).thenReturn(
|
||||
Mono.empty());
|
||||
void before() {
|
||||
when(mockedDatabasePopulator1.populate(mockedConnection)).thenReturn(Mono.empty());
|
||||
when(mockedDatabasePopulator2.populate(mockedConnection)).thenReturn(Mono.empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addPopulators() {
|
||||
void addPopulators() {
|
||||
CompositeDatabasePopulator populator = new CompositeDatabasePopulator();
|
||||
populator.addPopulators(mockedDatabasePopulator1, mockedDatabasePopulator2);
|
||||
|
||||
|
@ -64,7 +64,7 @@ public class CompositeDatabasePopulatorTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void setPopulatorsWithMultiple() {
|
||||
void setPopulatorsWithMultiple() {
|
||||
CompositeDatabasePopulator populator = new CompositeDatabasePopulator();
|
||||
populator.setPopulators(mockedDatabasePopulator1, mockedDatabasePopulator2); // multiple
|
||||
|
||||
|
@ -75,7 +75,7 @@ public class CompositeDatabasePopulatorTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void setPopulatorsForOverride() {
|
||||
void setPopulatorsForOverride() {
|
||||
CompositeDatabasePopulator populator = new CompositeDatabasePopulator();
|
||||
populator.setPopulators(mockedDatabasePopulator1);
|
||||
populator.setPopulators(mockedDatabasePopulator2); // override
|
||||
|
@ -87,9 +87,9 @@ public class CompositeDatabasePopulatorTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void constructWithVarargs() {
|
||||
CompositeDatabasePopulator populator = new CompositeDatabasePopulator(
|
||||
mockedDatabasePopulator1, mockedDatabasePopulator2);
|
||||
void constructWithVarargs() {
|
||||
CompositeDatabasePopulator populator =
|
||||
new CompositeDatabasePopulator(mockedDatabasePopulator1, mockedDatabasePopulator2);
|
||||
|
||||
populator.populate(mockedConnection).as(StepVerifier::create).verifyComplete();
|
||||
|
||||
|
@ -98,7 +98,7 @@ public class CompositeDatabasePopulatorTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void constructWithCollection() {
|
||||
void constructWithCollection() {
|
||||
Set<DatabasePopulator> populators = new LinkedHashSet<>();
|
||||
populators.add(mockedDatabasePopulator1);
|
||||
populators.add(mockedDatabasePopulator2);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -32,7 +32,7 @@ import static org.mockito.BDDMockito.when;
|
|||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class ConnectionFactoryInitializerUnitTests {
|
||||
class ConnectionFactoryInitializerUnitTests {
|
||||
|
||||
AtomicBoolean called = new AtomicBoolean();
|
||||
|
||||
|
@ -40,12 +40,11 @@ public class ConnectionFactoryInitializerUnitTests {
|
|||
|
||||
MockConnection connection = MockConnection.builder().build();
|
||||
|
||||
MockConnectionFactory connectionFactory = MockConnectionFactory.builder().connection(
|
||||
connection).build();
|
||||
MockConnectionFactory connectionFactory = MockConnectionFactory.builder().connection(connection).build();
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldInitializeConnectionFactory() {
|
||||
void shouldInitializeConnectionFactory() {
|
||||
when(populator.populate(connectionFactory)).thenReturn(
|
||||
Mono.<Void> empty().doOnSubscribe(subscription -> called.set(true)));
|
||||
|
||||
|
@ -59,7 +58,7 @@ public class ConnectionFactoryInitializerUnitTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void shouldCleanConnectionFactory() {
|
||||
void shouldCleanConnectionFactory() {
|
||||
when(populator.populate(connectionFactory)).thenReturn(
|
||||
Mono.<Void> empty().doOnSubscribe(subscription -> called.set(true)));
|
||||
|
||||
|
@ -67,7 +66,6 @@ public class ConnectionFactoryInitializerUnitTests {
|
|||
initializer.setConnectionFactory(connectionFactory);
|
||||
initializer.setDatabaseCleaner(populator);
|
||||
|
||||
initializer.afterPropertiesSet();
|
||||
initializer.destroy();
|
||||
|
||||
assertThat(called).isTrue();
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -20,16 +20,13 @@ import java.util.UUID;
|
|||
|
||||
import io.r2dbc.spi.ConnectionFactories;
|
||||
import io.r2dbc.spi.ConnectionFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link DatabasePopulator} using H2.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class H2DatabasePopulatorIntegrationTests
|
||||
extends AbstractDatabaseInitializationTests {
|
||||
class H2DatabasePopulatorIntegrationTests extends AbstractDatabasePopulatorTests {
|
||||
|
||||
UUID databaseName = UUID.randomUUID();
|
||||
|
||||
|
@ -42,20 +39,4 @@ public class H2DatabasePopulatorIntegrationTests
|
|||
return this.connectionFactory;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRunScript() {
|
||||
|
||||
databasePopulator.addScript(usersSchema());
|
||||
databasePopulator.addScript(resource("db-test-data-h2.sql"));
|
||||
// Set statement separator to double newline so that ";" is not
|
||||
// considered a statement separator within the source code of the
|
||||
// aliased function 'REVERSE'.
|
||||
databasePopulator.setSeparator("\n\n");
|
||||
|
||||
databasePopulator.populate(connectionFactory).as(
|
||||
StepVerifier::create).verifyComplete();
|
||||
|
||||
assertUsersDatabaseCreated(connectionFactory, "White");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -27,47 +27,43 @@ import static org.mockito.BDDMockito.mock;
|
|||
/**
|
||||
* Unit tests for {@link ResourceDatabasePopulator}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class ResourceDatabasePopulatorUnitTests {
|
||||
class ResourceDatabasePopulatorUnitTests {
|
||||
|
||||
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() {
|
||||
assertThatIllegalArgumentException().isThrownBy(
|
||||
() -> new ResourceDatabasePopulator((Resource) null));
|
||||
void constructWithNullResource() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
new ResourceDatabasePopulator((Resource) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructWithNullResourceArray() {
|
||||
assertThatIllegalArgumentException().isThrownBy(
|
||||
() -> new ResourceDatabasePopulator((Resource[]) null));
|
||||
void constructWithNullResourceArray() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
new ResourceDatabasePopulator((Resource[]) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructWithResource() {
|
||||
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(
|
||||
script1);
|
||||
void constructWithResource() {
|
||||
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(script1);
|
||||
assertThat(databasePopulator.scripts).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructWithMultipleResources() {
|
||||
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(
|
||||
script1, script2);
|
||||
void constructWithMultipleResources() {
|
||||
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(script1, script2);
|
||||
assertThat(databasePopulator.scripts).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructWithMultipleResourcesAndThenAddScript() {
|
||||
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(
|
||||
script1, script2);
|
||||
void constructWithMultipleResourcesAndThenAddScript() {
|
||||
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(script1, script2);
|
||||
assertThat(databasePopulator.scripts).hasSize(2);
|
||||
|
||||
databasePopulator.addScript(script3);
|
||||
|
@ -75,35 +71,35 @@ public class ResourceDatabasePopulatorUnitTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void addScriptsWithNullResource() {
|
||||
void addScriptsWithNullResource() {
|
||||
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
|
||||
assertThatIllegalArgumentException().isThrownBy(
|
||||
() -> databasePopulator.addScripts((Resource) null));
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
databasePopulator.addScripts((Resource) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addScriptsWithNullResourceArray() {
|
||||
void addScriptsWithNullResourceArray() {
|
||||
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
|
||||
assertThatIllegalArgumentException().isThrownBy(
|
||||
() -> databasePopulator.addScripts((Resource[]) null));
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
databasePopulator.addScripts((Resource[]) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setScriptsWithNullResource() {
|
||||
void setScriptsWithNullResource() {
|
||||
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
|
||||
assertThatIllegalArgumentException().isThrownBy(
|
||||
() -> databasePopulator.setScripts((Resource) null));
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
databasePopulator.setScripts((Resource) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setScriptsWithNullResourceArray() {
|
||||
void setScriptsWithNullResourceArray() {
|
||||
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
|
||||
assertThatIllegalArgumentException().isThrownBy(
|
||||
() -> databasePopulator.setScripts((Resource[]) null));
|
||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
||||
databasePopulator.setScripts((Resource[]) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setScriptsAndThenAddScript() {
|
||||
void setScriptsAndThenAddScript() {
|
||||
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
|
||||
assertThat(databasePopulator.scripts).isEmpty();
|
||||
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
INSERT INTO users(first_name, last_name) values('Walter', 'White');
|
Loading…
Reference in New Issue