-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a78103
commit a571dbe
Showing
52 changed files
with
1,280 additions
and
523 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...t/java/com/vladmihalcea/hibernate/util/providers/AbstractContainerDataSourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.vladmihalcea.hibernate.util.providers; | ||
|
||
import org.testcontainers.containers.JdbcDatabaseContainer; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
/** | ||
* @author Vlad Mihalcea | ||
*/ | ||
public abstract class AbstractContainerDataSourceProvider implements DataSourceProvider { | ||
|
||
@Override | ||
public DataSource dataSource() { | ||
DataSource dataSource = newDataSource(); | ||
Connection connection = null; | ||
try { | ||
connection = dataSource.getConnection(); | ||
return dataSource; | ||
} catch (SQLException e) { | ||
Database database = database(); | ||
if(database.getContainer() == null) { | ||
database.initContainer(username(), password()); | ||
} | ||
return newDataSource(); | ||
} finally { | ||
if (connection != null) { | ||
try { | ||
connection.close(); | ||
} catch (SQLException ignore) { | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public String url() { | ||
JdbcDatabaseContainer container = database().getContainer(); | ||
return container != null ? | ||
container.getJdbcUrl() : | ||
defaultJdbcUrl(); | ||
} | ||
|
||
protected abstract String defaultJdbcUrl(); | ||
|
||
protected abstract DataSource newDataSource(); | ||
} |
85 changes: 80 additions & 5 deletions
85
hibernate-types-4/src/test/java/com/vladmihalcea/hibernate/util/providers/Database.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,89 @@ | ||
package com.vladmihalcea.hibernate.util.providers; | ||
|
||
import org.testcontainers.containers.*; | ||
|
||
import java.util.Collections; | ||
|
||
/** | ||
* @author Vlad Mihalcea | ||
*/ | ||
public enum Database { | ||
HSQLDB, | ||
H2, | ||
POSTGRESQL, | ||
ORACLE, | ||
MYSQL, | ||
SQLSERVER, | ||
COCKROACHDB | ||
POSTGRESQL { | ||
@Override | ||
protected JdbcDatabaseContainer newJdbcDatabaseContainer() { | ||
return new PostgreSQLContainer("postgres:13.7"); | ||
} | ||
}, | ||
ORACLE { | ||
@Override | ||
protected JdbcDatabaseContainer newJdbcDatabaseContainer() { | ||
return new OracleContainer("gvenzl/oracle-xe:21.3.0-slim"); | ||
} | ||
|
||
@Override | ||
protected boolean supportsDatabaseName() { | ||
return false; | ||
} | ||
}, | ||
MYSQL { | ||
@Override | ||
protected JdbcDatabaseContainer newJdbcDatabaseContainer() { | ||
return new MySQLContainer("mysql:8.0"); | ||
} | ||
}, | ||
SQLSERVER { | ||
@Override | ||
protected JdbcDatabaseContainer newJdbcDatabaseContainer() { | ||
return new MSSQLServerContainer("mcr.microsoft.com/mssql/server:2019-latest"); | ||
} | ||
|
||
@Override | ||
protected boolean supportsDatabaseName() { | ||
return false; | ||
} | ||
|
||
@Override | ||
protected boolean supportsCredentials() { | ||
return false; | ||
} | ||
}, | ||
COCKROACHDB; | ||
|
||
private JdbcDatabaseContainer container; | ||
|
||
public JdbcDatabaseContainer getContainer() { | ||
return container; | ||
} | ||
|
||
public void initContainer(String username, String password) { | ||
container = (JdbcDatabaseContainer) newJdbcDatabaseContainer() | ||
.withEnv(Collections.singletonMap("ACCEPT_EULA", "Y")); | ||
if(supportsDatabaseName()) { | ||
container.withDatabaseName("high-performance-java-persistence"); | ||
} | ||
if(supportsCredentials()) { | ||
container.withUsername(username).withPassword(password); | ||
} | ||
container.withTmpFs(Collections.singletonMap("/testtmpfs", "rw")); | ||
container.start(); | ||
} | ||
|
||
protected JdbcDatabaseContainer newJdbcDatabaseContainer() { | ||
throw new UnsupportedOperationException( | ||
String.format( | ||
"The [%s] database was not configured to use Testcontainers!", | ||
name() | ||
) | ||
); | ||
} | ||
|
||
protected boolean supportsDatabaseName() { | ||
return true; | ||
} | ||
|
||
protected boolean supportsCredentials() { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.