Hibernate SVN: r20784 - jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2010-10-07 05:36:10 -0400 (Thu, 07 Oct 2010)
New Revision: 20784
Modified:
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/TypesMappedSuperclassTest.java
Log:
METAGEN-37 Fixed test name
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/TypesMappedSuperclassTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/TypesMappedSuperclassTest.java 2010-10-07 08:04:03 UTC (rev 20783)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/TypesMappedSuperclassTest.java 2010-10-07 09:36:10 UTC (rev 20784)
@@ -27,10 +27,11 @@
/**
* @author Hardy Ferentschik
+ * @see METAGEN-37
*/
public class TypesMappedSuperclassTest extends CompilationTest {
@Test
- public void testFoo() {
+ public void testExtractClosestRealType() {
assertMetamodelClassGeneratedFor( AttachmentGroup.class );
}
14 years, 1 month
Hibernate SVN: r20783 - in jpamodelgen/trunk/src: main/java/org/hibernate/jpamodelgen/annotation and 3 other directories.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2010-10-07 04:04:03 -0400 (Thu, 07 Oct 2010)
New Revision: 20783
Added:
jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroup.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroupInTopic.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroupPost.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroupPostInTopic.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/Post.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/TypesMappedSuperclassTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/UserRole.java
Modified:
jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/Version.java
jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java
jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java
Log:
METAGEN-37 fixed TypeUtils.extractClosestRealTypeAsString and added test
Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/Version.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/Version.java 2010-10-06 22:57:12 UTC (rev 20782)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/Version.java 2010-10-07 08:04:03 UTC (rev 20783)
@@ -24,7 +24,10 @@
*
* @author Hardy Ferentschik
*/
-public class Version {
+public final class Version {
+ private Version() {
+ }
+
public static String getVersionString() {
return "[WORKING]";
}
Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java 2010-10-06 22:57:12 UTC (rev 20782)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/AnnotationMetaEntity.java 2010-10-07 08:04:03 UTC (rev 20783)
@@ -24,33 +24,14 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
-import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.Name;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
-import javax.lang.model.type.ArrayType;
-import javax.lang.model.type.DeclaredType;
-import javax.lang.model.type.ExecutableType;
-import javax.lang.model.type.PrimitiveType;
-import javax.lang.model.type.TypeKind;
-import javax.lang.model.type.TypeMirror;
-import javax.lang.model.type.TypeVariable;
import javax.lang.model.util.ElementFilter;
-import javax.lang.model.util.SimpleTypeVisitor6;
import javax.persistence.AccessType;
-import javax.persistence.Basic;
-import javax.persistence.ElementCollection;
-import javax.persistence.Embeddable;
-import javax.persistence.ManyToMany;
-import javax.persistence.ManyToOne;
-import javax.persistence.MapKeyClass;
-import javax.persistence.OneToMany;
-import javax.persistence.OneToOne;
import javax.persistence.Transient;
-import javax.tools.Diagnostic;
import org.hibernate.jpamodelgen.AccessTypeInformation;
import org.hibernate.jpamodelgen.Context;
@@ -58,8 +39,6 @@
import org.hibernate.jpamodelgen.model.ImportContext;
import org.hibernate.jpamodelgen.model.MetaAttribute;
import org.hibernate.jpamodelgen.model.MetaEntity;
-import org.hibernate.jpamodelgen.util.Constants;
-import org.hibernate.jpamodelgen.util.StringUtil;
import org.hibernate.jpamodelgen.util.TypeUtils;
/**
@@ -92,6 +71,10 @@
}
}
+ public AccessTypeInformation getEntityAccessTypeInfo() {
+ return entityAccessTypeInfo;
+ }
+
public Context getContext() {
return context;
}
@@ -133,7 +116,7 @@
sb.append( '}' );
return sb.toString();
}
-
+
private void addPersistentMembers(List<? extends Element> membersOfClass, AccessType membersKind) {
for ( Element memberOfClass : membersOfClass ) {
AccessType forcedAccessType = TypeUtils.determineAnnotationSpecifiedAccessType( memberOfClass );
@@ -147,7 +130,7 @@
continue;
}
- TypeVisitor visitor = new TypeVisitor( this );
+ MetaAttributeGenerationVisitor visitor = new MetaAttributeGenerationVisitor( this, context );
AnnotationMetaAttribute result = memberOfClass.asType().accept( visitor, memberOfClass );
if ( result != null ) {
members.put( result.getPropertyName(), result );
@@ -155,7 +138,7 @@
}
}
- protected void init() {
+ protected final void init() {
TypeUtils.determineAccessTypeForHierarchy( element, context );
entityAccessTypeInfo = context.getAccessTypeInfo( getQualifiedName() );
@@ -166,285 +149,23 @@
addPersistentMembers( methodsOfClass, AccessType.PROPERTY );
}
- public String generateImports() {
+ public final String generateImports() {
return importContext.generateImports();
}
- public String importType(String fqcn) {
+ public final String importType(String fqcn) {
return importContext.importType( fqcn );
}
- public String staticImport(String fqcn, String member) {
+ public final String staticImport(String fqcn, String member) {
return importContext.staticImport( fqcn, member );
}
- public String importType(Name qualifiedName) {
+ public final String importType(Name qualifiedName) {
return importType( qualifiedName.toString() );
}
- public TypeElement getTypeElement() {
+ public final TypeElement getTypeElement() {
return element;
}
-
- class TypeVisitor extends SimpleTypeVisitor6<AnnotationMetaAttribute, Element> {
-
- /**
- * FQCN of the Hibernate specific @Target annotation. We do not use the class directly to avoid depending on Hibernate
- * Core.
- */
- private static final String ORG_HIBERNATE_ANNOTATIONS_TARGET = "org.hibernate.annotations.Target";
-
- AnnotationMetaEntity parent;
-
- TypeVisitor(AnnotationMetaEntity parent) {
- this.parent = parent;
- }
-
- @Override
- public AnnotationMetaAttribute visitPrimitive(PrimitiveType t, Element element) {
- return new AnnotationMetaSingleAttribute( parent, element, TypeUtils.toTypeString( t ) );
- }
-
- @Override
- public AnnotationMetaAttribute visitArray(ArrayType t, Element element) {
- // METAGEN-2 - For now we handle arrays as SingularAttribute
- // The code below is an attempt to be closer to the spec and only allow byte[], Byte[], char[] and Character[]
-// AnnotationMetaSingleAttribute attribute = null;
-// TypeMirror componentMirror = t.getComponentType();
-// if ( TypeKind.CHAR.equals( componentMirror.getKind() )
-// || TypeKind.BYTE.equals( componentMirror.getKind() ) ) {
-// attribute = new AnnotationMetaSingleAttribute( parent, element, TypeUtils.toTypeString( t ) );
-// }
-// else if ( TypeKind.DECLARED.equals( componentMirror.getKind() ) ) {
-// TypeElement componentElement = ( TypeElement ) context.getProcessingEnvironment()
-// .getTypeUtils()
-// .asElement( componentMirror );
-// if ( BASIC_ARRAY_TYPES.contains( componentElement.getQualifiedName().toString() ) ) {
-// attribute = new AnnotationMetaSingleAttribute( parent, element, TypeUtils.toTypeString( t ) );
-// }
-// }
-// return attribute;
- return new AnnotationMetaSingleAttribute( parent, element, TypeUtils.toTypeString( t ) );
- }
-
- public AnnotationMetaAttribute visitTypeVariable(TypeVariable t, Element element) {
- // METAGEN-29 - for a type variable we use the upper bound
- TypeMirror mirror = t.getUpperBound();
- TypeMirror erasedType = context.getTypeUtils().erasure( mirror );
- return new AnnotationMetaSingleAttribute(
- parent, element, erasedType.toString()
- );
- }
-
- @Override
- public AnnotationMetaAttribute visitDeclared(DeclaredType declaredType, Element element) {
- AnnotationMetaAttribute metaAttribute = null;
- TypeElement returnedElement = ( TypeElement ) context.getTypeUtils().asElement( declaredType );
- // WARNING: .toString() is necessary here since Name equals does not compare to String
- String fqNameOfReturnType = returnedElement.getQualifiedName().toString();
- String collection = Constants.COLLECTIONS.get( fqNameOfReturnType );
- String targetEntity = getTargetEntity( element.getAnnotationMirrors() );
- if ( collection != null ) {
- return createMetaCollectionAttribute(
- declaredType, element, fqNameOfReturnType, collection, targetEntity
- );
- }
- else if ( isBasicAttribute( element, returnedElement ) ) {
- String type = targetEntity != null ? targetEntity : returnedElement.getQualifiedName().toString();
- return new AnnotationMetaSingleAttribute( parent, element, type );
- }
- return metaAttribute;
- }
-
- private AnnotationMetaAttribute createMetaCollectionAttribute(DeclaredType declaredType, Element element, String fqNameOfReturnType, String collection, String targetEntity) {
- if ( TypeUtils.containsAnnotation( element, ElementCollection.class ) ) {
- String explicitTargetEntity = getTargetEntity( element.getAnnotationMirrors() );
- TypeMirror collectionElementType = TypeUtils.getCollectionElementType(
- declaredType, fqNameOfReturnType, explicitTargetEntity, context
- );
- final TypeElement collectionElement = ( TypeElement ) context.getTypeUtils()
- .asElement( collectionElementType );
- AccessTypeInformation accessTypeInfo = context.getAccessTypeInfo( collectionElement.getQualifiedName().toString() );
- if ( accessTypeInfo == null ) {
- AccessType explicitAccessType = TypeUtils.determineAnnotationSpecifiedAccessType(
- collectionElement
- );
- accessTypeInfo = new AccessTypeInformation(
- collectionElement.getQualifiedName().toString(),
- explicitAccessType,
- entityAccessTypeInfo.getAccessType()
- );
- context.addAccessTypeInformation(
- collectionElement.getQualifiedName().toString(), accessTypeInfo
- );
- }
- else {
- accessTypeInfo.setDefaultAccessType( entityAccessTypeInfo.getAccessType() );
- }
- }
- if ( collection.equals( "javax.persistence.metamodel.MapAttribute" ) ) {
- return createAnnotationMetaAttributeForMap( declaredType, element, collection, targetEntity );
- }
- else {
- return new AnnotationMetaCollection(
- parent, element, collection, getElementType( declaredType, targetEntity )
- );
- }
- }
-
- @Override
- public AnnotationMetaAttribute visitExecutable(ExecutableType t, Element p) {
- if ( !p.getKind().equals( ElementKind.METHOD ) ) {
- return null;
- }
-
- String string = p.getSimpleName().toString();
- if ( !StringUtil.isPropertyName( string ) ) {
- return null;
- }
-
- TypeMirror returnType = t.getReturnType();
- return returnType.accept( this, p );
- }
-
- private boolean isBasicAttribute(Element element, Element returnedElement) {
- if ( TypeUtils.containsAnnotation( element, Basic.class )
- || TypeUtils.containsAnnotation( element, OneToOne.class )
- || TypeUtils.containsAnnotation( element, ManyToOne.class ) ) {
- return true;
- }
-
- BasicAttributeVisitor basicVisitor = new BasicAttributeVisitor();
- return returnedElement.asType().accept( basicVisitor, returnedElement );
- }
-
- private AnnotationMetaAttribute createAnnotationMetaAttributeForMap(DeclaredType declaredType, Element element, String collection, String targetEntity) {
- String keyType;
- if ( TypeUtils.containsAnnotation( element, MapKeyClass.class ) ) {
- TypeMirror typeMirror = ( TypeMirror ) TypeUtils.getAnnotationValue(
- TypeUtils.getAnnotationMirror(
- element, MapKeyClass.class
- ), TypeUtils.DEFAULT_ANNOTATION_PARAMETER_NAME
- );
- keyType = typeMirror.toString();
- }
- else {
- keyType = TypeUtils.getKeyType( declaredType, context );
- }
- return new AnnotationMetaMap(
- parent,
- element,
- collection,
- keyType,
- getElementType( declaredType, targetEntity )
- );
- }
-
- private String getElementType(DeclaredType declaredType, String targetEntity) {
- if ( targetEntity != null ) {
- return targetEntity;
- }
- final List<? extends TypeMirror> mirrors = declaredType.getTypeArguments();
- if ( mirrors.size() == 1 ) {
- final TypeMirror type = mirrors.get( 0 );
- return TypeUtils.extractClosestRealTypeAsString( type, context );
- }
- else if ( mirrors.size() == 2 ) {
- return TypeUtils.extractClosestRealTypeAsString( mirrors.get( 1 ), context );
- }
- else {
- //for 0 or many
- //0 is expected, many is not
- if ( mirrors.size() > 2 ) {
- context.logMessage(
- Diagnostic.Kind.WARNING, "Unable to find the closest solid type" + declaredType
- );
- }
- return "?";
- }
- }
-
- /**
- * @param annotations list of annotation mirrors.
- *
- * @return target entity class name as string or {@code null} if no targetEntity is here or if equals to void
- */
- private String getTargetEntity(List<? extends AnnotationMirror> annotations) {
- String fullyQualifiedTargetEntityName = null;
- for ( AnnotationMirror mirror : annotations ) {
- if ( TypeUtils.isAnnotationMirrorOfType( mirror, ElementCollection.class ) ) {
- fullyQualifiedTargetEntityName = getFullyQualifiedClassNameOfTargetEntity( mirror, "targetClass" );
- }
- else if ( TypeUtils.isAnnotationMirrorOfType( mirror, OneToMany.class )
- || TypeUtils.isAnnotationMirrorOfType( mirror, ManyToMany.class )
- || TypeUtils.isAnnotationMirrorOfType( mirror, ManyToOne.class )
- || TypeUtils.isAnnotationMirrorOfType( mirror, OneToOne.class ) ) {
- fullyQualifiedTargetEntityName = getFullyQualifiedClassNameOfTargetEntity( mirror, "targetEntity" );
- }
- else if ( TypeUtils.isAnnotationMirrorOfType( mirror, ORG_HIBERNATE_ANNOTATIONS_TARGET ) ) {
- fullyQualifiedTargetEntityName = getFullyQualifiedClassNameOfTargetEntity( mirror, "value" );
- }
- }
- return fullyQualifiedTargetEntityName;
- }
-
- private String getFullyQualifiedClassNameOfTargetEntity(AnnotationMirror mirror, String parameterName) {
- assert mirror != null;
- assert parameterName != null;
-
- String targetEntityName = null;
- Object parameterValue = TypeUtils.getAnnotationValue( mirror, parameterName );
- if ( parameterValue != null ) {
- TypeMirror parameterType = ( TypeMirror ) parameterValue;
- if ( !parameterType.getKind().equals( TypeKind.VOID ) ) {
- targetEntityName = parameterType.toString();
- }
- }
- return targetEntityName;
- }
- }
-
- /**
- * Checks whether the visited type is a basic attribute according to the JPA 2 spec
- * ( section 2.8 Mapping Defaults for Non-Relationship Fields or Properties)
- */
- class BasicAttributeVisitor extends SimpleTypeVisitor6<Boolean, Element> {
- @Override
- public Boolean visitPrimitive(PrimitiveType t, Element element) {
- return Boolean.TRUE;
- }
-
- @Override
- public Boolean visitArray(ArrayType t, Element element) {
- TypeMirror componentMirror = t.getComponentType();
- TypeElement componentElement = ( TypeElement ) context.getTypeUtils().asElement( componentMirror );
-
- return Constants.BASIC_ARRAY_TYPES.contains( componentElement.getQualifiedName().toString() );
- }
-
- @Override
- public Boolean visitDeclared(DeclaredType declaredType, Element element) {
- if ( ElementKind.ENUM.equals( element.getKind() ) ) {
- return Boolean.TRUE;
- }
-
- if ( ElementKind.CLASS.equals( element.getKind() ) || ElementKind.INTERFACE.equals( element.getKind() ) ) {
- TypeElement typeElement = ( ( TypeElement ) element );
- String typeName = typeElement.getQualifiedName().toString();
- if ( Constants.BASIC_TYPES.contains( typeName ) ) {
- return Boolean.TRUE;
- }
- if ( TypeUtils.containsAnnotation( element, Embeddable.class ) ) {
- return Boolean.TRUE;
- }
- for ( TypeMirror mirror : typeElement.getInterfaces() ) {
- TypeElement interfaceElement = ( TypeElement ) context.getTypeUtils().asElement( mirror );
- if ( "java.io.Serializable".equals( interfaceElement.getQualifiedName().toString() ) ) {
- return Boolean.TRUE;
- }
- }
- }
- return Boolean.FALSE;
- }
- }
}
Added: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java (rev 0)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java 2010-10-07 08:04:03 UTC (rev 20783)
@@ -0,0 +1,325 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// $Id$
+package org.hibernate.jpamodelgen.annotation;
+
+import java.util.List;
+import javax.lang.model.element.AnnotationMirror;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.type.ArrayType;
+import javax.lang.model.type.DeclaredType;
+import javax.lang.model.type.ExecutableType;
+import javax.lang.model.type.PrimitiveType;
+import javax.lang.model.type.TypeKind;
+import javax.lang.model.type.TypeMirror;
+import javax.lang.model.type.TypeVariable;
+import javax.lang.model.util.SimpleTypeVisitor6;
+import javax.persistence.AccessType;
+import javax.persistence.Basic;
+import javax.persistence.ElementCollection;
+import javax.persistence.Embeddable;
+import javax.persistence.ManyToMany;
+import javax.persistence.ManyToOne;
+import javax.persistence.MapKeyClass;
+import javax.persistence.OneToMany;
+import javax.persistence.OneToOne;
+import javax.tools.Diagnostic;
+
+import org.hibernate.jpamodelgen.AccessTypeInformation;
+import org.hibernate.jpamodelgen.Context;
+import org.hibernate.jpamodelgen.util.Constants;
+import org.hibernate.jpamodelgen.util.StringUtil;
+import org.hibernate.jpamodelgen.util.TypeUtils;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class MetaAttributeGenerationVisitor extends SimpleTypeVisitor6<AnnotationMetaAttribute, Element> {
+
+ /**
+ * FQCN of the Hibernate specific @Target annotation. We do not use the class directly to avoid depending on Hibernate
+ * Core.
+ */
+ private static final String ORG_HIBERNATE_ANNOTATIONS_TARGET = "org.hibernate.annotations.Target";
+
+ private final AnnotationMetaEntity entity;
+ private final Context context;
+
+ MetaAttributeGenerationVisitor(AnnotationMetaEntity entity, Context context) {
+ this.entity = entity;
+ this.context = context;
+ }
+
+ @Override
+ public AnnotationMetaAttribute visitPrimitive(PrimitiveType t, Element element) {
+ return new AnnotationMetaSingleAttribute( entity, element, TypeUtils.toTypeString( t ) );
+ }
+
+ @Override
+ public AnnotationMetaAttribute visitArray(ArrayType t, Element element) {
+ // METAGEN-2 - For now we handle arrays as SingularAttribute
+ // The code below is an attempt to be closer to the spec and only allow byte[], Byte[], char[] and Character[]
+// AnnotationMetaSingleAttribute attribute = null;
+// TypeMirror componentMirror = t.getComponentType();
+// if ( TypeKind.CHAR.equals( componentMirror.getKind() )
+// || TypeKind.BYTE.equals( componentMirror.getKind() ) ) {
+// attribute = new AnnotationMetaSingleAttribute( entity, element, TypeUtils.toTypeString( t ) );
+// }
+// else if ( TypeKind.DECLARED.equals( componentMirror.getKind() ) ) {
+// TypeElement componentElement = ( TypeElement ) context.getProcessingEnvironment()
+// .getTypeUtils()
+// .asElement( componentMirror );
+// if ( BASIC_ARRAY_TYPES.contains( componentElement.getQualifiedName().toString() ) ) {
+// attribute = new AnnotationMetaSingleAttribute( entity, element, TypeUtils.toTypeString( t ) );
+// }
+// }
+// return attribute;
+ return new AnnotationMetaSingleAttribute( entity, element, TypeUtils.toTypeString( t ) );
+ }
+
+ public AnnotationMetaAttribute visitTypeVariable(TypeVariable t, Element element) {
+ // METAGEN-29 - for a type variable we use the upper bound
+ TypeMirror mirror = t.getUpperBound();
+ TypeMirror erasedType = context.getTypeUtils().erasure( mirror );
+ return new AnnotationMetaSingleAttribute(
+ entity, element, erasedType.toString()
+ );
+ }
+
+ @Override
+ public AnnotationMetaAttribute visitDeclared(DeclaredType declaredType, Element element) {
+ AnnotationMetaAttribute metaAttribute = null;
+ TypeElement returnedElement = (TypeElement) context.getTypeUtils().asElement( declaredType );
+ // WARNING: .toString() is necessary here since Name equals does not compare to String
+ String fqNameOfReturnType = returnedElement.getQualifiedName().toString();
+ String collection = Constants.COLLECTIONS.get( fqNameOfReturnType );
+ String targetEntity = getTargetEntity( element.getAnnotationMirrors() );
+ if ( collection != null ) {
+ return createMetaCollectionAttribute(
+ declaredType, element, fqNameOfReturnType, collection, targetEntity
+ );
+ }
+ else if ( isBasicAttribute( element, returnedElement ) ) {
+ String type = targetEntity != null ? targetEntity : returnedElement.getQualifiedName().toString();
+ return new AnnotationMetaSingleAttribute( entity, element, type );
+ }
+ return metaAttribute;
+ }
+
+ private AnnotationMetaAttribute createMetaCollectionAttribute(DeclaredType declaredType, Element element, String fqNameOfReturnType, String collection, String targetEntity) {
+ if ( TypeUtils.containsAnnotation( element, ElementCollection.class ) ) {
+ String explicitTargetEntity = getTargetEntity( element.getAnnotationMirrors() );
+ TypeMirror collectionElementType = TypeUtils.getCollectionElementType(
+ declaredType, fqNameOfReturnType, explicitTargetEntity, context
+ );
+ final TypeElement collectionElement = (TypeElement) context.getTypeUtils()
+ .asElement( collectionElementType );
+ AccessTypeInformation accessTypeInfo = context.getAccessTypeInfo( collectionElement.getQualifiedName().toString() );
+ if ( accessTypeInfo == null ) {
+ AccessType explicitAccessType = TypeUtils.determineAnnotationSpecifiedAccessType(
+ collectionElement
+ );
+ accessTypeInfo = new AccessTypeInformation(
+ collectionElement.getQualifiedName().toString(),
+ explicitAccessType,
+ entity.getEntityAccessTypeInfo().getAccessType()
+ );
+ context.addAccessTypeInformation(
+ collectionElement.getQualifiedName().toString(), accessTypeInfo
+ );
+ }
+ else {
+ accessTypeInfo.setDefaultAccessType( entity.getEntityAccessTypeInfo().getAccessType() );
+ }
+ }
+ if ( collection.equals( "javax.persistence.metamodel.MapAttribute" ) ) {
+ return createAnnotationMetaAttributeForMap( declaredType, element, collection, targetEntity );
+ }
+ else {
+ return new AnnotationMetaCollection(
+ entity, element, collection, getElementType( declaredType, targetEntity )
+ );
+ }
+ }
+
+ @Override
+ public AnnotationMetaAttribute visitExecutable(ExecutableType t, Element p) {
+ if ( !p.getKind().equals( ElementKind.METHOD ) ) {
+ return null;
+ }
+
+ String string = p.getSimpleName().toString();
+ if ( !StringUtil.isPropertyName( string ) ) {
+ return null;
+ }
+
+ TypeMirror returnType = t.getReturnType();
+ return returnType.accept( this, p );
+ }
+
+ private boolean isBasicAttribute(Element element, Element returnedElement) {
+ if ( TypeUtils.containsAnnotation( element, Basic.class )
+ || TypeUtils.containsAnnotation( element, OneToOne.class )
+ || TypeUtils.containsAnnotation( element, ManyToOne.class ) ) {
+ return true;
+ }
+
+ BasicAttributeVisitor basicVisitor = new BasicAttributeVisitor( context );
+ return returnedElement.asType().accept( basicVisitor, returnedElement );
+ }
+
+ private AnnotationMetaAttribute createAnnotationMetaAttributeForMap(DeclaredType declaredType, Element element, String collection, String targetEntity) {
+ String keyType;
+ if ( TypeUtils.containsAnnotation( element, MapKeyClass.class ) ) {
+ TypeMirror typeMirror = (TypeMirror) TypeUtils.getAnnotationValue(
+ TypeUtils.getAnnotationMirror(
+ element, MapKeyClass.class
+ ), TypeUtils.DEFAULT_ANNOTATION_PARAMETER_NAME
+ );
+ keyType = typeMirror.toString();
+ }
+ else {
+ keyType = TypeUtils.getKeyType( declaredType, context );
+ }
+ return new AnnotationMetaMap(
+ entity,
+ element,
+ collection,
+ keyType,
+ getElementType( declaredType, targetEntity )
+ );
+ }
+
+ private String getElementType(DeclaredType declaredType, String targetEntity) {
+ if ( targetEntity != null ) {
+ return targetEntity;
+ }
+ final List<? extends TypeMirror> mirrors = declaredType.getTypeArguments();
+ if ( mirrors.size() == 1 ) {
+ final TypeMirror type = mirrors.get( 0 );
+ return TypeUtils.extractClosestRealTypeAsString( type, context );
+ }
+ else if ( mirrors.size() == 2 ) {
+ return TypeUtils.extractClosestRealTypeAsString( mirrors.get( 1 ), context );
+ }
+ else {
+ //for 0 or many
+ //0 is expected, many is not
+ if ( mirrors.size() > 2 ) {
+ context.logMessage(
+ Diagnostic.Kind.WARNING, "Unable to find the closest solid type" + declaredType
+ );
+ }
+ return "?";
+ }
+ }
+
+ /**
+ * @param annotations list of annotation mirrors.
+ *
+ * @return target entity class name as string or {@code null} if no targetEntity is here or if equals to void
+ */
+ private String getTargetEntity(List<? extends AnnotationMirror> annotations) {
+ String fullyQualifiedTargetEntityName = null;
+ for ( AnnotationMirror mirror : annotations ) {
+ if ( TypeUtils.isAnnotationMirrorOfType( mirror, ElementCollection.class ) ) {
+ fullyQualifiedTargetEntityName = getFullyQualifiedClassNameOfTargetEntity( mirror, "targetClass" );
+ }
+ else if ( TypeUtils.isAnnotationMirrorOfType( mirror, OneToMany.class )
+ || TypeUtils.isAnnotationMirrorOfType( mirror, ManyToMany.class )
+ || TypeUtils.isAnnotationMirrorOfType( mirror, ManyToOne.class )
+ || TypeUtils.isAnnotationMirrorOfType( mirror, OneToOne.class ) ) {
+ fullyQualifiedTargetEntityName = getFullyQualifiedClassNameOfTargetEntity( mirror, "targetEntity" );
+ }
+ else if ( TypeUtils.isAnnotationMirrorOfType( mirror, ORG_HIBERNATE_ANNOTATIONS_TARGET ) ) {
+ fullyQualifiedTargetEntityName = getFullyQualifiedClassNameOfTargetEntity( mirror, "value" );
+ }
+ }
+ return fullyQualifiedTargetEntityName;
+ }
+
+ private String getFullyQualifiedClassNameOfTargetEntity(AnnotationMirror mirror, String parameterName) {
+ assert mirror != null;
+ assert parameterName != null;
+
+ String targetEntityName = null;
+ Object parameterValue = TypeUtils.getAnnotationValue( mirror, parameterName );
+ if ( parameterValue != null ) {
+ TypeMirror parameterType = (TypeMirror) parameterValue;
+ if ( !parameterType.getKind().equals( TypeKind.VOID ) ) {
+ targetEntityName = parameterType.toString();
+ }
+ }
+ return targetEntityName;
+ }
+}
+
+/**
+ * Checks whether the visited type is a basic attribute according to the JPA 2 spec
+ * ( section 2.8 Mapping Defaults for Non-Relationship Fields or Properties)
+ */
+class BasicAttributeVisitor extends SimpleTypeVisitor6<Boolean, Element> {
+
+ private final Context context;
+
+ public BasicAttributeVisitor(Context context) {
+ this.context = context;
+ }
+
+ @Override
+ public Boolean visitPrimitive(PrimitiveType t, Element element) {
+ return Boolean.TRUE;
+ }
+
+ @Override
+ public Boolean visitArray(ArrayType t, Element element) {
+ TypeMirror componentMirror = t.getComponentType();
+ TypeElement componentElement = (TypeElement) context.getTypeUtils().asElement( componentMirror );
+
+ return Constants.BASIC_ARRAY_TYPES.contains( componentElement.getQualifiedName().toString() );
+ }
+
+ @Override
+ public Boolean visitDeclared(DeclaredType declaredType, Element element) {
+ if ( ElementKind.ENUM.equals( element.getKind() ) ) {
+ return Boolean.TRUE;
+ }
+
+ if ( ElementKind.CLASS.equals( element.getKind() ) || ElementKind.INTERFACE.equals( element.getKind() ) ) {
+ TypeElement typeElement = ( (TypeElement) element );
+ String typeName = typeElement.getQualifiedName().toString();
+ if ( Constants.BASIC_TYPES.contains( typeName ) ) {
+ return Boolean.TRUE;
+ }
+ if ( TypeUtils.containsAnnotation( element, Embeddable.class ) ) {
+ return Boolean.TRUE;
+ }
+ for ( TypeMirror mirror : typeElement.getInterfaces() ) {
+ TypeElement interfaceElement = (TypeElement) context.getTypeUtils().asElement( mirror );
+ if ( "java.io.Serializable".equals( interfaceElement.getQualifiedName().toString() ) ) {
+ return Boolean.TRUE;
+ }
+ }
+ }
+ return Boolean.FALSE;
+ }
+}
+
+
Property changes on: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/annotation/MetaAttributeGenerationVisitor.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java 2010-10-06 22:57:12 UTC (rev 20782)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/util/TypeUtils.java 2010-10-07 08:04:03 UTC (rev 20783)
@@ -38,7 +38,6 @@
import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.Elements;
import javax.lang.model.util.SimpleTypeVisitor6;
-import javax.lang.model.util.Types;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Embeddable;
@@ -59,7 +58,7 @@
* @author Hardy Ferentschik
* @author Emmanuel Bernard
*/
-public class TypeUtils {
+public final class TypeUtils {
public static final String DEFAULT_ANNOTATION_PARAMETER_NAME = "value";
private static final Map<String, String> PRIMITIVES = new HashMap<String, String>();
@@ -94,8 +93,8 @@
//superclass of Object is of NoType which returns some other kind
if ( superClass.getKind() == TypeKind.DECLARED ) {
//F..king Ch...t Have those people used their horrible APIs even once?
- final Element superClassElement = ( ( DeclaredType ) superClass ).asElement();
- return ( TypeElement ) superClassElement;
+ final Element superClassElement = ( (DeclaredType) superClass ).asElement();
+ return (TypeElement) superClassElement;
}
else {
return null;
@@ -104,19 +103,11 @@
public static String extractClosestRealTypeAsString(TypeMirror type, Context context) {
if ( type instanceof TypeVariable ) {
- final TypeMirror compositeUpperBound = ( ( TypeVariable ) type ).getUpperBound();
- final Types types = context.getTypeUtils();
- final List<? extends TypeMirror> upperBounds = types.directSupertypes( compositeUpperBound );
- if ( upperBounds.size() == 0 ) {
- return compositeUpperBound.toString();
- }
- else {
- //take the first one
- return extractClosestRealTypeAsString( upperBounds.get( 0 ), context );
- }
+ final TypeMirror compositeUpperBound = ( (TypeVariable) type ).getUpperBound();
+ return extractClosestRealTypeAsString( compositeUpperBound, context );
}
else {
- return type.toString();
+ return context.getTypeUtils().erasure( type ).toString();
}
}
@@ -345,7 +336,7 @@
List<? extends AnnotationMirror> entityAnnotations =
context.getElementUtils().getAllAnnotationMirrors( subElement );
for ( Object entityAnnotation : entityAnnotations ) {
- AnnotationMirror annotationMirror = ( AnnotationMirror ) entityAnnotation;
+ AnnotationMirror annotationMirror = (AnnotationMirror) entityAnnotation;
if ( isIdAnnotation( annotationMirror ) ) {
defaultAccessType = getAccessTypeOfIdAnnotation( subElement );
break;
@@ -373,7 +364,7 @@
final AnnotationMirror accessAnnotationMirror = TypeUtils.getAnnotationMirror( element, Access.class );
AccessType forcedAccessType = null;
if ( accessAnnotationMirror != null ) {
- Element accessElement = ( Element ) TypeUtils.getAnnotationValue(
+ Element accessElement = (Element) TypeUtils.getAnnotationValue(
accessAnnotationMirror,
DEFAULT_ANNOTATION_PARAMETER_NAME
);
@@ -415,7 +406,7 @@
@Override
public String visitDeclared(DeclaredType declaredType, Element element) {
- TypeElement returnedElement = ( TypeElement ) context.getTypeUtils().asElement( declaredType );
+ TypeElement returnedElement = (TypeElement) context.getTypeUtils().asElement( declaredType );
String fqNameOfReturnType = null;
if ( containsAnnotation( returnedElement, Embeddable.class ) ) {
fqNameOfReturnType = returnedElement.getQualifiedName().toString();
Added: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroup.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroup.java (rev 0)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroup.java 2010-10-07 08:04:03 UTC (rev 20783)
@@ -0,0 +1,33 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hibernate.jpamodelgen.test.typedmappedsuperclass;
+
+import java.util.Set;
+import javax.persistence.Id;
+import javax.persistence.MappedSuperclass;
+import javax.persistence.OneToMany;
+
+@MappedSuperclass
+public abstract class AttachmentGroup<GroupType extends AttachmentGroup, PostType extends AttachmentGroupPost<UserRoleType, GroupType>, UserRoleType extends UserRole> {
+ @OneToMany(mappedBy = "parentGroup")
+ protected Set<PostType> posts;
+
+ @Id
+ long id;
+}
+
Property changes on: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroup.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroupInTopic.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroupInTopic.java (rev 0)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroupInTopic.java 2010-10-07 08:04:03 UTC (rev 20783)
@@ -0,0 +1,27 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hibernate.jpamodelgen.test.typedmappedsuperclass;
+
+import javax.persistence.Entity;
+
+@Entity
+public class AttachmentGroupInTopic
+ extends AttachmentGroup<AttachmentGroupInTopic, AttachmentGroupPostInTopic, UserRole> {
+ public AttachmentGroupInTopic() {
+ }
+}
Property changes on: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroupInTopic.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroupPost.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroupPost.java (rev 0)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroupPost.java 2010-10-07 08:04:03 UTC (rev 20783)
@@ -0,0 +1,27 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hibernate.jpamodelgen.test.typedmappedsuperclass;
+
+
+import javax.persistence.MappedSuperclass;
+
+@MappedSuperclass
+public abstract class AttachmentGroupPost<UserRoleType extends UserRole, GroupType extends AttachmentGroup>
+ extends Post<UserRoleType> {
+}
+
Property changes on: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroupPost.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroupPostInTopic.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroupPostInTopic.java (rev 0)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroupPostInTopic.java 2010-10-07 08:04:03 UTC (rev 20783)
@@ -0,0 +1,24 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hibernate.jpamodelgen.test.typedmappedsuperclass;
+
+import javax.persistence.Entity;
+
+@Entity
+public class AttachmentGroupPostInTopic extends AttachmentGroupPost<UserRole, AttachmentGroupInTopic> {
+}
Property changes on: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/AttachmentGroupPostInTopic.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/Post.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/Post.java (rev 0)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/Post.java 2010-10-07 08:04:03 UTC (rev 20783)
@@ -0,0 +1,25 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hibernate.jpamodelgen.test.typedmappedsuperclass;
+
+import javax.persistence.MappedSuperclass;
+
+@MappedSuperclass
+public abstract class Post<UserRoleType extends UserRole> {
+}
+
Property changes on: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/Post.java
___________________________________________________________________
Name: svn:keywords
+ Id
Copied: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/TypesMappedSuperclassTest.java (from rev 20778, jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/blob/BlobTest.java)
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/TypesMappedSuperclassTest.java (rev 0)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/TypesMappedSuperclassTest.java 2010-10-07 08:04:03 UTC (rev 20783)
@@ -0,0 +1,41 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// $Id$
+
+package org.hibernate.jpamodelgen.test.typedmappedsuperclass;
+
+import org.testng.annotations.Test;
+
+import org.hibernate.jpamodelgen.test.util.CompilationTest;
+
+import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class TypesMappedSuperclassTest extends CompilationTest {
+ @Test
+ public void testFoo() {
+ assertMetamodelClassGeneratedFor( AttachmentGroup.class );
+ }
+
+ @Override
+ protected String getPackageNameOfCurrentTest() {
+ return TypesMappedSuperclassTest.class.getPackage().getName();
+ }
+}
\ No newline at end of file
Added: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/UserRole.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/UserRole.java (rev 0)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/UserRole.java 2010-10-07 08:04:03 UTC (rev 20783)
@@ -0,0 +1,25 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hibernate.jpamodelgen.test.typedmappedsuperclass;
+
+import javax.persistence.Entity;
+
+@Entity
+public class UserRole {
+}
+
Property changes on: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/typedmappedsuperclass/UserRole.java
___________________________________________________________________
Name: svn:keywords
+ Id
14 years, 1 month
Hibernate SVN: r20781 - core/tags.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2010-10-06 18:51:58 -0400 (Wed, 06 Oct 2010)
New Revision: 20781
Added:
core/tags/git-marker/
Log:
Marking tag for move to Git
Copied: core/tags/git-marker (from rev 20780, core/trunk)
14 years, 1 month
Hibernate SVN: r20780 - in core/trunk: testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a and 1 other directory.
by hibernate-commits@lists.jboss.org
Author: steve.ebersole(a)jboss.com
Date: 2010-10-06 13:39:33 -0400 (Wed, 06 Oct 2010)
New Revision: 20780
Modified:
core/trunk/core/src/main/java/org/hibernate/event/def/DefaultLoadEventListener.java
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/DerivedIdentityIdClassParentSameIdTypeIdClassDepTest.java
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/MedicalHistory.java
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/Person.java
core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/PersonId.java
Log:
HHH-5629 - Inconsistent consistency checks for entities already found in session
Modified: core/trunk/core/src/main/java/org/hibernate/event/def/DefaultLoadEventListener.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/event/def/DefaultLoadEventListener.java 2010-10-06 11:19:32 UTC (rev 20779)
+++ core/trunk/core/src/main/java/org/hibernate/event/def/DefaultLoadEventListener.java 2010-10-06 17:39:33 UTC (rev 20780)
@@ -99,13 +99,13 @@
);
}
+ final Class idClass = persister.getIdentifierType().getReturnedClass();
if ( persister.getIdentifierType().isComponentType() && EntityMode.DOM4J == event.getSession().getEntityMode() ) {
// skip this check for composite-ids relating to dom4j entity-mode;
// alternatively, we could add a check to make sure the incoming id value is
// an instance of Element...
}
else {
- Class idClass = persister.getIdentifierType().getReturnedClass();
if ( idClass != null && ! idClass.isInstance( event.getEntityId() ) ) {
// we may have the kooky jpa requirement of allowing find-by-id where
// "id" is the "simple pk value" of a dependent objects parent. This
@@ -542,7 +542,8 @@
}
}
if ( options.isAllowNulls() ) {
- EntityPersister persister = event.getSession().getFactory().getEntityPersister( event.getEntityClassName() );
+// EntityPersister persister = event.getSession().getFactory().getEntityPersister( event.getEntityClassName() );
+ EntityPersister persister = event.getSession().getFactory().getEntityPersister( keyToLoad.getEntityName() );
if ( ! persister.isInstance( old, event.getSession().getEntityMode() ) ) {
return INCONSISTENT_RTN_CLASS_MARKER;
}
Modified: core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/DerivedIdentityIdClassParentSameIdTypeIdClassDepTest.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/DerivedIdentityIdClassParentSameIdTypeIdClassDepTest.java 2010-10-06 11:19:32 UTC (rev 20779)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/DerivedIdentityIdClassParentSameIdTypeIdClassDepTest.java 2010-10-06 17:39:33 UTC (rev 20780)
@@ -8,33 +8,67 @@
* @author Emmanuel Bernard
*/
public class DerivedIdentityIdClassParentSameIdTypeIdClassDepTest extends TestCase {
-
+ private static final String FIRST_NAME = "Emmanuel";
+ private static final String LAST_NAME = "Bernard";
+
public void testOneToOneExplicitJoinColumn() throws Exception {
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK1", getCfg() ) );
assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK2", getCfg() ) );
assertTrue( ! SchemaUtil.isColumnPresent( "MedicalHistory", "firstname", getCfg() ) );
- Person e = new Person();
- e.firstName = "Emmanuel";
- e.lastName = "Bernard";
- Session s = openSession( );
+
+ Session s = openSession();
s.getTransaction().begin();
+ Person e = new Person( FIRST_NAME, LAST_NAME );
s.persist( e );
- MedicalHistory d = new MedicalHistory();
- d.patient = e;
+ MedicalHistory d = new MedicalHistory( e );
s.persist( d );
s.flush();
- s.clear();
- PersonId pId = new PersonId();
- pId.firstName = e.firstName;
- pId.lastName = e.lastName;
- d = (MedicalHistory) s.get( MedicalHistory.class, pId);
- assertEquals( pId.firstName, d.patient.firstName );
- s.delete( d );
- s.delete( d.patient );
- s.getTransaction().rollback();
+ s.refresh( d );
+ s.getTransaction().commit();
s.close();
+
+ s = openSession();
+ s.getTransaction().begin();
+ PersonId pId = new PersonId( FIRST_NAME, LAST_NAME );
+ MedicalHistory d2 = (MedicalHistory) s.get( MedicalHistory.class, pId );
+ Person p2 = (Person) s.get( Person.class, pId );
+ assertEquals( pId.firstName, d2.patient.firstName );
+ assertEquals( pId.firstName, p2.firstName );
+ s.delete( d2 );
+ s.delete( p2 );
+ s.getTransaction().commit();
+ s.close();
}
+ public void testTckLikeBehavior() throws Exception {
+ assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK1", getCfg() ) );
+ assertTrue( SchemaUtil.isColumnPresent( "MedicalHistory", "FK2", getCfg() ) );
+ assertTrue( ! SchemaUtil.isColumnPresent( "MedicalHistory", "firstname", getCfg() ) );
+
+ Session s = openSession();
+ s.getTransaction().begin();
+ Person e = new Person( FIRST_NAME, LAST_NAME );
+ s.persist( e );
+ MedicalHistory d = new MedicalHistory( e );
+ s.persist( d );
+ s.flush();
+ s.refresh( d );
+ s.getTransaction().commit();
+
+ // NOTE THAT WE LEAVE THE SESSION OPEN!
+
+ s.getTransaction().begin();
+ PersonId pId = new PersonId( FIRST_NAME, LAST_NAME );
+ MedicalHistory d2 = (MedicalHistory) s.get( MedicalHistory.class, pId );
+ Person p2 = (Person) s.get( Person.class, pId );
+ assertEquals( pId.firstName, d2.patient.firstName );
+ assertEquals( pId.firstName, p2.firstName );
+ s.delete( d2 );
+ s.delete( p2 );
+ s.getTransaction().commit();
+ s.close();
+ }
+
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Modified: core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/MedicalHistory.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/MedicalHistory.java 2010-10-06 11:19:32 UTC (rev 20779)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/MedicalHistory.java 2010-10-06 17:39:33 UTC (rev 20780)
@@ -21,4 +21,11 @@
})
@OneToOne
Person patient;
+
+ public MedicalHistory() {
+ }
+
+ public MedicalHistory(Person patient) {
+ this.patient = patient;
+ }
}
Modified: core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/Person.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/Person.java 2010-10-06 11:19:32 UTC (rev 20779)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/Person.java 2010-10-06 17:39:33 UTC (rev 20780)
@@ -13,4 +13,12 @@
public class Person {
@Id String firstName;
@Id String lastName;
+
+ public Person() {
+ }
+
+ public Person(String firstName, String lastName) {
+ this.firstName = firstName;
+ this.lastName = lastName;
+ }
}
Modified: core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/PersonId.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/PersonId.java 2010-10-06 11:19:32 UTC (rev 20779)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/annotations/derivedidentities/e5/a/PersonId.java 2010-10-06 17:39:33 UTC (rev 20780)
@@ -8,4 +8,12 @@
public class PersonId implements Serializable {
String firstName;
String lastName;
+
+ public PersonId() {
+ }
+
+ public PersonId(String firstName, String lastName) {
+ this.firstName = firstName;
+ this.lastName = lastName;
+ }
}
14 years, 1 month
Hibernate SVN: r20779 - jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/util.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2010-10-06 07:19:32 -0400 (Wed, 06 Oct 2010)
New Revision: 20779
Modified:
jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/util/Constants.java
jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/util/StringUtil.java
Log:
METAGEN-35 Some coding style improvements
Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/util/Constants.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/util/Constants.java 2010-10-06 11:18:07 UTC (rev 20778)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/util/Constants.java 2010-10-06 11:19:32 UTC (rev 20779)
@@ -27,7 +27,7 @@
/**
* @author Hardy Ferentschik
*/
-public class Constants {
+public final class Constants {
public static Map<String, String> COLLECTIONS = new HashMap<String, String>();
static {
Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/util/StringUtil.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/util/StringUtil.java 2010-10-06 11:18:07 UTC (rev 20778)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/util/StringUtil.java 2010-10-06 11:19:32 UTC (rev 20779)
@@ -22,7 +22,7 @@
/**
* @author Hardy Ferentschik
*/
-public class StringUtil {
+public final class StringUtil {
private static final String NAME_SEPARATOR = ".";
private static final String PROPERTY_PREFIX_GET = "get";
private static final String PROPERTY_PREFIX_IS = "is";
@@ -63,16 +63,17 @@
return null;
}
+ String tmp = name;
if ( name.startsWith( PROPERTY_PREFIX_GET ) ) {
- name = name.replaceFirst( PROPERTY_PREFIX_GET, "" );
+ tmp = name.replaceFirst( PROPERTY_PREFIX_GET, "" );
}
else if ( name.startsWith( PROPERTY_PREFIX_IS ) ) {
- name = name.replaceFirst( PROPERTY_PREFIX_IS, "" );
+ tmp = name.replaceFirst( PROPERTY_PREFIX_IS, "" );
}
else if ( name.startsWith( PROPERTY_PREFIX_HAS ) ) {
- name = name.replaceFirst( PROPERTY_PREFIX_HAS, "" );
+ tmp = name.replaceFirst( PROPERTY_PREFIX_HAS, "" );
}
- return name.substring(0,1).toLowerCase() + name.substring(1);
+ return tmp.substring(0,1).toLowerCase() + tmp.substring(1);
}
}
14 years, 1 month
Hibernate SVN: r20778 - in jpamodelgen/trunk/src: test/java/org/hibernate/jpamodelgen/test/accesstype and 14 other directories.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2010-10-06 07:18:07 -0400 (Wed, 06 Oct 2010)
New Revision: 20778
Modified:
jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/xml/XmlMetaEntity.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/accesstype/AccessTypeTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/arraytype/ArrayTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/blob/BlobTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/ElementCollectionTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/EmbeddableMappedSuperClassTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest2.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generics/GenericsTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/inheritance/InheritanceTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/MixedConfigurationTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/XmlMetaCompleteTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/rawtypes/RawTypesTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/targetannotation/TargetAnnotationTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/util/CompilationTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/IgnoreInvalidXmlTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/XmlMappingTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/xmlmetacomplete/XmlMetaDataCompleteTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/xmlonly/XmlOnlyTest.java
Log:
METAGEN-35 Refactored the test base class
Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/xml/XmlMetaEntity.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/xml/XmlMetaEntity.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/xml/XmlMetaEntity.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -124,7 +124,7 @@
this.isMetaComplete = initIsMetaComplete( metaComplete );
}
- protected void init() {
+ protected final void init() {
this.accessTypeInfo = context.getAccessTypeInfo( getQualifiedName() );
if ( attributes != null ) {
parseAttributes( attributes );
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/accesstype/AccessTypeTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/accesstype/AccessTypeTest.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/accesstype/AccessTypeTest.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -99,7 +99,7 @@
}
@Override
- protected String getPackageNameOfTestSources() {
+ protected String getPackageNameOfCurrentTest() {
return AccessTypeTest.class.getPackage().getName();
}
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/arraytype/ArrayTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/arraytype/ArrayTest.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/arraytype/ArrayTest.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -49,7 +49,7 @@
}
@Override
- protected String getPackageNameOfTestSources() {
+ protected String getPackageNameOfCurrentTest() {
return Image.class.getPackage().getName();
}
}
\ No newline at end of file
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/blob/BlobTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/blob/BlobTest.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/blob/BlobTest.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -40,7 +40,7 @@
}
@Override
- protected String getPackageNameOfTestSources() {
+ protected String getPackageNameOfCurrentTest() {
return BlobTest.class.getPackage().getName();
}
}
\ No newline at end of file
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/ElementCollectionTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/ElementCollectionTest.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/elementcollection/ElementCollectionTest.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -78,7 +78,7 @@
}
@Override
- protected String getPackageNameOfTestSources() {
+ protected String getPackageNameOfCurrentTest() {
return ElementCollectionTest.class.getPackage().getName();
}
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/EmbeddableMappedSuperClassTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/EmbeddableMappedSuperClassTest.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/embeddablemappedsuperclass/EmbeddableMappedSuperClassTest.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -40,7 +40,7 @@
}
@Override
- protected String getPackageNameOfTestSources() {
+ protected String getPackageNameOfCurrentTest() {
return EmbeddableMappedSuperClassTest.class.getPackage().getName();
}
}
\ No newline at end of file
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -42,7 +42,7 @@
}
@Override
- protected String getPackageNameOfTestSources() {
+ protected String getPackageNameOfCurrentTest() {
return GeneratedAnnotationTest.class.getPackage().getName();
}
}
\ No newline at end of file
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest2.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest2.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generatedannotation/GeneratedAnnotationTest2.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -56,7 +56,7 @@
}
@Override
- protected String getPackageNameOfTestSources() {
+ protected String getPackageNameOfCurrentTest() {
return GeneratedAnnotationTest2.class.getPackage().getName();
}
}
\ No newline at end of file
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generics/GenericsTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generics/GenericsTest.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/generics/GenericsTest.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -37,7 +37,7 @@
}
@Override
- protected String getPackageNameOfTestSources() {
+ protected String getPackageNameOfCurrentTest() {
return GenericsTest.class.getPackage().getName();
}
}
\ No newline at end of file
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/inheritance/InheritanceTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/inheritance/InheritanceTest.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/inheritance/InheritanceTest.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -52,7 +52,7 @@
}
@Override
- protected String getPackageNameOfTestSources() {
+ protected String getPackageNameOfCurrentTest() {
return InheritanceTest.class.getPackage().getName();
}
}
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/MixedConfigurationTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/MixedConfigurationTest.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/MixedConfigurationTest.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -96,7 +96,7 @@
}
@Override
- protected String getPackageNameOfTestSources() {
+ protected String getPackageNameOfCurrentTest() {
return MixedConfigurationTest.class.getPackage().getName();
}
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/XmlMetaCompleteTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/XmlMetaCompleteTest.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/mixedmode/XmlMetaCompleteTest.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -42,7 +42,7 @@
}
@Override
- protected String getPackageNameOfTestSources() {
+ protected String getPackageNameOfCurrentTest() {
return XmlMetaCompleteTest.class.getPackage().getName();
}
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/rawtypes/RawTypesTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/rawtypes/RawTypesTest.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/rawtypes/RawTypesTest.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -37,7 +37,7 @@
}
@Override
- protected String getPackageNameOfTestSources() {
+ protected String getPackageNameOfCurrentTest() {
return DeskWithRawType.class.getPackage().getName();
}
}
\ No newline at end of file
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/targetannotation/TargetAnnotationTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/targetannotation/TargetAnnotationTest.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/targetannotation/TargetAnnotationTest.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -42,7 +42,7 @@
}
@Override
- protected String getPackageNameOfTestSources() {
+ protected String getPackageNameOfCurrentTest() {
return TargetAnnotationTest.class.getPackage().getName();
}
}
\ No newline at end of file
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/util/CompilationTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/util/CompilationTest.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/util/CompilationTest.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -49,7 +49,6 @@
*/
public abstract class CompilationTest {
private static final Logger log = LoggerFactory.getLogger( CompilationTest.class );
- private static final String PATH_SEPARATOR = System.getProperty( "file.separator" );
private static final String ANNOTATION_PROCESSOR_OPTION_PREFIX = "-A";
private static final String PROC_NONE = "-proc:none";
private static final String SOURCE_BASE_DIR_PROPERTY = "sourceBaseDir";
@@ -57,6 +56,8 @@
private static final String sourceBaseDir;
private static final String outBaseDir;
+ public static final String PATH_SEPARATOR = System.getProperty( "file.separator" );
+
private List<Diagnostic> compilationDiagnostics;
static {
@@ -81,16 +82,34 @@
return compilationDiagnostics;
}
+ public static String getSourceBaseDir() {
+ return sourceBaseDir;
+ }
+
@BeforeClass
- protected void compile() throws Exception {
+ protected void compileAllTestEntities() throws Exception {
+ List<File> sourceFiles = getCompilationUnits( sourceBaseDir, getPackageNameOfCurrentTest() );
+ // make sure there are no relics from previous runs
TestUtil.deleteGeneratedSourceFiles( new File( outBaseDir ) );
+ compile( sourceFiles, getPackageNameOfCurrentTest() );
+ }
+
+ /**
+ * Compiles the specified Java classes and generated the meta model java files which in turn get also compiled.
+ *
+ * @param sourceFiles the files containing the java source files to compile.
+ * @param packageName the package name of the source files
+ *
+ * @throws Exception in case the compilation fails
+ */
+ protected void compile(List<File> sourceFiles, String packageName) throws Exception {
List<String> options = createJavaOptions();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager( diagnostics, null, null );
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(
- getCompilationUnits( sourceBaseDir )
+ sourceFiles
);
// TODO - need to call the compiler twice. Once to compile the test classes and generate the java files
@@ -99,7 +118,7 @@
compileSources( options, compiler, diagnostics, fileManager, compilationUnits );
compilationUnits = fileManager.getJavaFileObjectsFromFiles(
- getCompilationUnits( outBaseDir )
+ getCompilationUnits( outBaseDir, packageName )
);
options.add( PROC_NONE ); // for the second compile skip the processor
compileSources( options, compiler, diagnostics, fileManager, compilationUnits );
@@ -107,6 +126,34 @@
fileManager.close();
}
+ protected List<File> getCompilationUnits(String baseDir, String packageName) {
+ List<File> javaFiles = new ArrayList<File>();
+ String packageDirName = baseDir + PATH_SEPARATOR + packageName.replace( ".", PATH_SEPARATOR );
+ File packageDir = new File( packageDirName );
+ FilenameFilter javaFileFilter = new FilenameFilter() {
+ @Override
+ public boolean accept(File dir, String name) {
+ return name.endsWith( ".java" ) && !name.endsWith( "Test.java" );
+ }
+ };
+ final File[] files = packageDir.listFiles( javaFileFilter );
+ if ( files == null ) {
+ throw new RuntimeException( "Cannot find package directory (is your base dir correct?): " + packageDirName );
+ }
+ javaFiles.addAll( Arrays.asList( files ) );
+ return javaFiles;
+ }
+
+ abstract protected String getPackageNameOfCurrentTest();
+
+ protected Map<String, String> getProcessorOptions() {
+ return Collections.emptyMap();
+ }
+
+ protected Collection<String> getOrmFiles() {
+ return Collections.emptyList();
+ }
+
private void compileSources(List<String> options, JavaCompiler compiler, DiagnosticCollector<JavaFileObject> diagnostics, StandardJavaFileManager fileManager, Iterable<? extends JavaFileObject> compilationUnits) {
JavaCompiler.CompilationTask task = compiler.getTask(
null, fileManager, diagnostics, options, null, compilationUnits
@@ -147,34 +194,6 @@
}
return options;
}
-
- private List<File> getCompilationUnits(String baseDir) {
- List<File> javaFiles = new ArrayList<File>();
- String packageDirName = baseDir + PATH_SEPARATOR + getPackageNameOfTestSources().replace( ".", PATH_SEPARATOR );
- File packageDir = new File( packageDirName );
- FilenameFilter javaFileFilter = new FilenameFilter() {
- @Override
- public boolean accept(File dir, String name) {
- return name.endsWith( ".java" ) && !name.endsWith( "Test.java" );
- }
- };
- final File[] files = packageDir.listFiles( javaFileFilter );
- if ( files == null ) {
- throw new RuntimeException( "Cannot find package directory (is your base dir correct?): " + packageDirName );
- }
- javaFiles.addAll( Arrays.asList( files ) );
- return javaFiles;
- }
-
- abstract protected String getPackageNameOfTestSources();
-
- protected Map<String, String> getProcessorOptions() {
- return Collections.emptyMap();
- }
-
- protected Collection<String> getOrmFiles() {
- return Collections.emptyList();
- }
}
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/IgnoreInvalidXmlTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/IgnoreInvalidXmlTest.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/IgnoreInvalidXmlTest.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -42,7 +42,7 @@
}
@Override
- protected String getPackageNameOfTestSources() {
+ protected String getPackageNameOfCurrentTest() {
return IgnoreInvalidXmlTest.class.getPackage().getName();
}
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/XmlMappingTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/XmlMappingTest.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/xmlmapped/XmlMappingTest.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -104,7 +104,7 @@
}
@Override
- protected String getPackageNameOfTestSources() {
+ protected String getPackageNameOfCurrentTest() {
return XmlMappingTest.class.getPackage().getName();
}
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/xmlmetacomplete/XmlMetaDataCompleteTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/xmlmetacomplete/XmlMetaDataCompleteTest.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/xmlmetacomplete/XmlMetaDataCompleteTest.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -42,7 +42,7 @@
}
@Override
- protected String getPackageNameOfTestSources() {
+ protected String getPackageNameOfCurrentTest() {
return XmlMetaDataCompleteTest.class.getPackage().getName();
}
Modified: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/xmlonly/XmlOnlyTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/xmlonly/XmlOnlyTest.java 2010-10-06 11:16:11 UTC (rev 20777)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/xmlonly/XmlOnlyTest.java 2010-10-06 11:18:07 UTC (rev 20778)
@@ -40,7 +40,7 @@
}
@Override
- protected String getPackageNameOfTestSources() {
+ protected String getPackageNameOfCurrentTest() {
return XmlOnlyTest.class.getPackage().getName();
}
14 years, 1 month
Hibernate SVN: r20777 - in jpamodelgen/trunk/src: test/java/org/hibernate/jpamodelgen/test and 2 other directories.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2010-10-06 07:16:11 -0400 (Wed, 06 Oct 2010)
New Revision: 20777
Added:
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/Entity.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/SeparateCompilationUnitsTest.java
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/superclass/
jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/superclass/MappedSuperclass.java
Modified:
jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java
Log:
METAGEN-35 Actual code changes and tests for the issue.
Modified: jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java
===================================================================
--- jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java 2010-10-05 14:08:57 UTC (rev 20776)
+++ jpamodelgen/trunk/src/main/java/org/hibernate/jpamodelgen/ClassWriter.java 2010-10-06 11:16:11 UTC (rev 20777)
@@ -31,6 +31,8 @@
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
+import javax.persistence.Entity;
+import javax.persistence.MappedSuperclass;
import javax.tools.Diagnostic;
import javax.tools.FileObject;
@@ -38,9 +40,13 @@
import org.hibernate.jpamodelgen.model.MetaEntity;
/**
+ * Helper class to write the actual meta model class using the {@link javax.annotation.processing.Filer} API.
+ *
* @author Emmanuel Bernard
+ * @author Hardy Ferentschik
*/
public final class ClassWriter {
+ private static final String META_MODEL_CLASS_NAME_SUFFIX = "_";
private ClassWriter() {
}
@@ -51,7 +57,7 @@
StringBuffer body = generateBody( entity, context );
FileObject fo = context.getProcessingEnvironment().getFiler().createSourceFile(
- metaModelPackage + "." + entity.getSimpleName() + "_"
+ getFullyQualifiedClassName( entity, metaModelPackage )
);
OutputStream os = fo.openOutputStream();
PrintWriter pw = new PrintWriter( os );
@@ -63,7 +69,6 @@
pw.flush();
pw.close();
-
}
catch ( FilerException filerEx ) {
context.logMessage(
@@ -87,15 +92,14 @@
* @return body content
*/
private static StringBuffer generateBody(MetaEntity entity, Context context) {
-
StringWriter sw = new StringWriter();
PrintWriter pw = null;
try {
pw = new PrintWriter( sw );
if ( context.isAddGeneratedAnnotation() ) {
- pw.println( "@" + entity.importType( Generated.class.getName() ) + "(\"JPA MetaModel for " + entity.getQualifiedName() + "\")" );
+ pw.println( writeGeneratedAnnotation( entity ) );
}
- pw.println( "@" + entity.importType( "javax.persistence.metamodel.StaticMetamodel" ) + "(" + entity.getSimpleName() + ".class)" );
+ pw.println( writeStaticMetaModelAnnotation( entity ) );
printClassDeclaration( entity, pw, context );
pw.println();
List<MetaAttribute> members = entity.getMembers();
@@ -114,19 +118,62 @@
}
private static void printClassDeclaration(MetaEntity entity, PrintWriter pw, Context context) {
- pw.print( "public abstract class " + entity.getSimpleName() + "_" );
+ pw.print( "public abstract class " + entity.getSimpleName() + META_MODEL_CLASS_NAME_SUFFIX );
final TypeMirror superClass = entity.getTypeElement().getSuperclass();
//superclass of Object is of NoType which returns some other kind
if ( superClass.getKind() == TypeKind.DECLARED ) {
//F..king Ch...t Have those people used their horrible APIs even once?
- final Element superClassElement = ( ( DeclaredType ) superClass ).asElement();
- String superClassName = ( ( TypeElement ) superClassElement ).getQualifiedName().toString();
- if ( context.containsMetaEntity( superClassName )
- || context.containsMetaEmbeddable( superClassName ) ) {
- pw.print( " extends " + superClassName + "_" );
+ final Element superClassElement = ( (DeclaredType) superClass ).asElement();
+ String superClassName = ( (TypeElement) superClassElement ).getQualifiedName().toString();
+ if ( extendsSuperMetaModel( superClassElement, entity.isMetaComplete(), context ) ) {
+ pw.print( " extends " + superClassName + META_MODEL_CLASS_NAME_SUFFIX );
}
}
pw.println( " {" );
}
+
+ /**
+ * Checks whether this metamodel class needs to extend another metamodel class.
+ * This methods checks whether the processor has generated a metamodel class for the super class, but it also
+ * allows for the possibility that the metamodel class was generated in a previous compilation (eg it could be
+ * part of a separate jar. See also METAGEN-35).
+ *
+ * @param superClassElement the super class element
+ * @param entityMetaComplete flag indicating if the entity for which the metamodel should be generarted is metamodel
+ * complete. If so we cannot use reflection to decide whether we have to add the extend clause
+ * @param context the execution context
+ *
+ * @return {@code true} in case there is super class meta model to extend from {@code false} otherwise.
+ */
+ private static boolean extendsSuperMetaModel(Element superClassElement, boolean entityMetaComplete, Context context) {
+ // if we processed the superclass in the same run we definitely need to extend
+ String superClassName = ( (TypeElement) superClassElement ).getQualifiedName().toString();
+ if ( context.containsMetaEntity( superClassName )
+ || context.containsMetaEmbeddable( superClassName ) ) {
+ return true;
+ }
+
+ // to allow for the case that the metamodel class for the super entity is for example contained in another
+ // jar file we use reflection. However, we need to consider the fact that there is xml configuration
+ // and annotations should be ignored
+ if ( !entityMetaComplete && ( superClassElement.getAnnotation( Entity.class ) != null
+ || superClassElement.getAnnotation( MappedSuperclass.class ) != null ) ) {
+ return true;
+ }
+
+ return false;
+ }
+
+ private static String getFullyQualifiedClassName(MetaEntity entity, String metaModelPackage) {
+ return metaModelPackage + "." + entity.getSimpleName() + META_MODEL_CLASS_NAME_SUFFIX;
+ }
+
+ private static String writeGeneratedAnnotation(MetaEntity entity) {
+ return "@" + entity.importType( Generated.class.getName() ) + "(\"JPA MetaModel for " + entity.getQualifiedName() + "\")";
+ }
+
+ private static String writeStaticMetaModelAnnotation(MetaEntity entity) {
+ return "@" + entity.importType( "javax.persistence.metamodel.StaticMetamodel" ) + "(" + entity.getSimpleName() + ".class)";
+ }
}
Added: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/Entity.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/Entity.java (rev 0)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/Entity.java 2010-10-06 11:16:11 UTC (rev 20777)
@@ -0,0 +1,31 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// $Id:$
+package org.hibernate.jpamodelgen.test.separatecompilationunits;
+
+import org.hibernate.jpamodelgen.test.separatecompilationunits.superclass.MappedSuperclass;
+
+/**
+ * @author Hardy Ferentschik
+ */
+(a)javax.persistence.Entity
+public class Entity extends MappedSuperclass {
+ private String name;
+}
+
+
Property changes on: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/Entity.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/SeparateCompilationUnitsTest.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/SeparateCompilationUnitsTest.java (rev 0)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/SeparateCompilationUnitsTest.java 2010-10-06 11:16:11 UTC (rev 20777)
@@ -0,0 +1,68 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// $Id$
+
+package org.hibernate.jpamodelgen.test.separatecompilationunits;
+
+import java.io.File;
+import java.util.List;
+
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import org.hibernate.jpamodelgen.test.util.CompilationTest;
+
+import static org.hibernate.jpamodelgen.test.util.TestUtil.getMetaModelSourceAsString;
+import static org.testng.Assert.assertTrue;
+
+/**
+ * @author Hardy Ferentschik
+ * @see METAGEN-35
+ */
+public class SeparateCompilationUnitsTest extends CompilationTest {
+ @Test
+ public void testInheritance() throws Exception {
+ // need to work with the source file. Entity_.class won't get generated, because the mapped superclass
+ // will not be on the classpath
+ String entityMetaModel = getMetaModelSourceAsString( Entity.class );
+ assertTrue(
+ entityMetaModel.contains(
+ "extends org.hibernate.jpamodelgen.test.separatecompilationunits.superclass.MappedSuperclass"
+ )
+ );
+ }
+
+ @Override
+ @BeforeClass
+ // override compileAllTestEntities to compile the mapped super class explicitly
+ protected void compileAllTestEntities() throws Exception {
+ String superClassPackageName = getPackageNameOfCurrentTest() + ".superclass";
+ List<File> sourceFiles = getCompilationUnits(
+ CompilationTest.getSourceBaseDir(), superClassPackageName
+ );
+ compile( sourceFiles, superClassPackageName );
+
+ sourceFiles = getCompilationUnits( getSourceBaseDir(), getPackageNameOfCurrentTest() );
+ compile( sourceFiles, getPackageNameOfCurrentTest() );
+ }
+
+ @Override
+ protected String getPackageNameOfCurrentTest() {
+ return SeparateCompilationUnitsTest.class.getPackage().getName();
+ }
+}
Property changes on: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/SeparateCompilationUnitsTest.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/superclass/MappedSuperclass.java
===================================================================
--- jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/superclass/MappedSuperclass.java (rev 0)
+++ jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/superclass/MappedSuperclass.java 2010-10-06 11:16:11 UTC (rev 20777)
@@ -0,0 +1,32 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// $Id:$
+package org.hibernate.jpamodelgen.test.separatecompilationunits.superclass;
+
+import javax.persistence.Id;
+
+/**
+ * @author Hardy Ferentschik
+ */
+(a)javax.persistence.MappedSuperclass
+public class MappedSuperclass {
+ @Id
+ private long id;
+}
+
+
Property changes on: jpamodelgen/trunk/src/test/java/org/hibernate/jpamodelgen/test/separatecompilationunits/superclass/MappedSuperclass.java
___________________________________________________________________
Name: svn:keywords
+ Id
14 years, 1 month
Hibernate SVN: r20776 - in validator/trunk/hibernate-validator/src: test/java/org/hibernate/validator/test/constraints/impl and 1 other directory.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2010-10-05 10:08:57 -0400 (Tue, 05 Oct 2010)
New Revision: 20776
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/ScriptAssertValidator.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/constraints/impl/ScriptAssertValidatorTest.java
Log:
HV-382 replaces String.isEmpty calls
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/ScriptAssertValidator.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/ScriptAssertValidator.java 2010-10-04 18:11:33 UTC (rev 20775)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validator/constraints/impl/ScriptAssertValidator.java 2010-10-05 14:08:57 UTC (rev 20776)
@@ -1,4 +1,3 @@
-// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
@@ -28,17 +27,14 @@
* Validator for the {@link ScriptAssert} constraint annotation.
*
* @author Gunnar Morling.
+ * @author Hardy Ferentschik
*/
public class ScriptAssertValidator implements ConstraintValidator<ScriptAssert, Object> {
-
private String script;
-
private String languageName;
-
private String alias;
public void initialize(ScriptAssert constraintAnnotation) {
-
validateParameters( constraintAnnotation );
this.script = constraintAnnotation.script();
@@ -47,7 +43,6 @@
}
public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) {
-
ScriptEvaluator scriptEvaluator = ScriptEvaluatorFactory.getInstance()
.getScriptEvaluatorByLanguageName( languageName );
@@ -55,14 +50,13 @@
}
private void validateParameters(ScriptAssert constraintAnnotation) {
-
- if ( constraintAnnotation.script().isEmpty() ) {
+ if ( constraintAnnotation.script().length() == 0 ) {
throw new IllegalArgumentException( "The parameter \"script\" must not be empty." );
}
- if ( constraintAnnotation.lang().isEmpty() ) {
+ if ( constraintAnnotation.lang().length() == 0 ) {
throw new IllegalArgumentException( "The parameter \"lang\" must not be empty." );
}
- if ( constraintAnnotation.alias().isEmpty() ) {
+ if ( constraintAnnotation.alias().length() == 0 ) {
throw new IllegalArgumentException( "The parameter \"alias\" must not be empty." );
}
}
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/constraints/impl/ScriptAssertValidatorTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/constraints/impl/ScriptAssertValidatorTest.java 2010-10-04 18:11:33 UTC (rev 20775)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validator/test/constraints/impl/ScriptAssertValidatorTest.java 2010-10-05 14:08:57 UTC (rev 20776)
@@ -1,4 +1,3 @@
-// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
@@ -36,12 +35,12 @@
* Unit test for {@link org.hibernate.validator.constraints.impl.ScriptAssertValidator}.
*
* @author Gunnar Morling
+ * @author Hardy Ferentschik
*/
public class ScriptAssertValidatorTest {
@Test
public void scriptEvaluatesToTrue() throws Exception {
-
ConstraintValidator<ScriptAssert, Object> validator = getInitializedValidator( "javascript", "true" );
assertTrue( validator.isValid( new Object(), null ) );
@@ -49,7 +48,6 @@
@Test
public void scriptEvaluatesToFalse() throws Exception {
-
ConstraintValidator<ScriptAssert, Object> validator = getInitializedValidator( "javascript", "false" );
assertFalse( validator.isValid( new Object(), null ) );
@@ -57,7 +55,6 @@
@Test
public void scriptExpressionReferencingAnnotatedObject() throws Exception {
-
ConstraintValidator<ScriptAssert, Object> validator = getInitializedValidator(
"javascript", "_this.startDate.before(_this.endDate)"
);
@@ -71,7 +68,6 @@
@Test
public void scriptExpressionUsingCustomizedAlias() throws Exception {
-
ConstraintValidator<ScriptAssert, Object> validator = getInitializedValidator(
"javascript", "_.startDate.before(_.endDate)", "_"
);
@@ -84,25 +80,21 @@
@Test(expectedExceptions = IllegalArgumentException.class)
public void emptyLanguageNameRaisesException() throws Exception {
-
getInitializedValidator( "", "script" );
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void emptyScriptRaisesException() throws Exception {
-
getInitializedValidator( "lang", "" );
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void emptyAliasRaisesException() throws Exception {
-
getInitializedValidator( "lang", "script", "" );
}
@Test(expectedExceptions = ConstraintDeclarationException.class)
public void unknownLanguageNameRaisesException() throws Exception {
-
ConstraintValidator<ScriptAssert, Object> validator = getInitializedValidator( "foo", "script" );
validator.isValid( new Object(), null );
@@ -110,7 +102,6 @@
@Test(expectedExceptions = ConstraintDeclarationException.class)
public void illegalScriptExpressionRaisesException() throws Exception {
-
ConstraintValidator<ScriptAssert, Object> validator = getInitializedValidator( "javascript", "foo" );
validator.isValid( new Object(), null );
@@ -118,7 +109,6 @@
@Test(expectedExceptions = ConstraintDeclarationException.class)
public void scriptExpressionReturningNullRaisesException() throws Exception {
-
ConstraintValidator<ScriptAssert, Object> validator = getInitializedValidator( "javascript", "null" );
validator.isValid( new Object(), null );
@@ -126,7 +116,6 @@
@Test(expectedExceptions = ConstraintDeclarationException.class)
public void scriptExpressionReturningNoBooleanRaisesException() throws Exception {
-
ConstraintValidator<ScriptAssert, Object> validator = getInitializedValidator(
"javascript", "new java.util.Date()"
);
@@ -135,18 +124,25 @@
}
/**
- * Returns a {@link org.hibernate.validator.constraints.impl.ScriptAssertValidator} initialized with a {@link ScriptAssert} with the given values.
+ * @param lang the script type
+ * @param script the actual script
+ * @param alias the alias name of the this object
+ *
+ * @return a {@link org.hibernate.validator.constraints.impl.ScriptAssertValidator} initialized with a {@link ScriptAssert} with the given values.
*/
- private ConstraintValidator<ScriptAssert, Object> getInitializedValidator(String lang, String script, String name) {
+ private ConstraintValidator<ScriptAssert, Object> getInitializedValidator(String lang, String script, String alias) {
ConstraintValidator<ScriptAssert, Object> validator = new ScriptAssertValidator();
- validator.initialize( getScriptAssert( lang, script, name ) );
+ validator.initialize( getScriptAssert( lang, script, alias ) );
return validator;
}
/**
- * Returns a {@link ScriptAssertValidator} initialized with a {@link ScriptAssert} with the given values.
+ * @param lang the script type
+ * @param script the actual script
+ *
+ * @return a {@link ScriptAssertValidator} initialized with a {@link ScriptAssert} with the given values.
*/
private ConstraintValidator<ScriptAssert, Object> getInitializedValidator(String lang, String script) {
@@ -157,16 +153,20 @@
}
/**
- * Returns a {@link ScriptAssert} initialized with the given values.
+ * @param lang the script type
+ * @param script the actual script
+ * @param alias the alias name of the this object
+ *
+ * @return a {@link ScriptAssert} initialized with the given values.
*/
- private ScriptAssert getScriptAssert(String lang, String script, String name) {
+ private ScriptAssert getScriptAssert(String lang, String script, String alias) {
AnnotationDescriptor<ScriptAssert> descriptor = AnnotationDescriptor.getInstance( ScriptAssert.class );
descriptor.setValue( "lang", lang );
descriptor.setValue( "script", script );
- if ( name != null ) {
- descriptor.setValue( "alias", name );
+ if ( alias != null ) {
+ descriptor.setValue( "alias", alias );
}
return AnnotationFactory.create( descriptor );
@@ -198,6 +198,5 @@
public Date getEndDate() {
return endDate;
}
-
}
}
14 years, 1 month
Hibernate SVN: r20775 - in core/trunk: envers/src/main/java/org/hibernate/envers/configuration and 5 other directories.
by hibernate-commits@lists.jboss.org
Author: fbascheper
Date: 2010-10-04 14:11:33 -0400 (Mon, 04 Oct 2010)
New Revision: 20775
Added:
core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/
core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/ValidityAuditStrategyRevEndTestCustomRevEnt.java
core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/ValidityAuditStrategyRevEndTsTest.java
Removed:
core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/ValidityAuditStrategyRevEndTestCustomRevEnt.java
core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/ValidityAuditStrategyRevEndTsTest.java
Modified:
core/trunk/documentation/envers/src/main/docbook/en-US/content/configuration.xml
core/trunk/envers/src/main/java/org/hibernate/envers/configuration/AuditConfiguration.java
core/trunk/envers/src/main/java/org/hibernate/envers/configuration/AuditEntitiesConfiguration.java
core/trunk/envers/src/main/java/org/hibernate/envers/configuration/RevisionInfoConfiguration.java
core/trunk/envers/src/main/java/org/hibernate/envers/configuration/metadata/AuditMetadataGenerator.java
core/trunk/envers/src/main/java/org/hibernate/envers/configuration/metadata/MetadataTools.java
core/trunk/envers/src/main/java/org/hibernate/envers/strategy/ValidityAuditStrategy.java
core/trunk/envers/src/test/resources/testng.xml
Log:
HHH-5371 Support revend timestamp to enable audit table partioning by time
Modified: core/trunk/documentation/envers/src/main/docbook/en-US/content/configuration.xml
===================================================================
--- core/trunk/documentation/envers/src/main/docbook/en-US/content/configuration.xml 2010-10-04 14:52:19 UTC (rev 20774)
+++ core/trunk/documentation/envers/src/main/docbook/en-US/content/configuration.xml 2010-10-04 18:11:33 UTC (rev 20775)
@@ -249,6 +249,33 @@
valid if the validity audit strategy is used.
</entry>
</row>
+ <row>
+ <entry>
+ <property>org.hibernate.envers.audit_strategy_validity_store_revend_timestamp</property>
+ </entry>
+ <entry>
+ false
+ </entry>
+ <entry>
+ Should the timestamp of the end revision be stored, until which the data was valid, in addition to the end revision itself.
+ This is useful to be able to purge old Audit records out of a relational database by using table partitioning.
+ Partitioning requires a column that exists within the table.
+ This property is only evaluated if the ValidityAuditStrategy is used.
+ </entry>
+ </row>
+ <row>
+ <entry>
+ <property>org.hibernate.envers.audit_strategy_validity_revend_timestamp_field_name</property>
+ </entry>
+ <entry>
+ REVEND_TSTMP
+ </entry>
+ <entry>
+ Column name of the timestamp of the end revision until which the data was valid.
+ Only used if the ValidityAuditStrategy is used, and
+ org.hibernate.envers.audit_strategy_validity_store_revend_timestamp evaluates to true
+ </entry>
+ </row>
</tbody>
</tgroup>
</table>
Modified: core/trunk/envers/src/main/java/org/hibernate/envers/configuration/AuditConfiguration.java
===================================================================
--- core/trunk/envers/src/main/java/org/hibernate/envers/configuration/AuditConfiguration.java 2010-10-04 14:52:19 UTC (rev 20774)
+++ core/trunk/envers/src/main/java/org/hibernate/envers/configuration/AuditConfiguration.java 2010-10-04 18:11:33 UTC (rev 20775)
@@ -28,16 +28,18 @@
import java.util.WeakHashMap;
import org.hibernate.MappingException;
+import org.hibernate.annotations.common.reflection.ReflectionManager;
+import org.hibernate.cfg.Configuration;
import org.hibernate.envers.entities.EntitiesConfigurations;
+import org.hibernate.envers.entities.PropertyData;
import org.hibernate.envers.revisioninfo.RevisionInfoNumberReader;
import org.hibernate.envers.revisioninfo.RevisionInfoQueryCreator;
+import org.hibernate.envers.strategy.AuditStrategy;
+import org.hibernate.envers.strategy.ValidityAuditStrategy;
import org.hibernate.envers.synchronization.AuditProcessManager;
-import org.hibernate.envers.strategy.AuditStrategy;
+import org.hibernate.envers.tools.reflection.ReflectionTools;
+import org.hibernate.property.Getter;
-import org.hibernate.cfg.Configuration;
-import org.hibernate.cfg.AnnotationConfiguration;
-import org.hibernate.annotations.common.reflection.ReflectionManager;
-
/**
* @author Adam Warski (adam at warski dot org)
* @author Stephanie Pau at Markit Group Plc
@@ -79,7 +81,6 @@
return auditStrategy;
}
- @SuppressWarnings({ "unchecked" })
public AuditConfiguration(Configuration cfg) {
Properties properties = cfg.getProperties();
@@ -89,20 +90,33 @@
auditEntCfg = new AuditEntitiesConfiguration(properties, revInfoCfgResult.getRevisionInfoEntityName());
globalCfg = new GlobalConfiguration(properties);
auditProcessManager = new AuditProcessManager(revInfoCfgResult.getRevisionInfoGenerator());
-
- try {
- Class auditStrategyClass = Thread.currentThread().getContextClassLoader().loadClass(auditEntCfg.getAuditStrategyName());
- auditStrategy = (AuditStrategy) auditStrategyClass.newInstance();
- } catch (Exception e) {
- throw new MappingException(String.format("Unable to create AuditStrategy[%s] instance." , auditEntCfg.getAuditStrategyName()));
- }
-
revisionInfoQueryCreator = revInfoCfgResult.getRevisionInfoQueryCreator();
revisionInfoNumberReader = revInfoCfgResult.getRevisionInfoNumberReader();
+ auditStrategy = initializeAuditStrategy(revInfoCfgResult.getRevisionInfoClass(),
+ revInfoCfgResult.getRevisionInfoTimestampData());
entCfg = new EntitiesConfigurator().configure(cfg, reflectionManager, globalCfg, auditEntCfg, auditStrategy,
revInfoCfgResult.getRevisionInfoXmlMapping(), revInfoCfgResult.getRevisionInfoRelationMapping());
}
+ private AuditStrategy initializeAuditStrategy(Class<?> revisionInfoClass, PropertyData revisionInfoTimestampData) {
+ AuditStrategy strategy;
+
+ try {
+ Class<?> auditStrategyClass = Thread.currentThread().getContextClassLoader().loadClass(auditEntCfg.getAuditStrategyName());
+ strategy = (AuditStrategy) auditStrategyClass.newInstance();
+ } catch (Exception e) {
+ throw new MappingException(String.format("Unable to create AuditStrategy[%s] instance." , auditEntCfg.getAuditStrategyName()));
+ }
+
+ if (strategy instanceof ValidityAuditStrategy) {
+ // further initialization required
+ Getter revisionTimestampGetter = ReflectionTools.getGetter(revisionInfoClass, revisionInfoTimestampData);
+ ((ValidityAuditStrategy) strategy).setRevisionTimestampGetter(revisionTimestampGetter);
+ }
+
+ return strategy;
+ }
+
//
private static Map<Configuration, AuditConfiguration> cfgs
Modified: core/trunk/envers/src/main/java/org/hibernate/envers/configuration/AuditEntitiesConfiguration.java
===================================================================
--- core/trunk/envers/src/main/java/org/hibernate/envers/configuration/AuditEntitiesConfiguration.java 2010-10-04 14:52:19 UTC (rev 20774)
+++ core/trunk/envers/src/main/java/org/hibernate/envers/configuration/AuditEntitiesConfiguration.java 2010-10-04 18:11:33 UTC (rev 20775)
@@ -25,17 +25,11 @@
import static org.hibernate.envers.tools.Tools.getProperty;
-import java.text.DateFormat;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
-import org.hibernate.MappingException;
import org.hibernate.envers.strategy.DefaultAuditStrategy;
-import org.hibernate.envers.strategy.ValidityAuditStrategy;
/**
* Configuration of versions entities - names of fields, entities and tables created to store versioning information.
@@ -61,6 +55,9 @@
private final Map<String, String> customAuditTablesNames;
private final String revisionEndFieldName;
+
+ private final boolean revisionEndTimestampEnabled;
+ private final String revisionEndTimestampFieldName;
public AuditEntitiesConfiguration(Properties properties, String revisionInfoEntityName) {
this.revisionInfoEntityName = revisionInfoEntityName;
@@ -97,6 +94,21 @@
"org.hibernate.envers.audit_strategy_valid_time_end_name",
"REVEND");
+ String revisionEndTimestampEnabledStr = getProperty(properties,
+ "org.hibernate.envers.audit_strategy_validity_store_revend_timestamp",
+ "org.hibernate.envers.audit_strategy_validity_store_revend_timestamp",
+ "false");
+ revisionEndTimestampEnabled = Boolean.parseBoolean(revisionEndTimestampEnabledStr);
+
+ if (revisionEndTimestampEnabled) {
+ revisionEndTimestampFieldName = getProperty(properties,
+ "org.hibernate.envers.audit_strategy_validity_revend_timestamp_field_name",
+ "org.hibernate.envers.audit_strategy_validity_revend_timestamp_field_name",
+ "REVEND_TSTMP");
+ } else {
+ revisionEndTimestampFieldName = null;
+ }
+
customAuditTablesNames = new HashMap<String, String>();
revisionNumberPath = originalIdPropName + "." + revisionFieldName + ".id";
@@ -111,6 +123,14 @@
return revisionFieldName;
}
+ public boolean isRevisionEndTimestampEnabled() {
+ return revisionEndTimestampEnabled;
+ }
+
+ public String getRevisionEndTimestampFieldName() {
+ return revisionEndTimestampFieldName;
+ }
+
public String getRevisionNumberPath() {
return revisionNumberPath;
}
Modified: core/trunk/envers/src/main/java/org/hibernate/envers/configuration/RevisionInfoConfiguration.java
===================================================================
--- core/trunk/envers/src/main/java/org/hibernate/envers/configuration/RevisionInfoConfiguration.java 2010-10-04 14:52:19 UTC (rev 20774)
+++ core/trunk/envers/src/main/java/org/hibernate/envers/configuration/RevisionInfoConfiguration.java 2010-10-04 18:11:33 UTC (rev 20775)
@@ -29,7 +29,6 @@
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
-import org.hibernate.envers.Audited;
import org.hibernate.envers.*;
import org.hibernate.envers.entities.PropertyData;
import org.hibernate.envers.configuration.metadata.MetadataTools;
@@ -176,7 +175,6 @@
"property");
}
- @SuppressWarnings({"unchecked"})
public RevisionInfoConfigurationResult configure(Configuration cfg, ReflectionManager reflectionManager) {
Iterator<PersistentClass> classes = (Iterator<PersistentClass>) cfg.getClassMappings();
boolean revisionEntityFound = false;
@@ -245,7 +243,8 @@
new RevisionInfoQueryCreator(revisionInfoEntityName, revisionInfoIdData.getName(),
revisionInfoTimestampData.getName(), isTimestampAsDate()),
generateRevisionInfoRelationMapping(),
- new RevisionInfoNumberReader(revisionInfoClass, revisionInfoIdData), revisionInfoEntityName);
+ new RevisionInfoNumberReader(revisionInfoClass, revisionInfoIdData), revisionInfoEntityName,
+ revisionInfoClass, revisionInfoTimestampData);
}
private boolean isTimestampAsDate() {
@@ -261,17 +260,22 @@
private final Element revisionInfoRelationMapping;
private final RevisionInfoNumberReader revisionInfoNumberReader;
private final String revisionInfoEntityName;
+ private final Class<?> revisionInfoClass;
+ private final PropertyData revisionInfoTimestampData;
RevisionInfoConfigurationResult(RevisionInfoGenerator revisionInfoGenerator,
Document revisionInfoXmlMapping, RevisionInfoQueryCreator revisionInfoQueryCreator,
Element revisionInfoRelationMapping,
- RevisionInfoNumberReader revisionInfoNumberReader, String revisionInfoEntityName) {
+ RevisionInfoNumberReader revisionInfoNumberReader, String revisionInfoEntityName, Class<?> revisionInfoClass,
+ PropertyData revisionInfoTimestampData) {
this.revisionInfoGenerator = revisionInfoGenerator;
this.revisionInfoXmlMapping = revisionInfoXmlMapping;
this.revisionInfoQueryCreator = revisionInfoQueryCreator;
this.revisionInfoRelationMapping = revisionInfoRelationMapping;
this.revisionInfoNumberReader = revisionInfoNumberReader;
this.revisionInfoEntityName = revisionInfoEntityName;
+ this.revisionInfoClass = revisionInfoClass;
+ this.revisionInfoTimestampData = revisionInfoTimestampData;
}
public RevisionInfoGenerator getRevisionInfoGenerator() {
@@ -297,4 +301,13 @@
public String getRevisionInfoEntityName() {
return revisionInfoEntityName;
}
+
+ public Class<?> getRevisionInfoClass() {
+ return revisionInfoClass;
+ }
+
+ public PropertyData getRevisionInfoTimestampData() {
+ return revisionInfoTimestampData;
+ }
+
}
\ No newline at end of file
Modified: core/trunk/envers/src/main/java/org/hibernate/envers/configuration/metadata/AuditMetadataGenerator.java
===================================================================
--- core/trunk/envers/src/main/java/org/hibernate/envers/configuration/metadata/AuditMetadataGenerator.java 2010-10-04 14:52:19 UTC (rev 20774)
+++ core/trunk/envers/src/main/java/org/hibernate/envers/configuration/metadata/AuditMetadataGenerator.java 2010-10-04 18:11:33 UTC (rev 20775)
@@ -55,6 +55,7 @@
import org.hibernate.type.ComponentType;
import org.hibernate.type.ManyToOneType;
import org.hibernate.type.OneToOneType;
+import org.hibernate.type.TimestampType;
import org.hibernate.type.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -64,7 +65,7 @@
* @author Sebastian Komander
* @author Tomasz Bech
* @author Stephanie Pau at Markit Group Plc
- * @author Hern�n Chanfreau
+ * @author Hernán Chanfreau
*/
public final class AuditMetadataGenerator {
private static final Logger log = LoggerFactory.getLogger(AuditMetadataGenerator.class);
@@ -144,7 +145,7 @@
addEndRevision(any_mapping);
}
- private void addEndRevision(Element any_mapping) {
+ private void addEndRevision(Element any_mapping ) {
// Add the end-revision field, if the appropriate strategy is used.
if (auditStrategy instanceof ValidityAuditStrategy) {
Element end_rev_mapping = (Element) revisionInfoRelationMapping.clone();
@@ -153,10 +154,16 @@
MetadataTools.addOrModifyColumn(end_rev_mapping, verEntCfg.getRevisionEndFieldName());
any_mapping.add(end_rev_mapping);
+
+ if (verEntCfg.isRevisionEndTimestampEnabled()) {
+ // add a column for the timestamp of the end revision
+ String revisionInfoTimestampSqlType = TimestampType.INSTANCE.getName();
+ Element timestampProperty = MetadataTools.addProperty(any_mapping, verEntCfg.getRevisionEndTimestampFieldName(), revisionInfoTimestampSqlType, true, true, false);
+ MetadataTools.addColumn(timestampProperty, verEntCfg.getRevisionEndTimestampFieldName(), 0, 0, 0, null);
+ }
}
}
- @SuppressWarnings({"unchecked"})
void addValue(Element parent, Value value, CompositeMapperBuilder currentMapper, String entityName,
EntityXmlMappingData xmlMappingData, PropertyAuditingData propertyAuditingData,
boolean insertable, boolean firstPass) {
@@ -204,7 +211,6 @@
}
}
- @SuppressWarnings({"unchecked"})
private void addProperties(Element parent, Iterator<Property> properties, CompositeMapperBuilder currentMapper,
ClassAuditingData auditingData, String entityName, EntityXmlMappingData xmlMappingData,
boolean firstPass) {
Modified: core/trunk/envers/src/main/java/org/hibernate/envers/configuration/metadata/MetadataTools.java
===================================================================
--- core/trunk/envers/src/main/java/org/hibernate/envers/configuration/metadata/MetadataTools.java 2010-10-04 14:52:19 UTC (rev 20774)
+++ core/trunk/envers/src/main/java/org/hibernate/envers/configuration/metadata/MetadataTools.java 2010-10-04 18:11:33 UTC (rev 20775)
@@ -49,7 +49,7 @@
return id_mapping;
}
- public static Element addProperty(Element parent, String name, String type, boolean insertable, boolean key) {
+ public static Element addProperty(Element parent, String name, String type, boolean insertable, boolean updateable, boolean key) {
Element prop_mapping;
if (key) {
prop_mapping = parent.addElement("key-property");
@@ -59,7 +59,7 @@
prop_mapping.addAttribute("name", name);
prop_mapping.addAttribute("insert", Boolean.toString(insertable));
- prop_mapping.addAttribute("update", "false");
+ prop_mapping.addAttribute("update", Boolean.toString(updateable));
if (type != null) {
prop_mapping.addAttribute("type", type);
@@ -68,6 +68,10 @@
return prop_mapping;
}
+ public static Element addProperty(Element parent, String name, String type, boolean insertable, boolean key) {
+ return addProperty(parent, name, type, insertable, false, key);
+ }
+
private static void addOrModifyAttribute(Element parent, String name, String value) {
Attribute attribute = parent.attribute(name);
if (attribute == null) {
Modified: core/trunk/envers/src/main/java/org/hibernate/envers/strategy/ValidityAuditStrategy.java
===================================================================
--- core/trunk/envers/src/main/java/org/hibernate/envers/strategy/ValidityAuditStrategy.java 2010-10-04 14:52:19 UTC (rev 20774)
+++ core/trunk/envers/src/main/java/org/hibernate/envers/strategy/ValidityAuditStrategy.java 2010-10-04 18:11:33 UTC (rev 20775)
@@ -1,6 +1,7 @@
package org.hibernate.envers.strategy;
import java.io.Serializable;
+import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -15,6 +16,7 @@
import org.hibernate.envers.entities.mapper.relation.MiddleIdData;
import org.hibernate.envers.tools.query.Parameters;
import org.hibernate.envers.tools.query.QueryBuilder;
+import org.hibernate.property.Getter;
/**
* Audit strategy which persists and retrieves audit information using a validity algorithm, based on the
@@ -39,6 +41,10 @@
* @author Adam Warski (adam at warski dot org)
*/
public class ValidityAuditStrategy implements AuditStrategy {
+
+ /** getter for the revision entity field annotated with @RevisionTimestamp */
+ private Getter revisionTimestampGetter = null;
+
public void perform(Session session, String entityName, AuditConfiguration auditCfg, Serializable id, Object data,
Object revision) {
AuditEntitiesConfiguration audEntCfg = auditCfg.getAuditEntCfg();
@@ -110,6 +116,10 @@
addRevisionRestriction(rootParameters, revisionProperty, revisionEndProperty, addAlias);
}
+ public void setRevisionTimestampGetter(Getter revisionTimestampGetter) {
+ this.revisionTimestampGetter = revisionTimestampGetter;
+ }
+
private void addRevisionRestriction(Parameters rootParameters,
String revisionProperty, String revisionEndProperty, boolean addAlias) {
@@ -129,7 +139,7 @@
private void updateLastRevision(Session session, AuditConfiguration auditCfg, QueryBuilder qb,
Object id, String auditedEntityName, Object revision) {
String revisionEndFieldName = auditCfg.getAuditEntCfg().getRevisionEndFieldName();
-
+
// e.end_rev is null
qb.getRootParameters().addWhere(revisionEndFieldName, true, "is", "null", false);
@@ -141,10 +151,29 @@
Object previousData = l.get(0);
((Map<String, Object>) previousData).put(revisionEndFieldName, revision);
+ if (auditCfg.getAuditEntCfg().isRevisionEndTimestampEnabled()) {
+ // Determine the value of the revision property annotated with @RevisionTimestamp
+ Date revisionEndTimestamp;
+ String revEndTimestampFieldName = auditCfg.getAuditEntCfg().getRevisionEndTimestampFieldName();
+ Object revEndTimestampObj = this.revisionTimestampGetter.get(revision);
+
+ // convert to a java.util.Date
+ if (revEndTimestampObj instanceof Date) {
+ revisionEndTimestamp = (Date) revEndTimestampObj;
+ } else {
+ revisionEndTimestamp = new Date((Long) revEndTimestampObj);
+ }
+
+ // Setting the end revision timestamp
+ ((Map<String, Object>) previousData).put(revEndTimestampFieldName, revisionEndTimestamp);
+ }
+
// Saving the previous version
session.save(auditedEntityName, previousData);
+
} else {
throw new RuntimeException("Cannot find previous revision for entity " + auditedEntityName + " and id " + id);
}
}
}
+
Copied: core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy (from rev 20744, core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy)
Deleted: core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/ValidityAuditStrategyRevEndTestCustomRevEnt.java
===================================================================
--- core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/ValidityAuditStrategyRevEndTestCustomRevEnt.java 2010-09-28 22:24:09 UTC (rev 20744)
+++ core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/ValidityAuditStrategyRevEndTestCustomRevEnt.java 2010-10-04 18:11:33 UTC (rev 20775)
@@ -1,433 +0,0 @@
-/*
- * Hibernate, Relational Persistence for Idiomatic Java
- *
- * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
- * indicated by the @author tags or express copyright attribution
- * statements applied by the authors. All third-party contributions are
- * distributed under license by Red Hat Middleware LLC.
- *
- * This copyrighted material is made available to anyone wishing to use, modify,
- * copy, or redistribute it subject to the terms and conditions of the GNU
- * Lesser General Public License, as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
- * for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this distribution; if not, write to:
- * Free Software Foundation, Inc.
- * 51 Franklin Street, Fifth Floor
- * Boston, MA 02110-1301 USA
- */
-package org.hibernate.envers.test.integration.strategy;
-
-import java.util.Arrays;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import javax.persistence.EntityManager;
-import javax.persistence.Query;
-
-import org.hibernate.Session;
-import org.hibernate.ejb.Ejb3Configuration;
-import org.hibernate.envers.strategy.ValidityAuditStrategy;
-import org.hibernate.envers.test.AbstractEntityTest;
-import org.hibernate.envers.test.entities.manytomany.sametable.Child1Entity;
-import org.hibernate.envers.test.entities.manytomany.sametable.Child2Entity;
-import org.hibernate.envers.test.entities.manytomany.sametable.ParentEntity;
-import org.hibernate.envers.test.entities.reventity.CustomDateRevEntity;
-import org.hibernate.envers.test.tools.TestTools;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-/**
- * Test which checks that the revision end timestamp is correctly set for
- * {@link ValidityAuditStrategy}.
- *
- * @author Erik-Berndt Scheper
- */
-public class ValidityAuditStrategyRevEndTestCustomRevEnt extends AbstractEntityTest {
- private final String revendTimestampColumName = "REVEND_TIMESTAMP";
-
- private Integer p1_id;
- private Integer p2_id;
- private Integer c1_1_id;
- private Integer c1_2_id;
- private Integer c2_1_id;
- private Integer c2_2_id;
- private Map<Number, CustomDateRevEntity> revisions;
-
- public void configure(Ejb3Configuration cfg) {
- cfg.addAnnotatedClass(ParentEntity.class);
- cfg.addAnnotatedClass(Child1Entity.class);
- cfg.addAnnotatedClass(Child2Entity.class);
- cfg.addAnnotatedClass(CustomDateRevEntity.class);
-
- cfg.setProperty("org.hibernate.envers.audit_strategy",
- "org.hibernate.envers.strategy.ValidityAuditStrategy");
- cfg
- .setProperty(
- "org.hibernate.envers.audit_strategy_validity_store_revend_timestamp",
- "true");
- cfg
- .setProperty(
- "org.hibernate.envers.audit_strategy_validity_revend_timestamp_field_name",
- revendTimestampColumName);
- }
-
- @BeforeClass(enabled = true, dependsOnMethods = "init")
- public void initData() {
- EntityManager em = getEntityManager();
-
- // We need first to modify the columns in the middle (join table) to
- // allow null values. Hbm2ddl doesn't seem
- // to allow this.
- em.getTransaction().begin();
- Session session = (Session) em.getDelegate();
- session.createSQLQuery("DROP TABLE children").executeUpdate();
- session
- .createSQLQuery(
- "CREATE TABLE children(parent_id integer, child1_id integer NULL, child2_id integer NULL)")
- .executeUpdate();
- session.createSQLQuery("DROP TABLE children_AUD").executeUpdate();
- session
- .createSQLQuery(
- "CREATE TABLE children_AUD(REV integer NOT NULL, REVEND integer, "
- + revendTimestampColumName
- + " timestamp, REVTYPE tinyint, "
- + "parent_id integer, child1_id integer NULL, child2_id integer NULL)")
- .executeUpdate();
- em.getTransaction().commit();
- em.clear();
-
- ParentEntity p1 = new ParentEntity("parent_1");
- ParentEntity p2 = new ParentEntity("parent_2");
-
- Child1Entity c1_1 = new Child1Entity("child1_1");
- Child1Entity c1_2 = new Child1Entity("child1_2");
-
- Child2Entity c2_1 = new Child2Entity("child2_1");
- Child2Entity c2_2 = new Child2Entity("child2_2");
-
- // Revision 1
- em.getTransaction().begin();
-
- em.persist(p1);
- em.persist(p2);
- em.persist(c1_1);
- em.persist(c1_2);
- em.persist(c2_1);
- em.persist(c2_2);
-
- em.getTransaction().commit();
- em.clear();
-
- // Revision 2 - (p1: c1_1, p2: c2_1)
-
- em.getTransaction().begin();
-
- p1 = em.find(ParentEntity.class, p1.getId());
- p2 = em.find(ParentEntity.class, p2.getId());
- c1_1 = em.find(Child1Entity.class, c1_1.getId());
- c2_1 = em.find(Child2Entity.class, c2_1.getId());
-
- p1.getChildren1().add(c1_1);
- p2.getChildren2().add(c2_1);
-
- em.getTransaction().commit();
- em.clear();
-
- // Revision 3 - (p1: c1_1, c1_2, c2_2, p2: c1_1, c2_1)
- em.getTransaction().begin();
-
- p1 = em.find(ParentEntity.class, p1.getId());
- p2 = em.find(ParentEntity.class, p2.getId());
- c1_1 = em.find(Child1Entity.class, c1_1.getId());
- c1_2 = em.find(Child1Entity.class, c1_2.getId());
- c2_2 = em.find(Child2Entity.class, c2_2.getId());
-
- p1.getChildren1().add(c1_2);
- p1.getChildren2().add(c2_2);
-
- p2.getChildren1().add(c1_1);
-
- em.getTransaction().commit();
- em.clear();
-
- // Revision 4 - (p1: c1_2, c2_2, p2: c1_1, c2_1, c2_2)
- em.getTransaction().begin();
-
- p1 = em.find(ParentEntity.class, p1.getId());
- p2 = em.find(ParentEntity.class, p2.getId());
- c1_1 = em.find(Child1Entity.class, c1_1.getId());
- c2_2 = em.find(Child2Entity.class, c2_2.getId());
-
- p1.getChildren1().remove(c1_1);
- p2.getChildren2().add(c2_2);
-
- em.getTransaction().commit();
- em.clear();
-
- // Revision 5 - (p1: c2_2, p2: c1_1, c2_1)
- em.getTransaction().begin();
-
- p1 = em.find(ParentEntity.class, p1.getId());
- p2 = em.find(ParentEntity.class, p2.getId());
- c1_2 = em.find(Child1Entity.class, c1_2.getId());
- c2_2 = em.find(Child2Entity.class, c2_2.getId());
-
- c2_2.getParents().remove(p2);
- c1_2.getParents().remove(p1);
-
- em.getTransaction().commit();
- em.clear();
-
- //
-
- p1_id = p1.getId();
- p2_id = p2.getId();
- c1_1_id = c1_1.getId();
- c1_2_id = c1_2.getId();
- c2_1_id = c2_1.getId();
- c2_2_id = c2_2.getId();
-
- Set<Number> revisionNumbers = new HashSet<Number>();
- revisionNumbers.addAll(Arrays.asList(1, 2, 3, 4, 5));
- revisions = getAuditReader().findRevisions(CustomDateRevEntity.class,
- revisionNumbers);
-
- assert revisions.size() == 5;
- }
-
- @Test(enabled = true)
- public void testRevisionsCounts() {
- assert Arrays.asList(1, 2, 3, 4).equals(
- getAuditReader().getRevisions(ParentEntity.class, p1_id));
- assert Arrays.asList(1, 2, 3, 4).equals(
- getAuditReader().getRevisions(ParentEntity.class, p2_id));
-
- assert Arrays.asList(1).equals(
- getAuditReader().getRevisions(Child1Entity.class, c1_1_id));
- assert Arrays.asList(1, 5).equals(
- getAuditReader().getRevisions(Child1Entity.class, c1_2_id));
-
- assert Arrays.asList(1).equals(
- getAuditReader().getRevisions(Child2Entity.class, c2_1_id));
- assert Arrays.asList(1, 5).equals(
- getAuditReader().getRevisions(Child2Entity.class, c2_2_id));
- }
-
- @Test(enabled = true)
- public void testAllRevEndTimeStamps() {
- List<Map<String, Object>> p1RevList = getRevisions(ParentEntity.class,
- p1_id);
- List<Map<String, Object>> p2RevList = getRevisions(ParentEntity.class,
- p2_id);
- List<Map<String, Object>> c1_1_List = getRevisions(Child1Entity.class,
- c1_1_id);
- List<Map<String, Object>> c1_2_List = getRevisions(Child1Entity.class,
- c1_2_id);
- List<Map<String, Object>> c2_1_List = getRevisions(Child2Entity.class,
- c2_1_id);
- List<Map<String, Object>> c2_2_List = getRevisions(Child2Entity.class,
- c2_2_id);
-
- verifyRevEndTimeStamps("ParentEntity: " + p1_id, p1RevList);
- verifyRevEndTimeStamps("ParentEntity: " + p2_id, p2RevList);
- verifyRevEndTimeStamps("Child1Entity: " + c1_1_id, c1_1_List);
- verifyRevEndTimeStamps("Child1Entity: " + c1_2_id, c1_2_List);
- verifyRevEndTimeStamps("Child2Entity: " + c2_1_id, c2_1_List);
- verifyRevEndTimeStamps("Child2Entity: " + c2_2_id, c2_2_List);
-
- }
-
- @Test(enabled = true)
- public void testHistoryOfParent1() {
-
- Child1Entity c1_1 = getEntityManager()
- .find(Child1Entity.class, c1_1_id);
- Child1Entity c1_2 = getEntityManager()
- .find(Child1Entity.class, c1_2_id);
- Child2Entity c2_2 = getEntityManager()
- .find(Child2Entity.class, c2_2_id);
-
- ParentEntity rev1 = getAuditReader().find(ParentEntity.class, p1_id, 1);
- ParentEntity rev2 = getAuditReader().find(ParentEntity.class, p1_id, 2);
- ParentEntity rev3 = getAuditReader().find(ParentEntity.class, p1_id, 3);
- ParentEntity rev4 = getAuditReader().find(ParentEntity.class, p1_id, 4);
- ParentEntity rev5 = getAuditReader().find(ParentEntity.class, p1_id, 5);
-
- assert TestTools.checkList(rev1.getChildren1());
- assert TestTools.checkList(rev2.getChildren1(), c1_1);
- assert TestTools.checkList(rev3.getChildren1(), c1_1, c1_2);
- assert TestTools.checkList(rev4.getChildren1(), c1_2);
- assert TestTools.checkList(rev5.getChildren1());
-
- assert TestTools.checkList(rev1.getChildren2());
- assert TestTools.checkList(rev2.getChildren2());
- assert TestTools.checkList(rev3.getChildren2(), c2_2);
- assert TestTools.checkList(rev4.getChildren2(), c2_2);
- assert TestTools.checkList(rev5.getChildren2(), c2_2);
- }
-
- @Test(enabled = true)
- public void testHistoryOfParent2() {
- Child1Entity c1_1 = getEntityManager()
- .find(Child1Entity.class, c1_1_id);
- Child2Entity c2_1 = getEntityManager()
- .find(Child2Entity.class, c2_1_id);
- Child2Entity c2_2 = getEntityManager()
- .find(Child2Entity.class, c2_2_id);
-
- ParentEntity rev1 = getAuditReader().find(ParentEntity.class, p2_id, 1);
- ParentEntity rev2 = getAuditReader().find(ParentEntity.class, p2_id, 2);
- ParentEntity rev3 = getAuditReader().find(ParentEntity.class, p2_id, 3);
- ParentEntity rev4 = getAuditReader().find(ParentEntity.class, p2_id, 4);
- ParentEntity rev5 = getAuditReader().find(ParentEntity.class, p2_id, 5);
-
- assert TestTools.checkList(rev1.getChildren1());
- assert TestTools.checkList(rev2.getChildren1());
- assert TestTools.checkList(rev3.getChildren1(), c1_1);
- assert TestTools.checkList(rev4.getChildren1(), c1_1);
- assert TestTools.checkList(rev5.getChildren1(), c1_1);
-
- assert TestTools.checkList(rev1.getChildren2());
- assert TestTools.checkList(rev2.getChildren2(), c2_1);
- assert TestTools.checkList(rev3.getChildren2(), c2_1);
- assert TestTools.checkList(rev4.getChildren2(), c2_1, c2_2);
- assert TestTools.checkList(rev5.getChildren2(), c2_1);
- }
-
- @Test(enabled = true)
- public void testHistoryOfChild1_1() {
- ParentEntity p1 = getEntityManager().find(ParentEntity.class, p1_id);
- ParentEntity p2 = getEntityManager().find(ParentEntity.class, p2_id);
-
- Child1Entity rev1 = getAuditReader().find(Child1Entity.class, c1_1_id,
- 1);
- Child1Entity rev2 = getAuditReader().find(Child1Entity.class, c1_1_id,
- 2);
- Child1Entity rev3 = getAuditReader().find(Child1Entity.class, c1_1_id,
- 3);
- Child1Entity rev4 = getAuditReader().find(Child1Entity.class, c1_1_id,
- 4);
- Child1Entity rev5 = getAuditReader().find(Child1Entity.class, c1_1_id,
- 5);
-
- assert TestTools.checkList(rev1.getParents());
- assert TestTools.checkList(rev2.getParents(), p1);
- assert TestTools.checkList(rev3.getParents(), p1, p2);
- assert TestTools.checkList(rev4.getParents(), p2);
- assert TestTools.checkList(rev5.getParents(), p2);
- }
-
- @Test(enabled = false)
- public void testHistoryOfChild1_2() {
- ParentEntity p1 = getEntityManager().find(ParentEntity.class, p1_id);
-
- Child1Entity rev1 = getAuditReader().find(Child1Entity.class, c1_2_id,
- 1);
- Child1Entity rev2 = getAuditReader().find(Child1Entity.class, c1_2_id,
- 2);
- Child1Entity rev3 = getAuditReader().find(Child1Entity.class, c1_2_id,
- 3);
- Child1Entity rev4 = getAuditReader().find(Child1Entity.class, c1_2_id,
- 4);
- Child1Entity rev5 = getAuditReader().find(Child1Entity.class, c1_2_id,
- 5);
-
- assert TestTools.checkList(rev1.getParents());
- assert TestTools.checkList(rev2.getParents());
- assert TestTools.checkList(rev3.getParents(), p1);
- assert TestTools.checkList(rev4.getParents(), p1);
- assert TestTools.checkList(rev5.getParents());
- }
-
- @Test(enabled = true)
- public void testHistoryOfChild2_1() {
- ParentEntity p2 = getEntityManager().find(ParentEntity.class, p2_id);
-
- Child2Entity rev1 = getAuditReader().find(Child2Entity.class, c2_1_id,
- 1);
- Child2Entity rev2 = getAuditReader().find(Child2Entity.class, c2_1_id,
- 2);
- Child2Entity rev3 = getAuditReader().find(Child2Entity.class, c2_1_id,
- 3);
- Child2Entity rev4 = getAuditReader().find(Child2Entity.class, c2_1_id,
- 4);
- Child2Entity rev5 = getAuditReader().find(Child2Entity.class, c2_1_id,
- 5);
-
- assert TestTools.checkList(rev1.getParents());
- assert TestTools.checkList(rev2.getParents(), p2);
- assert TestTools.checkList(rev3.getParents(), p2);
- assert TestTools.checkList(rev4.getParents(), p2);
- assert TestTools.checkList(rev5.getParents(), p2);
- }
-
- @Test(enabled = true)
- public void testHistoryOfChild2_2() {
- ParentEntity p1 = getEntityManager().find(ParentEntity.class, p1_id);
- ParentEntity p2 = getEntityManager().find(ParentEntity.class, p2_id);
-
- Child2Entity rev1 = getAuditReader().find(Child2Entity.class, c2_2_id,
- 1);
- Child2Entity rev2 = getAuditReader().find(Child2Entity.class, c2_2_id,
- 2);
- Child2Entity rev3 = getAuditReader().find(Child2Entity.class, c2_2_id,
- 3);
- Child2Entity rev4 = getAuditReader().find(Child2Entity.class, c2_2_id,
- 4);
- Child2Entity rev5 = getAuditReader().find(Child2Entity.class, c2_2_id,
- 5);
-
- assert TestTools.checkList(rev1.getParents());
- assert TestTools.checkList(rev2.getParents());
- assert TestTools.checkList(rev3.getParents(), p1);
- assert TestTools.checkList(rev4.getParents(), p1, p2);
- assert TestTools.checkList(rev5.getParents(), p1);
- }
-
- private List<Map<String, Object>> getRevisions(
- Class<?> originalEntityClazz, Integer originalEntityId) {
- // Build the query:
- // select auditEntity from
- // org.hibernate.envers.test.entities.manytomany.sametable.ParentEntity_AUD
- // auditEntity where auditEntity.originalId.id = :originalEntityId
-
- StringBuilder builder = new StringBuilder("select auditEntity from ");
- builder.append(originalEntityClazz.getName())
- .append("_AUD auditEntity");
- builder.append(" where auditEntity.originalId.id = :originalEntityId");
-
- Query qry = getEntityManager().createQuery(builder.toString());
- qry.setParameter("originalEntityId", originalEntityId);
-
- @SuppressWarnings("unchecked")
- List<Map<String, Object>> resultList = qry.getResultList();
- return resultList;
- }
-
- private void verifyRevEndTimeStamps(String debugInfo,
- List<Map<String, Object>> revisionEntities) {
- for (Map<String, Object> revisionEntity : revisionEntities) {
-
- Date revendTimestamp = (Date) revisionEntity
- .get(revendTimestampColumName);
- CustomDateRevEntity revEnd = (CustomDateRevEntity) revisionEntity
- .get("REVEND");
-
- if (revendTimestamp == null) {
- assert revEnd == null;
- } else {
- assert revendTimestamp.getTime() == revEnd.getDateTimestamp().getTime();
- }
- }
- }
-
-}
Copied: core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/ValidityAuditStrategyRevEndTestCustomRevEnt.java (from rev 20744, core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/ValidityAuditStrategyRevEndTestCustomRevEnt.java)
===================================================================
--- core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/ValidityAuditStrategyRevEndTestCustomRevEnt.java (rev 0)
+++ core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/ValidityAuditStrategyRevEndTestCustomRevEnt.java 2010-10-04 18:11:33 UTC (rev 20775)
@@ -0,0 +1,433 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.envers.test.integration.strategy;
+
+import java.util.Arrays;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import org.hibernate.Session;
+import org.hibernate.ejb.Ejb3Configuration;
+import org.hibernate.envers.strategy.ValidityAuditStrategy;
+import org.hibernate.envers.test.AbstractEntityTest;
+import org.hibernate.envers.test.entities.manytomany.sametable.Child1Entity;
+import org.hibernate.envers.test.entities.manytomany.sametable.Child2Entity;
+import org.hibernate.envers.test.entities.manytomany.sametable.ParentEntity;
+import org.hibernate.envers.test.entities.reventity.CustomDateRevEntity;
+import org.hibernate.envers.test.tools.TestTools;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+/**
+ * Test which checks that the revision end timestamp is correctly set for
+ * {@link ValidityAuditStrategy}.
+ *
+ * @author Erik-Berndt Scheper
+ */
+public class ValidityAuditStrategyRevEndTestCustomRevEnt extends AbstractEntityTest {
+ private final String revendTimestampColumName = "REVEND_TIMESTAMP";
+
+ private Integer p1_id;
+ private Integer p2_id;
+ private Integer c1_1_id;
+ private Integer c1_2_id;
+ private Integer c2_1_id;
+ private Integer c2_2_id;
+ private Map<Number, CustomDateRevEntity> revisions;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(ParentEntity.class);
+ cfg.addAnnotatedClass(Child1Entity.class);
+ cfg.addAnnotatedClass(Child2Entity.class);
+ cfg.addAnnotatedClass(CustomDateRevEntity.class);
+
+ cfg.setProperty("org.hibernate.envers.audit_strategy",
+ "org.hibernate.envers.strategy.ValidityAuditStrategy");
+ cfg
+ .setProperty(
+ "org.hibernate.envers.audit_strategy_validity_store_revend_timestamp",
+ "true");
+ cfg
+ .setProperty(
+ "org.hibernate.envers.audit_strategy_validity_revend_timestamp_field_name",
+ revendTimestampColumName);
+ }
+
+ @BeforeClass(enabled = true, dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ // We need first to modify the columns in the middle (join table) to
+ // allow null values. Hbm2ddl doesn't seem
+ // to allow this.
+ em.getTransaction().begin();
+ Session session = (Session) em.getDelegate();
+ session.createSQLQuery("DROP TABLE children").executeUpdate();
+ session
+ .createSQLQuery(
+ "CREATE TABLE children(parent_id integer, child1_id integer NULL, child2_id integer NULL)")
+ .executeUpdate();
+ session.createSQLQuery("DROP TABLE children_AUD").executeUpdate();
+ session
+ .createSQLQuery(
+ "CREATE TABLE children_AUD(REV integer NOT NULL, REVEND integer, "
+ + revendTimestampColumName
+ + " timestamp, REVTYPE tinyint, "
+ + "parent_id integer, child1_id integer NULL, child2_id integer NULL)")
+ .executeUpdate();
+ em.getTransaction().commit();
+ em.clear();
+
+ ParentEntity p1 = new ParentEntity("parent_1");
+ ParentEntity p2 = new ParentEntity("parent_2");
+
+ Child1Entity c1_1 = new Child1Entity("child1_1");
+ Child1Entity c1_2 = new Child1Entity("child1_2");
+
+ Child2Entity c2_1 = new Child2Entity("child2_1");
+ Child2Entity c2_2 = new Child2Entity("child2_2");
+
+ // Revision 1
+ em.getTransaction().begin();
+
+ em.persist(p1);
+ em.persist(p2);
+ em.persist(c1_1);
+ em.persist(c1_2);
+ em.persist(c2_1);
+ em.persist(c2_2);
+
+ em.getTransaction().commit();
+ em.clear();
+
+ // Revision 2 - (p1: c1_1, p2: c2_1)
+
+ em.getTransaction().begin();
+
+ p1 = em.find(ParentEntity.class, p1.getId());
+ p2 = em.find(ParentEntity.class, p2.getId());
+ c1_1 = em.find(Child1Entity.class, c1_1.getId());
+ c2_1 = em.find(Child2Entity.class, c2_1.getId());
+
+ p1.getChildren1().add(c1_1);
+ p2.getChildren2().add(c2_1);
+
+ em.getTransaction().commit();
+ em.clear();
+
+ // Revision 3 - (p1: c1_1, c1_2, c2_2, p2: c1_1, c2_1)
+ em.getTransaction().begin();
+
+ p1 = em.find(ParentEntity.class, p1.getId());
+ p2 = em.find(ParentEntity.class, p2.getId());
+ c1_1 = em.find(Child1Entity.class, c1_1.getId());
+ c1_2 = em.find(Child1Entity.class, c1_2.getId());
+ c2_2 = em.find(Child2Entity.class, c2_2.getId());
+
+ p1.getChildren1().add(c1_2);
+ p1.getChildren2().add(c2_2);
+
+ p2.getChildren1().add(c1_1);
+
+ em.getTransaction().commit();
+ em.clear();
+
+ // Revision 4 - (p1: c1_2, c2_2, p2: c1_1, c2_1, c2_2)
+ em.getTransaction().begin();
+
+ p1 = em.find(ParentEntity.class, p1.getId());
+ p2 = em.find(ParentEntity.class, p2.getId());
+ c1_1 = em.find(Child1Entity.class, c1_1.getId());
+ c2_2 = em.find(Child2Entity.class, c2_2.getId());
+
+ p1.getChildren1().remove(c1_1);
+ p2.getChildren2().add(c2_2);
+
+ em.getTransaction().commit();
+ em.clear();
+
+ // Revision 5 - (p1: c2_2, p2: c1_1, c2_1)
+ em.getTransaction().begin();
+
+ p1 = em.find(ParentEntity.class, p1.getId());
+ p2 = em.find(ParentEntity.class, p2.getId());
+ c1_2 = em.find(Child1Entity.class, c1_2.getId());
+ c2_2 = em.find(Child2Entity.class, c2_2.getId());
+
+ c2_2.getParents().remove(p2);
+ c1_2.getParents().remove(p1);
+
+ em.getTransaction().commit();
+ em.clear();
+
+ //
+
+ p1_id = p1.getId();
+ p2_id = p2.getId();
+ c1_1_id = c1_1.getId();
+ c1_2_id = c1_2.getId();
+ c2_1_id = c2_1.getId();
+ c2_2_id = c2_2.getId();
+
+ Set<Number> revisionNumbers = new HashSet<Number>();
+ revisionNumbers.addAll(Arrays.asList(1, 2, 3, 4, 5));
+ revisions = getAuditReader().findRevisions(CustomDateRevEntity.class,
+ revisionNumbers);
+
+ assert revisions.size() == 5;
+ }
+
+ @Test(enabled = true)
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1, 2, 3, 4).equals(
+ getAuditReader().getRevisions(ParentEntity.class, p1_id));
+ assert Arrays.asList(1, 2, 3, 4).equals(
+ getAuditReader().getRevisions(ParentEntity.class, p2_id));
+
+ assert Arrays.asList(1).equals(
+ getAuditReader().getRevisions(Child1Entity.class, c1_1_id));
+ assert Arrays.asList(1, 5).equals(
+ getAuditReader().getRevisions(Child1Entity.class, c1_2_id));
+
+ assert Arrays.asList(1).equals(
+ getAuditReader().getRevisions(Child2Entity.class, c2_1_id));
+ assert Arrays.asList(1, 5).equals(
+ getAuditReader().getRevisions(Child2Entity.class, c2_2_id));
+ }
+
+ @Test(enabled = true)
+ public void testAllRevEndTimeStamps() {
+ List<Map<String, Object>> p1RevList = getRevisions(ParentEntity.class,
+ p1_id);
+ List<Map<String, Object>> p2RevList = getRevisions(ParentEntity.class,
+ p2_id);
+ List<Map<String, Object>> c1_1_List = getRevisions(Child1Entity.class,
+ c1_1_id);
+ List<Map<String, Object>> c1_2_List = getRevisions(Child1Entity.class,
+ c1_2_id);
+ List<Map<String, Object>> c2_1_List = getRevisions(Child2Entity.class,
+ c2_1_id);
+ List<Map<String, Object>> c2_2_List = getRevisions(Child2Entity.class,
+ c2_2_id);
+
+ verifyRevEndTimeStamps("ParentEntity: " + p1_id, p1RevList);
+ verifyRevEndTimeStamps("ParentEntity: " + p2_id, p2RevList);
+ verifyRevEndTimeStamps("Child1Entity: " + c1_1_id, c1_1_List);
+ verifyRevEndTimeStamps("Child1Entity: " + c1_2_id, c1_2_List);
+ verifyRevEndTimeStamps("Child2Entity: " + c2_1_id, c2_1_List);
+ verifyRevEndTimeStamps("Child2Entity: " + c2_2_id, c2_2_List);
+
+ }
+
+ @Test(enabled = true)
+ public void testHistoryOfParent1() {
+
+ Child1Entity c1_1 = getEntityManager()
+ .find(Child1Entity.class, c1_1_id);
+ Child1Entity c1_2 = getEntityManager()
+ .find(Child1Entity.class, c1_2_id);
+ Child2Entity c2_2 = getEntityManager()
+ .find(Child2Entity.class, c2_2_id);
+
+ ParentEntity rev1 = getAuditReader().find(ParentEntity.class, p1_id, 1);
+ ParentEntity rev2 = getAuditReader().find(ParentEntity.class, p1_id, 2);
+ ParentEntity rev3 = getAuditReader().find(ParentEntity.class, p1_id, 3);
+ ParentEntity rev4 = getAuditReader().find(ParentEntity.class, p1_id, 4);
+ ParentEntity rev5 = getAuditReader().find(ParentEntity.class, p1_id, 5);
+
+ assert TestTools.checkList(rev1.getChildren1());
+ assert TestTools.checkList(rev2.getChildren1(), c1_1);
+ assert TestTools.checkList(rev3.getChildren1(), c1_1, c1_2);
+ assert TestTools.checkList(rev4.getChildren1(), c1_2);
+ assert TestTools.checkList(rev5.getChildren1());
+
+ assert TestTools.checkList(rev1.getChildren2());
+ assert TestTools.checkList(rev2.getChildren2());
+ assert TestTools.checkList(rev3.getChildren2(), c2_2);
+ assert TestTools.checkList(rev4.getChildren2(), c2_2);
+ assert TestTools.checkList(rev5.getChildren2(), c2_2);
+ }
+
+ @Test(enabled = true)
+ public void testHistoryOfParent2() {
+ Child1Entity c1_1 = getEntityManager()
+ .find(Child1Entity.class, c1_1_id);
+ Child2Entity c2_1 = getEntityManager()
+ .find(Child2Entity.class, c2_1_id);
+ Child2Entity c2_2 = getEntityManager()
+ .find(Child2Entity.class, c2_2_id);
+
+ ParentEntity rev1 = getAuditReader().find(ParentEntity.class, p2_id, 1);
+ ParentEntity rev2 = getAuditReader().find(ParentEntity.class, p2_id, 2);
+ ParentEntity rev3 = getAuditReader().find(ParentEntity.class, p2_id, 3);
+ ParentEntity rev4 = getAuditReader().find(ParentEntity.class, p2_id, 4);
+ ParentEntity rev5 = getAuditReader().find(ParentEntity.class, p2_id, 5);
+
+ assert TestTools.checkList(rev1.getChildren1());
+ assert TestTools.checkList(rev2.getChildren1());
+ assert TestTools.checkList(rev3.getChildren1(), c1_1);
+ assert TestTools.checkList(rev4.getChildren1(), c1_1);
+ assert TestTools.checkList(rev5.getChildren1(), c1_1);
+
+ assert TestTools.checkList(rev1.getChildren2());
+ assert TestTools.checkList(rev2.getChildren2(), c2_1);
+ assert TestTools.checkList(rev3.getChildren2(), c2_1);
+ assert TestTools.checkList(rev4.getChildren2(), c2_1, c2_2);
+ assert TestTools.checkList(rev5.getChildren2(), c2_1);
+ }
+
+ @Test(enabled = true)
+ public void testHistoryOfChild1_1() {
+ ParentEntity p1 = getEntityManager().find(ParentEntity.class, p1_id);
+ ParentEntity p2 = getEntityManager().find(ParentEntity.class, p2_id);
+
+ Child1Entity rev1 = getAuditReader().find(Child1Entity.class, c1_1_id,
+ 1);
+ Child1Entity rev2 = getAuditReader().find(Child1Entity.class, c1_1_id,
+ 2);
+ Child1Entity rev3 = getAuditReader().find(Child1Entity.class, c1_1_id,
+ 3);
+ Child1Entity rev4 = getAuditReader().find(Child1Entity.class, c1_1_id,
+ 4);
+ Child1Entity rev5 = getAuditReader().find(Child1Entity.class, c1_1_id,
+ 5);
+
+ assert TestTools.checkList(rev1.getParents());
+ assert TestTools.checkList(rev2.getParents(), p1);
+ assert TestTools.checkList(rev3.getParents(), p1, p2);
+ assert TestTools.checkList(rev4.getParents(), p2);
+ assert TestTools.checkList(rev5.getParents(), p2);
+ }
+
+ @Test(enabled = false)
+ public void testHistoryOfChild1_2() {
+ ParentEntity p1 = getEntityManager().find(ParentEntity.class, p1_id);
+
+ Child1Entity rev1 = getAuditReader().find(Child1Entity.class, c1_2_id,
+ 1);
+ Child1Entity rev2 = getAuditReader().find(Child1Entity.class, c1_2_id,
+ 2);
+ Child1Entity rev3 = getAuditReader().find(Child1Entity.class, c1_2_id,
+ 3);
+ Child1Entity rev4 = getAuditReader().find(Child1Entity.class, c1_2_id,
+ 4);
+ Child1Entity rev5 = getAuditReader().find(Child1Entity.class, c1_2_id,
+ 5);
+
+ assert TestTools.checkList(rev1.getParents());
+ assert TestTools.checkList(rev2.getParents());
+ assert TestTools.checkList(rev3.getParents(), p1);
+ assert TestTools.checkList(rev4.getParents(), p1);
+ assert TestTools.checkList(rev5.getParents());
+ }
+
+ @Test(enabled = true)
+ public void testHistoryOfChild2_1() {
+ ParentEntity p2 = getEntityManager().find(ParentEntity.class, p2_id);
+
+ Child2Entity rev1 = getAuditReader().find(Child2Entity.class, c2_1_id,
+ 1);
+ Child2Entity rev2 = getAuditReader().find(Child2Entity.class, c2_1_id,
+ 2);
+ Child2Entity rev3 = getAuditReader().find(Child2Entity.class, c2_1_id,
+ 3);
+ Child2Entity rev4 = getAuditReader().find(Child2Entity.class, c2_1_id,
+ 4);
+ Child2Entity rev5 = getAuditReader().find(Child2Entity.class, c2_1_id,
+ 5);
+
+ assert TestTools.checkList(rev1.getParents());
+ assert TestTools.checkList(rev2.getParents(), p2);
+ assert TestTools.checkList(rev3.getParents(), p2);
+ assert TestTools.checkList(rev4.getParents(), p2);
+ assert TestTools.checkList(rev5.getParents(), p2);
+ }
+
+ @Test(enabled = true)
+ public void testHistoryOfChild2_2() {
+ ParentEntity p1 = getEntityManager().find(ParentEntity.class, p1_id);
+ ParentEntity p2 = getEntityManager().find(ParentEntity.class, p2_id);
+
+ Child2Entity rev1 = getAuditReader().find(Child2Entity.class, c2_2_id,
+ 1);
+ Child2Entity rev2 = getAuditReader().find(Child2Entity.class, c2_2_id,
+ 2);
+ Child2Entity rev3 = getAuditReader().find(Child2Entity.class, c2_2_id,
+ 3);
+ Child2Entity rev4 = getAuditReader().find(Child2Entity.class, c2_2_id,
+ 4);
+ Child2Entity rev5 = getAuditReader().find(Child2Entity.class, c2_2_id,
+ 5);
+
+ assert TestTools.checkList(rev1.getParents());
+ assert TestTools.checkList(rev2.getParents());
+ assert TestTools.checkList(rev3.getParents(), p1);
+ assert TestTools.checkList(rev4.getParents(), p1, p2);
+ assert TestTools.checkList(rev5.getParents(), p1);
+ }
+
+ private List<Map<String, Object>> getRevisions(
+ Class<?> originalEntityClazz, Integer originalEntityId) {
+ // Build the query:
+ // select auditEntity from
+ // org.hibernate.envers.test.entities.manytomany.sametable.ParentEntity_AUD
+ // auditEntity where auditEntity.originalId.id = :originalEntityId
+
+ StringBuilder builder = new StringBuilder("select auditEntity from ");
+ builder.append(originalEntityClazz.getName())
+ .append("_AUD auditEntity");
+ builder.append(" where auditEntity.originalId.id = :originalEntityId");
+
+ Query qry = getEntityManager().createQuery(builder.toString());
+ qry.setParameter("originalEntityId", originalEntityId);
+
+ @SuppressWarnings("unchecked")
+ List<Map<String, Object>> resultList = qry.getResultList();
+ return resultList;
+ }
+
+ private void verifyRevEndTimeStamps(String debugInfo,
+ List<Map<String, Object>> revisionEntities) {
+ for (Map<String, Object> revisionEntity : revisionEntities) {
+
+ Date revendTimestamp = (Date) revisionEntity
+ .get(revendTimestampColumName);
+ CustomDateRevEntity revEnd = (CustomDateRevEntity) revisionEntity
+ .get("REVEND");
+
+ if (revendTimestamp == null) {
+ assert revEnd == null;
+ } else {
+ assert revendTimestamp.getTime() == revEnd.getDateTimestamp().getTime();
+ }
+ }
+ }
+
+}
Deleted: core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/ValidityAuditStrategyRevEndTsTest.java
===================================================================
--- core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/ValidityAuditStrategyRevEndTsTest.java 2010-09-28 22:24:09 UTC (rev 20744)
+++ core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/ValidityAuditStrategyRevEndTsTest.java 2010-10-04 18:11:33 UTC (rev 20775)
@@ -1,432 +0,0 @@
-/*
- * Hibernate, Relational Persistence for Idiomatic Java
- *
- * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
- * indicated by the @author tags or express copyright attribution
- * statements applied by the authors. All third-party contributions are
- * distributed under license by Red Hat Middleware LLC.
- *
- * This copyrighted material is made available to anyone wishing to use, modify,
- * copy, or redistribute it subject to the terms and conditions of the GNU
- * Lesser General Public License, as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
- * for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this distribution; if not, write to:
- * Free Software Foundation, Inc.
- * 51 Franklin Street, Fifth Floor
- * Boston, MA 02110-1301 USA
- */
-package org.hibernate.envers.test.integration.strategy;
-
-import java.util.Arrays;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import javax.persistence.EntityManager;
-import javax.persistence.Query;
-
-import org.hibernate.Session;
-import org.hibernate.ejb.Ejb3Configuration;
-import org.hibernate.envers.DefaultRevisionEntity;
-import org.hibernate.envers.strategy.ValidityAuditStrategy;
-import org.hibernate.envers.test.AbstractEntityTest;
-import org.hibernate.envers.test.entities.manytomany.sametable.Child1Entity;
-import org.hibernate.envers.test.entities.manytomany.sametable.Child2Entity;
-import org.hibernate.envers.test.entities.manytomany.sametable.ParentEntity;
-import org.hibernate.envers.test.tools.TestTools;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
-
-/**
- * Test which checks that the revision end timestamp is correctly set for
- * {@link ValidityAuditStrategy}.
- *
- * @author Erik-Berndt Scheper
- */
-public class ValidityAuditStrategyRevEndTsTest extends AbstractEntityTest {
- private final String revendTimestampColumName = "REVEND_TIMESTAMP";
-
- private Integer p1_id;
- private Integer p2_id;
- private Integer c1_1_id;
- private Integer c1_2_id;
- private Integer c2_1_id;
- private Integer c2_2_id;
- private Map<Number, DefaultRevisionEntity> revisions;
-
- public void configure(Ejb3Configuration cfg) {
- cfg.addAnnotatedClass(ParentEntity.class);
- cfg.addAnnotatedClass(Child1Entity.class);
- cfg.addAnnotatedClass(Child2Entity.class);
-
- cfg.setProperty("org.hibernate.envers.audit_strategy",
- "org.hibernate.envers.strategy.ValidityAuditStrategy");
- cfg
- .setProperty(
- "org.hibernate.envers.audit_strategy_validity_store_revend_timestamp",
- "true");
- cfg
- .setProperty(
- "org.hibernate.envers.audit_strategy_validity_revend_timestamp_field_name",
- revendTimestampColumName);
- }
-
- @BeforeClass(enabled = true, dependsOnMethods = "init")
- public void initData() {
- EntityManager em = getEntityManager();
-
- // We need first to modify the columns in the middle (join table) to
- // allow null values. Hbm2ddl doesn't seem
- // to allow this.
- em.getTransaction().begin();
- Session session = (Session) em.getDelegate();
- session.createSQLQuery("DROP TABLE children").executeUpdate();
- session
- .createSQLQuery(
- "CREATE TABLE children(parent_id integer, child1_id integer NULL, child2_id integer NULL)")
- .executeUpdate();
- session.createSQLQuery("DROP TABLE children_AUD").executeUpdate();
- session
- .createSQLQuery(
- "CREATE TABLE children_AUD(REV integer NOT NULL, REVEND integer, "
- + revendTimestampColumName
- + " timestamp, REVTYPE tinyint, "
- + "parent_id integer, child1_id integer NULL, child2_id integer NULL)")
- .executeUpdate();
- em.getTransaction().commit();
- em.clear();
-
- ParentEntity p1 = new ParentEntity("parent_1");
- ParentEntity p2 = new ParentEntity("parent_2");
-
- Child1Entity c1_1 = new Child1Entity("child1_1");
- Child1Entity c1_2 = new Child1Entity("child1_2");
-
- Child2Entity c2_1 = new Child2Entity("child2_1");
- Child2Entity c2_2 = new Child2Entity("child2_2");
-
- // Revision 1
- em.getTransaction().begin();
-
- em.persist(p1);
- em.persist(p2);
- em.persist(c1_1);
- em.persist(c1_2);
- em.persist(c2_1);
- em.persist(c2_2);
-
- em.getTransaction().commit();
- em.clear();
-
- // Revision 2 - (p1: c1_1, p2: c2_1)
-
- em.getTransaction().begin();
-
- p1 = em.find(ParentEntity.class, p1.getId());
- p2 = em.find(ParentEntity.class, p2.getId());
- c1_1 = em.find(Child1Entity.class, c1_1.getId());
- c2_1 = em.find(Child2Entity.class, c2_1.getId());
-
- p1.getChildren1().add(c1_1);
- p2.getChildren2().add(c2_1);
-
- em.getTransaction().commit();
- em.clear();
-
- // Revision 3 - (p1: c1_1, c1_2, c2_2, p2: c1_1, c2_1)
- em.getTransaction().begin();
-
- p1 = em.find(ParentEntity.class, p1.getId());
- p2 = em.find(ParentEntity.class, p2.getId());
- c1_1 = em.find(Child1Entity.class, c1_1.getId());
- c1_2 = em.find(Child1Entity.class, c1_2.getId());
- c2_2 = em.find(Child2Entity.class, c2_2.getId());
-
- p1.getChildren1().add(c1_2);
- p1.getChildren2().add(c2_2);
-
- p2.getChildren1().add(c1_1);
-
- em.getTransaction().commit();
- em.clear();
-
- // Revision 4 - (p1: c1_2, c2_2, p2: c1_1, c2_1, c2_2)
- em.getTransaction().begin();
-
- p1 = em.find(ParentEntity.class, p1.getId());
- p2 = em.find(ParentEntity.class, p2.getId());
- c1_1 = em.find(Child1Entity.class, c1_1.getId());
- c2_2 = em.find(Child2Entity.class, c2_2.getId());
-
- p1.getChildren1().remove(c1_1);
- p2.getChildren2().add(c2_2);
-
- em.getTransaction().commit();
- em.clear();
-
- // Revision 5 - (p1: c2_2, p2: c1_1, c2_1)
- em.getTransaction().begin();
-
- p1 = em.find(ParentEntity.class, p1.getId());
- p2 = em.find(ParentEntity.class, p2.getId());
- c1_2 = em.find(Child1Entity.class, c1_2.getId());
- c2_2 = em.find(Child2Entity.class, c2_2.getId());
-
- c2_2.getParents().remove(p2);
- c1_2.getParents().remove(p1);
-
- em.getTransaction().commit();
- em.clear();
-
- //
-
- p1_id = p1.getId();
- p2_id = p2.getId();
- c1_1_id = c1_1.getId();
- c1_2_id = c1_2.getId();
- c2_1_id = c2_1.getId();
- c2_2_id = c2_2.getId();
-
- Set<Number> revisionNumbers = new HashSet<Number>();
- revisionNumbers.addAll(Arrays.asList(1, 2, 3, 4, 5));
- revisions = getAuditReader().findRevisions(DefaultRevisionEntity.class,
- revisionNumbers);
-
- assert revisions.size() == 5;
- }
-
- @Test(enabled = true)
- public void testRevisionsCounts() {
- assert Arrays.asList(1, 2, 3, 4).equals(
- getAuditReader().getRevisions(ParentEntity.class, p1_id));
- assert Arrays.asList(1, 2, 3, 4).equals(
- getAuditReader().getRevisions(ParentEntity.class, p2_id));
-
- assert Arrays.asList(1).equals(
- getAuditReader().getRevisions(Child1Entity.class, c1_1_id));
- assert Arrays.asList(1, 5).equals(
- getAuditReader().getRevisions(Child1Entity.class, c1_2_id));
-
- assert Arrays.asList(1).equals(
- getAuditReader().getRevisions(Child2Entity.class, c2_1_id));
- assert Arrays.asList(1, 5).equals(
- getAuditReader().getRevisions(Child2Entity.class, c2_2_id));
- }
-
- @Test(enabled = true)
- public void testAllRevEndTimeStamps() {
- List<Map<String, Object>> p1RevList = getRevisions(ParentEntity.class,
- p1_id);
- List<Map<String, Object>> p2RevList = getRevisions(ParentEntity.class,
- p2_id);
- List<Map<String, Object>> c1_1_List = getRevisions(Child1Entity.class,
- c1_1_id);
- List<Map<String, Object>> c1_2_List = getRevisions(Child1Entity.class,
- c1_2_id);
- List<Map<String, Object>> c2_1_List = getRevisions(Child2Entity.class,
- c2_1_id);
- List<Map<String, Object>> c2_2_List = getRevisions(Child2Entity.class,
- c2_2_id);
-
- verifyRevEndTimeStamps("ParentEntity: " + p1_id, p1RevList);
- verifyRevEndTimeStamps("ParentEntity: " + p2_id, p2RevList);
- verifyRevEndTimeStamps("Child1Entity: " + c1_1_id, c1_1_List);
- verifyRevEndTimeStamps("Child1Entity: " + c1_2_id, c1_2_List);
- verifyRevEndTimeStamps("Child2Entity: " + c2_1_id, c2_1_List);
- verifyRevEndTimeStamps("Child2Entity: " + c2_2_id, c2_2_List);
-
- }
-
- @Test(enabled = true)
- public void testHistoryOfParent1() {
-
- Child1Entity c1_1 = getEntityManager()
- .find(Child1Entity.class, c1_1_id);
- Child1Entity c1_2 = getEntityManager()
- .find(Child1Entity.class, c1_2_id);
- Child2Entity c2_2 = getEntityManager()
- .find(Child2Entity.class, c2_2_id);
-
- ParentEntity rev1 = getAuditReader().find(ParentEntity.class, p1_id, 1);
- ParentEntity rev2 = getAuditReader().find(ParentEntity.class, p1_id, 2);
- ParentEntity rev3 = getAuditReader().find(ParentEntity.class, p1_id, 3);
- ParentEntity rev4 = getAuditReader().find(ParentEntity.class, p1_id, 4);
- ParentEntity rev5 = getAuditReader().find(ParentEntity.class, p1_id, 5);
-
- assert TestTools.checkList(rev1.getChildren1());
- assert TestTools.checkList(rev2.getChildren1(), c1_1);
- assert TestTools.checkList(rev3.getChildren1(), c1_1, c1_2);
- assert TestTools.checkList(rev4.getChildren1(), c1_2);
- assert TestTools.checkList(rev5.getChildren1());
-
- assert TestTools.checkList(rev1.getChildren2());
- assert TestTools.checkList(rev2.getChildren2());
- assert TestTools.checkList(rev3.getChildren2(), c2_2);
- assert TestTools.checkList(rev4.getChildren2(), c2_2);
- assert TestTools.checkList(rev5.getChildren2(), c2_2);
- }
-
- @Test(enabled = true)
- public void testHistoryOfParent2() {
- Child1Entity c1_1 = getEntityManager()
- .find(Child1Entity.class, c1_1_id);
- Child2Entity c2_1 = getEntityManager()
- .find(Child2Entity.class, c2_1_id);
- Child2Entity c2_2 = getEntityManager()
- .find(Child2Entity.class, c2_2_id);
-
- ParentEntity rev1 = getAuditReader().find(ParentEntity.class, p2_id, 1);
- ParentEntity rev2 = getAuditReader().find(ParentEntity.class, p2_id, 2);
- ParentEntity rev3 = getAuditReader().find(ParentEntity.class, p2_id, 3);
- ParentEntity rev4 = getAuditReader().find(ParentEntity.class, p2_id, 4);
- ParentEntity rev5 = getAuditReader().find(ParentEntity.class, p2_id, 5);
-
- assert TestTools.checkList(rev1.getChildren1());
- assert TestTools.checkList(rev2.getChildren1());
- assert TestTools.checkList(rev3.getChildren1(), c1_1);
- assert TestTools.checkList(rev4.getChildren1(), c1_1);
- assert TestTools.checkList(rev5.getChildren1(), c1_1);
-
- assert TestTools.checkList(rev1.getChildren2());
- assert TestTools.checkList(rev2.getChildren2(), c2_1);
- assert TestTools.checkList(rev3.getChildren2(), c2_1);
- assert TestTools.checkList(rev4.getChildren2(), c2_1, c2_2);
- assert TestTools.checkList(rev5.getChildren2(), c2_1);
- }
-
- @Test(enabled = true)
- public void testHistoryOfChild1_1() {
- ParentEntity p1 = getEntityManager().find(ParentEntity.class, p1_id);
- ParentEntity p2 = getEntityManager().find(ParentEntity.class, p2_id);
-
- Child1Entity rev1 = getAuditReader().find(Child1Entity.class, c1_1_id,
- 1);
- Child1Entity rev2 = getAuditReader().find(Child1Entity.class, c1_1_id,
- 2);
- Child1Entity rev3 = getAuditReader().find(Child1Entity.class, c1_1_id,
- 3);
- Child1Entity rev4 = getAuditReader().find(Child1Entity.class, c1_1_id,
- 4);
- Child1Entity rev5 = getAuditReader().find(Child1Entity.class, c1_1_id,
- 5);
-
- assert TestTools.checkList(rev1.getParents());
- assert TestTools.checkList(rev2.getParents(), p1);
- assert TestTools.checkList(rev3.getParents(), p1, p2);
- assert TestTools.checkList(rev4.getParents(), p2);
- assert TestTools.checkList(rev5.getParents(), p2);
- }
-
- @Test(enabled = false)
- public void testHistoryOfChild1_2() {
- ParentEntity p1 = getEntityManager().find(ParentEntity.class, p1_id);
-
- Child1Entity rev1 = getAuditReader().find(Child1Entity.class, c1_2_id,
- 1);
- Child1Entity rev2 = getAuditReader().find(Child1Entity.class, c1_2_id,
- 2);
- Child1Entity rev3 = getAuditReader().find(Child1Entity.class, c1_2_id,
- 3);
- Child1Entity rev4 = getAuditReader().find(Child1Entity.class, c1_2_id,
- 4);
- Child1Entity rev5 = getAuditReader().find(Child1Entity.class, c1_2_id,
- 5);
-
- assert TestTools.checkList(rev1.getParents());
- assert TestTools.checkList(rev2.getParents());
- assert TestTools.checkList(rev3.getParents(), p1);
- assert TestTools.checkList(rev4.getParents(), p1);
- assert TestTools.checkList(rev5.getParents());
- }
-
- @Test(enabled = true)
- public void testHistoryOfChild2_1() {
- ParentEntity p2 = getEntityManager().find(ParentEntity.class, p2_id);
-
- Child2Entity rev1 = getAuditReader().find(Child2Entity.class, c2_1_id,
- 1);
- Child2Entity rev2 = getAuditReader().find(Child2Entity.class, c2_1_id,
- 2);
- Child2Entity rev3 = getAuditReader().find(Child2Entity.class, c2_1_id,
- 3);
- Child2Entity rev4 = getAuditReader().find(Child2Entity.class, c2_1_id,
- 4);
- Child2Entity rev5 = getAuditReader().find(Child2Entity.class, c2_1_id,
- 5);
-
- assert TestTools.checkList(rev1.getParents());
- assert TestTools.checkList(rev2.getParents(), p2);
- assert TestTools.checkList(rev3.getParents(), p2);
- assert TestTools.checkList(rev4.getParents(), p2);
- assert TestTools.checkList(rev5.getParents(), p2);
- }
-
- @Test(enabled = true)
- public void testHistoryOfChild2_2() {
- ParentEntity p1 = getEntityManager().find(ParentEntity.class, p1_id);
- ParentEntity p2 = getEntityManager().find(ParentEntity.class, p2_id);
-
- Child2Entity rev1 = getAuditReader().find(Child2Entity.class, c2_2_id,
- 1);
- Child2Entity rev2 = getAuditReader().find(Child2Entity.class, c2_2_id,
- 2);
- Child2Entity rev3 = getAuditReader().find(Child2Entity.class, c2_2_id,
- 3);
- Child2Entity rev4 = getAuditReader().find(Child2Entity.class, c2_2_id,
- 4);
- Child2Entity rev5 = getAuditReader().find(Child2Entity.class, c2_2_id,
- 5);
-
- assert TestTools.checkList(rev1.getParents());
- assert TestTools.checkList(rev2.getParents());
- assert TestTools.checkList(rev3.getParents(), p1);
- assert TestTools.checkList(rev4.getParents(), p1, p2);
- assert TestTools.checkList(rev5.getParents(), p1);
- }
-
- private List<Map<String, Object>> getRevisions(
- Class<?> originalEntityClazz, Integer originalEntityId) {
- // Build the query:
- // select auditEntity from
- // org.hibernate.envers.test.entities.manytomany.sametable.ParentEntity_AUD
- // auditEntity where auditEntity.originalId.id = :originalEntityId
-
- StringBuilder builder = new StringBuilder("select auditEntity from ");
- builder.append(originalEntityClazz.getName())
- .append("_AUD auditEntity");
- builder.append(" where auditEntity.originalId.id = :originalEntityId");
-
- Query qry = getEntityManager().createQuery(builder.toString());
- qry.setParameter("originalEntityId", originalEntityId);
-
- @SuppressWarnings("unchecked")
- List<Map<String, Object>> resultList = qry.getResultList();
- return resultList;
- }
-
- private void verifyRevEndTimeStamps(String debugInfo,
- List<Map<String, Object>> revisionEntities) {
- for (Map<String, Object> revisionEntity : revisionEntities) {
-
- Date revendTimestamp = (Date) revisionEntity
- .get(revendTimestampColumName);
- DefaultRevisionEntity revEnd = (DefaultRevisionEntity) revisionEntity
- .get("REVEND");
-
- if (revendTimestamp == null) {
- assert revEnd == null;
- } else {
- assert revendTimestamp.getTime() == revEnd.getTimestamp();
- }
- }
- }
-
-}
Copied: core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/ValidityAuditStrategyRevEndTsTest.java (from rev 20744, core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/ValidityAuditStrategyRevEndTsTest.java)
===================================================================
--- core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/ValidityAuditStrategyRevEndTsTest.java (rev 0)
+++ core/trunk/envers/src/test/java/org/hibernate/envers/test/integration/strategy/ValidityAuditStrategyRevEndTsTest.java 2010-10-04 18:11:33 UTC (rev 20775)
@@ -0,0 +1,432 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors. All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301 USA
+ */
+package org.hibernate.envers.test.integration.strategy;
+
+import java.util.Arrays;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import org.hibernate.Session;
+import org.hibernate.ejb.Ejb3Configuration;
+import org.hibernate.envers.DefaultRevisionEntity;
+import org.hibernate.envers.strategy.ValidityAuditStrategy;
+import org.hibernate.envers.test.AbstractEntityTest;
+import org.hibernate.envers.test.entities.manytomany.sametable.Child1Entity;
+import org.hibernate.envers.test.entities.manytomany.sametable.Child2Entity;
+import org.hibernate.envers.test.entities.manytomany.sametable.ParentEntity;
+import org.hibernate.envers.test.tools.TestTools;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+/**
+ * Test which checks that the revision end timestamp is correctly set for
+ * {@link ValidityAuditStrategy}.
+ *
+ * @author Erik-Berndt Scheper
+ */
+public class ValidityAuditStrategyRevEndTsTest extends AbstractEntityTest {
+ private final String revendTimestampColumName = "REVEND_TIMESTAMP";
+
+ private Integer p1_id;
+ private Integer p2_id;
+ private Integer c1_1_id;
+ private Integer c1_2_id;
+ private Integer c2_1_id;
+ private Integer c2_2_id;
+ private Map<Number, DefaultRevisionEntity> revisions;
+
+ public void configure(Ejb3Configuration cfg) {
+ cfg.addAnnotatedClass(ParentEntity.class);
+ cfg.addAnnotatedClass(Child1Entity.class);
+ cfg.addAnnotatedClass(Child2Entity.class);
+
+ cfg.setProperty("org.hibernate.envers.audit_strategy",
+ "org.hibernate.envers.strategy.ValidityAuditStrategy");
+ cfg
+ .setProperty(
+ "org.hibernate.envers.audit_strategy_validity_store_revend_timestamp",
+ "true");
+ cfg
+ .setProperty(
+ "org.hibernate.envers.audit_strategy_validity_revend_timestamp_field_name",
+ revendTimestampColumName);
+ }
+
+ @BeforeClass(enabled = true, dependsOnMethods = "init")
+ public void initData() {
+ EntityManager em = getEntityManager();
+
+ // We need first to modify the columns in the middle (join table) to
+ // allow null values. Hbm2ddl doesn't seem
+ // to allow this.
+ em.getTransaction().begin();
+ Session session = (Session) em.getDelegate();
+ session.createSQLQuery("DROP TABLE children").executeUpdate();
+ session
+ .createSQLQuery(
+ "CREATE TABLE children(parent_id integer, child1_id integer NULL, child2_id integer NULL)")
+ .executeUpdate();
+ session.createSQLQuery("DROP TABLE children_AUD").executeUpdate();
+ session
+ .createSQLQuery(
+ "CREATE TABLE children_AUD(REV integer NOT NULL, REVEND integer, "
+ + revendTimestampColumName
+ + " timestamp, REVTYPE tinyint, "
+ + "parent_id integer, child1_id integer NULL, child2_id integer NULL)")
+ .executeUpdate();
+ em.getTransaction().commit();
+ em.clear();
+
+ ParentEntity p1 = new ParentEntity("parent_1");
+ ParentEntity p2 = new ParentEntity("parent_2");
+
+ Child1Entity c1_1 = new Child1Entity("child1_1");
+ Child1Entity c1_2 = new Child1Entity("child1_2");
+
+ Child2Entity c2_1 = new Child2Entity("child2_1");
+ Child2Entity c2_2 = new Child2Entity("child2_2");
+
+ // Revision 1
+ em.getTransaction().begin();
+
+ em.persist(p1);
+ em.persist(p2);
+ em.persist(c1_1);
+ em.persist(c1_2);
+ em.persist(c2_1);
+ em.persist(c2_2);
+
+ em.getTransaction().commit();
+ em.clear();
+
+ // Revision 2 - (p1: c1_1, p2: c2_1)
+
+ em.getTransaction().begin();
+
+ p1 = em.find(ParentEntity.class, p1.getId());
+ p2 = em.find(ParentEntity.class, p2.getId());
+ c1_1 = em.find(Child1Entity.class, c1_1.getId());
+ c2_1 = em.find(Child2Entity.class, c2_1.getId());
+
+ p1.getChildren1().add(c1_1);
+ p2.getChildren2().add(c2_1);
+
+ em.getTransaction().commit();
+ em.clear();
+
+ // Revision 3 - (p1: c1_1, c1_2, c2_2, p2: c1_1, c2_1)
+ em.getTransaction().begin();
+
+ p1 = em.find(ParentEntity.class, p1.getId());
+ p2 = em.find(ParentEntity.class, p2.getId());
+ c1_1 = em.find(Child1Entity.class, c1_1.getId());
+ c1_2 = em.find(Child1Entity.class, c1_2.getId());
+ c2_2 = em.find(Child2Entity.class, c2_2.getId());
+
+ p1.getChildren1().add(c1_2);
+ p1.getChildren2().add(c2_2);
+
+ p2.getChildren1().add(c1_1);
+
+ em.getTransaction().commit();
+ em.clear();
+
+ // Revision 4 - (p1: c1_2, c2_2, p2: c1_1, c2_1, c2_2)
+ em.getTransaction().begin();
+
+ p1 = em.find(ParentEntity.class, p1.getId());
+ p2 = em.find(ParentEntity.class, p2.getId());
+ c1_1 = em.find(Child1Entity.class, c1_1.getId());
+ c2_2 = em.find(Child2Entity.class, c2_2.getId());
+
+ p1.getChildren1().remove(c1_1);
+ p2.getChildren2().add(c2_2);
+
+ em.getTransaction().commit();
+ em.clear();
+
+ // Revision 5 - (p1: c2_2, p2: c1_1, c2_1)
+ em.getTransaction().begin();
+
+ p1 = em.find(ParentEntity.class, p1.getId());
+ p2 = em.find(ParentEntity.class, p2.getId());
+ c1_2 = em.find(Child1Entity.class, c1_2.getId());
+ c2_2 = em.find(Child2Entity.class, c2_2.getId());
+
+ c2_2.getParents().remove(p2);
+ c1_2.getParents().remove(p1);
+
+ em.getTransaction().commit();
+ em.clear();
+
+ //
+
+ p1_id = p1.getId();
+ p2_id = p2.getId();
+ c1_1_id = c1_1.getId();
+ c1_2_id = c1_2.getId();
+ c2_1_id = c2_1.getId();
+ c2_2_id = c2_2.getId();
+
+ Set<Number> revisionNumbers = new HashSet<Number>();
+ revisionNumbers.addAll(Arrays.asList(1, 2, 3, 4, 5));
+ revisions = getAuditReader().findRevisions(DefaultRevisionEntity.class,
+ revisionNumbers);
+
+ assert revisions.size() == 5;
+ }
+
+ @Test(enabled = true)
+ public void testRevisionsCounts() {
+ assert Arrays.asList(1, 2, 3, 4).equals(
+ getAuditReader().getRevisions(ParentEntity.class, p1_id));
+ assert Arrays.asList(1, 2, 3, 4).equals(
+ getAuditReader().getRevisions(ParentEntity.class, p2_id));
+
+ assert Arrays.asList(1).equals(
+ getAuditReader().getRevisions(Child1Entity.class, c1_1_id));
+ assert Arrays.asList(1, 5).equals(
+ getAuditReader().getRevisions(Child1Entity.class, c1_2_id));
+
+ assert Arrays.asList(1).equals(
+ getAuditReader().getRevisions(Child2Entity.class, c2_1_id));
+ assert Arrays.asList(1, 5).equals(
+ getAuditReader().getRevisions(Child2Entity.class, c2_2_id));
+ }
+
+ @Test(enabled = true)
+ public void testAllRevEndTimeStamps() {
+ List<Map<String, Object>> p1RevList = getRevisions(ParentEntity.class,
+ p1_id);
+ List<Map<String, Object>> p2RevList = getRevisions(ParentEntity.class,
+ p2_id);
+ List<Map<String, Object>> c1_1_List = getRevisions(Child1Entity.class,
+ c1_1_id);
+ List<Map<String, Object>> c1_2_List = getRevisions(Child1Entity.class,
+ c1_2_id);
+ List<Map<String, Object>> c2_1_List = getRevisions(Child2Entity.class,
+ c2_1_id);
+ List<Map<String, Object>> c2_2_List = getRevisions(Child2Entity.class,
+ c2_2_id);
+
+ verifyRevEndTimeStamps("ParentEntity: " + p1_id, p1RevList);
+ verifyRevEndTimeStamps("ParentEntity: " + p2_id, p2RevList);
+ verifyRevEndTimeStamps("Child1Entity: " + c1_1_id, c1_1_List);
+ verifyRevEndTimeStamps("Child1Entity: " + c1_2_id, c1_2_List);
+ verifyRevEndTimeStamps("Child2Entity: " + c2_1_id, c2_1_List);
+ verifyRevEndTimeStamps("Child2Entity: " + c2_2_id, c2_2_List);
+
+ }
+
+ @Test(enabled = true)
+ public void testHistoryOfParent1() {
+
+ Child1Entity c1_1 = getEntityManager()
+ .find(Child1Entity.class, c1_1_id);
+ Child1Entity c1_2 = getEntityManager()
+ .find(Child1Entity.class, c1_2_id);
+ Child2Entity c2_2 = getEntityManager()
+ .find(Child2Entity.class, c2_2_id);
+
+ ParentEntity rev1 = getAuditReader().find(ParentEntity.class, p1_id, 1);
+ ParentEntity rev2 = getAuditReader().find(ParentEntity.class, p1_id, 2);
+ ParentEntity rev3 = getAuditReader().find(ParentEntity.class, p1_id, 3);
+ ParentEntity rev4 = getAuditReader().find(ParentEntity.class, p1_id, 4);
+ ParentEntity rev5 = getAuditReader().find(ParentEntity.class, p1_id, 5);
+
+ assert TestTools.checkList(rev1.getChildren1());
+ assert TestTools.checkList(rev2.getChildren1(), c1_1);
+ assert TestTools.checkList(rev3.getChildren1(), c1_1, c1_2);
+ assert TestTools.checkList(rev4.getChildren1(), c1_2);
+ assert TestTools.checkList(rev5.getChildren1());
+
+ assert TestTools.checkList(rev1.getChildren2());
+ assert TestTools.checkList(rev2.getChildren2());
+ assert TestTools.checkList(rev3.getChildren2(), c2_2);
+ assert TestTools.checkList(rev4.getChildren2(), c2_2);
+ assert TestTools.checkList(rev5.getChildren2(), c2_2);
+ }
+
+ @Test(enabled = true)
+ public void testHistoryOfParent2() {
+ Child1Entity c1_1 = getEntityManager()
+ .find(Child1Entity.class, c1_1_id);
+ Child2Entity c2_1 = getEntityManager()
+ .find(Child2Entity.class, c2_1_id);
+ Child2Entity c2_2 = getEntityManager()
+ .find(Child2Entity.class, c2_2_id);
+
+ ParentEntity rev1 = getAuditReader().find(ParentEntity.class, p2_id, 1);
+ ParentEntity rev2 = getAuditReader().find(ParentEntity.class, p2_id, 2);
+ ParentEntity rev3 = getAuditReader().find(ParentEntity.class, p2_id, 3);
+ ParentEntity rev4 = getAuditReader().find(ParentEntity.class, p2_id, 4);
+ ParentEntity rev5 = getAuditReader().find(ParentEntity.class, p2_id, 5);
+
+ assert TestTools.checkList(rev1.getChildren1());
+ assert TestTools.checkList(rev2.getChildren1());
+ assert TestTools.checkList(rev3.getChildren1(), c1_1);
+ assert TestTools.checkList(rev4.getChildren1(), c1_1);
+ assert TestTools.checkList(rev5.getChildren1(), c1_1);
+
+ assert TestTools.checkList(rev1.getChildren2());
+ assert TestTools.checkList(rev2.getChildren2(), c2_1);
+ assert TestTools.checkList(rev3.getChildren2(), c2_1);
+ assert TestTools.checkList(rev4.getChildren2(), c2_1, c2_2);
+ assert TestTools.checkList(rev5.getChildren2(), c2_1);
+ }
+
+ @Test(enabled = true)
+ public void testHistoryOfChild1_1() {
+ ParentEntity p1 = getEntityManager().find(ParentEntity.class, p1_id);
+ ParentEntity p2 = getEntityManager().find(ParentEntity.class, p2_id);
+
+ Child1Entity rev1 = getAuditReader().find(Child1Entity.class, c1_1_id,
+ 1);
+ Child1Entity rev2 = getAuditReader().find(Child1Entity.class, c1_1_id,
+ 2);
+ Child1Entity rev3 = getAuditReader().find(Child1Entity.class, c1_1_id,
+ 3);
+ Child1Entity rev4 = getAuditReader().find(Child1Entity.class, c1_1_id,
+ 4);
+ Child1Entity rev5 = getAuditReader().find(Child1Entity.class, c1_1_id,
+ 5);
+
+ assert TestTools.checkList(rev1.getParents());
+ assert TestTools.checkList(rev2.getParents(), p1);
+ assert TestTools.checkList(rev3.getParents(), p1, p2);
+ assert TestTools.checkList(rev4.getParents(), p2);
+ assert TestTools.checkList(rev5.getParents(), p2);
+ }
+
+ @Test(enabled = false)
+ public void testHistoryOfChild1_2() {
+ ParentEntity p1 = getEntityManager().find(ParentEntity.class, p1_id);
+
+ Child1Entity rev1 = getAuditReader().find(Child1Entity.class, c1_2_id,
+ 1);
+ Child1Entity rev2 = getAuditReader().find(Child1Entity.class, c1_2_id,
+ 2);
+ Child1Entity rev3 = getAuditReader().find(Child1Entity.class, c1_2_id,
+ 3);
+ Child1Entity rev4 = getAuditReader().find(Child1Entity.class, c1_2_id,
+ 4);
+ Child1Entity rev5 = getAuditReader().find(Child1Entity.class, c1_2_id,
+ 5);
+
+ assert TestTools.checkList(rev1.getParents());
+ assert TestTools.checkList(rev2.getParents());
+ assert TestTools.checkList(rev3.getParents(), p1);
+ assert TestTools.checkList(rev4.getParents(), p1);
+ assert TestTools.checkList(rev5.getParents());
+ }
+
+ @Test(enabled = true)
+ public void testHistoryOfChild2_1() {
+ ParentEntity p2 = getEntityManager().find(ParentEntity.class, p2_id);
+
+ Child2Entity rev1 = getAuditReader().find(Child2Entity.class, c2_1_id,
+ 1);
+ Child2Entity rev2 = getAuditReader().find(Child2Entity.class, c2_1_id,
+ 2);
+ Child2Entity rev3 = getAuditReader().find(Child2Entity.class, c2_1_id,
+ 3);
+ Child2Entity rev4 = getAuditReader().find(Child2Entity.class, c2_1_id,
+ 4);
+ Child2Entity rev5 = getAuditReader().find(Child2Entity.class, c2_1_id,
+ 5);
+
+ assert TestTools.checkList(rev1.getParents());
+ assert TestTools.checkList(rev2.getParents(), p2);
+ assert TestTools.checkList(rev3.getParents(), p2);
+ assert TestTools.checkList(rev4.getParents(), p2);
+ assert TestTools.checkList(rev5.getParents(), p2);
+ }
+
+ @Test(enabled = true)
+ public void testHistoryOfChild2_2() {
+ ParentEntity p1 = getEntityManager().find(ParentEntity.class, p1_id);
+ ParentEntity p2 = getEntityManager().find(ParentEntity.class, p2_id);
+
+ Child2Entity rev1 = getAuditReader().find(Child2Entity.class, c2_2_id,
+ 1);
+ Child2Entity rev2 = getAuditReader().find(Child2Entity.class, c2_2_id,
+ 2);
+ Child2Entity rev3 = getAuditReader().find(Child2Entity.class, c2_2_id,
+ 3);
+ Child2Entity rev4 = getAuditReader().find(Child2Entity.class, c2_2_id,
+ 4);
+ Child2Entity rev5 = getAuditReader().find(Child2Entity.class, c2_2_id,
+ 5);
+
+ assert TestTools.checkList(rev1.getParents());
+ assert TestTools.checkList(rev2.getParents());
+ assert TestTools.checkList(rev3.getParents(), p1);
+ assert TestTools.checkList(rev4.getParents(), p1, p2);
+ assert TestTools.checkList(rev5.getParents(), p1);
+ }
+
+ private List<Map<String, Object>> getRevisions(
+ Class<?> originalEntityClazz, Integer originalEntityId) {
+ // Build the query:
+ // select auditEntity from
+ // org.hibernate.envers.test.entities.manytomany.sametable.ParentEntity_AUD
+ // auditEntity where auditEntity.originalId.id = :originalEntityId
+
+ StringBuilder builder = new StringBuilder("select auditEntity from ");
+ builder.append(originalEntityClazz.getName())
+ .append("_AUD auditEntity");
+ builder.append(" where auditEntity.originalId.id = :originalEntityId");
+
+ Query qry = getEntityManager().createQuery(builder.toString());
+ qry.setParameter("originalEntityId", originalEntityId);
+
+ @SuppressWarnings("unchecked")
+ List<Map<String, Object>> resultList = qry.getResultList();
+ return resultList;
+ }
+
+ private void verifyRevEndTimeStamps(String debugInfo,
+ List<Map<String, Object>> revisionEntities) {
+ for (Map<String, Object> revisionEntity : revisionEntities) {
+
+ Date revendTimestamp = (Date) revisionEntity
+ .get(revendTimestampColumName);
+ DefaultRevisionEntity revEnd = (DefaultRevisionEntity) revisionEntity
+ .get("REVEND");
+
+ if (revendTimestamp == null) {
+ assert revEnd == null;
+ } else {
+ assert revendTimestamp.getTime() == revEnd.getTimestamp();
+ }
+ }
+ }
+
+}
Modified: core/trunk/envers/src/test/resources/testng.xml
===================================================================
--- core/trunk/envers/src/test/resources/testng.xml 2010-10-04 14:52:19 UTC (rev 20774)
+++ core/trunk/envers/src/test/resources/testng.xml 2010-10-04 18:11:33 UTC (rev 20775)
@@ -68,6 +68,7 @@
<package name="org.hibernate.envers.test.integration.secondary" />
<package name="org.hibernate.envers.test.integration.secondary.ids" />
<package name="org.hibernate.envers.test.integration.serialization" />
+ <package name="org.hibernate.envers.test.integration.strategy" />
<package name="org.hibernate.envers.test.integration.superclass" />
<package name="org.hibernate.envers.test.integration.entityNames.auditedEntity" />
<package name="org.hibernate.envers.test.integration.entityNames.manyToManyAudited" />
14 years, 1 month