Skip to content

Commit 8b411f1

Browse files
haiphucnguyenHai Phuc Nguyen
authored andcommitted
Update
1 parent 024ec9e commit 8b411f1

File tree

6 files changed

+181
-1
lines changed

6 files changed

+181
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.gradle
22
build
3+
release.sh
4+
.env.release

build.gradle.kts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@ subprojects {
4343
from(project.the<SourceSetContainer>()["main"].allSource)
4444
}
4545

46+
val javadocJar by tasks.registering(Jar::class) {
47+
archiveClassifier.set("javadoc")
48+
from(tasks.named("javadoc"))
49+
}
50+
4651
extensions.configure<PublishingExtension>("publishing") {
4752
publications {
4853
create<MavenPublication>("mavenJava") {
4954
from(components["java"])
5055
artifact(sourcesJar.get())
56+
artifact(javadocJar.get())
5157

5258
pom {
5359
name.set("Spring Test Containers")
@@ -81,7 +87,8 @@ subprojects {
8187
repositories {
8288
mavenLocal()
8389
maven {
84-
url = layout.buildDirectory.dir("staging-deploy").get().asFile.toURI()
90+
name = "staging"
91+
url = rootProject.layout.buildDirectory.dir("staging-deploy").get().asFile.toURI()
8592
}
8693
}
8794
}

modules/mysql/src/main/java/io/flowinquiry/testcontainers/jdbc/mysql/MySqlContainerProvider.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,54 @@
44
import io.flowinquiry.testcontainers.jdbc.SpringAwareJdbcContainerProvider;
55
import org.testcontainers.containers.MySQLContainer;
66

7+
/**
8+
* MySQL-specific implementation of the {@link SpringAwareJdbcContainerProvider}.
9+
*
10+
* <p>This class provides support for MySQL database containers in the Spring TestContainers
11+
* framework. It creates and manages a {@link MySQLContainer} instance, which is a TestContainers
12+
* implementation for MySQL databases.
13+
*
14+
* <p>This provider is automatically discovered by the {@link io.flowinquiry.testcontainers.jdbc.JdbcContainerProviderFactory}
15+
* using Java's ServiceLoader mechanism when a test class is annotated with
16+
* {@code @EnableMySQL}.
17+
*
18+
* <p>The provider handles:
19+
* <ul>
20+
* <li>Creating a MySQL container with the specified Docker image and version</li>
21+
* <li>Starting and stopping the container</li>
22+
* <li>Integrating the container with Spring's environment configuration</li>
23+
* </ul>
24+
*
25+
* @see SpringAwareJdbcContainerProvider
26+
* @see MySQLContainer
27+
* @see io.flowinquiry.testcontainers.jdbc.EnableMySQL
28+
*/
729
public class MySqlContainerProvider extends SpringAwareJdbcContainerProvider {
830

31+
/**
32+
* Returns the type of database managed by this provider.
33+
*
34+
* <p>This implementation returns {@link Rdbms#MYSQL}, indicating that this provider
35+
* supports MySQL databases.
36+
*
37+
* @return {@link Rdbms#MYSQL} as the supported database type
38+
*/
939
@Override
1040
public Rdbms getType() {
1141
return Rdbms.MYSQL;
1242
}
1343

44+
/**
45+
* Creates a new MySQL database container.
46+
*
47+
* <p>This method creates a {@link MySQLContainer} instance configured with the Docker image
48+
* and version specified in the {@code @EnableMySQL} annotation on the test class.
49+
*
50+
* <p>The container is created but not started. The {@link #start()} method must be called
51+
* to start the container before it can be used.
52+
*
53+
* @return a new {@link MySQLContainer} instance
54+
*/
1455
@Override
1556
protected MySQLContainer<?> createJdbcDatabaseContainer() {
1657
return new MySQLContainer<>(dockerImage + ":" + version);

modules/postgresql/src/main/java/io/flowinquiry/testcontainers/jdbc/postgresql/PostgreSqlContainerProvider.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,54 @@
55
import org.testcontainers.containers.JdbcDatabaseContainer;
66
import org.testcontainers.containers.PostgreSQLContainer;
77

8+
/**
9+
* PostgreSQL-specific implementation of the {@link SpringAwareJdbcContainerProvider}.
10+
*
11+
* <p>This class provides support for PostgreSQL database containers in the Spring TestContainers
12+
* framework. It creates and manages a {@link PostgreSQLContainer} instance, which is a TestContainers
13+
* implementation for PostgreSQL databases.
14+
*
15+
* <p>This provider is automatically discovered by the {@link io.flowinquiry.testcontainers.jdbc.JdbcContainerProviderFactory}
16+
* using Java's ServiceLoader mechanism when a test class is annotated with
17+
* {@code @EnablePostgreSQL}.
18+
*
19+
* <p>The provider handles:
20+
* <ul>
21+
* <li>Creating a PostgreSQL container with the specified Docker image and version</li>
22+
* <li>Starting and stopping the container</li>
23+
* <li>Integrating the container with Spring's environment configuration</li>
24+
* </ul>
25+
*
26+
* @see SpringAwareJdbcContainerProvider
27+
* @see PostgreSQLContainer
28+
* @see io.flowinquiry.testcontainers.jdbc.EnablePostgreSQL
29+
*/
830
public class PostgreSqlContainerProvider extends SpringAwareJdbcContainerProvider {
931

32+
/**
33+
* Returns the type of database managed by this provider.
34+
*
35+
* <p>This implementation returns {@link Rdbms#POSTGRESQL}, indicating that this provider
36+
* supports PostgreSQL databases.
37+
*
38+
* @return {@link Rdbms#POSTGRESQL} as the supported database type
39+
*/
1040
@Override
1141
public Rdbms getType() {
1242
return Rdbms.POSTGRESQL;
1343
}
1444

45+
/**
46+
* Creates a new PostgreSQL database container.
47+
*
48+
* <p>This method creates a {@link PostgreSQLContainer} instance configured with the Docker image
49+
* and version specified in the {@code @EnablePostgreSQL} annotation on the test class.
50+
*
51+
* <p>The container is created but not started. The {@link #start()} method must be called
52+
* to start the container before it can be used.
53+
*
54+
* @return a new {@link PostgreSQLContainer} instance
55+
*/
1556
@Override
1657
protected JdbcDatabaseContainer<?> createJdbcDatabaseContainer() {
1758
return new PostgreSQLContainer<>(dockerImage + ":" + version);

spring-testcontainers/src/main/java/io/flowinquiry/testcontainers/jdbc/JdbcContainerProviderFactory.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,45 @@
22

33
import java.util.ServiceLoader;
44

5+
/**
6+
* Factory class for creating and configuring JDBC container providers.
7+
*
8+
* <p>This factory is responsible for discovering and initializing the appropriate
9+
* {@link JdbcContainerProvider} implementation based on the database type specified
10+
* in the {@link EnableJdbcContainer} annotation.
11+
*
12+
* <p>The factory uses Java's {@link ServiceLoader} mechanism to discover available
13+
* provider implementations. It selects the provider that matches the requested database
14+
* type, initializes it with the specified Docker image and version, and prepares it
15+
* for use in tests.
16+
*
17+
* <p>This class is primarily used by the {@link JdbcExtension} to create container
18+
* providers for test classes annotated with database-specific annotations like
19+
* {@code @EnablePostgreSQL} or {@code @EnableMySQL}.
20+
*
21+
* @see JdbcContainerProvider
22+
* @see EnableJdbcContainer
23+
* @see ServiceLoader
24+
*/
525
public class JdbcContainerProviderFactory {
626

27+
/**
28+
* Creates and initializes a JDBC container provider for the specified configuration.
29+
*
30+
* <p>This method:
31+
* <ol>
32+
* <li>Discovers all available {@link JdbcContainerProvider} implementations using
33+
* the {@link ServiceLoader} mechanism</li>
34+
* <li>Selects the provider that supports the database type specified in the
35+
* {@link EnableJdbcContainer} annotation</li>
36+
* <li>Initializes the provider with the specified Docker image and version</li>
37+
* <li>Creates the JDBC database container</li>
38+
* </ol>
39+
*
40+
* @param enableJdbcContainer the annotation containing the database configuration
41+
* @return a fully initialized JDBC container provider ready for use in tests
42+
* @throws IllegalStateException if no provider is found for the specified database type
43+
*/
744
public static JdbcContainerProvider getProvider(EnableJdbcContainer enableJdbcContainer) {
845
SpringAwareJdbcContainerProvider provider =
946
(SpringAwareJdbcContainerProvider)

spring-testcontainers/src/main/java/io/flowinquiry/testcontainers/jdbc/JdbcExtension.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,44 @@
1010
import org.slf4j.Logger;
1111
import org.slf4j.LoggerFactory;
1212

13+
/**
14+
* A JUnit 5 extension that manages JDBC database containers for integration testing.
15+
*
16+
* <p>This extension automatically starts and stops database containers based on annotations
17+
* present on the test class. It works with meta-annotations that are themselves annotated
18+
* with {@link EnableJdbcContainer}, such as {@code @EnablePostgreSQL}, {@code @EnableMySQL}, etc.
19+
*
20+
* <p>The extension handles the lifecycle of database containers:
21+
* <ul>
22+
* <li>Before all tests: Detects database configuration from annotations and starts the appropriate container</li>
23+
* <li>After all tests: Stops the container and cleans up resources</li>
24+
* </ul>
25+
*
26+
* <p>Usage example:
27+
* <pre>
28+
* {@code
29+
* @EnablePostgreSQL
30+
* class MyIntegrationTest {
31+
* // Test methods that require a PostgreSQL database
32+
* }
33+
* }
34+
* </pre>
35+
*/
1336
public class JdbcExtension implements BeforeAllCallback, AfterAllCallback {
1437

1538
private static final Logger log = LoggerFactory.getLogger(JdbcExtension.class);
1639

1740
private JdbcContainerProvider provider;
1841

42+
/**
43+
* Called before all tests in the current test class.
44+
*
45+
* <p>This method detects the database configuration from annotations on the test class,
46+
* creates the appropriate container provider, and starts the database container if needed.
47+
* If a container for this test class is already running, it reuses the existing container.
48+
*
49+
* @param context the extension context for the test class
50+
*/
1951
@Override
2052
public void beforeAll(ExtensionContext context) {
2153
Class<?> testClass = context.getRequiredTestClass();
@@ -33,6 +65,14 @@ public void beforeAll(ExtensionContext context) {
3365
}
3466
}
3567

68+
/**
69+
* Called after all tests in the current test class have completed.
70+
*
71+
* <p>This method stops the database container if it was started by this extension
72+
* and removes the container reference from the registry to allow proper cleanup.
73+
*
74+
* @param context the extension context for the test class
75+
*/
3676
@Override
3777
public void afterAll(ExtensionContext context) {
3878
if (provider != null) {
@@ -88,6 +128,18 @@ private Annotation findNearestAnnotationWith(
88128
return null;
89129
}
90130

131+
/**
132+
* Builds a resolved JDBC container configuration from a source annotation and its meta-annotation.
133+
*
134+
* <p>This method extracts configuration values (version and dockerImage) from the source annotation
135+
* and combines them with the database type (rdbms) from the meta-annotation to create a complete
136+
* {@link EnableJdbcContainer} configuration.
137+
*
138+
* @param sourceAnnotation the source annotation containing version and dockerImage values
139+
* @param meta the meta-annotation containing the database type (rdbms)
140+
* @return a resolved {@link EnableJdbcContainer} configuration
141+
* @throws IllegalStateException if reflection fails to extract values from the source annotation
142+
*/
91143
private EnableJdbcContainer buildResolvedJdbcConfig(
92144
Annotation sourceAnnotation, EnableJdbcContainer meta) {
93145
try {

0 commit comments

Comments
 (0)