Skip to content

Commit

Permalink
Fail deployment on presence of an hibernate.properties resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanne committed Jun 1, 2021
1 parent c27fe87 commit 6f44b40
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}

0 comments on commit 6f44b40

Please sign in to comment.