How to use withInitScript method of org.testcontainers.containers.JdbcDatabaseContainer class

Best Testcontainers-java code snippet using org.testcontainers.containers.JdbcDatabaseContainer.withInitScript

Source:DDLCreateIT.java Github

copy

Full Screen

...33import org.testcontainers.containers.PostgreSQLContainer;34public class DDLCreateIT {35 @Test36 public void mysql() {37 testSchemaCreate(() -> new MariaDBContainer("mariadb:10.2").withConfigurationOverride(null).withInitScript("Ambari-DDL-MySQL-CREATE.sql"));38 testSchemaCreate(() -> new MySQLContainer("mysql:5.7").withConfigurationOverride(null).withInitScript("Ambari-DDL-MySQL-CREATE.sql"));39 }40 @Test41 public void postgres() {42 testSchemaCreate(() -> new PostgreSQLContainer("postgres:9.6").withInitScript("Ambari-DDL-Postgres-CREATE.sql"));43 testSchemaCreate(() -> new PostgreSQLContainer("postgres:10").withInitScript("Ambari-DDL-Postgres-CREATE.sql"));44 }45 private static void testSchemaCreate(Supplier<? extends JdbcDatabaseContainer> containerSupplier) {46 try (JdbcDatabaseContainer container = containerSupplier.get().withPassword(Configuration.SERVER_JDBC_USER_PASSWD.getDefaultValue())) {47 container.start();48 Properties props = new Properties();49 props.put(Configuration.SERVER_PERSISTENCE_TYPE.getKey(), PersistenceType.REMOTE.getValue());50 props.put(Configuration.SERVER_DB_NAME.getKey(), container.getDatabaseName());51 props.put(Configuration.SERVER_JDBC_DRIVER.getKey(), container.getDriverClassName());52 props.put(Configuration.SERVER_JDBC_URL.getKey(), container.getJdbcUrl().replace("mariadb", "mysql"));53 props.put(Configuration.SERVER_JDBC_USER_NAME.getKey(), container.getUsername());54 Configuration config = new Configuration(props);55 DBAccessor db = new DBAccessorImpl(config);56 assertTrue(db.tableExists("metainfo"));57 assertEquals(new Integer(1),...

Full Screen

Full Screen
copy

Full Screen

...13 public static JdbcDatabaseContainer<?> dbContainer = new PostgreSQLContainer<>("postgres:14")14 .withDatabaseName("tests-db")15 .withUsername("sa")16 .withPassword("sa")17 .withInitScript("postgre-init.sql")18 .withEnv("TZ", "UTC")19 .withEnv("PGTZ", "UTC")20 .withReuse(true);21 static {22 dbContainer.start();23 }24 @DynamicPropertySource25 static void datasourceProperties(DynamicPropertyRegistry registry) {26 registry.add("spring.datasource.url", dbContainer::getJdbcUrl);27 registry.add("spring.datasource.username", dbContainer::getUsername);28 registry.add("spring.datasource.password", dbContainer::getPassword);29 }30}...

Full Screen

Full Screen

withInitScript

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.containers.JdbcDatabaseContainer;3import org.testcontainers.containers.MySQLContainer;4import org.testcontainers.containers.PostgreSQLContainer;5import org.testcontainers.containers.output.Slf4jLogConsumer;6import org.testcontainers.containers.output.ToStringConsumer;7import org.testcontainers.containers.output.WaitingConsumer;8import org.testcontainers.images.builder.ImageFromDockerfile;9import org.testcontainers.utility.MountableFile;10import java.io.File;11import java.io.IOException;12import java.nio.charset.StandardCharsets;13import java.nio.file.Files;14import java.nio.file.Path;15import java.nio.file.Paths;16import java.sql.Connection;17import java.sql.DriverManager;18import java.sql.SQLException;19import java.sql.Statement;20import java.util.ArrayList;21import java.util.List;22import java.util.concurrent.TimeUnit;23import java.util.stream.Collectors;24import java.util.stream.Stream;25public class Main {26 public static void main(String[] args) throws IOException, InterruptedException {27 MySQLContainer mySQLContainer = new MySQLContainer();28 mySQLContainer.start();29 PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer();30 postgreSQLContainer.start();31 String initScript = "CREATE TABLE IF NOT EXISTS `TEST` ( `ID` INT(11) NOT NULL, `NAME` VARCHAR(20) NOT NULL, PRIMARY KEY (`ID`)) ENGINE = InnoDB;";32 JdbcDatabaseContainer databaseContainer = null;33 String dbType = "mysql";34 if(dbType.equals("mysql")){35 databaseContainer = mySQLContainer;36 } else if(dbType.equals("postgres")){37 databaseContainer = postgreSQLContainer;38 }39 if(databaseContainer != null){40 databaseContainer.withInitScript(initScript);41 }42 }43}

Full Screen

Full Screen

withInitScript

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.JdbcDatabaseContainer;2import org.testcontainers.containers.MySQLContainer;3import org.testcontainers.containers.PostgreSQLContainer;4import org.testcontainers.containers.GenericContainer;5import org.testcontainers.containers.output.Slf4jLogConsumer;6import org.testcontainers.containers.output.OutputFrame;7import org.testcontainers.utility.MountableFile;8import org.slf4j.Logger;9import org.slf4j.LoggerFactory;10import java.io.File;11import java.io.IOException;12import java.nio.charset.StandardCharsets;13import java.nio.file.Files;14import java.nio.file.Path;15import java.nio.file.Paths;16import java.nio.file.StandardCopyOption;17import java.nio.file.StandardOpenOption;18import java.util.stream.Stream;19import java.util.Collections;20import java.util.List;21import java.util.ArrayList;22import java.util.Arrays;23import java.util.Optional;24import java.util.concurrent.TimeUnit;25import java.util.concurrent.TimeoutException;26import java.util.concurrent.atomic.AtomicBoolean;27import java.util.concurrent.atomic.AtomicReference;28import java.util.function.Consumer;29import java.util.function.Function;30import java.util.function.Predicate;31import java.util.function.Supplier;32import java.lang.reflect.Field;33import java.lang.reflect.Method;34import java.lang.reflect.InvocationTargetException;35import java.util.concurrent.Executors;36import java.util.concurrent.ExecutorService;37import java.util.concurrent.Executor;38import java.util.concurrent.Callable;39import java.util.concurrent.CompletableFuture;40import java.util.concurrent.CompletionException;41import org.testcontainers.containers.wait.strategy.WaitStrategy;42import org.testcontainers.containers.wait.strategy.Wait;43import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy;44import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;45import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;

Full Screen

Full Screen

withInitScript

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers;2import java.io.IOException;3import java.nio.charset.StandardCharsets;4import java.nio.file.Files;5import java.nio.file.Paths;6import java.sql.Connection;7import java.sql.DriverManager;8import java.sql.SQLException;9import java.sql.Statement;10import java.util.stream.Collectors;11import org.junit.Assert;12import org.junit.Test;13import org.testcontainers.jdbc.ContainerDatabaseDriver;14public class JdbcDatabaseContainerTest {15 public void testWithInitScript() throws SQLException, IOException {16 try (MySQLContainer mySQLContainer = new MySQLContainer()) {17 mySQLContainer.start();18 ContainerDatabaseDriver driver = ContainerDatabaseDriver.newInstance(mySQLContainer);19 try (Connection connection = DriverManager.getConnection(driver.getJdbcUrl(), driver.getUsername(), driver.getPassword())) {20 Statement statement = connection.createStatement();21 String createTableScript = Files.lines(Paths.get("src/​test/​resources/​create_table.sql"), StandardCharsets.UTF_8).collect(Collectors.joining("22"));23 statement.execute(createTableScript);24 String insertDataScript = Files.lines(Paths.get("src/​test/​resources/​insert_data.sql"), StandardCharsets.UTF_8).collect(Collectors.joining("25"));26 statement.execute(insertDataScript);27 mySQLContainer.withInitScript("src/​test/​resources/​insert_data.sql");28 Assert.assertEquals(3, mySQLContainer.getInitScriptContents().size());29 }30 }31 }32}33package org.testcontainers.containers;34import java.io.IOException;35import java.nio.charset.StandardCharsets;36import java.nio.file.Files;37import java.nio.file.Paths;38import java.sql.Connection;39import java.sql.DriverManager;40import java.sql.SQLException;41import java.sql.Statement;42import java.util.stream.Collectors;43import org.junit.Assert;44import org.junit.Test;45import org.testcontainers.jdbc.ContainerDatabaseDriver;46public class JdbcDatabaseContainerTest {47 public void testWithInitScript() throws SQLException, IOException {48 try (MySQLContainer mySQLContainer = new MySQLContainer()) {49 mySQLContainer.start();50 ContainerDatabaseDriver driver = ContainerDatabaseDriver.newInstance(mySQLContainer);51 try (Connection connection = DriverManager.getConnection(driver.getJdbcUrl(), driver.getUsername(), driver.getPassword())) {52 Statement statement = connection.createStatement();53 String createTableScript = Files.lines(Paths.get("src/​test/​resources/​create_table.sql"), StandardCharsets.UTF_8).collect(Collectors

Full Screen

Full Screen

withInitScript

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers;2import java.sql.Connection;3import java.sql.DriverManager;4import java.sql.ResultSet;5import java.sql.SQLException;6import java.sql.Statement;7import java.util.ArrayList;8import java.util.List;9import org.testcontainers.containers.JdbcDatabaseContainer;10import org.testcontainers.containers.PostgreSQLContainer;11public class TestcontainersDemo {12 public static void main(String[] args) throws SQLException {13 JdbcDatabaseContainer container = new PostgreSQLContainer();14 container.withInitScript("init.sql");15 container.start();16 Connection connection = DriverManager.getConnection(container.getJdbcUrl(), container.getUsername(), container.getPassword());17 Statement statement = connection.createStatement();18 ResultSet resultSet = statement.executeQuery("SELECT * FROM test");19 List<String> result = new ArrayList<>();20 while (resultSet.next()) {21 result.add(resultSet.getString("name"));22 }23 System.out.println(result);24 }25}26CREATE TABLE test (name varchar(100));27INSERT INTO test (name) VALUES ('test1');28INSERT INTO test (name) VALUES ('test2');

Full Screen

Full Screen

withInitScript

Using AI Code Generation

copy

Full Screen

1import java.io.File;2import java.io.IOException;3import java.nio.file.Files;4import java.nio.file.Paths;5import java.sql.Connection;6import java.sql.DriverManager;7import java.sql.SQLException;8import java.sql.Statement;9import java.util.List;10import org.apache.commons.io.FileUtils;11import org.testcontainers.containers.JdbcDatabaseContainer;12import org.testcontainers.containers.PostgreSQLContainer;13import org.testcontainers.containers.output.Slf4jLogConsumer;14import org.testcontainers.containers.output.ToStringConsumer;15import org.testcontainers.containers.output.WaitingConsumer;16import org.testcontainers.containers.output.OutputFrame;17import org.testcontainers.utility.MountableFile;18public class TestcontainersInitScript {19 public static void main(String[] args) throws SQLException, IOException {20 JdbcDatabaseContainer container = new PostgreSQLContainer();21 String initScript = "CREATE TABLE IF NOT EXISTS test_table (id SERIAL PRIMARY KEY, name VARCHAR(50), value INT);"22 + "INSERT INTO test_table VALUES(1, 'test', 10);";23 container.withInitScript(initScript);24 container.start();25 Connection connection = DriverManager.getConnection(container.getJdbcUrl(), container.getUsername(), container.getPassword());26 Statement statement = connection.createStatement();27 statement.execute("SELECT * FROM test_table");28 container.stop();29 }30}31import java.io.File;32import java.io.IOException;33import java.nio.file.Files;34import java.nio.file.Paths;35import java.sql.Connection;36import java.sql.DriverManager;37import java.sql.SQLException;38import java.sql.Statement;39import java.util.List;40import org.apache.commons.io.FileUtils;41import org.testcontainers.containers.JdbcDatabaseContainer;42import org.testcontainers.containers.PostgreSQLContainer;43import org.testcontainers.containers.output.Slf4jLogConsumer;44import org.testcontainers.containers.output.ToStringConsumer;45import org.testcontainers.containers.output.WaitingConsumer;46import org.testcontainers.utility.MountableFile;47public class TestcontainersInitScript {48 public static void main(String[] args) throws SQLException, IOException {49 JdbcDatabaseContainer container = new PostgreSQLContainer();

Full Screen

Full Screen

withInitScript

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.JdbcDatabaseContainer;2import org.testcontainers.containers.MySQLContainer;3import org.testcontainers.containers.PostgreSQLContainer;4import org.testcontainers.containers.MSSQLServerContainer;5import java.io.File;6import java.io.IOException;7import java.nio.charset.StandardCharsets;8import java.nio.file.Files;9import java.nio.file.Paths;10import java.util.stream.Stream;11import org.apache.commons.io.FileUtils;12import org.apache.commons.io.IOUtils;13import org.apache.commons.io.LineIterator;14import org.testcontainers.utility.MountableFile;15public class TestContainer {16 public static void main(String[] args) {17 MSSQLServerContainer mssql = new MSSQLServerContainer("mcr.microsoft.com/​mssql/​server:2017-latest").withInitScript("1.sql");18 mssql.start();19 System.out.println(mssql.getJdbcUrl());20 System.out.println(mssql.getUsername());21 System.out.println(mssql.getPassword());22 mssql.stop();23 }24}25CREATE DATABASE test;26USE test;27CREATE TABLE test_table (id INT, name VARCHAR(20));28INSERT INTO test_table VALUES (1, 'test1');29INSERT INTO test_table VALUES (2, 'test2');30INSERT INTO test_table VALUES (3, 'test3');31CREATE DATABASE test;32\c test;33CREATE TABLE test_table (id INT, name VARCHAR(20));34INSERT INTO test_table VALUES (1, 'test1');35INSERT INTO test_table VALUES (2, 'test2');36INSERT INTO test_table VALUES (3, 'test3');37CREATE DATABASE test;38USE test;39CREATE TABLE test_table (id INT, name VARCHAR(20));40INSERT INTO test_table VALUES (

Full Screen

Full Screen

withInitScript

Using AI Code Generation

copy

Full Screen

1import java.io.File;2import java.io.IOException;3import java.sql.Connection;4import java.sql.ResultSet;5import java.sql.SQLException;6import java.sql.Statement;7import org.testcontainers.containers.JdbcDatabaseContainer;8import org.testcontainers.containers.MySQLContainer;9public class 1 {10 public static void main(String[] args) throws SQLException, IOException {11 JdbcDatabaseContainer<?> container = new MySQLContainer<>("mysql:8.0.19")12 .withInitScript("init.sql");13 container.start();14 Connection connection = container.createConnection("");15 Statement statement = connection.createStatement();16 ResultSet resultSet = statement.executeQuery("SELECT * FROM test");17 while (resultSet.next()) {18 System.out.println(resultSet.getString("name"));19 }20 connection.close();21 }22}23CREATE TABLE test (name VARCHAR(20));24INSERT INTO test (name) VALUES ('test1');25INSERT INTO test (name) VALUES ('test2');26INSERT INTO test (name) VALUES ('test3');27plugins {28}29repositories {30 mavenCentral()31}32dependencies {33}34docker {35}36task dockerBuildImage(type: DockerBuildImage) {37 inputDir = file('.')38}39task dockerComposeUp(type: DockerComposeUp) {40 doFirst {41 }

Full Screen

Full Screen

Blogs

Check out the latest blogs from LambdaTest on this topic:

Putting Together a Testing Team

As part of one of my consulting efforts, I worked with a mid-sized company that was looking to move toward a more agile manner of developing software. As with any shift in work style, there is some bewilderment and, for some, considerable anxiety. People are being challenged to leave their comfort zones and embrace a continuously changing, dynamic working environment. And, dare I say it, testing may be the most ‘disturbed’ of the software roles in agile development.

A Reconsideration of Software Testing Metrics

There is just one area where each member of the software testing community has a distinct point of view! Metrics! This contentious issue sparks intense disputes, and most conversations finish with no definitive conclusion. It covers a wide range of topics: How can testing efforts be measured? What is the most effective technique to assess effectiveness? Which of the many components should be quantified? How can we measure the quality of our testing performance, among other things?

Fluent Interface Design Pattern in Automation Testing

Recently, I was going through some of the design patterns in Java by reading the book Head First Design Patterns by Eric Freeman, Elisabeth Robson, Bert Bates, and Kathy Sierra.

Different Ways To Style CSS Box Shadow Effects

Have you ever visited a website that only has plain text and images? Most probably, no. It’s because such websites do not exist now. But there was a time when websites only had plain text and images with almost no styling. For the longest time, websites did not focus on user experience. For instance, this is how eBay’s homepage looked in 1999.

What will come after “agile”?

I think that probably most development teams describe themselves as being “agile” and probably most development teams have standups, and meetings called retrospectives.There is also a lot of discussion about “agile”, much written about “agile”, and there are many presentations about “agile”. A question that is often asked is what comes after “agile”? Many testers work in “agile” teams so this question matters to us.

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful