Best Testcontainers-java code snippet using org.testcontainers.jdbc.mysql.JDBCDriverWithPoolTest.getTomcatDataSource
Source:JDBCDriverWithPoolTest.java
...30 private final DataSource dataSource;31 @Parameterized.Parameters32 public static Iterable<Supplier<DataSource>> dataSourceSuppliers() {33 return asList(34 JDBCDriverWithPoolTest::getTomcatDataSourceWithDriverClassName,35 JDBCDriverWithPoolTest::getTomcatDataSource,36 JDBCDriverWithPoolTest::getHikariDataSourceWithDriverClassName,37 JDBCDriverWithPoolTest::getHikariDataSource,38 JDBCDriverWithPoolTest::getViburDataSourceWithDriverClassName,39 JDBCDriverWithPoolTest::getViburDataSource40 );41 }42 public JDBCDriverWithPoolTest(Supplier<DataSource> dataSourceSupplier) {43 this.dataSource = dataSourceSupplier.get();44 }45 private ExecutorService executorService = Executors.newFixedThreadPool(5);46 @Test47 public void testMySQLWithConnectionPoolUsingSameContainer() throws SQLException, InterruptedException {48 // Populate the database with some data in multiple threads, so that multiple connections from the pool will be used49 for (int i = 0; i < 100; i++) {50 executorService.submit(() -> {51 try {52 new QueryRunner(dataSource).insert("INSERT INTO my_counter (n) VALUES (5)",53 (ResultSetHandler<Object>) rs -> true);54 } catch (SQLException e) {55 e.printStackTrace();56 }57 });58 }59 // Complete population of the database60 executorService.shutdown();61 executorService.awaitTermination(5, TimeUnit.MINUTES);62 // compare to expected results63 int count = new QueryRunner(dataSource).query("SELECT COUNT(1) FROM my_counter", rs -> {64 rs.next();65 return rs.getInt(1);66 });67 assertEquals("Reuse of a datasource points to the same DB container", 100, count);68 int sum = new QueryRunner(dataSource).query("SELECT SUM(n) FROM my_counter", rs -> {69 rs.next();70 return rs.getInt(1);71 });72 // 100 records * 5 = 500 expected73 assertEquals("Reuse of a datasource points to the same DB container", 500, sum);74 }75 private static DataSource getTomcatDataSourceWithDriverClassName() {76 PoolProperties poolProperties = new PoolProperties();77 poolProperties.setUrl(URL + ";TEST=TOMCAT_WITH_CLASSNAME"); // append a dummy URL element to ensure different DB per test78 poolProperties.setValidationQuery("SELECT 1");79 poolProperties.setMinIdle(3);80 poolProperties.setMaxActive(10);81 poolProperties.setDriverClassName(ContainerDatabaseDriver.class.getName());82 return new org.apache.tomcat.jdbc.pool.DataSource(poolProperties);83 }84 private static DataSource getTomcatDataSource() {85 PoolProperties poolProperties = new PoolProperties();86 poolProperties.setUrl(URL + ";TEST=TOMCAT"); // append a dummy URL element to ensure different DB per test87 poolProperties.setValidationQuery("SELECT 1");88 poolProperties.setInitialSize(3);89 poolProperties.setMaxActive(10);90 return new org.apache.tomcat.jdbc.pool.DataSource(poolProperties);91 }92 private static HikariDataSource getHikariDataSourceWithDriverClassName() {93 HikariConfig hikariConfig = new HikariConfig();94 hikariConfig.setJdbcUrl(URL + ";TEST=HIKARI_WITH_CLASSNAME"); // append a dummy URL element to ensure different DB per test95 hikariConfig.setConnectionTestQuery("SELECT 1");96 hikariConfig.setMinimumIdle(3);97 hikariConfig.setMaximumPoolSize(10);98 hikariConfig.setDriverClassName(ContainerDatabaseDriver.class.getName());...
getTomcatDataSource
Using AI Code Generation
1JDBCDriverWithPoolTest getTomcatDataSource = new JDBCDriverWithPoolTest();2getTomcatDataSource.getTomcatDataSource();3JDBCDriverWithPoolTest getTomcatDataSource = new JDBCDriverWithPoolTest();4getTomcatDataSource.getTomcatDataSource();5JDBCDriverWithPoolTest getTomcatDataSource = new JDBCDriverWithPoolTest();6getTomcatDataSource.getTomcatDataSource();7JDBCDriverWithPoolTest getTomcatDataSource = new JDBCDriverWithPoolTest();8getTomcatDataSource.getTomcatDataSource();9JDBCDriverWithPoolTest getTomcatDataSource = new JDBCDriverWithPoolTest();10getTomcatDataSource.getTomcatDataSource();11JDBCDriverWithPoolTest getTomcatDataSource = new JDBCDriverWithPoolTest();12getTomcatDataSource.getTomcatDataSource();13JDBCDriverWithPoolTest getTomcatDataSource = new JDBCDriverWithPoolTest();14getTomcatDataSource.getTomcatDataSource();15JDBCDriverWithPoolTest getTomcatDataSource = new JDBCDriverWithPoolTest();16getTomcatDataSource.getTomcatDataSource();17JDBCDriverWithPoolTest getTomcatDataSource = new JDBCDriverWithPoolTest();18getTomcatDataSource.getTomcatDataSource();19JDBCDriverWithPoolTest getTomcatDataSource = new JDBCDriverWithPoolTest();20getTomcatDataSource.getTomcatDataSource();
getTomcatDataSource
Using AI Code Generation
1public class JDBCDriverWithPoolTest extends JDBCDriverTest {2 public void testGetTomcatDataSource() throws SQLException {3 DataSource dataSource = getTomcatDataSource();4 assertThat(dataSource, instanceOf(org.apache.tomcat.jdbc.pool.DataSource.class));5 org.apache.tomcat.jdbc.pool.DataSource tomcatDataSource = (org.apache.tomcat.jdbc.pool.DataSource) dataSource;6 }7}
getTomcatDataSource
Using AI Code Generation
1DataSource dataSource = getTomcatDataSource(container, "test", "test");2Connection connection = dataSource.getConnection();3Statement statement = connection.createStatement();4ResultSet resultSet = statement.executeQuery("SELECT 1");5resultSet.next();6int result = resultSet.getInt(1);7connection.close();8container.stop();9container.close();
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!!