HHH-8505: Test suplemented with user type

This commit is contained in:
zuchos 2013-10-01 09:57:00 +02:00 committed by adamw
parent b11c14f00c
commit 180639a18b
5 changed files with 146 additions and 0 deletions

View File

@ -78,4 +78,6 @@ public class AdvancedEntity {
", dynamicConfiguration=" + dynamicConfiguration + ", dynamicConfiguration=" + dynamicConfiguration +
'}'; '}';
} }
} }

View File

@ -0,0 +1,51 @@
package org.hibernate.envers.test.integration.components.dynamic;
public class Age {
private int ageInYears;
public Age() {
}
public Age(int ageInYears) {
this.ageInYears = ageInYears;
}
public int getAgeInYears() {
return ageInYears;
}
public void setAgeInYears(int ageInYears) {
this.ageInYears = ageInYears;
}
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( !( o instanceof Age ) ) {
return false;
}
Age age = (Age) o;
if ( ageInYears != age.ageInYears ) {
return false;
}
return true;
}
@Override
public int hashCode() {
return ageInYears;
}
@Override
public String toString() {
return "Age{" +
"ageInYears=" + ageInYears +
'}';
}
}

View File

@ -0,0 +1,73 @@
package org.hibernate.envers.test.integration.components.dynamic;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.type.IntegerType;
import org.hibernate.usertype.UserType;
public class AgeType implements UserType {
@Override
public int[] sqlTypes() {
return new int[] {
IntegerType.INSTANCE.sqlType()
};
}
@Override
public Class<Age> returnedClass() {
return Age.class;
}
@Override
public boolean equals(Object x, Object y) throws HibernateException {
return x != null ? x.equals( y ) : y == null;
}
@Override
public int hashCode(Object x) throws HibernateException {
return x != null ? x.hashCode() : 1;
}
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
throws HibernateException, SQLException {
return new Age( rs.getInt( rs.findColumn( names[0] ) ) );
}
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
throws HibernateException, SQLException {
st.setInt( index, ( (Age) value ).getAgeInYears() );
}
@Override
public Object deepCopy(Object value) throws HibernateException {
return new Age( ( (Age) value ).getAgeInYears() );
}
@Override
public boolean isMutable() {
return false;
}
@Override
public Serializable disassemble(Object value) throws HibernateException {
return null;
}
@Override
public Object assemble(Serializable cached, Object owner) throws HibernateException {
return null;
}
@Override
public Object replace(Object original, Object target, Object owner) throws HibernateException {
return null;
}
}

View File

@ -36,6 +36,7 @@ public class AuditedDynamicComponentsAdvancedCasesTest extends BaseEnversFunctio
public static final String INTERNAL_MAP_WITH_MANY_TO_MANY = "internalMapWithEntities"; public static final String INTERNAL_MAP_WITH_MANY_TO_MANY = "internalMapWithEntities";
public static final String INTERNAL_SET = "internalSet"; public static final String INTERNAL_SET = "internalSet";
public static final String INTERNAL_SET_OF_COMPONENTS = "internalSetOfComponents"; public static final String INTERNAL_SET_OF_COMPONENTS = "internalSetOfComponents";
public static final String AGE_USER_TYPE = "ageUserType";
@Override @Override
protected String[] getMappings() { protected String[] getMappings() {
@ -85,6 +86,7 @@ public class AuditedDynamicComponentsAdvancedCasesTest extends BaseEnversFunctio
componentSet.add( new InternalComponent( "Ein" ) ); componentSet.add( new InternalComponent( "Ein" ) );
componentSet.add( new InternalComponent( "Zwei" ) ); componentSet.add( new InternalComponent( "Zwei" ) );
advancedEntity.getDynamicConfiguration().put( INTERNAL_SET_OF_COMPONENTS, componentSet ); advancedEntity.getDynamicConfiguration().put( INTERNAL_SET_OF_COMPONENTS, componentSet );
advancedEntity.getDynamicConfiguration().put( AGE_USER_TYPE, new Age(18) );
return advancedEntity; return advancedEntity;
} }
@ -154,6 +156,12 @@ public class AuditedDynamicComponentsAdvancedCasesTest extends BaseEnversFunctio
session.save( advancedEntity ); session.save( advancedEntity );
session.getTransaction().commit(); session.getTransaction().commit();
//rev 8
session.getTransaction().begin();
advancedEntity.getDynamicConfiguration().put( AGE_USER_TYPE , new Age(19));
session.save( advancedEntity );
session.getTransaction().commit();
AdvancedEntity advancedEntityActual = (AdvancedEntity) session.load( AdvancedEntity.class, 1L ); AdvancedEntity advancedEntityActual = (AdvancedEntity) session.load( AdvancedEntity.class, 1L );
Assert.assertEquals( advancedEntity, advancedEntityActual ); Assert.assertEquals( advancedEntity, advancedEntityActual );
@ -249,6 +257,17 @@ public class AuditedDynamicComponentsAdvancedCasesTest extends BaseEnversFunctio
); );
Assert.assertEquals( advancedEntity, ver7 ); Assert.assertEquals( advancedEntity, ver7 );
//then v8
advancedEntity.getDynamicConfiguration().put( AGE_USER_TYPE , new Age(19));
AdvancedEntity ver8 = getAuditReader().find(
AdvancedEntity.class,
advancedEntity.getId(),
8
);
Assert.assertEquals( advancedEntity, ver8 );
session.getTransaction().commit(); session.getTransaction().commit();
} }

View File

@ -45,6 +45,7 @@
<many-to-many class="org.hibernate.envers.test.integration.components.dynamic.ManyToManyEntity" <many-to-many class="org.hibernate.envers.test.integration.components.dynamic.ManyToManyEntity"
column="MAP_VAL_MANY_TO_MANY"/> column="MAP_VAL_MANY_TO_MANY"/>
</map> </map>
<property name="ageUserType" type="org.hibernate.envers.test.integration.components.dynamic.AgeType" column="CUSTOM_TYPE_AGE"/>
</dynamic-component> </dynamic-component>
<!--<one-to-one name="oneToOneEntity" class="org.hibernate.envers.test.integration.components.dynamic.OneToOneEntity"/>--> <!--<one-to-one name="oneToOneEntity" class="org.hibernate.envers.test.integration.components.dynamic.OneToOneEntity"/>-->
</class> </class>