source: trunk/fmgVen/test/com/fmguler/ven/sample/Sample.java @ 24

Last change on this file since 24 was 24, checked in by fmguler, 14 years ago

Refs #3 - Added liquibase library for test database operations, which means we can update and rollback a test database in the test scenarios. Now we should add test scenarios to be tested on this database.

File size: 3.7 KB
Line 
1/*
2 *  fmgVen - A Convention over Configuration Java ORM Tool
3 *  Copyright 2010 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 */
18package com.fmguler.ven.sample;
19
20import com.fmguler.ven.Ven;
21import com.fmguler.ven.sample.domain.SomeDomainObject;
22import java.sql.SQLException;
23import java.util.Locale;
24import javax.sql.DataSource;
25import liquibase.FileSystemFileOpener;
26import liquibase.exception.JDBCException;
27import liquibase.exception.LiquibaseException;
28import org.springframework.jdbc.datasource.DriverManagerDataSource;
29import liquibase.Liquibase;
30import liquibase.database.Database;
31import liquibase.database.DatabaseFactory;
32
33/**
34 * Demonstrates sample usage of fmgVen.
35 * @author Fatih Mehmet Güler
36 */
37public class Sample {
38    public static void main(String[] args) {
39        buildDatabase();
40        test1();
41        //rollbackDatabase();
42    }
43
44    public static void test1() {
45        Ven ven = getVen();
46        SomeDomainObject obj = (SomeDomainObject)ven.get(1, SomeDomainObject.class);
47        System.out.println(obj);
48    }
49
50    //---------------------------------------------------------
51    private static Ven getVen() {
52        Ven ven = new Ven();
53        ven.setDataSource(getDataSource());
54        ven.addDomainPackage("com.fmguler.ven.sample.domain").addDomainPackage("another.package");
55        return ven;
56    }
57
58    private static DataSource getDataSource() {
59        DriverManagerDataSource ds = new DriverManagerDataSource();
60        ds.setDriverClassName("org.postgresql.Driver");
61        ds.setUsername("postgres");
62        ds.setPassword("qwerty");
63        ds.setUrl("jdbc:postgresql://127.0.0.1:5432/ven-test");
64        return ds;
65    }
66
67    private static void buildDatabase() {
68        try {
69            Locale currLocale = Locale.getDefault();
70            Locale.setDefault(Locale.ENGLISH);
71            Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(getDataSource().getConnection());
72            Liquibase liquibase = new Liquibase("etc/test-db/test-db-changelog.xml", new FileSystemFileOpener(), database);
73            liquibase.update("");
74            Locale.setDefault(currLocale);
75        } catch (SQLException ex) {
76            ex.printStackTrace();
77        } catch (JDBCException ex) {
78            ex.printStackTrace();
79        } catch (LiquibaseException ex) {
80            ex.printStackTrace();
81        }
82    }
83
84    private static void rollbackDatabase() {
85        try {
86            Locale currLocale = Locale.getDefault();
87            Locale.setDefault(Locale.ENGLISH);
88            Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(getDataSource().getConnection());
89            Liquibase liquibase = new Liquibase("etc/test-db/test-db-changelog.xml", new FileSystemFileOpener(), database);
90            //liquibase.rollback(51, "");
91            liquibase.rollback("0", "");
92            Locale.setDefault(currLocale);
93        } catch (SQLException ex) {
94            ex.printStackTrace();
95        } catch (JDBCException ex) {
96            ex.printStackTrace();
97        } catch (LiquibaseException ex) {
98            ex.printStackTrace();
99        }
100    }
101}
Note: See TracBrowser for help on using the repository browser.