Skip to content

Commit c06c7e5

Browse files
committed
Document usage
1 parent 7e2789a commit c06c7e5

3 files changed

Lines changed: 139 additions & 4 deletions

File tree

README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,110 @@ testCompile "org.firebirdsql:firebird-testcontainers-java:1.0.0"
3838
</dependency>
3939
```
4040

41+
Usage
42+
-----
43+
44+
For extensive documentation, consult https://www.testcontainers.org/modules/databases/
45+
46+
### JUnit rule
47+
48+
Using a JUnit `@Rule` or `@ClassRule` you can configure a container and start it
49+
per test (`@Rule`) or per class (`@ClassRule`).
50+
51+
The container defines several `withXXX` methods for configuration.
52+
53+
Important standard options are:
54+
55+
- `withUsername(String)` - Sets the username to create (defaults to `test`); if
56+
other than `sysdba` (case insensitive), sets docker environment variable
57+
`FIREBIRD_USER`
58+
- `withPassword(String)` - Sets the password of the user (defaults to `test`);
59+
if `withUsername` is other than `sysdba` (case insensitive), sets the docker
60+
environment variable `FIREBIRD_PASSWORD`, otherwise `ISC_PASSWORD`
61+
- `withDatabaseName(String)` - Sets the database name (defaults to `test`);
62+
sets docker environment variable `FIREBIRD_DATABASE`
63+
64+
Firebird specific options are:
65+
66+
- `withEnableLegacyClientAuth()` - (_Firebird 3+_) Enables `LegacyAuth` and uses
67+
it as the default for creating users, also relaxes `WireCrypt` to `Enabled`;
68+
sets docker environment variable `EnableLegacyClientAuth` to `true`
69+
- `withEnableWireCrypt` - (_Firebird 3+_) Relaxes `WireCrypt` from `Required` to
70+
`Enabled`; sets docker environment variable `EnableWireCrypt` to `true
71+
- `withTimeZone(String)` - Sets the time zone (defaults to JVM default time
72+
zone); sets docker environment variable `TZ` to the specified value
73+
- `withSysdbaPassword(String)` - Sets the SYSDBA password, but if
74+
`withUsername(String)` is set to `sysdba` (case insensitive), this property is
75+
ignored and the value of `withPassword` is used instead; sets docker
76+
environment variable `ISC_PASSWORD` to the specified value
77+
78+
Example of use:
79+
80+
```java
81+
/**
82+
* Simple test demonstrating use of {@code @Rule}.
83+
*/
84+
public class ExampleRuleTest {
85+
86+
@Rule
87+
public final FirebirdContainer container = new FirebirdContainer()
88+
.withUsername("testuser")
89+
.withPassword("testpassword");
90+
91+
@Test
92+
public void canConnectToContainer() throws Exception {
93+
try (Connection connection = DriverManager
94+
.getConnection(container.getJdbcUrl(), container.getUsername(), container.getPassword());
95+
Statement stmt = connection.createStatement();
96+
ResultSet rs = stmt.executeQuery("select CURRENT_USER from RDB$DATABASE")) {
97+
assertTrue("has row", rs.next());
98+
assertEquals("user name", "TESTUSER", rs.getString(1));
99+
}
100+
}
101+
}
102+
```
103+
104+
### Testcontainers URL
105+
106+
The testcontainers URL defines the container and connects to it. As long as there
107+
are active connections, the container will stay up.
108+
109+
For Firebird the URL format is:
110+
111+
- `jdbc:tc:firebird[:<image-tag>]://hostname/<databasename>[?<property>=<value>[&<property>=<value>...]]`
112+
- `jdbc:tc:firebirdsql[:<image-tag>]://hostname/<databasename>[?<property>=<value>[&<property>=<value>...]]`
113+
114+
Where:
115+
116+
- `<image-tag>` (_optional_) is the tag of the docker image to use
117+
- `<databasename>` (_optional_) is the name of the database (defaults to `test`)
118+
- `<property>` is a connection property (Jaybird properties **and** testcontainers properties are possible) \
119+
Of special note are the properties:
120+
- `user` (_optional_) specifies the username to create and connect (defaults to `test`)
121+
- `password` (_optional_) specifies the password for the user (defaults to `test`)
122+
- `<value>` is the value of the property
123+
124+
Example of use:
125+
126+
```java
127+
/**
128+
* Simple test demonstrating use of url to instantiate container.
129+
*/
130+
public class ExampleUrlTest {
131+
132+
@Test
133+
public void canConnectUsingUrl() throws Exception {
134+
try (Connection connection = DriverManager
135+
.getConnection("jdbc:tc:firebird://hostname/databasename?user=someuser&password=somepwd");
136+
Statement stmt = connection.createStatement();
137+
ResultSet rs = stmt.executeQuery("select CURRENT_USER from RDB$DATABASE")) {
138+
assertTrue("has row", rs.next());
139+
assertEquals("user name", "SOMEUSER", rs.getString(1));
140+
}
141+
}
142+
}
143+
```
144+
41145
License
42146
-------
43147

src/test/java/org/firebirdsql/testcontainers/ExampleTest.java renamed to src/test/java/org/firebirdsql/testcontainers/examples/ExampleRuleTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package org.firebirdsql.testcontainers;
1+
package org.firebirdsql.testcontainers.examples;
22

3+
import org.firebirdsql.testcontainers.FirebirdContainer;
34
import org.junit.Rule;
45
import org.junit.Test;
56

@@ -14,10 +15,12 @@
1415
/**
1516
* Simple test demonstrating use of {@code @Rule}.
1617
*/
17-
public class ExampleTest {
18+
public class ExampleRuleTest {
1819

1920
@Rule
20-
public final FirebirdContainer container = new FirebirdContainer();
21+
public final FirebirdContainer container = new FirebirdContainer()
22+
.withUsername("testuser")
23+
.withPassword("testpassword");
2124

2225
@Test
2326
public void canConnectToContainer() throws Exception {
@@ -26,7 +29,7 @@ public void canConnectToContainer() throws Exception {
2629
Statement stmt = connection.createStatement();
2730
ResultSet rs = stmt.executeQuery("select CURRENT_USER from RDB$DATABASE")) {
2831
assertTrue("has row", rs.next());
29-
assertEquals("user name", container.getUsername().toUpperCase(), rs.getString(1));
32+
assertEquals("user name", "TESTUSER", rs.getString(1));
3033
}
3134
}
3235
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.firebirdsql.testcontainers.examples;
2+
3+
import org.junit.Test;
4+
5+
import java.sql.Connection;
6+
import java.sql.DriverManager;
7+
import java.sql.ResultSet;
8+
import java.sql.Statement;
9+
10+
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
11+
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;
12+
13+
/**
14+
* Simple test demonstrating use of url to instantiate container.
15+
*/
16+
public class ExampleUrlTest {
17+
18+
@Test
19+
public void canConnectUsingUrl() throws Exception {
20+
try (Connection connection = DriverManager
21+
.getConnection("jdbc:tc:firebird://hostname/databasename?user=someuser&password=somepwd");
22+
Statement stmt = connection.createStatement();
23+
ResultSet rs = stmt.executeQuery("select CURRENT_USER from RDB$DATABASE")) {
24+
assertTrue("has row", rs.next());
25+
assertEquals("user name", "SOMEUSER", rs.getString(1));
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)