You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2002/02/09 14:49:47 UTC

cvs commit: jakarta-commons-sandbox/simplestore/src/test/org/apache/commons/simplestore TestPersistent.java TestSample.java

baliuka     02/02/09 05:49:47

  Modified:    simplestore/src/sample/org/apache/commons/simplestore/jdbc
                        DBStorage.java
               simplestore/src/sample/org/apache/commons/simplestore/persistence
                        AbstractStorage.java MetaObject.java
                        PersistenceManager.java PersistentProxy.java
  Added:       simplestore/src/test/org/apache/commons/simplestore
                        TestPersistent.java TestSample.java
  Log:
  Added test for sample
  
  Revision  Changes    Path
  1.2       +3 -107    jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/jdbc/DBStorage.java
  
  Index: DBStorage.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/jdbc/DBStorage.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DBStorage.java	7 Feb 2002 16:25:36 -0000	1.1
  +++ DBStorage.java	9 Feb 2002 13:49:47 -0000	1.2
  @@ -17,29 +17,16 @@
    *
    * @author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - * @version $Id: DBStorage.java,v 1.1 2002/02/07 16:25:36 baliuka Exp $
  + * @version $Id: DBStorage.java,v 1.2 2002/02/09 13:49:47 baliuka Exp $
    */
   public class DBStorage extends AbstractStorage{
       
       private static final boolean DEBUG = true;
       private static final String CONNECTION = "org.apache.commons.simplestore.jdbc.DBStorage.connection";
  -    private static java.util.Map defaults = new java.util.Hashtable();
       private Store store = null;
       private javax.sql.DataSource ds;
       private static java.util.Properties procedures = new java.util.Properties();
       
  -    static {
  -        
  -        defaults.put( byte.class,new java.lang.Byte((byte)0) );
  -        defaults.put( short.class,new java.lang.Short((short)0) );
  -        defaults.put( int.class,new java.lang.Integer(0) );
  -        defaults.put( long.class,new java.lang.Long(0) );
  -        defaults.put( float.class,new java.lang.Float(0) );
  -        defaults.put( double.class,new java.lang.Double(0) );
  -        defaults.put( char.class,new java.lang.Character('\u0000') );
  -        defaults.put( boolean.class,new java.lang.Boolean(false) );
  -        
  -    }
       
       
       interface ResultSetHandler {
  @@ -78,7 +65,7 @@
                   String property = toPropertyName(name);
                   if( property.equals("Id") || map == null )
                       return;
  -                map.put(property , convert(value,getReturnType(clasz,"get" + property)) );
  +                map.put(property , PersistentProxy.convert(value,getReturnType(clasz,"get" + property)) );
                   
               }catch( java.lang.Throwable t ){
                   t.printStackTrace();
  @@ -142,98 +129,7 @@
           
           excecute(connection,sql,values.toArray(),null);
           
  -    }
  -    // TODO must be converter interface
  -    public static Object convertNumber(Number number,Class cls){
  -        
  -        if(cls.equals(Byte.class))
  -            return new Byte(number.byteValue());
  -        else if( cls.equals(Short.class) )
  -            return new Short(number.shortValue());
  -        else if(cls.equals(Integer.class))
  -            return new Integer(number.intValue());
  -        else if( cls.equals( Long.class ) )
  -            return new Long(number.longValue());
  -        else if( cls.equals( Float.class ) )
  -            return new Float(number.floatValue());
  -        else if( cls.equals( Double.class ) )
  -            return new Double(number.doubleValue());
  -        else if( cls.equals( Boolean.class ) )
  -            return new Boolean( number.intValue() != 0  );
  -        else if( cls.equals( Character.TYPE ) )
  -            return new Character( number.toString().charAt(0)  );
  -        else
  -            throw new java.lang.UnsupportedOperationException( "Number class = " + number.getClass().getName() + " Target Class " + cls.getName());
  -        
  -    }
  -    // TODO must be converter interface
  -    public static Object convertPrimityve(Number number,Class cls){
           
  -        if(cls.equals(Byte.TYPE))
  -            return new Byte(number.byteValue());
  -        else if (cls.equals(Short.TYPE))
  -            return new Short( number.shortValue());
  -        else if(cls.equals(Integer.TYPE))
  -            return new Integer(number.intValue());
  -        else if ( cls.equals( Long.TYPE ) )
  -            return new Long(number.longValue());
  -        else if ( cls.equals( Float.TYPE ) )
  -            return new Float(number.floatValue());
  -        else if ( cls.equals( Double.TYPE ) )
  -            return new Double(number.doubleValue());
  -        else if ( cls.equals( Boolean.TYPE ) )
  -            return new Boolean( number.intValue() != 0  );
  -        else if ( cls.equals( Character.TYPE ) )
  -            return new Character( number.toString().charAt(0)  );
  -        else
  -            throw new java.lang.UnsupportedOperationException( "Number class = " + number.getClass().getName() + " Target Class " + cls.getName());
  -        
  -    }
  -    // TODO must be converter interface
  -    public static Object convert(Object object,Class cls){
  -        try{
  -            if( cls.isPrimitive() ){
  -                if( object == null ){
  -                    return defaults.get(cls);
  -                }
  -                if( object instanceof Number ){
  -                    return convertPrimityve((Number)object,cls);
  -                }
  -                
  -            }
  -            
  -            if(object == null){
  -                return null;
  -            }
  -            
  -            if( cls.isAssignableFrom(object.getClass())  ){
  -                return object;
  -            }
  -            
  -            if( object instanceof String ){
  -                return org.apache.commons.beanutils.ConvertUtils.convert((String)object,cls);
  -            }
  -            
  -            
  -            if( cls.isAssignableFrom(Number.class ) && object instanceof Number ){
  -                return convertNumber((Number)object,cls);
  -            }
  -            
  -            if( cls.equals( Boolean.class ) && object instanceof Number ){
  -                return new Boolean((( Number )object).intValue() != 0  );
  -            }
  -            
  -            if( cls.equals( Character.TYPE ) ){
  -                return new Character( object.toString().charAt(0)  );
  -            }
  -            
  -            throw new java.lang.UnsupportedOperationException( cls.getName() + ":" + object );
  -            
  -        }catch(Throwable t){
  -            // TODO
  -            t.printStackTrace();
  -            throw new RuntimeException( t.getClass().getName() + ":" + t.getMessage() );
  -        }
       }
       
       // TODO must be converter interface
  @@ -291,7 +187,7 @@
                           String property = toPropertyName(name);
                           if( property.equals("Id") )
                               return;
  -                        map.put(property , convert(value,getReturnType(clasz,"get" + property)) );
  +                        map.put(property , PersistentProxy.convert(value,getReturnType(clasz,"get" + property)) );
                       }
                   }catch(java.lang.Throwable t){
                       t.printStackTrace();
  
  
  
  1.2       +2 -2      jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/persistence/AbstractStorage.java
  
  Index: AbstractStorage.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/persistence/AbstractStorage.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AbstractStorage.java	7 Feb 2002 16:25:36 -0000	1.1
  +++ AbstractStorage.java	9 Feb 2002 13:49:47 -0000	1.2
  @@ -8,13 +8,13 @@
   
   package org.apache.commons.simplestore.persistence;
   
  -import com.mw.persist.*;
  +import org.apache.commons.simplestore.*;
   
   /**
    *
    * @author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - * @version $Id: AbstractStorage.java,v 1.1 2002/02/07 16:25:36 baliuka Exp $
  + * @version $Id: AbstractStorage.java,v 1.2 2002/02/09 13:49:47 baliuka Exp $
    */
   
   public abstract class AbstractStorage implements Storage, InternalTransaction, TransactionManager{
  
  
  
  1.2       +2 -2      jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/persistence/MetaObject.java
  
  Index: MetaObject.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/persistence/MetaObject.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MetaObject.java	7 Feb 2002 16:25:36 -0000	1.1
  +++ MetaObject.java	9 Feb 2002 13:49:47 -0000	1.2
  @@ -12,7 +12,7 @@
    *
    * @author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - * @version $Id: MetaObject.java,v 1.1 2002/02/07 16:25:36 baliuka Exp $
  + * @version $Id: MetaObject.java,v 1.2 2002/02/09 13:49:47 baliuka Exp $
    */
   
   public interface MetaObject  {
  @@ -39,6 +39,6 @@
       
       public void setDirty(boolean dirty);
       
  -    
  +    public Persistent getObject();
   }
   
  
  
  
  1.2       +2 -1      jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/persistence/PersistenceManager.java
  
  Index: PersistenceManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/persistence/PersistenceManager.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PersistenceManager.java	7 Feb 2002 16:25:36 -0000	1.1
  +++ PersistenceManager.java	9 Feb 2002 13:49:47 -0000	1.2
  @@ -14,7 +14,7 @@
    *
    * @author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - * @version $Id: PersistenceManager.java,v 1.1 2002/02/07 16:25:36 baliuka Exp $
  + * @version $Id: PersistenceManager.java,v 1.2 2002/02/09 13:49:47 baliuka Exp $
    */
   
   public class PersistenceManager {
  @@ -51,6 +51,7 @@
           
           Object id = generateOID(aclass);
           Persistent p = PersistentProxy.getPersitent(aclass,id,true,transactionManager);
  +        store.put(id,p);
           
           return p;
           
  
  
  
  1.2       +122 -13   jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/persistence/PersistentProxy.java
  
  Index: PersistentProxy.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/sample/org/apache/commons/simplestore/persistence/PersistentProxy.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PersistentProxy.java	7 Feb 2002 16:25:36 -0000	1.1
  +++ PersistentProxy.java	9 Feb 2002 13:49:47 -0000	1.2
  @@ -14,16 +14,17 @@
    *
    * @author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - * @version $Id: PersistentProxy.java,v 1.1 2002/02/07 16:25:36 baliuka Exp $
  + * @version $Id: PersistentProxy.java,v 1.2 2002/02/09 13:49:47 baliuka Exp $
    */
   public class PersistentProxy implements MetaObject, InvocationHandler, java.io.Serializable {
       
       
  -    static Method HASH_CODE;
  -    static Method EQUALS;
  -    static Method TO_STRING;
  -    static Method GET_OID;
  -    static Method GET_META_OBJECT;
  +    private static Method HASH_CODE;
  +    private static Method EQUALS;
  +    private static Method TO_STRING;
  +    private static Method GET_OID;
  +    private static Method GET_META_OBJECT;
  +    private static java.util.Map defaults = new java.util.Hashtable();
       
       static{
           
  @@ -37,6 +38,15 @@
               TO_STRING       = Object.class.getMethod("toString",null);
               EQUALS          = Object.class.getMethod("equals",new Class[]{Object.class});
               
  +           defaults.put( byte.class,new java.lang.Byte((byte)0) );
  +           defaults.put( short.class,new java.lang.Short((short)0) );
  +           defaults.put( int.class,new java.lang.Integer(0) );
  +           defaults.put( long.class,new java.lang.Long(0) );
  +           defaults.put( float.class,new java.lang.Float(0) );
  +           defaults.put( double.class,new java.lang.Double(0) );
  +           defaults.put( char.class,new java.lang.Character('\u0000') );
  +           defaults.put( boolean.class,new java.lang.Boolean(false) );
  +        
           }catch(Exception e){
               e.printStackTrace();
               throw new Error(e.getMessage());
  @@ -48,12 +58,14 @@
       java.util.Map map = new java.util.HashMap();
       
       Object  oid  = null;
  +    Persistent  object;
       Class pClass;
       TransactionManager transactionManager;
       boolean dirty   = false;
       boolean deleted = false;
       boolean newCreated ;
       
  +    
       /** Creates new ValueProxy */
       public PersistentProxy(Class pClass,Object oid, boolean newCreated, TransactionManager transactionManager) {
           
  @@ -98,7 +110,7 @@
                   
               }else if( TO_STRING.equals( method ) ){
                   
  -                return oid;
  +                return oid + "";
                   
               }else if( GET_OID.equals( method ) ){
                   
  @@ -141,8 +153,10 @@
               if(method.getName().startsWith("get"))
                   name = method.getName().substring(3);
               else name = method.getName().substring(2);
  -            
  -            return getProperty(name);
  +            Object value = getProperty(name);
  +            if( value == null && method.getReturnType().isPrimitive() )
  +                value = convert(value, method.getReturnType());
  +            return value;
               
           }
           
  @@ -153,11 +167,10 @@
       
       
       public static Persistent getPersitent(Class persistent,Object oid,boolean newCreated, TransactionManager transactionManager){
  -        
  +        PersistentProxy handler = new PersistentProxy(persistent,oid,newCreated, transactionManager);
           Persistent p =  (Persistent)Proxy.newProxyInstance(
  -        ClassLoader.getSystemClassLoader(),new Class[]{persistent,Persistent.class},
  -        new PersistentProxy(persistent,oid,newCreated, transactionManager));
  -        
  +        ClassLoader.getSystemClassLoader(),new Class[]{persistent,Persistent.class},handler);
  +        handler.object = p; 
           if( newCreated ) {
               
               MetaObject mo = p.getMetaObject();
  @@ -224,4 +237,100 @@
           this.newCreated = false;
       }
       
  +    public Persistent getObject() {
  +        return object;
  +    }
  +// TODO must be converter interface
  +    public static Object convertNumber(Number number,Class cls){
  +        
  +        if(cls.equals(Byte.class))
  +            return new Byte(number.byteValue());
  +        else if( cls.equals(Short.class) )
  +            return new Short(number.shortValue());
  +        else if(cls.equals(Integer.class))
  +            return new Integer(number.intValue());
  +        else if( cls.equals( Long.class ) )
  +            return new Long(number.longValue());
  +        else if( cls.equals( Float.class ) )
  +            return new Float(number.floatValue());
  +        else if( cls.equals( Double.class ) )
  +            return new Double(number.doubleValue());
  +        else if( cls.equals( Boolean.class ) )
  +            return new Boolean( number.intValue() != 0  );
  +        else if( cls.equals( Character.TYPE ) )
  +            return new Character( number.toString().charAt(0)  );
  +        else
  +            throw new java.lang.UnsupportedOperationException( "Number class = " + number.getClass().getName() + " Target Class " + cls.getName());
  +        
  +    }
  +    // TODO must be converter interface
  +    public static Object convertPrimityve(Number number,Class cls){
  +        
  +        if(cls.equals(Byte.TYPE))
  +            return new Byte(number.byteValue());
  +        else if (cls.equals(Short.TYPE))
  +            return new Short( number.shortValue());
  +        else if(cls.equals(Integer.TYPE))
  +            return new Integer(number.intValue());
  +        else if ( cls.equals( Long.TYPE ) )
  +            return new Long(number.longValue());
  +        else if ( cls.equals( Float.TYPE ) )
  +            return new Float(number.floatValue());
  +        else if ( cls.equals( Double.TYPE ) )
  +            return new Double(number.doubleValue());
  +        else if ( cls.equals( Boolean.TYPE ) )
  +            return new Boolean( number.intValue() != 0  );
  +        else if ( cls.equals( Character.TYPE ) )
  +            return new Character( number.toString().charAt(0)  );
  +        else
  +            throw new java.lang.UnsupportedOperationException( "Number class = " + number.getClass().getName() + " Target Class " + cls.getName());
  +        
  +    }
  +    // TODO must be converter interface
  +    public static Object convert(Object object,Class cls){
  +        try{
  +            if( cls.isPrimitive() ){
  +                if( object == null ){
  +                    return defaults.get(cls);
  +                }
  +                if( object instanceof Number ){
  +                    return convertPrimityve((Number)object,cls);
  +                }
  +                
  +            }
  +            
  +            if(object == null){
  +                return null;
  +            }
  +            
  +            if( cls.isAssignableFrom(object.getClass())  ){
  +                return object;
  +            }
  +            
  +            if( object instanceof String ){
  +                return org.apache.commons.beanutils.ConvertUtils.convert((String)object,cls);
  +            }
  +            
  +            
  +            if( cls.isAssignableFrom(Number.class ) && object instanceof Number ){
  +                return convertNumber((Number)object,cls);
  +            }
  +            
  +            if( cls.equals( Boolean.class ) && object instanceof Number ){
  +                return new Boolean((( Number )object).intValue() != 0  );
  +            }
  +            
  +            if( cls.equals( Character.TYPE ) ){
  +                return new Character( object.toString().charAt(0)  );
  +            }
  +            
  +            throw new java.lang.UnsupportedOperationException( cls.getName() + ":" + object );
  +            
  +        }catch(Throwable t){
  +            // TODO
  +            t.printStackTrace();
  +            throw new RuntimeException( t.getClass().getName() + ":" + t.getMessage() );
  +        }
  +    }
  +        
   }
  
  
  
  1.1                  jakarta-commons-sandbox/simplestore/src/test/org/apache/commons/simplestore/TestPersistent.java
  
  Index: TestPersistent.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.commons.simplestore;
  /**
   * @author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
   *      baliuka@mwm.lt</a>
   * @version $Id: TestPersistent.java,v 1.1 2002/02/09 13:49:47 baliuka Exp $
   */
  public interface TestPersistent {
      
    public boolean getBoolVal();
    
    public void setBoolVal(boolean val);
    
    public void setIntVal(int val);
    
    public int getIntVal();
    
    public void setFloatVal( float val);
    
    public float getFloatVal();
    
    public java.util.Date getDateVal();
    
    public void setDateVal( java.util.Date val );
    
    public void setStrVal( String val );
    
    public String getStrVal( );
  
    //TODO : PesistentObjectVal and unknown SerializableObjectVal
    
  }
  
  
  
  
  1.1                  jakarta-commons-sandbox/simplestore/src/test/org/apache/commons/simplestore/TestSample.java
  
  Index: TestSample.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.commons.simplestore;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import java.util.Map;
  import java.util.HashMap;
  import org.apache.commons.simplestore.persistence.*;
  import org.apache.commons.simplestore.jdbc.*;
  
  /**
   * @author Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
   *      baliuka@mwm.lt</a>
   * @version $Id: TestSample.java,v 1.1 2002/02/09 13:49:47 baliuka Exp $
   */
  public class TestSample extends TestCase {
     
    PersistenceManager pm; 
    
      public TestSample(String testName) {
          super(testName);
      }
      
      public static Test suite() {
          return new TestSuite(TestSample.class);
      }
      
      public static void main(String args[]) {
          String[] testCaseName = { TestSample.class.getName() };
          junit.textui.TestRunner.main(testCaseName);
      }
      
      public void testCreate()throws java.lang.Exception{
       for(int i = 0 ; i < 10; i++ ){
           
          Transaction transaction =  pm.getTransaction();
          
          transaction.begin();
          TestPersistent object1 = (TestPersistent)pm.createInstance(TestPersistent.class);
          Object oid = pm.getOID(object1);
          transaction.commit();
                  
          transaction.begin();
          TestPersistent object2 = (TestPersistent)pm.findInstance(TestPersistent.class,oid);
          transaction.commit();
          
          transaction.begin();
           TestPersistent object3 = (TestPersistent)pm.createInstance(TestPersistent.class);
           object3.setDateVal(new java.util.Date());
           object3.setIntVal(i);
           object3.setFloatVal(i/2);
           object3.setStrVal("" + i);
           object3.setBoolVal(true);
          transaction.commit();
          
          assertEquals("Equals " + oid, object2 , object1 );
          assertTrue(" == " + oid , object2 == object1 );
          
       }
      
      }
      
      public void testRetrieve()throws java.lang.Exception{
      
         Transaction transaction =  pm.getTransaction();
          
          transaction.begin();
           java.util.Set objects = pm.findAll( TestPersistent.class );
           System.out.println("retrieved " + objects.size() );
           java.util.Iterator i = objects.iterator();
           
           while(i.hasNext()){
                
             TestPersistent object = (TestPersistent)i.next();
             System.out.println("ID       =" + object);
             System.out.println("BoolVal  = " + object.getBoolVal());
             System.out.println("DateVal  = "+ object.getDateVal());
             System.out.println("IntVal   = " + object.getIntVal());
             System.out.println("FloatVal =" + object.getFloatVal());
             System.out.println("StrVal   =" + object.getStrVal());
            
           }
          
          transaction.commit();
       
      
      }
      protected void setUp() throws java.lang.Exception {
          super.setUp();
          
          // this datasource is for reentrant connections only !!!
          DriverDataSource ds = new DriverDataSource();
          ds.setDriver("org.hsqldb.jdbcDriver");
          ds.setMaxConnections(1);
          ds.setUrl("jdbc:hsqldb:sample");
          ds.setUser("sa");
          ds.setPassword("");
          String sql = "CREATE TABLE TEST_PERSISTENT ( ID bigint not null primary key, Bool_Val smallint," + 
           " Int_Val integer, Float_Val float, Date_Val date, Str_Val longVarChar )  ";
          
          java.sql.Connection con  = ds.getConnection();
          try{
            java.sql.Statement  stmt = con.createStatement();
           try{ 
            stmt.executeUpdate(sql);
           }catch(java.sql.SQLException e){
               System.out.println(e.getMessage());// exists ? 
           }
            stmt.close();
          }finally {
            con.close();
          } 
          
          DBStorage storage = new DBStorage(ds);
          pm = PersistenceManager.getPersistenceManager(storage,storage);  
      }
      
      
  }
  
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>