Skip to content

Commit b75b218

Browse files
authored
Merge pull request #242 from eduardknezovic/java-testcontainers
Java Testcontainers
2 parents 77b8436 + 95bad89 commit b75b218

File tree

16 files changed

+1221
-0
lines changed

16 files changed

+1221
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ buildNumber.properties
1313
*.csv
1414
presto/resources/etc/node.properties
1515
presto/.docker
16+
17+
!java-testcontainers/gradle/wrapper/*

java-testcontainers/.gitattributes

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# https://help.github.com/articles/dealing-with-line-endings/
3+
#
4+
# Linux start script should use lf
5+
/gradlew text eol=lf
6+
7+
# These are Windows script files and should use crlf
8+
*.bat text eol=crlf
9+
10+
# Binary files should be left untouched
11+
*.jar binary
12+

java-testcontainers/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ignore Gradle project-specific cache directory
2+
.gradle
3+
4+
# Ignore Gradle build output directory
5+
build

java-testcontainers/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
# Testcontainers for Java
3+
4+
## Install Java
5+
6+
```run
7+
apt install openjdk-21-jdk
8+
```
9+
10+
## Verify the installation
11+
12+
```run
13+
java --version
14+
```
15+
16+
## Navigate to the project
17+
18+
```run
19+
cd /root/eduard-scllya-code-samples/scylla-code-samples/java-testcontainers
20+
```
21+
22+
## Run the program
23+
24+
```run
25+
./gradlew build
26+
```
27+
28+
## Expected output
29+
30+
The command above will download the dependencies and run the tests.
31+
32+
After the tests pass, you should see the following output:
33+
34+
```
35+
#...
36+
37+
ScyllaDBExampleTest > testScyllaDBOperations() PASSED
38+
39+
# ...
40+
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* This file was generated by the Gradle 'init' task.
3+
*
4+
* This generated file contains a sample Java application project to get you started.
5+
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.12.1/userguide/building_java_projects.html in the Gradle documentation.
6+
*/
7+
8+
plugins {
9+
// Apply the application plugin to add support for building a CLI application in Java.
10+
id 'application'
11+
}
12+
13+
repositories {
14+
// Use Maven Central for resolving dependencies.
15+
mavenCentral()
16+
}
17+
18+
dependencies {
19+
// Use JUnit Jupiter for testing.
20+
testImplementation libs.junit.jupiter
21+
22+
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
23+
24+
// This dependency is used by the application.
25+
implementation libs.guava
26+
27+
// Add the required dependencies for the test
28+
testImplementation 'org.testcontainers:scylladb:1.20.5'
29+
testImplementation 'com.scylladb:java-driver-core:4.18.1.0'
30+
implementation 'ch.qos.logback:logback-classic:1.4.11'
31+
}
32+
33+
// Apply a specific Java toolchain to ease working on different environments.
34+
java {
35+
toolchain {
36+
languageVersion = JavaLanguageVersion.of(21)
37+
}
38+
}
39+
40+
application {
41+
// Define the main class for the application.
42+
mainClass = 'org.example.App'
43+
}
44+
45+
tasks.named('test') {
46+
// Use JUnit Platform for unit tests.
47+
useJUnitPlatform()
48+
49+
// Add the test logging configuration to get the test results in terminal
50+
testLogging {
51+
events "passed", "skipped", "failed"
52+
showStandardStreams = true
53+
exceptionFormat = 'full'
54+
showCauses = true
55+
}
56+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* This source file was generated by the Gradle 'init' task
3+
*/
4+
package org.example;
5+
6+
public class App {
7+
public String getGreeting() {
8+
return "Hello World!";
9+
}
10+
11+
public static void main(String[] args) {
12+
System.out.println(new App().getGreeting());
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* This source file was generated by the Gradle 'init' task
3+
*/
4+
package org.example;
5+
6+
import org.junit.jupiter.api.Test;
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class AppTest {
10+
@Test void appHasAGreeting() {
11+
App classUnderTest = new App();
12+
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
13+
}
14+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import org.junit.jupiter.api.AfterEach;
2+
import org.junit.jupiter.api.BeforeEach;
3+
import org.junit.jupiter.api.Test;
4+
import org.testcontainers.scylladb.ScyllaDBContainer;
5+
import com.datastax.oss.driver.api.core.CqlSession;
6+
import com.datastax.oss.driver.api.core.cql.ResultSet;
7+
import com.datastax.oss.driver.api.core.cql.Row;
8+
9+
import java.net.InetSocketAddress;
10+
import java.util.UUID;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static org.junit.jupiter.api.Assertions.assertNotNull;
14+
15+
public class ScyllaDBExampleTest {
16+
17+
private ScyllaDBContainer scylladb;
18+
private CqlSession session;
19+
20+
@BeforeEach
21+
public void setUp() {
22+
scylladb = new ScyllaDBContainer("scylladb/scylla:2025.1")
23+
.withExposedPorts(9042, 19042);
24+
scylladb.start();
25+
26+
session = CqlSession.builder()
27+
.addContactPoint(new InetSocketAddress(scylladb.getHost(), scylladb.getMappedPort(9042)))
28+
.withLocalDatacenter("datacenter1")
29+
.build();
30+
31+
session.execute("CREATE KEYSPACE IF NOT EXISTS test_keyspace WITH replication = "
32+
+ "{'class': 'NetworkTopologyStrategy', 'datacenter1': 1}");
33+
session.execute("USE test_keyspace");
34+
session.execute("CREATE TABLE IF NOT EXISTS users (id UUID PRIMARY KEY, name text, age int)");
35+
}
36+
37+
@AfterEach
38+
public void tearDown() {
39+
if (session != null) {
40+
session.close();
41+
}
42+
if (scylladb != null) {
43+
scylladb.stop();
44+
}
45+
}
46+
47+
@Test
48+
public void testScyllaDBOperations() {
49+
// Insert sample data
50+
UUID user1Id = UUID.randomUUID();
51+
UUID user2Id = UUID.randomUUID();
52+
53+
session.execute("INSERT INTO users (id, name, age) VALUES (?, ?, ?)", user1Id, "John Doe", 30);
54+
session.execute("INSERT INTO users (id, name, age) VALUES (?, ?, ?)", user2Id, "Jane Doe", 27);
55+
56+
// Retrieve and verify the inserted data
57+
ResultSet results = session.execute("SELECT * FROM users");
58+
int count = 0;
59+
for (Row row : results) {
60+
assertNotNull(row.getString("name"));
61+
assertNotNull(row.getInt("age"));
62+
count++;
63+
}
64+
65+
assertEquals(2, count); // Ensure two users are present
66+
}
67+
}

0 commit comments

Comments
 (0)