|
31 | 31 | import org.springframework.core.io.ClassPathResource;
|
32 | 32 | import org.springframework.jdbc.BadSqlGrammarException;
|
33 | 33 | import org.springframework.jdbc.core.JdbcTemplate;
|
| 34 | +import org.springframework.jdbc.datasource.AbstractDriverBasedDataSource; |
34 | 35 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean;
|
35 | 36 | import org.springframework.jdbc.datasource.init.DataSourceInitializer;
|
36 | 37 | import org.springframework.tests.Assume;
|
37 | 38 | import org.springframework.tests.TestGroup;
|
38 | 39 |
|
39 | 40 | import static org.hamcrest.CoreMatchers.*;
|
40 | 41 | import static org.junit.Assert.*;
|
| 42 | +import static org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory.*; |
41 | 43 |
|
42 | 44 | /**
|
43 | 45 | * @author Dave Syer
|
@@ -65,13 +67,24 @@ public void createEmbeddedDatabaseAgain() throws Exception {
|
65 | 67 | }
|
66 | 68 |
|
67 | 69 | @Test
|
68 |
| - public void createWithAnonymousDataSource() throws Exception { |
69 |
| - assertCorrectSetupAndCloseContextForSingleDataSource("jdbc-config-anonymous-datasource.xml", 1); |
| 70 | + public void createWithResourcePattern() throws Exception { |
| 71 | + assertCorrectSetup("jdbc-config-pattern.xml", "dataSource"); |
70 | 72 | }
|
71 | 73 |
|
72 | 74 | @Test
|
73 |
| - public void createWithResourcePattern() throws Exception { |
74 |
| - assertCorrectSetup("jdbc-config-pattern.xml", "dataSource"); |
| 75 | + public void createWithAnonymousDataSourceAndDefaultDatabaseName() throws Exception { |
| 76 | + assertCorrectSetupForSingleDataSource("jdbc-config-db-name-default-and-anonymous-datasource.xml", |
| 77 | + DEFAULT_DATABASE_NAME); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void createWithImplicitDatabaseName() throws Exception { |
| 82 | + assertCorrectSetupForSingleDataSource("jdbc-config-db-name-implicit.xml", "dataSource"); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void createWithExplicitDatabaseName() throws Exception { |
| 87 | + assertCorrectSetupForSingleDataSource("jdbc-config-db-name-explicit.xml", "customDbName"); |
75 | 88 | }
|
76 | 89 |
|
77 | 90 | @Test
|
@@ -165,21 +178,25 @@ private void assertCorrectSetupAndCloseContext(String file, int count, String...
|
165 | 178 | try {
|
166 | 179 | for (String dataSourceName : dataSources) {
|
167 | 180 | DataSource dataSource = context.getBean(dataSourceName, DataSource.class);
|
168 |
| - JdbcTemplate template = new JdbcTemplate(dataSource); |
169 |
| - assertNumRowsInTestTable(template, count); |
| 181 | + assertNumRowsInTestTable(new JdbcTemplate(dataSource), count); |
| 182 | + assertTrue(dataSource instanceof AbstractDriverBasedDataSource); |
| 183 | + AbstractDriverBasedDataSource adbDataSource = (AbstractDriverBasedDataSource) dataSource; |
| 184 | + assertThat(adbDataSource.getUrl(), containsString(dataSourceName)); |
170 | 185 | }
|
171 | 186 | }
|
172 | 187 | finally {
|
173 | 188 | context.close();
|
174 | 189 | }
|
175 | 190 | }
|
176 | 191 |
|
177 |
| - private void assertCorrectSetupAndCloseContextForSingleDataSource(String file, int count) { |
| 192 | + private void assertCorrectSetupForSingleDataSource(String file, String dbName) { |
178 | 193 | ConfigurableApplicationContext context = context(file);
|
179 | 194 | try {
|
180 | 195 | DataSource dataSource = context.getBean(DataSource.class);
|
181 |
| - JdbcTemplate template = new JdbcTemplate(dataSource); |
182 |
| - assertNumRowsInTestTable(template, count); |
| 196 | + assertNumRowsInTestTable(new JdbcTemplate(dataSource), 1); |
| 197 | + assertTrue(dataSource instanceof AbstractDriverBasedDataSource); |
| 198 | + AbstractDriverBasedDataSource adbDataSource = (AbstractDriverBasedDataSource) dataSource; |
| 199 | + assertThat(adbDataSource.getUrl(), containsString(dbName)); |
183 | 200 | }
|
184 | 201 | finally {
|
185 | 202 | context.close();
|
|
0 commit comments