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/dirty OptimisticTransactionTest.java AllTests.java DirtyBrokerTest.java UnitTest.java
Date Tue, 24 Aug 2004 02:18:38 GMT
brianm      2004/08/23 19:18:38

  Modified:    src/java/org/apache/ojb/dirty BrokerStateMonitor.java
                        DirtyBrokerFactory.java DirtyPersistenceBroker.java
                        ObjectRegistrationListener.java
                        UnitFlushListener.java
               src/test/org/apache/ojb/dirty AllTests.java
                        DirtyBrokerTest.java UnitTest.java
  Added:       src/java/org/apache/ojb/dirty/grafolia
                        OJBTrackerFactory.java
               src/java/org/apache/ojb/dirty/ops Change.java
                        ChangeList.java Delete.java Insert.java Update.java
               src/java/org/apache/ojb/dirty DelegatingDirtyBroker.java
                        ObjectStateChangeListener.java
                        ObjectTransaction.java UnitObjectTransaction.java
               src/test/org/apache/ojb/dirty OptimisticTransactionTest.java
  Removed:     src/java/org/apache/ojb/dirty Change.java ChangeList.java
                        Insert.java UnitAdaptor.java Update.java
  Log:
  * Lots of refactoring on the dirty broker classes.
  * Started adding support for explicit optimistic transactions
  * Using OJB specific facades in front of the Grafolia libraries (ObjectTransaction in front of Unit)
  * Starting to implement OJB specific factories for Grafolia unit
  
  Revision  Changes    Path
  1.1                  db-ojb/src/java/org/apache/ojb/dirty/grafolia/OJBTrackerFactory.java
  
  Index: OJBTrackerFactory.java
  ===================================================================
  /* Copyright 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.dirty.grafolia;
  
  import org.apache.commons.grafolia.TrackerFactory;
  import org.apache.commons.grafolia.StateTracker;
  import org.apache.commons.grafolia.State;
  import org.apache.commons.grafolia.managed.ManagedTrackerFactory;
  
  public class OJBTrackerFactory implements TrackerFactory
  {
      private final TrackerFactory delegate;
  
      public OJBTrackerFactory()
      {
          delegate = new ManagedTrackerFactory();
      }
  
      public StateTracker build(final Object instance, final State initial)
      {
          return delegate.build(instance, initial);
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/dirty/ops/Change.java
  
  Index: Change.java
  ===================================================================
  /* Copyright 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.dirty.ops;
  
  import org.apache.ojb.broker.PersistenceBroker;
  
  public interface Change
  {
      Object getObject();
  
      void execute(PersistenceBroker broker);
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/dirty/ops/ChangeList.java
  
  Index: ChangeList.java
  ===================================================================
  /* Copyright 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.dirty.ops;
  
  import org.apache.commons.grafolia.analysis.GraphAnalysis;
  import org.apache.commons.grafolia.analysis.ObjectInfo;
  import org.apache.commons.graph.domain.dependency.DependencyGraph;
  import org.apache.ojb.broker.PersistenceBroker;
  import org.apache.ojb.broker.util.IdentityMapFactory;
  import org.apache.ojb.dirty.ops.Change;
  
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  import java.util.HashMap;
  import java.util.Collections;
  
  public class ChangeList
  {
      private final ArrayList changes = new ArrayList();
  
      public void add(Change change)
      {
          this.changes.add(change);
      }
  
      public void clear()
      {
          changes.clear();
      }
  
      public void execute(final PersistenceBroker broker, final GraphAnalysis analysis)
      {
          final List my_changes = this.sort(analysis, changes);
          for (int i = 0; i < my_changes.size(); i++)
          {
              final Change change = (Change) my_changes.get(i);
              change.execute(broker);
          }
      }
  
      /**
       * @todo This is UGLY UGLY UGLY
       */
      private List sort(final GraphAnalysis analysis, final ArrayList changes)
      {
          final Map objectToDeps = IdentityMapFactory.getIdentityMap();
          final Map objectToChange = IdentityMapFactory.getIdentityMap();
  
          for (int i = 0; i < changes.size(); i++)
          {
              final Change change = (Change) changes.get(i);
              final Object object = change.getObject();
              final ObjectInfo info = analysis.getObjectInfo(object);
              final HashSet deps = new HashSet();
              objectToDeps.put(object, deps);
  
              deps.addAll(info.getReferringObjects());
  
              objectToChange.put(object, change);
              final Collection refsViaCollection = getCollectionRefs(analysis, info);
              for (Iterator iterator = refsViaCollection.iterator(); iterator.hasNext();)
              {
                  final Object o = iterator.next();
                  final Set innerDeps = (Set) (objectToDeps.containsKey(o)
                                               ? objectToDeps.get(o)
                                               : new HashSet());
                  objectToDeps.put(o, innerDeps);
                  innerDeps.add(object);
              }
          }
  
          final DependencyGraph graph = new DependencyGraph();
  
          for (Iterator iterator = objectToDeps.entrySet().iterator(); iterator.hasNext();)
          {
              final Map.Entry entry = (Map.Entry) iterator.next();
              final Object obj = entry.getKey();
              final Set odeps = (Set) entry.getValue();
              final HashSet cdeps = new HashSet();
              for (Iterator iterator1 = odeps.iterator(); iterator1.hasNext();)
              {
                  final Object o = iterator1.next();
                  final Change c = (Change) objectToChange.get(o);
                  cdeps.add(c);
              }
              graph.addDependencies(objectToChange.get(obj), cdeps);
          }
  
          final List deps = graph.getSortedDependencies();
          final ArrayList sortedChanges = new ArrayList(deps.size());
          for (int i = 0; i < deps.size(); i++)
          {
              final Object val = deps.get(i);
              if (val == null) continue;
              if (val instanceof Collection) continue;
              if ((! (val instanceof Change)) && objectToChange.get(val) == null) continue;
              sortedChanges.add(( val instanceof Change
                                  ? val
                                  : objectToChange.get(val)));
          }
          Collections.reverse(sortedChanges);
          return sortedChanges;
      }
  
  
      private static Collection getCollectionRefs(final GraphAnalysis analysis, final ObjectInfo info)
      {
          final HashSet objects = new HashSet();
          for (Iterator iterator = info.getReferringCollections().iterator(); iterator.hasNext();)
          {
              final Collection collection = (Collection) iterator.next();
              final ObjectInfo collection_info = analysis.getObjectInfo(collection);
              objects.addAll(collection_info.getReferringObjects());
          }
          return objects;
      }
  
  //    public static class TopologicalComparator
  //    {
  //        public static final int BEFORE = -1;
  //        public static final int AFTER = 1;
  //        public static final int EQUAL = 0;
  //        public static final int NO_COMPARISON = Integer.MAX_VALUE;
  //
  //        private final GraphAnalysis analysis;
  //
  //        public TopologicalComparator(final GraphAnalysis analysis)
  //        {
  //            this.analysis = analysis;
  //        }
  //
  //        /**
  //         * @return one of the constants on defined in
  //         *         {@link org.apache.ojb.dirty.ops.ChangeList.TopologicalComparator}
  //         */
  //        public int compare(final Change changeOne, final Change changeTwo)
  //        {
  //            final Object object_one = changeOne.getObject();
  //            final Object object_two = changeTwo.getObject();
  //            final ObjectInfo info_one = analysis.getObjectInfo(object_one);
  //            final ObjectInfo info_two = analysis.getObjectInfo(object_two);
  //
  //            final Collection object_refs_via_collections_one = getCollectionRefs(analysis, info_one);
  //            final Collection object_refs_via_collection_two = getCollectionRefs(analysis, info_one);
  //
  //            if (object_refs_via_collections_one.contains(object_two))
  //            {
  //                if (info_one.getReferringObjects().contains(object_two))
  //                {
  //                    throw new IllegalStateException("Unresolvable FK Dependencies");
  //                }
  //                return AFTER;
  //            }
  //            if (object_refs_via_collection_two.contains(object_one))
  //            {
  //                if (info_two.getReferringObjects().contains(object_one))
  //                {
  //                    throw new IllegalStateException("Unresolvable FK Dependencies");
  //                }
  //                return BEFORE;
  //            }
  //
  //            if (info_one.getReferringObjects().contains(object_two)) return BEFORE;
  //            if (info_two.getReferringObjects().contains(object_one)) return AFTER;
  //
  //            return NO_COMPARISON;
  //        }
  //    }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/dirty/ops/Delete.java
  
  Index: Delete.java
  ===================================================================
  /* Copyright 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.dirty.ops;
  
  import org.apache.ojb.broker.PersistenceBroker;
  import org.apache.ojb.dirty.ops.Change;
  
  public class Delete implements Change
  {
      private final Object deleted;
  
      public Delete(final Object updated)
      {
          this.deleted = updated;
      }
  
      public Object getObject()
      {
          return deleted;
      }
  
      public String toString()
      {
          return "delete: " + deleted;
      }
  
      public void execute(final PersistenceBroker broker)
      {
          broker.store(deleted);
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/dirty/ops/Insert.java
  
  Index: Insert.java
  ===================================================================
  /* Copyright 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.dirty.ops;
  
  import org.apache.ojb.broker.PersistenceBroker;
  import org.apache.ojb.dirty.ops.Change;
  
  public class Insert implements Change
  {
      private final Object inserted;
  
      public Insert(final Object inserted)
      {
          this.inserted = inserted;
      }
  
      public Object getObject()
      {
          return inserted;
      }
  
      public void execute(final PersistenceBroker broker)
      {
          broker.store(inserted);
      }
  
      public String toString()
      {
          return "insert: " + inserted;
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/dirty/ops/Update.java
  
  Index: Update.java
  ===================================================================
  /* Copyright 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.dirty.ops;
  
  import org.apache.ojb.broker.PersistenceBroker;
  import org.apache.ojb.dirty.ops.Change;
  
  public class Update implements Change
  {
      private final Object updated;
  
      public Update(final Object updated)
      {
          this.updated = updated;
      }
  
      public Object getObject()
      {
          return updated;
      }
  
      public void execute(final PersistenceBroker broker)
      {
          broker.store(updated);
      }
  
      public String toString()
      {
          return "update: " + updated;
      }
  }
  
  
  
  1.2       +16 -16    db-ojb/src/java/org/apache/ojb/dirty/BrokerStateMonitor.java
  
  Index: BrokerStateMonitor.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/dirty/BrokerStateMonitor.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- BrokerStateMonitor.java	9 Aug 2004 23:38:31 -0000	1.1
  +++ BrokerStateMonitor.java	24 Aug 2004 02:18:37 -0000	1.2
  @@ -19,47 +19,47 @@
   
   public class BrokerStateMonitor implements PBStateListener
   {
  -    private final UnitAdaptor adaptor;
  +    private final ObjectTransaction tx;
   
  -    BrokerStateMonitor(final UnitAdaptor adaptor)
  +    BrokerStateMonitor(final ObjectTransaction tx)
       {
  -        this.adaptor = adaptor;
  +        this.tx = tx;
       }
   
  -    public void afterOpen(PBStateEvent event)
  +    public void afterOpen(final PBStateEvent event)
       {
       }
   
  -    public void beforeBegin(PBStateEvent event)
  +    public void beforeBegin(final PBStateEvent event)
       {
       }
   
  -    public void afterBegin(PBStateEvent event)
  +    public void afterBegin(final PBStateEvent event)
       {
  -        adaptor.begin();
  +        if (!tx.isOptimistic()) tx.begin();
       }
   
  -    public void beforeCommit(PBStateEvent event)
  +    public void beforeCommit(final PBStateEvent event)
       {
  -        if (!adaptor.isTransactionInProgress()) return;        
  -        adaptor.commit();
  +        if (!tx.isOpen() || tx.isOptimistic() ) return;
  +        tx.commit();
       }
   
  -    public void afterCommit(PBStateEvent event)
  +    public void afterCommit(final PBStateEvent event)
       {
       }
   
  -    public void beforeRollback(PBStateEvent event)
  +    public void beforeRollback(final PBStateEvent event)
       {
  -        if (!adaptor.isTransactionInProgress()) return;
  -        adaptor.rollback();
  +        if (!tx.isOpen()) return;
  +        tx.rollback();
       }
   
  -    public void afterRollback(PBStateEvent event)
  +    public void afterRollback(final PBStateEvent event)
       {
       }
   
  -    public void beforeClose(PBStateEvent event)
  +    public void beforeClose(final PBStateEvent event)
       {
       }
   }
  
  
  
  1.3       +47 -70    db-ojb/src/java/org/apache/ojb/dirty/DirtyBrokerFactory.java
  
  Index: DirtyBrokerFactory.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/dirty/DirtyBrokerFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DirtyBrokerFactory.java	14 Aug 2004 20:10:07 -0000	1.2
  +++ DirtyBrokerFactory.java	24 Aug 2004 02:18:37 -0000	1.3
  @@ -14,32 +14,60 @@
    */
   package org.apache.ojb.dirty;
   
  +import org.apache.commons.grafolia.AttachementFactory;
   import org.apache.commons.grafolia.DefaultUnit;
  +import org.apache.commons.grafolia.FieldAccessor;
  +import org.apache.commons.grafolia.IdentityFactory;
  +import org.apache.commons.grafolia.TrackerFactory;
  +import org.apache.commons.grafolia.Unit;
  +import org.apache.commons.grafolia.access.DirectFieldAccessor;
  +import org.apache.commons.grafolia.detach.JDOStyleAttachmentFactory;
  +import org.apache.commons.grafolia.identity.ObjectIdentityFactory;
   import org.apache.ojb.broker.OJB;
   import org.apache.ojb.broker.PBKey;
   import org.apache.ojb.broker.PersistenceBroker;
  -
  -import java.lang.reflect.InvocationHandler;
  -import java.lang.reflect.Method;
  -import java.lang.reflect.Proxy;
  +import org.apache.ojb.dirty.grafolia.OJBTrackerFactory;
   
   public class DirtyBrokerFactory
   {
       private final PBKey key;
       private final OJB ojb;
  +    private final TrackerFactory trackerFactory;
  +    private final AttachementFactory attachementFactory;
  +    private final IdentityFactory identityFactory;
  +    private final FieldAccessor fieldAccessor;
   
       public DirtyBrokerFactory()
       {
           ojb = new OJB();
  -        PersistenceBroker pb = ojb.lookupBroker();
  -        key = pb.getPBKey();
  -        pb.close();
  +        PersistenceBroker broker = null;
  +        try
  +        {
  +            broker = ojb.lookupBroker();
  +            key = broker.getPBKey();
  +        }
  +        finally
  +        {
  +            if (broker != null) broker.close();
  +        }
  +        trackerFactory = new OJBTrackerFactory();
  +        identityFactory = new ObjectIdentityFactory();
  +        fieldAccessor = new DirectFieldAccessor();
  +        attachementFactory = new JDOStyleAttachmentFactory(identityFactory,
  +                                                           fieldAccessor,
  +                                                           trackerFactory);
       }
   
       public DirtyBrokerFactory(final PBKey key)
       {
           ojb = new OJB();
           this.key = key;
  +        trackerFactory = new OJBTrackerFactory();
  +        identityFactory = new ObjectIdentityFactory();
  +        fieldAccessor = new DirectFieldAccessor();
  +        attachementFactory = new JDOStyleAttachmentFactory(identityFactory,
  +                                                           fieldAccessor,
  +                                                           trackerFactory);
       }
   
       public DirtyBrokerFactory(final String db, final String login, final String pass)
  @@ -50,6 +78,12 @@
           {
               broker = ojb.lookupBroker(db, login, pass);
               key = broker.getPBKey();
  +            trackerFactory = new OJBTrackerFactory();
  +            identityFactory = new ObjectIdentityFactory();
  +            fieldAccessor = new DirectFieldAccessor();
  +            attachementFactory = new JDOStyleAttachmentFactory(identityFactory,
  +                                                               fieldAccessor,
  +                                                               trackerFactory);
           }
           finally
           {
  @@ -60,70 +94,13 @@
       public DirtyPersistenceBroker getBroker()
       {
           final PersistenceBroker broker = ojb.lookupBroker(key);
  -        final UnitAdaptor adaptor = new UnitAdaptor(broker, new DefaultUnit());
  -        broker.addListener(new ObjectRegistrationListener(adaptor));
  -        broker.addListener(new BrokerStateMonitor(adaptor));
  -        Object proxy = Proxy.newProxyInstance(this.getClass().getClassLoader(),
  -                                              new Class[]{DirtyPersistenceBroker.class},
  -                                              new MyInvocationHandler(broker, adaptor));
  -        return (DirtyPersistenceBroker) proxy;
  -    }
  -
  -    private static class MyInvocationHandler implements InvocationHandler
  -    {
  -        private final PersistenceBroker broker;
  -        private final UnitAdaptor unit;
  +        final Unit unit = new DefaultUnit(trackerFactory, identityFactory, attachementFactory);
   
  -        public MyInvocationHandler(final PersistenceBroker broker, final UnitAdaptor unit)
  -        {
  -            this.broker = broker;
  -            this.unit = unit;
  -        }
  +        final ObjectTransaction tx = new UnitObjectTransaction(broker, unit);
  +        broker.addListener(new BrokerStateMonitor(tx));
  +        broker.addListener(new ObjectRegistrationListener(tx));
   
  -        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
  -        {
  -            if (method.getDeclaringClass().equals(DirtyPersistenceBroker.class))
  -            {
  -                return unit.getUnit();
  -            }
  -            else
  -            {
  -                return method.invoke(broker, args);
  -            }
  -        }
  +        final DelegatingDirtyBroker db = new DelegatingDirtyBroker(tx, broker);
  +        return db;
       }
  -//    private final PBKey key;
  -//
  -//    public DirtyBrokerFactory()
  -//    {
  -//        this(PersistenceBrokerFactory.getDefaultKey());
  -//    }
  -//
  -//    public DirtyBrokerFactory(final PBKey key)
  -//    {
  -//        this.key = key;
  -//    }
  -//
  -//    public DirtyBrokerFactory(final String db, final String login, final String pass)
  -//    {
  -//        PersistenceBroker broker = null;
  -//        try
  -//        {
  -//            broker = PersistenceBrokerFactory.createPersistenceBroker(db, login, pass);
  -//            key = broker.getPBKey();
  -//        }
  -//        finally
  -//        {
  -//            if (broker != null) broker.close();
  -//        }
  -//    }
  -//
  -//    public PersistenceBroker getBroker()
  -//    {
  -//        final PersistenceBroker broker = PersistenceBrokerFactory.createPersistenceBroker(key);
  -//        final UnitAdaptor adaptor = new UnitAdaptor(broker, new DefaultUnit());
  -//        broker.addListener(new ObjectRegistrationListener(adaptor));
  -//        broker.addListener(new BrokerStateMonitor(adaptor));
  -//        return broker;
  -//    }
   }
  
  
  
  1.2       +1 -1      db-ojb/src/java/org/apache/ojb/dirty/DirtyPersistenceBroker.java
  
  Index: DirtyPersistenceBroker.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/dirty/DirtyPersistenceBroker.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DirtyPersistenceBroker.java	14 Aug 2004 20:10:07 -0000	1.1
  +++ DirtyPersistenceBroker.java	24 Aug 2004 02:18:37 -0000	1.2
  @@ -19,5 +19,5 @@
   
   public interface DirtyPersistenceBroker extends PersistenceBroker
   {
  -    public Unit getUnit();
  +    public ObjectTransaction getObjectTransaction();
   }
  
  
  
  1.4       +13 -13    db-ojb/src/java/org/apache/ojb/dirty/ObjectRegistrationListener.java
  
  Index: ObjectRegistrationListener.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/dirty/ObjectRegistrationListener.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ObjectRegistrationListener.java	15 Aug 2004 13:13:09 -0000	1.3
  +++ ObjectRegistrationListener.java	24 Aug 2004 02:18:37 -0000	1.4
  @@ -14,46 +14,46 @@
    */
   package org.apache.ojb.dirty;
   
  -import org.apache.ojb.broker.PBLifeCycleListener;
   import org.apache.ojb.broker.PBLifeCycleEvent;
  +import org.apache.ojb.broker.PBLifeCycleListener;
   import org.apache.ojb.broker.PersistenceBrokerException;
   
   public class ObjectRegistrationListener implements PBLifeCycleListener
   {
  -    private final UnitAdaptor adaptor;
  +    private final ObjectTransaction tx;
   
  -    ObjectRegistrationListener(final UnitAdaptor adaptor)
  +    ObjectRegistrationListener(final ObjectTransaction adaptor)
       {
  -        this.adaptor = adaptor;
  +        this.tx = adaptor;
       }
   
  -    public void beforeInsert(PBLifeCycleEvent event) throws PersistenceBrokerException
  +    public void afterLookup(final PBLifeCycleEvent event) throws PersistenceBrokerException
       {
  +        if (!tx.isOpen()) return;
  +        tx.register(event.getTarget());
       }
   
  -    public void afterInsert(PBLifeCycleEvent event) throws PersistenceBrokerException
  +    public void beforeInsert(final PBLifeCycleEvent event) throws PersistenceBrokerException
       {
       }
   
  -    public void beforeUpdate(PBLifeCycleEvent event) throws PersistenceBrokerException
  +    public void afterInsert(final PBLifeCycleEvent event) throws PersistenceBrokerException
       {
       }
   
  -    public void afterUpdate(PBLifeCycleEvent event) throws PersistenceBrokerException
  +    public void beforeUpdate(final PBLifeCycleEvent event) throws PersistenceBrokerException
       {
       }
   
  -    public void beforeDelete(PBLifeCycleEvent event) throws PersistenceBrokerException
  +    public void afterUpdate(final PBLifeCycleEvent event) throws PersistenceBrokerException
       {
       }
   
  -    public void afterDelete(PBLifeCycleEvent event) throws PersistenceBrokerException
  +    public void beforeDelete(final PBLifeCycleEvent event) throws PersistenceBrokerException
       {
       }
   
  -    public void afterLookup(PBLifeCycleEvent event) throws PersistenceBrokerException
  +    public void afterDelete(final PBLifeCycleEvent event) throws PersistenceBrokerException
       {
  -        if (!adaptor.isTransactionInProgress()) return;
  -        adaptor.register(event.getTarget());
       }
   }
  
  
  
  1.2       +14 -23    db-ojb/src/java/org/apache/ojb/dirty/UnitFlushListener.java
  
  Index: UnitFlushListener.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/dirty/UnitFlushListener.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- UnitFlushListener.java	9 Aug 2004 23:38:31 -0000	1.1
  +++ UnitFlushListener.java	24 Aug 2004 02:18:37 -0000	1.2
  @@ -16,39 +16,30 @@
   
   import org.apache.commons.grafolia.Unit;
   import org.apache.commons.grafolia.analysis.GraphAnalysis;
  -import org.apache.commons.grafolia.hooks.UpdateListener;
  -import org.apache.commons.grafolia.hooks.InsertListener;
   import org.apache.commons.grafolia.hooks.FlushListener;
  +import org.apache.ojb.broker.PersistenceBroker;
  +import org.apache.ojb.dirty.ops.ChangeList;
   
  -import java.util.Collection;
  -
  -public class UnitFlushListener implements UpdateListener, InsertListener, FlushListener
  +public class UnitFlushListener implements FlushListener
   {
  -    private final UnitAdaptor adaptor;
  -
  -    public UnitFlushListener(UnitAdaptor adaptor)
  -    {
  -        this.adaptor = adaptor;
  -    }
  -
  -    public void onUpdate(Unit unit, Object dirty)
  -    {
  -        adaptor.update(dirty);
  -    }
  -
  +    private final ChangeList changes;
  +    private final PersistenceBroker broker;
   
  -    public void onInsert(Unit unit, Object inserted)
  +    public UnitFlushListener(final ChangeList changes,
  +                             final PersistenceBroker broker)
       {
  -        if (! (inserted instanceof Collection)) adaptor.insert(inserted);
  +        this.changes = changes;
  +        this.broker = broker;
       }
   
  -    public void OnFlushStart(Unit unit)
  +    public void OnFlushStart(final Unit unit)
       {
  -        // we don't care
  +        // ignore
       }
   
  -    public void onFlushFinish(Unit unit, GraphAnalysis analysis)
  +    public void onFlushFinish(final Unit unit, final GraphAnalysis analysis)
       {
  -        adaptor.flushed(analysis);
  +        changes.execute(broker, analysis);
  +        changes.clear();
       }
   }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/dirty/DelegatingDirtyBroker.java
  
  Index: DelegatingDirtyBroker.java
  ===================================================================
  /* Copyright 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.dirty;
  
  import org.apache.ojb.broker.PersistenceBroker;
  import org.apache.ojb.broker.IdentityFactory;
  import org.apache.ojb.broker.PersistenceBrokerEvent;
  import org.apache.ojb.broker.PBLifeCycleEvent;
  import org.apache.ojb.broker.PBStateEvent;
  import org.apache.ojb.broker.PersistenceBrokerException;
  import org.apache.ojb.broker.PBListener;
  import org.apache.ojb.broker.TransactionNotInProgressException;
  import org.apache.ojb.broker.TransactionInProgressException;
  import org.apache.ojb.broker.TransactionAbortedException;
  import org.apache.ojb.broker.PBKey;
  import org.apache.ojb.broker.PersistenceConfiguration;
  import org.apache.ojb.broker.MtoNImplementor;
  import org.apache.ojb.broker.ManageableCollection;
  import org.apache.ojb.broker.Identity;
  import org.apache.ojb.broker.query.Query;
  import org.apache.ojb.broker.metadata.DescriptorRepository;
  import org.apache.ojb.broker.metadata.ClassDescriptor;
  import org.apache.ojb.broker.cache.ObjectCache;
  import org.apache.ojb.broker.util.sequence.SequenceManager;
  import org.apache.ojb.broker.util.BrokerHelper;
  import org.apache.ojb.broker.util.ObjectModification;
  import org.apache.ojb.broker.util.configuration.Configuration;
  import org.apache.ojb.broker.util.configuration.ConfigurationException;
  import org.apache.ojb.broker.accesslayer.StatementManagerIF;
  import org.apache.ojb.broker.accesslayer.ConnectionManagerIF;
  import org.apache.ojb.broker.accesslayer.JdbcAccess;
  import org.apache.ojb.broker.accesslayer.OJBIterator;
  import org.apache.ojb.broker.accesslayer.sql.SqlGenerator;
  import org.apache.commons.grafolia.Unit;
  
  import java.util.Collection;
  import java.util.Enumeration;
  
  public class DelegatingDirtyBroker implements DirtyPersistenceBroker
  {
      private final ObjectTransaction tx;
      private final PersistenceBroker base;
  
      public DelegatingDirtyBroker(final ObjectTransaction tx, final PersistenceBroker base)
      {
          this.tx = tx;
          this.base = base;
      }
  
      public ObjectTransaction getObjectTransaction()
      {
          return tx;
      }
  
      public StatementManagerIF serviceStatementManager()
      {
          return base.serviceStatementManager();
      }
  
      public ConnectionManagerIF serviceConnectionManager()
      {
          return base.serviceConnectionManager();
      }
  
      public SqlGenerator serviceSqlGenerator()
      {
          return base.serviceSqlGenerator();
      }
  
      public JdbcAccess serviceJdbcAccess()
      {
          return base.serviceJdbcAccess();
      }
  
      public SequenceManager serviceSequenceManager()
      {
          return base.serviceSequenceManager();
      }
  
      public BrokerHelper serviceBrokerHelper()
      {
          return base.serviceBrokerHelper();
      }
  
      public ObjectCache serviceObjectCache()
      {
          return base.serviceObjectCache();
      }
  
      public IdentityFactory serviceIdentity()
      {
          return base.serviceIdentity();
      }
  
      public void fireBrokerEvent(final PersistenceBrokerEvent event)
      {
          base.fireBrokerEvent(event);
      }
  
      public void fireBrokerEvent(final PBLifeCycleEvent event)
      {
          base.fireBrokerEvent(event);
      }
  
      public void fireBrokerEvent(final PBStateEvent event)
      {
          base.fireBrokerEvent(event);
      }
  
      public void removeAllListeners() throws PersistenceBrokerException
      {
          base.removeAllListeners();
      }
  
      public void removeAllListeners(final boolean permanent) throws PersistenceBrokerException
      {
          base.removeAllListeners(permanent);
      }
  
      public void addListener(final PBListener listener) throws PersistenceBrokerException
      {
          base.addListener(listener);
      }
  
      public void addListener(final PBListener listener, final boolean permanent) throws PersistenceBrokerException
      {
          base.addListener(listener, permanent);
      }
  
      public void removeListener(final PBListener listener) throws PersistenceBrokerException
      {
          base.removeListener(listener);
      }
  
      public void abortTransaction() throws TransactionNotInProgressException
      {
          base.abortTransaction();
      }
  
      public void beginTransaction()
              throws TransactionInProgressException, TransactionAbortedException
      {
          base.beginTransaction();
      }
  
      public void commitTransaction()
              throws TransactionNotInProgressException, TransactionAbortedException
      {
          base.commitTransaction();
      }
  
      public boolean isInTransaction() throws PersistenceBrokerException
      {
          return base.isInTransaction();
      }
  
      public boolean close()
      {
          return base.close();
      }
  
      public boolean isClosed()
      {
          return base.isClosed();
      }
  
      public DescriptorRepository getDescriptorRepository()
      {
          return base.getDescriptorRepository();
      }
  
      public PBKey getPBKey()
      {
          return base.getPBKey();
      }
  
      public ClassDescriptor getClassDescriptor(Class clazz) throws PersistenceBrokerException
      {
          return base.getClassDescriptor(clazz);
      }
  
      public boolean hasClassDescriptor(Class clazz)
      {
          return base.hasClassDescriptor(clazz);
      }
  
      public Class getTopLevelClass(Class clazz) throws PersistenceBrokerException
      {
          return base.getTopLevelClass(clazz);
      }
  
      public PersistenceConfiguration getConfiguration()
      {
          return base.getConfiguration();
      }
  
      public void clearCache() throws PersistenceBrokerException
      {
          base.clearCache();
      }
  
      public void removeFromCache(Object objectOrIdentity) throws PersistenceBrokerException
      {
          base.removeFromCache(objectOrIdentity);
      }
  
      public void store(Object obj,
                        ObjectModification modification) throws PersistenceBrokerException
      {
          base.store(obj, modification);
      }
  
      public void store(Object obj) throws PersistenceBrokerException
      {
          base.store(obj);
      }
  
      public void delete(Object obj) throws PersistenceBrokerException
      {
          base.delete(obj);
      }
  
      public void deleteMtoNImplementor(MtoNImplementor m2nImpl) throws PersistenceBrokerException
      {
          base.deleteMtoNImplementor(m2nImpl);
      }
  
      public void addMtoNImplementor(MtoNImplementor m2nImpl) throws PersistenceBrokerException
      {
          base.addMtoNImplementor(m2nImpl);
      }
  
      public void deleteByQuery(Query query) throws PersistenceBrokerException
      {
          base.deleteByQuery(query);
      }
  
      public void retrieveAllReferences(Object pInstance) throws PersistenceBrokerException
      {
          base.retrieveAllReferences(pInstance);
      }
  
      public void retrieveReference(Object pInstance, String pAttributeName) throws PersistenceBrokerException
      {
          base.retrieveReference(pInstance, pAttributeName);
      }
  
      public int getCount(Query query) throws PersistenceBrokerException
      {
          return base.getCount(query);
      }
  
      public Collection getCollectionByQuery(Query query) throws PersistenceBrokerException
      {
          return base.getCollectionByQuery(query);
      }
  
      public ManageableCollection getCollectionByQuery(Class collectionClass, Query query)
              throws PersistenceBrokerException
      {
          return base.getCollectionByQuery(collectionClass, query);
      }
  
      public OJBIterator getIteratorByQuery(Query query) throws PersistenceBrokerException
      {
          return base.getIteratorByQuery(query);
      }
  
      public OJBIterator getReportQueryIteratorByQuery(Query query) throws PersistenceBrokerException
      {
          return base.getReportQueryIteratorByQuery(query);
      }
  
      public Object getObjectByIdentity(Identity id) throws PersistenceBrokerException
      {
          return base.getObjectByIdentity(id);
      }
  
      public Object getObjectByQuery(Query query) throws PersistenceBrokerException
      {
          return base.getObjectByQuery(query);
      }
  
      public Enumeration getPKEnumerationByQuery(Class PrimaryKeyClass, Query query)
              throws PersistenceBrokerException
      {
          return base.getPKEnumerationByQuery(PrimaryKeyClass, query);
      }
  
      public void configure(Configuration pConfig) throws ConfigurationException
      {
          base.configure(pConfig);
      }
  
      public org.odbms.Query query()
      {
          return base.query();
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/dirty/ObjectStateChangeListener.java
  
  Index: ObjectStateChangeListener.java
  ===================================================================
  /* Copyright 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.dirty;
  
  import org.apache.commons.grafolia.Unit;
  import org.apache.commons.grafolia.analysis.GraphAnalysis;
  import org.apache.commons.grafolia.hooks.UpdateListener;
  import org.apache.commons.grafolia.hooks.InsertListener;
  import org.apache.commons.grafolia.hooks.FlushListener;
  import org.apache.commons.grafolia.hooks.DeleteListener;
  import org.apache.ojb.dirty.ops.ChangeList;
  import org.apache.ojb.dirty.ops.Update;
  import org.apache.ojb.dirty.ops.Insert;
  import org.apache.ojb.dirty.ops.Delete;
  
  import java.util.Collection;
  
  public class ObjectStateChangeListener implements UpdateListener, InsertListener, DeleteListener
  {
      private final ChangeList changes;
  
      public ObjectStateChangeListener(final ChangeList adaptor)
      {
          this.changes = adaptor;
      }
  
      public void onUpdate(final Unit unit, final Object dirty)
      {
          changes.add(new Update(dirty));
      }
  
      public void onInsert(final Unit unit, final Object inserted)
      {
          if (! (inserted instanceof Collection)) changes.add(new Insert(inserted));
      }
  
      public void onDelete(final Unit unit, final Object instance)
      {
          changes.add(new Delete(instance));
      }
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/dirty/ObjectTransaction.java
  
  Index: ObjectTransaction.java
  ===================================================================
  /* Copyright 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.dirty;
  
  import org.apache.commons.grafolia.State;
  
  import java.util.Collection;
  
  public interface ObjectTransaction
  {
      /**
       * Register an object as PERSISTENT_CLEAN
       */
      Object register(Object root);
  
      /**
       * Make a TRANSIENT instance PERSISTENT_NEW
       */
      Object insert(Object beer);
  
      /**
       * Make a collection of TRANSIENT instances PERSISTENT_NEW
       */
      Collection insert(Collection beers);
  
      /**
       * Make a PERSISTENT_* instance PERSISTENT_DELETED
       */
      void delete(Object instance);
  
      /**
       * Begin an object level transaction
       */
      void begin();
  
      /**
       * Flush all changes down to the datastore (still in context of
       * a datastore transaction if one is in progress)
       */
      void flush();
  
      /**
       * Flush changes and commit (will percolate to datastore)
       */
      void commit();
  
      /**
       * Is a transaction in progress
       */
      boolean isOpen();
  
      /**
       * Rollback object space changes
       */
      void rollback();
  
      /**
       * Detach <code>objects</code> from the transaction. Any changes to
       * these objects will be flushed to the persistence store before
       * detach.
       */
      Collection detach(Collection objects);
  
      /**
       * Re-attach a collection of detached objects. Objects which have changed,
       * or are not already bound, will be registered as PERSISTENT_DIRTY
       */
      Collection attach(Collection objects);
  
      /**
       * Detach a single instance
       */
      Object detach(Object beer);
  
      /**
       * Attach a single instance
       */
      Object attach(Object beer);
  
      /**
       * Obtain the present transactional state of <code>object</code>
       */
      State getState(Object object);
  
      /**
       * Specify whether this should be an optimistic transaction
       * <p>
       * Note that all instances participating in an optimistic transaction
       * will need to have a version column mapped, or the changes will
       * be assumed to be valid (iow, last commit wins)
       */
      void setOptimistic(boolean optimistic);
  
      boolean isOptimistic();
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/dirty/UnitObjectTransaction.java
  
  Index: UnitObjectTransaction.java
  ===================================================================
  /* Copyright 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.dirty;
  
  import org.apache.commons.grafolia.State;
  import org.apache.commons.grafolia.Unit;
  import org.apache.ojb.broker.PersistenceBroker;
  import org.apache.ojb.dirty.ops.ChangeList;
  
  import java.util.Collection;
  
  public class UnitObjectTransaction implements ObjectTransaction
  {
      private final Unit unit;
      private final ChangeList changes;
      private boolean optimistic = false;
      private final PersistenceBroker broker;
  
      public UnitObjectTransaction(final PersistenceBroker broker, final Unit unit)
      {
          this.broker = broker;
          this.unit = unit;
          this.changes = new ChangeList();
          final ObjectStateChangeListener listener = new ObjectStateChangeListener(changes);
          unit.addUpdateListener(listener);
          unit.addDeleteListener(listener);
          unit.addInsertListener(listener);
          unit.addFlushListener(new UnitFlushListener(changes, broker));
      }
  
      public Object register(final Object root)
      {
          return unit.register(root);
      }
  
      public Object insert(final Object beer)
      {
          return unit.insert(beer);
      }
  
      public Collection insert(final Collection beers)
      {
          return unit.insert(beers);
      }
  
      public void delete(final Object instance)
      {
          unit.delete(instance);
      }
  
      public void begin()
      {
          unit.begin();
          if (!optimistic) broker.beginTransaction();
      }
  
      public State getState(final Object object)
      {
          return unit.getState(object);
      }
  
      public void setOptimistic(final boolean optimistic)
      {
          this.optimistic = optimistic;
      }
  
      public boolean isOptimistic()
      {
          return optimistic;
      }
  
      public void flush()
      {
          unit.flush();
      }
  
      public void commit()
      {
          final boolean pb_tx = broker.isInTransaction();
          if (optimistic && !pb_tx) broker.beginTransaction();
          unit.commit();
          if (optimistic && !pb_tx) broker.commitTransaction();
          if (!optimistic) broker.commitTransaction();
      }
  
      public boolean isOpen()
      {
          return unit.isOpen();
      }
  
      public void rollback()
      {
          unit.rollback();
          if (!optimistic) broker.abortTransaction();
      }
  
      public Collection detach(final Collection objects)
      {
          return unit.detach(objects);
      }
  
      public Collection attach(final Collection objects)
      {
          return unit.attach(objects);
      }
  
      public Object detach(final Object beer)
      {
          return unit.detach(beer);
      }
  
      public Object attach(final Object beer)
      {
          return unit.attach(beer);
      }
  }
  
  
  
  1.2       +3 -1      db-ojb/src/test/org/apache/ojb/dirty/AllTests.java
  
  Index: AllTests.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/dirty/AllTests.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AllTests.java	9 Aug 2004 23:38:32 -0000	1.1
  +++ AllTests.java	24 Aug 2004 02:18:38 -0000	1.2
  @@ -22,8 +22,10 @@
   {
       public static Test suite()
       {
  -        TestSuite suite = new TestSuite("Dirty Tests");
  +        final TestSuite suite = new TestSuite("Dirty Tests");
           suite.addTest(new TestSuite(DirtyBrokerFactoryTest.class));
  +        suite.addTest(new TestSuite(DirtyBrokerTest.class));
  +        suite.addTest(new TestSuite(UnitTest.class));
           return suite;
       }
   }
  
  
  
  1.5       +4 -4      db-ojb/src/test/org/apache/ojb/dirty/DirtyBrokerTest.java
  
  Index: DirtyBrokerTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/dirty/DirtyBrokerTest.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- DirtyBrokerTest.java	15 Aug 2004 13:13:08 -0000	1.4
  +++ DirtyBrokerTest.java	24 Aug 2004 02:18:38 -0000	1.5
  @@ -138,11 +138,11 @@
           broker.beginTransaction();
           person = (Person) broker.getObjectByIdentity(broker.serviceIdentity().buildIdentity(person));
   
  -        Address address = new Address("USA", "Wilmington", "Wombat Court");
  +        final Address address = new Address("USA", "Wilmington", "Wombat Court");
           // should be disabled to have valid test -- broker.store(address);
           person.addOtherAddress("Store", address);
           
  -        Address mainAddress = new Address("USA", "Wilmington-Main", "Wombat Court");
  +        final Address mainAddress = new Address("USA", "Wilmington-Main", "Wombat Court");
           person.setMainAddress(mainAddress);
   
           broker.commitTransaction();
  @@ -150,10 +150,10 @@
           broker.clearCache();
   
           person = (Person) broker.getObjectByIdentity(broker.serviceIdentity().buildIdentity(person));
  -        Collection others = person.getOtherAddresses();
  +        final Collection others = person.getOtherAddresses();
           assertNotNull(others);
           assertEquals(1, others.size());
  -        AddressDesc desc = (AddressDesc) others.iterator().next();
  +        final AddressDesc desc = (AddressDesc) others.iterator().next();
           assertNotNull(desc);
           assertEquals(address.getCity(), desc.getAddress().getCity());
   
  
  
  
  1.2       +40 -8     db-ojb/src/test/org/apache/ojb/dirty/UnitTest.java
  
  Index: UnitTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/dirty/UnitTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- UnitTest.java	14 Aug 2004 20:10:08 -0000	1.1
  +++ UnitTest.java	24 Aug 2004 02:18:38 -0000	1.2
  @@ -29,9 +29,9 @@
   
       public void setUp() throws Exception
       {
  -        PersistenceBroker broker = factory.getBroker();
  +        final PersistenceBroker broker = factory.getBroker();
           broker.beginTransaction();
  -        Criteria all = null;
  +        final Criteria all = null;
   
           broker.deleteByQuery(QueryFactory.newQuery(AddressDesc.class, all));
           broker.deleteByQuery(QueryFactory.newQuery(Person.class, all));
  @@ -43,21 +43,21 @@
   
       public void testGetUnit()
       {
  -        DirtyPersistenceBroker broker = factory.getBroker();
  +        final DirtyPersistenceBroker broker = factory.getBroker();
   
           broker.beginTransaction();
  -        Unit unit = broker.getUnit();
  +        final ObjectTransaction unit = broker.getObjectTransaction();
           assertNotNull(unit);
           broker.commitTransaction();
       }
   
       public void testLazyGraphInsert()
       {
  -        DirtyPersistenceBroker broker = factory.getBroker();
  +        final DirtyPersistenceBroker broker = factory.getBroker();
   
           broker.beginTransaction();
  -        Unit unit = broker.getUnit();
  -        Person person = new Person("Chris", "Double");
  +        final ObjectTransaction unit = broker.getObjectTransaction();
  +        final Person person = new Person("Chris", "Double");
           person.setMainAddress(new Address("USA", "New York", "5th Avenue"));
           person.addOtherAddress("vacation", new Address("USA", "Lake George", "Reed St"));
   
  @@ -69,8 +69,40 @@
   
           broker.clearCache();
   
  -        Collection addresses = broker.getCollectionByQuery(QueryFactory.newQuery(Address.class,
  +        final Collection addresses = broker.getCollectionByQuery(QueryFactory.newQuery(Address.class,
                                                                                    (Criteria)null));
           assertEquals(2, addresses.size());
  +    }
  +
  +    public void _testLazyDeletion()
  +    {
  +        final DirtyPersistenceBroker broker = factory.getBroker();
  +
  +        broker.beginTransaction();
  +        //Unit unit = broker.getUnit();
  +        ObjectTransaction unit = broker.getObjectTransaction();
  +        final Person person;
  +        person = new Person("Chris", "Double");
  +        person.setMainAddress(new Address("USA", "New York", "5th Avenue"));
  +
  +        unit.insert(person);
  +
  +        broker.commitTransaction();
  +
  +        broker.clearCache();
  +
  +        broker.beginTransaction();
  +        unit = broker.getObjectTransaction();
  +        final Address main =
  +                (Address) broker.getObjectByIdentity(
  +                        broker.serviceIdentity().buildIdentity(person.getMainAddress()));
  +        unit.delete(main);
  +        broker.commitTransaction();
  +
  +        broker.clearCache();
  +
  +        final Collection addresses = broker.getCollectionByQuery(QueryFactory.newQuery(Address.class,
  +                                                                                 (Criteria)null));
  +        assertEquals(0, addresses.size());
       }
   }
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/dirty/OptimisticTransactionTest.java
  
  Index: OptimisticTransactionTest.java
  ===================================================================
  /* Copyright 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.dirty;
  
  import junit.framework.TestCase;
  
  public class OptimisticTransactionTest extends TestCase
  {
      private final DirtyBrokerFactory factory = new DirtyBrokerFactory();
      private DirtyPersistenceBroker broker;
      private ObjectTransaction tx;
  
      public void setUp()
      {
          broker = factory.getBroker();
          tx = broker.getObjectTransaction();
      }
  
      public void tearDown()
      {
          if (!broker.isClosed()) broker.close();
      }
  
      public void testBeginStates()
      {
          tx.setOptimistic(true);
          tx.begin();
  
          assertTrue(tx.isOpen());
          assertFalse(broker.isInTransaction());
      }
  
      public void testUnitCommitFlushesInNonTransactionalBroker()
      {
          tx.setOptimistic(true);
          tx.begin();
          final Person brian = new Person();
          brian.setFirstname("Brian");
          tx.insert(brian);
          tx.commit();
  
          broker.clearCache();
  
          final Person same = (Person) broker.getObjectByIdentity(broker.serviceIdentity().buildIdentity(brian));
          assertNotNull(same);
          assertEquals(brian.getId(), same.getId());
      }
  }
  
  
  

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