HHH-17683 Reproducer for jpamodelgen generating wrong constructor for metamodel of entities whose methods contain a `static EntityManager getEntityManager()`
This commit is contained in:
parent
b499523fff
commit
1ac21756c6
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.jpamodelgen.test.constructor;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class EntityExtendingEntityWithInstanceGetEntityManager extends EntityWithInstanceGetEntityManager {
|
||||
private String otherName;
|
||||
|
||||
public String getOtherName() {
|
||||
return otherName;
|
||||
}
|
||||
|
||||
public void setOtherName(String otherName) {
|
||||
this.otherName = otherName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.jpamodelgen.test.constructor;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class EntityExtendingEntityWithStaticGetEntityManager extends EntityWithStaticGetEntityManager {
|
||||
private String otherName;
|
||||
|
||||
public String getOtherName() {
|
||||
return otherName;
|
||||
}
|
||||
|
||||
public void setOtherName(String otherName) {
|
||||
this.otherName = otherName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.jpamodelgen.test.constructor;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class EntityExtendingMapperSuperClassExtendingNonEntityWithInstanceGetEntityManager
|
||||
extends MapperSuperClassExtendingNonEntityWithInstanceGetEntityManager {
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private String otherName;
|
||||
|
||||
public String getOtherName() {
|
||||
return otherName;
|
||||
}
|
||||
|
||||
public void setOtherName(String otherName) {
|
||||
this.otherName = otherName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.jpamodelgen.test.constructor;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class EntityExtendingMapperSuperClassExtendingNonEntityWithStaticGetEntityManager
|
||||
extends MapperSuperClassExtendingNonEntityWithStaticGetEntityManager {
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private String otherName;
|
||||
|
||||
public String getOtherName() {
|
||||
return otherName;
|
||||
}
|
||||
|
||||
public void setOtherName(String otherName) {
|
||||
this.otherName = otherName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.jpamodelgen.test.constructor;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class EntityExtendingMapperSuperClassWithInstanceGetEntityManager
|
||||
extends MapperSuperClassWithInstanceGetEntityManager {
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private String otherName;
|
||||
|
||||
public String getOtherName() {
|
||||
return otherName;
|
||||
}
|
||||
|
||||
public void setOtherName(String otherName) {
|
||||
this.otherName = otherName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.jpamodelgen.test.constructor;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class EntityExtendingMapperSuperClassWithStaticGetEntityManager
|
||||
extends MapperSuperClassWithStaticGetEntityManager {
|
||||
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private String otherName;
|
||||
|
||||
public String getOtherName() {
|
||||
return otherName;
|
||||
}
|
||||
|
||||
public void setOtherName(String otherName) {
|
||||
this.otherName = otherName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.jpamodelgen.test.constructor;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class EntityExtendingNonEntityWithInstanceGetEntityManager extends NonEntityWithInstanceGetEntityManager {
|
||||
@Id
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private String otherName;
|
||||
|
||||
public String getOtherName() {
|
||||
return otherName;
|
||||
}
|
||||
|
||||
public void setOtherName(String otherName) {
|
||||
this.otherName = otherName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.jpamodelgen.test.constructor;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class EntityExtendingNonEntityWithStaticGetEntityManager extends NonEntityWithStaticGetEntityManager {
|
||||
@Id
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private String otherName;
|
||||
|
||||
public String getOtherName() {
|
||||
return otherName;
|
||||
}
|
||||
|
||||
public void setOtherName(String otherName) {
|
||||
this.otherName = otherName;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.jpamodelgen.test.constructor;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Transient;
|
||||
|
||||
@Entity
|
||||
public class EntityWithInstanceGetEntityManager {
|
||||
|
||||
@Transient
|
||||
public EntityManager getEntityManager() {
|
||||
// In a real-world scenario, this would contain some framework-specific code
|
||||
throw new IllegalStateException( "This method shouldn't be called in tests" );
|
||||
}
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
private String name;
|
||||
private String entityManager;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.jpamodelgen.test.constructor;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class EntityWithStaticGetEntityManager {
|
||||
|
||||
public static EntityManager getEntityManager() {
|
||||
// In a real-world scenario, this would contain some framework-specific code
|
||||
throw new IllegalStateException( "This method shouldn't be called in tests" );
|
||||
}
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.jpamodelgen.test.constructor;
|
||||
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
|
||||
@MappedSuperclass
|
||||
public abstract class MapperSuperClassExtendingNonEntityWithInstanceGetEntityManager
|
||||
extends NonEntityWithInstanceGetEntityManager {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.jpamodelgen.test.constructor;
|
||||
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
|
||||
@MappedSuperclass
|
||||
public abstract class MapperSuperClassExtendingNonEntityWithStaticGetEntityManager
|
||||
extends NonEntityWithStaticGetEntityManager {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.jpamodelgen.test.constructor;
|
||||
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import jakarta.persistence.Transient;
|
||||
|
||||
@MappedSuperclass
|
||||
public abstract class MapperSuperClassWithInstanceGetEntityManager {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Transient
|
||||
public String getEntityManager() {
|
||||
// In a real-world scenario, this would contain some framework-specific code
|
||||
throw new IllegalStateException( "This method shouldn't be called in tests" );
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.jpamodelgen.test.constructor;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
|
||||
@MappedSuperclass
|
||||
public abstract class MapperSuperClassWithStaticGetEntityManager {
|
||||
|
||||
public static EntityManager getEntityManager() {
|
||||
// In a real-world scenario, this would contain some framework-specific code
|
||||
throw new IllegalStateException( "This method shouldn't be called in tests" );
|
||||
}
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.jpamodelgen.test.constructor;
|
||||
|
||||
import jakarta.persistence.Transient;
|
||||
|
||||
public abstract class NonEntityWithInstanceGetEntityManager {
|
||||
|
||||
@Transient
|
||||
public String getEntityManager() {
|
||||
// In a real-world scenario, this would contain some framework-specific code
|
||||
throw new IllegalStateException( "This method shouldn't be called in tests" );
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.jpamodelgen.test.constructor;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
|
||||
public abstract class NonEntityWithStaticGetEntityManager {
|
||||
|
||||
public static EntityManager getEntityManager() {
|
||||
// In a real-world scenario, this would contain some framework-specific code
|
||||
throw new IllegalStateException( "This method shouldn't be called in tests" );
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,125 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.jpamodelgen.test.constructor;
|
||||
|
||||
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertAbsenceOfNonDefaultConstructorInMetamodelFor;
|
||||
import static org.hibernate.jpamodelgen.test.util.TestUtil.assertMetamodelClassGeneratedFor;
|
||||
|
||||
import org.hibernate.jpamodelgen.test.util.CompilationTest;
|
||||
import org.hibernate.jpamodelgen.test.util.TestForIssue;
|
||||
import org.hibernate.jpamodelgen.test.util.TestUtil;
|
||||
import org.hibernate.jpamodelgen.test.util.WithClasses;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test various scenarios where a superclass of an entity has a {@code getEntityManager()} method.
|
||||
* <p>
|
||||
* The superclass may be itself an entity, or a mapped superclass, or not mapped at all.
|
||||
* <p>
|
||||
* The method may be static or not.
|
||||
*/
|
||||
@TestForIssue(jiraKey = "HHH-17683")
|
||||
public class SuperClassWithGetEntityManagerTest extends CompilationTest {
|
||||
@Test
|
||||
@WithClasses({ EntityWithInstanceGetEntityManager.class, EntityExtendingEntityWithInstanceGetEntityManager.class })
|
||||
public void entityWithInstanceGetEntityManager() {
|
||||
doTest( EntityWithInstanceGetEntityManager.class, EntityExtendingEntityWithInstanceGetEntityManager.class );
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithClasses({ EntityWithStaticGetEntityManager.class, EntityExtendingEntityWithStaticGetEntityManager.class })
|
||||
public void entityWithStaticGetEntityManager() {
|
||||
doTest( EntityWithStaticGetEntityManager.class, EntityExtendingEntityWithStaticGetEntityManager.class );
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithClasses({
|
||||
NonEntityWithInstanceGetEntityManager.class, EntityExtendingNonEntityWithInstanceGetEntityManager.class
|
||||
})
|
||||
public void nonEntityWithInstanceGetEntityManager() {
|
||||
doTest(
|
||||
NonEntityWithInstanceGetEntityManager.class,
|
||||
EntityExtendingNonEntityWithInstanceGetEntityManager.class
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithClasses({
|
||||
NonEntityWithStaticGetEntityManager.class,
|
||||
EntityExtendingNonEntityWithStaticGetEntityManager.class
|
||||
})
|
||||
public void nonEntityWithStaticGetEntityManager() {
|
||||
doTest(
|
||||
NonEntityWithStaticGetEntityManager.class,
|
||||
EntityExtendingNonEntityWithStaticGetEntityManager.class
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithClasses({
|
||||
MapperSuperClassWithInstanceGetEntityManager.class,
|
||||
EntityExtendingMapperSuperClassWithInstanceGetEntityManager.class
|
||||
})
|
||||
public void mappedSuperClassWithInstanceGetEntityManager() {
|
||||
doTest(
|
||||
MapperSuperClassWithInstanceGetEntityManager.class,
|
||||
EntityExtendingMapperSuperClassWithInstanceGetEntityManager.class
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithClasses({
|
||||
MapperSuperClassWithStaticGetEntityManager.class,
|
||||
EntityExtendingMapperSuperClassWithStaticGetEntityManager.class
|
||||
})
|
||||
public void mappedSuperClassWithStaticGetEntityManager() {
|
||||
doTest(
|
||||
MapperSuperClassWithStaticGetEntityManager.class,
|
||||
EntityExtendingMapperSuperClassWithStaticGetEntityManager.class
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithClasses({
|
||||
NonEntityWithInstanceGetEntityManager.class,
|
||||
MapperSuperClassExtendingNonEntityWithInstanceGetEntityManager.class,
|
||||
EntityExtendingMapperSuperClassExtendingNonEntityWithInstanceGetEntityManager.class
|
||||
})
|
||||
public void mappedSuperClassExtendingNonEntityWithInstanceGetEntityManager() {
|
||||
doTest(
|
||||
MapperSuperClassExtendingNonEntityWithInstanceGetEntityManager.class,
|
||||
EntityExtendingMapperSuperClassExtendingNonEntityWithInstanceGetEntityManager.class
|
||||
);
|
||||
}
|
||||
|
||||
// NOTE: only this test matches the Panache use case exactly.
|
||||
// see https://github.com/quarkusio/quarkus/issues/38378#issuecomment-1911702314
|
||||
@Test
|
||||
@WithClasses({
|
||||
NonEntityWithStaticGetEntityManager.class,
|
||||
MapperSuperClassExtendingNonEntityWithStaticGetEntityManager.class,
|
||||
EntityExtendingMapperSuperClassExtendingNonEntityWithStaticGetEntityManager.class
|
||||
})
|
||||
public void mappedSuperClassExtendingNonEntityWithStaticGetEntityManager() {
|
||||
doTest(
|
||||
MapperSuperClassExtendingNonEntityWithStaticGetEntityManager.class,
|
||||
EntityExtendingMapperSuperClassExtendingNonEntityWithStaticGetEntityManager.class
|
||||
);
|
||||
}
|
||||
|
||||
private void doTest(Class<?> superclass, Class<?> entitySubclass) {
|
||||
System.out.println( TestUtil.getMetaModelSourceAsString( superclass ) );
|
||||
System.out.println( TestUtil.getMetaModelSourceAsString( entitySubclass ) );
|
||||
assertMetamodelClassGeneratedFor( entitySubclass );
|
||||
assertAbsenceOfNonDefaultConstructorInMetamodelFor(
|
||||
entitySubclass,
|
||||
"The generated metamodel shouldn't include a non-default constructor. In particular, it shouldn't be injected with an entity manager."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -6,11 +6,18 @@
|
|||
*/
|
||||
package org.hibernate.jpamodelgen.test.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.GenericArrayType;
|
||||
import java.lang.reflect.Method;
|
||||
|
@ -18,14 +25,14 @@ import java.lang.reflect.ParameterizedType;
|
|||
import java.lang.reflect.Type;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import jakarta.persistence.metamodel.ListAttribute;
|
||||
import jakarta.persistence.metamodel.SetAttribute;
|
||||
import javax.tools.Diagnostic;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import jakarta.persistence.metamodel.ListAttribute;
|
||||
import jakarta.persistence.metamodel.SetAttribute;
|
||||
|
||||
/**
|
||||
* @author Hardy Ferentschik
|
||||
|
@ -59,6 +66,10 @@ public class TestUtil {
|
|||
assertFalse( "There should be no source file: " + sourceFile.getName(), sourceFile.exists() );
|
||||
}
|
||||
|
||||
public static void assertAbsenceOfNonDefaultConstructorInMetamodelFor(Class<?> clazz, String errorString) {
|
||||
assertFalse(buildErrorString( errorString, clazz ), hasNonDefaultConstructorInMetamodelFor( clazz ) );
|
||||
}
|
||||
|
||||
public static void assertAbsenceOfFieldInMetamodelFor(Class<?> clazz, String fieldName) {
|
||||
assertAbsenceOfFieldInMetamodelFor(
|
||||
clazz,
|
||||
|
@ -92,16 +103,21 @@ public class TestUtil {
|
|||
assertTrue( buildErrorString( errorString, clazz ), hasFieldInMetamodelFor( clazz, fieldName ) );
|
||||
}
|
||||
|
||||
public static void assertPresenceOfMethodInMetamodelFor(Class<?> clazz, String fieldName, String errorString, Class<?>... params) {
|
||||
public static void assertPresenceOfMethodInMetamodelFor(Class<?> clazz, String fieldName, String errorString,
|
||||
Class<?>... params) {
|
||||
assertTrue( buildErrorString( errorString, clazz ), hasMethodInMetamodelFor( clazz, fieldName, params ) );
|
||||
}
|
||||
|
||||
public static void assertPresenceOfNameFieldInMetamodelFor(Class<?> clazz, String fieldName, String errorString) {
|
||||
assertTrue( buildErrorString( errorString, clazz ), hasFieldInMetamodelFor( clazz, fieldName ) );
|
||||
assertEquals(buildErrorString(errorString, clazz), getFieldFromMetamodelFor(clazz, fieldName).getType(), String.class);
|
||||
assertEquals(
|
||||
buildErrorString( errorString, clazz ), getFieldFromMetamodelFor( clazz, fieldName ).getType(),
|
||||
String.class
|
||||
);
|
||||
}
|
||||
|
||||
public static void assertAttributeTypeInMetaModelFor(Class<?> clazz, String fieldName, Class<?> expectedType, String errorString) {
|
||||
public static void assertAttributeTypeInMetaModelFor(Class<?> clazz, String fieldName, Class<?> expectedType,
|
||||
String errorString) {
|
||||
Field field = getFieldFromMetamodelFor( clazz, fieldName );
|
||||
assertNotNull( "Cannot find field '" + fieldName + "' in " + clazz.getName(), field );
|
||||
ParameterizedType type = (ParameterizedType) field.getGenericType();
|
||||
|
@ -117,7 +133,8 @@ public class TestUtil {
|
|||
);
|
||||
}
|
||||
|
||||
public static void assertAttributeTypeInMetaModelFor(Class<?> clazz, String fieldName, Type expectedType, String errorString) {
|
||||
public static void assertAttributeTypeInMetaModelFor(Class<?> clazz, String fieldName, Type expectedType,
|
||||
String errorString) {
|
||||
Field field = getFieldFromMetamodelFor( clazz, fieldName );
|
||||
assertNotNull( "Cannot find field '" + fieldName + "' in " + clazz.getName(), field );
|
||||
ParameterizedType type = (ParameterizedType) field.getGenericType();
|
||||
|
@ -129,15 +146,18 @@ public class TestUtil {
|
|||
);
|
||||
}
|
||||
|
||||
public static void assertSetAttributeTypeInMetaModelFor(Class<?> clazz, String fieldName, Class<?> expectedType, String errorString) {
|
||||
public static void assertSetAttributeTypeInMetaModelFor(Class<?> clazz, String fieldName, Class<?> expectedType,
|
||||
String errorString) {
|
||||
assertCollectionAttributeTypeInMetaModelFor( clazz, fieldName, SetAttribute.class, expectedType, errorString );
|
||||
}
|
||||
|
||||
public static void assertListAttributeTypeInMetaModelFor(Class<?> clazz, String fieldName, Class<?> expectedType, String errorString) {
|
||||
public static void assertListAttributeTypeInMetaModelFor(Class<?> clazz, String fieldName, Class<?> expectedType,
|
||||
String errorString) {
|
||||
assertCollectionAttributeTypeInMetaModelFor( clazz, fieldName, ListAttribute.class, expectedType, errorString );
|
||||
}
|
||||
|
||||
public static void assertMapAttributesInMetaModelFor(Class<?> clazz, String fieldName, Class<?> expectedMapKey, Class<?> expectedMapValue, String errorString) {
|
||||
public static void assertMapAttributesInMetaModelFor(Class<?> clazz, String fieldName, Class<?> expectedMapKey,
|
||||
Class<?> expectedMapValue, String errorString) {
|
||||
Field field = getFieldFromMetamodelFor( clazz, fieldName );
|
||||
assertNotNull( field );
|
||||
ParameterizedType type = (ParameterizedType) field.getGenericType();
|
||||
|
@ -180,7 +200,8 @@ public class TestUtil {
|
|||
getMetamodelClassFor( clazz );
|
||||
fail();
|
||||
}
|
||||
catch (AssertionError ae) {}
|
||||
catch (AssertionError ae) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -216,7 +237,7 @@ public class TestUtil {
|
|||
URLClassLoader classLoader = new URLClassLoader( urls, TestUtil.class.getClassLoader() );
|
||||
return classLoader.loadClass( metaModelClassName );
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
catch (Exception e) {
|
||||
fail( metaModelClassName + " was not generated." );
|
||||
}
|
||||
// keep the compiler happy
|
||||
|
@ -240,11 +261,11 @@ public class TestUtil {
|
|||
try {
|
||||
String line;
|
||||
/*
|
||||
* readLine is a bit quirky :
|
||||
* it returns the content of a line MINUS the newline.
|
||||
* it returns null only for the END of the stream.
|
||||
* it returns an empty String if two newlines appear in a row.
|
||||
*/
|
||||
* readLine is a bit quirky :
|
||||
* it returns the content of a line MINUS the newline.
|
||||
* it returns null only for the END of the stream.
|
||||
* it returns an empty String if two newlines appear in a row.
|
||||
*/
|
||||
while ( ( line = input.readLine() ) != null ) {
|
||||
contents.append( line );
|
||||
contents.append( System.lineSeparator() );
|
||||
|
@ -254,7 +275,7 @@ public class TestUtil {
|
|||
input.close();
|
||||
}
|
||||
}
|
||||
catch ( IOException ex ) {
|
||||
catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
|
@ -266,12 +287,17 @@ public class TestUtil {
|
|||
log.info( getMetaModelSourceAsString( clazz ) );
|
||||
}
|
||||
|
||||
public static Constructor<?>[] getConstructorsFromMetamodelFor(Class<?> entityClass) {
|
||||
Class<?> metaModelClass = getMetamodelClassFor( entityClass );
|
||||
return metaModelClass.getConstructors();
|
||||
}
|
||||
|
||||
public static Field getFieldFromMetamodelFor(Class<?> entityClass, String fieldName) {
|
||||
Class<?> metaModelClass = getMetamodelClassFor( entityClass );
|
||||
try {
|
||||
return metaModelClass.getDeclaredField( fieldName );
|
||||
}
|
||||
catch ( NoSuchFieldException e ) {
|
||||
catch (NoSuchFieldException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -281,7 +307,7 @@ public class TestUtil {
|
|||
try {
|
||||
return metaModelClass.getDeclaredMethod( methodName, params );
|
||||
}
|
||||
catch ( NoSuchMethodException e ) {
|
||||
catch (NoSuchMethodException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -290,6 +316,11 @@ public class TestUtil {
|
|||
return fcn.replace( PACKAGE_SEPARATOR, RESOURCE_SEPARATOR );
|
||||
}
|
||||
|
||||
private static boolean hasNonDefaultConstructorInMetamodelFor(Class<?> clazz) {
|
||||
return Arrays.stream( getConstructorsFromMetamodelFor( clazz ) )
|
||||
.anyMatch( constructor -> constructor.getParameterCount() > 0 );
|
||||
}
|
||||
|
||||
private static boolean hasFieldInMetamodelFor(Class<?> clazz, String fieldName) {
|
||||
return getFieldFromMetamodelFor( clazz, fieldName ) != null;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue