Package dk.kosmisk.postgresql.it
Class PostgresITDataSource
- java.lang.Object
-
- org.apache.commons.dbcp2.PoolingDataSource<org.apache.commons.dbcp2.PoolableConnection>
-
- dk.kosmisk.postgresql.it.PostgresITDataSource
-
- All Implemented Interfaces:
AutoCloseable,Wrapper,CommonDataSource,DataSource
public class PostgresITDataSource extends org.apache.commons.dbcp2.PoolingDataSource<org.apache.commons.dbcp2.PoolableConnection>A pooling datasource for integration testing with PostgreSQL- Author:
- Source (source (at) kosmisk.dk)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classPostgresITDataSource.BuilderBuilder pattern for aPostgresITDataSourcestatic classPostgresITDataSource.DatabaseFromEnvironmentConstruct aPostgresITDataSource.DatabaseLocationfrom an environment variablestatic classPostgresITDataSource.DatabaseFromPropertyDatabase location from System Property, with user/password from ${user.name}static interfacePostgresITDataSource.DatabaseLocationInterface describing a way to locate a database
-
Constructor Summary
Constructors Constructor Description PostgresITDataSource(PostgresITDataSource.DatabaseLocation location)Construct a PostgreSQL datasource from the first available locationPostgresITDataSource(String databaseName)Construct a PostgreSQL datasource from the system properties or fall back to system databasePostgresITDataSource(String databaseName, String portPropertyName)Construct a PostgreSQL datasource from the system properties or fall back to system databasePostgresITDataSource(List<PostgresITDataSource.DatabaseLocation> locations, boolean useFallback)Construct a PostgreSQL datasource from the first available location
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description List<String>allTableNames()List all tables in schema publicstatic PostgresITDataSource.Builderbuilder()Construct a default buildervoidcopyAllTablesFromDisk()Convenience method forcopyTablesFromDisk(java.util.Collection)voidcopyAllTablesToDisk()Convenience method forcopyTablesToDisk(java.util.Collection)voidcopyTablesFromDisk(String... tables)Convenience method forcopyTablesFromDisk(java.util.Collection)voidcopyTablesFromDisk(Collection<String> tables)Ask the database to copy all the content backup files into the tablesvoidcopyTablesToDisk(String... tables)Convenience method forcopyTablesToDisk(java.util.Collection)voidcopyTablesToDisk(Collection<String> tables)Ask the database to copy all the content of listed tables to diskConnectiongetConnection()ConnectiongetConnection(String user, String password)voidtruncateAllTables()Convenience method fortruncateTables(java.util.Collection)voidtruncateTables(String... tables)Convenience method fortruncateTables(java.util.Collection)voidtruncateTables(Collection<String> tables)Truncate database tablesvoidwipe()Drop and create public schema.-
Methods inherited from class org.apache.commons.dbcp2.PoolingDataSource
close, getLoginTimeout, getLogWriter, getParentLogger, getPool, isAccessToUnderlyingConnectionAllowed, isWrapperFor, setAccessToUnderlyingConnectionAllowed, setLoginTimeout, setLogWriter, unwrap
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface javax.sql.CommonDataSource
createShardingKeyBuilder
-
Methods inherited from interface javax.sql.DataSource
createConnectionBuilder
-
-
-
-
Constructor Detail
-
PostgresITDataSource
public PostgresITDataSource(List<PostgresITDataSource.DatabaseLocation> locations, boolean useFallback)
Construct a PostgreSQL datasource from the first available location- Parameters:
locations- list of locationsPostgresITDataSource.DatabaseLocationto search throughuseFallback- if no database could be found, should we try the PG* environment variables and fall back to ${user.name}
-
PostgresITDataSource
public PostgresITDataSource(PostgresITDataSource.DatabaseLocation location)
Construct a PostgreSQL datasource from the first available locationconvenience constructor for
PostgresITDataSource(java.util.List, boolean)with useFallback set to true- Parameters:
location- seePostgresITDataSource(java.util.List, boolean)
-
PostgresITDataSource
public PostgresITDataSource(String databaseName, String portPropertyName)
Construct a PostgreSQL datasource from the system properties or fall back to system database- Parameters:
databaseName- name of databaseportPropertyName- name of property containing port number
-
PostgresITDataSource
public PostgresITDataSource(String databaseName)
Construct a PostgreSQL datasource from the system properties or fall back to system database- Parameters:
databaseName- name of database, and find port from system property "postgresql.${name}.port"
-
-
Method Detail
-
getConnection
public Connection getConnection() throws SQLException
- Specified by:
getConnectionin interfaceDataSource- Overrides:
getConnectionin classorg.apache.commons.dbcp2.PoolingDataSource<org.apache.commons.dbcp2.PoolableConnection>- Throws:
SQLException
-
getConnection
public Connection getConnection(String user, String password) throws SQLException
- Specified by:
getConnectionin interfaceDataSource- Overrides:
getConnectionin classorg.apache.commons.dbcp2.PoolingDataSource<org.apache.commons.dbcp2.PoolableConnection>- Throws:
SQLException
-
truncateTables
public void truncateTables(Collection<String> tables) throws SQLException
Truncate database tablesThis runs in a single transaction, so if one fails (table listed, but doesn't exist), all tables retain their content.
- Parameters:
tables- list of table names- Throws:
SQLException- if tables doesn't exist
-
truncateTables
public void truncateTables(String... tables) throws SQLException
Convenience method fortruncateTables(java.util.Collection)- Parameters:
tables- list of table names- Throws:
SQLException- if tables doesn't exist
-
truncateAllTables
public void truncateAllTables() throws SQLExceptionConvenience method fortruncateTables(java.util.Collection)Takes list of tables from
allTableNames()- Throws:
SQLException- if tables doesn't exist - This really shouldn't happen
-
wipe
public void wipe() throws SQLExceptionDrop and create public schema. This is the fastest way to empty a database using schemaname "public"- Throws:
SQLException- if there's problems dropping schema "public"
-
allTableNames
public List<String> allTableNames() throws SQLException
List all tables in schema publicThis list is ordered in a way so tables with foreign keys comes after the tables they refer. If mutual references exists a RuntimeExcepttion is thrown
- Returns:
- ordered list of table names
- Throws:
SQLException- if lists cannot be made
-
copyTablesToDisk
public void copyTablesToDisk(Collection<String> tables) throws SQLException
Ask the database to copy all the content of listed tables to disk- Parameters:
tables- list of table names- Throws:
SQLException- if the database cannot copy table content
-
copyTablesToDisk
public void copyTablesToDisk(String... tables) throws SQLException
Convenience method forcopyTablesToDisk(java.util.Collection)- Parameters:
tables- list of table names- Throws:
SQLException- if the database cannot copy table content
-
copyAllTablesToDisk
public void copyAllTablesToDisk() throws SQLExceptionConvenience method forcopyTablesToDisk(java.util.Collection)Takes list of tables from
allTableNames()- Throws:
SQLException- if the database cannot copy table content
-
copyTablesFromDisk
public void copyTablesFromDisk(Collection<String> tables) throws SQLException
Ask the database to copy all the content backup files into the tables- Parameters:
tables- list of table names- Throws:
SQLException- if the database cannot copy table content
-
copyTablesFromDisk
public void copyTablesFromDisk(String... tables) throws SQLException
Convenience method forcopyTablesFromDisk(java.util.Collection)- Parameters:
tables- list of table names- Throws:
SQLException- if the database cannot copy table content
-
copyAllTablesFromDisk
public void copyAllTablesFromDisk() throws SQLExceptionConvenience method forcopyTablesFromDisk(java.util.Collection)Takes list of tables from
allTableNames()- Throws:
SQLException- if the database cannot copy table content
-
builder
public static PostgresITDataSource.Builder builder()
Construct a default builder- Returns:
- new Builder
-
-