METAGEN-3

This commit is contained in:
Hardy Ferentschik 2009-11-03 14:52:05 +00:00 committed by Strong Liu
parent 31901374fc
commit 95da451ce7
61 changed files with 730 additions and 437 deletions

View File

@ -46,7 +46,7 @@
<name>Emmanuel Bernard</name>
<email>emmanuel@hibernate.org</email>
<url>http://in.relation.to/Bloggers/Emmanuel</url>
</developer>
</developer>
<developer>
<id>hardy.ferentschik</id>
<name>Hardy Ferentschik</name>
@ -85,36 +85,6 @@
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>process_annotations</id>
<phase>process-test-resources</phase>
<configuration>
<tasks>
<property name="build.compiler" value="extJavac"/>
<property name="target.dir" value="${project.build.directory}/generated-src/jpamodelgen"/>
<mkdir dir="${target.dir}"/>
<javac srcdir="${project.build.testSourceDirectory}"
destdir="${target.dir}"
failonerror="false"
excludes="test/**">
<compilerarg value="-proc:only"/>
<classpath>
<path refid="maven.test.classpath"/>
</classpath>
</javac>
</tasks>
<testSourceRoot>${project.build.directory}/generated-src/jpamodelgen</testSourceRoot>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
@ -149,6 +119,16 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemProperties>
<property>
<name>sourceBaseDir</name>
<value>${basedir}/src/test/java</value>
</property>
<property>
<name>outBaseDir</name>
<value>${basedir}/target/test-classes</value>
</property>
</systemProperties>
<suiteXmlFiles>
<suiteXmlFile>${basedir}/src/test/suite/unit-tests.xml</suiteXmlFile>
</suiteXmlFiles>
@ -215,7 +195,7 @@
</goals>
</execution>
</executions>
</plugin>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>

View File

