From 6f44b40652b582a86276a3eb6472a486647e560f Mon Sep 17 00:00:00 2001 From: Sanne Grinovero <sanne@hibernate.org> Date: Tue, 1 Jun 2021 19:18:43 +0100 Subject: [PATCH] Fail deployment on presence of an hibernate.properties resource --- .../orm/deployment/HibernateOrmProcessor.java | 19 +++++++++ .../orm/config/NoHibernatePropertiesTest.java | 42 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/NoHibernatePropertiesTest.java 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 85847a7908842..c69513496c5b2 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 @@ -9,11 +9,13 @@ import static org.hibernate.cfg.AvailableSettings.USE_SECOND_LEVEL_CACHE; import java.io.IOException; +import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashSet; @@ -423,6 +425,7 @@ public void build(RecorderContext recorderContext, HibernateOrmRecorder recorder BuildProducer<BeanContainerListenerBuildItem> beanContainerListener) throws Exception { feature.produce(new FeatureBuildItem(Feature.HIBERNATE_ORM)); + validateHibernatePropertiesNotUsed(); final boolean enableORM = hasEntities(jpaModel); final boolean hibernateReactivePresent = capabilities.isPresent(Capability.HIBERNATE_REACTIVE); @@ -472,6 +475,22 @@ public void build(RecorderContext recorderContext, HibernateOrmRecorder recorder proxyDefinitions.getProxies()))); } + private void validateHibernatePropertiesNotUsed() { + try { + final Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources( + "hibernate.properties"); + if (resources.hasMoreElements()) { + final URL url = resources.nextElement(); + throw new IllegalStateException( + "The Hibernate ORM configuration in Quarkus does not support sourcing configuration properties from resources named `hibernate.properties`," + + " and this is now expressly prohibited as such a file could lead to unpredictable semantics. Please remove it from `" + + url.toExternalForm() + '`'); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + @BuildStep void handleNativeImageImportSql(BuildProducer<NativeImageResourceBuildItem> resources, List<PersistenceUnitDescriptorBuildItem> descriptors, diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/NoHibernatePropertiesTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/NoHibernatePropertiesTest.java new file mode 100644 index 0000000000000..bbc40d8c0b08a --- /dev/null +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/NoHibernatePropertiesTest.java @@ -0,0 +1,42 @@ +package io.quarkus.hibernate.orm.config; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; + +/** + * Since we decided that we're no longer supporting to read an hibernate.properties resource, + * let's also test that this is made explicit. + * N.B. while we no longer parse the file during boot, there are other components in Hibernate ORM + * that look for it so this would lead to inconsistencies. + */ +public class NoHibernatePropertiesTest { + + @RegisterExtension + static QuarkusUnitTest runner = new QuarkusUnitTest() + .assertException(t -> { + assertThat(t) + .isInstanceOf(IllegalStateException.class) + .hasMessageContainingAll( + "The Hibernate ORM configuration in Quarkus does not support sourcing configuration properties from resources named `hibernate.properties`"); + }) + .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) + .addClass(MyEntity.class) + .addAsResource(new StringAsset(""), "hibernate.properties") + .addAsResource("application.properties")) + .overrideConfigKey("quarkus.datasource.devservices", "false"); + + @Test + public void testInvalidConfiguration() { + // deployment exception should happen first + Assertions.fail(); + } + +}