db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bri...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/broker/dirty DirtyTest.java
Date Thu, 08 Jul 2004 00:47:05 GMT
brianm      2004/07/07 17:47:05

  Modified:    src/java/org/apache/ojb/broker/dirty
                        DirtyPersistenceBrokerFactory.java Entry.java
                        LifeCycleListener.java StateListener.java
                        Tracker.java
               src/test/org/apache/ojb repository_junit.xml
               src/test/org/apache/ojb/broker AllTests.java
               src/test/org/apache/ojb/broker/dirty DirtyTest.java
  Added:       src/java/org/apache/ojb/broker/dirty ManagedEntry.java
                        PersistenceAware.java PersistenceAwareEntry.java
                        StateReporter.java StateReporterImpl.java
               src/test/org/apache/ojb/broker PersistenceAwarePerson.java
  Log:
  Add the ability for a persistent class to handle its own state management.
  
  Push persistence operations out to the Entry class, making better use of polymorphism
  between the different ways of managing state.
  
  Provide the hooks needed for spefic field updates as soon as we support it.
  
  Revision  Changes    Path
  1.2       +1 -1      db-ojb/src/java/org/apache/ojb/broker/dirty/DirtyPersistenceBrokerFactory.java
  
  Index: DirtyPersistenceBrokerFactory.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/dirty/DirtyPersistenceBrokerFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DirtyPersistenceBrokerFactory.java	6 Jul 2004 12:45:43 -0000	1.1
  +++ DirtyPersistenceBrokerFactory.java	8 Jul 2004 00:47:04 -0000	1.2
  @@ -26,7 +26,7 @@
   
   /**
    * Provides PersistenceBroker instances configured to do
  - * dirty checking on commit.
  + * dirty checking on prepare.
    * <p>
    * Behavior is iffy on rollback, but database will be clean
    */
  
  
  
  1.2       +16 -76    db-ojb/src/java/org/apache/ojb/broker/dirty/Entry.java
  
  Index: Entry.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/dirty/Entry.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Entry.java	6 Jul 2004 12:45:43 -0000	1.1
  +++ Entry.java	8 Jul 2004 00:47:04 -0000	1.2
  @@ -1,81 +1,21 @@
  -/* Copyright 2004-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.
  - */
   package org.apache.ojb.broker.dirty;
   
  -import org.apache.ojb.otm.states.State;
  -import org.apache.ojb.broker.metadata.MetadataManager;
  -import org.apache.ojb.broker.metadata.ClassDescriptor;
  -import org.apache.ojb.broker.metadata.FieldDescriptor;
  -import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
  +import org.apache.ojb.broker.PersistenceBroker;
   
  -public class Entry
  +/**
  + * Interface to provide polymorphism between the different ways of handling
  + * persistent objects.
  + */
  +interface Entry
   {
  -    private State state;
  -    private Object[] fields;
  -
  -    public Entry(Object original, State state)
  -    {
  -
  -        MetadataManager man = MetadataManager.getInstance();
  -        ClassDescriptor cd = man.getRepository().getDescriptorFor(original.getClass());
  -        FieldDescriptor[] fds = cd.getFieldDescriptions();
  -        fields = new Object[fds.length];
  -        for (int i = 0; i < fds.length; i++)
  -        {
  -            FieldDescriptor fd = fds[i];
  -            PersistentField pd = fd.getPersistentField();
  -            fields[i] = pd.get(original);
  -        }
  -        this.state = state;
  -    }
  -
  -    public boolean changed(Object key)
  -    {
  -        MetadataManager man = MetadataManager.getInstance();
  -        ClassDescriptor cd = man.getRepository().getDescriptorFor(key.getClass());
  -        FieldDescriptor[] fds = cd.getFieldDescriptions();
  -        for (int i = 0; i < fds.length; i++)
  -        {
  -            FieldDescriptor fd = fds[i];
  -            PersistentField pd = fd.getPersistentField();
  -            if (fields[i] == null && pd.get(key) == null) continue;
  -            if (fields[i] == null && pd.get(key) != null) return true;
  -            if (!fields[i].equals(pd.get(key))
  -                && ( this.state == State.PERSISTENT_CLEAN || this.state == State.PERSISTENT_NEW))
return true;
  -        }
  -        return false;
  -    }
  -
  -    public void restore(Object key)
  -    {
  -        MetadataManager man = MetadataManager.getInstance();
  -        ClassDescriptor cd = man.getRepository().getDescriptorFor(key.getClass());
  -        FieldDescriptor[] fds = cd.getFieldDescriptions();
  -        for (int i = 0; i < fds.length; i++)
  -        {
  -            FieldDescriptor fd = fds[i];
  -            PersistentField pd = fd.getPersistentField();
  -            if (fields[i] == null && pd.get(key) == null) continue;
  -            if (fields[i] == null && pd.get(key) != null)
  -            {
  -                pd.set(key, null);
  -            }
  -            if (!fields[i].equals(pd.get(key)))
  -            {
  -                pd.set(key, fields[i]);
  -            }
  -        }
  -    }
  +    /**
  +     * prepare for commit (typically store() changed attributes to the pb provided)
  +     * @param broker PB about to commit
  +     */
  +    void prepare(PersistenceBroker broker);
  +
  +    /**
  +     * Restore changed attributes after rollback
  +     */
  +    void restore();
   }
  
  
  
  1.2       +1 -1      db-ojb/src/java/org/apache/ojb/broker/dirty/LifeCycleListener.java
  
  Index: LifeCycleListener.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/dirty/LifeCycleListener.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- LifeCycleListener.java	6 Jul 2004 12:45:43 -0000	1.1
  +++ LifeCycleListener.java	8 Jul 2004 00:47:04 -0000	1.2
  @@ -18,7 +18,7 @@
   import org.apache.ojb.broker.PBLifeCycleEvent;
   import org.apache.ojb.broker.PersistenceBrokerException;
   
  -public class LifeCycleListener implements PBLifeCycleListener
  +class LifeCycleListener implements PBLifeCycleListener
   {
       private Tracker tracker;
   
  
  
  
  1.2       +5 -4      db-ojb/src/java/org/apache/ojb/broker/dirty/StateListener.java
  
  Index: StateListener.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/dirty/StateListener.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- StateListener.java	6 Jul 2004 12:45:43 -0000	1.1
  +++ StateListener.java	8 Jul 2004 00:47:04 -0000	1.2
  @@ -17,7 +17,7 @@
   import org.apache.ojb.broker.PBStateListener;
   import org.apache.ojb.broker.PBStateEvent;
   
  -public class StateListener implements PBStateListener
  +class StateListener implements PBStateListener
   {
       private Tracker tracker;
   
  @@ -44,15 +44,15 @@
       }
   
       /**
  -     * Called before a <tt>PersistenceBroker commit</tt> was called.
  +     * Called before a <tt>PersistenceBroker prepare</tt> was called.
        */
       public void beforeCommit(PBStateEvent event)
       {
  -        tracker.commit(event.getTriggeringBroker());
  +        tracker.prepare(event.getTriggeringBroker());
       }
   
       /**
  -     * Called after a <tt>PersistenceBroker commit</tt> was called.
  +     * Called after a <tt>PersistenceBroker prepare</tt> was called.
        */
       public void afterCommit(PBStateEvent event)
       {
  @@ -70,6 +70,7 @@
        */
       public void afterRollback(PBStateEvent event)
       {
  +        tracker.rollback();
       }
   
       /**
  
  
  
  1.2       +49 -28    db-ojb/src/java/org/apache/ojb/broker/dirty/Tracker.java
  
  Index: Tracker.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/dirty/Tracker.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Tracker.java	6 Jul 2004 12:45:43 -0000	1.1
  +++ Tracker.java	8 Jul 2004 00:47:04 -0000	1.2
  @@ -18,13 +18,15 @@
   import org.apache.ojb.broker.core.proxy.ProxyHelper;
   import org.apache.ojb.otm.states.State;
   
  +import java.util.HashSet;
   import java.util.Iterator;
  -import java.util.Map;
  -import java.util.IdentityHashMap;
   
  -public class Tracker
  +/**
  + * Coordinator for persistent state tracking stuff
  + */
  +class Tracker
   {
  -    private final IdentityHashMap map = new IdentityHashMap();
  +    private final HashSet dirties = new HashSet();
       private boolean flushing = false;
       private boolean transactional = true;
   
  @@ -33,54 +35,46 @@
        */
       public void begin()
       {
  -        map.clear();
           flushing = false;
           transactional = true;
  +        dirties.clear();
       }
   
       public void rollback()
       {
  -        for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();)
  +        transactional = false;
  +        for (Iterator iterator = dirties.iterator(); iterator.hasNext();)
           {
  -            Map.Entry map_entry = (Map.Entry) iterator.next();
  -            Object key = map_entry.getKey();
  -            Entry entry = (Entry) map_entry.getValue();
  -            entry.restore(key);
  +            Entry entry = (Entry) iterator.next();
  +            entry.restore();
           }
  -        map.clear();
  -        transactional = false;
  +        dirties.clear();
       }
   
       /**
  -     * About to commit, flush dirty objects!
  +     * About to prepare, flush dirty objects!
        */
  -    public void commit(PersistenceBroker broker)
  +    public void prepare(PersistenceBroker broker)
       {
           flushing = true;
  -        for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();)
  +        for (Iterator iterator = dirties.iterator(); iterator.hasNext();)
           {
  -            Map.Entry map_entry = (Map.Entry) iterator.next();
  -            Object key = map_entry.getKey();
  -            Entry entry = (Entry) map_entry.getValue();
  -            if (entry.changed(key))
  -            {
  -                broker.store(key);
  -            }
  +            Entry entry = (Entry) iterator.next();
  +            entry.prepare(broker);
           }
  -        map.clear();
           flushing = false;
           transactional = false;
       }
   
       /**
        * Called on explicit update. Set target as the
  -     * current instance in entity map, and make it
  +     * current instance in entity managed, and make it
        * persistentclean
        */
       public void flushed(Object target)
       {
           if (flushing || !transactional) return;
  -        map.put(target, new Entry(target, State.PERSISTENT_CLEAN));
  +        this.track(target);
       }
   
       /**
  @@ -89,7 +83,7 @@
       public void inserted(Object source)
       {
           if (!transactional) return;
  -        map.put(source, new Entry(source, State.PERSISTENT_NEW));
  +        track(source);
       }
   
       /**
  @@ -98,7 +92,6 @@
       public void deleted(Object source)
       {
           if (!transactional) return;
  -        map.put(source, new Entry(source, State.PERSISTENT_DELETED));
       }
   
       /**
  @@ -107,7 +100,35 @@
       public void fetched(Object source)
       {
           if (!transactional) return;
  +        this.track(source);
  +    }
  +
  +    private void track(Object source)
  +    {
           if (ProxyHelper.isProxy(source)) return;
  -        map.put(source, new Entry(source, State.PERSISTENT_CLEAN));
  +        if (source instanceof PersistenceAware)
  +        {
  +            PersistenceAware aware = (PersistenceAware) source;
  +            aware.setStateReporter(new StateReporterImpl(this, new PersistenceAwareEntry(aware)));
  +        }
  +        else
  +        {
  +            dirties.add(new ManagedEntry(source));
  +        }
  +    }
  +
  +    void makeDirty(Entry entry)
  +    {
  +        this.dirties.add(entry);
  +    }
  +
  +    void makeClean(Entry entry)
  +    {
  +        this.dirties.remove(entry);
  +    }
  +
  +    public boolean isTransactional()
  +    {
  +        return transactional;
       }
   }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/dirty/ManagedEntry.java
  
  Index: ManagedEntry.java
  ===================================================================
  /* Copyright 2004-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.
   */
  package org.apache.ojb.broker.dirty;
  
  import org.apache.ojb.otm.states.State;
  import org.apache.ojb.broker.metadata.MetadataManager;
  import org.apache.ojb.broker.metadata.ClassDescriptor;
  import org.apache.ojb.broker.metadata.FieldDescriptor;
  import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
  import org.apache.ojb.broker.PersistenceBroker;
  
  /**
   * Provide copy-on-read and compare-on-commit style state management for
   * persistent types which are not aware of transactions.
   * <p>
   * Will always be registered as dirty, but will not update the
   * persistent instance on prepare(..) if the instance is clean
   */
  class ManagedEntry implements Entry
  {
      private Object[] fields;
      private Object original;
  
      public ManagedEntry(Object original)
      {
          this.original = original;
          MetadataManager man = MetadataManager.getInstance();
          ClassDescriptor cd = man.getRepository().getDescriptorFor(original.getClass());
          FieldDescriptor[] fds = cd.getFieldDescriptions();
          fields = new Object[fds.length];
          for (int i = 0; i < fds.length; i++)
          {
              FieldDescriptor fd = fds[i];
              PersistentField pd = fd.getPersistentField();
              fields[i] = pd.get(original);
          }
      }
  
      public void prepare(PersistenceBroker broker)
      {
          MetadataManager man = MetadataManager.getInstance();
          ClassDescriptor cd = man.getRepository().getDescriptorFor(original.getClass());
          FieldDescriptor[] fds = cd.getFieldDescriptions();
          boolean save = false;
          for (int i = 0; i < fds.length; i++)
          {
              FieldDescriptor fd = fds[i];
              PersistentField pd = fd.getPersistentField();
              if (fields[i] == null && pd.get(original) == null) continue;
              if (fields[i] == null && pd.get(original) != null)
              {
                  save =  true;
                  break;
              }
              if (!fields[i].equals(pd.get(original)))
              {
                  save = true;
                  break;
              }
          }
          if (save)
          {
              broker.store(original);
          }
      }
  
      public void restore()
      {
          MetadataManager man = MetadataManager.getInstance();
          ClassDescriptor cd = man.getRepository().getDescriptorFor(original.getClass());
          FieldDescriptor[] fds = cd.getFieldDescriptions();
          for (int i = 0; i < fds.length; i++)
          {
              FieldDescriptor fd = fds[i];
              PersistentField pd = fd.getPersistentField();
              if (fields[i] == null && pd.get(original) == null) continue;
              if (fields[i] == null && pd.get(original) != null)
              {
                  pd.set(original, null);
              }
              if (!fields[i].equals(pd.get(original)))
              {
                  pd.set(original, fields[i]);
              }
          }
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/dirty/PersistenceAware.java
  
  Index: PersistenceAware.java
  ===================================================================
  /* Copyright 2004-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.
   */
  package org.apache.ojb.broker.dirty;
  
  /**
   * Contract a class can implement in order to manage its own clean/dirty state on a
   * field by field basis. Implementing classes will be given a <code>StateReporter</code>
   * instance when they are made persistent, or retrieved from the datastore.
   * <p>
   * They are also responsible for restoring their state on transaction rollbacks, if
   * they choose to. They will be notified via the <code>restore</code> method
   *
   * @see StateReporter
   */
  public interface PersistenceAware
  {
      /**
       * Provides a callback receiver to which persistent field state changes should be reported
to.
       * <p>
       * Also provides a lookup method to determine if a transaction is in progress
       *
       * @param reporter instance to notify of persistent field changes
       */
      public void setStateReporter(StateReporter reporter);
  
      /**
       * Called by OJB when a transaction is aborted in order to allow
       * the <code>PersistenceAware</code> instance to rollback its state
       */
      void restore();
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/dirty/PersistenceAwareEntry.java
  
  Index: PersistenceAwareEntry.java
  ===================================================================
  /* Copyright 2004-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.
   */
  package org.apache.ojb.broker.dirty;
  
  import org.apache.ojb.broker.PersistenceBroker;
  
  class PersistenceAwareEntry implements Entry
  {
      private PersistenceAware aware;
      private boolean dirty = false;
  
      PersistenceAwareEntry(PersistenceAware aware)
      {
  
          this.aware = aware;
      }
      public void prepare(PersistenceBroker broker)
      {
          if (dirty)
          {
              broker.store(aware);
          }
      }
  
      public void restore()
      {
          aware.restore();
      }
  
      public void makeDirty()
      {
          dirty = true;
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/dirty/StateReporter.java
  
  Index: StateReporter.java
  ===================================================================
  /* Copyright 2004-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.
   */
  package org.apache.ojb.broker.dirty;
  
  /**
   * Instances of this class will be provided to <code>PersistenceAware</code>
   * classes when they are persisted or are retrieved from the datastore.
   * <p>
   * This class provides hooks for the instance to specify persistent field state
   * and the ability to query for transaction status.
   *
   * @see PersistenceAware
   */
  public interface StateReporter
  {
      boolean isTransactional();
  
      void makeDirty(String field);
  
      void makeClean(String field);
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/dirty/StateReporterImpl.java
  
  Index: StateReporterImpl.java
  ===================================================================
  /* Copyright 2004-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.
   */
  package org.apache.ojb.broker.dirty;
  
  class StateReporterImpl implements StateReporter
  {
      private Tracker tracker;
      private PersistenceAwareEntry entry;
      private boolean dirty = false;
  
      StateReporterImpl(Tracker tracker, PersistenceAwareEntry entry)
      {
          this.tracker = tracker;
          this.entry = entry;
      }
  
      public boolean isTransactional()
      {
          return tracker.isTransactional();
      }
  
      public void makeDirty(String field)
      {
          if (!dirty)
          {
              tracker.makeDirty(entry);
              entry.makeDirty();
              dirty = true;
          }
      }
  
      public void makeClean(String field)
      {
          if (dirty)
          {
              tracker.makeClean(entry);
              dirty = false;
          }
      }
  }
  
  
  
  1.115     +43 -1     db-ojb/src/test/org/apache/ojb/repository_junit.xml
  
  Index: repository_junit.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/repository_junit.xml,v
  retrieving revision 1.114
  retrieving revision 1.115
  diff -u -r1.114 -r1.115
  --- repository_junit.xml	3 Jul 2004 23:32:16 -0000	1.114
  +++ repository_junit.xml	8 Jul 2004 00:47:04 -0000	1.115
  @@ -920,6 +920,48 @@
         </collection-descriptor>
      </class-descriptor>
   
  +<class-descriptor
  +   	  class="org.apache.ojb.broker.PersistenceAwarePerson"
  +   	  table="PERSON"
  +   >
  +      <field-descriptor
  +         name="id"
  +         column="ID"
  +         jdbc-type="INTEGER"
  +         primarykey="true"
  +         autoincrement="true"
  +      />
  +      <field-descriptor
  +         name="firstname"
  +         column="FIRSTNAME"
  +         jdbc-type="VARCHAR"
  +      />
  +      <field-descriptor
  +         name="lastname"
  +         column="LASTNAME"
  +         jdbc-type="VARCHAR"
  +      />
  +      <collection-descriptor
  +         name="roles"
  +         element-class-ref="org.apache.ojb.broker.Role"
  +         auto-delete="true"
  +         proxy="true"
  +         auto-retrieve="true"
  +      >
  +         <inverse-foreignkey field-ref="person_id"/>
  +      </collection-descriptor>
  +      <collection-descriptor
  +         name="projects"
  +         element-class-ref="org.apache.ojb.broker.Project"
  +         auto-retrieve="true"
  +         auto-update="true"
  +         indirection-table="PERSON_PROJECT"
  +      >
  +         <fk-pointing-to-this-class column="PERSON_ID"/>
  +         <fk-pointing-to-element-class column="PROJECT_ID"/>
  +      </collection-descriptor>
  +   </class-descriptor>
  +
   <!-- Definitions for org.apache.ojb.broker.PersonWithArray -->
      <class-descriptor
      	  class="org.apache.ojb.broker.PersonWithArray"
  
  
  
  1.50      +2 -0      db-ojb/src/test/org/apache/ojb/broker/AllTests.java
  
  Index: AllTests.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/AllTests.java,v
  retrieving revision 1.49
  retrieving revision 1.50
  diff -u -r1.49 -r1.50
  --- AllTests.java	26 Jun 2004 00:22:25 -0000	1.49
  +++ AllTests.java	8 Jul 2004 00:47:04 -0000	1.50
  @@ -16,6 +16,7 @@
   import org.apache.ojb.broker.sequence.NativeIdentifierTest;
   import org.apache.ojb.broker.sequence.SMMultiThreadedTest;
   import org.apache.ojb.broker.sequence.SequenceManagerTest;
  +import org.apache.ojb.broker.dirty.DirtyTest;
   
   /**
    * the facade to all TestCases in this package.
  @@ -99,6 +100,7 @@
           suite.addTestSuite(PrimaryKeyForeignKeyTest.class);
           suite.addTestSuite(M2NGraphTest.class);
           suite.addTestSuite(PersistentFieldTest.class);
  +        suite.addTest(new TestSuite(DirtyTest.class));
           return suite;
       }
   
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/broker/PersistenceAwarePerson.java
  
  Index: PersistenceAwarePerson.java
  ===================================================================
  package org.apache.ojb.broker;
  
  import org.apache.ojb.broker.dirty.PersistenceAware;
  import org.apache.ojb.broker.dirty.StateReporter;
  
  public class PersistenceAwarePerson extends Person implements PersistenceAware
  {
      private StateReporter reporter;
      private String firstNameCopy = null;
      private String lastNameCopy = null;
  
      public void setStateReporter(StateReporter reporter)
      {
          this.reporter = reporter;
      }
  
      public void restore()
      {
          if (lastNameCopy != null) this.setLastname(lastNameCopy);
          if (firstNameCopy != null) this.setFirstname(firstNameCopy);
          firstNameCopy = null;
          lastNameCopy = null;
      }
  
      public void setLastname(String lastname)
      {
          this.lastNameCopy = this.getLastname();
          super.setLastname(lastname);
          if (reporter != null) reporter.makeDirty("lastName");
      }
  
      public void setFirstname(String firstname)
      {
          this.firstNameCopy = this.getFirstname();
          super.setFirstname(firstname);
          if (reporter != null) reporter.makeDirty("firstName");
      }
  
  }
  
  
  
  1.2       +72 -0     db-ojb/src/test/org/apache/ojb/broker/dirty/DirtyTest.java
  
  Index: DirtyTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/dirty/DirtyTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DirtyTest.java	6 Jul 2004 12:45:43 -0000	1.1
  +++ DirtyTest.java	8 Jul 2004 00:47:05 -0000	1.2
  @@ -7,6 +7,7 @@
   import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.Person;
   import org.apache.ojb.broker.ProductGroupWithCollectionProxy;
  +import org.apache.ojb.broker.PersistenceAwarePerson;
   import org.apache.ojb.broker.core.proxy.CollectionProxy;
   import org.apache.ojb.broker.core.proxy.ProxyHelper;
   
  @@ -88,6 +89,77 @@
   
           a = (Article) pb.getObjectByIdentity(new Identity(a, pb));
           assertEquals("new name", a.getArticleName());
  +
  +        pb.close();
  +    }
  +
  +    public void testPersistenceAware()
  +    {
  +        PersistenceAwarePerson p = new PersistenceAwarePerson();
  +        p.setFirstname("brian");
  +        PersistenceBroker pb = DirtyPersistenceBrokerFactory.defaultPersistenceBroker();
  +        pb.store(p);
  +
  +        pb.clearCache();
  +
  +        Identity id = new Identity(p, pb);
  +        pb.beginTransaction();
  +        PersistenceAwarePerson same = (PersistenceAwarePerson) pb.getObjectByIdentity(id);
  +        assertEquals("brian", same.getFirstname());
  +        same.setFirstname("Eric");
  +        pb.commitTransaction();
  +
  +        pb.clearCache();
  +
  +        same = (PersistenceAwarePerson) pb.getObjectByIdentity(id);
  +        assertEquals("Eric", same.getFirstname());
  +        pb.close();
  +    }
  +
  +    public void testAwareRestore()
  +    {
  +        PersistenceAwarePerson p = new PersistenceAwarePerson();
  +        p.setFirstname("brian");
  +        PersistenceBroker pb = DirtyPersistenceBrokerFactory.defaultPersistenceBroker();
  +        pb.store(p);
  +
  +        pb.clearCache();
  +
  +        pb.beginTransaction();
  +        p = (PersistenceAwarePerson) pb.getObjectByIdentity(new Identity(p, pb));
  +        p.setFirstname("Doug");
  +        pb.abortTransaction();
  +
  +        assertEquals("brian", p.getFirstname());
  +
  +        pb.clearCache();
  +
  +        p = (PersistenceAwarePerson) pb.getObjectByIdentity(new Identity(p, pb));
  +        assertEquals("brian", p.getFirstname());
  +
  +        pb.close();
  +    }
  +
  +    public void testManagedRestore()
  +    {
  +        Person p = new Person();
  +        p.setFirstname("brian");
  +        PersistenceBroker pb = DirtyPersistenceBrokerFactory.defaultPersistenceBroker();
  +        pb.store(p);
  +
  +        pb.clearCache();
  +
  +        pb.beginTransaction();
  +        p = (Person) pb.getObjectByIdentity(new Identity(p, pb));
  +        p.setFirstname("Doug");
  +        pb.abortTransaction();
  +
  +        assertEquals("brian", p.getFirstname());
  +
  +        pb.clearCache();
  +
  +        p = (Person) pb.getObjectByIdentity(new Identity(p, pb));
  +        assertEquals("brian", p.getFirstname());
   
           pb.close();
       }
  
  
  

---------------------------------------------------------------------
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