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

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

Refs #3 - Ven.list() is converted to the new format. Almost the same as Ven.get(). Added javadoc to Ven public methods. Added listing test, and made the sample domain object toString methods more readable.

Now, adding the criteria parameter to the operations is to be done.

File size: 4.2 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.LiquibaseUtil;
21import com.fmguler.ven.Ven;
22import com.fmguler.ven.sample.domain.AnotherDomainObject;
23import com.fmguler.ven.sample.domain.SomeDomainObject;
24import java.util.Date;
25import java.util.HashSet;
26import java.util.Iterator;
27import java.util.List;
28import java.util.Set;
29
30/**
31 * Demonstrates sample usage of fmgVen.
32 * @author Fatih Mehmet Güler
33 */
34public class Sample {
35    public static void main(String[] args) {
36        //build the sample database
37        LiquibaseUtil.buildDatabase();
38
39        //save an object
40        testSave();
41        //get an object
42        testGet();
43        //list the objects
44        testList();
45        //delete an object
46        testDelete();
47
48        //rollback the sample database to original state
49        LiquibaseUtil.rollbackDatabase("tag-init");
50    }
51
52    /**
53     * Test save an object
54     */
55    public static void testSave() {
56        System.out.println("******SAVE******");
57        Ven ven = getVen();
58
59        //insert
60        SomeDomainObject obj = new SomeDomainObject();
61        obj.setName("name");
62        obj.setDescription("desc");
63        obj.setDate(new Date());
64        ven.save(obj);
65        System.out.println(obj);
66
67        //update
68        obj.setName("name update");
69        ven.save(obj);
70        System.out.println(obj);
71    }
72
73    /**
74     * Test delete an object
75     */
76    public static void testDelete() {
77        System.out.println("******DELETE******");
78        Ven ven = getVen();
79        ven.delete(2, SomeDomainObject.class);
80    }
81
82    /**
83     * Test get an object by primary key
84     */
85    public static void testGet() {
86        System.out.println("******GET******");
87        Ven ven = getVen();
88
89        //get with includes
90        Set joins = new HashSet();
91        joins.add("SomeDomainObject.anotherDomainObjects");
92        joins.add("SomeDomainObject.anotherDomainObject");
93        SomeDomainObject obj = (SomeDomainObject)ven.get(1, SomeDomainObject.class, joins);
94        System.out.println(obj);
95
96        Set joins2 = new HashSet();
97        joins2.add("AnotherDomainObject.someDomainObject");
98        AnotherDomainObject obj2 = (AnotherDomainObject)ven.get(1, AnotherDomainObject.class, joins2);
99        System.out.println(obj2);
100    }
101
102    /**
103     * Test list the collection of objects
104     */
105    public static void testList() {
106        System.out.println("******LIST******");
107        Ven ven = getVen();
108
109        //list with includes
110        Set joins = new HashSet();
111        joins.add("SomeDomainObject.anotherDomainObjects");
112        joins.add("SomeDomainObject.anotherDomainObject");
113        List objList = ven.list(SomeDomainObject.class, joins);
114       
115        Iterator it = objList.iterator();
116        while (it.hasNext()) {
117            SomeDomainObject someDomainObject = (SomeDomainObject)it.next();
118            System.out.println(someDomainObject);
119        }
120    }
121
122    /**
123     * Test list the collection of objects by some criteria
124     */
125    public static void testListByCriteria() {
126        Ven ven = getVen();
127        //List objList = ven.list(SomeDomainObject.class/*, criteria */);
128        //System.out.println(objList);
129    }
130
131    //---------------------------------------------------------
132    private static Ven getVen() {
133        Ven ven = new Ven();
134        ven.setDataSource(LiquibaseUtil.getDataSource());
135        ven.addDomainPackage("com.fmguler.ven.sample.domain").addDomainPackage("another.package");
136        return ven;
137    }
138}
Note: See TracBrowser for help on using the repository browser.