From 8977f2929ee37844da379cb48f71ed213507ab75 Mon Sep 17 00:00:00 2001 From: robfrank Date: Thu, 19 Jul 2018 16:19:27 +0200 Subject: [PATCH] adds basic examples --- .gitignore | 169 ++++++++++++++++ pom.xml | 188 ++++++++++++++++++ .../JavaCustomContainerTest.java | 97 +++++++++ .../testcontainers/JavaJdbcUrlTest.java | 40 ++++ .../JavaJdbcUrlWithScriptTest.java | 32 +++ .../JavaSimpleQueryTestTest.java | 34 ++++ .../testcontainers/KotlinSimpleQueryTest.kt | 33 +++ src/test/resources/initdb.sql | 8 + 8 files changed, 601 insertions(+) create mode 100644 pom.xml create mode 100644 src/test/java/io/github/robfrank/testcontainers/JavaCustomContainerTest.java create mode 100644 src/test/java/io/github/robfrank/testcontainers/JavaJdbcUrlTest.java create mode 100644 src/test/java/io/github/robfrank/testcontainers/JavaJdbcUrlWithScriptTest.java create mode 100644 src/test/java/io/github/robfrank/testcontainers/JavaSimpleQueryTestTest.java create mode 100644 src/test/kotlin/io/github/robfrank/testcontainers/KotlinSimpleQueryTest.kt create mode 100644 src/test/resources/initdb.sql diff --git a/.gitignore b/.gitignore index 5f2dbe1..70ac742 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,172 @@ buildNumber.properties # Avoid ignoring Maven wrapper jar file (.jar files are usually ignored) !/.mvn/wrapper/maven-wrapper.jar +### Eclipse template + +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# CDT- autotools +.autotools + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + +# Scala IDE specific (Scala & Java development for Eclipse) +.cache-main +.scala_dependencies +.worksheet +### Maven template +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties + +# Avoid ignoring Maven wrapper jar file (.jar files are usually ignored) +!/.mvn/wrapper/maven-wrapper.jar +### Kotlin template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +### Java template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/dictionaries +.idea/**/shelf + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ +cmake-build-release/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..728559e --- /dev/null +++ b/pom.xml @@ -0,0 +1,188 @@ + + + + + 4.0.0 + + io.github.robfrank + testcontainers-examples + jar + 1.0-SNAPSHOT + + + 1.8 + 1.8 + UTF-8 + 1.2.51 + + + + + + kotlin-maven-plugin + org.jetbrains.kotlin + ${kotlin.version} + + + compile + + compile + + + + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/main/java + + + + + test-compile + + test-compile + + + + ${project.basedir}/src/test/kotlin + ${project.basedir}/src/test/java + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + + + default-compile + none + + + + default-testCompile + none + + + java-compile + compile + + compile + + + + java-test-compile + test-compile + + testCompile + + + + + + + + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-reflect + ${kotlin.version} + + + org.apache.commons + commons-csv + [1,) + + + org.apache.commons + commons-lang3 + [3,) + + + org.apache.commons + commons-text + [1,) + + + commons-io + commons-io + [2,) + + + com.google.guava + guava + 25.1-jre + + + + org.postgresql + postgresql + [42,) + + + + com.orientechnologies + orientdb-graphdb + 2.2.35 + + + + org.slf4j + slf4j-simple + 1.7.25 + + + + + + + org.jetbrains.kotlin + kotlin-test-junit + ${kotlin.version} + test + + + + org.assertj + assertj-core + [3,) + test + + + org.assertj + assertj-guava + [3,) + test + + + + org.testcontainers + testcontainers + [1.8,) + test + + + org.testcontainers + postgresql + [1.8,) + test + + + org.testcontainers + mysql + [1.8,) + test + + + + + diff --git a/src/test/java/io/github/robfrank/testcontainers/JavaCustomContainerTest.java b/src/test/java/io/github/robfrank/testcontainers/JavaCustomContainerTest.java new file mode 100644 index 0000000..131d9ef --- /dev/null +++ b/src/test/java/io/github/robfrank/testcontainers/JavaCustomContainerTest.java @@ -0,0 +1,97 @@ +package io.github.robfrank.testcontainers; + +import com.orientechnologies.orient.client.remote.OServerAdmin; +import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx; +import com.orientechnologies.orient.core.sql.OCommandSQL; +import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.output.Slf4jLogConsumer; +import org.testcontainers.containers.wait.strategy.Wait; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; + +public class JavaCustomContainerTest { + + private static final Logger LOGGER = LoggerFactory.getLogger(JavaCustomContainerTest.class); + + @ClassRule + public static GenericContainer container = new GenericContainer("orientdb:2.2.35") + .withExposedPorts(2424, 2480) + .withEnv("ORIENTDB_ROOT_PASSWORD", "rootpassword") + .waitingFor(Wait.forListeningPort()); + + @Rule + public TestName name = new TestName(); + + @Before + public void setUp() throws Exception { + Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(LOGGER); + container.followOutput(logConsumer); + + String serverUrl = "remote:" + container.getContainerIpAddress() + ":" + container.getMappedPort(2424); + + final String database = createDatabase(serverUrl, name.getMethodName()); + populateDatabase(database); + } + + private String createDatabase(String serverUrl, String dbname) throws IOException { + + OServerAdmin serverAdmin = new OServerAdmin(serverUrl); + serverAdmin.connect("root", "rootpassword"); + serverAdmin.createDatabase(dbname, "graph", "plocal"); + serverAdmin.close(); + + return serverUrl + "/" + dbname; + + + } + + + private void populateDatabase(String database) { + + ODatabaseDocumentTx db = new ODatabaseDocumentTx(database); + + db.open("admin", "admin"); + + db.command(new OCommandSQL("create class Person extends V")).execute(); + db.command(new OCommandSQL("create property Person.name string")).execute(); + db.command(new OCommandSQL("create index Person.name on Person(name) unique")).execute(); + + db.command(new OCommandSQL("create class FriendOf extends E")).execute(); + db.command(new OCommandSQL("create property FriendOf.kind string")).execute(); + + db.command(new OCommandSQL("insert into Person set name='rob'")).execute(); + db.command(new OCommandSQL("insert into Person set name='frank'")).execute(); + db.command(new OCommandSQL("insert into Person set name='john'")).execute(); + db.command(new OCommandSQL("insert into Person set name='jane'")).execute(); + + db.command(new OCommandSQL("CREATE EDGE FriendOf FROM (SELECT FROM Person WHERE name = 'rob') TO (SELECT FROM Person WHERE name = 'frank')set kind = 'fraternal' ")).execute(); + db.command(new OCommandSQL("CREATE EDGE FriendOf FROM (SELECT FROM Person WHERE name = 'john') TO (SELECT FROM Person WHERE name = 'jane')set kind = 'fraternal' ")).execute(); + + + db.close(); + + } + + @Test + public void shouldQuery() { + + String dbUrl = "remote:" + container.getContainerIpAddress() + ":" + container.getMappedPort(2424) + "/" + name.getMethodName(); + ODatabaseDocumentTx db = new ODatabaseDocumentTx(dbUrl); + + db.open("admin", "admin"); + + final int person = db.query(new OSQLSynchQuery<>("SELECT FROM Person")).size(); + + assertThat(person).isEqualTo(4); + } +} diff --git a/src/test/java/io/github/robfrank/testcontainers/JavaJdbcUrlTest.java b/src/test/java/io/github/robfrank/testcontainers/JavaJdbcUrlTest.java new file mode 100644 index 0000000..0afbdf2 --- /dev/null +++ b/src/test/java/io/github/robfrank/testcontainers/JavaJdbcUrlTest.java @@ -0,0 +1,40 @@ +package io.github.robfrank.testcontainers; + +import org.junit.Test; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import static org.assertj.core.api.Assertions.assertThat; + +public class JavaJdbcUrlTest { + + public static void sampleInitFunction(Connection connection) throws SQLException { + connection.createStatement().execute("CREATE TABLE bar (\n" + + " foo VARCHAR(255)\n" + + ");"); + connection.createStatement().execute("INSERT INTO bar (foo) VALUES ('hello world');"); + connection.createStatement().execute("CREATE TABLE my_counter (\n" + + " n INT\n" + + ");"); + } + + @Test + public void shouldSelectFromBar() throws SQLException { + String jdbcUrl = "jdbc:tc:postgresql:9.6.8://hostname/databasename?&TC_INITFUNCTION=io.github.robfrank.testcontainers.JavaJdbcUrlTest::sampleInitFunction"; + + Connection conn = DriverManager.getConnection(jdbcUrl); + + Statement stmt = conn.createStatement(); + stmt.execute("SELECT * FROM bar"); + + ResultSet resultSet = stmt.getResultSet(); + resultSet.next(); + + assertThat(resultSet.getString("foo")).isEqualTo("hello world"); + + } +} diff --git a/src/test/java/io/github/robfrank/testcontainers/JavaJdbcUrlWithScriptTest.java b/src/test/java/io/github/robfrank/testcontainers/JavaJdbcUrlWithScriptTest.java new file mode 100644 index 0000000..f91a9c4 --- /dev/null +++ b/src/test/java/io/github/robfrank/testcontainers/JavaJdbcUrlWithScriptTest.java @@ -0,0 +1,32 @@ +package io.github.robfrank.testcontainers; + +import org.junit.Test; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import static org.assertj.core.api.Assertions.assertThat; + +public class JavaJdbcUrlWithScriptTest { + + + @Test + public void shouldSelectFromBar() throws SQLException { + + String jdbcUrl = "jdbc:tc:postgresql:9.6.8://hostname/databasename?&TC_INITSCRIPT=initdb.sql"; + + Connection conn = DriverManager.getConnection(jdbcUrl); + + Statement stmt = conn.createStatement(); + stmt.execute("SELECT * FROM bar"); + + ResultSet resultSet = stmt.getResultSet(); + resultSet.next(); + + assertThat(resultSet.getString("foo")).isEqualTo("hello world"); + + } +} diff --git a/src/test/java/io/github/robfrank/testcontainers/JavaSimpleQueryTestTest.java b/src/test/java/io/github/robfrank/testcontainers/JavaSimpleQueryTestTest.java new file mode 100644 index 0000000..da77d97 --- /dev/null +++ b/src/test/java/io/github/robfrank/testcontainers/JavaSimpleQueryTestTest.java @@ -0,0 +1,34 @@ +package io.github.robfrank.testcontainers; + +import org.junit.ClassRule; +import org.junit.Test; +import org.testcontainers.containers.PostgreSQLContainer; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import static org.assertj.core.api.Assertions.assertThat; + +public class JavaSimpleQueryTestTest { + + @ClassRule + public static PostgreSQLContainer container = + new PostgreSQLContainer("postgres:9.6.9"); + + @Test + public void shouldTestSimpleQuery() throws SQLException { + + System.out.println("container.getJdbcUrl() = " + container.getJdbcUrl()); + Connection conn = DriverManager.getConnection(container.getJdbcUrl(), container.getUsername(), container.getPassword()); + Statement stmt = conn.createStatement(); + stmt.execute("SELECT 1"); + + ResultSet resultSet = stmt.getResultSet(); + resultSet.next(); + + assertThat(resultSet.getInt(1)).isEqualTo(1); + } +} diff --git a/src/test/kotlin/io/github/robfrank/testcontainers/KotlinSimpleQueryTest.kt b/src/test/kotlin/io/github/robfrank/testcontainers/KotlinSimpleQueryTest.kt new file mode 100644 index 0000000..a8ae384 --- /dev/null +++ b/src/test/kotlin/io/github/robfrank/testcontainers/KotlinSimpleQueryTest.kt @@ -0,0 +1,33 @@ +package io.github.robfrank.testcontainers + +import org.assertj.core.api.Assertions +import org.junit.Test +import org.testcontainers.containers.PostgreSQLContainer +import org.testcontainers.containers.wait.strategy.Wait +import java.sql.DriverManager + +class KotlinSimpleQueryTest { + + val container: PostgreSQLContainer + + init { + container = PostgreSQLContainer().apply { + start() + } + } + + @Test + fun `should perform simple query`() { + + val conn = DriverManager.getConnection(container.jdbcUrl, container.username, container.password) + val stmt = conn.createStatement() + stmt.execute("SELECT 1") + + val resultSet = stmt.resultSet + resultSet.next() + + Assertions.assertThat(resultSet.getInt(1)).isEqualTo(1) + } + + +} \ No newline at end of file diff --git a/src/test/resources/initdb.sql b/src/test/resources/initdb.sql new file mode 100644 index 0000000..c5e673a --- /dev/null +++ b/src/test/resources/initdb.sql @@ -0,0 +1,8 @@ +CREATE TABLE bar ( + foo VARCHAR(255) + ); + +INSERT INTO bar (foo) VALUES ('hello world'); +CREATE TABLE my_counter ( + n INT + ); \ No newline at end of file