|
2 | 2 |
|
3 | 3 | import liquibase.Liquibase;
|
4 | 4 | import liquibase.database.Database;
|
5 |
| -import liquibase.database.core.HsqlDatabase; |
| 5 | +import liquibase.database.ObjectQuotingStrategy; |
| 6 | +import liquibase.database.core.H2Database; |
6 | 7 | import liquibase.database.jvm.JdbcConnection;
|
7 | 8 | import liquibase.diff.DiffResult;
|
8 | 9 | import liquibase.diff.Difference;
|
@@ -46,9 +47,9 @@ public class HibernateIntegrationTest {
|
46 | 47 |
|
47 | 48 | @Before
|
48 | 49 | public void setUp() throws Exception {
|
49 |
| - Class.forName("org.hsqldb.jdbc.JDBCDriver"); |
50 |
| - connection = DriverManager.getConnection("jdbc:hsqldb:mem:TESTDB" + System.currentTimeMillis(), "SA", ""); |
51 |
| - database = new HsqlDatabase(); |
| 50 | + Class.forName("org.h2.Driver"); |
| 51 | + connection = DriverManager.getConnection("jdbc:h2:mem:TESTDB" + System.currentTimeMillis(), "SA", ""); |
| 52 | + database = new H2Database(); |
52 | 53 | database.setConnection(new JdbcConnection(connection));
|
53 | 54 |
|
54 | 55 | // Class.forName("com.mysql.jdbc.Driver");
|
@@ -135,98 +136,100 @@ public void runGeneratedChangeLog() throws Exception {
|
135 | 136 | *
|
136 | 137 | * @throws Exception
|
137 | 138 | */
|
138 |
| -// @Test |
139 |
| -// public void hibernateSchemaExport() throws Exception { |
140 |
| -// |
141 |
| -// SingleConnectionDataSource ds = new SingleConnectionDataSource(connection, true); |
142 |
| -// |
143 |
| -// Configuration cfg = new Configuration(); |
144 |
| -// cfg.configure(HIBERNATE_CONFIG_FILE); |
145 |
| -// Properties properties = new Properties(); |
146 |
| -// properties.put(Environment.DATASOURCE, ds); |
147 |
| -// cfg.addProperties(properties); |
148 |
| -// |
149 |
| -// SchemaExport export = new SchemaExport(cfg); |
150 |
| -// export.execute(true, true, false, false); |
151 |
| -// |
152 |
| -// Database hibernateDatabase = new HibernateClassicDatabase(); |
153 |
| -// hibernateDatabase.setDefaultSchemaName("PUBLIC"); |
154 |
| -// hibernateDatabase.setDefaultCatalogName("TESTDB"); |
155 |
| -// hibernateDatabase.setConnection(new JdbcConnection(new HibernateConnection("hibernate:classic:" + HIBERNATE_CONFIG_FILE))); |
156 |
| -// |
157 |
| -// Liquibase liquibase = new Liquibase((String) null, new ClassLoaderResourceAccessor(), database); |
158 |
| -// DiffResult diffResult = liquibase.diff(hibernateDatabase, database, compareControl); |
159 |
| -// |
160 |
| -// ignoreDatabaseChangeLogTable(diffResult); |
161 |
| -// ignoreConversionFromFloatToDouble64(diffResult); |
162 |
| -// |
163 |
| -// String differences = toString(diffResult); |
164 |
| -// |
165 |
| -// assertEquals(differences, 0, diffResult.getMissingObjects().size()); |
166 |
| -// assertEquals(differences, 0, diffResult.getUnexpectedObjects().size()); |
167 |
| -//// assertEquals(differences, 0, diffResult.getChangedObjects().size()); //unimportant differences in schema name and datatypes causing test to fail |
168 |
| -// |
169 |
| -// } |
170 |
| - |
171 |
| -// /** |
172 |
| -// * Generates the changelog from Hibernate mapping, creates 2 databases, |
173 |
| -// * updates 1 of the databases with HibernateSchemaUpdate. Compare the 2 |
174 |
| -// * databases. |
175 |
| -// * |
176 |
| -// * @throws Exception |
177 |
| -// */ |
178 |
| -// @Test |
179 |
| -// public void hibernateSchemaUpdate() throws Exception { |
180 |
| -// |
181 |
| -// Liquibase liquibase = new Liquibase((String) null, new ClassLoaderResourceAccessor(), database); |
182 |
| -// |
183 |
| -// Database hibernateDatabase = new HibernateClassicDatabase(); |
184 |
| -// hibernateDatabase.setDefaultSchemaName("PUBLIC"); |
185 |
| -// hibernateDatabase.setDefaultCatalogName("TESTDB"); |
186 |
| -// hibernateDatabase.setConnection(new JdbcConnection(new HibernateConnection("hibernate:classic:" + HIBERNATE_CONFIG_FILE))); |
187 |
| -// |
188 |
| -// DiffResult diffResult = liquibase.diff(hibernateDatabase, database, compareControl); |
189 |
| -// |
190 |
| -// assertTrue(diffResult.getMissingObjects().size() > 0); |
191 |
| -// |
192 |
| -// File outFile = File.createTempFile("lb-test", ".xml"); |
193 |
| -// OutputStream outChangeLog = new FileOutputStream(outFile); |
194 |
| -// String changeLogString = toChangeLog(diffResult); |
195 |
| -// outChangeLog.write(changeLogString.getBytes("UTF-8")); |
196 |
| -// outChangeLog.close(); |
197 |
| -// |
198 |
| -// log.info("Changelog:\n" + changeLogString); |
199 |
| -// |
200 |
| -// liquibase = new Liquibase(outFile.toString(), new FileSystemResourceAccessor(), database); |
201 |
| -// StringWriter stringWriter = new StringWriter(); |
202 |
| -// liquibase.update((String) null, stringWriter); |
203 |
| -// log.info(stringWriter.toString()); |
204 |
| -// liquibase.update((String) null); |
205 |
| -// |
206 |
| -// long currentTimeMillis = System.currentTimeMillis(); |
207 |
| -// Connection connection2 = DriverManager.getConnection("jdbc:hsqldb:mem:TESTDB2" + currentTimeMillis, "SA", ""); |
208 |
| -// Database database2 = new HsqlDatabase(); |
209 |
| -// database2.setConnection(new JdbcConnection(connection2)); |
210 |
| -// |
211 |
| -// Configuration cfg = new Configuration(); |
212 |
| -// cfg.configure(HIBERNATE_CONFIG_FILE); |
213 |
| -// cfg.getProperties().remove(Environment.DATASOURCE); |
214 |
| -// cfg.setProperty(Environment.URL, "jdbc:hsqldb:mem:TESTDB2" + currentTimeMillis); |
215 |
| -// |
216 |
| -// SchemaUpdate update = new SchemaUpdate(cfg); |
217 |
| -// update.execute(true, true); |
218 |
| -// |
219 |
| -// diffResult = liquibase.diff(database, database2, compareControl); |
220 |
| -// |
221 |
| -// ignoreDatabaseChangeLogTable(diffResult); |
222 |
| -// ignoreConversionFromFloatToDouble64(diffResult); |
223 |
| -// |
224 |
| -// String differences = toString(diffResult); |
225 |
| -// |
226 |
| -// assertEquals(differences, 0, diffResult.getMissingObjects().size()); |
227 |
| -// assertEquals(differences, 0, diffResult.getUnexpectedObjects().size()); |
228 |
| -// assertEquals(differences, 0, diffResult.getChangedObjects().size()); |
229 |
| -// } |
| 139 | + @Test |
| 140 | + public void hibernateSchemaExport() throws Exception { |
| 141 | + |
| 142 | + SingleConnectionDataSource ds = new SingleConnectionDataSource(connection, true); |
| 143 | + |
| 144 | + Configuration cfg = new Configuration(); |
| 145 | + cfg.configure(HIBERNATE_CONFIG_FILE); |
| 146 | + Properties properties = new Properties(); |
| 147 | + properties.put(Environment.DATASOURCE, ds); |
| 148 | + cfg.addProperties(properties); |
| 149 | + |
| 150 | + SchemaExport export = new SchemaExport(cfg); |
| 151 | + export.execute(true, true, false, false); |
| 152 | + |
| 153 | + Database hibernateDatabase = new HibernateClassicDatabase(); |
| 154 | + hibernateDatabase.setDefaultSchemaName("PUBLIC"); |
| 155 | + hibernateDatabase.setDefaultCatalogName("TESTDB"); |
| 156 | + hibernateDatabase.setConnection(new JdbcConnection(new HibernateConnection("hibernate:classic:" + HIBERNATE_CONFIG_FILE))); |
| 157 | + |
| 158 | + Liquibase liquibase = new Liquibase((String) null, new ClassLoaderResourceAccessor(), database); |
| 159 | + DiffResult diffResult = liquibase.diff(hibernateDatabase, database, compareControl); |
| 160 | + |
| 161 | + ignoreDatabaseChangeLogTable(diffResult); |
| 162 | + ignoreConversionFromFloatToDouble64(diffResult); |
| 163 | + |
| 164 | + String differences = toString(diffResult); |
| 165 | + |
| 166 | + assertEquals(differences, 0, diffResult.getMissingObjects().size()); |
| 167 | + assertEquals(differences, 0, diffResult.getUnexpectedObjects().size()); |
| 168 | +// assertEquals(differences, 0, diffResult.getChangedObjects().size()); //unimportant differences in schema name and datatypes causing test to fail |
| 169 | + |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Generates the changelog from Hibernate mapping, creates 2 databases, |
| 174 | + * updates 1 of the databases with HibernateSchemaUpdate. Compare the 2 |
| 175 | + * databases. |
| 176 | + * |
| 177 | + * @throws Exception |
| 178 | + */ |
| 179 | + @Test |
| 180 | + public void hibernateSchemaUpdate() throws Exception { |
| 181 | + |
| 182 | + Liquibase liquibase = new Liquibase((String) null, new ClassLoaderResourceAccessor(), database); |
| 183 | + |
| 184 | + Database hibernateDatabase = new HibernateClassicDatabase(); |
| 185 | + hibernateDatabase.setDefaultSchemaName("PUBLIC"); |
| 186 | + hibernateDatabase.setDefaultCatalogName("TESTDB"); |
| 187 | + hibernateDatabase.setConnection(new JdbcConnection(new HibernateConnection("hibernate:classic:" + HIBERNATE_CONFIG_FILE))); |
| 188 | + |
| 189 | + DiffResult diffResult = liquibase.diff(hibernateDatabase, database, compareControl); |
| 190 | + |
| 191 | + assertTrue(diffResult.getMissingObjects().size() > 0); |
| 192 | + |
| 193 | + File outFile = File.createTempFile("lb-test", ".xml"); |
| 194 | + OutputStream outChangeLog = new FileOutputStream(outFile); |
| 195 | + String changeLogString = toChangeLog(diffResult); |
| 196 | + outChangeLog.write(changeLogString.getBytes("UTF-8")); |
| 197 | + outChangeLog.close(); |
| 198 | + |
| 199 | + log.info("Changelog:\n" + changeLogString); |
| 200 | + |
| 201 | + liquibase = new Liquibase(outFile.toString(), new FileSystemResourceAccessor(), database); |
| 202 | + StringWriter stringWriter = new StringWriter(); |
| 203 | + liquibase.update((String) null, stringWriter); |
| 204 | + log.info(stringWriter.toString()); |
| 205 | + liquibase.update((String) null); |
| 206 | + |
| 207 | + long currentTimeMillis = System.currentTimeMillis(); |
| 208 | + Connection connection2 = DriverManager.getConnection("jdbc:h2:mem:TESTDB2" + currentTimeMillis, "SA", ""); |
| 209 | + Database database2 = new H2Database(); |
| 210 | + database2.setConnection(new JdbcConnection(connection2)); |
| 211 | + |
| 212 | + Configuration cfg = new Configuration(); |
| 213 | + cfg.configure(HIBERNATE_CONFIG_FILE); |
| 214 | + cfg.getProperties().remove(Environment.DATASOURCE); |
| 215 | + cfg.setProperty(Environment.URL, "jdbc:h2:mem:TESTDB2" + currentTimeMillis); |
| 216 | + cfg.setProperty(Environment.USER, "SA"); |
| 217 | + cfg.setProperty(Environment.PASS, ""); |
| 218 | + |
| 219 | + SchemaUpdate update = new SchemaUpdate(cfg); |
| 220 | + update.execute(true, true); |
| 221 | + |
| 222 | + diffResult = liquibase.diff(database, database2, compareControl); |
| 223 | + |
| 224 | + ignoreDatabaseChangeLogTable(diffResult); |
| 225 | + ignoreConversionFromFloatToDouble64(diffResult); |
| 226 | + |
| 227 | + String differences = toString(diffResult); |
| 228 | + |
| 229 | + assertEquals(differences, 0, diffResult.getMissingObjects().size()); |
| 230 | + assertEquals(differences, 0, diffResult.getUnexpectedObjects().size()); |
| 231 | + assertEquals(differences, 0, diffResult.getChangedObjects().size()); |
| 232 | + } |
230 | 233 |
|
231 | 234 | private String toString(DiffResult diffResult) throws Exception {
|
232 | 235 | ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
0 commit comments