source: trunk/fmgVen/test/com/fmguler/ven/LiquibaseUtil.java @ 30

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

Refs #3 - Ven.get() is converted to the new format. QueryGenerator and QueryMapper are mostly OK, generateRecursively and mapRecursively are converted and checked. Handles joins (includes/associations) many to one and one to many. For one to many, the reverse join field can be determined in a couple of ways. First way is (prefereed) having VenList as the list implementation which specifies the element class and the join field. Second way is using Java 1.5 generic type to detect element class (not yet implemented) and guessing join field by convention (if multiple joins exist, this won't work). The last way is to have some kind of annotation or configuration, which is of course the least preferred way. VenList has a static method to determine the element class in the object list, which currently calls getElementClass if the list is an instance of VenList. In the future other options can be implemented.

Getting object using joins (includes/associations) are tested using dummy assocations between SomeDomainObject and AnotherDomainObject. The Sample class builds the database, tests the operations and rolls back to the initial state. Database refactoring operations are moved to the LiquibaseUtil for clarity.

In the future, the generated queries will be shortened using hashed aliases, and the criteria subsystem will be implemented.

File size: 3.4 KB
Line 
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 */
18package com.fmguler.ven;
19
20import java.sql.SQLException;
21import java.util.Locale;
22import javax.sql.DataSource;
23import liquibase.FileSystemFileOpener;
24import liquibase.Liquibase;
25import liquibase.database.Database;
26import liquibase.database.DatabaseFactory;
27import liquibase.exception.JDBCException;
28import liquibase.exception.LiquibaseException;
29import org.springframework.jdbc.datasource.DriverManagerDataSource;
30
31/**
32 * Database utilities for testing
33 *
34 * @author Fatih Mehmet Güler
35 */
36public 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}
Note: See TracBrowser for help on using the repository browser.