Author: arminw Date: Tue Feb 6 17:09:14 2007 New Revision: 504399 URL: http://svn.apache.org/viewvc?view=rev&rev=504399 Log: add new tests
Modified: db/ojb/trunk/src/test/org/apache/ojb/broker/InheritanceMultipleTableTest.java db/ojb/trunk/src/test/org/apache/ojb/odmg/ObjectImageTest.java Modified: db/ojb/trunk/src/test/org/apache/ojb/broker/InheritanceMultipleTableTest.java URL: http://svn.apache.org/viewvc/db/ojb/trunk/src/test/org/apache/ojb/broker/InheritanceMultipleTableTest.java?view=diff&rev=504399&r1=504398&r2=504399 ============================================================================== --- db/ojb/trunk/src/test/org/apache/ojb/broker/InheritanceMultipleTableTest.java (original) +++ db/ojb/trunk/src/test/org/apache/ojb/broker/InheritanceMultipleTableTest.java Tue Feb 6 17:09:14 2007 @@ -32,11 +32,71 @@ junit.textui.TestRunner.main(new String[]{InheritanceMultipleTableTest.class.getName()}); } - public void setUp() throws Exception + public void testRefreshObject() { - super.setUp(); - // allow to batch statements - broker.serviceBatchManager().setBatchMode(true); + ClassDescriptor cld = broker.getClassDescriptor(FourthA.class); + boolean old = cld.isAlwaysRefresh(); + try + { + cld.setAlwaysRefresh(true); + + long timestamp = System.currentTimeMillis(); + String name = "testRefreshObject_" + timestamp; + String t_name = "fourth_" + name; + + FourthA fourth = new FourthA(t_name); + fourth.setFourth("fourth"); + fourth.setThird("third"); + fourth.setSecond("second"); + + broker.beginTransaction(); + broker.store(fourth); + broker.commitTransaction(); + + Identity oid_3 = broker.serviceIdentity().buildIdentity(fourth); + // we expect a DB roundup, because we force refresh + FourthA newFourth = (FourthA) broker.getObjectByIdentity(oid_3); + + assertNotNull(newFourth); + assertEquals(t_name, newFourth.getName()); + assertEquals("fourth", fourth.getFourth()); + assertEquals("third", fourth.getThird()); + assertEquals("second", fourth.getSecond()); + + newFourth.setName(newFourth.getName() + "_updated"); + newFourth.setFourth("fourth_updated"); + newFourth.setThird("third_updated"); + newFourth.setSecond("second_updated"); + + newFourth = (FourthA) SerializationUtils.clone(newFourth); + + broker.beginTransaction(); + broker.store(newFourth); + broker.commitTransaction(); + + // we expect a DB roundup, because we force refresh + newFourth = (FourthA) broker.getObjectByIdentity(oid_3); + + assertNotNull(newFourth); + assertEquals(t_name + "_updated", newFourth.getName()); + assertEquals("second_updated", newFourth.getSecond()); + assertEquals("third_updated", newFourth.getThird()); + assertEquals("fourth_updated", newFourth.getFourth()); + + newFourth = (FourthA) SerializationUtils.clone(newFourth); + + broker.beginTransaction(); + broker.delete(newFourth); + broker.commitTransaction(); + + newFourth = (FourthA) broker.getObjectByIdentity(oid_3); + + assertNull(newFourth); + } + finally + { + cld.setAlwaysRefresh(old); + } } /** @@ -72,6 +132,81 @@ //System.out.println("Result: " + result); assertEquals(Executive.class.getName(), result.getClass().getName()); assertEquals(executive, result); + } + + public void testQueryByExample_1() + { + long timestamp = System.currentTimeMillis(); + Long id_2 = new Long(timestamp); + String name = "testQueryByExample_1_" + timestamp; + Employee em1 = new Employee(id_2, "employee_" + name); + Executive ex1 = new Executive(id_2, "executive_" + name, "department_1", null); + Executive ex2 = new Executive(id_2, "executive_" + name, "department_2", null); + ArrayList list = new ArrayList(); + list.add(ex1); + list.add(ex2); + Manager m1 = new Manager(id_2, "manager_" + name); + m1.setExecutives(list); + + broker.beginTransaction(); + broker.store(em1); + broker.store(m1); + broker.commitTransaction(); + + broker.clearCache(); + + Criteria crit = new Criteria() + .addLike("executives.name", "%" + name) + .addEqualTo("name", "manager_" + name) + .addIsNull("department") + .addIsNull("manager") + .addIsNull("address"); + Query q = QueryFactory.newQuery(Manager.class, crit, true); + //System.out.println("crit: " + crit.toString()); + + Collection result = broker.getCollectionByQuery(q); + assertEquals(1, result.size()); + + Manager newM1 = (Manager) broker.getObjectByQuery(q); + assertNotNull(newM1); + assertEquals(2, newM1.getExecutives().size()); + assertEquals(m1.getId(), newM1.getId()); + assertEquals(m1.getId_2(), newM1.getId_2()); + } + + public void testQueryByExample_2() + { + long timestamp = System.currentTimeMillis(); + Long id_2 = new Long(timestamp); + String name = "testQueryByExample_2_" + timestamp; + Employee em1 = new Employee(id_2, "employee_" + name); + Executive ex1 = new Executive(id_2, "executive_" + name, "department_1", null); + Executive ex2 = new Executive(id_2, "executive_" + name, "department_2", null); + ArrayList list = new ArrayList(); + list.add(ex1); + list.add(ex2); + Manager m1 = new Manager(id_2, "manager_" + name); + m1.setExecutives(list); + + broker.beginTransaction(); + broker.store(em1); + broker.store(m1); + broker.commitTransaction(); + + broker.clearCache(); + + Manager tmp = new Manager(); + tmp.setName("manager_" + name); + + Query q = QueryFactory.newQueryByExample(tmp); + System.out.println("crit: " +q.toString()); + + Manager newM1 = (Manager) broker.getObjectByQuery(q); + assertNotNull(newM1); + assertEquals("manager_" + name, newM1.getName()); + assertEquals(2, newM1.getExecutives().size()); + assertEquals(m1.getId(), newM1.getId()); + assertEquals(m1.getId_2(), newM1.getId_2()); } public void testLookupByIdentity() Modified: db/ojb/trunk/src/test/org/apache/ojb/odmg/ObjectImageTest.java URL: http://svn.apache.org/viewvc/db/ojb/trunk/src/test/org/apache/ojb/odmg/ObjectImageTest.java?view=diff&rev=504399&r1=504398&r2=504399 ============================================================================== --- db/ojb/trunk/src/test/org/apache/ojb/odmg/ObjectImageTest.java (original) +++ db/ojb/trunk/src/test/org/apache/ojb/odmg/ObjectImageTest.java Tue Feb 6 17:09:14 2007 @@ -24,6 +24,7 @@ import org.apache.commons.lang.SerializationUtils; import org.apache.commons.lang.SystemUtils; +import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.ojb.broker.Identity; import org.apache.ojb.broker.query.Criteria; @@ -34,6 +35,9 @@ import org.apache.ojb.broker.core.proxy.IndirectionHandler; import org.apache.ojb.broker.core.proxy.MaterializationListener; import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor; +import org.apache.ojb.broker.metadata.MetadataManager; +import org.apache.ojb.broker.metadata.DescriptorRepository; +import org.apache.ojb.broker.metadata.ClassDescriptor; import org.apache.ojb.junit.ODMGTestCase; import org.odmg.OQLQuery; import org.odmg.Transaction; @@ -48,7 +52,6 @@ * Test cases for refactored odmg-api implementation. These tests asserts previously outstanding * ODMG-issues and proxy object handling in the ODMG API. * - * @author <a href="mailto:[EMAIL PROTECTED]">Armin Waibel</a> * @version $Id$ */ public class ObjectImageTest extends ODMGTestCase @@ -61,6 +64,200 @@ public static void main(String[] args) { junit.textui.TestRunner.main(new String[]{ObjectImageTest.class.getName()}); + } + + private MetadataManager getMetadataManager() + { + return ojb.getMetadataManager(); + } + + /** + * if the state detection is disabled, we don't expect any object changes + */ + public void testStateDetection_1() throws Exception + { + String name = "testStateDetection_1_" + System.currentTimeMillis(); + MetadataManager mm = getMetadataManager(); + DescriptorRepository repository = mm.getRepository(); + boolean oldState = repository.isStateDetection(); + // disable object state detection + repository.setStateDetection(false); + + try + { + disabledStateDetection(name); + } + finally + { + // restore old state + repository = mm.getRepository(); + repository.setStateDetection(oldState); + } + } + + /** + * if the state detection is disabled, we don't expect any object changes + */ + public void testStateDetection_2() throws Exception + { + String name = "testStateDetection_2_" + System.currentTimeMillis(); + MetadataManager mm = getMetadataManager(); + ClassDescriptor cld = mm.getRepository().getDescriptorFor(Book.class); + boolean oldState = cld.isStateDetection(); + // disable object state detection + cld.setStateDetection(false); + + try + { + disabledStateDetection(name); + } + finally + { + // restore old state + cld = mm.getRepository().getDescriptorFor(Book.class); + cld.setStateDetection(oldState); + } + } + + /** + * if the state detection is disabled, we don't expect any object changes + */ + private void disabledStateDetection(String name) throws Exception + { + Date date = new Date(); + byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; + Book book = new Book(name, date, cover); + + TransactionExt tx = (TransactionExt) odmg.newTransaction(); + tx.begin(); + database.makePersistent(book); + tx.commit(); + + Integer version = book.getVersion(); + assertTrue(book.getVersion().intValue() > 0); + + tx.begin(); + tx.getBroker().clearCache(); + OQLQuery query = odmg.newOQLQuery(); + query.create("select books from " + Book.class.getName() + " where title like $1"); + query.bind(name); + Collection result = (Collection) query.execute(); + assertEquals(1, result.size()); + Book b = (Book) result.iterator().next(); + assertEquals(version, b.getVersion()); + tx.commit(); + + tx.begin(); + tx.lock(b, Transaction.WRITE); + b.setCover(new byte[]{2,3,4,5,6,7,8,8}); + b.setPublicationDate(new Date(1)); + tx.commit(); + + assertEquals(version, book.getVersion()); + tx.begin(); + tx.getBroker().clearCache(); + query = odmg.newOQLQuery(); + query.create("select books from " + Book.class.getName() + " where title like $1"); + query.bind(name); + result = (Collection) query.execute(); + assertEquals(version, b.getVersion()); + b = (Book) result.iterator().next(); + assertEquals(version, b.getVersion()); + tx.commit(); + + // System.err.println("### 4. commit, changed date"); + assertEquals(version, b.getVersion()); + assertEquals(date, b.getPublicationDate()); + assertTrue(ArrayUtils.isEquals(cover, b.getCover())); + } + + /** + * if the state detection of a field is disabled, we don't expect any + * automatic object changes if this field change + */ + public void testStateDetection_3() throws Exception + { + MetadataManager mm = getMetadataManager(); + ClassDescriptor cld = mm.getRepository().getDescriptorFor(Book.class); + boolean oldState = cld.getFieldDescriptorByName("title").isStateDetection(); + // disable field state detection + cld.getFieldDescriptorByName("title").setStateDetection(false); + + try + { + String name = "testStateDetection_3_" + System.currentTimeMillis(); + Date date = new Date(); + byte[] cover = new byte[]{2,3,4,5,6,7,8,9}; + Book book = new Book(name, date, cover); + + TransactionExt tx = (TransactionExt) odmg.newTransaction(); + tx.begin(); + database.makePersistent(book); + tx.commit(); + + Integer version = book.getVersion(); + assertTrue(book.getVersion().intValue() > 0); + + tx.begin(); + tx.getBroker().clearCache(); + OQLQuery query = odmg.newOQLQuery(); + query.create("select books from " + Book.class.getName() + " where title like $1"); + query.bind(name); + Collection result = (Collection) query.execute(); + assertEquals(1, result.size()); + Book b = (Book) result.iterator().next(); + assertEquals(version, b.getVersion()); + tx.commit(); + + tx.begin(); + tx.lock(b, Transaction.WRITE); + b.setTitle(b.getTitle() + "_updated"); + tx.commit(); + + // field 'title' has disabled state detection, so nothing should happen + assertEquals(version, book.getVersion()); + tx.begin(); + tx.getBroker().clearCache(); + query = odmg.newOQLQuery(); + query.create("select books from " + Book.class.getName() + " where title like $1"); + query.bind(name); + result = (Collection) query.execute(); + assertEquals(version, b.getVersion()); + b = (Book) result.iterator().next(); + assertEquals(version, b.getVersion()); + tx.commit(); + + // System.err.println("### 4. commit, changed date"); + assertEquals(version, b.getVersion()); + assertEquals(date, b.getPublicationDate()); + assertTrue(ArrayUtils.isEquals(cover, b.getCover())); + + tx.begin(); + tx.lock(b, Transaction.WRITE); + b.setCover(new byte[]{2,3,4,5,6,7,8,8}); + b.setPublicationDate(new Date(1)); + tx.commit(); + + // other fields have state detection enabled, so we expect + // automatic object update + assertEquals(version.intValue() + 1, b.getVersion().intValue()); + tx.begin(); + tx.getBroker().clearCache(); + query = odmg.newOQLQuery(); + query.create("select books from " + Book.class.getName() + " where title like $1"); + query.bind(name); + result = (Collection) query.execute(); + assertFalse(version.equals(b.getVersion())); + b = (Book) result.iterator().next(); + assertEquals(version.intValue() + 1, b.getVersion().intValue()); + tx.commit(); + } + finally + { + // restore old state + cld = mm.getRepository().getDescriptorFor(Book.class); + cld.getFieldDescriptorByName("title").setStateDetection(oldState); + } } public void testReplaceOneToOneReference() throws Exception --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]