org.testcontainers.containers.MySQLContainer Java Examples

The following examples show how to use org.testcontainers.containers.MySQLContainer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SqlTestUtil.java    From kork with Apache License 2.0 6 votes vote down vote up
@Deprecated
public static TestDatabase initPreviousTcMysqlDatabase() {
  MySQLContainer container =
      new MySQLContainer("mysql:5.7.22")
          .withDatabaseName("previous")
          .withUsername("test")
          .withPassword("test");

  container.start();

  String jdbcUrl =
      String.format(
          "%s?user=%s&password=%s",
          container.getJdbcUrl(), container.getUsername(), container.getPassword());

  return initDatabase(jdbcUrl, SQLDialect.MYSQL, "previous");
}
 
Example #2
Source File: SimpleMySQLTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void testWithAdditionalUrlParamInJdbcUrl() {
    MySQLContainer mysql = (MySQLContainer) new MySQLContainer()
        .withUrlParam("allowMultiQueries", "true")
        .withUrlParam("rewriteBatchedStatements", "true")
        .withLogConsumer(new Slf4jLogConsumer(logger));

    try {
        mysql.start();
        String jdbcUrl = mysql.getJdbcUrl();
        assertThat(jdbcUrl, containsString("?"));
        assertThat(jdbcUrl, containsString("&"));
        assertThat(jdbcUrl, containsString("rewriteBatchedStatements=true"));
        assertThat(jdbcUrl, containsString("allowMultiQueries=true"));
    } finally {
        mysql.stop();
    }
}
 
Example #3
Source File: SimpleMySQLTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void testWithAdditionalUrlParamMultiQueries() throws SQLException {
    MySQLContainer mysql = (MySQLContainer) new MySQLContainer()
        .withUrlParam("allowMultiQueries", "true")
        .withLogConsumer(new Slf4jLogConsumer(logger));
    mysql.start();

    try(Connection connection = mysql.createConnection("")) {
        Statement statement = connection.createStatement();
        String multiQuery = "DROP TABLE IF EXISTS bar; " +
            "CREATE TABLE bar (foo VARCHAR(20)); " +
            "INSERT INTO bar (foo) VALUES ('hello world');";
        statement.execute(multiQuery);
        statement.execute("SELECT foo FROM bar;");
        try(ResultSet resultSet = statement.getResultSet()) {
            resultSet.next();
            String firstColumnValue = resultSet.getString(1);
            assertEquals("Value from bar should equal real value", "hello world", firstColumnValue);
        }
    } finally {
        mysql.stop();
    }
}
 
Example #4
Source File: SimpleMySQLTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void testWithAdditionalUrlParamTimeZone() throws SQLException {
    MySQLContainer mysql = (MySQLContainer) new MySQLContainer()
        .withUrlParam("serverTimezone", "Europe/Zurich")
        .withEnv("TZ", "Europe/Zurich")
        .withLogConsumer(new Slf4jLogConsumer(logger));
    mysql.start();

    try(Connection connection = mysql.createConnection("")) {
        Statement statement = connection.createStatement();
        statement.execute("SELECT NOW();");
        try (ResultSet resultSet = statement.getResultSet()) {
            resultSet.next();

            // checking that the time_zone MySQL is Europe/Zurich
            LocalDateTime localDateTime = resultSet.getObject(1, LocalDateTime.class);
            ZonedDateTime actualDateTime = localDateTime.atZone(ZoneId.of("Europe/Zurich"))
                .truncatedTo(ChronoUnit.MINUTES);
            ZonedDateTime expectedDateTime = ZonedDateTime.now(ZoneId.of("Europe/Zurich"))
                .truncatedTo(ChronoUnit.MINUTES);

            String message = String.format("MySQL time zone is not Europe/Zurich. MySQL date:%s, current date:%s",
                actualDateTime, expectedDateTime);
            assertTrue(message, actualDateTime.equals(expectedDateTime));
        }
    } finally {
        mysql.stop();
    }
}
 
Example #5
Source File: SimpleMySQLTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void testEmptyPasswordWithRootUser() throws SQLException {
    // Add MYSQL_ROOT_HOST environment so that we can root login from anywhere for testing purposes
    try (MySQLContainer<?> mysql = new MySQLContainer<>("mysql:5.5")
        .withDatabaseName("foo")
        .withUsername("root")
        .withPassword("")
        .withEnv("MYSQL_ROOT_HOST", "%")) {

        mysql.start();

        ResultSet resultSet = performQuery(mysql, "SELECT 1");
        int resultSetInt = resultSet.getInt(1);

        assertEquals("A basic SELECT query succeeds", 1, resultSetInt);
    }
}
 
