|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.jdbc; |
18 | 18 |
|
| 19 | +import java.sql.SQLException; |
19 | 20 | import java.util.Random; |
20 | 21 |
|
21 | 22 | import javax.sql.DataSource; |
|
31 | 32 | import org.springframework.context.annotation.Bean; |
32 | 33 | import org.springframework.context.annotation.Configuration; |
33 | 34 | import org.springframework.context.annotation.Primary; |
| 35 | +import org.springframework.jdbc.BadSqlGrammarException; |
34 | 36 | import org.springframework.jdbc.core.JdbcOperations; |
35 | 37 | import org.springframework.jdbc.core.JdbcTemplate; |
36 | 38 | import org.springframework.util.ClassUtils; |
37 | 39 |
|
38 | 40 | import static org.junit.Assert.assertEquals; |
39 | 41 | import static org.junit.Assert.assertNotNull; |
40 | 42 | import static org.junit.Assert.assertTrue; |
| 43 | +import static org.junit.Assert.fail; |
41 | 44 |
|
42 | 45 | /** |
43 | 46 | * Tests for {@link DataSourceInitializer}. |
@@ -96,48 +99,56 @@ public void testDataSourceInitialized() throws Exception { |
96 | 99 | assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); |
97 | 100 | assertNotNull(dataSource); |
98 | 101 | JdbcOperations template = new JdbcTemplate(dataSource); |
99 | | - assertEquals(new Integer(0), |
| 102 | + assertEquals(new Integer(1), |
100 | 103 | template.queryForObject("SELECT COUNT(*) from BAR", Integer.class)); |
101 | 104 | } |
102 | 105 |
|
103 | 106 | @Test |
104 | 107 | public void testDataSourceInitializedWithExplicitScript() throws Exception { |
105 | 108 | this.context.register(DataSourceAutoConfiguration.class, |
106 | 109 | PropertyPlaceholderAutoConfiguration.class); |
107 | | - EnvironmentTestUtils.addEnvironment( |
108 | | - this.context, |
109 | | - "spring.datasource.initialize:true", |
110 | | - "spring.datasource.schema:" |
111 | | - + ClassUtils.addResourcePathToPackagePath(getClass(), |
112 | | - "schema.sql")); |
| 110 | + EnvironmentTestUtils |
| 111 | + .addEnvironment( |
| 112 | + this.context, |
| 113 | + "spring.datasource.initialize:true", |
| 114 | + "spring.datasource.schema:" |
| 115 | + + ClassUtils.addResourcePathToPackagePath(getClass(), |
| 116 | + "schema.sql"), |
| 117 | + "spring.datasource.data:" |
| 118 | + + ClassUtils.addResourcePathToPackagePath(getClass(), |
| 119 | + "data.sql")); |
113 | 120 | this.context.refresh(); |
114 | 121 | DataSource dataSource = this.context.getBean(DataSource.class); |
115 | 122 | assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); |
116 | 123 | assertNotNull(dataSource); |
117 | 124 | JdbcOperations template = new JdbcTemplate(dataSource); |
118 | | - assertEquals(new Integer(0), |
| 125 | + assertEquals(new Integer(1), |
119 | 126 | template.queryForObject("SELECT COUNT(*) from FOO", Integer.class)); |
120 | 127 | } |
121 | 128 |
|
122 | 129 | @Test |
123 | 130 | public void testDataSourceInitializedWithMultipleScripts() throws Exception { |
124 | | - EnvironmentTestUtils.addEnvironment( |
125 | | - this.context, |
126 | | - "spring.datasource.initialize:true", |
127 | | - "spring.datasource.schema:" |
128 | | - + ClassUtils.addResourcePathToPackagePath(getClass(), |
129 | | - "schema.sql") |
130 | | - + "," |
131 | | - + ClassUtils.addResourcePathToPackagePath(getClass(), |
132 | | - "another.sql")); |
| 131 | + EnvironmentTestUtils |
| 132 | + .addEnvironment( |
| 133 | + this.context, |
| 134 | + "spring.datasource.initialize:true", |
| 135 | + "spring.datasource.schema:" |
| 136 | + + ClassUtils.addResourcePathToPackagePath(getClass(), |
| 137 | + "schema.sql") |
| 138 | + + "," |
| 139 | + + ClassUtils.addResourcePathToPackagePath(getClass(), |
| 140 | + "another.sql"), |
| 141 | + "spring.datasource.data:" |
| 142 | + + ClassUtils.addResourcePathToPackagePath(getClass(), |
| 143 | + "data.sql")); |
133 | 144 | this.context.register(DataSourceAutoConfiguration.class, |
134 | 145 | PropertyPlaceholderAutoConfiguration.class); |
135 | 146 | this.context.refresh(); |
136 | 147 | DataSource dataSource = this.context.getBean(DataSource.class); |
137 | 148 | assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); |
138 | 149 | assertNotNull(dataSource); |
139 | 150 | JdbcOperations template = new JdbcTemplate(dataSource); |
140 | | - assertEquals(new Integer(0), |
| 151 | + assertEquals(new Integer(1), |
141 | 152 | template.queryForObject("SELECT COUNT(*) from FOO", Integer.class)); |
142 | 153 | assertEquals(new Integer(0), |
143 | 154 | template.queryForObject("SELECT COUNT(*) from SPAM", Integer.class)); |
@@ -170,6 +181,31 @@ public void testDataSourceInitializedWithExplicitSqlScriptEncoding() throws Exce |
170 | 181 | template.queryForObject("SELECT name from BAR WHERE id=2", String.class)); |
171 | 182 | } |
172 | 183 |
|
| 184 | + @Test |
| 185 | + public void testInitializationDisabled() throws Exception { |
| 186 | + this.context.register(DataSourceAutoConfiguration.class, |
| 187 | + PropertyPlaceholderAutoConfiguration.class); |
| 188 | + this.context.refresh(); |
| 189 | + |
| 190 | + DataSource dataSource = this.context.getBean(DataSource.class); |
| 191 | + |
| 192 | + this.context.publishEvent(new DataSourceInitializedEvent(dataSource)); |
| 193 | + |
| 194 | + assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); |
| 195 | + assertNotNull(dataSource); |
| 196 | + JdbcOperations template = new JdbcTemplate(dataSource); |
| 197 | + |
| 198 | + try { |
| 199 | + template.queryForObject("SELECT COUNT(*) from BAR", Integer.class); |
| 200 | + fail("Query should have failed as BAR table does not exist"); |
| 201 | + } |
| 202 | + catch (BadSqlGrammarException ex) { |
| 203 | + SQLException sqlException = ex.getSQLException(); |
| 204 | + int expectedCode = -5501; // user lacks privilege or object not found |
| 205 | + assertEquals(expectedCode, sqlException.getErrorCode()); |
| 206 | + } |
| 207 | + } |
| 208 | + |
173 | 209 | @Configuration |
174 | 210 | @EnableConfigurationProperties |
175 | 211 | protected static class TwoDataSources { |
|
0 commit comments