You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by br...@apache.org on 2004/08/10 01:38:32 UTC
cvs commit: db-ojb/src/test/org/apache/ojb/dirty Address.java AddressDesc.java AllTests.java DirtyBrokerFactoryTest.java DirtyBrokerTest.java Person.java
brianm 2004/08/09 16:38:32
Modified: src/test/org/apache/ojb repository.xml
src/test/org/apache/ojb/broker AllTests.java
src/test/org/apache/ojb/broker/dirty DirtyTest.java
Added: lib commons-grafolia-1.0.jar commons-graph-0.8.1.jar
src/java/org/apache/ojb/dirty BrokerStateMonitor.java
Change.java ChangeList.java DirtyBrokerFactory.java
Insert.java ObjectRegistrationListener.java
UnitAdaptor.java UnitFlushListener.java Update.java
src/test/org/apache/ojb repository_junit_dirty.xml
src/test/org/apache/ojb/dirty Address.java AddressDesc.java
AllTests.java DirtyBrokerFactoryTest.java
DirtyBrokerTest.java Person.java
Log:
Take 2 on dirty state tracking for the PB.
This is definately an in-progress checkin -- parts of this code are criminally inefficient (the statement re-ordering, for instance).
Also added two libraries -- commons-grafolia which has the state management code written for OJB and is in db-commons-sandbox at the moment, and commons-graph which is in jakarta-commons-sandbox. Grafolia will remain a dependency, commons-graph may not as I don't know if it buys us enough to be worth including. It is only used in the statement reordering mudpie.
Revision Changes Path
1.1 db-ojb/lib/commons-grafolia-1.0.jar
<<Binary file>>
1.1 db-ojb/lib/commons-graph-0.8.1.jar
<<Binary file>>
1.1 db-ojb/src/java/org/apache/ojb/dirty/BrokerStateMonitor.java
Index: BrokerStateMonitor.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.PBStateListener;
import org.apache.ojb.broker.PBStateEvent;
public class BrokerStateMonitor implements PBStateListener
{
private final UnitAdaptor adaptor;
BrokerStateMonitor(final UnitAdaptor adaptor)
{
this.adaptor = adaptor;
}
public void afterOpen(PBStateEvent event)
{
}
public void beforeBegin(PBStateEvent event)
{
}
public void afterBegin(PBStateEvent event)
{
adaptor.begin();
}
public void beforeCommit(PBStateEvent event)
{
if (!adaptor.isTransactionInProgress()) return;
adaptor.commit();
}
public void afterCommit(PBStateEvent event)
{
}
public void beforeRollback(PBStateEvent event)
{
if (!adaptor.isTransactionInProgress()) return;
adaptor.rollback();
}
public void afterRollback(PBStateEvent event)
{
}
public void beforeClose(PBStateEvent event)
{
}
}
1.1 db-ojb/src/java/org/apache/ojb/dirty/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;
public interface Change
{
Object getObject();
}
1.1 db-ojb/src/java/org/apache/ojb/dirty/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;
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 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++)
{
Change change = (Change) my_changes.get(i);
broker.store(change.getObject());
}
}
/**
* @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.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/DirtyBrokerFactory.java
Index: DirtyBrokerFactory.java
===================================================================
/* 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.
*/
package org.apache.ojb.dirty;
import org.apache.commons.grafolia.DefaultUnit;
import org.apache.ojb.broker.PBKey;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.PersistenceBrokerFactory;
public class DirtyBrokerFactory
{
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.1 db-ojb/src/java/org/apache/ojb/dirty/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;
public class Insert implements Change
{
private final Object inserted;
public Insert(final Object inserted)
{
this.inserted = inserted;
}
public Object getObject()
{
return inserted;
}
public String toString()
{
return "insert: " + inserted;
}
}
1.1 db-ojb/src/java/org/apache/ojb/dirty/ObjectRegistrationListener.java
Index: ObjectRegistrationListener.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.PBLifeCycleListener;
import org.apache.ojb.broker.PBLifeCycleEvent;
import org.apache.ojb.broker.PersistenceBrokerException;
public class ObjectRegistrationListener implements PBLifeCycleListener
{
private final UnitAdaptor adaptor;
ObjectRegistrationListener(final UnitAdaptor adaptor)
{
this.adaptor = adaptor;
}
public void beforeInsert(PBLifeCycleEvent event) throws PersistenceBrokerException
{
}
public void afterInsert(PBLifeCycleEvent event) throws PersistenceBrokerException
{
}
public void beforeUpdate(PBLifeCycleEvent event) throws PersistenceBrokerException
{
}
public void afterUpdate(PBLifeCycleEvent event) throws PersistenceBrokerException
{
}
public void beforeDelete(PBLifeCycleEvent event) throws PersistenceBrokerException
{
}
public void afterDelete(PBLifeCycleEvent event) throws PersistenceBrokerException
{
}
public void afterLookup(PBLifeCycleEvent event) throws PersistenceBrokerException
{
if (!adaptor.isTransactionInProgress()) return;
adaptor.register(event.getTarget());
}
}
1.1 db-ojb/src/java/org/apache/ojb/dirty/UnitAdaptor.java
Index: UnitAdaptor.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.ojb.broker.PersistenceBroker;
/**
* Bridges the grafolia and OJB parts. Should be simply callbacks on events, no real logic
*/
class UnitAdaptor
{
private final PersistenceBroker broker;
private final Unit unit;
private final ChangeList changes;
UnitAdaptor(final PersistenceBroker broker, Unit unit)
{
this.broker = broker;
this.unit = unit;
this.changes = new ChangeList();
final UnitFlushListener listener = new UnitFlushListener(this);
unit.addUpdateListener(listener);
unit.addInsertListener(listener);
unit.addFlushListener(listener);
}
public void begin()
{
unit.begin();
}
public void commit()
{
changes.clear();
unit.commit();
// flushed(..) will be called during commit()
}
public void flushed(GraphAnalysis analysis)
{
changes.execute(broker, analysis);
changes.clear();
}
public void rollback()
{
unit.rollback();
}
public void register(Object target)
{
unit.register(target);
}
public boolean isTransactionInProgress()
{
return unit.isOpen();
}
public void update(Object dirty)
{
changes.add(new Update(dirty));
}
public void insert(Object inserted)
{
changes.add(new Insert(inserted));
}
}
1.1 db-ojb/src/java/org/apache/ojb/dirty/UnitFlushListener.java
Index: UnitFlushListener.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 java.util.Collection;
public class UnitFlushListener implements UpdateListener, InsertListener, FlushListener
{
private final UnitAdaptor adaptor;
public UnitFlushListener(UnitAdaptor adaptor)
{
this.adaptor = adaptor;
}
public void onUpdate(Unit unit, Object dirty)
{
adaptor.update(dirty);
}
public void onInsert(Unit unit, Object inserted)
{
if (! (inserted instanceof Collection)) adaptor.insert(inserted);
}
public void OnFlushStart(Unit unit)
{
// we don't care
}
public void onFlushFinish(Unit unit, GraphAnalysis analysis)
{
adaptor.flushed(analysis);
}
}
1.1 db-ojb/src/java/org/apache/ojb/dirty/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;
public class Update implements Change
{
private final Object updated;
public Update(final Object updated)
{
this.updated = updated;
}
public Object getObject()
{
return updated;
}
public String toString()
{
return "update: " + updated;
}
}
1.27 +2 -0 db-ojb/src/test/org/apache/ojb/repository.xml
Index: repository.xml
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/repository.xml,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -r1.26 -r1.27
--- repository.xml 25 Jun 2004 14:49:40 -0000 1.26
+++ repository.xml 9 Aug 2004 23:38:31 -0000 1.27
@@ -43,6 +43,7 @@
<!ENTITY junit SYSTEM "repository_junit.xml">
<!ENTITY junit_odmg SYSTEM "repository_junit_odmg.xml">
<!ENTITY junit_otm SYSTEM "repository_junit_otm.xml">
+<!ENTITY junit_dirty SYSTEM "repository_junit_dirty.xml">
<!ENTITY junit_ref SYSTEM "repository_junit_reference.xml">
<!ENTITY junit_meta_seq SYSTEM "repository_junit_meta_seq.xml">
<!ENTITY junit_cache SYSTEM "repository_junit_cache.xml">
@@ -74,6 +75,7 @@
&junit;
&junit_odmg;
&junit_otm;
+ &junit_dirty;
&junit_ref;
&junit_meta_seq;
&junit_cache;
1.1 db-ojb/src/test/org/apache/ojb/repository_junit_dirty.xml
Index: repository_junit_dirty.xml
===================================================================
<!-- Mapping of classes used in junit tests -->
<!-- @version $Id: repository_junit_dirty.xml,v 1.1 2004/08/09 23:38:31 brianm Exp $ -->
<!--
#/* 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.
*/
-->
<class-descriptor
class="org.apache.ojb.dirty.Person"
table="OTM_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"
/>
<field-descriptor
name="mainAddressId"
column="MAIN_ADDRESS_ID"
jdbc-type="INTEGER"
auto-retrieve="true"
/>
<reference-descriptor
name="mainAddress"
class-ref="org.apache.ojb.dirty.Address"
auto-retrieve="true"
>
<foreignkey field-ref="mainAddressId"/>
</reference-descriptor>
<collection-descriptor
name="otherAddresses"
element-class-ref="org.apache.ojb.dirty.AddressDesc"
collection-class="org.apache.ojb.broker.util.collections.ManageableArrayList"
auto-retrieve="true"
>
<inverse-foreignkey field-ref="personId"/>
</collection-descriptor>
</class-descriptor>
<class-descriptor
class="org.apache.ojb.dirty.Address"
table="OTM_ADDRESS"
>
<field-descriptor
name="id"
column="ID"
jdbc-type="INTEGER"
primarykey="true"
autoincrement="true"
/>
<field-descriptor
name="country"
column="COUNTRY"
jdbc-type="VARCHAR"
/>
<field-descriptor
name="city"
column="CITY"
jdbc-type="VARCHAR"
/>
<field-descriptor
name="street"
column="STREET"
jdbc-type="VARCHAR"
/>
</class-descriptor>
<class-descriptor
class="org.apache.ojb.dirty.AddressDesc"
table="OTM_ADDRESS_DESC"
>
<field-descriptor
name="id"
column="ID"
jdbc-type="INTEGER"
primarykey="true"
autoincrement="true"
/>
<field-descriptor
name="desc"
column="DESCRIPTION"
jdbc-type="VARCHAR"
/>
<field-descriptor
name="personId"
column="PERSON_ID"
jdbc-type="INTEGER"
/>
<field-descriptor
name="addressId"
column="ADDRESS_ID"
jdbc-type="INTEGER"
/>
<reference-descriptor
name="person"
class-ref="org.apache.ojb.dirty.Person"
auto-retrieve="true"
>
<foreignkey field-ref="personId"/>
</reference-descriptor>
<reference-descriptor
name="address"
class-ref="org.apache.ojb.dirty.Address"
auto-retrieve="true"
>
<foreignkey field-ref="addressId"/>
</reference-descriptor>
</class-descriptor>
1.52 +0 -2 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.51
retrieving revision 1.52
diff -u -r1.51 -r1.52
--- AllTests.java 27 Jul 2004 23:38:49 -0000 1.51
+++ AllTests.java 9 Aug 2004 23:38:31 -0000 1.52
@@ -4,7 +4,6 @@
import junit.framework.TestSuite;
import org.apache.ojb.broker.cache.LocalCacheTest;
import org.apache.ojb.broker.cache.ObjectCacheTest;
-import org.apache.ojb.broker.dirty.DirtyTest;
import org.apache.ojb.broker.metadata.CustomAttributesTest;
import org.apache.ojb.broker.metadata.MetadataMultithreadedTest;
import org.apache.ojb.broker.metadata.MetadataTest;
@@ -101,7 +100,6 @@
suite.addTestSuite(PrimaryKeyForeignKeyTest.class);
suite.addTestSuite(M2NGraphTest.class);
suite.addTestSuite(PersistentFieldTest.class);
- suite.addTest(new TestSuite(DirtyTest.class));
return suite;
}
1.6 +1 -1 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.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- DirtyTest.java 30 Jul 2004 23:20:25 -0000 1.5
+++ DirtyTest.java 9 Aug 2004 23:38:32 -0000 1.6
@@ -7,11 +7,11 @@
import org.apache.ojb.broker.InterfaceArticle;
import org.apache.ojb.broker.PersistenceAwarePerson;
import org.apache.ojb.broker.PersistenceBroker;
-import org.apache.ojb.broker.Person;
import org.apache.ojb.broker.ProductGroupWithCollectionProxy;
import org.apache.ojb.broker.core.proxy.CollectionProxy;
import org.apache.ojb.broker.core.proxy.ProxyHelper;
import org.apache.ojb.junit.OJBTestCase;
+import org.apache.ojb.dirty.Person;
public class DirtyTest extends OJBTestCase
{
1.1 db-ojb/src/test/org/apache/ojb/dirty/Address.java
Index: Address.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 java.io.Serializable;
public class Address implements Serializable
{
private int id;
private String country;
private String city;
private String street;
public Address()
{
}
public Address(String country, String city, String street)
{
this.country = country;
this.city = city;
this.street = street;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getCountry()
{
return country;
}
public void setCountry(String country)
{
this.country = country;
}
public String getCity()
{
return city;
}
public void setCity(String city)
{
this.city = city;
}
public String getStreet()
{
return street;
}
public void setStreet(String street)
{
this.street = street;
}
}
1.1 db-ojb/src/test/org/apache/ojb/dirty/AddressDesc.java
Index: AddressDesc.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 java.io.Serializable;
public class AddressDesc implements Serializable
{
private int id;
private String desc;
private int personId;
private Person person;
private int addressId;
private Address address;
public AddressDesc()
{
}
public AddressDesc(String desc, Address address)
{
this.desc = desc;
this.address = address;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getDesc()
{
return desc;
}
public void setDesc(String desc)
{
this.desc = desc;
}
public int getPersonId()
{
return personId;
}
public void setPersonId(int personId)
{
this.personId = personId;
}
public Person getPerson()
{
return person;
}
public void setPerson(Person person)
{
this.person = person;
}
public int getAddressId()
{
return addressId;
}
public void setAddressId(int addressId)
{
this.addressId = addressId;
}
public Address getAddress()
{
return address;
}
public void setAddress(Address address)
{
this.address = address;
}
}
1.1 db-ojb/src/test/org/apache/ojb/dirty/AllTests.java
Index: AllTests.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.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class AllTests extends TestCase
{
public static Test suite()
{
TestSuite suite = new TestSuite("Dirty Tests");
suite.addTest(new TestSuite(DirtyBrokerFactoryTest.class));
return suite;
}
}
1.1 db-ojb/src/test/org/apache/ojb/dirty/DirtyBrokerFactoryTest.java
Index: DirtyBrokerFactoryTest.java
===================================================================
/*
* Copyright (c) 2004 Your Corporation. All Rights Reserved.
*/
package org.apache.ojb.dirty;
import junit.framework.TestCase;
import org.apache.ojb.broker.PBKey;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.PersistenceBrokerFactory;
public class DirtyBrokerFactoryTest extends TestCase
{
public void testBuildDefault()
{
DirtyBrokerFactory factory = new DirtyBrokerFactory();
PersistenceBroker broker = factory.getBroker();
assertNotNull(broker);
}
public void testSpecificPBKey()
{
PBKey key = PersistenceBrokerFactory.getDefaultKey();
DirtyBrokerFactory factory = new DirtyBrokerFactory(key);
PersistenceBroker broker = factory.getBroker();
assertNotNull(broker);
}
}
1.1 db-ojb/src/test/org/apache/ojb/dirty/DirtyBrokerTest.java
Index: DirtyBrokerTest.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;
import org.apache.ojb.broker.PersistenceBroker;
import java.util.Collection;
public class DirtyBrokerTest extends TestCase
{
private final DirtyBrokerFactory factory = new DirtyBrokerFactory();
private PersistenceBroker broker;
public void setUp()
{
broker = factory.getBroker();
}
public void tearDown()
{
if (!broker.isClosed()) broker.close();
}
public void testDetectChangeOnSingleObject()
{
Person person = new Person();
person.setFirstname("Brian");
broker.store(person);
broker.clearCache();
broker.beginTransaction();
person = (Person) broker.getObjectByIdentity(broker.serviceIdentity().buildIdentity(person));
person.setFirstname("Paul");
broker.commitTransaction();
broker.clearCache();
person = (Person) broker.getObjectByIdentity(broker.serviceIdentity().buildIdentity(person));
assertEquals("Paul", person.getFirstname());
}
public void testDetectChangeOnReferencedObject()
{
Person person = new Person();
Address main = new Address("USA", "Montchanin", "100 Rockland Road");
person.setMainAddress(main);
person.setFirstname("Brian");
broker.store(main);
broker.store(person);
broker.clearCache();
broker.beginTransaction();
person = (Person) broker.getObjectByIdentity(broker.serviceIdentity().buildIdentity(person));
main = person.getMainAddress();
main.setCity("Wilmington");
broker.commitTransaction();
broker.clearCache();
person = (Person) broker.getObjectByIdentity(broker.serviceIdentity().buildIdentity(person));
assertEquals("Wilmington", person.getMainAddress().getCity());
}
public void testRollback()
{
Person person = new Person();
person.setFirstname("Brian");
broker.store(person);
broker.clearCache();
broker.beginTransaction();
person = (Person) broker.getObjectByIdentity(broker.serviceIdentity().buildIdentity(person));
person.setFirstname("Paul");
broker.abortTransaction();
assertEquals("Brian", person.getFirstname());
broker.clearCache();
person = (Person) broker.getObjectByIdentity(broker.serviceIdentity().buildIdentity(person));
assertEquals("Brian", person.getFirstname());
}
public void testInsertByReachability()
{
Person person = new Person();
person.setFirstname("Brian");
broker.store(person);
broker.clearCache();
broker.beginTransaction();
person = (Person) broker.getObjectByIdentity(broker.serviceIdentity().buildIdentity(person));
Address address = new Address("USA", "Wilmington", "Wombat Court");
person.setMainAddress(address);
broker.commitTransaction();
broker.clearCache();
person = (Person) broker.getObjectByIdentity(broker.serviceIdentity().buildIdentity(person));
address = person.getMainAddress();
assertNotNull(address);
assertEquals("USA", address.getCountry());
}
/**
* This test "works" because addOtherAddress actually inserts an
* AddressDesc with a reference to the address. The Address
* must be inserted before the AddressDesc in order for
* the FK's to work, but traversal finds the AddressDesc before
* the Address
*/
public void testStatementReordering()
{
Person person = new Person();
person.setFirstname("Brian");
broker.store(person);
broker.clearCache();
broker.beginTransaction();
person = (Person) broker.getObjectByIdentity(broker.serviceIdentity().buildIdentity(person));
Address address = new Address("USA", "Wilmington", "Wombat Court");
// should be disabled to have valid test -- broker.store(address);
person.addOtherAddress("Store", address);
broker.commitTransaction();
broker.clearCache();
person = (Person) broker.getObjectByIdentity(broker.serviceIdentity().buildIdentity(person));
Collection others = person.getOtherAddresses();
assertNotNull(others);
assertEquals(1, others.size());
AddressDesc desc = (AddressDesc) others.iterator().next();
assertNotNull(desc);
assertEquals(address.getCity(), desc.getAddress().getCity());
}
}
1.1 db-ojb/src/test/org/apache/ojb/dirty/Person.java
Index: Person.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 java.io.Serializable;
import java.util.ArrayList;
public class Person implements Serializable
{
private int id;
private String firstname;
private String lastname;
private Integer mainAddressId;
private Address mainAddress;
private ArrayList otherAddresses;
public Person()
{
}
public Person(String firstname, String lastname)
{
this.firstname = firstname;
this.lastname = lastname;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getFirstname()
{
return firstname;
}
public void setFirstname(String firstname)
{
this.firstname = firstname;
}
public String getLastname()
{
return lastname;
}
public void setLastname(String lastname)
{
this.lastname = lastname;
}
public Integer getMainAddressId()
{
return mainAddressId;
}
public void setMainAddressId(Integer mainAddressId)
{
this.mainAddressId = mainAddressId;
}
public Address getMainAddress()
{
return mainAddress;
}
public void setMainAddress(Address mainAddress)
{
this.mainAddress = mainAddress;
}
public ArrayList getOtherAddresses()
{
return otherAddresses;
}
public void setOtherAddresses(ArrayList otherAddresses)
{
this.otherAddresses = otherAddresses;
}
public void addOtherAddress(String desc, Address address)
{
if (otherAddresses == null)
{
otherAddresses = new ArrayList();
}
AddressDesc addrDesc = new AddressDesc(desc, address);
this.otherAddresses.add(addrDesc);
addrDesc.setPerson(this);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org