[28] | 1 | /* |
---|
| 2 | * fmgVen - A Convention over Configuration Java ORM Tool |
---|
| 3 | * Copyright 2011 Fatih Mehmet Güler |
---|
| 4 | * |
---|
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 6 | * you may not use this file except in compliance with the License. |
---|
| 7 | * You may obtain a copy of the License at |
---|
| 8 | * |
---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 10 | * |
---|
| 11 | * Unless required by applicable law or agreed to in writing, software |
---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 14 | * See the License for the specific language governing permissions and |
---|
| 15 | * limitations under the License. |
---|
| 16 | * under the License. |
---|
| 17 | */ |
---|
| 18 | package com.fmguler.ven; |
---|
| 19 | |
---|
| 20 | import java.sql.SQLException; |
---|
| 21 | import java.util.Locale; |
---|
| 22 | import javax.sql.DataSource; |
---|
| 23 | import liquibase.FileSystemFileOpener; |
---|
| 24 | import liquibase.Liquibase; |
---|
| 25 | import liquibase.database.Database; |
---|
| 26 | import liquibase.database.DatabaseFactory; |
---|
| 27 | import liquibase.exception.JDBCException; |
---|
| 28 | import liquibase.exception.LiquibaseException; |
---|
| 29 | import org.springframework.jdbc.datasource.DriverManagerDataSource; |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * Database utilities for testing |
---|
| 33 | * |
---|
| 34 | * @author Fatih Mehmet Güler |
---|
| 35 | */ |
---|
| 36 | public class LiquibaseUtil { |
---|
| 37 | /** |
---|
| 38 | * @return DataSource for the test database |
---|
| 39 | */ |
---|
| 40 | public static DataSource getDataSource() { |
---|
| 41 | DriverManagerDataSource ds = new DriverManagerDataSource(); |
---|
| 42 | ds.setDriverClassName("org.postgresql.Driver"); |
---|
| 43 | ds.setUsername("postgres"); |
---|
| 44 | ds.setPassword("qwerty"); |
---|
| 45 | ds.setUrl("jdbc:postgresql://127.0.0.1:5432/ven-test"); |
---|
| 46 | return ds; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * Build the test database |
---|
| 51 | */ |
---|
| 52 | public static void buildDatabase() { |
---|
| 53 | try { |
---|
| 54 | Locale currLocale = Locale.getDefault(); |
---|
| 55 | Locale.setDefault(Locale.ENGLISH); |
---|
| 56 | Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(getDataSource().getConnection()); |
---|
| 57 | Liquibase liquibase = new Liquibase("etc/test-db/test-db-changelog.xml", new FileSystemFileOpener(), database); |
---|
| 58 | liquibase.update(""); |
---|
| 59 | Locale.setDefault(currLocale); |
---|
| 60 | } catch (SQLException ex) { |
---|
| 61 | ex.printStackTrace(); |
---|
| 62 | } catch (JDBCException ex) { |
---|
| 63 | ex.printStackTrace(); |
---|
| 64 | } catch (LiquibaseException ex) { |
---|
| 65 | ex.printStackTrace(); |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | /** |
---|
| 70 | * Undo all changes in the test database |
---|
| 71 | */ |
---|
| 72 | public static void rollbackDatabase(String tag) { |
---|
| 73 | try { |
---|
| 74 | Locale currLocale = Locale.getDefault(); |
---|
| 75 | Locale.setDefault(Locale.ENGLISH); |
---|
| 76 | Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(getDataSource().getConnection()); |
---|
| 77 | Liquibase liquibase = new Liquibase("etc/test-db/test-db-changelog.xml", new FileSystemFileOpener(), database); |
---|
| 78 | liquibase.rollback(tag, ""); |
---|
| 79 | Locale.setDefault(currLocale); |
---|
| 80 | } catch (SQLException ex) { |
---|
| 81 | ex.printStackTrace(); |
---|
| 82 | } catch (JDBCException ex) { |
---|
| 83 | ex.printStackTrace(); |
---|
| 84 | } catch (LiquibaseException ex) { |
---|
| 85 | ex.printStackTrace(); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | * Quick test |
---|
| 91 | */ |
---|
| 92 | public static void main(String[] args) { |
---|
| 93 | //buildDatabase(); |
---|
| 94 | //rollbackDatabase("tag-no-data"); |
---|
| 95 | rollbackDatabase("tag-init"); |
---|
| 96 | |
---|
| 97 | } |
---|
| 98 | } |
---|