From c750d3bfbea8df810cd13ea1b23efdd7b57b49e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Thu, 1 Apr 2021 15:34:10 +0200 Subject: [PATCH] Move all class name constants of the hibernate-orm extension to a single class So that we can test them easily. --- .../hibernate/orm/deployment/ClassNames.java | 51 ++++++++ .../deployment/HibernateOrmCdiProcessor.java | 26 ++-- .../orm/deployment/HibernateOrmProcessor.java | 38 ++---- .../HibernateUserTypeProcessor.java | 9 +- .../orm/deployment/JpaJandexScavenger.java | 20 ++- .../quarkus/hibernate/orm/ConstantsTest.java | 114 ------------------ .../orm/deployment/ConstantsTest.java | 52 ++++++++ 7 files changed, 134 insertions(+), 176 deletions(-) create mode 100644 extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/ClassNames.java delete mode 100644 extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/ConstantsTest.java create mode 100644 extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/deployment/ConstantsTest.java diff --git a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/ClassNames.java b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/ClassNames.java new file mode 100644 index 00000000000000..81a272b886663f --- /dev/null +++ b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/ClassNames.java @@ -0,0 +1,51 @@ +package io.quarkus.hibernate.orm.deployment; + +import java.util.HashSet; +import java.util.Set; + +import org.jboss.jandex.DotName; + +public class ClassNames { + + static final Set CREATED_CONSTANTS = new HashSet<>(); + + private ClassNames() { + } + + private static DotName createConstant(String fqcn) { + DotName result = DotName.createSimple(fqcn); + CREATED_CONSTANTS.add(result); + return result; + } + + public static final DotName ENUM = createConstant("java.lang.Enum"); + + public static final DotName TENANT_CONNECTION_RESOLVER = createConstant( + "io.quarkus.hibernate.orm.runtime.tenant.TenantConnectionResolver"); + public static final DotName TENANT_RESOLVER = createConstant("io.quarkus.hibernate.orm.runtime.tenant.TenantResolver"); + + public static final DotName STATIC_METAMODEL = createConstant("javax.persistence.metamodel.StaticMetamodel"); + + public static final DotName QUARKUS_PERSISTENCE_UNIT = createConstant("io.quarkus.hibernate.orm.PersistenceUnit"); + public static final DotName QUARKUS_PERSISTENCE_UNIT_REPEATABLE_CONTAINER = createConstant( + "io.quarkus.hibernate.orm.PersistenceUnit$List"); + public static final DotName JPA_PERSISTENCE_UNIT = createConstant("javax.persistence.PersistenceUnit"); + public static final DotName JPA_PERSISTENCE_CONTEXT = createConstant("javax.persistence.PersistenceContext"); + + public static final DotName JPA_ENTITY = createConstant("javax.persistence.Entity"); + public static final DotName MAPPED_SUPERCLASS = createConstant("javax.persistence.MappedSuperclass"); + public static final DotName EMBEDDABLE = createConstant("javax.persistence.Embeddable"); + public static final DotName EMBEDDED = createConstant("javax.persistence.Embedded"); + public static final DotName ELEMENT_COLLECTION = createConstant("javax.persistence.ElementCollection"); + public static final DotName PROXY = createConstant("org.hibernate.annotations.Proxy"); + public static final DotName HIBERNATE_PROXY = createConstant("org.hibernate.proxy.HibernateProxy"); + public static final DotName TYPE = createConstant("org.hibernate.annotations.Type"); + public static final DotName TYPE_DEFINITION = createConstant("org.hibernate.annotations.TypeDef"); + public static final DotName TYPE_DEFINITIONS = createConstant("org.hibernate.annotations.TypeDefs"); + + public static final DotName ENTITY_MANAGER_FACTORY = createConstant("javax.persistence.EntityManagerFactory"); + public static final DotName SESSION_FACTORY = createConstant("org.hibernate.SessionFactory"); + public static final DotName ENTITY_MANAGER = createConstant("javax.persistence.EntityManager"); + public static final DotName SESSION = createConstant("org.hibernate.Session"); + +} diff --git a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmCdiProcessor.java b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmCdiProcessor.java index 3585e445b13fb9..0382b6b4997f3d 100644 --- a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmCdiProcessor.java +++ b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmCdiProcessor.java @@ -31,19 +31,9 @@ public class HibernateOrmCdiProcessor { - public static final List SESSION_FACTORY_EXPOSED_TYPES = Arrays.asList( - DotName.createSimple("javax.persistence.EntityManagerFactory"), - DotName.createSimple("org.hibernate.SessionFactory")); - public static final List SESSION_EXPOSED_TYPES = Arrays.asList( - DotName.createSimple("javax.persistence.EntityManager"), - DotName.createSimple("org.hibernate.Session")); - - public static final DotName PERSISTENCE_UNIT_QUALIFIER = DotName.createSimple("io.quarkus.hibernate.orm.PersistenceUnit"); - - public static final DotName JPA_PERSISTENCE_UNIT = DotName.createSimple("javax.persistence.PersistenceUnit"); - - public static final DotName JPA_PERSISTENCE_CONTEXT = DotName - .createSimple("javax.persistence.PersistenceContext"); + private static final List SESSION_FACTORY_EXPOSED_TYPES = Arrays.asList(ClassNames.ENTITY_MANAGER_FACTORY, + ClassNames.SESSION_FACTORY); + private static final List SESSION_EXPOSED_TYPES = Arrays.asList(ClassNames.ENTITY_MANAGER, ClassNames.SESSION); @BuildStep AnnotationsTransformerBuildItem convertJpaResourceAnnotationsToQualifier( @@ -69,10 +59,10 @@ public void transform(TransformationContext transformationContext) { } DotName jpaAnnotation; - if (field.hasAnnotation(JPA_PERSISTENCE_UNIT)) { - jpaAnnotation = JPA_PERSISTENCE_UNIT; - } else if (field.hasAnnotation(JPA_PERSISTENCE_CONTEXT)) { - jpaAnnotation = JPA_PERSISTENCE_CONTEXT; + if (field.hasAnnotation(ClassNames.JPA_PERSISTENCE_UNIT)) { + jpaAnnotation = ClassNames.JPA_PERSISTENCE_UNIT; + } else if (field.hasAnnotation(ClassNames.JPA_PERSISTENCE_CONTEXT)) { + jpaAnnotation = ClassNames.JPA_PERSISTENCE_CONTEXT; } else { return; } @@ -91,7 +81,7 @@ public void transform(TransformationContext transformationContext) { // in this case, we consider it the default too if the name matches transformation.add(DotNames.DEFAULT); } else { - transformation.add(PERSISTENCE_UNIT_QUALIFIER, + transformation.add(ClassNames.QUARKUS_PERSISTENCE_UNIT, AnnotationValue.createStringValue("value", persistenceUnitNameAnnotationValue.asString())); } transformation.done(); diff --git a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java index 8557ea2f0185f9..7394013f079339 100644 --- a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java +++ b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java @@ -137,21 +137,7 @@ public final class HibernateOrmProcessor { private static final Logger LOG = Logger.getLogger(HibernateOrmProcessor.class); - public static final DotName TENANT_CONNECTION_RESOLVER = DotName - .createSimple("io.quarkus.hibernate.orm.runtime.tenant.TenantConnectionResolver"); - public static final DotName TENANT_RESOLVER = DotName - .createSimple("io.quarkus.hibernate.orm.runtime.tenant.TenantResolver"); - - public static final DotName STATIC_METAMODEL = DotName.createSimple("javax.persistence.metamodel.StaticMetamodel"); - public static final DotName PERSISTENCE_UNIT = DotName.createSimple("io.quarkus.hibernate.orm.PersistenceUnit"); - public static final DotName PERSISTENCE_UNIT_REPEATABLE_CONTAINER = DotName - .createSimple("io.quarkus.hibernate.orm.PersistenceUnit$List"); - public static final DotName JPA_ENTITY = DotName.createSimple("javax.persistence.Entity"); - public static final DotName MAPPED_SUPERCLASS = DotName.createSimple("javax.persistence.MappedSuperclass"); - public static final DotName PROXY = DotName.createSimple("org.hibernate.annotations.Proxy"); - private static final String INTEGRATOR_SERVICE_FILE = "META-INF/services/org.hibernate.integrator.spi.Integrator"; - public static final String HIBERNATE_PROXY_INTERFACENAME = "org.hibernate.proxy.HibernateProxy"; @BuildStep CapabilityBuildItem capability() { @@ -188,7 +174,7 @@ void includeArchivesHostingEntityPackagesInIndex(HibernateOrmConfig hibernateOrm @BuildStep AdditionalIndexedClassesBuildItem addPersistenceUnitAnnotationToIndex() { - return new AdditionalIndexedClassesBuildItem(PERSISTENCE_UNIT.toString()); + return new AdditionalIndexedClassesBuildItem(ClassNames.QUARKUS_PERSISTENCE_UNIT.toString()); } // We do our own enhancement during the compilation phase, so disable any @@ -594,8 +580,9 @@ public void multitenancy(HibernateOrmRecorder recorder, } if (multitenancyEnabled) { - unremovableBeans.produce(new UnremovableBeanBuildItem(new BeanTypeExclusion(TENANT_CONNECTION_RESOLVER))); - unremovableBeans.produce(new UnremovableBeanBuildItem(new BeanTypeExclusion(TENANT_RESOLVER))); + unremovableBeans + .produce(new UnremovableBeanBuildItem(new BeanTypeExclusion(ClassNames.TENANT_CONNECTION_RESOLVER))); + unremovableBeans.produce(new UnremovableBeanBuildItem(new BeanTypeExclusion(ClassNames.TENANT_RESOLVER))); } } @@ -610,7 +597,7 @@ public void produceLoggingCategories(HibernateOrmConfig hibernateOrmConfig, @BuildStep(onlyIf = NativeOrNativeSourcesBuild.class) public void registerStaticMetamodelClassesForReflection(CombinedIndexBuildItem index, BuildProducer reflective) { - Collection annotationInstances = index.getIndex().getAnnotations(STATIC_METAMODEL); + Collection annotationInstances = index.getIndex().getAnnotations(ClassNames.STATIC_METAMODEL); if (!annotationInstances.isEmpty()) { String[] metamodel = annotationInstances.stream() @@ -1036,8 +1023,8 @@ private static Map> getModelClassesAndPackagesPerPersistence Set relatedModelClassNames = getRelatedModelClassNames(index, jpaEntities.getAllModelClassNames(), modelClassInfo); - if (modelClassInfo != null && (modelClassInfo.classAnnotation(PERSISTENCE_UNIT) != null - || modelClassInfo.classAnnotation(PERSISTENCE_UNIT_REPEATABLE_CONTAINER) != null)) { + if (modelClassInfo != null && (modelClassInfo.classAnnotation(ClassNames.QUARKUS_PERSISTENCE_UNIT) != null + || modelClassInfo.classAnnotation(ClassNames.QUARKUS_PERSISTENCE_UNIT_REPEATABLE_CONTAINER) != null)) { modelClassesWithPersistenceUnitAnnotations.add(modelClassInfo.name().toString()); } @@ -1097,8 +1084,8 @@ private static Set getRelatedModelClassNames(IndexView index, Set relatedModelClassNames = new HashSet<>(); // for now we only deal with entities and mapped super classes - if (modelClassInfo.classAnnotation(JPA_ENTITY) == null && - modelClassInfo.classAnnotation(MAPPED_SUPERCLASS) == null) { + if (modelClassInfo.classAnnotation(ClassNames.JPA_ENTITY) == null && + modelClassInfo.classAnnotation(ClassNames.MAPPED_SUPERCLASS) == null) { return Collections.emptySet(); } @@ -1137,7 +1124,8 @@ private static boolean hasPackagesInQuarkusConfig(HibernateOrmConfig hibernateOr } private static Collection getPackageLevelPersistenceUnitAnnotations(IndexView index) { - Collection persistenceUnitAnnotations = index.getAnnotationsWithRepeatable(PERSISTENCE_UNIT, index); + Collection persistenceUnitAnnotations = index + .getAnnotationsWithRepeatable(ClassNames.QUARKUS_PERSISTENCE_UNIT, index); Collection packageLevelPersistenceUnitAnnotations = new ArrayList<>(); for (AnnotationInstance persistenceUnitAnnotation : persistenceUnitAnnotations) { @@ -1220,7 +1208,7 @@ private PreGeneratedProxies generatedProxies(Set managedClassAndPackageN //create a map of entity to proxy type PreGeneratedProxies preGeneratedProxies = new PreGeneratedProxies(); Map proxyAnnotations = new HashMap<>(); - for (AnnotationInstance i : combinedIndex.getAnnotations(PROXY)) { + for (AnnotationInstance i : combinedIndex.getAnnotations(ClassNames.PROXY)) { AnnotationValue proxyClass = i.value("proxyClass"); if (proxyClass == null) { continue; @@ -1235,7 +1223,7 @@ private PreGeneratedProxies generatedProxies(Set managedClassAndPackageN result = proxyCache.cache.get(managedClassOrPackageName); } else { Set proxyInterfaceNames = new TreeSet<>(); - proxyInterfaceNames.add(HIBERNATE_PROXY_INTERFACENAME); //always added + proxyInterfaceNames.add(ClassNames.HIBERNATE_PROXY.toString()); //always added String proxy = proxyAnnotations.get(managedClassOrPackageName); if (proxy == null) { if (!proxyHelper.isProxiable(managedClassOrPackageName)) { diff --git a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateUserTypeProcessor.java b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateUserTypeProcessor.java index 3e47c390639ee8..31923da3643a70 100644 --- a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateUserTypeProcessor.java +++ b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateUserTypeProcessor.java @@ -18,9 +18,6 @@ public final class HibernateUserTypeProcessor { private static final String TYPE_VALUE = "type"; private static final String TYPE_CLASS_VALUE = "typeClass"; - public static final DotName TYPE = DotName.createSimple("org.hibernate.annotations.Type"); - public static final DotName TYPE_DEFINITION = DotName.createSimple("org.hibernate.annotations.TypeDef"); - public static final DotName TYPE_DEFINITIONS = DotName.createSimple("org.hibernate.annotations.TypeDefs"); @BuildStep public void build(BuildProducer reflectiveClass, CombinedIndexBuildItem combinedIndexBuildItem) { @@ -28,9 +25,9 @@ public void build(BuildProducer reflectiveClass, Combi final Set userTypes = new HashSet<>(); - Collection typeAnnotationInstances = index.getAnnotations(TYPE); - Collection typeDefinitionAnnotationInstances = index.getAnnotations(TYPE_DEFINITION); - Collection typeDefinitionsAnnotationInstances = index.getAnnotations(TYPE_DEFINITIONS); + Collection typeAnnotationInstances = index.getAnnotations(ClassNames.TYPE); + Collection typeDefinitionAnnotationInstances = index.getAnnotations(ClassNames.TYPE_DEFINITION); + Collection typeDefinitionsAnnotationInstances = index.getAnnotations(ClassNames.TYPE_DEFINITIONS); userTypes.addAll(getUserTypes(typeDefinitionAnnotationInstances)); diff --git a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/JpaJandexScavenger.java b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/JpaJandexScavenger.java index 1d15ac09263493..d7c0b639302334 100644 --- a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/JpaJandexScavenger.java +++ b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/JpaJandexScavenger.java @@ -34,14 +34,7 @@ */ public final class JpaJandexScavenger { - public static final DotName JPA_ENTITY = DotName.createSimple("javax.persistence.Entity"); - public static final DotName EMBEDDABLE = DotName.createSimple("javax.persistence.Embeddable"); - public static final List EMBEDDED_ANNOTATIONS = Arrays.asList( - DotName.createSimple("javax.persistence.Embedded"), - DotName.createSimple("javax.persistence.ElementCollection")); - public static final DotName MAPPED_SUPERCLASS = DotName.createSimple("javax.persistence.MappedSuperclass"); - - public static final DotName ENUM = DotName.createSimple("java.lang.Enum"); + public static final List EMBEDDED_ANNOTATIONS = Arrays.asList(ClassNames.EMBEDDED, ClassNames.ELEMENT_COLLECTION); private final List explicitDescriptors; private final BuildProducer reflectiveClass; @@ -72,11 +65,12 @@ public JpaEntitiesBuildItem discoverModelAndRegisterForReflection() throws IOExc for (DotName packageAnnotation : HibernateOrmAnnotations.PACKAGE_ANNOTATIONS) { enlistJPAModelAnnotatedPackages(indexView, domainObjectCollector, packageAnnotation); } - enlistJPAModelClasses(indexView, domainObjectCollector, enumTypeCollector, javaTypeCollector, JPA_ENTITY, + enlistJPAModelClasses(indexView, domainObjectCollector, enumTypeCollector, javaTypeCollector, ClassNames.JPA_ENTITY, unindexedClasses); - enlistJPAModelClasses(indexView, domainObjectCollector, enumTypeCollector, javaTypeCollector, EMBEDDABLE, + enlistJPAModelClasses(indexView, domainObjectCollector, enumTypeCollector, javaTypeCollector, ClassNames.EMBEDDABLE, unindexedClasses); - enlistJPAModelClasses(indexView, domainObjectCollector, enumTypeCollector, javaTypeCollector, MAPPED_SUPERCLASS, + enlistJPAModelClasses(indexView, domainObjectCollector, enumTypeCollector, javaTypeCollector, + ClassNames.MAPPED_SUPERCLASS, unindexedClasses); enlistEmbeddedsAndElementCollections(indexView, domainObjectCollector, enumTypeCollector, javaTypeCollector, unindexedClasses); @@ -241,7 +235,7 @@ private static void addClassHierarchyToReflectiveList(IndexView index, JpaEntiti for (FieldInfo fieldInfo : classInfo.fields()) { DotName fieldType = fieldInfo.type().name(); ClassInfo fieldTypeClassInfo = index.getClassByName(fieldType); - if (fieldTypeClassInfo != null && ENUM.equals(fieldTypeClassInfo.superName())) { + if (fieldTypeClassInfo != null && ClassNames.ENUM.equals(fieldTypeClassInfo.superName())) { enumTypeCollector.add(fieldType.toString()); } } @@ -268,7 +262,7 @@ private static void collectPackage(JpaEntitiesBuildItem domainObjectCollector, C } private static void collectDomainObject(JpaEntitiesBuildItem domainObjectCollector, ClassInfo modelClass) { - if (modelClass.classAnnotation(JPA_ENTITY) != null) { + if (modelClass.classAnnotation(ClassNames.JPA_ENTITY) != null) { domainObjectCollector.addEntityClass(modelClass.name().toString()); } else { domainObjectCollector.addModelClass(modelClass.name().toString()); diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/ConstantsTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/ConstantsTest.java deleted file mode 100644 index 0ae7c70b4f583e..00000000000000 --- a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/ConstantsTest.java +++ /dev/null @@ -1,114 +0,0 @@ -package io.quarkus.hibernate.orm; - -import java.util.Collection; -import java.util.Optional; - -import javax.persistence.ElementCollection; -import javax.persistence.Embeddable; -import javax.persistence.Embedded; -import javax.persistence.Entity; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.MappedSuperclass; -import javax.persistence.metamodel.StaticMetamodel; - -import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.hibernate.dialect.DB297Dialect; -import org.hibernate.dialect.DerbyTenSevenDialect; -import org.hibernate.dialect.MariaDB103Dialect; -import org.hibernate.dialect.MySQL8Dialect; -import org.hibernate.dialect.SQLServer2012Dialect; -import org.hibernate.proxy.HibernateProxy; -import org.jboss.jandex.DotName; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import io.quarkus.datasource.common.runtime.DatabaseKind; -import io.quarkus.hibernate.orm.deployment.Dialects; -import io.quarkus.hibernate.orm.deployment.HibernateOrmCdiProcessor; -import io.quarkus.hibernate.orm.deployment.HibernateOrmProcessor; -import io.quarkus.hibernate.orm.deployment.HibernateUserTypeProcessor; -import io.quarkus.hibernate.orm.deployment.JpaJandexScavenger; -import io.quarkus.hibernate.orm.runtime.dialect.QuarkusH2Dialect; -import io.quarkus.hibernate.orm.runtime.dialect.QuarkusPostgreSQL10Dialect; - -public class ConstantsTest { - - @Test - public void testConstantsInHibernateOrmProcessor() { - assertMatch(HibernateOrmProcessor.TENANT_CONNECTION_RESOLVER, - io.quarkus.hibernate.orm.runtime.tenant.TenantConnectionResolver.class); - assertMatch(HibernateOrmProcessor.TENANT_RESOLVER, io.quarkus.hibernate.orm.runtime.tenant.TenantResolver.class); - assertMatch(HibernateOrmProcessor.STATIC_METAMODEL, StaticMetamodel.class); - assertMatch(HibernateOrmProcessor.PERSISTENCE_UNIT, PersistenceUnit.class); - assertMatch(HibernateOrmProcessor.PERSISTENCE_UNIT_REPEATABLE_CONTAINER, PersistenceUnit.List.class); - assertMatch(HibernateOrmProcessor.JPA_ENTITY, javax.persistence.Entity.class); - assertMatch(HibernateOrmProcessor.MAPPED_SUPERCLASS, javax.persistence.MappedSuperclass.class); - assertMatch(HibernateOrmProcessor.PROXY, org.hibernate.annotations.Proxy.class); - Assertions.assertEquals(HibernateProxy.class.getName(), HibernateOrmProcessor.HIBERNATE_PROXY_INTERFACENAME); - } - - @Test - public void testHibernateUserTypeProcessor() { - assertMatch(HibernateUserTypeProcessor.TYPE, org.hibernate.annotations.Type.class); - assertMatch(HibernateUserTypeProcessor.TYPE_DEFINITION, org.hibernate.annotations.TypeDef.class); - assertMatch(HibernateUserTypeProcessor.TYPE_DEFINITIONS, org.hibernate.annotations.TypeDefs.class); - } - - @Test - public void testHibernateOrmCdiProcessor() { - assertMatch(HibernateOrmCdiProcessor.SESSION_FACTORY_EXPOSED_TYPES, EntityManagerFactory.class, SessionFactory.class); - assertMatch(HibernateOrmCdiProcessor.SESSION_EXPOSED_TYPES, EntityManager.class, Session.class); - assertMatch(HibernateOrmCdiProcessor.PERSISTENCE_UNIT_QUALIFIER, PersistenceUnit.class); - assertMatch(HibernateOrmCdiProcessor.JPA_PERSISTENCE_UNIT, javax.persistence.PersistenceUnit.class); - assertMatch(HibernateOrmCdiProcessor.JPA_PERSISTENCE_CONTEXT, javax.persistence.PersistenceContext.class); - } - - @Test - public void testJpaJandexScavenger() { - assertMatch(JpaJandexScavenger.JPA_ENTITY, Entity.class); - assertMatch(JpaJandexScavenger.EMBEDDABLE, Embeddable.class); - assertMatch(JpaJandexScavenger.EMBEDDED_ANNOTATIONS, Embedded.class, ElementCollection.class); - assertMatch(JpaJandexScavenger.MAPPED_SUPERCLASS, MappedSuperclass.class); - assertMatch(JpaJandexScavenger.ENUM, Enum.class); - } - - @Test - public void testDialectNames() { - assertDialectMatch(DatabaseKind.DB2, DB297Dialect.class); - assertDialectMatch(DatabaseKind.POSTGRESQL, QuarkusPostgreSQL10Dialect.class); - assertDialectMatch(DatabaseKind.H2, QuarkusH2Dialect.class); - assertDialectMatch(DatabaseKind.MARIADB, MariaDB103Dialect.class); - assertDialectMatch(DatabaseKind.MYSQL, MySQL8Dialect.class); - assertDialectMatch(DatabaseKind.DERBY, DerbyTenSevenDialect.class); - assertDialectMatch(DatabaseKind.MSSQL, SQLServer2012Dialect.class); - } - - private void assertDialectMatch(String dbName, Class dialectClass) { - final Optional guessDialect = Dialects.guessDialect(dbName); - Assertions.assertTrue(guessDialect.isPresent()); - Assertions.assertEquals(dialectClass.getName(), guessDialect.get()); - } - - private void assertMatch(final DotName dotName, final Class clazz) { - Assertions.assertEquals(dotName.toString(), clazz.getName()); - } - - private void assertMatch(final Collection dotNames, final Class... clazzez) { - Assertions.assertEquals(dotNames.size(), clazzez.length); - for (Class c : clazzez) { - Assertions.assertTrue(oneMatches(dotNames, c)); - } - } - - private boolean oneMatches(Collection dotNames, Class c) { - int matches = 0; - for (DotName d : dotNames) { - if (c.getName().equals(d.toString())) - matches++; - } - return 1 == matches; - } - -} diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/deployment/ConstantsTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/deployment/ConstantsTest.java new file mode 100644 index 00000000000000..0fd5e540c08d4f --- /dev/null +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/deployment/ConstantsTest.java @@ -0,0 +1,52 @@ +package io.quarkus.hibernate.orm.deployment; + +import static org.assertj.core.api.Assertions.assertThatCode; + +import java.util.Optional; +import java.util.Set; + +import org.hibernate.dialect.DB297Dialect; +import org.hibernate.dialect.DerbyTenSevenDialect; +import org.hibernate.dialect.MariaDB103Dialect; +import org.hibernate.dialect.MySQL8Dialect; +import org.hibernate.dialect.SQLServer2012Dialect; +import org.jboss.jandex.DotName; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import io.quarkus.datasource.common.runtime.DatabaseKind; +import io.quarkus.hibernate.orm.runtime.dialect.QuarkusH2Dialect; +import io.quarkus.hibernate.orm.runtime.dialect.QuarkusPostgreSQL10Dialect; + +public class ConstantsTest { + @ParameterizedTest + @MethodSource("provideConstantsToTest") + void testClassNameRefersToExistingClass(DotName constant) { + assertThatCode(() -> getClass().getClassLoader().loadClass(constant.toString())) + .doesNotThrowAnyException(); + } + + private static Set provideConstantsToTest() { + return ClassNames.CREATED_CONSTANTS; + } + + @Test + public void testDialectNames() { + assertDialectMatch(DatabaseKind.DB2, DB297Dialect.class); + assertDialectMatch(DatabaseKind.POSTGRESQL, QuarkusPostgreSQL10Dialect.class); + assertDialectMatch(DatabaseKind.H2, QuarkusH2Dialect.class); + assertDialectMatch(DatabaseKind.MARIADB, MariaDB103Dialect.class); + assertDialectMatch(DatabaseKind.MYSQL, MySQL8Dialect.class); + assertDialectMatch(DatabaseKind.DERBY, DerbyTenSevenDialect.class); + assertDialectMatch(DatabaseKind.MSSQL, SQLServer2012Dialect.class); + } + + private void assertDialectMatch(String dbName, Class dialectClass) { + final Optional guessDialect = Dialects.guessDialect(dbName); + Assertions.assertTrue(guessDialect.isPresent()); + Assertions.assertEquals(dialectClass.getName(), guessDialect.get()); + } + +}