Class JdbcDatabaseContainer<SELF extends JdbcDatabaseContainer<SELF>>

java.lang.Object
org.testcontainers.containers.GenericContainer<SELF>
org.testcontainers.containers.JdbcDatabaseContainer<SELF>
All Implemented Interfaces:
AutoCloseable, org.testcontainers.containers.Container<SELF>, org.testcontainers.containers.ContainerState, org.testcontainers.containers.traits.LinkableContainer, org.testcontainers.containers.wait.strategy.WaitStrategyTarget, org.testcontainers.lifecycle.Startable

public abstract class JdbcDatabaseContainer<SELF extends JdbcDatabaseContainer<SELF>> extends org.testcontainers.containers.GenericContainer<SELF> implements org.testcontainers.containers.traits.LinkableContainer
Base class for containers that expose a JDBC connection
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
     

    Nested classes/interfaces inherited from interface org.testcontainers.containers.Container

    org.testcontainers.containers.Container.ExecResult
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected Map<String,String>
     
    protected Map<String,String>
     

    Fields inherited from class org.testcontainers.containers.GenericContainer

    CONTAINER_RUNNING_TIMEOUT_SEC, dependencies, dockerClient, INTERNAL_HOST_HOSTNAME, waitStrategy

    Fields inherited from interface org.testcontainers.containers.ContainerState

    STATE_HEALTHY
  • Constructor Summary

    Constructors
    Constructor
    Description
    JdbcDatabaseContainer(@NonNull String dockerImageName)
     
    JdbcDatabaseContainer(org.testcontainers.utility.DockerImageName dockerImageName)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addParameter(String paramName, String value)
     
    protected String
    Template method for constructing the JDBC URL to be used for creating Connections.
    protected String
    constructUrlParameters(String startCharacter, String delimiter)
     
    protected String
    constructUrlParameters(String startCharacter, String delimiter, String endCharacter)
     
    protected void
    containerIsStarted(com.github.dockerjava.api.command.InspectContainerResponse containerInfo)
     
    createConnection(String queryString)
    Creates a connection to the underlying containerized database instance.
    createConnection(String queryString, Properties info)
    Creates a connection to the underlying containerized database instance.
    protected int
    Deprecated.
    should not be overridden anymore, use withConnectTimeoutSeconds(int) in constructor instead
    protected org.testcontainers.delegate.DatabaseDelegate
     
     
    abstract String
     
    Obtain an instance of the correct JDBC driver for this particular database container type
    abstract String
     
    abstract String
     
    protected int
    Deprecated.
    should not be overridden anymore, use withStartupTimeoutSeconds(int) in constructor instead
    protected abstract String
     
    abstract String
     
    protected void
    optionallyMapResourceParameterAsVolume(@NotNull String paramName, @NotNull String pathNameInContainer, @NotNull String defaultResource)
    Deprecated.
    protected void
    optionallyMapResourceParameterAsVolume(@NotNull String paramName, @NotNull String pathNameInContainer, @NotNull String defaultResource, @Nullable Integer fileMode)
     
    protected void
    Load init script content and apply it to the database if initScriptPath is set
    void
     
    protected void
     
    withConnectTimeoutSeconds(int connectTimeoutSeconds)
    Set time to allow for the database to start and establish an initial connection, in seconds.
     
    withInitScript(String initScriptPath)
    Sets a script for initialization.
    withInitScripts(Iterable<String> initScriptPaths)
    Sets an ordered collection of scripts for initialization.
    withInitScripts(String... initScriptPaths)
    Sets an ordered array of scripts for initialization.
    withPassword(String password)
     
    withStartupTimeoutSeconds(int startupTimeoutSeconds)
    Set startup time to allow, including image pull time, in seconds.
    withUrlParam(String paramName, String paramValue)
     
    withUsername(String username)
     

    Methods inherited from class org.testcontainers.containers.GenericContainer

    addEnv, addExposedPort, addExposedPorts, addFileSystemBind, addFixedExposedPort, addFixedExposedPort, addLink, canBeReused, configure, containerIsCreated, containerIsStarted, containerIsStarting, containerIsStarting, containerIsStopped, containerIsStopping, copyFileFromContainer, createVolumeDirectory, dependsOn, dependsOn, dependsOn, doStart, equals, getBinds, getCommandParts, getContainerId, getContainerInfo, getContainerName, getCopyToFileContainerPathMap, getCreateContainerCmdModifiers, getDependencies, getDockerClient, getDockerImageName, getEnv, getEnvMap, getExposedPorts, getExtraHosts, getImage, getIpAddress, getLabels, getLinkedContainers, getLivenessCheckPort, getLivenessCheckPortNumbers, getLivenessCheckPorts, getLogConsumers, getNetwork, getNetworkAliases, getNetworkMode, getPortBindings, getShmSize, getStartupAttempts, getStartupCheckStrategy, getTestHostIpAddress, getTmpFsMapping, getVolumesFroms, getWaitStrategy, getWorkingDirectory, hashCode, isHostAccessible, isPrivilegedMode, isShouldBeReused, logger, setBinds, setCommand, setCommand, setCommandParts, setContainerDef, setCopyToFileContainerPathMap, setDockerImageName, setEnv, setExposedPorts, setExtraHosts, setHostAccessible, setImage, setLabels, setLinkedContainers, setLogConsumers, setNetwork, setNetworkAliases, setNetworkMode, setPortBindings, setPrivilegedMode, setShmSize, setStartupAttempts, setStartupCheckStrategy, setTmpFsMapping, setVolumesFroms, setWaitStrategy, setWorkingDirectory, start, stop, toString, waitingFor, withAccessToHost, withClasspathResourceMapping, withClasspathResourceMapping, withCommand, withCommand, withCopyFileToContainer, withCopyToContainer, withCreateContainerCmdModifier, withEnv, withEnv, withExposedPorts, withExtraHost, withFileSystemBind, withImagePullPolicy, withLabel, withLabels, withLogConsumer, withMinimumRunningDuration, withNetwork, withNetworkAliases, withNetworkMode, withPrivilegedMode, withReuse, withSharedMemorySize, withStartupAttempts, withStartupCheckStrategy, withStartupTimeout, withTmpFs, withVolumesFrom, withWorkingDirectory

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.testcontainers.containers.Container

    addFileSystemBind, followOutput, followOutput, self, withEnv, withFileSystemBind

    Methods inherited from interface org.testcontainers.containers.ContainerState

    copyFileFromContainer, copyFileToContainer, copyFileToContainer, execInContainer, execInContainer, execInContainer, execInContainer, execInContainerWithUser, execInContainerWithUser, getBoundPortNumbers, getContainerIpAddress, getCurrentContainerInfo, getFirstMappedPort, getHost, getLogs, getLogs, getMappedPort, isCreated, isHealthy, isRunning

    Methods inherited from interface org.testcontainers.containers.traits.LinkableContainer

    getContainerName

    Methods inherited from interface org.testcontainers.lifecycle.Startable

    close
  • Field Details

  • Constructor Details

    • JdbcDatabaseContainer

      public JdbcDatabaseContainer(@NonNull @NonNull String dockerImageName)
    • JdbcDatabaseContainer

      public JdbcDatabaseContainer(@NonNull @NonNull Future<String> image)
    • JdbcDatabaseContainer

      public JdbcDatabaseContainer(org.testcontainers.utility.DockerImageName dockerImageName)
  • Method Details

    • getDriverClassName

      public abstract String getDriverClassName()
      Returns:
      the name of the actual JDBC driver to use
    • getJdbcUrl

      public abstract String getJdbcUrl()
      Returns:
      a JDBC URL that may be used to connect to the dockerized DB
    • getDatabaseName

      public String getDatabaseName()
      Returns:
      the database name
    • getUsername

      public abstract String getUsername()
      Returns:
      the standard database username that should be used for connections
    • getPassword

      public abstract String getPassword()
      Returns:
      the standard password that should be used for connections
    • getTestQueryString

      protected abstract String getTestQueryString()
      Returns:
      a test query string suitable for testing that this particular database type is alive
    • withUsername

      public SELF withUsername(String username)
    • withPassword

      public SELF withPassword(String password)
    • withDatabaseName

      public SELF withDatabaseName(String dbName)
    • withUrlParam

      public SELF withUrlParam(String paramName, String paramValue)
    • withStartupTimeoutSeconds

      public SELF withStartupTimeoutSeconds(int startupTimeoutSeconds)
      Set startup time to allow, including image pull time, in seconds.
      Parameters:
      startupTimeoutSeconds - startup time to allow, including image pull time, in seconds
      Returns:
      self
    • withConnectTimeoutSeconds

      public SELF withConnectTimeoutSeconds(int connectTimeoutSeconds)
      Set time to allow for the database to start and establish an initial connection, in seconds.
      Parameters:
      connectTimeoutSeconds - time to allow for the database to start and establish an initial connection in seconds
      Returns:
      self
    • withInitScript

      public SELF withInitScript(String initScriptPath)
      Sets a script for initialization.
      Parameters:
      initScriptPath - path to the script file
      Returns:
      self
    • withInitScripts

      public SELF withInitScripts(String... initScriptPaths)
      Sets an ordered array of scripts for initialization.
      Parameters:
      initScriptPaths - paths to the script files
      Returns:
      self
    • withInitScripts

      public SELF withInitScripts(Iterable<String> initScriptPaths)
      Sets an ordered collection of scripts for initialization.
      Parameters:
      initScriptPaths - paths to the script files
      Returns:
      self
    • waitUntilContainerStarted

      protected void waitUntilContainerStarted()
      Overrides:
      waitUntilContainerStarted in class org.testcontainers.containers.GenericContainer<SELF extends JdbcDatabaseContainer<SELF>>
    • containerIsStarted

      protected void containerIsStarted(com.github.dockerjava.api.command.InspectContainerResponse containerInfo)
      Overrides:
      containerIsStarted in class org.testcontainers.containers.GenericContainer<SELF extends JdbcDatabaseContainer<SELF>>
    • getJdbcDriverInstance

      public Driver getJdbcDriverInstance() throws JdbcDatabaseContainer.NoDriverFoundException
      Obtain an instance of the correct JDBC driver for this particular database container type
      Returns:
      a JDBC Driver
      Throws:
      JdbcDatabaseContainer.NoDriverFoundException
    • createConnection

      public Connection createConnection(String queryString) throws SQLException, JdbcDatabaseContainer.NoDriverFoundException
      Creates a connection to the underlying containerized database instance.
      Parameters:
      queryString - query string parameters that should be appended to the JDBC connection URL. The '?' character must be included
      Returns:
      a Connection
      Throws:
      SQLException - if there is a repeated failure to create the connection
      JdbcDatabaseContainer.NoDriverFoundException
    • createConnection

      public Connection createConnection(String queryString, Properties info) throws SQLException, JdbcDatabaseContainer.NoDriverFoundException
      Creates a connection to the underlying containerized database instance.
      Parameters:
      queryString - query string parameters that should be appended to the JDBC connection URL. The '?' character must be included
      info - additional properties to be passed to the JDBC driver
      Returns:
      a Connection
      Throws:
      SQLException - if there is a repeated failure to create the connection
      JdbcDatabaseContainer.NoDriverFoundException
    • constructUrlForConnection

      protected String constructUrlForConnection(String queryString)
      Template method for constructing the JDBC URL to be used for creating Connections. This should be overridden if the JDBC URL and query string concatenation or URL string construction needs to be different to normal.
      Parameters:
      queryString - query string parameters that should be appended to the JDBC connection URL. The '?' character must be included
      Returns:
      a full JDBC URL including queryString
    • constructUrlParameters

      protected String constructUrlParameters(String startCharacter, String delimiter)
    • constructUrlParameters

      protected String constructUrlParameters(String startCharacter, String delimiter, String endCharacter)
    • optionallyMapResourceParameterAsVolume

      @Deprecated protected void optionallyMapResourceParameterAsVolume(@NotNull @NotNull String paramName, @NotNull @NotNull String pathNameInContainer, @NotNull @NotNull String defaultResource)
      Deprecated.
    • optionallyMapResourceParameterAsVolume

      protected void optionallyMapResourceParameterAsVolume(@NotNull @NotNull String paramName, @NotNull @NotNull String pathNameInContainer, @NotNull @NotNull String defaultResource, @Nullable @Nullable Integer fileMode)
    • runInitScriptIfRequired

      protected void runInitScriptIfRequired()
      Load init script content and apply it to the database if initScriptPath is set
    • setParameters

      public void setParameters(Map<String,String> parameters)
    • addParameter

      public void addParameter(String paramName, String value)
    • getStartupTimeoutSeconds

      @Deprecated protected int getStartupTimeoutSeconds()
      Deprecated.
      should not be overridden anymore, use withStartupTimeoutSeconds(int) in constructor instead
      Returns:
      startup time to allow, including image pull time, in seconds
    • getConnectTimeoutSeconds

      @Deprecated protected int getConnectTimeoutSeconds()
      Deprecated.
      should not be overridden anymore, use withConnectTimeoutSeconds(int) in constructor instead
      Returns:
      time to allow for the database to start and establish an initial connection, in seconds
    • getDatabaseDelegate

      protected org.testcontainers.delegate.DatabaseDelegate getDatabaseDelegate()