Skip to content

Commit 11ca661

Browse files
committed
Add tests
1 parent 5fc6238 commit 11ca661

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
<changelist>999999-SNAPSHOT</changelist>
1919
<gitHubRepo>jenkinsci/database-mysql-plugin</gitHubRepo>
2020
<jenkins.version>2.414.3</jenkins.version>
21+
22+
<!-- Test dependencies version -->
23+
<testcontainer.version>1.19.3</testcontainer.version>
24+
2125
</properties>
2226

2327
<licenses>
@@ -49,6 +53,33 @@
4953
<groupId>org.jenkins-ci.plugins</groupId>
5054
<artifactId>database</artifactId>
5155
</dependency>
56+
<!-- Test dependencies -->
57+
<dependency>
58+
<groupId>org.testcontainers</groupId>
59+
<artifactId>junit-jupiter</artifactId>
60+
<version>${testcontainer.version}</version>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.testcontainers</groupId>
65+
<artifactId>mysql</artifactId>
66+
<version>${testcontainer.version}</version>
67+
<scope>test</scope>
68+
<exclusions>
69+
<exclusion>
70+
<groupId>org.apache.commons</groupId>
71+
<artifactId>commons-compress</artifactId>
72+
</exclusion>
73+
<exclusion>
74+
<groupId>org.jetbrains</groupId>
75+
<artifactId>annotations</artifactId>
76+
</exclusion>
77+
<exclusion>
78+
<groupId>org.slf4j</groupId>
79+
<artifactId>slf4j-api</artifactId>
80+
</exclusion>
81+
</exclusions>
82+
</dependency>
5283
</dependencies>
5384

5485
<repositories>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.jenkinsci.plugins.database.mysql;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.instanceOf;
5+
import static org.hamcrest.Matchers.is;
6+
7+
import hudson.util.Secret;
8+
import java.io.IOException;
9+
import org.jenkinsci.plugins.database.GlobalDatabaseConfiguration;
10+
import org.jenkinsci.plugins.database.mysql.MySQLDatabase;
11+
import org.junit.jupiter.api.Test;
12+
import org.jvnet.hudson.test.JenkinsRule;
13+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
14+
import org.testcontainers.containers.MySQLContainer;
15+
import org.testcontainers.junit.jupiter.Container;
16+
import org.testcontainers.junit.jupiter.Testcontainers;
17+
18+
@WithJenkins
19+
@Testcontainers
20+
public class MySQLDatabaseTest {
21+
22+
public static final String TEST_IMAGE = "mysql:8.2.0";
23+
24+
@Container
25+
private static final MySQLContainer<?> mysql = new MySQLContainer<>(TEST_IMAGE);
26+
27+
public void setConfiguration() throws IOException {
28+
MySQLDatabase database = new MySQLDatabase(
29+
mysql.getHost() + ":" + mysql.getMappedPort(3306),
30+
mysql.getDatabaseName(),
31+
mysql.getUsername(),
32+
Secret.fromString(mysql.getPassword()),
33+
null);
34+
database.setValidationQuery("SELECT 1");
35+
GlobalDatabaseConfiguration.get().setDatabase(database);
36+
}
37+
38+
@Test
39+
public void shouldSetConfiguration(JenkinsRule j) throws IOException {
40+
setConfiguration();
41+
assertThat(GlobalDatabaseConfiguration.get().getDatabase(), instanceOf(MySQLDatabase.class));
42+
}
43+
44+
@Test
45+
public void shouldConstructDatabase(JenkinsRule j) throws IOException {
46+
MySQLDatabase database = new MySQLDatabase(
47+
mysql.getHost() + ":" + mysql.getMappedPort(3306),
48+
mysql.getDatabaseName(),
49+
mysql.getUsername(),
50+
Secret.fromString(mysql.getPassword()),
51+
null);
52+
assertThat(database.getDescriptor().getDisplayName(), is("MySQL"));
53+
assertThat(
54+
database.getJdbcUrl(),
55+
is("jdbc:mysql://" + mysql.getHost() + ":" + mysql.getMappedPort(3306) + "/"
56+
+ mysql.getDatabaseName() + ""));
57+
assertThat(database.getDriverClass(), is(com.mysql.cj.jdbc.Driver.class));
58+
}
59+
}

0 commit comments

Comments
 (0)