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 ar...@apache.org on 2004/04/22 18:12:35 UTC
cvs commit: db-ojb/src/test/org/apache/ojb/broker AnonymousFieldsTest.java
arminw 2004/04/22 09:12:35
Modified: src/test/org/apache/ojb/broker AnonymousFieldsTest.java
Log:
new test cases testing 1:1 references with anonymous keys.
Revision Changes Path
1.13 +265 -102 db-ojb/src/test/org/apache/ojb/broker/AnonymousFieldsTest.java
Index: AnonymousFieldsTest.java
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/AnonymousFieldsTest.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- AnonymousFieldsTest.java 11 Jan 2004 01:30:46 -0000 1.12
+++ AnonymousFieldsTest.java 22 Apr 2004 16:12:35 -0000 1.13
@@ -1,69 +1,34 @@
package org.apache.ojb.broker;
-import java.util.Collection;
+import java.io.Serializable;
import java.util.ArrayList;
+import java.util.Collection;
-import junit.framework.TestCase;
+import org.apache.commons.lang.SerializationUtils;
import org.apache.ojb.broker.ObjectRepository.F1;
import org.apache.ojb.broker.query.Criteria;
import org.apache.ojb.broker.query.Query;
import org.apache.ojb.broker.query.QueryByCriteria;
import org.apache.ojb.broker.query.QueryFactory;
-import org.apache.commons.lang.SerializationUtils;
+import org.apache.ojb.junit.PBTestCase;
/**
* This TestClass tests the anonymous fields feature.
*/
-public class AnonymousFieldsTest extends TestCase
+public class AnonymousFieldsTest extends PBTestCase
{
public static void main(String[] args)
{
- String[] arr = { AnonymousFieldsTest.class.getName()};
+ String[] arr = {AnonymousFieldsTest.class.getName()};
junit.textui.TestRunner.main(arr);
}
- private PersistenceBroker broker;
-
- /**
- * Insert the method's description here. Creation date: (24.12.2000
- * 00:33:40)
- */
- public AnonymousFieldsTest(String name)
- {
- super(name);
- }
-
- /**
- * Insert the method's description here. Creation date: (06.12.2000
- * 21:58:53)
- */
- public void setUp() throws Exception
- {
- broker = PersistenceBrokerFactory.defaultPersistenceBroker();
- }
-
- /**
- * Insert the method's description here. Creation date: (06.12.2000
- * 21:59:14)
- */
- public void tearDown()
- {
- try
- {
- broker.clearCache();
- broker.close();
- }
- catch (PersistenceBrokerException e)
- {
- }
- }
-
public void testHandlingOfMultipleAnonymousFieldPerObject()
{
- String prefix = "testHandlingOfMultipleAnonymousFieldPerObject"+System.currentTimeMillis()+"_";
+ String prefix = "testHandlingOfMultipleAnonymousFieldPerObject_" + System.currentTimeMillis() + "_";
- ObjectRepository.Component comp = new ObjectRepository.Component();
- comp.setName(prefix+"main_component");
+ ObjectRepository.Component parent = new ObjectRepository.Component();
+ parent.setName(prefix + "main_component");
ObjectRepository.Component compSub1 = new ObjectRepository.Component();
compSub1.setName(prefix + "sub_1");
@@ -77,62 +42,62 @@
ObjectRepository.Group group = new ObjectRepository.Group();
group.setName(prefix + "test_group");
- compSub1.setParentComponent(comp);
- compSub2.setParentComponent(comp);
- compSub3.setParentComponent(comp);
+ compSub1.setParentComponent(parent);
+ compSub2.setParentComponent(parent);
+ compSub3.setParentComponent(parent);
ArrayList list = new ArrayList();
list.add(compSub1);
list.add(compSub2);
list.add(compSub3);
- comp.setChildComponents(list);
- comp.setGroup(group);
+ parent.setChildComponents(list);
+ parent.setGroup(group);
broker.beginTransaction();
- broker.store(comp);
+ broker.store(parent);
broker.commitTransaction();
broker.clearCache();
- Query query = QueryFactory.newQuery(comp);
- comp = (ObjectRepository.Component) broker.getObjectByQuery(query);
+ Query query = QueryFactory.newQuery(parent);
+ parent = (ObjectRepository.Component) broker.getObjectByQuery(query);
Query groupQuery = QueryFactory.newQuery(group);
ObjectRepository.Group lookedUpGroup = (ObjectRepository.Group) broker.getObjectByQuery(groupQuery);
- assertNotNull(comp);
- assertNotNull(comp.getGroup());
- assertNotNull(comp.getChildComponents());
- assertNotNull(comp.getName());
+ assertNotNull(parent);
+ assertNotNull(parent.getGroup());
+ assertNotNull(parent.getChildComponents());
+ assertNotNull(parent.getName());
assertNotNull(lookedUpGroup);
- assertEquals(3, comp.getChildComponents().size());
- assertEquals(group.getName(), (comp.getGroup().getName()));
+ assertEquals(3, parent.getChildComponents().size());
+ assertEquals(group.getName(), (parent.getGroup().getName()));
- comp.setName(prefix+"updated_comp_name");
- comp.setGroup(null);
+ parent.setName(prefix + "updated_comp_name");
+ parent.setGroup(null);
broker.beginTransaction();
- broker.store(comp);
+ broker.store(parent);
broker.commitTransaction();
broker.clearCache();
- query = QueryFactory.newQuery(comp);
- comp = (ObjectRepository.Component) broker.getObjectByQuery(query);
+ query = QueryFactory.newQuery(parent);
+ parent = (ObjectRepository.Component) broker.getObjectByQuery(query);
- assertNotNull(comp);
- assertNull(comp.getGroup());
- assertNotNull(comp.getChildComponents());
- assertNotNull(comp.getName());
+ assertNotNull(parent);
+ assertNull(parent.getGroup());
+ assertNotNull(parent.getChildComponents());
+ assertNotNull(parent.getName());
- assertEquals(3, comp.getChildComponents().size());
- assertEquals(prefix+"updated_comp_name", comp.getName());
+ assertEquals(3, parent.getChildComponents().size());
+ assertEquals(prefix + "updated_comp_name", parent.getName());
broker.beginTransaction();
- broker.delete(comp);
+ broker.delete(parent);
broker.commitTransaction();
- comp = (ObjectRepository.Component) broker.getObjectByQuery(query);
+ parent = (ObjectRepository.Component) broker.getObjectByQuery(query);
- assertNull(comp);
+ assertNull(parent);
groupQuery = QueryFactory.newQuery(group);
lookedUpGroup = (ObjectRepository.Group) broker.getObjectByQuery(groupQuery);
assertNotNull(lookedUpGroup);
@@ -142,12 +107,12 @@
* test handling of serialized objects (simulate
* handling of objects across different JVM, e.g. AppServer)
*/
- public void testHandlingOfSerializedObjects()
+ public void testSerializedObjectsDelete()
{
- String prefix = "testHandlingOfSerializedObjects"+System.currentTimeMillis()+"_";
+ String prefix = "testSerializedObjectsDelete_" + System.currentTimeMillis() + "_";
ObjectRepository.Component comp = new ObjectRepository.Component();
- comp.setName(prefix+"main_component");
+ comp.setName(prefix + "main_component");
ObjectRepository.Component compSub1 = new ObjectRepository.Component();
compSub1.setName(prefix + "sub_1");
@@ -191,7 +156,7 @@
assertEquals(3, comp.getChildComponents().size());
assertEquals(group.getName(), (comp.getGroup().getName()));
- comp.setName(prefix+"updated_comp_name");
+ comp.setName(prefix + "updated_comp_name");
comp.setGroup(null);
comp = (ObjectRepository.Component) SerializationUtils.deserialize(SerializationUtils.serialize(comp));
@@ -210,13 +175,15 @@
assertNotNull(comp.getName());
assertEquals(3, comp.getChildComponents().size());
- assertEquals(prefix+"updated_comp_name", comp.getName());
-
- comp = (ObjectRepository.Component) SerializationUtils.deserialize(SerializationUtils.serialize(comp));
+ assertEquals(prefix + "updated_comp_name", comp.getName());
+ //*****************************************
+ // now we generate a deep copy
+ comp = (ObjectRepository.Component) serializeDeserializeObject(comp);
broker.beginTransaction();
broker.delete(comp);
broker.commitTransaction();
+ //*****************************************
comp = (ObjectRepository.Component) broker.getObjectByQuery(query);
@@ -226,9 +193,200 @@
assertNotNull(lookedUpGroup);
}
+ public void testSerializedObjectsUpdate()
+ {
+ String prefix = "testSerializedObjectsUpdate_" + System.currentTimeMillis() + "_";
+
+ ObjectRepository.Component parent = new ObjectRepository.Component();
+ parent.setName(prefix + "main_component");
+
+ ObjectRepository.Component compSub1 = new ObjectRepository.Component();
+ compSub1.setName(prefix + "sub_1");
+
+ ObjectRepository.Component compSub2 = new ObjectRepository.Component();
+ compSub2.setName(prefix + "sub_2");
+
+ ObjectRepository.Component compSub3 = new ObjectRepository.Component();
+ compSub2.setName(prefix + "sub_3");
+
+ ObjectRepository.Group group = new ObjectRepository.Group();
+ group.setName(prefix + "test_group");
+
+ compSub1.setParentComponent(parent);
+ compSub2.setParentComponent(parent);
+ compSub3.setParentComponent(parent);
+ ArrayList list = new ArrayList();
+ list.add(compSub1);
+ list.add(compSub2);
+ list.add(compSub3);
+ parent.setChildComponents(list);
+ parent.setGroup(group);
+
+ broker.beginTransaction();
+ broker.store(parent);
+ broker.commitTransaction();
+
+ broker.clearCache();
+ Query query = QueryFactory.newQuery(parent);
+ parent = (ObjectRepository.Component) broker.getObjectByQuery(query);
+
+ Query groupQuery = QueryFactory.newQuery(group);
+ ObjectRepository.Group lookedUpGroup = (ObjectRepository.Group) broker.getObjectByQuery(groupQuery);
+
+ assertNotNull(parent);
+ assertNotNull(parent.getGroup());
+ assertNotNull(parent.getChildComponents());
+ assertNotNull(parent.getName());
+ assertNotNull(lookedUpGroup);
+
+ assertEquals(3, parent.getChildComponents().size());
+ assertEquals(group.getName(), (parent.getGroup().getName()));
+
+ parent.setName(prefix + "updated_comp_name");
+ parent.setGroup(null);
+
+ //**************************************************
+ // now we generate a deep copy
+ parent = (ObjectRepository.Component) serializeDeserializeObject(parent);
+ broker.beginTransaction();
+ broker.store(parent);
+ broker.commitTransaction();
+ //**************************************************
+
+ query = QueryFactory.newQuery(parent);
+ parent = (ObjectRepository.Component) broker.getObjectByQuery(query);
+
+ assertNotNull(parent);
+ assertNull(parent.getGroup());
+ assertNotNull(parent.getChildComponents());
+ assertNotNull(parent.getName());
+
+ assertEquals(3, parent.getChildComponents().size());
+ assertEquals(prefix + "updated_comp_name", parent.getName());
+
+ // same with cleared cache
+ broker.clearCache();
+ query = QueryFactory.newQuery(parent);
+ parent = (ObjectRepository.Component) broker.getObjectByQuery(query);
+
+ assertNotNull(parent);
+ assertNull(parent.getGroup());
+ assertNotNull(parent.getChildComponents());
+ assertNotNull(parent.getName());
+
+ assertEquals(3, parent.getChildComponents().size());
+ assertEquals(prefix + "updated_comp_name", parent.getName());
+
+ broker.beginTransaction();
+ broker.delete(parent);
+ broker.commitTransaction();
+
+ parent = (ObjectRepository.Component) broker.getObjectByQuery(query);
+
+ assertNull(parent);
+ groupQuery = QueryFactory.newQuery(group);
+ lookedUpGroup = (ObjectRepository.Group) broker.getObjectByQuery(groupQuery);
+ assertNotNull(lookedUpGroup);
+ }
+
+ public void testSerializedObjectsRefresh()
+ {
+ String prefix = "testSerializedObjectsRefresh_" + System.currentTimeMillis() + "_";
+ ObjectRepository.Component parent = new ObjectRepository.Component();
+ parent.setName(prefix + "main_component");
+
+ ObjectRepository.Component compSub1 = new ObjectRepository.Component();
+ compSub1.setName(prefix + "sub_1");
+
+ ObjectRepository.Component compSub2 = new ObjectRepository.Component();
+ compSub2.setName(prefix + "sub_2");
+
+ ObjectRepository.Component compSub3 = new ObjectRepository.Component();
+ compSub2.setName(prefix + "sub_3");
+
+ ObjectRepository.Group group = new ObjectRepository.Group();
+ group.setName(prefix + "test_group");
+
+ compSub1.setParentComponent(parent);
+ compSub2.setParentComponent(parent);
+ compSub3.setParentComponent(parent);
+ ArrayList list = new ArrayList();
+ list.add(compSub1);
+ list.add(compSub2);
+ list.add(compSub3);
+ parent.setChildComponents(list);
+ parent.setGroup(group);
+
+ broker.beginTransaction();
+ broker.store(parent);
+ broker.commitTransaction();
+
+ broker.clearCache();
+ Query query = QueryFactory.newQuery(parent);
+ parent = (ObjectRepository.Component) broker.getObjectByQuery(query);
+
+ Query groupQuery = QueryFactory.newQuery(group);
+ ObjectRepository.Group lookedUpGroup = (ObjectRepository.Group) broker.getObjectByQuery(groupQuery);
+
+ assertNotNull(parent);
+ assertNotNull(parent.getGroup());
+ assertNotNull(parent.getChildComponents());
+ assertNotNull(parent.getName());
+ assertEquals(3, parent.getChildComponents().size());
+ assertEquals(group.getName(), (parent.getGroup().getName()));
+ ObjectRepository.Component aChild = (ObjectRepository.Component) parent.getChildComponents().iterator().next();
+ assertNotNull(aChild);
+ assertNotNull(aChild.getParentComponent());
+ assertEquals(parent, aChild.getParentComponent());
+ assertNotNull(lookedUpGroup);
+
+ //*************************************
+ assertNotNull(parent);
+ assertNotNull(parent.getGroup());
+ parent = (ObjectRepository.Component) serializeDeserializeObject(parent);
+ broker.retrieveAllReferences(parent);
+ assertNotNull(parent);
+ /*
+ Now we have a problem! After serialization we can't find the anonymous keys
+ for parent object, because object identity has changed!!
+ This is now fixed in class QueryReferenceBroker#getReferencedObjectIdentity
+ */
+ assertNotNull(parent.getGroup());
+ //*************************************
+ assertNotNull(parent.getChildComponents());
+ assertNotNull(parent.getName());
+ assertEquals(3, parent.getChildComponents().size());
+ aChild = (ObjectRepository.Component) parent.getChildComponents().iterator().next();
+ assertNotNull(aChild);
+ assertNotNull(aChild.getParentComponent());
+ assertEquals(parent, aChild.getParentComponent());
+
+ broker.beginTransaction();
+ broker.store(parent);
+ broker.commitTransaction();
+
+ // now nothing should happen, because we don't make any changes
+ broker.clearCache();
+ query = QueryFactory.newQuery(parent);
+ parent = (ObjectRepository.Component) broker.getObjectByQuery(query);
+ groupQuery = QueryFactory.newQuery(group);
+ lookedUpGroup = (ObjectRepository.Group) broker.getObjectByQuery(groupQuery);
+ assertNotNull(parent);
+ assertNotNull(parent.getGroup());
+ assertNotNull(parent.getChildComponents());
+ assertNotNull(parent.getName());
+ assertEquals(3, parent.getChildComponents().size());
+ assertEquals(group.getName(), (parent.getGroup().getName()));
+ aChild = (ObjectRepository.Component) parent.getChildComponents().iterator().next();
+ assertNotNull(aChild);
+ assertNotNull(aChild.getParentComponent());
+ assertEquals(parent, aChild.getParentComponent());
+ assertNotNull(lookedUpGroup);
+ }
+
/**
- * write an entry using vertical inheritance and try to read it again. E-F
- */
+ * write an entry using vertical inheritance and try to read it again. E-F
+ */
public void testVerticalInheritanceStoreAndLoad() throws Exception
{
// produce some test data
@@ -251,9 +409,9 @@
}
/**
- * write an entry using vertical inheritance and try to read it again.
- * E-F-G
- */
+ * write an entry using vertical inheritance and try to read it again.
+ * E-F-G
+ */
public void testVerticalInheritanceStoreAndLoad2() throws Exception
{
// produce some test data G
@@ -278,9 +436,9 @@
}
/**
- * write an entry using vertical inheritance and try to read it again.
- * E-F1-G1, autoincrement id in E
- */
+ * write an entry using vertical inheritance and try to read it again.
+ * E-F1-G1, autoincrement id in E
+ */
public void testVerticalInheritanceStoreAndLoad3() throws Exception
{
// produce some test data G1
@@ -305,8 +463,8 @@
}
/**
- * write an entry using vertical inheritance and try to read it again. E-F
- */
+ * write an entry using vertical inheritance and try to read it again. E-F
+ */
public void testVerticalInheritanceUpdate() throws Exception
{
// produce some test data
@@ -335,9 +493,9 @@
}
/**
- * write an entry using vertical inheritance and try to read it again.
- * E-F-G
- */
+ * write an entry using vertical inheritance and try to read it again.
+ * E-F-G
+ */
public void testVerticalInheritanceUpdate2() throws Exception
{
// produce some test data
@@ -369,9 +527,9 @@
}
/**
- * write an entry using vertical inheritance and try to read it again.
- * E-F1-G1, autoincrement id in E
- */
+ * write an entry using vertical inheritance and try to read it again.
+ * E-F1-G1, autoincrement id in E
+ */
public void testVerticalInheritanceUpdate3() throws Exception
{
// produce some test data
@@ -403,8 +561,8 @@
}
/**
- * Query Attribute of Super class E-F
- */
+ * Query Attribute of Super class E-F
+ */
public void testQuerySuperField()
{
int data1 = (int) (Math.random() * Integer.MAX_VALUE);
@@ -440,8 +598,8 @@
}
/**
- * Query Attribute of Super class E-F-G
- */
+ * Query Attribute of Super class E-F-G
+ */
public void testQuerySuperField2()
{
int data1 = (int) (Math.random() * Integer.MAX_VALUE);
@@ -497,14 +655,19 @@
// --------------------------------------------------------------------------------------
// Utiltity methods.
+ private Object serializeDeserializeObject(Serializable obj)
+ {
+ return (ObjectRepository.Component) SerializationUtils.deserialize(SerializationUtils.serialize(obj));
+ }
+
private Object findById(Class type, int id)
{
- Collection result = broker.getCollectionByQuery(createQueryById(type,id));
- if (result == null || result.size() == 0)
+ Collection result = broker.getCollectionByQuery(createQueryById(type, id));
+ if(result == null || result.size() == 0)
{
return null;
}
- if (result.size() > 1)
+ if(result.size() > 1)
{
throw new RuntimeException("Unexpected unique id constraint violation ");
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org