How to use runInitScriptIfRequired method of org.testcontainers.jdbc.ContainerDatabaseDriver class

Best Testcontainers-java code snippet using org.testcontainers.jdbc.ContainerDatabaseDriver.runInitScriptIfRequired

Source:ContainerDatabaseDriver.java Github

copy

Full Screen

...107 an init script or function has been specified, use it108 */109 if (!initializedContainers.contains(container.getContainerId())) {110 DatabaseDelegate databaseDelegate = new JdbcDatabaseDelegate(container, queryString);111 runInitScriptIfRequired(connectionUrl, databaseDelegate);112 runInitFunctionIfRequired(connectionUrl, connection);113 initializedContainers.add(container.getContainerId());114 }115 return wrapConnection(connection, container, connectionUrl);116 }117 }118 /**119 * Wrap the connection, setting up a callback to be called when the connection is closed.120 * <p>121 * When there are no more open connections, the container itself will be stopped.122 *123 * @param connection the new connection to be wrapped124 * @param container the container which the connection is associated with125 * @param connectionUrl {@link ConnectionUrl} instance representing JDBC Url for this connection126 * @return the connection, wrapped127 */128 private Connection wrapConnection(final Connection connection, final JdbcDatabaseContainer container, final ConnectionUrl connectionUrl) {129 final boolean isDaemon = connectionUrl.isInDaemonMode() || connectionUrl.isReusable();130 Set<Connection> connections = containerConnections.computeIfAbsent(container.getContainerId(), k -> new HashSet<>());131 connections.add(connection);132 final Set<Connection> finalConnections = connections;133 return new ConnectionWrapper(connection, () -> {134 finalConnections.remove(connection);135 if (!isDaemon && finalConnections.isEmpty()) {136 container.stop();137 jdbcUrlContainerCache.remove(connectionUrl.getUrl());138 }139 });140 }141 /**142 * Run an init script from the classpath.143 *144 * @param connectionUrl {@link ConnectionUrl} instance representing JDBC Url with init script.145 * @param databaseDelegate database delegate to apply init scripts to the database146 * @throws SQLException on script or DB error147 */148 private void runInitScriptIfRequired(final ConnectionUrl connectionUrl, DatabaseDelegate databaseDelegate) throws SQLException {149 if (connectionUrl.getInitScriptPath().isPresent()) {150 String initScriptPath = connectionUrl.getInitScriptPath().get();151 try {152 URL resource;153 if (initScriptPath.startsWith(FILE_PATH_PREFIX)) {154 //relative workdir path155 resource = new URL(initScriptPath);156 } else {157 //classpath resource158 resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath);159 }160 if (resource == null) {161 LOGGER.warn("Could not load classpath init script: {}", initScriptPath);162 throw new SQLException("Could not load classpath init script: " + initScriptPath + ". Resource not found.");...

Full Screen

Full Screen

runInitScriptIfRequired

Using AI Code Generation

copy

Full Screen

1[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ testcontainers-demo ---2[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ testcontainers-demo ---3[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ testcontainers-demo ---4[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ testcontainers-demo ---5[INFO] --- spring-boot-maven-plugin:2.2.6.RELEASE:repackage (repackage) @ testcontainers-demo ---6[INFO] --- maven-failsafe-plugin:2.22.2:integration-test (default) @ testcontainers-demo ---

Full Screen

Full Screen

runInitScriptIfRequired

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.JdbcDatabaseContainer2import java.sql.DriverManager3import java.sql.Connection4import java.sql.ResultSet5import java.sql.Statement6import java.sql.SQLException7import java.sql.SQLFeatureNotSupportedException8import java.sql.DriverPropertyInfo9import java.util.Properties10import java.util.logging.Logger11import java.util.logging.Level12import java.util.regex.Pattern13import java.util.regex.Matcher14import java.util.stream.Collectors15import java.util.stream.Stream16import java.util.function.Consumer17import java.util.function.Function18import java.util.function.Supplier19import java.util.function.Predicate20import java.util.function.BiFunction21import java.util.function.BiConsumer22import java.util.function.BinaryOperator23import java.util.function.UnaryOperator24import java.util.function.IntConsumer25import java.util.function.LongConsumer26import java.util.function.DoubleConsumer27import java.util.function.ObjIntConsumer28import java.util.function.ObjLongConsumer29import java.util.function.ObjDoubleConsumer30import java.util.function.IntFunction31import java.util.function.IntToLongFunction32import java.util.function.IntToDoubleFunction33import java.util.function.LongToIntFunction34import java.util.function.LongToDoubleFunction35import java.util.function.DoubleToIntFunction36import java.util.function.DoubleToLongFunction37import java.util.function.IntUnaryOperator38import java.util.function.LongUnaryOperator39import java.util.function.DoubleUnaryOperator40import java.util.function.IntBinaryOperator41import java.util.function.LongBinaryOperator42import java.util.function.DoubleBinaryOperator43import java.util.function.ToIntFunction44import java.util.function.ToLongFunction45import java.util.function.ToDoubleFunction46import java.util.function.ToIntBiFunction47import java.util.function.ToLongBiFunction48import java.util.function.ToDoubleBiFunction49import java.util.function.BiPredicate50import java.util.function.IntPredicate51import java.util.function.LongPredicate52import java.util.function.DoublePredicate53import java.util.function.Supplier54import java.util.function.IntSupplier55import java.util.function.LongSupplier56import java.util.function.DoubleSupplier57import java.util.function.Function58import java.util.function.IntFunction59import java.util.function.LongFunction60import java.util.function.DoubleFunction61import java.util.function.BiFunction62import java.util.function.BiConsumer63import java.util.function.ObjIntConsumer64import java.util.function.ObjLongConsumer65import java.util.function.ObjDoubleConsumer66import java.util.function.BiPredicate67import java.util.function.IntPredicate68import java.util.function.LongPredicate69import java.util.function.DoublePredicate

Full Screen

Full Screen

runInitScriptIfRequired

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.jdbc;2import java.sql.Connection;3import java.sql.DriverManager;4import java.sql.SQLException;5import java.sql.Statement;6import java.util.Properties;7public class ContainerDatabaseDriver extends AbstractDriver {8 static {9 register();10 }11 public static void register() {12 try {13 DriverManager.registerDriver(new ContainerDatabaseDriver());14 } catch (SQLException e) {15 throw new RuntimeException("Failed to register ContainerDatabaseDriver with DriverManager", e);16 }17 }18 public Connection connect(String url, Properties info) throws SQLException {19 if (!acceptsURL(url)) {20 return null;21 }22 String jdbcUrl = url.replaceFirst("

Full Screen

Full Screen

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.

Run Testcontainers-java automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful