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

Last change on this file since 26 was 26, checked in by fmguler, 13 years ago

Refs #3 - Ven.save() is transferred to the new package. Save use case is completed. Convert.toDB() converts camel case object names to database names with underscores. Unchecked VenException is thrown by Ven methods. Liquibase changelog (test-db-changelog.xml) is changed to include only a simple object to test save operation. It creates a sample database table and inserts a sample row. On rollback it removes sample data. Using liquibase, Sample.java builds database, tests save operation and rolls back.

QueryGenerator generates insert and update queries for save and generates sequence query to assign ids to new objects. Ven calls query generator and runs the query using spring jdbc template.

Old codebase updated to run at Java 1.4

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        testSave();
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("tag-single-table", "");
142            Locale.setDefault(currLocale);
143        } catch (SQLException ex) {
144            ex.printStackTrace();
145        } catch (JDBCException ex) {
146            ex.printStackTrace();
147        } catch (LiquibaseException ex) {
148            ex.printStackTrace();
149        }
150    }
151}
Note: See TracBrowser for help on using the repository browser.