HHH-7934 - Fix and test

This commit is contained in:
Lukasz Antoniak 2013-01-24 07:52:49 +01:00
parent 11e6fd5daf
commit 69230d1ebd
9 changed files with 162 additions and 7 deletions

View File

@ -39,6 +39,7 @@ import org.hibernate.envers.strategy.AuditStrategy;
import org.hibernate.envers.strategy.ValidityAuditStrategy;
import org.hibernate.envers.synchronization.AuditProcessManager;
import org.hibernate.envers.tools.reflection.ReflectionTools;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.property.Getter;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
@ -131,7 +132,7 @@ public class AuditConfiguration {
.loadClass( auditEntCfg.getAuditStrategyName() );
}
strategy = (AuditStrategy) auditStrategyClass.newInstance();
strategy = (AuditStrategy) ReflectHelper.getDefaultConstructor(auditStrategyClass).newInstance();
}
catch ( Exception e ) {
throw new MappingException(

View File

@ -27,6 +27,7 @@ import java.util.Map;
import org.hibernate.envers.entities.PropertyData;
import org.hibernate.envers.exception.AuditException;
import org.hibernate.envers.tools.Tools;
import org.hibernate.internal.util.ReflectHelper;
/**
* @author Adam Warski (adam at warski dot org)
@ -53,7 +54,8 @@ public abstract class AbstractCompositeIdMapper extends AbstractIdMapper impleme
Object ret;
try {
ret = Thread.currentThread().getContextClassLoader().loadClass(compositeIdClass).newInstance();
final Class clazz = Thread.currentThread().getContextClassLoader().loadClass(compositeIdClass);
ret = ReflectHelper.getDefaultConstructor(clazz).newInstance();
} catch (Exception e) {
throw new AuditException(e);
}

View File

@ -29,6 +29,7 @@ import java.util.Map;
import org.hibernate.envers.entities.PropertyData;
import org.hibernate.envers.exception.AuditException;
import org.hibernate.internal.util.ReflectHelper;
/**
* @author Adam Warski (adam at warski dot org)
@ -75,7 +76,8 @@ public class MultipleIdMapper extends AbstractCompositeIdMapper implements Simpl
Object ret;
try {
ret = Thread.currentThread().getContextClassLoader().loadClass(compositeIdClass).newInstance();
final Class clazz = Thread.currentThread().getContextClassLoader().loadClass(compositeIdClass);
ret = ReflectHelper.getDefaultConstructor(clazz).newInstance();
} catch (Exception e) {
throw new AuditException(e);
}

View File

@ -23,6 +23,7 @@
*/
package org.hibernate.envers.entities.mapper.relation.lazy.initializor;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@ -32,6 +33,7 @@ import org.hibernate.envers.entities.mapper.relation.MiddleComponentData;
import org.hibernate.envers.entities.mapper.relation.query.RelationQueryGenerator;
import org.hibernate.envers.exception.AuditException;
import org.hibernate.envers.reader.AuditReaderImplementor;
import org.hibernate.internal.util.ReflectHelper;
/**
* Initializes a non-indexed java collection (set or list, eventually sorted).
@ -55,11 +57,13 @@ public class BasicCollectionInitializor<T extends Collection> extends AbstractCo
protected T initializeCollection(int size) {
try {
return collectionClass.newInstance();
return (T) ReflectHelper.getDefaultConstructor(collectionClass).newInstance();
} catch (InstantiationException e) {
throw new AuditException(e);
} catch (IllegalAccessException e) {
throw new AuditException(e);
} catch (InvocationTargetException e) {
throw new AuditException(e);
}
}

View File

@ -23,6 +23,7 @@
*/
package org.hibernate.envers.entities.mapper.relation.lazy.initializor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
@ -31,6 +32,7 @@ import org.hibernate.envers.entities.mapper.relation.MiddleComponentData;
import org.hibernate.envers.entities.mapper.relation.query.RelationQueryGenerator;
import org.hibernate.envers.exception.AuditException;
import org.hibernate.envers.reader.AuditReaderImplementor;
import org.hibernate.internal.util.ReflectHelper;
/**
* Initializes a map.
@ -57,11 +59,13 @@ public class MapCollectionInitializor<T extends Map> extends AbstractCollectionI
protected T initializeCollection(int size) {
try {
return collectionClass.newInstance();
return (T) ReflectHelper.getDefaultConstructor(collectionClass).newInstance();
} catch (InstantiationException e) {
throw new AuditException(e);
} catch (IllegalAccessException e) {
throw new AuditException(e);
} catch (InvocationTargetException e) {
throw new AuditException(e);
}
}

View File

@ -24,6 +24,7 @@
package org.hibernate.envers.revisioninfo;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import org.hibernate.MappingException;
@ -34,6 +35,7 @@ import org.hibernate.envers.RevisionType;
import org.hibernate.envers.entities.PropertyData;
import org.hibernate.envers.synchronization.SessionCacheCleaner;
import org.hibernate.envers.tools.reflection.ReflectionTools;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.property.Setter;
/**
@ -61,11 +63,13 @@ public class DefaultRevisionInfoGenerator implements RevisionInfoGenerator {
if (!listenerClass.equals(RevisionListener.class)) {
// This is not the default value.
try {
listener = listenerClass.newInstance();
listener = (RevisionListener) ReflectHelper.getDefaultConstructor(listenerClass).newInstance();
} catch (InstantiationException e) {
throw new MappingException(e);
} catch (IllegalAccessException e) {
throw new MappingException(e);
} catch (InvocationTargetException e) {
throw new MappingException(e);
}
} else {
// Default listener - none
@ -83,7 +87,7 @@ public class DefaultRevisionInfoGenerator implements RevisionInfoGenerator {
public Object generate() {
Object revisionInfo;
try {
revisionInfo = revisionInfoClass.newInstance();
revisionInfo = ReflectHelper.getDefaultConstructor(revisionInfoClass).newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}

View File

@ -0,0 +1,65 @@
package org.hibernate.envers.test.integration.ids.protectedmodifier;
import java.io.Serializable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import org.hibernate.envers.Audited;
@Entity
@Audited
public class ProtectedConstructorEntity implements Serializable {
@EmbeddedId
private WrappedStringId wrappedStringId;
private String str1;
@SuppressWarnings("unused")
protected ProtectedConstructorEntity() {
// For JPA. Protected access modifier is essential in terms of unit test.
}
public ProtectedConstructorEntity(WrappedStringId wrappedStringId, String str1) {
this.wrappedStringId = wrappedStringId;
this.str1 = str1;
}
public boolean equals(Object o) {
if ( this == o ) return true;
if ( !( o instanceof ProtectedConstructorEntity ) ) return false;
ProtectedConstructorEntity that = (ProtectedConstructorEntity) o;
if ( wrappedStringId != null ? !wrappedStringId.equals( that.wrappedStringId ) : that.wrappedStringId != null ) return false;
if ( str1 != null ? !str1.equals( that.str1 ) : that.str1 != null ) return false;
return true;
}
public int hashCode() {
int result = ( wrappedStringId != null ? wrappedStringId.hashCode() : 0 );
result = 31 * result + ( str1 != null ? str1.hashCode() : 0 );
return result;
}
@Override
public String toString() {
return "ProtectedConstructorEntity(wrappedStringId = " + wrappedStringId + ", str1 = " + str1 + ")";
}
public WrappedStringId getWrappedStringId() {
return wrappedStringId;
}
public void setWrappedStringId(WrappedStringId wrappedStringId) {
this.wrappedStringId = wrappedStringId;
}
public String getStr1() {
return str1;
}
public void setStr1(String str1) {
this.str1 = str1;
}
}

View File

@ -0,0 +1,42 @@
package org.hibernate.envers.test.integration.ids.protectedmodifier;
import java.util.Arrays;
import java.util.List;
import javax.persistence.EntityManager;
import org.junit.Assert;
import org.junit.Test;
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
import org.hibernate.envers.test.Priority;
import org.hibernate.testing.TestForIssue;
/**
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@TestForIssue(jiraKey = "HHH-7934")
public class ProtectedConstructorTest extends BaseEnversJPAFunctionalTestCase {
private final ProtectedConstructorEntity testEntity = new ProtectedConstructorEntity( new WrappedStringId( "embeddedStringId" ), "string" );
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { WrappedStringId.class, ProtectedConstructorEntity.class };
}
@Test
@Priority(10)
public void initData() {
// Revision 1
EntityManager em = getEntityManager();
em.getTransaction().begin();
em.persist( testEntity );
em.getTransaction().commit();
em.close();
}
@Test
public void testAuditEntityInstantiation() {
List result = getAuditReader().createQuery().forEntitiesAtRevision( ProtectedConstructorEntity.class, 1 ).getResultList();
Assert.assertEquals( Arrays.asList( testEntity ), result );
}
}

View File

@ -0,0 +1,31 @@
package org.hibernate.envers.test.integration.ids.protectedmodifier;
import java.io.Serializable;
public class WrappedStringId implements Serializable {
String id;
@SuppressWarnings("unused")
protected WrappedStringId() {
// For JPA. Protected access modifier is essential in terms of unit test.
}
public WrappedStringId(String id) {
this.id = id;
}
public String toString() {
return id;
}
public boolean equals(Object o) {
if ( this == o ) return true;
if ( o == null || getClass() != o.getClass() ) return false;
WrappedStringId that = (WrappedStringId) o;
return !( id != null ? !id.equals( that.id ) : that.id != null );
}
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
}