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

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

Refs #3 - Started adding test scenarios. These are in line with the use cases: http://trac.fmguler.com/UtilityProjects/wiki/FmgVen/Development/UseCases

File size: 5.0 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.Date;
24import java.util.List;
25import java.util.Locale;
26import javax.sql.DataSource;
27import liquibase.FileSystemFileOpener;
28import liquibase.exception.JDBCException;
29import liquibase.exception.LiquibaseException;
30import org.springframework.jdbc.datasource.DriverManagerDataSource;
31import liquibase.Liquibase;
32import liquibase.database.Database;
33import liquibase.database.DatabaseFactory;
34
35/**
36 * Demonstrates sample usage of fmgVen.
37 * @author Fatih Mehmet Güler
38 */
39public class Sample {
40    public static void main(String[] args) {
41        buildDatabase();
42        testGet();
43        //rollbackDatabase();
44    }
45
46    /**
47     * Test save an object
48     */
49    public static void testSave() {
50        Ven ven = getVen();
51
52        //insert
53        SomeDomainObject obj = new SomeDomainObject();
54        obj.setName("name");
55        obj.setDescription("desc");
56        obj.setDate(new Date());
57        ven.save(obj);
58        System.out.println(obj);
59
60        //update
61        obj.setName("name update");
62        ven.save(obj);
63        System.out.println(obj);
64    }
65
66    /**
67     * Test delete an object
68     */
69    public static void testDelete() {
70        Ven ven = getVen();
71        ven.delete(1, SomeDomainObject.class);
72    }
73
74    /**
75     * Test get an object by primary key
76     */
77    public static void testGet() {
78        Ven ven = getVen();
79        SomeDomainObject obj = (SomeDomainObject)ven.get(1, SomeDomainObject.class);
80        System.out.println(obj);
81    }
82
83    /**
84     * Test list the collection of objects
85     */
86    public static void testList() {
87        Ven ven = getVen();
88        List objList = ven.list(SomeDomainObject.class);
89        System.out.println(objList);
90    }
91
92    /**
93     * Test list the collection of objects by some criteria
94     */
95    public static void testListByCriteria() {
96        Ven ven = getVen();
97        List objList = ven.list(SomeDomainObject.class/*, criteria */);
98        System.out.println(objList);
99    }
100
101    //---------------------------------------------------------
102    private static Ven getVen() {
103        Ven ven = new Ven();
104        ven.setDataSource(getDataSource());
105        ven.addDomainPackage("com.fmguler.ven.sample.domain").addDomainPackage("another.package");
106        return ven;
107    }
108
109    private static DataSource getDataSource() {
110        DriverManagerDataSource ds = new DriverManagerDataSource();
111        ds.setDriverClassName("org.postgresql.Driver");
112        ds.setUsername("postgres");
113        ds.setPassword("qwerty");
114        ds.setUrl("jdbc:postgresql://127.0.0.1:5432/ven-test");
115        return ds;
116    }
117
118    private static void buildDatabase() {
119        try {
120            Locale currLocale = Locale.getDefault();
121            Locale.setDefault(Locale.ENGLISH);
122            Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(getDataSource().getConnection());
123            Liquibase liquibase = new Liquibase("etc/test-db/test-db-changelog.xml", new FileSystemFileOpener(), database);
124            liquibase.update("");
125            Locale.setDefault(currLocale);
126        } catch (SQLException ex) {
127            ex.printStackTrace();
128        } catch (JDBCException ex) {
129            ex.printStackTrace();
130        } catch (LiquibaseException ex) {
131            ex.printStackTrace();
132        }
133    }
134
135    private static void rollbackDatabase() {
136        try {
137            Locale currLocale = Locale.getDefault();
138            Locale.setDefault(Locale.ENGLISH);
139            Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(getDataSource().getConnection());
140            Liquibase liquibase = new Liquibase("etc/test-db/test-db-changelog.xml", new FileSystemFileOpener(), database);
141            //liquibase.rollback(51, "");
142            liquibase.rollback("0", "");
143            Locale.setDefault(currLocale);
144        } catch (SQLException ex) {
145            ex.printStackTrace();
146        } catch (JDBCException ex) {
147            ex.printStackTrace();
148        } catch (LiquibaseException ex) {
149            ex.printStackTrace();
150        }
151    }
152}
Note: See TracBrowser for help on using the repository browser.