Author: steve.ebersole(a)jboss.com
Date: 2006-11-02 14:18:52 -0500 (Thu, 02 Nov 2006)
New Revision: 10695
Added:
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/cases/TestLazyPropertyCustomTypeExecutable.java
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/domain/CustomBlobType.java
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/domain/Problematic.hbm.xml
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/domain/Problematic.java
Modified:
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/cases/AbstractExecutable.java
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/runtime/AbstractTransformingClassLoaderInstrumentTestCase.java
Log:
added tests specific for lazy properties using user-type mappings
Modified:
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/cases/AbstractExecutable.java
===================================================================
---
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/cases/AbstractExecutable.java 2006-11-02
19:17:39 UTC (rev 10694)
+++
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/cases/AbstractExecutable.java 2006-11-02
19:18:52 UTC (rev 10695)
@@ -1,28 +1,43 @@
-package org.hibernate.test.instrument.cases;
-
-import org.hibernate.SessionFactory;
-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;
- }
-}
+package org.hibernate.test.instrument.cases;
+
+import org.hibernate.SessionFactory;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
+
+/**
+ * @author Steve Ebersole
+ */
+public abstract class AbstractExecutable implements Executable {
+
+ private SessionFactory factory;
+
+ public final void prepare() {
+ Configuration cfg = new Configuration()
+ .setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
+ String[] resources = getResources();
+ for ( int i = 0; i < resources.length; i++ ) {
+ cfg.addResource( resources[i] );
+ }
+ factory = cfg.buildSessionFactory();
+ }
+
+ public final void complete() {
+ try {
+ cleanup();
+ }
+ finally {
+ factory.close();
+ }
+ }
+
+ protected SessionFactory getFactory() {
+ return factory;
+ }
+
+ protected void cleanup() {
+ }
+
+ protected String[] getResources() {
+ return new String[] {
"org/hibernate/test/instrument/domain/Documents.hbm.xml" };
+ }
+}
Added:
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/cases/TestLazyPropertyCustomTypeExecutable.java
===================================================================
---
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/cases/TestLazyPropertyCustomTypeExecutable.java 2006-11-02
19:17:39 UTC (rev 10694)
+++
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/cases/TestLazyPropertyCustomTypeExecutable.java 2006-11-02
19:18:52 UTC (rev 10695)
@@ -0,0 +1,71 @@
+package org.hibernate.test.instrument.cases;
+
+import java.util.Iterator;
+
+import junit.framework.Assert;
+
+import org.hibernate.Session;
+import org.hibernate.intercept.FieldInterceptionHelper;
+import org.hibernate.test.instrument.domain.Problematic;
+
+/**
+ * {@inheritDoc}
+ *
+ * @author Steve Ebersole
+ */
+public class TestLazyPropertyCustomTypeExecutable extends AbstractExecutable {
+
+ protected String[] getResources() {
+ return new String[] {
"org/hibernate/test/instrument/domain/Problematic.hbm.xml" };
+ }
+
+ public void execute() {
+ Session s = getFactory().openSession();
+ s.beginTransaction();
+ Problematic p = new Problematic();
+ p.setName( "whatever" );
+ p.setBytes( new byte[] { 1, 0, 1, 1, 0 } );
+ s.save( p );
+ s.getTransaction().commit();
+ s.close();
+
+ // this access should be ok because p1 is not a lazy proxy
+ s = getFactory().openSession();
+ s.beginTransaction();
+ Problematic p1 = ( Problematic ) s.get( Problematic.class, p.getId() );
+ Assert.assertTrue( FieldInterceptionHelper.isInstrumented( p1 ) );
+ p1.getRepresentation();
+ s.getTransaction().commit();
+ s.close();
+
+ // this should cause problems
+ s = getFactory().openSession();
+ s.beginTransaction();
+ p1 = ( Problematic ) s.load( Problematic.class, p.getId() );
+ Assert.assertFalse( FieldInterceptionHelper.isInstrumented( p1 ) );
+ p1.getRepresentation();
+ s.getTransaction().commit();
+ s.close();
+
+ // this should cause problems
+ s = getFactory().openSession();
+ s.beginTransaction();
+ p1 = ( Problematic ) s.load( Problematic.class, p.getId() );
+ Assert.assertFalse( FieldInterceptionHelper.isInstrumented( p1 ) );
+ p1.setRepresentation( p.getRepresentation() );
+ s.getTransaction().commit();
+ s.close();
+ }
+
+ protected void cleanup() {
+ Session s = getFactory().openSession();
+ s.beginTransaction();
+ Iterator itr = s.createQuery( "from Problematic" ).list().iterator();
+ while( itr.hasNext() ) {
+ Problematic p = ( Problematic ) itr.next();
+ s.delete( p );
+ }
+ s.getTransaction().commit();
+ s.close();
+ }
+}
Added:
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/domain/CustomBlobType.java
===================================================================
---
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/domain/CustomBlobType.java 2006-11-02
19:17:39 UTC (rev 10694)
+++
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/domain/CustomBlobType.java 2006-11-02
19:18:52 UTC (rev 10695)
@@ -0,0 +1,109 @@
+package org.hibernate.test.instrument.domain;
+
+import java.io.Serializable;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Types;
+import java.util.Arrays;
+
+import org.hibernate.HibernateException;
+import org.hibernate.Hibernate;
+import org.hibernate.usertype.UserType;
+
+/**
+ * A simple byte[]-based custom type.
+ */
+public class CustomBlobType implements UserType {
+ /**
+ * {@inheritDoc}
+ */
+ public Object nullSafeGet(ResultSet rs, String names[], Object owner) throws
SQLException {
+ // cast just to make sure...
+ return ( byte[] ) Hibernate.BINARY.nullSafeGet( rs, names );
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void nullSafeSet(PreparedStatement ps, Object value, int index) throws
SQLException, HibernateException {
+ // cast just to make sure...
+ Hibernate.BINARY.nullSafeSet( ps, ( byte[] ) value, index );
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object deepCopy(Object value) {
+ byte result[] = null;
+
+ if ( value != null ) {
+ byte bytes[] = ( byte[] ) value;
+
+ result = new byte[bytes.length];
+ System.arraycopy( bytes, 0, result, 0, bytes.length );
+ }
+
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isMutable() {
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int[] sqlTypes() {
+ return new int[] { Types.VARBINARY };
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Class returnedClass() {
+ return byte[].class;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean equals(Object x, Object y) {
+ return Arrays.equals( ( byte[] ) x, ( byte[] ) y );
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object assemble(Serializable arg0, Object arg1)
+ throws HibernateException {
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Serializable disassemble(Object arg0)
+ throws HibernateException {
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int hashCode(Object arg0)
+ throws HibernateException {
+ return 0;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Object replace(Object arg0, Object arg1, Object arg2)
+ throws HibernateException {
+ return null;
+ }
+}
\ No newline at end of file
Added:
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/domain/Problematic.hbm.xml
===================================================================
---
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/domain/Problematic.hbm.xml 2006-11-02
19:17:39 UTC (rev 10694)
+++
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/domain/Problematic.hbm.xml 2006-11-02
19:18:52 UTC (rev 10695)
@@ -0,0 +1,14 @@
+ <?xml version="1.0"?>
+ <!DOCTYPE hibernate-mapping PUBLIC
+ "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+
+ <hibernate-mapping
package="org.hibernate.test.instrument.domain">
+ <class name="Problematic">
+ <id name="id" type="long"
column="ID">
+ <generator class="increment" />
+ </id>
+ <property name="name" type="string"
column="NAME" />
+ <property name="bytes"
type="org.hibernate.test.instrument.domain.CustomBlobType"
column="DATA" lazy="true" />
+ </class>
+ </hibernate-mapping>
Added:
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/domain/Problematic.java
===================================================================
---
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/domain/Problematic.java 2006-11-02
19:17:39 UTC (rev 10694)
+++
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/domain/Problematic.java 2006-11-02
19:18:52 UTC (rev 10695)
@@ -0,0 +1,69 @@
+package org.hibernate.test.instrument.domain;
+
+/**
+ * {@inheritDoc}
+ *
+ * @author Steve Ebersole
+ */
+public class Problematic {
+ private Long id;
+ private String name;
+ private byte[] bytes;
+
+ private Representation representation;
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public byte[] getBytes() {
+ return bytes;
+ }
+
+ public void setBytes(byte[] bytes) {
+ this.bytes = bytes;
+ }
+
+ public Representation getRepresentation() {
+ if ( representation == null ) {
+ representation = ( ( bytes == null ) ? null : new Representation( bytes ) );
+ }
+ return representation;
+ }
+
+ public void setRepresentation(Representation rep) {
+ bytes = rep.getBytes();
+ }
+
+ public static class Representation {
+ private byte[] bytes;
+
+ public Representation(byte[] bytes) {
+ this.bytes = bytes;
+ }
+
+ public byte[] getBytes() {
+ return bytes;
+ }
+
+ public String toString() {
+ String result = "";
+ for ( int i = 0; i < bytes.length; i++ ) {
+ result += bytes[i];
+ }
+ return result;
+ }
+ }
+}
Modified:
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/runtime/AbstractTransformingClassLoaderInstrumentTestCase.java
===================================================================
---
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/runtime/AbstractTransformingClassLoaderInstrumentTestCase.java 2006-11-02
19:17:39 UTC (rev 10694)
+++
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/instrument/runtime/AbstractTransformingClassLoaderInstrumentTestCase.java 2006-11-02
19:18:52 UTC (rev 10695)
@@ -70,6 +70,9 @@
executeExecutable(
"org.hibernate.test.instrument.cases.TestManyToOneProxyExecutable" );
}
+ public void testLazyPropertyCustomType() {
+ executeExecutable(
"org.hibernate.test.instrument.cases.TestLazyPropertyCustomTypeExecutable" );
+ }
// reflection code to ensure isolation into the created classloader ~~~~~~~