Example #6
Source File: CustomizableMysqlTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void testSimple() throws SQLException {
    // Add MYSQL_ROOT_HOST environment so that we can root login from anywhere for testing purposes
    try (MySQLContainer<?> mysql = new MySQLContainer<>("mysql:5.5")
        .withDatabaseName(DB_NAME)
        .withUsername(USER)
        .withPassword(PWD)
        .withEnv("MYSQL_ROOT_HOST", "%")) {

        mysql.start();

        ResultSet resultSet = performQuery(mysql, "SELECT 1");

        int resultSetInt = resultSet.getInt(1);
        assertEquals("A basic SELECT query succeeds", 1, resultSetInt);
    }
}
 
Example #7
Source File: TestRaptorIntegrationSmokeTestMySql.java    From presto with Apache License 2.0 5 votes vote down vote up
private static String getJdbcUrl(MySQLContainer<?> container)
{
    return format("%s?user=%s&password=%s&useSSL=false&allowPublicKeyRetrieval=true",
            container.getJdbcUrl(),
            container.getUsername(),
            container.getPassword());
}
 
Example #8
Source File: SqlTestUtil.java    From kork with Apache License 2.0 5 votes vote down vote up
public static TestDatabase initDualTcMysqlDatabases() {
  MySQLContainer container =
      new MySQLContainer("mysql:5.7.22")
          .withDatabaseName("current")
          .withUsername("test")
          .withPassword("test");
  container.start();

  String rootJdbcUrl =
      String.format(
          "%s?user=%s&password=%s", container.getJdbcUrl(), "root", container.getPassword());

  try {
    Connection rootCon = DriverManager.getConnection(rootJdbcUrl);
    rootCon.createStatement().executeUpdate("create database previous");
    rootCon.createStatement().executeUpdate("grant all privileges on previous.* to 'test'@'%'");
    rootCon.close();
  } catch (SQLException e) {
    throw new RuntimeException("Error setting up testcontainer database", e);
  }

  String currentJdbcUrl =
      String.format(
          "%s?user=%s&password=%s",
          container.getJdbcUrl(), container.getUsername(), container.getPassword());

  String previousJdbcUrl = currentJdbcUrl.replace("/current", "/previous");

  TestDatabase currentTDB = initDatabase(currentJdbcUrl, SQLDialect.MYSQL, "current");
  TestDatabase previousTDB = initDatabase(previousJdbcUrl, SQLDialect.MYSQL, "previous");

  return new TestDatabase(
      currentTDB.dataSource,
      currentTDB.context,
      currentTDB.liquibase,
      previousTDB.dataSource,
      previousTDB.context,
      previousTDB.liquibase);
}
 
Example #9
Source File: MultiVersionMySQLTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void versionCheckTest() throws SQLException {
    try (MySQLContainer<?> mysql = new MySQLContainer<>("mysql:" + version)) {
        mysql.start();
        final ResultSet resultSet = performQuery(mysql, "SELECT VERSION()");
        final String resultSetString = resultSet.getString(1);

        assertEquals("The database version can be set using a container rule parameter", version, resultSetString);
    }
}
 
Example #10
Source File: SimpleMySQLTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test(expected = ContainerLaunchException.class)
public void testEmptyPasswordWithNonRootUser() {
    try (MySQLContainer<?> container = new MySQLContainer<>("mysql:5.5")
                .withDatabaseName("TEST")
                .withUsername("test")
                .withPassword("")
                .withEnv("MYSQL_ROOT_HOST", "%")){
        container.start();
        fail("ContainerLaunchException expected to be thrown");
    }
}
 
Example #11
Source File: SimpleMySQLTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void testExplicitInitScript() throws SQLException {
    try (MySQLContainer<?> container = new MySQLContainer<>()
        .withInitScript("somepath/init_mysql.sql")
        .withLogConsumer(new Slf4jLogConsumer(logger))) {
        container.start();

        ResultSet resultSet = performQuery(container, "SELECT foo FROM bar");
        String firstColumnValue = resultSet.getString(1);

        assertEquals("Value from init script should equal real value", "hello world", firstColumnValue);
    }
}
 
Example #12
Source File: SimpleMySQLTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void testCommandOverride() throws SQLException {
    try (MySQLContainer<?> mysqlCustomConfig = new MySQLContainer<>()
        .withCommand("mysqld --auto_increment_increment=42")) {

        mysqlCustomConfig.start();

        ResultSet resultSet = performQuery(mysqlCustomConfig, "show variables like 'auto_increment_increment'");
        String result = resultSet.getString("Value");

        assertEquals("Auto increment increment should be overriden by command line", "42", result);
    }
}
 
