11package cloud .testcontainers .example ;
22
3- import java .sql .Connection ;
4- import java .sql .DriverManager ;
5- import java .sql .PreparedStatement ;
6- import java .sql .ResultSet ;
7- import java .sql .SQLException ;
8- import java .util .Arrays ;
9-
103import com .github .dockerjava .api .DockerClient ;
114import com .github .dockerjava .api .model .Info ;
125import org .junit .jupiter .api .Test ;
136import org .junit .jupiter .api .extension .ExtendWith ;
147import org .testcontainers .DockerClientFactory ;
158import org .testcontainers .containers .PostgreSQLContainer ;
169import org .testcontainers .images .builder .Transferable ;
10+ import org .testcontainers .shaded .com .google .common .collect .Streams ;
11+
12+ import java .sql .Connection ;
13+ import java .sql .DriverManager ;
14+ import java .sql .PreparedStatement ;
15+ import java .sql .ResultSet ;
16+ import java .sql .SQLException ;
17+ import java .util .Arrays ;
18+ import java .util .List ;
19+ import java .util .stream .Collectors ;
20+ import java .util .stream .Stream ;
1721
1822import static org .assertj .core .api .Assertions .assertThat ;
1923
2024@ ExtendWith (TccTestWatcher .class )
2125public class TestcontainersCloudFirstTest {
2226
27+ public static final String DOCKER_CLOUD_VERSION_LABEL = "com.docker.cloud.version" ;
28+
29+ public static final String TESTCONTAINERS_DESKTOP_APP_NAME = "Testcontainers Desktop" ;
30+
31+ public static final String TESTCONTAINERS_CLOUD_VERSION_NAME = "testcontainerscloud" ;
32+
2333 @ Test
2434 public void createPostgreSQLContainer () throws SQLException {
2535 try (PostgreSQLContainer <?> postgreSQLContainer = new PostgreSQLContainer <>("postgres:14-alpine" )
2636 .withCopyToContainer (Transferable .of (initsql ), "/docker-entrypoint-initdb.d/init.sql" )) {
2737 postgreSQLContainer .start ();
28- Connection connection = DriverManager .getConnection (postgreSQLContainer .getJdbcUrl (), postgreSQLContainer .getUsername (), postgreSQLContainer .getPassword ());
29- PreparedStatement preparedStatement = connection .prepareStatement ("SELECT COUNT(*) FROM guides" );
30- preparedStatement .execute ();
31- ResultSet resultSet = preparedStatement .getResultSet ();
32- resultSet .next ();
33- assertThat (resultSet .getInt (1 )).isEqualTo (6 );
34- }
38+ Connection connection = DriverManager .getConnection (postgreSQLContainer .getJdbcUrl (), postgreSQLContainer .getUsername (), postgreSQLContainer .getPassword ());
39+ PreparedStatement preparedStatement = connection .prepareStatement ("SELECT COUNT(*) FROM guides" );
40+ preparedStatement .execute ();
41+ ResultSet resultSet = preparedStatement .getResultSet ();
42+ resultSet .next ();
43+ assertThat (resultSet .getInt (1 )).isEqualTo (6 );
44+ }
3545 }
3646
3747 @ Test
@@ -42,43 +52,48 @@ public void testcontainersCloudDockerEngine() {
4252 String serverVersion = dockerInfo .getServerVersion ();
4353 String [] labels = dockerInfo .getLabels ();
4454
45- boolean isCloudServer = labels != null && Arrays .asList (labels ).contains ("docker/cloud" );
55+ List <String > info = Streams .concat (
56+ Stream .of (String .format ("server.version=%s" , serverVersion )),
57+ Arrays .stream (labels == null ? new String []{} : labels )
58+ ).collect (Collectors .toList ());
4659
47- if (!isCloudServer ) {
48- assertThat (serverVersion )
60+ assertThat (info )
4961 .as ("Docker Client is configured via the Testcontainers desktop app" )
50- .satisfiesAnyOf (
51- dockerString -> assertThat ( dockerString ). contains ( "Testcontainers Desktop" ) ,
52- dockerString -> assertThat ( dockerString ). contains ( "testcontainerscloud" )
53- );
54- }
62+ .anySatisfy ( it -> assertThat ( it ). containsAnyOf (
63+ TESTCONTAINERS_DESKTOP_APP_NAME ,
64+ TESTCONTAINERS_CLOUD_VERSION_NAME ,
65+ DOCKER_CLOUD_VERSION_LABEL
66+ ));
5567
68+ logRuntimeeDetails (serverVersion , dockerInfo );
69+ }
5670
71+ private static void logRuntimeeDetails (String serverVersion , Info dockerInfo ) {
5772 String runtimeName = "Testcontainers Cloud" ;
58- if (!serverVersion .contains ("testcontainerscloud" )) {
73+ if (!serverVersion .contains (TESTCONTAINERS_CLOUD_VERSION_NAME )) {
5974 runtimeName = dockerInfo .getOperatingSystem ();
6075 }
61- if (serverVersion .contains ("Testcontainers Desktop" )) {
76+ if (serverVersion .contains (TESTCONTAINERS_DESKTOP_APP_NAME )) {
6277 runtimeName += " via Testcontainers Desktop app" ;
6378 }
6479 System .out .println (PrettyStrings .getLogo (runtimeName ));
6580 }
6681
6782 private static final String initsql =
6883 "create table guides\n " +
69- "(\n " +
70- " id bigserial not null,\n " +
71- " title varchar(1023) not null,\n " +
72- " url varchar(1023) not null,\n " +
73- " primary key (id)\n " +
74- ");\n " +
75- "\n " +
76- "insert into guides(title, url)\n " +
77- "values ('Getting started with Testcontainers', 'https://testcontainers.com/getting-started/'),\n " +
78- " ('Getting started with Testcontainers for Java', 'https://testcontainers.com/guides/getting-started-with-testcontainers-for-java/'),\n " +
79- " ('Getting started with Testcontainers for .NET', 'https://testcontainers.com/guides/getting-started-with-testcontainers-for-dotnet/'),\n " +
80- " ('Getting started with Testcontainers for Node.js', 'https://testcontainers.com/guides/getting-started-with-testcontainers-for-nodejs/'),\n " +
81- " ('Getting started with Testcontainers for Go', 'https://testcontainers.com/guides/getting-started-with-testcontainers-for-go/'),\n " +
82- " ('Testcontainers container lifecycle management using JUnit 5', 'https://testcontainers.com/guides/testcontainers-container-lifecycle/')\n " +
83- ";" ;
84+ "(\n " +
85+ " id bigserial not null,\n " +
86+ " title varchar(1023) not null,\n " +
87+ " url varchar(1023) not null,\n " +
88+ " primary key (id)\n " +
89+ ");\n " +
90+ "\n " +
91+ "insert into guides(title, url)\n " +
92+ "values ('Getting started with Testcontainers', 'https://testcontainers.com/getting-started/'),\n " +
93+ " ('Getting started with Testcontainers for Java', 'https://testcontainers.com/guides/getting-started-with-testcontainers-for-java/'),\n " +
94+ " ('Getting started with Testcontainers for .NET', 'https://testcontainers.com/guides/getting-started-with-testcontainers-for-dotnet/'),\n " +
95+ " ('Getting started with Testcontainers for Node.js', 'https://testcontainers.com/guides/getting-started-with-testcontainers-for-nodejs/'),\n " +
96+ " ('Getting started with Testcontainers for Go', 'https://testcontainers.com/guides/getting-started-with-testcontainers-for-go/'),\n " +
97+ " ('Testcontainers container lifecycle management using JUnit 5', 'https://testcontainers.com/guides/testcontainers-container-lifecycle/')\n " +
98+ ";" ;
8499}
0 commit comments