19
19
import java .io .IOException ;
20
20
import java .io .UncheckedIOException ;
21
21
import java .sql .SQLException ;
22
+ import java .text .DateFormat ;
23
+ import java .text .SimpleDateFormat ;
22
24
import java .time .Duration ;
23
25
import java .util .ArrayList ;
24
26
import java .util .HashMap ;
25
27
import java .util .List ;
26
28
import java .util .Map ;
29
+ import java .util .concurrent .ConcurrentHashMap ;
30
+ import java .util .concurrent .atomic .AtomicInteger ;
27
31
import javax .sql .DataSource ;
28
32
import org .jooq .DSLContext ;
29
33
import org .jooq .SQLDialect ;
@@ -51,12 +55,30 @@ abstract public class TestDatabase<C extends JdbcDatabaseContainer<?>, T extends
51
55
final private ArrayList <String > cleanupSQL = new ArrayList <>();
52
56
final private Map <String , String > connectionProperties = new HashMap <>();
53
57
54
- private DataSource dataSource ;
55
- private DSLContext dslContext ;
58
+ private volatile DataSource dataSource ;
59
+ private volatile DSLContext dslContext ;
56
60
61
+ protected final int databaseId ;
62
+ private static final AtomicInteger nextDatabaseId = new AtomicInteger (0 );
63
+
64
+ protected final int containerId ;
65
+ private static final AtomicInteger nextContainerId = new AtomicInteger (0 );
66
+ private static final Map <String , Integer > containerUidToId = new ConcurrentHashMap <>();
67
+
68
+ @ SuppressWarnings ("this-escape" )
57
69
protected TestDatabase (C container ) {
58
70
this .container = container ;
59
71
this .suffix = Strings .addRandomSuffix ("" , "_" , 10 );
72
+ this .databaseId = nextDatabaseId .getAndIncrement ();
73
+ this .containerId = containerUidToId .computeIfAbsent (container .getContainerId (), k -> nextContainerId .getAndIncrement ());
74
+ LOGGER .info (formatLogLine ("creating database " + getDatabaseName ()));
75
+ }
76
+
77
+ private final DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS" );
78
+
79
+ protected String formatLogLine (String logLine ) {
80
+ String retVal = "SGX TestDatabase databaseId=" + databaseId + ", containerId=" + containerId + " - " + logLine ;
81
+ return retVal ;
60
82
}
61
83
62
84
@ SuppressWarnings ("unchecked" )
@@ -170,7 +192,14 @@ public Database getDatabase() {
170
192
protected void execSQL (final List <String > sqls ) {
171
193
try {
172
194
for (String sql : sqls ) {
173
- getDslContext ().execute (sql );
195
+ LOGGER .info (formatLogLine ("executing SQL: " + sql ));
196
+ try {
197
+ getDslContext ().execute (sql );
198
+ LOGGER .info (formatLogLine ("completed SQL: " + sql ));
199
+ } catch (Throwable t ) {
200
+ LOGGER .error (formatLogLine ("error when executing SQL: " + sql + "e=\n " + t ));
201
+ throw t ;
202
+ }
174
203
}
175
204
} catch (DataAccessException e ) {
176
205
throw new RuntimeException (e );
@@ -182,12 +211,13 @@ protected void execInContainer(List<String> cmd) {
182
211
return ;
183
212
}
184
213
try {
185
- LOGGER .debug ( "executing {} " , Strings .join (cmd , " " ));
214
+ LOGGER .info ( formatLogLine ( String . format ( "executing command %s " , Strings .join (cmd , " " )) ));
186
215
final var exec = getContainer ().execInContainer (cmd .toArray (new String [0 ]));
187
216
if (exec .getExitCode () == 0 ) {
188
- LOGGER .debug ( "execution success\n stdout:\n {} \n stderr:\n {} " , exec .getStdout (), exec .getStderr ());
217
+ LOGGER .info ( formatLogLine ( String . format ( "execution success\n stdout:\n %s \n stderr:\n %s " , exec .getStdout (), exec .getStderr ()) ));
189
218
} else {
190
- LOGGER .error ("execution failure, code {}\n stdout:\n {}\n stderr:\n {}" , exec .getExitCode (), exec .getStdout (), exec .getStderr ());
219
+ LOGGER .error (formatLogLine (
220
+ String .format ("execution failure, code %s\n stdout:\n %s\n stderr:\n %s" , exec .getExitCode (), exec .getStdout (), exec .getStderr ())));
191
221
}
192
222
} catch (IOException e ) {
193
223
throw new UncheckedIOException (e );
@@ -229,6 +259,7 @@ public B integrationTestConfigBuilder() {
229
259
public void close () {
230
260
execSQL (this .cleanupSQL );
231
261
execInContainer (inContainerUndoBootstrapCmd ());
262
+ LOGGER .info ("closing database databaseId=" + databaseId );
232
263
}
233
264
234
265
static public class ConfigBuilder <T extends TestDatabase <?, ?, ?>, B extends ConfigBuilder <T , B >> {
0 commit comments