1
1
/*
2
- * Copyright 2002-2010 the original author or authors.
2
+ * Copyright 2002-2012 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
import static org .junit .Assert .assertEquals ;
20
20
21
21
import java .sql .Connection ;
22
+ import java .sql .SQLException ;
23
+
24
+ import org .easymock .EasyMock ;
22
25
23
26
import org .junit .After ;
24
27
import org .junit .Test ;
28
+
25
29
import org .springframework .core .io .ClassRelativeResourceLoader ;
26
30
import org .springframework .jdbc .core .JdbcTemplate ;
31
+ import org .springframework .jdbc .datasource .DataSourceUtils ;
27
32
import org .springframework .jdbc .datasource .embedded .EmbeddedDatabase ;
28
33
import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseBuilder ;
34
+ import org .springframework .transaction .support .TransactionSynchronizationManager ;
29
35
30
36
/**
31
37
* @author Dave Syer
32
38
* @author Sam Brannen
39
+ * @author Oliver Gierke
33
40
*/
34
41
public class DatabasePopulatorTests {
35
42
@@ -54,6 +61,12 @@ private void assertUsersDatabaseCreated() {
54
61
55
62
@ After
56
63
public void shutDown () {
64
+
65
+ if (TransactionSynchronizationManager .isSynchronizationActive ()) {
66
+ TransactionSynchronizationManager .clear ();
67
+ TransactionSynchronizationManager .unbindResource (db );
68
+ }
69
+
57
70
db .shutdown ();
58
71
}
59
72
@@ -207,4 +220,22 @@ public void testBuildWithSelectStatements() throws Exception {
207
220
assertEquals (1 , jdbcTemplate .queryForInt ("select COUNT(NAME) from T_TEST where NAME='Dave'" ));
208
221
}
209
222
223
+ /**
224
+ * @see SPR-9457
225
+ */
226
+ @ Test
227
+ public void usesBoundConnectionIfAvailable () throws SQLException {
228
+
229
+ TransactionSynchronizationManager .initSynchronization ();
230
+ Connection connection = DataSourceUtils .getConnection (db );
231
+
232
+ DatabasePopulator populator = EasyMock .createMock (DatabasePopulator .class );
233
+ populator .populate (connection );
234
+ EasyMock .expectLastCall ();
235
+ EasyMock .replay (populator );
236
+
237
+ DatabasePopulatorUtils .execute (populator , db );
238
+
239
+ EasyMock .verify (populator );
240
+ }
210
241
}
0 commit comments