Best Testcontainers-java code snippet using org.testcontainers.containers.OracleContainer.withDatabaseName
Source:SimpleOracleTest.java
...35 @Test36 public void testPluggableDatabase() throws SQLException {37 try (38 OracleContainer oracle = new OracleContainer(ORACLE_DOCKER_IMAGE_NAME)39 .withDatabaseName("testDB")40 ) {41 runTest(oracle, "testDB", "test", "test");42 }43 }44 @Test45 public void testPluggableDatabaseAndCustomUser() throws SQLException {46 try (47 OracleContainer oracle = new OracleContainer(ORACLE_DOCKER_IMAGE_NAME)48 .withDatabaseName("testDB")49 .withUsername("testUser")50 .withPassword("testPassword")51 ) {52 runTest(oracle, "testDB", "testUser", "testPassword");53 }54 }55 @Test56 public void testCustomUser() throws SQLException {57 try (58 OracleContainer oracle = new OracleContainer(ORACLE_DOCKER_IMAGE_NAME)59 .withUsername("testUser")60 .withPassword("testPassword")61 ) {62 runTest(oracle, "xepdb1", "testUser", "testPassword");63 }64 }65 @Test66 public void testSID() throws SQLException {67 try (68 OracleContainer oracle = new OracleContainer(ORACLE_DOCKER_IMAGE_NAME)69 .usingSid();70 ) {71 runTest(oracle, "xepdb1", "system", "test");72 // Match against the last ':'73 String urlSuffix = oracle.getJdbcUrl().split("(\\:)(?!.*\\:)", 2)[1];74 assertEquals("xe", urlSuffix);75 }76 }77 @Test78 public void testSIDAndCustomPassword() throws SQLException {79 try (80 OracleContainer oracle = new OracleContainer(ORACLE_DOCKER_IMAGE_NAME)81 .usingSid()82 .withPassword("testPassword");83 ) {84 runTest(oracle, "xepdb1", "system", "testPassword");85 }86 }87 @Test88 public void testErrorPaths() throws SQLException {89 try (OracleContainer oracle = new OracleContainer(ORACLE_DOCKER_IMAGE_NAME)) {90 try {91 oracle.withDatabaseName("XEPDB1");92 fail("Should not have been able to set database name to xepdb1.");93 } catch (IllegalArgumentException e) {94 //expected95 }96 try {97 oracle.withDatabaseName("");98 fail("Should not have been able to set database name to nothing.");99 } catch (IllegalArgumentException e) {100 //expected101 }102 try {103 oracle.withUsername("SYSTEM");104 fail("Should not have been able to set username to system.");105 } catch (IllegalArgumentException e) {106 //expected107 }108 try {109 oracle.withUsername("SYS");110 fail("Should not have been able to set username to sys.");111 } catch (IllegalArgumentException e) {...
Source:DatabaseWithTestContainersOracleIT.java
...38 }39 // will be shared between test methods40 @Container41 private static final OracleContainer ORACLE_CONTAINER = new OracleContainer("gvenzl/oracle-xe:18.4.0-slim")42 .withDatabaseName("testDB")43 .withUsername("testUser")44 .withPassword("testPassword");45 @Override46 public JdbcDatabaseContainer getJdbcDatabaseContainer() {47 return ORACLE_CONTAINER;48 }49 @Override50 public DatabaseEngine getDatabaseEngineType() {51 return DatabaseEngine.ORACLE;52 }53 54 @Override55 public LeaderElectorConfiguration getLeaderElectorConfiguration(String schemaName, String tableName) {56 return LeaderElectorConfiguration.builder()...
withDatabaseName
Using AI Code Generation
1import org.testcontainers.containers.OracleContainer;2import org.testcontainers.containers.output.Slf4jLogConsumer;3import org.testcontainers.utility.DockerImageName;4import org.slf4j.Logger;5import org.slf4j.LoggerFactory;6public class Test {7 private static final Logger logger = LoggerFactory.getLogger(Test.class);8 public static void main(String[] args) {9 OracleContainer oracleContainer = new OracleContainer(DockerImageName.parse("oracleinanutshell/oracle-xe-11g"))10 .withDatabaseName("TESTDB")11 .withLogConsumer(new Slf4jLogConsumer(logger));12 oracleContainer.start();13 System.out.println("Oracle JDBC URL: " + oracleContainer.getJdbcUrl());14 System.out.println("Oracle JDBC Username: " + oracleContainer.getUsername());15 System.out.println("Oracle JDBC Password: " + oracleContainer.getPassword());16 oracleContainer.stop();17 }18}19import org.testcontainers.containers.OracleContainer;20import org.testcontainers.containers.output.Slf4jLogConsumer;21import org.testcontainers.utility.DockerImageName;22import org.slf4j.Logger;23import org.slf4j.LoggerFactory;24import java.io.File;25import java.io.IOException;26import java.net.URISyntaxException;27import java.net.URL;28import java.nio.file.Files;29import java.nio.file.Path;30import java.nio.file.Paths;31public class Test {32 private static final Logger logger = LoggerFactory.getLogger(Test.class);33 public static void main(String[] args) throws IOException, URISyntaxException {34 URL resource = Test.class.getClassLoader().getResource("init.sql");35 File file = Paths.get(resource.toURI()).toFile();36 String initScript = file.getAbsolutePath();37 OracleContainer oracleContainer = new OracleContainer(DockerImageName.parse("oracleinanutshell/oracle-xe-11g"))38 .withInitScript(initScript)39 .withLogConsumer(new Slf4jLogConsumer(logger));40 oracleContainer.start();41 System.out.println("Oracle JDBC URL: " + oracleContainer.getJdbcUrl());42 System.out.println("Oracle JDBC Username: " + oracleContainer.getUsername());43 System.out.println("Oracle JDBC Password: " + oracleContainer.getPassword());44 oracleContainer.stop();45 }46}47create table test (id number(10), name varchar2(20
withDatabaseName
Using AI Code Generation
1import org.testcontainers.containers.OracleContainer;2import org.testcontainers.containers.output.Slf4jLogConsumer;3import org.testcontainers.utility.DockerImageName;4import org.slf4j.Logger;5import org.slf4j.LoggerFactory;6public class OracleContainerDemo {7 private static final Logger LOGGER = LoggerFactory.getLogger(OracleContainerDemo.class);8 public static void main(String[] args) {9 OracleContainer oracleContainer = new OracleContainer(DockerImageName.parse("oracleinanutshell/oracle-xe-11g"))10 .withDatabaseName("testdb")11 .withUsername("testuser")12 .withPassword("testpassword");13 oracleContainer.start();14 LOGGER.info("Oracle JDBC URL: " + oracleContainer.getJdbcUrl());15 LOGGER.info("Oracle JDBC Username: " + oracleContainer.getUsername());16 LOGGER.info("Oracle JDBC Password: " + oracleContainer.getPassword());17 oracleContainer.stop();18 }19}20import org.testcontainers.containers.OracleContainer;21import org.testcontainers.containers.output.Slf4jLogConsumer;22import org.testcontainers.utility.DockerImageName;23import org.slf4j.Logger;24import org.slf4j.LoggerFactory;25public class OracleContainerDemo {26 private static final Logger LOGGER = LoggerFactory.getLogger(OracleContainerDemo.class);27 public static void main(String[] args) {28 OracleContainer oracleContainer = new OracleContainer(DockerImageName.parse("oracleinanutshell/oracle-xe-11g"))29 .withDatabaseName("testdb")30 .withUsername("testuser")31 .withPassword("testpassword");32 oracleContainer.start();33 LOGGER.info("Oracle JDBC URL: " + oracleContainer.getJdbcUrl());34 LOGGER.info("Oracle JDBC Username: " + oracleContainer.getUsername());35 LOGGER.info("Oracle JDBC Password: " + oracleContainer.getPassword());36 oracleContainer.stop();37 }38}
withDatabaseName
Using AI Code Generation
1import java.sql.Connection;2import java.sql.DriverManager;3import java.sql.ResultSet;4import java.sql.SQLException;5import java.sql.Statement;6import org.junit.Assert;7import org.junit.Test;8import org.testcontainers.containers.OracleContainer;9public class OracleContainerWithDatabaseNameTest {10 public void testSimple() throws SQLException {11 try (OracleContainer oracle = new OracleContainer()12 .withDatabaseName("test")13 .withUsername("test")14 .withPassword("test")) {15 oracle.start();16 try (Connection connection = DriverManager.getConnection(oracle.getJdbcUrl(), oracle.getUsername(), oracle.getPassword())) {17 try (Statement statement = connection.createStatement()) {18 try (ResultSet resultSet = statement.executeQuery("SELECT 1 FROM DUAL")) {19 Assert.assertTrue(resultSet.next());20 Assert.assertEquals(1, resultSet.getInt(1));21 }22 }23 }24 }25 }26}
withDatabaseName
Using AI Code Generation
1import org.testcontainers.containers.OracleContainer;2import java.sql.Connection;3import java.sql.DriverManager;4import java.sql.ResultSet;5import java.sql.SQLException;6import java.sql.Statement;7public class 1 {8 public static void main(String[] args) throws SQLException {9 OracleContainer oracle = new OracleContainer("oracleinanutshell/oracle-xe-11g:18.4.0");10 oracle.start();11 String jdbcUrl = oracle.getJdbcUrl();12 String username = oracle.getUsername();13 String password = oracle.getPassword();14 Connection connection = DriverManager.getConnection(jdbcUrl, username, password);15 Statement statement = connection.createStatement();16 ResultSet resultSet = statement.executeQuery("SELECT 1 FROM DUAL");17 while (resultSet.next()) {18 System.out.println(resultSet.getString(1));19 }20 statement.close();21 connection.close();22 oracle.stop();23 }24}
withDatabaseName
Using AI Code Generation
1package org.testcontainers.containers;2import org.junit.Test;3import org.testcontainers.containers.output.Slf4jLogConsumer;4import org.testcontainers.utility.DockerImageName;5import java.sql.Connection;6import java.sql.ResultSet;7import java.sql.SQLException;8import java.sql.Statement;9public class OracleContainerTest {10 public void test() throws SQLException {11 OracleContainer oracleContainer = new OracleContainer(DockerImageName.parse("oracleinanutshell/oracle-xe-11g:18.4.0").asCompatibleSubstituteFor("oracle-xe-11g"));12 oracleContainer.withDatabaseName("test");13 oracleContainer.start();14 oracleContainer.followOutput(new Slf4jLogConsumer(LoggerFactory.getLogger("oracle-container")));15 Connection connection = oracleContainer.createConnection("");16 Statement statement = connection.createStatement();17 ResultSet resultSet = statement.executeQuery("select * from dual");18 while(resultSet.next()) {19 System.out.println(resultSet.getString(1));20 }21 }22}23package org.testcontainers.containers;24import org.junit.Test;25import org.testcontainers.containers.output.Slf4jLogConsumer;26import org.testcontainers.utility.DockerImageName;27import java.sql.Connection;28import java.sql.ResultSet;29import java.sql.SQLException;30import java.sql.Statement;31public class OracleContainerTest {32 public void test() throws SQLException {33 OracleContainer oracleContainer = new OracleContainer(DockerImageName.parse("oracleinanutshell/oracle-xe-11g:18.4.0").asCompatibleSubstituteFor("oracle-xe-11g"));34 oracleContainer.withDatabaseName("test");35 oracleContainer.start();36 oracleContainer.followOutput(new Slf4jLogConsumer(LoggerFactory.getLogger("oracle-container")));37 Connection connection = oracleContainer.createConnection("");38 Statement statement = connection.createStatement();39 ResultSet resultSet = statement.executeQuery("select * from dual");40 while(resultSet.next()) {41 System.out.println(resultSet.getString(1));42 }43 }44}45package org.testcontainers.containers;46import org.junit.Test;47import org.testcontainers.containers.output.Slf4jLogConsumer;48import org.testcontainers.utility.DockerImageName;49import java.sql.Connection;50import java.sql.ResultSet;51import
withDatabaseName
Using AI Code Generation
1package org.testcontainers.containers;2import org.junit.Test;3import org.testcontainers.containers.output.Slf4jLogConsumer;4import java.sql.Connection;5import java.sql.ResultSet;6import java.sql.SQLException;7import java.sql.Statement;8public class OracleContainerTest {9 private static final String ORACLE_IMAGE = "wnameless/oracle-xe-11g-r2";10 public void testOracleContainer() throws SQLException {11 OracleContainer oracleContainer = new OracleContainer(ORACLE_IMAGE)12 .withDatabaseName("test")13 .withUsername("test")14 .withPassword("test");15 oracleContainer.start();16 Connection connection = oracleContainer.createConnection("");17 try (Statement statement = connection.createStatement();18 ResultSet resultSet = statement.executeQuery("select * from dual")) {19 resultSet.next();20 System.out.println(resultSet.getString(1));21 }22 oracleContainer.stop();23 }24}
withDatabaseName
Using AI Code Generation
1import org.testcontainers.containers.OracleContainer;2import org.testcontainers.containers.output.Slf4jLogConsumer;3import org.testcontainers.containers.output.WaitingConsumer;4import org.testcontainers.containers.OracleContainer;5import org.testcontainers.containers.output.Slf4jLogConsumer;6import org.testcontainers.containers.output.WaitingConsumer;7import org.testcontainers.utility.DockerImageName;8import org.slf4j.Logger;9import org.slf4j.LoggerFactory;10import java.sql.Connection;11import java.sql.DriverManager;12import java.sql.SQLException;13import java.sql.Statement;14import java.util.concurrent.TimeUnit;15import java.util.concurrent.TimeoutException;16import java.util.concurrent.atomic.AtomicReference;17import java.util.function.Consumer;18import java.util.stream.Collectors;19import java.util.stream.Stream;20import org.junit.*;21import org.testcontainers.containers.OracleContainer;22import org.testcontainers.containers.output.Slf4jLogConsumer;23import org.testcontainers.containers.output.WaitingConsumer;24import org.testcontainers.utility.DockerImageName;25import org.slf4j.Logger;26import org.slf4j.LoggerFactory;27import java.sql.Connection;28import java.sql.DriverManager;29import java.sql.SQLException;30import java.sql.Statement;31import java.util.concurrent.TimeUnit;32import java.util.concurrent.TimeoutException;33import java.util.concurrent.atomic.AtomicReference;34import java.util.function.Consumer;35import java.util.stream.Collectors;36import java.util.stream.Stream;37import org.junit.*;38import static org.junit.Assert.*;39import static org.rnorth.visibleassertions.VisibleAssertions.*;40public class 1 {41 private static final Logger logger = LoggerFactory.getLogger(1.class);42 private static OracleContainer oracle;43 private static final String ORACLE_IMAGE = "oracleinanutshell/oracle-xe-11g";
withDatabaseName
Using AI Code Generation
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 org.junit.Test;8public class TestOracleContainer {9 public void testOracleContainer() throws SQLException {10 OracleContainer oracle = new OracleContainer()11 .withDatabaseName("testDB");12 oracle.start();13 Connection connection = DriverManager.getConnection(oracle.getJdbcUrl(), oracle.getUsername(), oracle.getPassword());14 Statement statement = connection.createStatement();15 statement.execute("CREATE TABLE test (id NUMBER(6))");16 statement.execute("INSERT INTO test VALUES (1)");17 ResultSet resultSet = statement.executeQuery("SELECT * FROM test");18 resultSet.next();19 int id = resultSet.getInt("id");20 System.out.println(id);21 }22}23package org.testcontainers.containers;24import java.sql.Connection;25import java.sql.DriverManager;26import java.sql.ResultSet;27import java.sql.SQLException;28import java.sql.Statement;29import org.junit.Test;30public class TestOracleContainer {31 public void testOracleContainer() throws SQLException {32 OracleContainer oracle = new OracleContainer()33 .withInitScript("init.sql");34 oracle.start();35 Connection connection = DriverManager.getConnection(oracle.getJdbcUrl(), oracle.getUsername(), oracle.getPassword());36 Statement statement = connection.createStatement();37 ResultSet resultSet = statement.executeQuery("SELECT * FROM test");38 resultSet.next();39 int id = resultSet.getInt("id");40 System.out.println(id);41 }42}43CREATE TABLE test (id NUMBER(6));44INSERT INTO test VALUES (1);45package org.testcontainers.containers;46import java.sql.Connection;47import java.sql.DriverManager;48import java.sql.ResultSet;49import java.sql.SQLException;50import java.sql.Statement;51import org.junit.Test;52public class TestOracleContainer {53 public void testOracleContainer() throws SQLException {54 OracleContainer oracle = new OracleContainer()55 .withEnv("ORACLE_PWD", "password");56 oracle.start();57 Connection connection = DriverManager.getConnection(oracle.getJdbcUrl(), oracle.getUsername(), oracle.getPassword
withDatabaseName
Using AI Code Generation
1import org.junit.Test;2import org.testcontainers.containers.OracleContainer;3public class OracleContainerTest {4 public void testOracleContainer() {5 OracleContainer oracleContainer = new OracleContainer("oracleinanutshell/oracle-xe-11g");6 oracleContainer.withDatabaseName("test");7 oracleContainer.start();8 System.out.println(oracleContainer.getJdbcUrl());9 System.out.println(oracleContainer.getUsername());10 System.out.println(oracleContainer.getPassword());11 oracleContainer.stop();12 }13}14import org.junit.Test;15import org.testcontainers.containers.OracleContainer;16public class OracleContainerTest {17 public void testOracleContainer() {18 OracleContainer oracleContainer = new OracleContainer("oracleinanutshell/oracle-xe-11g");19 oracleContainer.withInitScript("init.sql");20 oracleContainer.start();21 System.out.println(oracleContainer.getJdbcUrl());22 System.out.println(oracleContainer.getUsername());23 System.out.println(oracleContainer.getPassword());24 oracleContainer.stop();25 }26}27import org.junit.Test;28import org.testcontainers.containers.OracleContainer;29public class OracleContainerTest {30 public void testOracleContainer() {31 OracleContainer oracleContainer = new OracleContainer("oracleinanutshell/oracle-xe-11g");32 oracleContainer.withUrlParam("useUnicode", "true");33 oracleContainer.withUrlParam("characterEncoding", "UTF-8");34 oracleContainer.start();35 System.out.println(oracleContainer.getJdbcUrl());36 System.out.println(oracleContainer.getUsername());37 System.out.println(oracleContainer.getPassword());38 oracleContainer.stop();39 }40}41import org.junit.Test;42import org.testcontainers.containers.OracleContainer;43public class OracleContainerTest {44 public void testOracleContainer() {45 OracleContainer oracleContainer = new OracleContainer("oracleinanutshell/oracle-xe-11g");46 oracleContainer.withCommand("sh", "-c", "exec /etc/init.d/oracle
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!