From: <hib...@li...> - 2006-03-04 00:18:04
|
Author: ste...@jb... Date: 2006-03-03 19:17:57 -0500 (Fri, 03 Mar 2006) New Revision: 9538 Added: trunk/Hibernate3/test/org/hibernate/test/instrument/buildtime/ trunk/Hibernate3/test/org/hibernate/test/instrument/buildtime/InstrumentTest.java trunk/Hibernate3/test/org/hibernate/test/instrument/domain/ trunk/Hibernate3/test/org/hibernate/test/instrument/domain/Document.java trunk/Hibernate3/test/org/hibernate/test/instrument/domain/Documents.hbm.xml trunk/Hibernate3/test/org/hibernate/test/instrument/domain/Folder.java trunk/Hibernate3/test/org/hibernate/test/instrument/domain/Owner.java trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/ trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/AbstractExecutable.java trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/AbstractTransformingClassLoaderInstrumentTestCase.java trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/CGLIBTest.java trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/Executable.java trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestDirtyCheckExecutable.java trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestFetchAllExecutable.java trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestInjectFieldInterceptorExecutable.java trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestLazyExecutable.java trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestLazyManyToOneExecutable.java Removed: trunk/Hibernate3/test/org/hibernate/test/instrument/Document.java trunk/Hibernate3/test/org/hibernate/test/instrument/Documents.hbm.xml trunk/Hibernate3/test/org/hibernate/test/instrument/Folder.java trunk/Hibernate3/test/org/hibernate/test/instrument/InstrumentTest.java trunk/Hibernate3/test/org/hibernate/test/instrument/Owner.java Log: HHH-1435 : lazy="no-proxy" on many/one-to-one associations; use of ~transforming class loaders~ for runtime instrumentation of domain objects for testing purposes Deleted: trunk/Hibernate3/test/org/hibernate/test/instrument/Document.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/Document.java 2006-03-03 10:51:04 UTC (rev 9537) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/Document.java 2006-03-04 00:17:57 UTC (rev 9538) @@ -1,110 +0,0 @@ -//$Id$ -package org.hibernate.test.instrument; - -import java.util.Date; - -/** - * @author Gavin King - */ -public class Document { - private Long id; - private String name; - private String upperCaseName; - private String summary; - private String text; - private Owner owner; - private Folder folder; - private Date lastTextModification = new Date(); - /** - * @return Returns the folder. - */ - public Folder getFolder() { - return folder; - } - /** - * @param folder The folder to set. - */ - public void setFolder(Folder folder) { - this.folder = folder; - } - /** - * @return Returns the owner. - */ - public Owner getOwner() { - return owner; - } - /** - * @param owner The owner to set. - */ - public void setOwner(Owner owner) { - this.owner = owner; - } - /** - * @return Returns the id. - */ - public Long getId() { - return id; - } - /** - * @param id The id to set. - */ - public void setId(Long id) { - this.id = id; - } - /** - * @return Returns the name. - */ - public String getName() { - return name; - } - /** - * @param name The name to set. - */ - public void setName(String name) { - this.name = name; - } - /** - * @return Returns the summary. - */ - public String getSummary() { - return summary; - } - /** - * @param summary The summary to set. - */ - public void setSummary(String summary) { - this.summary = summary; - } - /** - * @return Returns the text. - */ - public String getText() { - return text; - } - /** - * @param text The text to set. - */ - private void setText(String text) { - this.text = text; - } - /** - * @return Returns the upperCaseName. - */ - public String getUpperCaseName() { - return upperCaseName; - } - /** - * @param upperCaseName The upperCaseName to set. - */ - public void setUpperCaseName(String upperCaseName) { - this.upperCaseName = upperCaseName; - } - - public void updateText(String newText) { - if ( !newText.equals(text) ) { - this.text = newText; - lastTextModification = new Date(); - } - } - -} Deleted: trunk/Hibernate3/test/org/hibernate/test/instrument/Documents.hbm.xml =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/Documents.hbm.xml 2006-03-03 10:51:04 UTC (rev 9537) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/Documents.hbm.xml 2006-03-04 00:17:57 UTC (rev 9538) @@ -1,69 +0,0 @@ -<?xml version="1.0"?> -<!DOCTYPE hibernate-mapping PUBLIC - "-//Hibernate/Hibernate Mapping DTD 3.0//EN" - "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> - -<!-- - - This mapping demonstrates - - (1) use of lazy properties - this feature requires buildtime - bytecode instrumentation; we don't think this is a very - necessary feature, but provide it for completeleness; if - Hibernate encounters uninstrumented classes, lazy property - fetching will be silently disabled, to enable testing - - (2) use of a formula to define a "derived property" - ---> - -<hibernate-mapping - package="org.hibernate.test.instrument" - default-access="field"> - - <class name="Folder" table="folders"> - <id name="id"> - <generator class="native"/> - </id> - <property name="name" - not-null="true" - length="50"/> - <many-to-one name="parent"/> - <bag name="subfolders" - inverse="true" - cascade="save-update"> - <key column="parent"/> - <one-to-many class="Folder"/> - </bag> - <bag name="documents" - inverse="true" - cascade="all-delete-orphan"> - <key column="folder"/> - <one-to-many class="Document"/> - </bag> - </class> - - <class name="Owner" table="owners" lazy="false"> - <id name="id"> - <generator class="native"/> - </id> - <property name="name" - not-null="true" - length="50"/> - </class> - - <class name="Document" table="documents"> - <id name="id"> - <generator class="native"/> - </id> - <property name="name" not-null="true" length="50"/> - <property name="upperCaseName" formula="upper(name)" lazy="true"/> - <property name="summary" not-null="true" length="200" lazy="true"/> - <many-to-one name="folder" not-null="true" lazy="no-proxy"/> - <many-to-one name="owner" not-null="true" lazy="no-proxy" fetch="select"/> - <property name="text" not-null="true" length="2000" lazy="true"/> - <property name="lastTextModification" not-null="true" lazy="true" access="field"/> - </class> - - -</hibernate-mapping> Deleted: trunk/Hibernate3/test/org/hibernate/test/instrument/Folder.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/Folder.java 2006-03-03 10:51:04 UTC (rev 9537) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/Folder.java 2006-03-04 00:17:57 UTC (rev 9538) @@ -1,79 +0,0 @@ -//$Id$ -package org.hibernate.test.instrument; - -import java.util.ArrayList; -import java.util.Collection; - -/** - * @author Gavin King - */ -public class Folder { - private Long id; - private String name; - private Folder parent; - private Collection subfolders = new ArrayList(); - private Collection documents = new ArrayList(); - boolean nameWasread; - - /** - * @return Returns the id. - */ - public Long getId() { - return id; - } - /** - * @param id The id to set. - */ - public void setId(Long id) { - this.id = id; - } - /** - * @return Returns the name. - */ - public String getName() { - nameWasread = true; - return name; - } - /** - * @param name The name to set. - */ - public void setName(String name) { - this.name = name; - } - /** - * @return Returns the documents. - */ - public Collection getDocuments() { - return documents; - } - /** - * @param documents The documents to set. - */ - public void setDocuments(Collection documents) { - this.documents = documents; - } - /** - * @return Returns the parent. - */ - public Folder getParent() { - return parent; - } - /** - * @param parent The parent to set. - */ - public void setParent(Folder parent) { - this.parent = parent; - } - /** - * @return Returns the subfolders. - */ - public Collection getSubfolders() { - return subfolders; - } - /** - * @param subfolders The subfolders to set. - */ - public void setSubfolders(Collection subfolders) { - this.subfolders = subfolders; - } -} Deleted: trunk/Hibernate3/test/org/hibernate/test/instrument/InstrumentTest.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/InstrumentTest.java 2006-03-03 10:51:04 UTC (rev 9537) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/InstrumentTest.java 2006-03-04 00:17:57 UTC (rev 9538) @@ -1,307 +0,0 @@ -//$Id$ -package org.hibernate.test.instrument; - -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; - -import junit.framework.Test; -import junit.framework.TestSuite; - -import org.hibernate.Hibernate; -import org.hibernate.LockMode; -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.intercept.FieldInterceptionHelper; -import org.hibernate.test.TestCase; - -/** - * @author Gavin King - */ -public class InstrumentTest extends TestCase { - - public InstrumentTest(String str) { - super(str); - } - - public void testDirtyCheck() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - Folder pics = new Folder(); - pics.setName("pics"); - Folder docs = new Folder(); - docs.setName("docs"); - s.persist(docs); - s.persist(pics); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - List list = s.createCriteria(Folder.class).list(); - for ( Iterator iter = list.iterator(); iter.hasNext(); ) { - Folder f = (Folder) iter.next(); - assertFalse( f.nameWasread ); - } - t.commit(); - s.close(); - - for ( Iterator iter = list.iterator(); iter.hasNext(); ) { - Folder f = (Folder) iter.next(); - assertFalse( f.nameWasread ); - } - - s = openSession(); - t = s.beginTransaction(); - s.createQuery("delete from Folder").executeUpdate(); - t.commit(); - s.close(); - } - - public void testFetchAll() throws Exception { - Session s = openSession(); - Owner o = new Owner(); - Document doc = new Document(); - Folder fol = new Folder(); - o.setName("gavin"); - doc.setName("Hibernate in Action"); - doc.setSummary("blah"); - doc.updateText("blah blah"); - fol.setName("books"); - doc.setOwner(o); - doc.setFolder(fol); - fol.getDocuments().add(doc); - s.persist(o); - s.persist(fol); - s.flush(); - s.clear(); - doc = (Document) s.createQuery("from Document fetch all properties").uniqueResult(); - assertTrue( Hibernate.isPropertyInitialized( doc, "summary" ) ); - assertTrue( Hibernate.isPropertyInitialized( doc, "upperCaseName" ) ); - assertTrue( Hibernate.isPropertyInitialized( doc, "owner" ) ); - assertEquals( doc.getSummary(), "blah" ); - s.delete(doc); - s.delete( doc.getOwner() ); - s.delete( doc.getFolder() ); - s.flush(); - s.connection().commit(); - s.close(); - } - - public void testLazy() throws Exception { - Session s = openSession(); - Owner o = new Owner(); - Document doc = new Document(); - Folder fol = new Folder(); - o.setName("gavin"); - doc.setName("Hibernate in Action"); - doc.setSummary("blah"); - doc.updateText("blah blah"); - fol.setName("books"); - doc.setOwner(o); - doc.setFolder(fol); - fol.getDocuments().add(doc); - s.save(o); - s.save(fol); - s.flush(); - s.connection().commit(); - s.close(); - - getSessions().evict(Document.class); - - s = openSession(); - doc = (Document) s.createQuery("from Document").uniqueResult(); - doc.getName(); - assertEquals( doc.getText(), "blah blah" ); - s.connection().commit(); - s.close(); - - s = openSession(); - doc = (Document) s.createQuery("from Document").uniqueResult(); - doc.getName(); - assertFalse(Hibernate.isPropertyInitialized(doc, "text")); - assertFalse(Hibernate.isPropertyInitialized(doc, "summary")); - assertEquals( doc.getText(), "blah blah" ); - assertTrue(Hibernate.isPropertyInitialized(doc, "text")); - assertTrue(Hibernate.isPropertyInitialized(doc, "summary")); - s.connection().commit(); - s.close(); - - s = openSession(); - doc = (Document) s.createQuery("from Document").uniqueResult(); - doc.setName("HiA"); - s.flush(); - s.connection().commit(); - s.close(); - - s = openSession(); - doc = (Document) s.createQuery("from Document").uniqueResult(); - assertEquals( doc.getName(), "HiA" ); - assertEquals( doc.getText(), "blah blah" ); - s.connection().commit(); - s.close(); - - s = openSession(); - doc = (Document) s.createQuery("from Document").uniqueResult(); - doc.getText(); - doc.setName("HiA second edition"); - s.flush(); - s.connection().commit(); - s.close(); - - s = openSession(); - doc = (Document) s.createQuery("from Document").uniqueResult(); - assertTrue(Hibernate.isPropertyInitialized(doc, "weirdProperty")); - assertTrue(Hibernate.isPropertyInitialized(doc, "name")); - assertFalse(Hibernate.isPropertyInitialized(doc, "text")); - assertFalse(Hibernate.isPropertyInitialized(doc, "upperCaseName")); - //assertFalse(Hibernate.isPropertyInitialized(doc, "owner")); - assertEquals( doc.getName(), "HiA second edition" ); - assertEquals( doc.getText(), "blah blah" ); - assertEquals( doc.getUpperCaseName(), "HIA SECOND EDITION" ); - assertTrue(Hibernate.isPropertyInitialized(doc, "text")); - assertTrue(Hibernate.isPropertyInitialized(doc, "weirdProperty")); - assertTrue(Hibernate.isPropertyInitialized(doc, "upperCaseName")); - s.connection().commit(); - s.close(); - - s = openSession(); - doc = (Document) s.createQuery("from Document").uniqueResult(); - s.connection().commit(); - s.close(); - - assertFalse(Hibernate.isPropertyInitialized(doc, "text")); - - s = openSession(); - s.lock(doc, LockMode.NONE); - assertFalse(Hibernate.isPropertyInitialized(doc, "text")); - assertEquals( doc.getText(), "blah blah" ); - assertTrue(Hibernate.isPropertyInitialized(doc, "text")); - s.connection().commit(); - s.close(); - - s = openSession(); - doc = (Document) s.createQuery("from Document").uniqueResult(); - s.connection().commit(); - s.close(); - - doc.setName("HiA2"); - - assertFalse(Hibernate.isPropertyInitialized(doc, "text")); - - s = openSession(); - s.saveOrUpdate(doc); - s.flush(); - assertFalse(Hibernate.isPropertyInitialized(doc, "text")); - assertEquals( doc.getText(), "blah blah" ); - assertTrue(Hibernate.isPropertyInitialized(doc, "text")); - doc.updateText("blah blah blah blah"); - s.flush(); - s.connection().commit(); - s.close(); - - s = openSession(); - doc = (Document) s.createQuery("from Document").uniqueResult(); - assertEquals( doc.getName(), "HiA2" ); - assertEquals( doc.getText(), "blah blah blah blah" ); - s.connection().commit(); - s.close(); - - s = openSession(); - doc = (Document) s.load( Document.class, doc.getId() ); - doc.getName(); - assertFalse(Hibernate.isPropertyInitialized(doc, "text")); - assertFalse(Hibernate.isPropertyInitialized(doc, "summary")); - s.connection().commit(); - s.close(); - - s = openSession(); - doc = (Document) s.createQuery("from Document").uniqueResult(); - //s.delete(doc); - s.delete( doc.getFolder() ); - s.delete( doc.getOwner() ); - s.flush(); - s.connection().commit(); - s.close(); - - } - - public void testLazyManyToOne() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - Owner gavin = new Owner(); - Document hia = new Document(); - Folder fol = new Folder(); - gavin.setName("gavin"); - hia.setName("Hibernate in Action"); - hia.setSummary("blah"); - hia.updateText("blah blah"); - fol.setName("books"); - hia.setOwner(gavin); - hia.setFolder(fol); - fol.getDocuments().add(hia); - s.persist(gavin); - s.persist(fol); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - hia = (Document) s.createCriteria(Document.class).uniqueResult(); - assertEquals( hia.getFolder().getClass(), Folder.class); - fol = hia.getFolder(); - assertTrue( Hibernate.isInitialized(fol) ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - hia = (Document) s.createCriteria(Document.class).uniqueResult(); - assertSame( hia.getFolder(), s.load(Folder.class, fol.getId()) ); - assertTrue( Hibernate.isInitialized( hia.getFolder() ) ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - fol = (Folder) s.get(Folder.class, fol.getId()); - hia = (Document) s.createCriteria(Document.class).uniqueResult(); - assertSame( fol, hia.getFolder() ); - fol = hia.getFolder(); - assertTrue( Hibernate.isInitialized(fol) ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - fol = (Folder) s.load(Folder.class, fol.getId()); - hia = (Document) s.createCriteria(Document.class).uniqueResult(); - assertNotSame( fol, hia.getFolder() ); - fol = hia.getFolder(); - assertTrue( Hibernate.isInitialized(fol) ); - s.delete(hia.getFolder()); - s.delete(hia.getOwner()); - t.commit(); - s.close(); - } - - protected String[] getMappings() { - return new String[] { "instrument/Documents.hbm.xml" }; - } - - public void testSetFieldInterceptor() { - Document doc = new Document(); - FieldInterceptionHelper.injectFieldInterceptor( doc, "Document", new HashSet(), null ); - doc.getId(); - } - - public static Test suite() { - return new TestSuite(InstrumentTest.class); - } - - public static boolean isRunnable() { - return FieldInterceptionHelper.isInstrumented( new Document() ); - } - -} - Deleted: trunk/Hibernate3/test/org/hibernate/test/instrument/Owner.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/Owner.java 2006-03-03 10:51:04 UTC (rev 9537) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/Owner.java 2006-03-04 00:17:57 UTC (rev 9538) @@ -1,34 +0,0 @@ -//$Id$ -package org.hibernate.test.instrument; - -/** - * @author Gavin King - */ -public class Owner { - private Long id; - private String name; - /** - * @return Returns the id. - */ - public Long getId() { - return id; - } - /** - * @param id The id to set. - */ - public void setId(Long id) { - this.id = id; - } - /** - * @return Returns the name. - */ - public String getName() { - return name; - } - /** - * @param name The name to set. - */ - public void setName(String name) { - this.name = name; - } -} Copied: trunk/Hibernate3/test/org/hibernate/test/instrument/buildtime/InstrumentTest.java (from rev 9531, trunk/Hibernate3/test/org/hibernate/test/instrument/InstrumentTest.java) =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/InstrumentTest.java 2006-03-02 03:14:31 UTC (rev 9531) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/buildtime/InstrumentTest.java 2006-03-04 00:17:57 UTC (rev 9538) @@ -0,0 +1,370 @@ +//$Id$ +package org.hibernate.test.instrument.buildtime; + +import java.util.HashSet; +import java.net.URL; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.hibernate.Hibernate; +import org.hibernate.LockMode; +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.HibernateException; +import org.hibernate.bytecode.cglib.BytecodeProviderImpl; +import org.hibernate.intercept.FieldInterceptionHelper; +import org.hibernate.test.TestCase; +import org.hibernate.test.instrument.domain.Owner; +import org.hibernate.test.instrument.domain.Document; +import org.hibernate.test.instrument.domain.Folder; + +/** + * @author Gavin King + */ +public class InstrumentTest extends TestCase { + + private ClassLoader dynamicLoader; + private BytecodeProviderImpl provider; + + public InstrumentTest(String str) { + super(str); + } + + protected void setUp() throws Exception { + // need to setup the specialized classloader + String myFileName = InstrumentTest.class.getName().replace( '.', '/' ) + ".class"; + URL fileURL = this.getClass().getClassLoader().getResource( myFileName ); + String filePath = fileURL.getPath(); + String classPath = filePath.substring( 0, filePath.length() - myFileName.length() ); + provider = new BytecodeProviderImpl(); + dynamicLoader = provider.generateDynamicFieldInterceptionClassLoader( + Thread.currentThread().getContextClassLoader(), + new String[] { classPath }, + new String[] { "org.hibernate.test" } + ); + super.setUp(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + provider.releaseDynamicFieldInterceptionClassLoader( dynamicLoader ); + dynamicLoader = null; + provider = null; + } + + public void testDirtyCheck() { + executeExecutable( "org.hibernate.test.instrument.runtime.TestDirtyCheckExecutable" ); +// Session s = openSession(); +// Transaction t = s.beginTransaction(); +// Folder pics = new Folder(); +// pics.setName("pics"); +// Folder docs = new Folder(); +// docs.setName("docs"); +// s.persist(docs); +// s.persist(pics); +// t.commit(); +// s.close(); +// +// s = openSession(); +// t = s.beginTransaction(); +// List list = s.createCriteria(Folder.class).list(); +// for ( Iterator iter = list.iterator(); iter.hasNext(); ) { +// Folder f = (Folder) iter.next(); +// assertFalse( f.nameWasread ); +// } +// t.commit(); +// s.close(); +// +// for ( Iterator iter = list.iterator(); iter.hasNext(); ) { +// Folder f = (Folder) iter.next(); +// assertFalse( f.nameWasread ); +// } +// +// s = openSession(); +// t = s.beginTransaction(); +// s.createQuery("delete from Folder").executeUpdate(); +// t.commit(); +// s.close(); + } + + public void testFetchAll() throws Exception { + Session s = openSession(); + Owner o = new Owner(); + Document doc = new Document(); + Folder fol = new Folder(); + o.setName("gavin"); + doc.setName("Hibernate in Action"); + doc.setSummary("blah"); + doc.updateText("blah blah"); + fol.setName("books"); + doc.setOwner(o); + doc.setFolder(fol); + fol.getDocuments().add(doc); + s.persist(o); + s.persist(fol); + s.flush(); + s.clear(); + doc = (Document) s.createQuery("from Document fetch all properties").uniqueResult(); + assertTrue( Hibernate.isPropertyInitialized( doc, "summary" ) ); + assertTrue( Hibernate.isPropertyInitialized( doc, "upperCaseName" ) ); + assertTrue( Hibernate.isPropertyInitialized( doc, "owner" ) ); + assertEquals( doc.getSummary(), "blah" ); + s.delete(doc); + s.delete( doc.getOwner() ); + s.delete( doc.getFolder() ); + s.flush(); + s.connection().commit(); + s.close(); + } + + public void testLazy() throws Exception { + Session s = openSession(); + Transaction t = s.beginTransaction(); + Owner o = new Owner(); + Document doc = new Document(); + Folder fol = new Folder(); + o.setName("gavin"); + doc.setName("Hibernate in Action"); + doc.setSummary("blah"); + doc.updateText("blah blah"); + fol.setName("books"); + doc.setOwner(o); + doc.setFolder(fol); + fol.getDocuments().add(doc); + s.save(o); + s.save(fol); + t.commit(); + s.close(); + + getSessions().evict(Document.class); + + s = openSession(); + t = s.beginTransaction(); + doc = ( Document ) s.get( Document.class, doc.getId() ); + assertTrue(Hibernate.isPropertyInitialized(doc, "weirdProperty")); + assertTrue(Hibernate.isPropertyInitialized(doc, "name")); + assertFalse(Hibernate.isPropertyInitialized(doc, "text")); + assertFalse(Hibernate.isPropertyInitialized(doc, "upperCaseName")); + assertFalse(Hibernate.isPropertyInitialized(doc, "folder")); + assertFalse(Hibernate.isPropertyInitialized(doc, "owner")); + doc.getUpperCaseName(); // should force initialization + assertTrue(Hibernate.isPropertyInitialized(doc, "text")); + assertTrue(Hibernate.isPropertyInitialized(doc, "weirdProperty")); + assertTrue(Hibernate.isPropertyInitialized(doc, "upperCaseName")); + assertTrue(Hibernate.isPropertyInitialized(doc, "folder")); + assertTrue(Hibernate.isPropertyInitialized(doc, "owner")); + t.commit(); + s.close(); + + getSessions().evict(Document.class); + + s = openSession(); + doc = (Document) s.createQuery("from Document").uniqueResult(); + doc.getName(); + assertEquals( doc.getText(), "blah blah" ); + s.connection().commit(); + s.close(); + + s = openSession(); + doc = (Document) s.createQuery("from Document").uniqueResult(); + doc.getName(); + assertFalse(Hibernate.isPropertyInitialized(doc, "text")); + assertFalse(Hibernate.isPropertyInitialized(doc, "summary")); + assertEquals( doc.getText(), "blah blah" ); + assertTrue(Hibernate.isPropertyInitialized(doc, "text")); + assertTrue(Hibernate.isPropertyInitialized(doc, "summary")); + s.connection().commit(); + s.close(); + + s = openSession(); + doc = ( Document ) s.createQuery("from Document").uniqueResult(); + doc.setName("HiA"); + s.flush(); + s.connection().commit(); + s.close(); + + s = openSession(); + doc = (Document) s.createQuery("from Document").uniqueResult(); + assertEquals( doc.getName(), "HiA" ); + assertEquals( doc.getText(), "blah blah" ); + s.connection().commit(); + s.close(); + + s = openSession(); + doc = (Document) s.createQuery("from Document").uniqueResult(); + doc.getText(); + doc.setName("HiA second edition"); + s.flush(); + s.connection().commit(); + s.close(); + + s = openSession(); + doc = (Document) s.createQuery("from Document").uniqueResult(); + assertTrue(Hibernate.isPropertyInitialized(doc, "weirdProperty")); + assertTrue(Hibernate.isPropertyInitialized(doc, "name")); + assertFalse(Hibernate.isPropertyInitialized(doc, "text")); + assertFalse(Hibernate.isPropertyInitialized(doc, "upperCaseName")); + assertFalse(Hibernate.isPropertyInitialized(doc, "owner")); + assertEquals( doc.getName(), "HiA second edition" ); + assertEquals( doc.getText(), "blah blah" ); + assertEquals( doc.getUpperCaseName(), "HIA SECOND EDITION" ); + assertTrue(Hibernate.isPropertyInitialized(doc, "text")); + assertTrue(Hibernate.isPropertyInitialized(doc, "weirdProperty")); + assertTrue(Hibernate.isPropertyInitialized(doc, "upperCaseName")); + s.connection().commit(); + s.close(); + + s = openSession(); + doc = (Document) s.createQuery("from Document").uniqueResult(); + s.connection().commit(); + s.close(); + + assertFalse(Hibernate.isPropertyInitialized(doc, "text")); + + s = openSession(); + s.lock(doc, LockMode.NONE); + assertFalse(Hibernate.isPropertyInitialized(doc, "text")); + assertEquals( doc.getText(), "blah blah" ); + assertTrue(Hibernate.isPropertyInitialized(doc, "text")); + s.connection().commit(); + s.close(); + + s = openSession(); + doc = (Document) s.createQuery("from Document").uniqueResult(); + s.connection().commit(); + s.close(); + + doc.setName("HiA2"); + + assertFalse(Hibernate.isPropertyInitialized(doc, "text")); + + s = openSession(); + s.saveOrUpdate(doc); + s.flush(); + assertFalse(Hibernate.isPropertyInitialized(doc, "text")); + assertEquals( doc.getText(), "blah blah" ); + assertTrue(Hibernate.isPropertyInitialized(doc, "text")); + doc.updateText("blah blah blah blah"); + s.flush(); + s.connection().commit(); + s.close(); + + s = openSession(); + doc = (Document) s.createQuery("from Document").uniqueResult(); + assertEquals( doc.getName(), "HiA2" ); + assertEquals( doc.getText(), "blah blah blah blah" ); + s.connection().commit(); + s.close(); + + s = openSession(); + doc = (Document) s.load( Document.class, doc.getId() ); + doc.getName(); + assertFalse(Hibernate.isPropertyInitialized(doc, "text")); + assertFalse(Hibernate.isPropertyInitialized(doc, "summary")); + s.connection().commit(); + s.close(); + + s = openSession(); + doc = (Document) s.createQuery("from Document").uniqueResult(); + //s.delete(doc); + s.delete( doc.getFolder() ); + s.delete( doc.getOwner() ); + s.flush(); + s.connection().commit(); + s.close(); + + } + + public void testLazyManyToOne() { + Session s = openSession(); + Transaction t = s.beginTransaction(); + Owner gavin = new Owner(); + Document hia = new Document(); + Folder fol = new Folder(); + gavin.setName("gavin"); + hia.setName("Hibernate in Action"); + hia.setSummary("blah"); + hia.updateText("blah blah"); + fol.setName("books"); + hia.setOwner(gavin); + hia.setFolder(fol); + fol.getDocuments().add(hia); + s.persist(gavin); + s.persist(fol); + t.commit(); + s.close(); + + s = openSession(); + t = s.beginTransaction(); + hia = (Document) s.createCriteria(Document.class).uniqueResult(); + assertEquals( hia.getFolder().getClass(), Folder.class); + fol = hia.getFolder(); + assertTrue( Hibernate.isInitialized(fol) ); + t.commit(); + s.close(); + + s = openSession(); + t = s.beginTransaction(); + hia = (Document) s.createCriteria(Document.class).uniqueResult(); + assertSame( hia.getFolder(), s.load(Folder.class, fol.getId()) ); + assertTrue( Hibernate.isInitialized( hia.getFolder() ) ); + t.commit(); + s.close(); + + s = openSession(); + t = s.beginTransaction(); + fol = (Folder) s.get(Folder.class, fol.getId()); + hia = (Document) s.createCriteria(Document.class).uniqueResult(); + assertSame( fol, hia.getFolder() ); + fol = hia.getFolder(); + assertTrue( Hibernate.isInitialized(fol) ); + t.commit(); + s.close(); + + s = openSession(); + t = s.beginTransaction(); + fol = (Folder) s.load(Folder.class, fol.getId()); + hia = (Document) s.createCriteria(Document.class).uniqueResult(); + assertNotSame( fol, hia.getFolder() ); + fol = hia.getFolder(); + assertTrue( Hibernate.isInitialized(fol) ); + s.delete(hia.getFolder()); + s.delete(hia.getOwner()); + t.commit(); + s.close(); + } + + protected String[] getMappings() { + return new String[] { "instrument/Documents.hbm.xml" }; + } + + public void testSetFieldInterceptor() { + Document doc = new Document(); + FieldInterceptionHelper.injectFieldInterceptor( doc, "Document", new HashSet(), null ); + doc.getId(); + } + + public static Test suite() { + return new TestSuite(InstrumentTest.class); + } + + public static boolean isRunnable() { + return FieldInterceptionHelper.isInstrumented( new Document() ); + } + + private static final Class[] SIG = new Class[] { TestCase.class }; + + public void executeExecutable(String name) { + try { + Class execClass = dynamicLoader.loadClass( name ); + Object executable = execClass.newInstance(); + execClass.getDeclaredMethod( "execute", SIG ).invoke( executable, new Object[] { this } ); + + } + catch( Throwable t ) { + throw new HibernateException( "could not load executable", t ); + } + } +} + Property changes on: trunk/Hibernate3/test/org/hibernate/test/instrument/buildtime/InstrumentTest.java ___________________________________________________________________ Name: svn:executable + * Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Copied: trunk/Hibernate3/test/org/hibernate/test/instrument/domain/Document.java (from rev 9531, trunk/Hibernate3/test/org/hibernate/test/instrument/Document.java) =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/Document.java 2006-03-02 03:14:31 UTC (rev 9531) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/domain/Document.java 2006-03-04 00:17:57 UTC (rev 9538) @@ -0,0 +1,110 @@ +//$Id$ +package org.hibernate.test.instrument.domain; + +import java.util.Date; + +/** + * @author Gavin King + */ +public class Document { + private Long id; + private String name; + private String upperCaseName; + private String summary; + private String text; + private Owner owner; + private Folder folder; + private Date lastTextModification = new Date(); + /** + * @return Returns the folder. + */ + public Folder getFolder() { + return folder; + } + /** + * @param folder The folder to set. + */ + public void setFolder(Folder folder) { + this.folder = folder; + } + /** + * @return Returns the owner. + */ + public Owner getOwner() { + return owner; + } + /** + * @param owner The owner to set. + */ + public void setOwner(Owner owner) { + this.owner = owner; + } + /** + * @return Returns the id. + */ + public Long getId() { + return id; + } + /** + * @param id The id to set. + */ + public void setId(Long id) { + this.id = id; + } + /** + * @return Returns the name. + */ + public String getName() { + return name; + } + /** + * @param name The name to set. + */ + public void setName(String name) { + this.name = name; + } + /** + * @return Returns the summary. + */ + public String getSummary() { + return summary; + } + /** + * @param summary The summary to set. + */ + public void setSummary(String summary) { + this.summary = summary; + } + /** + * @return Returns the text. + */ + public String getText() { + return text; + } + /** + * @param text The text to set. + */ + private void setText(String text) { + this.text = text; + } + /** + * @return Returns the upperCaseName. + */ + public String getUpperCaseName() { + return upperCaseName; + } + /** + * @param upperCaseName The upperCaseName to set. + */ + public void setUpperCaseName(String upperCaseName) { + this.upperCaseName = upperCaseName; + } + + public void updateText(String newText) { + if ( !newText.equals(text) ) { + this.text = newText; + lastTextModification = new Date(); + } + } + +} Property changes on: trunk/Hibernate3/test/org/hibernate/test/instrument/domain/Document.java ___________________________________________________________________ Name: svn:executable + * Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Copied: trunk/Hibernate3/test/org/hibernate/test/instrument/domain/Documents.hbm.xml (from rev 9531, trunk/Hibernate3/test/org/hibernate/test/instrument/Documents.hbm.xml) =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/Documents.hbm.xml 2006-03-02 03:14:31 UTC (rev 9531) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/domain/Documents.hbm.xml 2006-03-04 00:17:57 UTC (rev 9538) @@ -0,0 +1,67 @@ +<?xml version="1.0"?> +<!DOCTYPE hibernate-mapping PUBLIC + "-//Hibernate/Hibernate Mapping DTD 3.0//EN" + "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> + +<!-- + + This mapping demonstrates + + (1) use of lazy properties - this feature requires buildtime + bytecode instrumentation; we don't think this is a very + necessary feature, but provide it for completeleness; if + Hibernate encounters uninstrumented classes, lazy property + fetching will be silently disabled, to enable testing + + (2) use of a formula to define a "derived property" + +--> + +<hibernate-mapping package="org.hibernate.test.instrument.domain" default-access="field"> + + <class name="Folder" table="folders"> + <id name="id"> + <generator class="native"/> + </id> + <property name="name" + not-null="true" + length="50"/> + <many-to-one name="parent"/> + <bag name="subfolders" + inverse="true" + cascade="save-update"> + <key column="parent"/> + <one-to-many class="Folder"/> + </bag> + <bag name="documents" + inverse="true" + cascade="all-delete-orphan"> + <key column="folder"/> + <one-to-many class="Document"/> + </bag> + </class> + + <class name="Owner" table="owners" lazy="false"> + <id name="id"> + <generator class="native"/> + </id> + <property name="name" + not-null="true" + length="50"/> + </class> + + <class name="Document" table="documents"> + <id name="id"> + <generator class="native"/> + </id> + <property name="name" not-null="true" length="50"/> + <property name="upperCaseName" formula="upper(name)" lazy="true"/> + <property name="summary" not-null="true" length="200" lazy="true"/> + <many-to-one name="folder" not-null="true" lazy="no-proxy"/> + <many-to-one name="owner" not-null="true" lazy="no-proxy" fetch="select"/> + <property name="text" not-null="true" length="2000" lazy="true"/> + <property name="lastTextModification" not-null="true" lazy="true" access="field"/> + </class> + + +</hibernate-mapping> Property changes on: trunk/Hibernate3/test/org/hibernate/test/instrument/domain/Documents.hbm.xml ___________________________________________________________________ Name: svn:executable + * Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Copied: trunk/Hibernate3/test/org/hibernate/test/instrument/domain/Folder.java (from rev 9531, trunk/Hibernate3/test/org/hibernate/test/instrument/Folder.java) =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/Folder.java 2006-03-02 03:14:31 UTC (rev 9531) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/domain/Folder.java 2006-03-04 00:17:57 UTC (rev 9538) @@ -0,0 +1,80 @@ +//$Id$ +package org.hibernate.test.instrument.domain; + +import java.util.ArrayList; +import java.util.Collection; + +/** + * @author Gavin King + */ +public class Folder { + private Long id; + private String name; + private Folder parent; + private Collection subfolders = new ArrayList(); + private Collection documents = new ArrayList(); + + public boolean nameWasread; + + /** + * @return Returns the id. + */ + public Long getId() { + return id; + } + /** + * @param id The id to set. + */ + public void setId(Long id) { + this.id = id; + } + /** + * @return Returns the name. + */ + public String getName() { + nameWasread = true; + return name; + } + /** + * @param name The name to set. + */ + public void setName(String name) { + this.name = name; + } + /** + * @return Returns the documents. + */ + public Collection getDocuments() { + return documents; + } + /** + * @param documents The documents to set. + */ + public void setDocuments(Collection documents) { + this.documents = documents; + } + /** + * @return Returns the parent. + */ + public Folder getParent() { + return parent; + } + /** + * @param parent The parent to set. + */ + public void setParent(Folder parent) { + this.parent = parent; + } + /** + * @return Returns the subfolders. + */ + public Collection getSubfolders() { + return subfolders; + } + /** + * @param subfolders The subfolders to set. + */ + public void setSubfolders(Collection subfolders) { + this.subfolders = subfolders; + } +} Property changes on: trunk/Hibernate3/test/org/hibernate/test/instrument/domain/Folder.java ___________________________________________________________________ Name: svn:executable + * Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Copied: trunk/Hibernate3/test/org/hibernate/test/instrument/domain/Owner.java (from rev 9531, trunk/Hibernate3/test/org/hibernate/test/instrument/Owner.java) =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/Owner.java 2006-03-02 03:14:31 UTC (rev 9531) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/domain/Owner.java 2006-03-04 00:17:57 UTC (rev 9538) @@ -0,0 +1,34 @@ +//$Id$ +package org.hibernate.test.instrument.domain; + +/** + * @author Gavin King + */ +public class Owner { + private Long id; + private String name; + /** + * @return Returns the id. + */ + public Long getId() { + return id; + } + /** + * @param id The id to set. + */ + public void setId(Long id) { + this.id = id; + } + /** + * @return Returns the name. + */ + public String getName() { + return name; + } + /** + * @param name The name to set. + */ + public void setName(String name) { + this.name = name; + } +} Property changes on: trunk/Hibernate3/test/org/hibernate/test/instrument/domain/Owner.java ___________________________________________________________________ Name: svn:executable + * Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Added: trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/AbstractExecutable.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/AbstractExecutable.java 2006-03-03 10:51:04 UTC (rev 9537) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/AbstractExecutable.java 2006-03-04 00:17:57 UTC (rev 9538) @@ -0,0 +1,29 @@ +package org.hibernate.test.instrument.runtime; + +import org.hibernate.SessionFactory; +import org.hibernate.test.instrument.runtime.Executable; +import org.hibernate.cfg.Configuration; +import org.hibernate.cfg.Environment; + +/** + * @author Steve Ebersole + */ +public abstract class AbstractExecutable implements Executable { + + private SessionFactory factory; + + public void prepare() { + factory = new Configuration() + .setProperty( Environment.HBM2DDL_AUTO, "create-drop" ) + .addResource( "org/hibernate/test/instrument/domain/Documents.hbm.xml" ) + .buildSessionFactory(); + } + + public void complete() { + factory.close(); + } + + protected SessionFactory getFactory() { + return factory; + } +} Added: trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/AbstractTransformingClassLoaderInstrumentTestCase.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/AbstractTransformingClassLoaderInstrumentTestCase.java 2006-03-03 10:51:04 UTC (rev 9537) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/AbstractTransformingClassLoaderInstrumentTestCase.java 2006-03-04 00:17:57 UTC (rev 9538) @@ -0,0 +1,97 @@ +package org.hibernate.test.instrument.runtime; + +import org.hibernate.test.AbstractClassLoaderIsolatedTestCase; +import org.hibernate.bytecode.BytecodeProvider; +import org.hibernate.HibernateException; + +import java.net.URL; +import java.lang.reflect.InvocationTargetException; + +/** + * @author Steve Ebersole + */ +public abstract class AbstractTransformingClassLoaderInstrumentTestCase extends AbstractClassLoaderIsolatedTestCase { + + private BytecodeProvider provider; + + protected ClassLoader buildIsolatedClassLoader(ClassLoader parent) { + String myFileName = AbstractTransformingClassLoaderInstrumentTestCase.class.getName().replace( '.', '/' ) + ".class"; + URL fileURL = this.getClass().getClassLoader().getResource( myFileName ); + String filePath = fileURL.getPath(); + String classPath = filePath.substring( 0, filePath.length() - myFileName.length() ); + provider = buildBytecodeProvider(); + return provider.generateDynamicFieldInterceptionClassLoader( + parent, + new String[] { classPath }, + new String[] { "org.hibernate.test.instrument" } + ); + } + + protected void releaseIsolatedClassLoader(ClassLoader isolatedLoader) { + provider.releaseDynamicFieldInterceptionClassLoader( isolatedLoader ); + provider = null; + } + + protected abstract BytecodeProvider buildBytecodeProvider(); + + + // the tests ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + public void testSetFieldInterceptor() { + executeExecutable( "org.hibernate.test.instrument.runtime.TestInjectFieldInterceptorExecutable" ); + } + + public void testDirtyCheck() { + executeExecutable( "org.hibernate.test.instrument.runtime.TestDirtyCheckExecutable" ); + } + + public void testFetchAll() throws Exception { + executeExecutable( "org.hibernate.test.instrument.runtime.TestFetchAllExecutable" ); + } + + public void testLazy() { + executeExecutable( "org.hibernate.test.instrument.runtime.TestLazyExecutable" ); + } + + public void testLazyManyToOne() { + executeExecutable( "org.hibernate.test.instrument.runtime.TestLazyManyToOneExecutable" ); + } + + + // reflection code to ensure isolation into the created classloader ~~~~~~~ + + private static final Class[] SIG = new Class[] {}; + private static final Object[] ARGS = new Object[] {}; + + public void executeExecutable(String name) { + Class execClass = null; + Object executable = null; + try { + execClass = Thread.currentThread().getContextClassLoader().loadClass( name ); + executable = execClass.newInstance(); + } + catch( Throwable t ) { + throw new HibernateException( "could not load executable", t ); + } + try { + execClass.getMethod( "prepare", SIG ).invoke( executable, ARGS ); + execClass.getMethod( "execute", SIG ).invoke( executable, ARGS ); + } + catch ( NoSuchMethodException e ) { + throw new HibernateException( "could not exeucte executable", e ); + } + catch ( IllegalAccessException e ) { + throw new HibernateException( "could not exeucte executable", e ); + } + catch ( InvocationTargetException e ) { + throw new HibernateException( "could not exeucte executable", e.getTargetException() ); + } + finally { + try { + execClass.getMethod( "complete", SIG ).invoke( executable, ARGS ); + } + catch ( Throwable ignore ) { + } + } + } +} Added: trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/CGLIBTest.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/CGLIBTest.java 2006-03-03 10:51:04 UTC (rev 9537) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/CGLIBTest.java 2006-03-04 00:17:57 UTC (rev 9538) @@ -0,0 +1,13 @@ +package org.hibernate.test.instrument.runtime; + +import org.hibernate.bytecode.BytecodeProvider; +import org.hibernate.bytecode.cglib.BytecodeProviderImpl; + +/** + * @author Steve Ebersole + */ +public class CGLIBTest extends AbstractTransformingClassLoaderInstrumentTestCase { + protected BytecodeProvider buildBytecodeProvider() { + return new BytecodeProviderImpl(); + } +} Added: trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/Executable.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/Executable.java 2006-03-03 10:51:04 UTC (rev 9537) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/Executable.java 2006-03-04 00:17:57 UTC (rev 9538) @@ -0,0 +1,10 @@ +package org.hibernate.test.instrument.runtime; + +/** + * @author Steve Ebersole + */ +public interface Executable { + public void prepare(); + public void execute(); + public void complete(); +} Added: trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestDirtyCheckExecutable.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestDirtyCheckExecutable.java 2006-03-03 10:51:04 UTC (rev 9537) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestDirtyCheckExecutable.java 2006-03-04 00:17:57 UTC (rev 9538) @@ -0,0 +1,50 @@ +package org.hibernate.test.instrument.runtime; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.test.instrument.runtime.AbstractExecutable; +import org.hibernate.test.instrument.domain.Folder; + +import java.util.List; +import java.util.Iterator; + +import junit.framework.Assert; + +/** + * @author Steve Ebersole + */ +public class TestDirtyCheckExecutable extends AbstractExecutable { + public void execute() { + Session s = getFactory().openSession(); + Transaction t = s.beginTransaction(); + Folder pics = new Folder(); + pics.setName("pics"); + Folder docs = new Folder(); + docs.setName("docs"); + s.persist(docs); + s.persist(pics); + t.commit(); + s.close(); + + s = getFactory().openSession(); + t = s.beginTransaction(); + List list = s.createCriteria(Folder.class).list(); + for ( Iterator iter = list.iterator(); iter.hasNext(); ) { + Folder f = (Folder) iter.next(); + Assert.assertFalse( f.nameWasread ); + } + t.commit(); + s.close(); + + for ( Iterator iter = list.iterator(); iter.hasNext(); ) { + Folder f = (Folder) iter.next(); + Assert.assertFalse( f.nameWasread ); + } + + s = getFactory().openSession(); + t = s.beginTransaction(); + s.createQuery("delete from Folder").executeUpdate(); + t.commit(); + s.close(); + } +} Added: trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestFetchAllExecutable.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestFetchAllExecutable.java 2006-03-03 10:51:04 UTC (rev 9537) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestFetchAllExecutable.java 2006-03-04 00:17:57 UTC (rev 9538) @@ -0,0 +1,47 @@ +package org.hibernate.test.instrument.runtime; + +import org.hibernate.Session; +import org.hibernate.Hibernate; +import org.hibernate.Transaction; +import org.hibernate.test.instrument.domain.Owner; +import org.hibernate.test.instrument.domain.Document; +import org.hibernate.test.instrument.domain.Folder; +import junit.framework.Assert; + +/** + * @author Steve Ebersole + */ +public class TestFetchAllExecutable extends AbstractExecutable { + public void execute() { + Session s = getFactory().openSession(); + Transaction t = s.beginTransaction(); + Owner o = new Owner(); + Document doc = new Document(); + Folder fol = new Folder(); + o.setName("gavin"); + doc.setName("Hibernate in Action"); + doc.setSummary("blah"); + doc.updateText("blah blah"); + fol.setName("books"); + doc.setOwner(o); + doc.setFolder(fol); + fol.getDocuments().add(doc); + s.persist(o); + s.persist(fol); + t.commit(); + s.close(); + + s = getFactory().openSession(); + t = s.beginTransaction(); + doc = (Document) s.createQuery("from Document fetch all properties").uniqueResult(); + Assert.assertTrue( Hibernate.isPropertyInitialized( doc, "summary" ) ); + Assert.assertTrue( Hibernate.isPropertyInitialized( doc, "upperCaseName" ) ); + Assert.assertTrue( Hibernate.isPropertyInitialized( doc, "owner" ) ); + Assert.assertEquals( doc.getSummary(), "blah" ); + s.delete(doc); + s.delete( doc.getOwner() ); + s.delete( doc.getFolder() ); + t.commit(); + s.close(); + } +} Added: trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestInjectFieldInterceptorExecutable.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestInjectFieldInterceptorExecutable.java 2006-03-03 10:51:04 UTC (rev 9537) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestInjectFieldInterceptorExecutable.java 2006-03-04 00:17:57 UTC (rev 9538) @@ -0,0 +1,17 @@ +package org.hibernate.test.instrument.runtime; + +import org.hibernate.test.instrument.domain.Document; +import org.hibernate.intercept.FieldInterceptionHelper; + +import java.util.HashSet; + +/** + * @author Steve Ebersole + */ +public class TestInjectFieldInterceptorExecutable extends AbstractExecutable { + public void execute() { + Document doc = new Document(); + FieldInterceptionHelper.injectFieldInterceptor( doc, "Document", new HashSet(), null ); + doc.getId(); + } +} Added: trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestLazyExecutable.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestLazyExecutable.java 2006-03-03 10:51:04 UTC (rev 9537) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestLazyExecutable.java 2006-03-04 00:17:57 UTC (rev 9538) @@ -0,0 +1,197 @@ +package org.hibernate.test.instrument.runtime; + +import org.hibernate.test.TestCase; +import org.hibernate.test.instrument.runtime.AbstractExecutable; +import org.hibernate.test.instrument.domain.Owner; +import org.hibernate.test.instrument.domain.Document; +import org.hibernate.test.instrument.domain.Folder; +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.Hibernate; +import org.hibernate.LockMode; +import org.hibernate.CacheMode; +import org.hibernate.SessionFactory; + +/** + * @author Steve Ebersole + */ +public class TestLazyExecutable extends AbstractExecutable { + public void execute() { + SessionFactory factory = getFactory(); + Session s = factory.openSession(); + Transaction t = s.beginTransaction(); + Owner o = new Owner(); + Document doc = new Document(); + Folder fol = new Folder(); + o.setName("gavin"); + doc.setName("Hibernate in Action"); + doc.setSummary("blah"); + doc.updateText("blah blah"); + fol.setName("books"); + doc.setOwner(o); + doc.setFolder(fol); + fol.getDocuments().add(doc); + s.save(o); + s.save(fol); + t.commit(); + s.close(); + + s = factory.openSession(); + s.setCacheMode( CacheMode.IGNORE ); + t = s.beginTransaction(); + doc = ( Document ) s.get( Document.class, doc.getId() ); + TestCase.assertTrue( Hibernate.isPropertyInitialized(doc, "weirdProperty")); + TestCase.assertTrue(Hibernate.isPropertyInitialized(doc, "name")); + TestCase.assertFalse(Hibernate.isPropertyInitialized(doc, "text")); + TestCase.assertFalse(Hibernate.isPropertyInitialized(doc, "upperCaseName")); + TestCase.assertFalse(Hibernate.isPropertyInitialized(doc, "folder")); + TestCase.assertFalse(Hibernate.isPropertyInitialized(doc, "owner")); + doc.getUpperCaseName(); // should force initialization + TestCase.assertTrue(Hibernate.isPropertyInitialized(doc, "text")); + TestCase.assertTrue(Hibernate.isPropertyInitialized(doc, "weirdProperty")); + TestCase.assertTrue(Hibernate.isPropertyInitialized(doc, "upperCaseName")); + TestCase.assertTrue(Hibernate.isPropertyInitialized(doc, "folder")); + TestCase.assertTrue(Hibernate.isPropertyInitialized(doc, "owner")); + t.commit(); + s.close(); + + s = factory.openSession(); + s.setCacheMode( CacheMode.IGNORE ); + t = s.beginTransaction(); + doc = (Document) s.createQuery("from Document").uniqueResult(); + doc.getName(); + TestCase.assertEquals( doc.getText(), "blah blah" ); + t.commit(); + s.close(); + + s = factory.openSession(); + s.setCacheMode( CacheMode.IGNORE ); + t = s.beginTransaction(); + doc = (Document) s.createQuery("from Document").uniqueResult(); + doc.getName(); + TestCase.assertFalse(Hibernate.isPropertyInitialized(doc, "text")); + TestCase.assertFalse(Hibernate.isPropertyInitialized(doc, "summary")); + TestCase.assertEquals( doc.getText(), "blah blah" ); + TestCase.assertTrue(Hibernate.isPropertyInitialized(doc, "text")); + TestCase.assertTrue(Hibernate.isPropertyInitialized(doc, "summary")); + t.commit(); + s.close(); + + s = factory.openSession(); + s.setCacheMode( CacheMode.IGNORE ); + t = s.beginTransaction(); + doc = (Document) s.createQuery("from Document").uniqueResult(); + doc.setName("HiA"); + t.commit(); + s.close(); + + s = factory.openSession(); + s.setCacheMode( CacheMode.IGNORE ); + t = s.beginTransaction(); + doc = (Document) s.createQuery("from Document").uniqueResult(); + TestCase.assertEquals( doc.getName(), "HiA" ); + TestCase.assertEquals( doc.getText(), "blah blah" ); + t.commit(); + s.close(); + + s = factory.openSession(); + s.setCacheMode( CacheMode.IGNORE ); + t = s.beginTransaction(); + doc = (Document) s.createQuery("from Document").uniqueResult(); + doc.getText(); + doc.setName("HiA second edition"); + t.commit(); + s.close(); + + s = factory.openSession(); + s.setCacheMode( CacheMode.IGNORE ); + t = s.beginTransaction(); + doc = (Document) s.createQuery("from Document").uniqueResult(); + TestCase.assertTrue(Hibernate.isPropertyInitialized(doc, "weirdProperty")); + TestCase.assertTrue(Hibernate.isPropertyInitialized(doc, "name")); + TestCase.assertFalse(Hibernate.isPropertyInitialized(doc, "text")); + TestCase.assertFalse(Hibernate.isPropertyInitialized(doc, "upperCaseName")); + TestCase.assertFalse(Hibernate.isPropertyInitialized(doc, "owner")); + TestCase.assertEquals( doc.getName(), "HiA second edition" ); + TestCase.assertEquals( doc.getText(), "blah blah" ); + TestCase.assertEquals( doc.getUpperCaseName(), "HIA SECOND EDITION" ); + TestCase.assertTrue(Hibernate.isPropertyInitialized(doc, "text")); + TestCase.assertTrue(Hibernate.isPropertyInitialized(doc, "weirdProperty")); + TestCase.assertTrue(Hibernate.isPropertyInitialized(doc, "upperCaseName")); + t.commit(); + s.close(); + + s = factory.openSession(); + s.setCacheMode( CacheMode.IGNORE ); + t = s.beginTransaction(); + doc = (Document) s.createQuery("from Document").uniqueResult(); + t.commit(); + s.close(); + + TestCase.assertFalse(Hibernate.isPropertyInitialized(doc, "text")); + + s = factory.openSession(); + s.setCacheMode( CacheMode.IGNORE ); + t = s.beginTransaction(); + s.lock(doc, LockMode.NONE); + TestCase.assertFalse(Hibernate.isPropertyInitialized(doc, "text")); + TestCase.assertEquals( doc.getText(), "blah blah" ); + TestCase.assertTrue(Hibernate.isPropertyInitialized(doc, "text")); + t.commit(); + s.close(); + + s = factory.openSession(); + s.setCacheMode( CacheMode.IGNORE ); + t = s.beginTransaction(); + doc = (Document) s.createQuery("from Document").uniqueResult(); + t.commit(); + s.close(); + + doc.setName("HiA2"); + + TestCase.assertFalse(Hibernate.isPropertyInitialized(doc, "text")); + + s = factory.openSession(); + s.setCacheMode( CacheMode.IGNORE ); + t = s.beginTransaction(); + s.saveOrUpdate(doc); + s.flush(); + TestCase.assertFalse(Hibernate.isPropertyInitialized(doc, "text")); + TestCase.assertEquals( doc.getText(), "blah blah" ); + TestCase.assertTrue(Hibernate.isPropertyInitialized(doc, "text")); + doc.updateText("blah blah blah blah"); + t.commit(); + s.close(); + + s = factory.openSession(); + s.setCacheMode( CacheMode.IGNORE ); + t = s.beginTransaction(); + doc = ( Document ) s.createQuery("from Document").uniqueResult(); + TestCase.assertEquals( doc.getName(), "HiA2" ); + TestCase.assertEquals( doc.getText(), "blah blah blah blah" ); + t.commit(); + s.close(); + + s = factory.openSession(); + s.setCacheMode( CacheMode.IGNORE ); + t = s.beginTransaction(); + doc = (Document) s.load( Document.class, doc.getId() ); + doc.getName(); + TestCase.assertFalse(Hibernate.isPropertyInitialized(doc, "text")); + TestCase.assertFalse(Hibernate.isPropertyInitialized(doc, "summary")); + t.commit(); + s.close(); + + s = factory.openSession(); + s.setCacheMode( CacheMode.IGNORE ); + t = s.beginTransaction(); + doc = (Document) s.createQuery("from Document").uniqueResult(); + //s.delete(doc); + s.delete( doc.getFolder() ); + s.delete( doc.getOwner() ); + s.flush(); + t.commit(); + s.close(); + } + +} Added: trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestLazyManyToOneExecutable.java =================================================================== --- trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestLazyManyToOneExecutable.java 2006-03-03 10:51:04 UTC (rev 9537) +++ trunk/Hibernate3/test/org/hibernate/test/instrument/runtime/TestLazyManyToOneExecutable.java 2006-03-04 00:17:57 UTC (rev 9538) @@ -0,0 +1,73 @@ +package org.hibernate.test.instrument.runtime; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.Hibernate; +import org.hibernate.test.instrument.domain.Owner; +import org.hibernate.test.instrument.domain.Document; +import org.hibernate.test.instrument.domain.Folder; +import junit.framework.Assert; + +/** + * @author Steve Ebersole + */ +public class TestLazyManyToOneExecutable extends AbstractExecutable { + public void execute() { + Session s = getFactory().openSession(); + Transaction t = s.beginTransaction(); + Owner gavin = new Owner(); + Document hia = new Document(); + Folder fol = new Folder(); + gavin.setName("gavin"); + hia.setName("Hibernate in Action"); + hia.setSummary("blah"); + hia.updateText("blah blah"); + fol.setName("books"); + hia.setOwner(gavin); + hia.setFolder(fol); + fol.getDocuments().add(hia); + s.persist(gavin); + s.persist(fol); + t.commit(); + s.close(); + + s = getFactory().openSession(); + t = s.beginTransaction(); +... [truncated message content] |