Example #13
Source File: SimpleMySQLTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void testMySQLWithCustomIniFile() throws SQLException {
    assumeFalse(SystemUtils.IS_OS_WINDOWS);

    try (MySQLContainer<?> mysqlCustomConfig = new MySQLContainer<>("mysql:5.6")
        .withConfigurationOverride("somepath/mysql_conf_override")) {

        mysqlCustomConfig.start();

        ResultSet resultSet = performQuery(mysqlCustomConfig, "SELECT @@GLOBAL.innodb_file_format");
        String result = resultSet.getString(1);

        assertEquals("The InnoDB file format has been set by the ini file content", "Barracuda", result);
    }
}
 
Example #14
Source File: SimpleMySQLTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void testSpecificVersion() throws SQLException {
    try (MySQLContainer<?> mysqlOldVersion = new MySQLContainer<>("mysql:5.5")
        .withConfigurationOverride("somepath/mysql_conf_override")
        .withLogConsumer(new Slf4jLogConsumer(logger))) {

        mysqlOldVersion.start();

        ResultSet resultSet = performQuery(mysqlOldVersion, "SELECT VERSION()");
        String resultSetString = resultSet.getString(1);

        assertTrue("The database version can be set using a container rule parameter", resultSetString.startsWith("5.5"));
    }
}
 
Example #15
Source File: SimpleMySQLTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void testSimple() throws SQLException {
    try (MySQLContainer<?> mysql = new MySQLContainer<>()
        .withConfigurationOverride("somepath/mysql_conf_override")
        .withLogConsumer(new Slf4jLogConsumer(logger))) {

        mysql.start();

        ResultSet resultSet = performQuery(mysql, "SELECT 1");
        int resultSetInt = resultSet.getInt(1);

        assertEquals("A basic SELECT query succeeds", 1, resultSetInt);
    }
}
 
Example #16
Source File: ApolloMySQL.java    From apollo with Apache License 2.0 5 votes vote down vote up
public ApolloMySQL() throws SQLException, IOException, ScriptException {

        // Create mysql instance
        logger.info("Starting MySQL container");
        mysql = new MySQLContainer("mysql:5.7.22");
        mysql.start();
    }
 
Example #17
Source File: MySqlDataSourceFactory.java    From nifi-registry with Apache License 2.0 5 votes vote down vote up
@Override
protected DataSource createDataSource() {
    final MySQLContainer container = mysqlContainer();
    final MysqlDataSource dataSource = new MysqlDataSource();
    dataSource.setUrl(container.getJdbcUrl());
    dataSource.setUser(container.getUsername());
    dataSource.setPassword(container.getPassword());
    dataSource.setDatabaseName(container.getDatabaseName());
    return dataSource;
}
 
Example #18
Source File: DebeziumMysqlTestResource.java    From camel-quarkus with Apache License 2.0 5 votes vote down vote up
@Override
protected MySQLContainer createContainer() {
    return new MySQLContainer<>(MYSQL_IMAGE)
            .withUsername(DB_USERNAME)
            .withPassword(DB_PASSWORD)
            .withDatabaseName(DB_NAME)
            .withInitScript("initMysql.sql");
}
 
Example #19
Source File: TestRaptorIntegrationSmokeTestMySql.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
protected QueryRunner createQueryRunner()
        throws Exception
{
    mysqlContainer = new MySQLContainer<>("mysql:8.0.12");
    mysqlContainer.start();
    return createRaptorMySqlQueryRunner(getJdbcUrl(mysqlContainer));
}
 
Example #20
Source File: MySql8DataSourceFactory.java    From nifi-registry with Apache License 2.0 4 votes vote down vote up
@Override
protected MySQLContainer mysqlContainer() {
    return MYSQL_CONTAINER;
}
 
Example #21
Source File: MySql6DataSourceFactory.java    From nifi-registry with Apache License 2.0 4 votes vote down vote up
@Override
protected MySQLContainer mysqlContainer() {
    return MYSQL_CONTAINER;
}
 
Example #22
Source File: MySql7DataSourceFactory.java    From nifi-registry with Apache License 2.0 4 votes vote down vote up
@Override
protected MySQLContainer mysqlContainer() {
    return MYSQL_CONTAINER;
}
 
Example #23
Source File: DatabaseContainers.java    From spring-session with Apache License 2.0 4 votes vote down vote up
static MySQLContainer mySql5() {
	return new MySql5Container();
}
 
Example #24
Source File: DatabaseContainers.java    From spring-session with Apache License 2.0 4 votes vote down vote up
static MySQLContainer mySql8() {
	return new MySql8Container();
}
 
Example #25
Source File: MySqlDataSourceFactory.java    From nifi-registry with Apache License 2.0 votes vote down vote up
protected abstract MySQLContainer mysqlContainer();