Best Testcontainers-java code snippet using org.testcontainers.jdbc.AbstractJDBCDriverTest.performSimpleTestWithCharacterSet
Source:AbstractJDBCDriverTest.java
...49 if (options.contains(Options.JDBCParams)) {50 performTestForJDBCParamUsage(dataSource);51 }52 if (options.contains(Options.CharacterSet)) {53 performSimpleTestWithCharacterSet(jdbcUrl);54 performTestForCharacterEncodingForInitialScriptConnection(dataSource);55 }56 if (options.contains(Options.CustomIniFile)) {57 performTestForCustomIniFile(dataSource);58 }59 }60 }61 private void performSimpleTest(HikariDataSource dataSource) throws SQLException {62 String query = "SELECT 1";63 if (jdbcUrl.startsWith("jdbc:tc:db2:")) {64 query = "SELECT 1 FROM SYSIBM.SYSDUMMY1";65 }66 boolean result = new QueryRunner(dataSource, options.contains(Options.PmdKnownBroken)).query(query, rs -> {67 rs.next();68 int resultSetInt = rs.getInt(1);69 assertEquals("A basic SELECT query succeeds", 1, resultSetInt);70 return true;71 });72 assertTrue("The database returned a record as expected", result);73 }74 private void performTestForScriptedSchema(HikariDataSource dataSource) throws SQLException {75 boolean result = new QueryRunner(dataSource).query("SELECT foo FROM bar WHERE foo LIKE '%world'", rs -> {76 rs.next();77 String resultSetString = rs.getString(1);78 assertEquals("A basic SELECT query succeeds where the schema has been applied from a script", "hello world", resultSetString);79 return true;80 });81 assertTrue("The database returned a record as expected", result);82 }83 private void performTestForJDBCParamUsage(HikariDataSource dataSource) throws SQLException {84 boolean result = new QueryRunner(dataSource).query("select CURRENT_USER", rs -> {85 rs.next();86 String resultUser = rs.getString(1);87 // Not all databases (eg. Postgres) return @% at the end of user name. We just need to make sure the user name matches.88 if (resultUser.endsWith("@%")) {89 resultUser = resultUser.substring(0, resultUser.length() - 2);90 }91 assertEquals("User from query param is created.", "someuser", resultUser);92 return true;93 });94 assertTrue("The database returned a record as expected", result);95 String databaseQuery = "SELECT DATABASE()";96 // Postgres does not have Database() as a function97 String databaseType = ConnectionUrl.newInstance(jdbcUrl).getDatabaseType();98 if (databaseType.equalsIgnoreCase("postgresql") ||99 databaseType.equalsIgnoreCase("postgis") ||100 databaseType.equalsIgnoreCase("timescaledb")) {101 databaseQuery = "SELECT CURRENT_DATABASE()";102 }103 result = new QueryRunner(dataSource).query(databaseQuery, rs -> {104 rs.next();105 String resultDB = rs.getString(1);106 assertEquals("Database name from URL String is used.", "databasename", resultDB);107 return true;108 });109 assertTrue("The database returned a record as expected", result);110 }111 private void performTestForCharacterEncodingForInitialScriptConnection(HikariDataSource dataSource) throws SQLException {112 boolean result = new QueryRunner(dataSource).query("SELECT foo FROM bar WHERE foo LIKE '%миÑ'", rs -> {113 rs.next();114 String resultSetString = rs.getString(1);115 assertEquals("A SELECT query succeed and the correct charset has been applied for the init script", "пÑÐ¸Ð²ÐµÑ Ð¼Ð¸Ñ", resultSetString);116 return true;117 });118 assertTrue("The database returned a record as expected", result);119 }120 /**121 * This method intentionally verifies encoding twice to ensure that the query string parameters are used when122 * Connections are created from cached containers.123 *124 * @param jdbcUrl125 * @throws SQLException126 */127 private void performSimpleTestWithCharacterSet(String jdbcUrl) throws SQLException {128 HikariDataSource datasource1 = verifyCharacterSet(jdbcUrl);129 HikariDataSource datasource2 = verifyCharacterSet(jdbcUrl);130 datasource1.close();131 datasource2.close();132 }133 private HikariDataSource verifyCharacterSet(String jdbcUrl) throws SQLException {134 HikariDataSource dataSource = getDataSource(jdbcUrl, 1);135 boolean result = new QueryRunner(dataSource).query("SHOW VARIABLES LIKE 'character\\_set\\_connection'", rs -> {136 rs.next();137 String resultSetString = rs.getString(2);138 assertTrue("Passing query parameters to set DB connection encoding is successful", resultSetString.startsWith("utf8"));139 return true;140 });141 assertTrue("The database returned a record as expected", result);...
performSimpleTestWithCharacterSet
Using AI Code Generation
1public class MySQLDriverTest extends AbstractJDBCDriverTest {2 public MySQLDriverTest() {3 super(new MySQLContainer());4 }5}6public class PostgresDriverTest extends AbstractJDBCDriverTest {7 public PostgresDriverTest() {8 super(new PostgreSQLContainer());9 }10}11public class SQLServerDriverTest extends AbstractJDBCDriverTest {12 public SQLServerDriverTest() {13 super(new SQLServerContainer());14 }15}16public class OracleDriverTest extends AbstractJDBCDriverTest {17 public OracleDriverTest() {18 super(new OracleContainer());19 }20}21public class JDBCDriverTest {22 public void testMySQLDriver() {23 new MySQLDriverTest().performSimpleTestWithCharacterSet();24 }25 public void testPostgresDriver() {26 new PostgresDriverTest().performSimpleTestWithCharacterSet();27 }28 public void testSQLServerDriver() {29 new SQLServerDriverTest().performSimpleTestWithCharacterSet();30 }31 public void testOracleDriver() {32 new OracleDriverTest().performSimpleTestWithCharacterSet();33 }34}35public class OracleDriverTest extends AbstractJDBCDriverTest {36 public OracleDriverTest() {37 super(new OracleContainer());38 }39 protected void configureContainer(OracleContainer container) {40 container.withInitScript("init.sql");41 }42 protected String getJdbcUrl() {43 return getContainer().getJdbcUrl() + "?useUnicode=true&characterEncoding=UTF-8";44 }45}46ALTER SESSION SET NLS_LANGUAGE='AMERICAN';47ALTER SESSION SET NLS_TERRITORY='AMERICA';
performSimpleTestWithCharacterSet
Using AI Code Generation
1public class PostgreSqlContainerTest extends AbstractJDBCDriverTest {2 public JdbcDatabaseContainer createContainer() {3 return new PostgreSQLContainer();4 }5 public String getDriverClassName() {6 return "org.postgresql.Driver";7 }8 public String getTestQueryString() {9 return "SELECT 1";10 }11 public String getTestQueryStringWithCharacterSet() {12 return "SELECT 'áéíóú'";13 }14 public String getTestQueryStringWithSpecialCharacterSet() {15 return "SELECT 'àèìòù'";16 }17}18public class MySqlContainerTest extends AbstractJDBCDriverTest {19 public JdbcDatabaseContainer createContainer() {20 return new MySQLContainer();21 }22 public String getDriverClassName() {23 return "com.mysql.jdbc.Driver";24 }25 public String getTestQueryString() {26 return "SELECT 1";27 }28 public String getTestQueryStringWithCharacterSet() {29 return "SELECT 'áéíóú'";30 }31 public String getTestQueryStringWithSpecialCharacterSet() {32 return "SELECT 'àèìòù'";33 }34}35public class MariaDbContainerTest extends AbstractJDBCDriverTest {36 public JdbcDatabaseContainer createContainer() {37 return new MariaDBContainer();38 }39 public String getDriverClassName() {40 return "org.mariadb.jdbc.Driver";41 }42 public String getTestQueryString() {43 return "SELECT 1";44 }45 public String getTestQueryStringWithCharacterSet() {46 return "SELECT 'áéíóú'";47 }48 public String getTestQueryStringWithSpecialCharacterSet() {49 return "SELECT 'àèìòù'";50 }51}52public class OracleContainerTest extends AbstractJDBCDriverTest {
performSimpleTestWithCharacterSet
Using AI Code Generation
1public class JDBCDriverTest extends AbstractJDBCDriverTest {2 protected JdbcDatabaseContainer createContainer() {3 return new MariaDBContainer <>( "mariadb:10.4" );4 }5 protected String getDriverClassName() {6 return "org.mariadb.jdbc.Driver" ;7 }8 protected String getTestQueryString() {9 return "SELECT 1" ;10 }11 protected String getTestQueryStringWithParameter() {12 return "SELECT ?" ;13 }14 protected String getTestQueryStringWithParameters() {15 return "SELECT ?, ?" ;16 }17}
performSimpleTestWithCharacterSet
Using AI Code Generation
1def performSimpleTestWithCharacterSet(charset) {2 def driver = getDriverClassName()3 def container = getDriverContainerClass().newInstance()4 def test = new AbstractJDBCDriverTest() {5 protected String getDriverClassName() {6 }7 protected GenericContainer getDriverContainerClass() {8 }9 }10 return test.performSimpleTestWithCharacterSet(charset)11}12def testWithAllCharsets() {
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!!