6
6
import org .junit .jupiter .api .Test ;
7
7
import org .slf4j .Logger ;
8
8
import org .slf4j .LoggerFactory ;
9
+ import org .testcontainers .containers .MySQLContainer ;
10
+ import org .testcontainers .junit .jupiter .Container ;
11
+ import org .testcontainers .junit .jupiter .Testcontainers ;
9
12
10
13
import java .io .File ;
11
14
import java .nio .file .Files ;
22
25
*
23
26
*/
24
27
@ Tag ("Integration" )
28
+ @ Testcontainers
25
29
class MysqlBackup4JIntegrationTest {
26
30
27
31
private Logger logger = LoggerFactory .getLogger (getClass ());
28
32
private static final String TEST_DB = "mysqlbackup4j_test" ;
29
33
private static final String RESTORED_DB = "mysqlbackup4j_restored" ;
30
34
private static final String DB_USERNAME = "travis" ;
31
- private static final String DB_PASSWORD = "" ;
35
+ private static final String DB_PASSWORD = "test " ;
32
36
private static final String DRIVER_CLASS_NAME = "com.mysql.cj.jdbc.Driver" ;
33
37
38
+ @ Container
39
+ private static final MySQLContainer mySQLContainer = new MySQLContainer <>("mysql:8.0.30" )
40
+ .withDatabaseName (TEST_DB )
41
+ .withUsername (DB_USERNAME )
42
+ .withPassword (DB_PASSWORD )
43
+ .withExposedPorts (3306 )
44
+ .withInitScript ("sample_database.sql" );
45
+
46
+ @ Container
47
+ private static final MySQLContainer mySQLRestoredContainer = new MySQLContainer <>("mysql:8.0.30" )
48
+ .withDatabaseName (RESTORED_DB )
49
+ .withUsername (DB_USERNAME )
50
+ .withPassword (DB_PASSWORD )
51
+ .withExposedPorts (3306 );
52
+
34
53
@ BeforeAll
35
54
static void setUp () {
36
55
System .setProperty ("org.slf4j.simpleLogger.defaultLogLevel" , "debug" );
@@ -50,10 +69,12 @@ void givenDBCredentials_whenExportDatabaseAndImportDatabase_thenBackUpAndRestore
50
69
properties .setProperty (MysqlExportService .JDBC_DRIVER_NAME , DRIVER_CLASS_NAME );
51
70
properties .setProperty (MysqlExportService .ADD_IF_NOT_EXISTS , "true" );
52
71
53
-
54
72
properties .setProperty (MysqlExportService .TEMP_DIR , new File ("external" ).getPath ());
55
73
properties .setProperty (MysqlExportService .SQL_FILE_NAME , "test_output_file_name" );
56
74
75
+ properties .setProperty (MysqlExportService .JDBC_CONNECTION_STRING , mySQLContainer .getJdbcUrl () + "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false" );
76
+
77
+
57
78
MysqlExportService mysqlExportService = new MysqlExportService (properties );
58
79
mysqlExportService .export ();
59
80
@@ -71,7 +92,7 @@ void givenDBCredentials_whenExportDatabaseAndImportDatabase_thenBackUpAndRestore
71
92
String sql = new String (Files .readAllBytes (sqlFile .toPath ()));
72
93
MysqlImportService res = MysqlImportService .builder ()
73
94
.setJdbcDriver ("com.mysql.cj.jdbc.Driver" )
74
- .setDatabase ( RESTORED_DB )
95
+ .setJdbcConnString ( mySQLRestoredContainer . getJdbcUrl () )
75
96
.setSqlString (sql )
76
97
.setUsername (DB_USERNAME )
77
98
.setPassword (DB_PASSWORD )
@@ -92,7 +113,7 @@ void givenJDBCConString_whenExportDatabaseAndImportDatabase_thenBackUpAndRestore
92
113
properties .setProperty (MysqlExportService .DB_USERNAME , DB_USERNAME );
93
114
properties .setProperty (MysqlExportService .DB_PASSWORD , DB_PASSWORD );
94
115
properties .setProperty (MysqlExportService .DB_NAME , TEST_DB );
95
- properties .setProperty (MysqlExportService .JDBC_CONNECTION_STRING , "jdbc:mysql://localhost:3306/" + TEST_DB + "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false" );
116
+ properties .setProperty (MysqlExportService .JDBC_CONNECTION_STRING , mySQLContainer . getJdbcUrl () + "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false" );
96
117
97
118
properties .setProperty (MysqlExportService .PRESERVE_GENERATED_ZIP , "true" );
98
119
properties .setProperty (MysqlExportService .PRESERVE_GENERATED_SQL_FILE , "true" );
@@ -120,7 +141,7 @@ void givenJDBCConString_whenExportDatabaseAndImportDatabase_thenBackUpAndRestore
120
141
String sql = new String (Files .readAllBytes (sqlFile .toPath ()));
121
142
boolean res = MysqlImportService .builder ()
122
143
.setSqlString (sql )
123
- .setJdbcConnString ("jdbc:mysql://localhost:3306/" + RESTORED_DB + "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false" )
144
+ .setJdbcConnString (mySQLRestoredContainer . getJdbcUrl () + "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false" )
124
145
.setUsername (DB_USERNAME )
125
146
.setPassword (DB_PASSWORD )
126
147
.setDatabase (RESTORED_DB )
@@ -135,12 +156,12 @@ void givenJDBCConString_whenExportDatabaseAndImportDatabase_thenBackUpAndRestore
135
156
136
157
137
158
private void assertDatabaseBackedUp () throws Exception {
138
- Connection connection = MysqlBaseService .connect (DB_USERNAME , DB_PASSWORD , RESTORED_DB , DRIVER_CLASS_NAME );
159
+ Connection connection = MysqlBaseService .connectWithURL (DB_USERNAME , DB_PASSWORD , mySQLRestoredContainer . getJdbcUrl () , DRIVER_CLASS_NAME );
139
160
Statement statement = connection .createStatement (ResultSet .TYPE_SCROLL_INSENSITIVE , ResultSet .CONCUR_READ_ONLY );
140
161
statement .execute ("SELECT COUNT(1) as total FROM users" );
141
162
ResultSet resultSet = statement .getResultSet ();
142
163
resultSet .first ();
143
164
assertTrue (resultSet .getLong ("total" ) > 0 );
144
165
}
145
166
146
- }
167
+ }
0 commit comments