db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb OJB.properties
Date Sun, 27 Jun 2004 23:36:23 GMT
arminw      2004/06/27 16:36:23

  Modified:    src/java/org/apache/ojb/broker/metadata/fieldaccess
                        PersistentFieldAutoProxyImplNew.java
               src/test/org/apache/ojb/broker/metadata
                        PersistentFieldTest.java
               src/test/org/apache/ojb OJB.properties
  Added:       src/java/org/apache/ojb/broker/metadata/fieldaccess
                        PersistentFieldDynaBeanImplNew.java
  Log:
  add new DynaBean PF implementation
  add tests for PFDynaBean impl
  
  Revision  Changes    Path
  1.6       +3 -1      db-ojb/src/java/org/apache/ojb/broker/metadata/fieldaccess/PersistentFieldAutoProxyImplNew.java
  
  Index: PersistentFieldAutoProxyImplNew.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/fieldaccess/PersistentFieldAutoProxyImplNew.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- PersistentFieldAutoProxyImplNew.java	27 Jun 2004 15:37:55 -0000	1.5
  +++ PersistentFieldAutoProxyImplNew.java	27 Jun 2004 23:36:23 -0000	1.6
  @@ -30,6 +30,8 @@
    */
   public class PersistentFieldAutoProxyImplNew extends PersistentFieldBase
   {
  +    static final long serialVersionUID = 6286229945422476325L;
  +
       /**
        * Define the number and ordering of the used {@link PersistentField}
        * implementaions. Override this field to add new classes or to change
  @@ -39,7 +41,7 @@
           PersistentFieldDirectAccessImplNew.class
           , PersistentFieldIntrospectorImplNew.class
           , PersistentFieldPrivilegedImplNew.class
  -        , PersistentFieldDynaBeanAccessImpl.class};
  +        , PersistentFieldDynaBeanImplNew.class};
   
       private PersistentField currentPF;
       private ExceptionWrapper latestException;
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/metadata/fieldaccess/PersistentFieldDynaBeanImplNew.java
  
  Index: PersistentFieldDynaBeanImplNew.java
  ===================================================================
  package org.apache.ojb.broker.metadata.fieldaccess;
  
  /* Copyright 2003-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import org.apache.commons.beanutils.DynaBean;
  import org.apache.ojb.broker.PersistenceBrokerException;
  import org.apache.ojb.broker.metadata.MetadataException;
  import org.apache.ojb.broker.util.logging.Logger;
  import org.apache.ojb.broker.util.logging.LoggerFactory;
  
  /**
   * A {@link PersistentField} implementation accesses a property
   * from a {@link org.apache.commons.beanutils.DynaBean}.
   * Note that because of the way that PersistentField works,
   * at run time the type of the field could actually be different, since
   * it depends on the DynaClass of the DynaBean that is given at runtime.
   * <p>
   * This implementation does not support nested fields.
   * </p>
   *
   * @author James Strachan
   * @version $Id: PersistentFieldDynaBeanImplNew.java,v 1.1 2004/06/27 23:36:23 arminw Exp
$
   */
  public class PersistentFieldDynaBeanImplNew extends PersistentFieldBase
  {
      /*
      TODO: Don't know if it is possible to support nested fields with DynaBeans. This
      version does not support nested fields
      */
      private static final long serialVersionUID = 4728858060905429509L;
  
      public PersistentFieldDynaBeanImplNew()
      {
          super();
      }
  
      public PersistentFieldDynaBeanImplNew(Class aPropertyType, String aPropertyName)
      {
          super(aPropertyType, aPropertyName);
          checkNested(aPropertyName);
      }
  
      public void set(Object anObject, Object aValue) throws MetadataException
      {
          if(anObject == null) return;
          if (anObject instanceof DynaBean)
          {
              DynaBean dynaBean = (DynaBean) anObject;
              try
              {
                  dynaBean.set(getName(), aValue);
              }
              catch (Throwable t)
              {
                  String msg = dynaBean.getClass().getName();
                  logSetProblem(anObject, aValue, msg);
                  throw new PersistenceBrokerException(t);
              }
          }
          else
          {
              String msg = "the object is not a DynaBean";
              logSetProblem(anObject, aValue, msg);
              throw new PersistenceBrokerException(msg);
          }
      }
  
      public Object get(Object anObject) throws MetadataException
      {
          if(anObject == null) return null;
          if (anObject instanceof DynaBean)
          {
              DynaBean dynaBean = (DynaBean) anObject;
              try
              {
                  return dynaBean.get(getName());
              }
              catch (Throwable t)
              {
                  String msg = dynaBean.getClass().getName();
                  logGetProblem(anObject, msg);
                  throw new PersistenceBrokerException(t);
              }
          }
          else
          {
              String msg = "the object is not a DynaBean";
              logGetProblem(anObject, msg);
              throw new PersistenceBrokerException(msg);
          }
      }
  
      private void checkNested(String fieldName)
      {
          if(fieldName.indexOf(PATH_TOKEN) > -1)
          {
              throw new MetadataException("This implementation does not support nested fields");
          }
      }
  
      public Class getType()
      {
          return getDeclaringClass();
      }
  
      protected boolean makeAccessible()
      {
          return false;
      }
  
      public boolean usesAccessorsAndMutators()
      {
          return false;
      }
  
      /**
       * Let's give the user some hints as to what could be wrong.
       */
      protected void logSetProblem(Object anObject, Object aValue, String msg)
      {
          Logger logger = LoggerFactory.getDefaultLogger();
          logger.error("Error in operation [set] of object [" + this.getClass().getName()
+ "], " + msg);
          logger.error("Property Name [" + getName() + "]");
          if (anObject instanceof DynaBean)
          {
              DynaBean dynaBean = (DynaBean) anObject;
              logger.error("anObject was DynaClass [" + dynaBean.getDynaClass().getName()
+ "]");
          }
          else if (anObject != null)
          {
              logger.error("anObject was class [" + anObject.getClass().getName() + "]");
          }
          else
          {
              logger.error("anObject was null");
          }
          if (aValue != null)
              logger.error("aValue was class [" + aValue.getClass().getName() + "]");
          else
              logger.error("aValue was null");
      }
  
      /**
       * Let's give the user some hints as to what could be wrong.
       */
      protected void logGetProblem(Object anObject, String msg)
      {
          Logger logger = LoggerFactory.getDefaultLogger();
          logger.error("Error in operation [get of object [" + this.getClass().getName() +
"], " + msg);
          logger.error("Property Name [" + getName() + "]");
          if (anObject instanceof DynaBean)
          {
              DynaBean dynaBean = (DynaBean) anObject;
              logger.error("anObject was DynaClass [" + dynaBean.getDynaClass().getName()
+ "]");
          }
          else if (anObject != null)
          {
              logger.error("anObject was class [" + anObject.getClass().getName() + "]");
          }
          else
          {
              logger.error("anObject was null");
          }
      }
  }
  
  
  
  1.7       +82 -28    db-ojb/src/test/org/apache/ojb/broker/metadata/PersistentFieldTest.java
  
  Index: PersistentFieldTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/metadata/PersistentFieldTest.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- PersistentFieldTest.java	27 Jun 2004 09:56:09 -0000	1.6
  +++ PersistentFieldTest.java	27 Jun 2004 23:36:23 -0000	1.7
  @@ -2,6 +2,10 @@
   
   import java.util.Collection;
   
  +import org.apache.commons.beanutils.BasicDynaClass;
  +import org.apache.commons.beanutils.DynaBean;
  +import org.apache.commons.beanutils.DynaClass;
  +import org.apache.commons.beanutils.DynaProperty;
   import org.apache.commons.lang.SerializationUtils;
   import org.apache.commons.lang.builder.ToStringBuilder;
   import org.apache.commons.lang.builder.ToStringStyle;
  @@ -11,6 +15,7 @@
   import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldDirectAccessImpl;
   import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldDirectAccessImplNew;
   import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldDynaBeanAccessImpl;
  +import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldDynaBeanImplNew;
   import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldIntrospectorImpl;
   import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldIntrospectorImplNew;
   import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldPrivilegedImpl;
  @@ -55,7 +60,8 @@
           , PersistentFieldPrivilegedImplNew.class
           , PersistentFieldAutoProxyImpl.class
           , PersistentFieldAutoProxyImplNew.class
  -        , PersistentFieldDynaBeanAccessImpl.class};
  +        , PersistentFieldDynaBeanAccessImpl.class
  +        , PersistentFieldDynaBeanImplNew.class};
   
       public static void main(String[] args)
       {
  @@ -66,27 +72,27 @@
       protected void setUp() throws Exception
       {
           super.setUp();
  -        oldPFClass = ((OjbConfiguration)OjbConfigurator.getInstance()
  +        oldPFClass = ((OjbConfiguration) OjbConfigurator.getInstance()
                   .getConfigurationFor(null)).getPersistentFieldClass();
       }
   
       protected void tearDown() throws Exception
       {
           super.tearDown();
  -        ((OjbConfiguration)OjbConfigurator.getInstance()
  +        ((OjbConfiguration) OjbConfigurator.getInstance()
                   .getConfigurationFor(null)).setPersistentFieldClass(oldPFClass);
       }
   
       private void runFieldTestsFor(Class targetClass, boolean supportJavaBeanNames) throws
Exception
       {
  -        ((OjbConfiguration)OjbConfigurator.getInstance().getConfigurationFor(null)).setPersistentFieldClass(targetClass);
  +        ((OjbConfiguration) OjbConfigurator.getInstance().getConfigurationFor(null)).setPersistentFieldClass(targetClass);
   
           PersistentField pfNM_Name = newInstance(targetClass, NestedMain.class, NESTED_MAIN_NAME);
           PersistentField pfNDD_RD = newInstance(targetClass, NestedMain.class, NESTED_DETAIL_DETAIL_REAL_DETAIL);
           PersistentField pfNDD_RDD = newInstance(targetClass, NestedMain.class, NESTED_DETAIL_DETAIL_REAL_DESCRIPTION);
           PersistentField pfND_MJB = null;
           PersistentField pfNE_Name = null;
  -        if(supportJavaBeanNames)
  +        if (supportJavaBeanNames)
           {
               pfND_MJB = newInstance(targetClass, NestedMain.class, NESTED_DETAIL_MORE_JAVA_BEAN);
               pfNE_Name = newInstance(targetClass, NestedMain.class, NESTED_ENTRY_NAME);
  @@ -101,7 +107,7 @@
           result = pfNDD_RDD.get(nm);
           assertEquals(NESTED_DETAIL_DETAIL_REAL_DESCRIPTION_VALUE, result);
   
  -        if(supportJavaBeanNames)
  +        if (supportJavaBeanNames)
           {
               result = pfND_MJB.get(nm);
               assertEquals(NESTED_DETAIL_MORE_JAVA_BEAN_VALUE, result);
  @@ -114,14 +120,14 @@
           pfNM_Name.set(newNM, NESTED_MAIN_NAME_VALUE);
           pfNDD_RD.set(newNM, NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE);
           result = pfNDD_RDD.get(newNM);
  -        assertEquals(NESTED_DETAIL_DETAIL_REAL_DESCRIPTION_VALUE , result);
  +        assertEquals(NESTED_DETAIL_DETAIL_REAL_DESCRIPTION_VALUE, result);
   
           result = pfNM_Name.get(newNM);
           assertEquals(NESTED_MAIN_NAME_VALUE, result);
           result = pfNDD_RD.get(newNM);
  -        assertEquals(NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE , result);
  +        assertEquals(NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE, result);
   
  -        if(supportJavaBeanNames)
  +        if (supportJavaBeanNames)
           {
               pfND_MJB.set(newNM, NESTED_DETAIL_MORE_JAVA_BEAN_VALUE);
               pfNE_Name.set(newNM, NESTED_ENTRY_NAME_VALUE);
  @@ -135,7 +141,7 @@
           pfNM_Name = (PersistentField) SerializationUtils.deserialize(SerializationUtils.serialize(pfNM_Name));
           pfNDD_RD = (PersistentField) SerializationUtils.deserialize(SerializationUtils.serialize(pfNDD_RD));
           pfNDD_RDD = (PersistentField) SerializationUtils.deserialize(SerializationUtils.serialize(pfNDD_RDD));
  -        if(supportJavaBeanNames)
  +        if (supportJavaBeanNames)
           {
               pfND_MJB = (PersistentField) SerializationUtils.deserialize(SerializationUtils.serialize(pfND_MJB));
               pfNE_Name = (PersistentField) SerializationUtils.deserialize(SerializationUtils.serialize(pfNE_Name));
  @@ -150,7 +156,7 @@
           result = pfNDD_RDD.get(nm);
           assertEquals(NESTED_DETAIL_DETAIL_REAL_DESCRIPTION_VALUE, result);
   
  -        if(supportJavaBeanNames)
  +        if (supportJavaBeanNames)
           {
               result = pfND_MJB.get(nm);
               assertEquals(NESTED_DETAIL_MORE_JAVA_BEAN_VALUE, result);
  @@ -163,14 +169,14 @@
           pfNM_Name.set(newNM, NESTED_MAIN_NAME_VALUE);
           pfNDD_RD.set(newNM, NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE);
           result = pfNDD_RDD.get(newNM);
  -        assertEquals(NESTED_DETAIL_DETAIL_REAL_DESCRIPTION_VALUE , result);
  +        assertEquals(NESTED_DETAIL_DETAIL_REAL_DESCRIPTION_VALUE, result);
   
           result = pfNM_Name.get(newNM);
           assertEquals(NESTED_MAIN_NAME_VALUE, result);
           result = pfNDD_RD.get(newNM);
  -        assertEquals(NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE , result);
  +        assertEquals(NESTED_DETAIL_DETAIL_REAL_DETAIL_VALUE, result);
   
  -        if(supportJavaBeanNames)
  +        if (supportJavaBeanNames)
           {
               pfND_MJB.set(newNM, NESTED_DETAIL_MORE_JAVA_BEAN_VALUE);
               pfNE_Name.set(newNM, NESTED_ENTRY_NAME_VALUE);
  @@ -183,18 +189,26 @@
   
       private void checkBoundaryConditions(Class targetClass) throws Exception
       {
  +        checkBoundaryConditions(targetClass, true);
  +    }
  +
  +    private void checkBoundaryConditions(Class targetClass, boolean withNested) throws
Exception
  +    {
           PersistentField pf = newInstance(targetClass, NestedMain.class, NESTED_MAIN_NAME);
  -        PersistentField pf_2 = newInstance(targetClass, NestedMain.class, NESTED_DETAIL_DETAIL_REAL_DETAIL);
           pf.get(null);
           pf.set(null, null);
  -        pf_2.get(null);
  -        pf_2.set(null, null);
           pf = newInstance(targetClass, NestedMain.class, NESTED_MAIN_NAME);
  -        pf_2 = newInstance(targetClass, NestedMain.class, NESTED_DETAIL_DETAIL_REAL_DETAIL);
           pf.get(null);
           pf.set(null, "kkddk");
  -        pf_2.get(null);
  -        pf_2.set(null, "gkfgfg");
  +        if(withNested)
  +        {
  +            PersistentField pf_2 = newInstance(targetClass, NestedMain.class, NESTED_DETAIL_DETAIL_REAL_DETAIL);
  +            pf_2.get(null);
  +            pf_2.set(null, null);
  +            pf_2 = newInstance(targetClass, NestedMain.class, NESTED_DETAIL_DETAIL_REAL_DETAIL);
  +            pf_2.get(null);
  +            pf_2.set(null, "gkfgfg");
  +        }
       }
   
       public void testDirectAccess() throws Exception
  @@ -245,13 +259,43 @@
           checkBoundaryConditions(PersistentFieldAutoProxyImplNew.class);
       }
   
  -//    public void testDynaBean() throws Exception
  -//    {
  -//        // TODO: need a test for dynaBean
  -//        if(ojbSkipKnownIssueProblem()) return;
  -//        // not implemented
  -//        throw new UnsupportedOperationException("Don't panic! Test only needs implementation");
  -//    }
  +    public void testDynaBean() throws Exception
  +    {
  +        checkBoundaryConditions(PersistentFieldDynaBeanAccessImpl.class);
  +
  +        DynaClass dynaClass = createDynaClass();
  +        DynaBean bean = dynaClass.newInstance();
  +        bean.set("name", "testDynaBean");
  +
  +        PersistentField pf = new PersistentFieldDynaBeanAccessImpl(String.class, "name");
  +        String result = (String) pf.get(bean);
  +        assertNotNull(result);
  +        assertEquals("testDynaBean", result);
  +
  +        pf.set(bean, "XXXX");
  +        result = (String) pf.get(bean);
  +        assertNotNull(result);
  +        assertEquals("XXXX", result);
  +    }
  +
  +    public void testDynaBeanNew() throws Exception
  +    {
  +        checkBoundaryConditions(PersistentFieldDynaBeanImplNew.class, false);
  +
  +        DynaClass dynaClass = createDynaClass();
  +        DynaBean bean = dynaClass.newInstance();
  +        bean.set("name", "testDynaBean");
  +
  +        PersistentField pf = new PersistentFieldDynaBeanAccessImpl(String.class, "name");
  +        String result = (String) pf.get(bean);
  +        assertNotNull(result);
  +        assertEquals("testDynaBean", result);
  +
  +        pf.set(bean, "XXXX");
  +        result = (String) pf.get(bean);
  +        assertNotNull(result);
  +        assertEquals("XXXX", result);
  +    }
   
   
   
  @@ -276,6 +320,16 @@
           Class[] types = new Class[]{Class.class, String.class};
           Object[] args = new Object[]{testClass, fieldName};
           return (PersistentField) ClassHelper.newInstance(pfClass, types, args);
  +    }
  +
  +    protected DynaClass createDynaClass()
  +    {
  +        DynaClass dynaClass = new BasicDynaClass
  +                ("TestDynaClass", null,
  +                        new DynaProperty[]{
  +                            new DynaProperty("name", String.class),
  +                        });
  +        return (dynaClass);
       }
   
   
  
  
  
  1.75      +3 -2      db-ojb/src/test/org/apache/ojb/OJB.properties
  
  Index: OJB.properties
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/OJB.properties,v
  retrieving revision 1.74
  retrieving revision 1.75
  diff -u -r1.74 -r1.75
  --- OJB.properties	27 Jun 2004 13:50:33 -0000	1.74
  +++ OJB.properties	27 Jun 2004 23:36:23 -0000	1.75
  @@ -407,7 +407,8 @@
   #PersistentFieldClass=org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldPrivilegedImplNew
   #PersistentFieldClass=org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldIntrospectorImplNew
   #PersistentFieldClass=org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldAutoProxyImpl
  -#
  +#PersistentFieldClass=org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldDynaBeanImplNew
  +#(DynaBean implementation does not support nested fields)
   #
   #----------------------------------------------------------------------------------------
   # Component Intercepting for Profiling and Tracing
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message