diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/CannotReadScriptException.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/CannotReadScriptException.java index 05d389a0c84..6770fe44f87 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/CannotReadScriptException.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/CannotReadScriptException.java @@ -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 */ diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/CompositeDatabasePopulator.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/CompositeDatabasePopulator.java index 18960829319..8f59e16f366 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/CompositeDatabasePopulator.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/CompositeDatabasePopulator.java @@ -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 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); } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptException.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptException.java index 4699efff7e1..eb782d1e983 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptException.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptException.java @@ -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 */ diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptParseException.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptParseException.java index 29cd879146d..7c9f3f06b17 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptParseException.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptParseException.java @@ -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 diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/UncategorizedScriptException.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/UncategorizedScriptException.java index ad9d6b62bcf..e29a2fc2568 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/UncategorizedScriptException.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/UncategorizedScriptException.java @@ -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 */ diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/AbstractDatabaseInitializationTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/AbstractDatabaseInitializationTests.java index 80c523bc2ce..3632c40a2ac 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/AbstractDatabaseInitializationTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/AbstractDatabaseInitializationTests.java @@ -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); 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 index d24c7dfed70..0745c5cada8 100644 --- 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 @@ -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(mockedDatabasePopulator1, times(1)).populate(mockedConnection); verify(mockedDatabasePopulator2, times(1)).populate(mockedConnection); } @Test - public void setPopulatorsWithMultiple() 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 - public void setPopulatorsForOverride() throws SQLException { + 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 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/init/H2DatabasePopulatorTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/H2DatabasePopulatorIntegrationTests.java similarity index 95% rename from spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/H2DatabasePopulatorTests.java rename to spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/H2DatabasePopulatorIntegrationTests.java index e61ebab4aa6..37ea29bbf85 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/H2DatabasePopulatorTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/H2DatabasePopulatorIntegrationTests.java @@ -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() { diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulatorTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulatorUnitTests.java similarity index 67% rename from spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulatorTests.java rename to spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulatorUnitTests.java index deca1410457..f01c7c09013 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulatorTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulatorUnitTests.java @@ -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); } } diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/CannotReadScriptException.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/CannotReadScriptException.java index 68b9336b273..53909fef33d 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/CannotReadScriptException.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/CannotReadScriptException.java @@ -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); diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/CompositeDatabasePopulator.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/CompositeDatabasePopulator.java index 7082ad0f5a4..3d63698f570 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/CompositeDatabasePopulator.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/CompositeDatabasePopulator.java @@ -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 populators) { - Assert.notNull(populators, "Collection of DatabasePopulator must not be null"); + Assert.notNull(populators, "DatabasePopulators must not be null"); this.populators.addAll(populators); } diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ConnectionFactoryInitializer.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ConnectionFactoryInitializer.java index aef7f60bcb4..bfd89a9596c 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ConnectionFactoryInitializer.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ConnectionFactoryInitializer.java @@ -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. *

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() { diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/DatabasePopulator.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/DatabasePopulator.java index b2c63133a0a..467a25549cf 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/DatabasePopulator.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/DatabasePopulator.java @@ -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 diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptException.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptException.java index dd6b1626937..17491c476af 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptException.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptException.java @@ -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 */ diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptParseException.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptParseException.java index 54cc2981785..1b9d6883a9f 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptParseException.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptParseException.java @@ -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 */ diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptStatementFailedException.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptStatementFailedException.java index 7fc84fc9847..259c532bc71 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptStatementFailedException.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ScriptStatementFailedException.java @@ -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 */ diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/UncategorizedScriptException.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/UncategorizedScriptException.java index bd0eb04d9da..83c0dc0ed45 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/UncategorizedScriptException.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/UncategorizedScriptException.java @@ -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 */ diff --git a/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/AbstractDatabaseInitializationTests.java b/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/AbstractDatabasePopulatorTests.java similarity index 86% rename from spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/AbstractDatabaseInitializationTests.java rename to spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/AbstractDatabasePopulatorTests.java index dd8b13c2c2b..fe281681b53 100644 --- a/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/AbstractDatabaseInitializationTests.java +++ b/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/AbstractDatabasePopulatorTests.java @@ -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(); } } diff --git a/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/CompositeDatabasePopulatorTests.java b/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/CompositeDatabasePopulatorTests.java index baf257a6bf7..2b24c1132d3 100644 --- a/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/CompositeDatabasePopulatorTests.java +++ b/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/CompositeDatabasePopulatorTests.java @@ -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 populators = new LinkedHashSet<>(); populators.add(mockedDatabasePopulator1); populators.add(mockedDatabasePopulator2); diff --git a/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/ConnectionFactoryInitializerUnitTests.java b/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/ConnectionFactoryInitializerUnitTests.java index eadea3a14d1..0158fc5d9e0 100644 --- a/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/ConnectionFactoryInitializerUnitTests.java +++ b/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/ConnectionFactoryInitializerUnitTests.java @@ -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. 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. 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(); diff --git a/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/H2DatabasePopulatorIntegrationTests.java b/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/H2DatabasePopulatorIntegrationTests.java index 643beb6301f..e11cbe95cb2 100644 --- a/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/H2DatabasePopulatorIntegrationTests.java +++ b/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/H2DatabasePopulatorIntegrationTests.java @@ -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"); - } - } diff --git a/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/ResourceDatabasePopulatorUnitTests.java b/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/ResourceDatabasePopulatorUnitTests.java index af84b979707..4b066bb86ba 100644 --- a/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/ResourceDatabasePopulatorUnitTests.java +++ b/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/init/ResourceDatabasePopulatorUnitTests.java @@ -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(); diff --git a/spring-r2dbc/src/test/resources/org/springframework/r2dbc/connection/init/db-test-data-h2.sql b/spring-r2dbc/src/test/resources/org/springframework/r2dbc/connection/init/db-test-data-h2.sql deleted file mode 100644 index a62e920a2cc..00000000000 --- a/spring-r2dbc/src/test/resources/org/springframework/r2dbc/connection/init/db-test-data-h2.sql +++ /dev/null @@ -1 +0,0 @@ -INSERT INTO users(first_name, last_name) values('Walter', 'White');