@ -24,7 +24,6 @@ import java.io.StringWriter;
import java.util.List;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.FilerException;
import javax.annotation.Generated;
import javax.tools.FileObject;
import javax.tools.Diagnostic;
import javax.lang.model.type.TypeMirror;
@ -38,7 +37,7 @@ import javax.lang.model.element.TypeElement;
*/
public class ClassWriter {
public static void writeFile(IMetaEntity entity, ProcessingEnvironment processingEnv, Context context) {
public static void writeFile(MetaEntity entity, ProcessingEnvironment processingEnv, Context context) {
try {
String metaModelPackage = entity.getPackageName();
@ -85,7 +84,7 @@ public class ClassWriter {
*
* @return body content
*/
private static StringBuffer generateBody(IMetaEntity entity, Context context) {
private static StringBuffer generateBody(MetaEntity entity, Context context) {
StringWriter sw = new StringWriter();
PrintWriter pw = null;
@ -103,9 +102,9 @@ public class ClassWriter {
pw.println();
List<IMetaAttribute> members = entity.getMembers();
List<MetaAttribute> members = entity.getMembers();
for ( IMetaAttribute metaMember : members ) {
for ( MetaAttribute metaMember : members ) {
pw.println( " " + metaMember.getDeclarationString() );
}
pw.println();
@ -119,7 +118,7 @@ public class ClassWriter {
}
}
private static void printClassDeclaration(IMetaEntity entity, PrintWriter pw, Context context) {
private static void printClassDeclaration(MetaEntity entity, PrintWriter pw, Context context) {
pw.print( "public abstract class " + entity.getSimpleName() + "_" );
final TypeMirror superClass = entity.getTypeElement().getSuperclass();

View File

@ -26,7 +26,7 @@ import javax.persistence.AccessType;
import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.Diagnostic;
import org.hibernate.jpamodelgen.annotation.MetaEntity;
import org.hibernate.jpamodelgen.annotation.AnnotationMetaEntity;
/**
* @author Max Andersen
@ -38,8 +38,8 @@ public class Context {
private Map<TypeElement, AccessTypeHolder> accessTypes = new HashMap<TypeElement, AccessTypeHolder>();
private Set<String> elementsAlreadyProcessed = new HashSet<String>();
private ProcessingEnvironment pe;
private final Map<String, IMetaEntity> metaEntitiesToProcess = new HashMap<String, IMetaEntity>();
private final Map<String, IMetaEntity> metaSuperclassAndEmbeddableToProcess = new HashMap<String, IMetaEntity>();
private final Map<String, MetaEntity> metaEntitiesToProcess = new HashMap<String, MetaEntity>();
private final Map<String, MetaEntity> metaSuperclassAndEmbeddableToProcess = new HashMap<String, MetaEntity>();
private static class AccessTypeHolder {
public AccessType elementAccessType;
@ -50,11 +50,11 @@ public class Context {
this.pe = pe;
}
public Map<String, IMetaEntity> getMetaEntitiesToProcess() {
public Map<String, MetaEntity> getMetaEntitiesToProcess() {
return metaEntitiesToProcess;
}
public Map<String, IMetaEntity> getMetaSuperclassAndEmbeddableToProcess() {
public Map<String, MetaEntity> getMetaSuperclassAndEmbeddableToProcess() {
return metaSuperclassAndEmbeddableToProcess;
}
@ -98,7 +98,7 @@ public class Context {
return;
}
ClassWriter.writeFile( new MetaEntity( pe, element, this, defaultAccessTypeForHierarchy ), pe, this );
ClassWriter.writeFile( new AnnotationMetaEntity( pe, element, this, defaultAccessTypeForHierarchy ), pe, this );
elementsAlreadyProcessed.add( element.getQualifiedName().toString() );
}
}

View File

@ -48,7 +48,7 @@ import javax.xml.validation.SchemaFactory;
import org.xml.sax.SAXException;
import org.hibernate.jpamodelgen.annotation.MetaEntity;
import org.hibernate.jpamodelgen.annotation.AnnotationMetaEntity;
import org.hibernate.jpamodelgen.xml.XmlMetaEntity;
import org.hibernate.jpamodelgen.xml.jaxb.Entity;
import org.hibernate.jpamodelgen.xml.jaxb.EntityMappings;
@ -125,7 +125,7 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
}
private void createMetaModelClasses() {
for ( IMetaEntity entity : context.getMetaEntitiesToProcess().values() ) {
for ( MetaEntity entity : context.getMetaEntitiesToProcess().values() ) {
processingEnv.getMessager()
.printMessage( Diagnostic.Kind.NOTE, "Writing meta model for " + entity );
ClassWriter.writeFile( entity, processingEnv, context );
@ -136,7 +136,7 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
context.getMetaSuperclassAndEmbeddableToProcess().remove( className );
}
for ( IMetaEntity entity : context.getMetaSuperclassAndEmbeddableToProcess().values() ) {
for ( MetaEntity entity : context.getMetaSuperclassAndEmbeddableToProcess().values() ) {
processingEnv.getMessager()
.printMessage( Diagnostic.Kind.NOTE, "Writing meta model for " + entity );
ClassWriter.writeFile( entity, processingEnv, context );
@ -325,13 +325,13 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
if ( element.getKind() == ElementKind.CLASS ) {
if ( annotationType.equals( ENTITY_ANN ) ) {
MetaEntity metaEntity = new MetaEntity( processingEnv, ( TypeElement ) element, context );
AnnotationMetaEntity metaEntity = new AnnotationMetaEntity( processingEnv, ( TypeElement ) element, context );
// TODO instead of just adding the entity we have to do some merging.
context.getMetaEntitiesToProcess().put( metaEntity.getQualifiedName(), metaEntity );
}
else if ( annotationType.equals( MAPPED_SUPERCLASS_ANN )
|| annotationType.equals( EMBEDDABLE_ANN ) ) {
MetaEntity metaEntity = new MetaEntity( processingEnv, ( TypeElement ) element, context );
AnnotationMetaEntity metaEntity = new AnnotationMetaEntity( processingEnv, ( TypeElement ) element, context );
// TODO instead of just adding the entity we have to do some merging.
context.getMetaSuperclassAndEmbeddableToProcess().put( metaEntity.getQualifiedName(), metaEntity );
@ -351,11 +351,11 @@ public class JPAMetaModelEntityProcessor extends AbstractProcessor {
ormStream = fileObject.openInputStream();
}
catch ( IOException e1 ) {
processingEnv.getMessager()
.printMessage(
Diagnostic.Kind.WARNING,
"Could not load " + resource + " using processingEnv.getFiler().getResource(). Using classpath..."
);
// processingEnv.getMessager()
// .printMessage(
// Diagnostic.Kind.WARNING,
// "Could not load " + resource + " using processingEnv.getFiler().getResource(). Using classpath..."
// );
// TODO
// unfortunately, the Filer.getResource API seems not to be able to load from /META-INF. One gets a

View File

@ -20,7 +20,7 @@ package org.hibernate.jpamodelgen;
/**
* @author Hardy Ferentschik
*/
public interface IMetaAttribute {
public interface MetaAttribute {
String getDeclarationString();
String getMetaType();

View File

@ -17,10 +17,10 @@
*/
package org.hibernate.jpamodelgen;
import org.hibernate.jpamodelgen.IMetaAttribute;
import org.hibernate.jpamodelgen.MetaAttribute;
/**
* @author Hardy Ferentschik
*/
public interface IMetaCollection extends IMetaAttribute {
public interface MetaCollection extends MetaAttribute {
}

View File

@ -24,14 +24,14 @@ import java.util.List;
/**
* @author Hardy Ferentschik
*/
public interface IMetaEntity extends ImportContext {
public interface MetaEntity extends ImportContext {
String getSimpleName();
String getQualifiedName();
String getPackageName();
List<IMetaAttribute> getMembers();
List<MetaAttribute> getMembers();
String generateImports();

View File

@ -20,5 +20,5 @@ package org.hibernate.jpamodelgen;
/**
* @author Hardy Ferentschik
*/
public interface IMetaSingleAttribute extends IMetaAttribute {
public interface MetaSingleAttribute extends MetaAttribute {
}

View File

@ -17,7 +17,7 @@
*/
package org.hibernate.jpamodelgen.annotation;
import org.hibernate.jpamodelgen.IMetaAttribute;
import org.hibernate.jpamodelgen.MetaAttribute;
import java.beans.Introspector;
@ -31,14 +31,14 @@ import javax.lang.model.element.ElementKind;
* @author Hardy Ferentschik
* @author Emmanuel Bernard
*/
public abstract class MetaAttribute implements IMetaAttribute {
public abstract class AnnotationMetaAttribute implements MetaAttribute {
final protected Element element;
final protected MetaEntity parent;
final protected AnnotationMetaEntity parent;
final protected ProcessingEnvironment pe;
private final String type;
public MetaAttribute(MetaEntity parent, Element element, String type) {
public AnnotationMetaAttribute(AnnotationMetaEntity parent, Element element, String type) {
this.element = element;
this.parent = parent;
this.type = type;

View File

@ -17,7 +17,7 @@
*/
package org.hibernate.jpamodelgen.annotation;
import org.hibernate.jpamodelgen.IMetaCollection;
import org.hibernate.jpamodelgen.MetaCollection;
import javax.lang.model.element.Element;
@ -27,12 +27,12 @@ import javax.lang.model.element.Element;
* @author Hardy Ferentschik
* @author Emmanuel Bernard
*/
public class MetaCollection extends MetaAttribute implements IMetaCollection {
public class AnnotationMetaCollection extends AnnotationMetaAttribute implements MetaCollection {
private String collectionType;
public MetaCollection(MetaEntity parent, Element element, String collectionType, String elementType) {
public AnnotationMetaCollection(AnnotationMetaEntity parent, Element element, String collectionType, String elementType) {
super(parent, element, elementType);
this.collectionType = collectionType;
}

View File

@ -48,8 +48,8 @@ import javax.persistence.ElementCollection;
import javax.tools.Diagnostic.Kind;
import javax.tools.Diagnostic;
import org.hibernate.jpamodelgen.IMetaEntity;
import org.hibernate.jpamodelgen.IMetaAttribute;
import org.hibernate.jpamodelgen.MetaEntity;
import org.hibernate.jpamodelgen.MetaAttribute;
import org.hibernate.jpamodelgen.ImportContext;
import org.hibernate.jpamodelgen.ImportContextImpl;
import org.hibernate.jpamodelgen.TypeUtils;
@ -61,7 +61,7 @@ import org.hibernate.jpamodelgen.Context;
* @author Hardy Ferentschik
* @author Emmanuel Bernard
*/
public class MetaEntity implements IMetaEntity {
public class AnnotationMetaEntity implements MetaEntity {
final TypeElement element;
final protected ProcessingEnvironment pe;
@ -72,14 +72,14 @@ public class MetaEntity implements IMetaEntity {
private AccessType defaultAccessTypeForHierarchy;
private AccessType defaultAccessTypeForElement;
public MetaEntity(ProcessingEnvironment pe, TypeElement element, Context context) {
public AnnotationMetaEntity(ProcessingEnvironment pe, TypeElement element, Context context) {
this.element = element;
this.pe = pe;
importContext = new ImportContextImpl( getPackageName() );
this.context = context;
}
public MetaEntity(ProcessingEnvironment pe, TypeElement element, Context context, AccessType accessType) {
public AnnotationMetaEntity(ProcessingEnvironment pe, TypeElement element, Context context, AccessType accessType) {
this(pe, element, context);
this.defaultAccessTypeForHierarchy = accessType;
}
@ -101,8 +101,8 @@ public class MetaEntity implements IMetaEntity {
return pe.getElementUtils().getName( packageOf.getQualifiedName() ).toString();
}
public List<IMetaAttribute> getMembers() {
List<IMetaAttribute> membersFound = new ArrayList<IMetaAttribute>();
public List<MetaAttribute> getMembers() {
List<MetaAttribute> membersFound = new ArrayList<MetaAttribute>();
final AccessType elementAccessType = getAccessTypeForElement();
List<? extends Element> fieldsOfClass = ElementFilter.fieldsIn( element.getEnclosedElements() );
@ -132,7 +132,7 @@ public class MetaEntity implements IMetaEntity {
}
private void addPersistentMembers(
List<IMetaAttribute> membersFound,
List<MetaAttribute> membersFound,
AccessType elementAccessType,
List<? extends Element> membersOfClass,
AccessType membersKind) {
@ -149,7 +149,7 @@ public class MetaEntity implements IMetaEntity {
}
for ( Element memberOfClass : membersOfClass ) {
MetaAttribute result = memberOfClass.asType().accept( new TypeVisitor( this, explicitAccessType ),
AnnotationMetaAttribute result = memberOfClass.asType().accept( new TypeVisitor( this, explicitAccessType ),
memberOfClass
);
if ( result != null ) {
@ -296,27 +296,27 @@ public class MetaEntity implements IMetaEntity {
COLLECTIONS.put( "java.util.Map", "javax.persistence.metamodel.MapAttribute" );
}
class TypeVisitor extends SimpleTypeVisitor6<MetaAttribute, Element> {
class TypeVisitor extends SimpleTypeVisitor6<AnnotationMetaAttribute, Element> {
MetaEntity parent;
AnnotationMetaEntity parent;
//if null, process all members as implicit
//if not null, only process members marked as @Access(explicitAccessType)
private AccessType explicitAccessType;
TypeVisitor(MetaEntity parent, AccessType explicitAccessType) {
TypeVisitor(AnnotationMetaEntity parent, AccessType explicitAccessType) {
this.parent = parent;
this.explicitAccessType = explicitAccessType;
}
@Override
protected MetaAttribute defaultAction(TypeMirror e, Element p) {
protected AnnotationMetaAttribute defaultAction(TypeMirror e, Element p) {
return super.defaultAction( e, p );
}
@Override
public MetaAttribute visitPrimitive(PrimitiveType t, Element element) {
public AnnotationMetaAttribute visitPrimitive(PrimitiveType t, Element element) {
if ( isPersistent( element ) ) {
return new MetaSingleAttribute( parent, element, TypeUtils.toTypeString( t ) );
return new AnnotationMetaSingleAttribute( parent, element, TypeUtils.toTypeString( t ) );
}
else {
return null;
@ -344,7 +344,7 @@ public class MetaEntity implements IMetaEntity {
@Override
public MetaAttribute visitDeclared(DeclaredType t, Element element) {
public AnnotationMetaAttribute visitDeclared(DeclaredType t, Element element) {
//FIXME consider XML
if ( isPersistent( element ) ) {
TypeElement returnedElement = ( TypeElement ) pe.getTypeUtils().asElement( t );
@ -359,10 +359,10 @@ public class MetaEntity implements IMetaEntity {
this.parent.defaultAccessTypeForElement );
}
if ( collection.equals( "javax.persistence.metamodel.MapAttribute" ) ) {
return new MetaMap( parent, element, collection, getKeyType( t ), getElementType( t ) );
return new AnnotationMetaMap( parent, element, collection, getKeyType( t ), getElementType( t ) );
}
else {
return new MetaCollection( parent, element, collection, getElementType( t ) );
return new AnnotationMetaCollection( parent, element, collection, getElementType( t ) );
}
}
else {
@ -372,7 +372,7 @@ public class MetaEntity implements IMetaEntity {
this.parent.context.processElement( returnedElement,
this.parent.defaultAccessTypeForElement );
}
return new MetaSingleAttribute( parent, element, returnedElement.getQualifiedName().toString() );
return new AnnotationMetaSingleAttribute( parent, element, returnedElement.getQualifiedName().toString() );
}
}
else {
@ -382,7 +382,7 @@ public class MetaEntity implements IMetaEntity {
@Override
public MetaAttribute visitExecutable(ExecutableType t, Element p) {
public AnnotationMetaAttribute visitExecutable(ExecutableType t, Element p) {
String string = p.getSimpleName().toString();
// TODO: implement proper property get/is/boolean detection

View File

@ -25,16 +25,16 @@ import javax.lang.model.element.Element;
* @author Hardy Ferentschik
* @author Emmanuel Bernard
*/
public class MetaMap extends MetaCollection {
public class AnnotationMetaMap extends AnnotationMetaCollection {
private final String keyType;
public MetaMap(MetaEntity parent, Element element, String collectionType,
public AnnotationMetaMap(AnnotationMetaEntity parent, Element element, String collectionType,
String keyType, String elementType) {
super(parent, element, collectionType, elementType);
this.keyType = keyType;
}
public String getDeclarationString() {
return "public static volatile " + parent.importType(getMetaType()) + "<" + parent.importType(parent.getQualifiedName()) + ", " + parent.importType(keyType) + ", " + parent.importType(getTypeDeclaration()) + "> " + getPropertyName() + ";";
}

View File

@ -17,7 +17,7 @@
*/
package org.hibernate.jpamodelgen.annotation;
import org.hibernate.jpamodelgen.IMetaSingleAttribute;
import org.hibernate.jpamodelgen.MetaSingleAttribute;
import javax.lang.model.element.Element;
@ -27,9 +27,9 @@ import javax.lang.model.element.Element;
* @author Hardy Ferentschik
* @author Emmanuel Bernard
*/
public class MetaSingleAttribute extends MetaAttribute implements IMetaSingleAttribute {
public class AnnotationMetaSingleAttribute extends AnnotationMetaAttribute implements MetaSingleAttribute {
public MetaSingleAttribute(MetaEntity parent, Element element, String type) {
public AnnotationMetaSingleAttribute(AnnotationMetaEntity parent, Element element, String type) {
super(parent, element, type);
}

View File

@ -18,12 +18,12 @@
package org.hibernate.jpamodelgen.xml;
import org.hibernate.jpamodelgen.IMetaAttribute;
import org.hibernate.jpamodelgen.MetaAttribute;
/**
* @author Hardy Ferentschik
*/
public abstract class XmlMetaAttribute implements IMetaAttribute {
public abstract class XmlMetaAttribute implements MetaAttribute {
private XmlMetaEntity parentEntity;

View File

@ -17,12 +17,12 @@
*/
package org.hibernate.jpamodelgen.xml;
import org.hibernate.jpamodelgen.IMetaCollection;
import org.hibernate.jpamodelgen.MetaCollection;
/**
* @author Hardy Ferentschik
*/
public class XmlMetaCollection extends XmlMetaAttribute implements IMetaCollection {
public class XmlMetaCollection extends XmlMetaAttribute implements MetaCollection {
String collectionType;

View File

@ -26,9 +26,9 @@ import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import org.hibernate.jpamodelgen.IMetaAttribute;
import org.hibernate.jpamodelgen.MetaAttribute;
import org.hibernate.jpamodelgen.ImportContextImpl;
import org.hibernate.jpamodelgen.IMetaEntity;
import org.hibernate.jpamodelgen.MetaEntity;
import org.hibernate.jpamodelgen.ImportContext;
import org.hibernate.jpamodelgen.xml.jaxb.Attributes;
import org.hibernate.jpamodelgen.xml.jaxb.Basic;
@ -45,7 +45,7 @@ import org.hibernate.jpamodelgen.xml.jaxb.OneToOne;
/**
* @author Hardy Ferentschik
*/
public class XmlMetaEntity implements IMetaEntity {
public class XmlMetaEntity implements MetaEntity {
static Map<String, String> COLLECTIONS = new HashMap<String, String>();
@ -62,7 +62,7 @@ public class XmlMetaEntity implements IMetaEntity {
final private ImportContext importContext;
final private List<IMetaAttribute> members = new ArrayList<IMetaAttribute>();
final private List<MetaAttribute> members = new ArrayList<MetaAttribute>();
private TypeElement element;
@ -135,7 +135,7 @@ public class XmlMetaEntity implements IMetaEntity {
return packageName;
}
public List<IMetaAttribute> getMembers() {
public List<MetaAttribute> getMembers() {
return members;
}

View File

@ -17,12 +17,12 @@
*/
package org.hibernate.jpamodelgen.xml;
import org.hibernate.jpamodelgen.IMetaSingleAttribute;
import org.hibernate.jpamodelgen.MetaSingleAttribute;
/**
* @author Hardy Ferentschik
*/
public class XmlMetaSingleAttribute extends XmlMetaAttribute implements IMetaSingleAttribute {
public class XmlMetaSingleAttribute extends XmlMetaAttribute implements MetaSingleAttribute {
public XmlMetaSingleAttribute(XmlMetaEntity parent, String propertyName, String type) {
super(parent, propertyName, type);

View File

@ -0,0 +1,90 @@
// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, 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.accesstype;
import org.testng.annotations.Test;
import org.hibernate.jpamodelgen.test.util.CompilationTest;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertAbsenceOfField;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertPresenceOfField;
/**
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*/
public class AccessTypeTest extends CompilationTest {
@Test
public void testExcludeTransientFieldAndStatic() throws Exception {
assertAbsenceOfField( Product.class.getName() + "_", "nonPersistent" );
assertAbsenceOfField( Product.class.getName() + "_", "nonPersistent2" );
}
@Test
public void testDefaultAccessTypeOnEntity() throws Exception {
assertAbsenceOfField( User.class.getName() + "_", "nonPersistent" );
}
@Test
public void testDefaultAccessTypeForSubclassOfEntity() throws Exception {
assertAbsenceOfField( Customer.class.getName() + "_", "nonPersistent" );
}
@Test
public void testDefaultAccessTypeForEmbeddable() throws Exception {
assertAbsenceOfField( Detail.class.getName() + "_", "nonPersistent" );
}
@Test
public void testInheritedAccessTypeForEmbeddable() throws Exception {
assertAbsenceOfField( Country.class.getName() + "_", "nonPersistent" );
assertAbsenceOfField(
Pet.class.getName() + "_", "nonPersistent", "Collection of embeddable not taken care of"
);
}
@Test
public void testDefaultAccessTypeForMappedSuperclass() throws Exception {
assertAbsenceOfField( Detail.class.getName() + "_", "volume" );
}
@Test
public void testExplicitAccessTypeAndDefaultFromRootEntity() throws Exception {
assertAbsenceOfField(
LivingBeing.class.getName() + "_",
"nonPersistent",
"explicit access type on mapped superclass"
);
assertAbsenceOfField( Hominidae.class.getName() + "_", "nonPersistent", "explicit access type on entity" );
assertAbsenceOfField(
Human.class.getName() + "_",
"nonPersistent",
"proper inheritance from root entity access type"
);
}
@Test
public void testMemberAccessType() throws Exception {
assertPresenceOfField( Customer.class.getName() + "_", "goodPayer", "access type overriding" );
}
@Override
protected String getTestPackage() {
return Product.class.getPackage().getName();
}
}

View File

@ -15,12 +15,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
package org.hibernate.jpamodelgen.test.accesstype;
import java.util.Set;
import javax.persistence.Embeddable;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.ElementCollection;
import javax.persistence.CollectionTable;

View File

@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
package org.hibernate.jpamodelgen.test.accesstype;
import javax.persistence.MappedSuperclass;

View File

@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
package org.hibernate.jpamodelgen.test.accesstype;
import javax.persistence.MappedSuperclass;

View File

@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
package org.hibernate.jpamodelgen.test.accesstype;
import javax.persistence.Embeddable;

View File

@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
package org.hibernate.jpamodelgen.test.accesstype;
import java.util.Set;
import javax.persistence.Entity;

View File

@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
package org.hibernate.jpamodelgen.test.accesstype;
import javax.persistence.Embeddable;

View File

@ -15,11 +15,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
package org.hibernate.jpamodelgen.test.accesstype;
import javax.persistence.Entity;
import javax.persistence.Access;
import javax.persistence.AccessType;
/**
* @author Emmanuel Bernard

View File

@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
package org.hibernate.jpamodelgen.test.accesstype;
import javax.persistence.Id;
import javax.persistence.Entity;

View File

@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
package org.hibernate.jpamodelgen.test.accesstype;
import javax.persistence.Entity;

View File

@ -0,0 +1,46 @@
// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, 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.accesstype;
import javax.persistence.Entity;
/**
* @author Emmanuel Bernard
*/
@Entity
public class Image {
private String name;
private byte[] data;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
}

View File

@ -15,12 +15,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
package org.hibernate.jpamodelgen.test.accesstype;
import java.util.Set;
import javax.persistence.Embeddable;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.ElementCollection;
/**

View File

@ -15,16 +15,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
package org.hibernate.jpamodelgen.test.accesstype;
import java.util.Map;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import org.hibernate.jpamodelgen.test.accesstype.Product;
/**
*
* @author Max Andersen

View File

@ -15,11 +15,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
package org.hibernate.jpamodelgen.test.accesstype;
import javax.persistence.MappedSuperclass;
import javax.persistence.Access;
import javax.persistence.AccessType;
/**
* @author Emmanuel Bernard

View File

@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
package org.hibernate.jpamodelgen.test.accesstype;
import javax.persistence.Entity;
import javax.persistence.Id;

View File

@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
package org.hibernate.jpamodelgen.test.accesstype;
import java.util.Date;
import java.util.List;

View File

@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
package org.hibernate.jpamodelgen.test.accesstype;
import javax.persistence.Embeddable;

View File

@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
package org.hibernate.jpamodelgen.test.accesstype;
import java.math.BigDecimal;
import java.util.Set;
@ -25,6 +25,9 @@ import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import org.hibernate.jpamodelgen.test.accesstype.Item;
import org.hibernate.jpamodelgen.test.accesstype.Shop;
/**
*
* @author Max Andersen

View File

@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
package org.hibernate.jpamodelgen.test.accesstype;
import javax.persistence.Entity;
import javax.persistence.Id;

View File

@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
package org.hibernate.jpamodelgen.test.accesstype;
import javax.persistence.Entity;
import javax.persistence.Id;

View File

@ -0,0 +1,59 @@
// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, 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.inheritance;
import javax.persistence.MappedSuperclass;
/**
* @author Emmanuel Bernard
*/
@MappedSuperclass
public class Area {
private int length;
private int width;
private int height;
//should not be persistent
public int getVolume() {
return length*width*height;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}

View File

@ -0,0 +1,38 @@
// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, 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.inheritance;
import java.sql.Date;
import javax.persistence.MappedSuperclass;
/**
* @author Emmanuel Bernard
*/
@MappedSuperclass
public class Building extends Area {
public Date getBuiltIn() {
return builtIn;
}
public void setBuiltIn(Date builtIn) {
this.builtIn = builtIn;
}
private Date builtIn;
}

View File

@ -0,0 +1,27 @@
// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, 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.inheritance;
import javax.persistence.Entity;
/**
* @author Emmanuel Bernard
*/
@Entity
public class Customer extends User {
}

View File

@ -0,0 +1,38 @@
// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, 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.inheritance;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* @author Emmanuel Bernard
*/
@Entity
public class House extends Building {
@Id
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}

View File

@ -15,32 +15,33 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package test;
package org.hibernate.jpamodelgen.test.inheritance;
import org.testng.annotations.Test;
import org.testng.Assert;
import model.Customer_;
import model.User_;
import model.House_;
import model.Building_;
import model.Area_;
import org.hibernate.jpamodelgen.test.util.CompilationTest;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertSuperClass;
/**
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*/
@Test
public class InheritanceTest {
public class InheritanceTest extends CompilationTest {
@Test
public void testSuperEntity() throws Exception {
Assert.assertEquals( Customer_.class.getSuperclass(), User_.class,
"Entity with super entity should inherit at the metamodel level");
assertSuperClass(
Customer.class.getName() + "_", User.class.getName() + "_"
);
}
@Test
public void testMappedSuperclass() throws Exception {
Assert.assertEquals( House_.class.getSuperclass(), Building_.class,
"Entity with mapped superclass should inherit at the metamodel level");
Assert.assertEquals( Building_.class.getSuperclass(), Area_.class,
"mapped superclass with mapped superclass should inherit at the metamodel level");
assertSuperClass( House.class.getName() + "_", Building.class.getName() + "_" );
assertSuperClass( Building.class.getName() + "_", Area.class.getName() + "_" );
}
@Override
protected String getTestPackage() {
return Customer.class.getPackage().getName();
}
}

View File

@ -0,0 +1,58 @@
// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, 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.inheritance;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;
/**
* @author Emmanuel Bernard
*/
@Entity
public class User {
private Long id;
private String nonPersistent;
private String name;
@Id
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Transient
public String getNonPersistent() {
return nonPersistent;
}
public void setNonPersistent(String nonPersistent) {
this.nonPersistent = nonPersistent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,127 @@
// $Id:$
/*
* JBoss, Home of Professional Open Source
* Copyright 2009, Red Hat, Inc. and/or its affiliates, 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.util;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import static org.testng.FileAssert.fail;
/**
* @author Hardy Ferentschik
*/
public abstract class CompilationTest {
private static final String PATH_SEPARATOR = System.getProperty( "file.separator" );
private static final String sourceBaseDir;
private static final String outBaseDir;
static {
String tmp = System.getProperty( "sourceBaseDir" );
if ( tmp == null ) {
fail( "The system property sourceBaseDir has to be set and point to the base directory of the test java sources." );
}
sourceBaseDir = tmp;
tmp = System.getProperty( "outBaseDir" );
if ( tmp == null ) {
fail( "The system property outBaseDir has to be set and point to the base directory of the test output directory." );
}
outBaseDir = tmp;
}
public CompilationTest() {
try {
compile();
}
catch ( Exception e ) {
fail( "Unable to compile test sources. " + e.getMessage() );
}
}
private void compile() throws IOException {
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 )
);
// TODO - need to call the compiler twice. Once to compile the test classes and generate the java files
// of the generated metamodel. The second compile is for generated the class files of the metamodel.
// Note sure why this is not recursive the same way as on the command line
compileSources( options, compiler, diagnostics, fileManager, compilationUnits );
compilationUnits = fileManager.getJavaFileObjectsFromFiles(
getCompilationUnits( outBaseDir )
);
compileSources( options, compiler, diagnostics, fileManager, compilationUnits );
fileManager.close();
}
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
);
task.call();
// for ( Diagnostic diagnostic : diagnostics.getDiagnostics() ) {
// System.out.println( diagnostic.getMessage( null ) );
// }
}
private List<String> createJavaOptions() {
// TODO
// passing any other options as -d seems to throw IllegalArgumentExceptions. I would like to set -s for example
// in order to see whether recursive recompilation would work then. Also '-proc only' could be interesting
List<String> options = new ArrayList<String>();
options.add( "-d" );
options.add( outBaseDir );
return options;
}
private List<File> getCompilationUnits(String baseDir) {
List<File> javaFiles = new ArrayList<File>();
String packageDirName = baseDir + PATH_SEPARATOR + getTestPackage().replace( ".", PATH_SEPARATOR );
File packageDir = new File( packageDirName );
FilenameFilter javaFileFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith( ".java" );
}
};
for ( File file : packageDir.listFiles( javaFileFilter ) ) {
javaFiles.add( file );
}
return javaFiles;
}
abstract protected String getTestPackage();
}

View File

@ -0,0 +1,83 @@
// $Id:$
/*
* JBoss, Home of Professional Open Source
* Copyright 2009, Red Hat, Inc. and/or its affiliates, 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.util;
import org.testng.Assert;
import static org.testng.Assert.assertNotNull;
import static org.testng.FileAssert.fail;
/**
* @author Hardy Ferentschik
*/
public class TestUtil {
private TestUtil() {
}
public static void assertClassGenerated(String className) {
try {
assertNotNull( Class.forName( className ) );
}
catch ( ClassNotFoundException e ) {
fail( e.getMessage() );
}
}
public static void assertAbsenceOfField(String className, String fieldName) throws ClassNotFoundException {
assertAbsenceOfField( className, fieldName, "field should not be persistent" );
}
public static void assertAbsenceOfField(String className, String fieldName, String errorString)
throws ClassNotFoundException {
Assert.assertFalse( isFieldHere( className, fieldName ), errorString );
}
public static void assertPresenceOfField(String className, String fieldName, String errorString)
throws ClassNotFoundException {
Assert.assertTrue( isFieldHere( className, fieldName ), errorString );
}
public static void assertSuperClass(String className, String superClassName) {
Class<?> clazz;
Class<?> superClazz;
try {
clazz = Class.forName( className );
superClazz = Class.forName( superClassName );
Assert.assertEquals(
clazz.getSuperclass(), superClazz,
"Entity " + superClassName + " should be the superclass of " + className
);
}
catch ( ClassNotFoundException e ) {
fail( "Unable to load metamodel class: " + e.getMessage() );
}
}
private static boolean isFieldHere(String className, String fieldName) throws ClassNotFoundException {
Class<?> clazz = Class.forName( className );
try {
clazz.getField( fieldName );
return true;
}
catch ( NoSuchFieldException e ) {
return false;
}
}
}

View File

@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model.xmlmapped;
package org.hibernate.jpamodelgen.test.xmlmapped;
/**
* @author Hardy Ferentschik

View File

@ -15,10 +15,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model.xmlmapped;
package org.hibernate.jpamodelgen.test.xmlmapped;
import model.Address;
import model.Area;
import org.hibernate.jpamodelgen.test.accesstype.*;
import org.hibernate.jpamodelgen.test.accesstype.Address;
/**
* @author Hardy Ferentschik
@ -30,7 +30,7 @@ public class Building extends Area {
return address;
}
public void setAddress(Address address) {
public void setAddress(org.hibernate.jpamodelgen.test.accesstype.Address address) {
this.address = address;
}
}

View File

@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model.xmlmapped;
package org.hibernate.jpamodelgen.test.xmlmapped;
/**
* @author Hardy Ferentschik

View File

@ -15,7 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model.xmlmapped;
package org.hibernate.jpamodelgen.test.xmlmapped;
/**
* @author Hardy Ferentschik

View File

@ -15,41 +15,46 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package test;
package org.hibernate.jpamodelgen.test.xmlmapped;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
import org.hibernate.jpamodelgen.test.util.CompilationTest;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertClassGenerated;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertPresenceOfField;
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertSuperClass;
/**
* @author Hardy Ferentschik
*/
public class XmlMappingTest {
public class XmlMappingTest extends CompilationTest {
@Test
public void testXmlConfiguredEmbeddedClassGenerated() throws Exception {
assertNotNull( Class.forName( "model.xmlmapped.Address_" ) );
assertClassGenerated( Address.class.getName() + "_" );
}
@Test
public void testXmlConfiguredMappedSuperclassGenerated() throws Exception {
Class<?> building = Class.forName( "model.xmlmapped.Building_" );
assertNotNull( building );
assertNotNull( building.getField( "address" ) );
assertClassGenerated( Building.class.getName() + "_" );
assertPresenceOfField(
Building.class.getName() + "_", "address", "address field should exist"
);
}
@Test
public void testClassHierarchy() throws Exception {
Class<?> mammal = Class.forName( "model.xmlmapped.Mammal_" );
assertNotNull( mammal );
Class<?> being = Class.forName( "model.xmlmapped.LivingBeing_" );
assertNotNull( being );
assertTrue( mammal.getSuperclass().equals( being ) );
assertClassGenerated( Mammal.class.getName() + "_" );
assertClassGenerated( LivingBeing.class.getName() + "_" );
assertSuperClass( Mammal.class.getName() + "_", LivingBeing.class.getName() + "_" );
}
@Test(expectedExceptions = ClassNotFoundException.class)
public void testNonExistentMappedClassesGetIgnored() throws Exception {
Class.forName( "model.Dummy_" );
Class.forName( "org.hibernate.jpamodelgen.test.model.Dummy_" );
}
@Override
protected String getTestPackage() {
return Address.class.getPackage().getName();
}
}

View File

@ -1,94 +0,0 @@
// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, 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 test;
import org.testng.annotations.Test;
import org.testng.Assert;
/**
* @author Emmanuel Bernard
*/
@Test
public class AccessTypeTest {
@Test
public void testExcludeTransientFieldAndStatic() throws Exception{
absenceOfField( "model.Product_", "nonPersistent" );
absenceOfField( "model.Product_", "nonPersistent2" );
}
@Test
public void testDefaultAccessTypeOnEntity() throws Exception{
absenceOfField( "model.User_", "nonPersistent" );
}
@Test
public void testDefaultAccessTypeForSubclassOfEntity() throws Exception{
absenceOfField( "model.Customer_", "nonPersistent" );
}
@Test
public void testDefaultAccessTypeForEmbeddable() throws Exception{
absenceOfField( "model.Detail_", "nonPersistent" );
}
@Test
public void testInheritedAccessTypeForEmbeddable() throws Exception{
absenceOfField( "model.Country_", "nonPersistent" );
absenceOfField( "model.Pet_", "nonPersistent", "Colleciton of membeddable not taken care of" );
}
@Test
public void testDefaultAccessTypeForMappedSuperclass() throws Exception{
absenceOfField( "model.Detail_", "volume" );
}
@Test
public void testExplicitAccessTypeAndDefaultFromRootEntity() throws Exception{
absenceOfField( "model.LivingBeing_", "nonPersistent", "eplicit access type on mapped superclass" );
absenceOfField( "model.Hominidae_", "nonPersistent", "eplicit access type on entity" );
absenceOfField( "model.Human_", "nonPersistent", "proper inheritance from root entity access type" );
}
@Test
public void testMemberAccessType() throws Exception{
presenceOfField( "model.Customer_", "goodPayer", "access type overriding" );
}
private void absenceOfField(String className, String fieldName) throws ClassNotFoundException {
absenceOfField( className, fieldName, "field should not be persistent" );
}
private void absenceOfField(String className, String fieldName, String errorString) throws ClassNotFoundException {
Assert.assertFalse( isFieldHere(className, fieldName), errorString );
}
private void presenceOfField(String className, String fieldName, String errorString) throws ClassNotFoundException {
Assert.assertTrue( isFieldHere(className, fieldName), errorString );
}
private boolean isFieldHere(String className, String fieldName) throws ClassNotFoundException {
Class<?> user_ = Class.forName( className );
try {
user_.getField( fieldName );
return true;
}
catch (NoSuchFieldException e) {
return false;
}
}
}

View File

@ -1,166 +0,0 @@
// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, 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 test;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Set;
import javax.persistence.Tuple;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Join;
import static javax.persistence.criteria.JoinType.INNER;
import javax.persistence.criteria.ListJoin;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import model.Item;
import model.Item_;
import model.Order;
import model.Order_;
import model.Product;
import model.Product_;
import model.Shop_;
/**
* Writing queries involves passing typesafe, statically cached, metamodel
* objects to the query builder in order to create the various parts of
* the query. The typesafe metamodel objects were validated at init time,
* so it is impossible to build invalid queries in the application code.
*
* @author Max Andersen
* @author Hardy Ferentschik
* @author Emmanuel Bernard
*/
public class QueryTest {
CriteriaBuilder qb;
public void test() {
CriteriaQuery<Tuple> q = qb.createTupleQuery();
Root<Order> order = q.from( Order.class );
Join<Item, Product> product = order.join( Order_.items )
.join( Item_.product );
Path<BigDecimal> price = product.get( Product_.price );
Path<Boolean> filled = order.get( Order_.filled );
Path<Date> date = order.get( Order_.date );
q.select( qb.tuple( order, product ) )
.where( qb.and( qb.gt( price, 100.00 ), qb.not( filled ) ) )
.orderBy( qb.asc( price ), qb.desc( date ) );
}
public void testUntypesafe() {
CriteriaQuery<Tuple> q = qb.createTupleQuery();
Root<Order> order = q.from( Order.class );
Join<Item, Product> product = order.join( "items" )
.join( "product" );
Path<BigDecimal> price = product.get( "price" );
Path<Boolean> filled = order.get( "filled" );
Path<Date> date = order.get( "date" );
q.select( qb.tuple( order, product ) )
.where( qb.and( qb.gt( price, 100.00 ), qb.not( filled ) ) )
.orderBy( qb.asc( price ), qb.desc( date ) );
}
/**
* Navigation by joining
*/
public void test2() {
CriteriaQuery<Product> q = qb.createQuery( Product.class );
Root<Product> product = q.from( Product.class );
Join<Item, Order> order = product.join( Product_.items )
.join( Item_.order );
q.select( product )
.where( qb.equal( order.get( Order_.id ), 12345l ) );
}
public void testMap() {
CriteriaQuery<Item> q = qb.createQuery( Item.class );
Root<Item> item = q.from( Item.class );
item.join( Item_.namedOrders );
}
/**
* Navigation by compound Path
*/
public void test3() {
CriteriaQuery<Item> q = qb.createQuery( Item.class );
Root<Item> item = q.from( Item.class );
Path<String> shopName = item.get( Item_.order )
.get( Order_.shop )
.get( Shop_.name );
q.select( item )
.where( qb.equal( shopName, "amazon.com" ) );
}
// public void test4() {
// CriteriaQuery q = qb.create();
//
// Root<Order> order = q.from(Order.class);
// ListJoin<Order, String> note = order.join(Order_.notes);
// Expression<Set<Item>> items = order.get(Order_.items);
// order.fetch(Order_.items, JoinType.INNER);
//
// q.select(note)
// .where( qb.and( qb.lt(note.index(), 10), qb.isNotEmpty(items) ) );
// }
public void test4Untypesafe() {
CriteriaQuery<String> q = qb.createQuery( String.class );
Root<Order> order = q.from( Order.class );
ListJoin<Order, String> note = order.joinList( "notes" );
Expression<Set<Item>> items = order.get( "items" );
order.fetch( "items", INNER );
q.select( note )
.where( qb.and( qb.lt( note.index(), 10 ), qb.isNotEmpty( items ) ) );
}
/*public void test5() {
Expression<Long> l= null;
Expression<Integer> i= null;
Expression<Float> x= null;
Expression<Float> y= null;
Expression<Number> n;
Expression<Float> f;
Expression<String> s = null;
n = qb.quot(l, i);
f = qb.sum(x, y);
n = qb.quot(x, y);
javax.jpamodelgen.criteria.Order o = qb.asc(n);
javax.jpamodelgen.criteria.Order p = qb.ascending(s);
}*/
}

View File

@ -5,7 +5,7 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
version="2.0"
>
<package>model</package>
<package>org.hibernate.jpamodelgen.test.model</package>
<entity class="Dummy" access="FIELD" metadata-complete="true"> <!-- Class does not exist -->
<attributes>
<id name="id"/>

View File

@ -5,7 +5,7 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd"
version="1.0"
>
<package>model</package>
<package>org.hibernate.jpamodelgen.test.model</package>
<entity class="Airplane" metadata-complete="true" access="PROPERTY">
<attributes>
<id name="serialNumber"/>

View File

@ -5,7 +5,7 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
version="2.0"
>
<package>model</package>
<package>org.hibernate.jpamodelgen.test.model</package>
<entity class="Dummy" access="FIELD" metadata-complete="true"> <!-- Class does not exist -->
<attributes>
<id name="id"/>

View File

@ -5,7 +5,7 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
version="2.0"
>
<package>model</package>
<package>org.hibernate.jpamodelgen.test.accesstype</package>
<!-- default package -->
<entity class="Order" access="FIELD" metadata-complete="true"> <!--means ignore annotations-->
<attributes>
@ -16,7 +16,7 @@
<basic name="date"/>
<many-to-one name="shop"/>
<one-to-many name="items"
target-entity="Item"
target-entity="org.hibernate.jpamodelgen.test.accesstype.Item"
fetch="EAGER"> <!-- target-entity optional guess the type from the geenric-->
<cascade>
<cascade-persist/>

View File

@ -7,8 +7,8 @@
<mapping-file>/META-INF/dummy.xml</mapping-file>
<mapping-file>/META-INF/malformed-mapping-xml.xml</mapping-file>
<mapping-file>/META-INF/jpa1-orm.xml</mapping-file>
<mapping-file>/model/xmlmapped/address.xml</mapping-file>
<mapping-file>/model/xmlmapped/building.xml</mapping-file>
<mapping-file>/model/xmlmapped/mammal.xml</mapping-file>
<mapping-file>/org/hibernate/jpamodelgen/test/xmlmapped/address.xml</mapping-file>
<mapping-file>/org/hibernate/jpamodelgen/test/xmlmapped/building.xml</mapping-file>
<mapping-file>/org/hibernate/jpamodelgen/test/xmlmapped/mammal.xml</mapping-file>
</persistence-unit>
</persistence>

View File

@ -5,7 +5,7 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
version="2.0"
>
<package>model.xmlmapped</package> <!-- default package -->
<package>org.hibernate.jpamodelgen.test.xmlmapped</package> <!-- default package -->
<embeddable class="Address" access="FIELD" metadata-complete="true"> <!--means ignore annotations-->
<attributes>
<basic name="street1"/>

View File

@ -5,7 +5,7 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
version="2.0"
>
<package>model.xmlmapped</package>
<package>org.hibernate.jpamodelgen.test.xmlmapped</package>
<mapped-superclass class="Building" access="FIELD" metadata-complete="true"> <!--means ignore annotations-->
<attributes>
<one-to-one name="address" fetch="LAZY"/>

View File

@ -5,7 +5,7 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
version="2.0"
>
<package>model.xmlmapped</package>
<package>org.hibernate.jpamodelgen.test.xmlmapped</package>
<entity class="LivingBeing" access="FIELD" metadata-complete="true">
<attributes>
<basic name="isReallyAlive"/>

View File

@ -3,7 +3,9 @@
<suite name="Hibernate Model Generator Tests" verbose="1">
<test name="Unit tests">
<packages>
<package name="test"/>
<package name="org.hibernate.jpamodelgen.test.accesstype"/>
<package name="org.hibernate.jpamodelgen.test.inheritance"/>
<package name="org.hibernate.jpamodelgen.test.xmlmapped"/>
</packages>
</test>
</suite>