forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MSSQLServerContainer.java
116 lines (92 loc) · 3.57 KB
/
MSSQLServerContainer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package org.testcontainers.containers;
import org.testcontainers.utility.LicenseAcceptance;
import java.util.regex.Pattern;
import java.util.stream.Stream;
/**
* @author Stefan Hufschmidt
*/
public class MSSQLServerContainer<SELF extends MSSQLServerContainer<SELF>> extends JdbcDatabaseContainer<SELF> {
public static final String NAME = "sqlserver";
public static final String IMAGE = "mcr.microsoft.com/mssql/server";
public static final String DEFAULT_TAG = "2017-CU12";
public static final Integer MS_SQL_SERVER_PORT = 1433;
private String username = "SA";
private String password = "A_Str0ng_Required_Password";
private static final int DEFAULT_STARTUP_TIMEOUT_SECONDS = 240;
private static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 240;
private static final Pattern[] PASSWORD_CATEGORY_VALIDATION_PATTERNS = new Pattern[]{
Pattern.compile("[A-Z]+"),
Pattern.compile("[a-z]+"),
Pattern.compile("[0-9]+"),
Pattern.compile("[^a-zA-Z0-9]+", Pattern.CASE_INSENSITIVE)
};
public MSSQLServerContainer() {
this(IMAGE + ":" + DEFAULT_TAG);
}
public MSSQLServerContainer(final String dockerImageName) {
super(dockerImageName);
withStartupTimeoutSeconds(DEFAULT_STARTUP_TIMEOUT_SECONDS);
withConnectTimeoutSeconds(DEFAULT_CONNECT_TIMEOUT_SECONDS);
addExposedPort(MS_SQL_SERVER_PORT);
}
@Override
protected Integer getLivenessCheckPort() {
return getMappedPort(MS_SQL_SERVER_PORT);
}
@Override
protected void configure() {
LicenseAcceptance.assertLicenseAccepted(this.getDockerImageName());
addEnv("ACCEPT_EULA", "Y");
addEnv("SA_PASSWORD", password);
}
@Override
public String getDriverClassName() {
return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
}
@Override
public String getJdbcUrl() {
return "jdbc:sqlserver://" + getContainerIpAddress() + ":" + getMappedPort(MS_SQL_SERVER_PORT);
}
@Override
public String getUsername() {
return username;
}
@Override
public String getPassword() {
return password;
}
@Override
public String getTestQueryString() {
return "SELECT 1";
}
@Override
public SELF withPassword(final String password) {
checkPasswordStrength(password);
this.password = password;
return self();
}
private void checkPasswordStrength(String password) {
if (password == null) {
throw new IllegalArgumentException("Null password is not allowed");
}
if (password.length() < 8) {
throw new IllegalArgumentException("Password should be at least 8 characters long");
}
if (password.length() > 128) {
throw new IllegalArgumentException("Password can be up to 128 characters long");
}
long satisfiedCategories = Stream.of(PASSWORD_CATEGORY_VALIDATION_PATTERNS)
.filter(p -> p.matcher(password).find())
.count();
if (satisfiedCategories < 3) {
throw new IllegalArgumentException(
"Password must contain characters from three of the following four categories:\n" +
" - Latin uppercase letters (A through Z)\n" +
" - Latin lowercase letters (a through z)\n" +
" - Base 10 digits (0 through 9)\n" +
" - Non-alphanumeric characters such as: exclamation point (!), dollar sign ($), number sign (#), " +
"or percent (%)."
);
}
}
}