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-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();
}
}

View File

@@ -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);

View File

@@ -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();

View File

@@ -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");
}
}

View File

@@ -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();

View File

@@ -1 +0,0 @@
INSERT INTO users(first_name, last_name) values('Walter', 'White');