Skip to content

Commit

Permalink
Testcontainers for Java: Add showcase examples for different scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Apr 6, 2023
1 parent 6c497a8 commit b3224c9
Show file tree
Hide file tree
Showing 18 changed files with 938 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ updates:
package-ecosystem: "bundler"
schedule:
interval: "monthly"

- directory: "/testing/testcontainers/java"
package-ecosystem: "gradle"
schedule:
interval: "monthly"
47 changes: 47 additions & 0 deletions .github/workflows/testcontainers-java.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Testcontainers for Java
on:

pull_request:
branches: [ main ]
paths:
- '.github/workflows/testcontainers-java.yml'
- 'testing/testcontainers/java/**'
push:
branches: [ main ]
paths:
- '.github/workflows/testcontainers-java.yml'
- 'testing/testcontainers/java/**'

# Allow job to be triggered manually.
workflow_dispatch:

# Cancel in-progress jobs when pushing to the same branch.
concurrency:
cancel-in-progress: true
group: ${{ github.workflow }}-${{ github.ref }}

jobs:
test:
name: "CrateDB: ${{ matrix.cratedb-version }}
on ${{ matrix.os }}"
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ "ubuntu-latest" ]

steps:

- name: Acquire sources
uses: actions/checkout@v3

- name: Setup Java
uses: actions/setup-java@v3
with:
distribution: "temurin"
java-version: "17"
cache: "gradle"

- name: Run software tests
run: |
cd testing/testcontainers/java
./gradlew check
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ Layout
CrateDB is part of a larger software stack. Those resources may also be used
within "reference architecture" types of documentation.

- The ``testing`` folder contains reference implementations about how to use
different kinds of test layers for testing your applications with CrateDB.


.. _CrateDB: https://github.com/crate/crate
8 changes: 8 additions & 0 deletions testing/testcontainers/java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.gradle/
.idea
*.iws
*.iml
*.ipr
build/
out/
target/
1 change: 1 addition & 0 deletions testing/testcontainers/java/.java-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
17
62 changes: 62 additions & 0 deletions testing/testcontainers/java/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#######################
Testcontainers for Java
#######################

*How to run integration tests of Java applications with CrateDB.*


*****
About
*****

Introduction
============

`Testcontainers for Java`_ is a Java library that supports JUnit tests,
providing lightweight, throwaway instances of common databases suitable
for integration testing scenarios.

The `Testcontainers CrateDB Module`_ will provide your application test
framework with a single-node CrateDB instance. You will be able to choose
the `CrateDB OCI image`_ variant by version, or by selecting the ``nightly``
release.

What's inside
=============

This directory includes different canonical examples how to use those
components within test harnesses of custom applications. Currently,
all test cases are based on JUnit 4.


*****
Usage
*****

1. Make sure Java 17 is installed.
2. Run CrateDB::

docker run -it --rm --publish=4200:4200 --publish=5432:5432 \
crate:latest -Cdiscovery.type=single-node

3. Invoke example application::

./gradlew run --args="jdbc:crate://localhost:5432/"
./gradlew run --args="jdbc:postgresql://localhost:5432/"

3. Invoke software tests::

# Run all tests.
./gradlew test

# Run individual tests.
./gradlew test --tests TestFunctionScope

# Run test case showing how to select CrateDB version per environment variable.
export CRATEDB_VERSION=nightly
./gradlew test --tests TestSharedSingletonMatrix


.. _CrateDB OCI image: https://hub.docker.com/_/crate
.. _Testcontainers for Java: https://github.com/testcontainers/testcontainers-java
.. _Testcontainers CrateDB Module: https://www.testcontainers.org/modules/databases/cratedb/
70 changes: 70 additions & 0 deletions testing/testcontainers/java/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* An example application for demonstrating "Testcontainers for Java" with CrateDB and the PostgreSQL JDBC driver.
*/

buildscript {
repositories {
mavenCentral()
}
}

plugins {
id 'application'
id 'com.adarshr.test-logger' version '3.2.0'
id 'idea'
id 'java'
}

repositories {
mavenCentral()
mavenLocal()
}

dependencies {
implementation 'org.postgresql:postgresql:42.6.0'
implementation 'io.crate:crate-jdbc:2.6.0'
implementation 'org.slf4j:slf4j-api:2.0.7'
implementation 'org.slf4j:slf4j-simple:2.0.7'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.testcontainers:testcontainers:1.18.0'
testImplementation 'org.testcontainers:cratedb:1.18.0'
testImplementation 'org.testcontainers:postgresql:1.18.0'
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

jar {
archiveBaseName = 'cratedb-example-testcontainers-java'
archiveVersion = '0.0.1-SNAPSHOT'
}

sourceSets {
main {
java.srcDirs += [
"src/generated/java",
"src/main/java",
]
}
}

test {
dependsOn 'cleanTest'
}

apply plugin: "java"
apply plugin: "application"

application {
mainClass = 'io.crate.example.testing.Application'
}

ext.javaMainClass = "io.crate.example.testing.Application"


idea.module.inheritOutputDirs = true
processResources.destinationDir = compileJava.destinationDir
compileJava.dependsOn processResources
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit b3224c9

Please sign in to comment.