Ignore:
Timestamp:
Feb 9, 2011, 10:53:45 PM (14 years ago)
Author:
fmguler
Message:

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.

Location:
trunk/fmgVen/test/com/fmguler/ven/sample
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/fmgVen/test/com/fmguler/ven/sample/Sample.java

    r27 r28  
    1818package com.fmguler.ven.sample; 
    1919 
     20import com.fmguler.ven.LiquibaseUtil; 
    2021import com.fmguler.ven.Ven; 
     22import com.fmguler.ven.sample.domain.AnotherDomainObject; 
    2123import com.fmguler.ven.sample.domain.SomeDomainObject; 
    22 import java.sql.SQLException; 
    2324import java.util.Date; 
     25import java.util.HashSet; 
    2426import java.util.List; 
    25 import java.util.Locale; 
    26 import javax.sql.DataSource; 
    27 import liquibase.FileSystemFileOpener; 
    28 import liquibase.exception.JDBCException; 
    29 import liquibase.exception.LiquibaseException; 
    30 import org.springframework.jdbc.datasource.DriverManagerDataSource; 
    31 import liquibase.Liquibase; 
    32 import liquibase.database.Database; 
    33 import liquibase.database.DatabaseFactory; 
     27import java.util.Set; 
    3428 
    3529/** 
     
    4034    public static void main(String[] args) { 
    4135        //build the sample database 
    42         buildDatabase(); 
     36        LiquibaseUtil.buildDatabase(); 
    4337 
    4438        //save an object 
    4539        testSave(); 
     40        //get an object 
     41        testGet(); 
    4642        //delete an object 
    4743        testDelete(); 
    4844 
    4945        //rollback the sample database to original state 
    50         rollbackDatabase(); 
     46        LiquibaseUtil.rollbackDatabase("tag-init"); 
    5147    } 
    5248 
     
    7672    public static void testDelete() { 
    7773        Ven ven = getVen(); 
    78         ven.delete(1, SomeDomainObject.class); 
     74        ven.delete(2, SomeDomainObject.class); 
    7975    } 
    8076 
     
    8480    public static void testGet() { 
    8581        Ven ven = getVen(); 
    86         SomeDomainObject obj = (SomeDomainObject)ven.get(1, SomeDomainObject.class); 
     82 
     83        //get with includes 
     84        Set joins = new HashSet(); 
     85        joins.add("SomeDomainObject.anotherDomainObjects"); 
     86        joins.add("SomeDomainObject.anotherDomainObject"); 
     87        SomeDomainObject obj = (SomeDomainObject)ven.get(1, SomeDomainObject.class, joins); 
    8788        System.out.println(obj); 
     89 
     90        Set joins2 = new HashSet(); 
     91        joins2.add("AnotherDomainObject.someDomainObject"); 
     92        AnotherDomainObject obj2 = (AnotherDomainObject)ven.get(1, AnotherDomainObject.class, joins2); 
     93        System.out.println(obj2); 
    8894    } 
    8995 
     
    109115    private static Ven getVen() { 
    110116        Ven ven = new Ven(); 
    111         ven.setDataSource(getDataSource()); 
     117        ven.setDataSource(LiquibaseUtil.getDataSource()); 
    112118        ven.addDomainPackage("com.fmguler.ven.sample.domain").addDomainPackage("another.package"); 
    113119        return ven; 
    114120    } 
    115  
    116     private static DataSource getDataSource() { 
    117         DriverManagerDataSource ds = new DriverManagerDataSource(); 
    118         ds.setDriverClassName("org.postgresql.Driver"); 
    119         ds.setUsername("postgres"); 
    120         ds.setPassword("qwerty"); 
    121         ds.setUrl("jdbc:postgresql://127.0.0.1:5432/ven-test"); 
    122         return ds; 
    123     } 
    124  
    125     private static void buildDatabase() { 
    126         try { 
    127             Locale currLocale = Locale.getDefault(); 
    128             Locale.setDefault(Locale.ENGLISH); 
    129             Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(getDataSource().getConnection()); 
    130             Liquibase liquibase = new Liquibase("etc/test-db/test-db-changelog.xml", new FileSystemFileOpener(), database); 
    131             liquibase.update(""); 
    132             Locale.setDefault(currLocale); 
    133         } catch (SQLException ex) { 
    134             ex.printStackTrace(); 
    135         } catch (JDBCException ex) { 
    136             ex.printStackTrace(); 
    137         } catch (LiquibaseException ex) { 
    138             ex.printStackTrace(); 
    139         } 
    140     } 
    141  
    142     private static void rollbackDatabase() { 
    143         try { 
    144             Locale currLocale = Locale.getDefault(); 
    145             Locale.setDefault(Locale.ENGLISH); 
    146             Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(getDataSource().getConnection()); 
    147             Liquibase liquibase = new Liquibase("etc/test-db/test-db-changelog.xml", new FileSystemFileOpener(), database); 
    148             liquibase.rollback("tag-single-table", ""); 
    149             Locale.setDefault(currLocale); 
    150         } catch (SQLException ex) { 
    151             ex.printStackTrace(); 
    152         } catch (JDBCException ex) { 
    153             ex.printStackTrace(); 
    154         } catch (LiquibaseException ex) { 
    155             ex.printStackTrace(); 
    156         } 
    157     } 
    158121} 
  • trunk/fmgVen/test/com/fmguler/ven/sample/domain/SomeDomainObject.java

    r26 r28  
    1818package com.fmguler.ven.sample.domain; 
    1919 
     20import com.fmguler.ven.util.VenList; 
    2021import java.util.Date; 
     22import java.util.List; 
    2123 
    2224/** 
     
    2931    private String description; 
    3032    private Date date; 
     33    private List anotherDomainObjects = new VenList(AnotherDomainObject.class, "someDomainObject"); 
     34    private AnotherDomainObject anotherDomainObject = new AnotherDomainObject(); 
    3135 
    3236    /** 
     
    8690    } 
    8791 
     92    /** 
     93     * @return the list of AnotherDomainObject 
     94     */ 
     95    public List getAnotherDomainObjects() { 
     96        return anotherDomainObjects; 
     97    } 
     98 
     99    /** 
     100     * @return the anotherDomainObject 
     101     */ 
     102    public AnotherDomainObject getAnotherDomainObject() { 
     103        return anotherDomainObject; 
     104    } 
     105 
     106    /** 
     107     * @param anotherDomainObject the anotherDomainObject to set 
     108     */ 
     109    public void setAnotherDomainObject(AnotherDomainObject anotherDomainObject) { 
     110        this.anotherDomainObject = anotherDomainObject; 
     111    } 
     112 
    88113    public String toString() { 
    89         return id + " " + name + " " + description + " " + date; 
     114        return id + " " + name + " " + description + " another domain object: {" + anotherDomainObject + "} another domain objects:\n" + anotherDomainObjects; 
    90115    } 
    91116} 
Note: See TracChangeset for help on using the changeset viewer.