forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add build-time configuration property quarkus.hibernate-envers.active
- Loading branch information
Showing
8 changed files
with
214 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...st/java/io/quarkus/hibernate/orm/envers/config/ConfigActiveFalseAndAuditedEntityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.quarkus.hibernate.orm.envers.config; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import javax.inject.Inject; | ||
import javax.persistence.metamodel.Bindable; | ||
|
||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.envers.AuditReaderFactory; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.orm.envers.MyAuditedEntity; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ConfigActiveFalseAndAuditedEntityTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar.addClass(MyAuditedEntity.class)) | ||
.withConfigurationResource("application.properties") | ||
.overrideConfigKey("quarkus.hibernate-envers.active", "false"); | ||
|
||
@Inject | ||
SessionFactory sessionFactory; | ||
|
||
@Test | ||
public void test() { | ||
assertThat(sessionFactory.getMetamodel().getEntities()) | ||
.extracting(Bindable::getBindableJavaType) | ||
// In particular this should not contain the revision entity | ||
.containsExactlyInAnyOrder((Class) MyAuditedEntity.class); | ||
|
||
try (Session session = sessionFactory.openSession()) { | ||
assertThatThrownBy(() -> AuditReaderFactory.get(session).isEntityClassAudited(MyAuditedEntity.class)) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessageContaining("Service is not yet initialized"); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...test/java/io/quarkus/hibernate/orm/envers/config/ConfigEnabledFalseAndActiveTrueTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.quarkus.hibernate.orm.envers.config; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import javax.inject.Inject; | ||
import javax.persistence.metamodel.Bindable; | ||
|
||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.envers.AuditReaderFactory; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.orm.envers.MyAuditedEntity; | ||
import io.quarkus.runtime.configuration.ConfigurationException; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ConfigEnabledFalseAndActiveTrueTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar.addClass(MyAuditedEntity.class)) | ||
.withConfigurationResource("application.properties") | ||
.overrideConfigKey("quarkus.hibernate-envers.enabled", "false") | ||
.overrideConfigKey("quarkus.hibernate-envers.active", "true") | ||
.assertException(throwable -> assertThat(throwable) | ||
.isInstanceOf(ConfigurationException.class) | ||
.hasMessageContaining( | ||
"Hibernate Envers activated explicitly for persistence unit '<default>', but the Hibernate Envers extension was disabled at build time", | ||
"If you want Hibernate Envers to be active for this persistence unit, you must set 'quarkus.hibernate-envers.enabled' to 'true' at build time", | ||
"If you don't want Hibernate Envers to be active for this persistence unit, you must leave 'quarkus.hibernate-envers.active' unset or set it to 'false'")); | ||
|
||
@Inject | ||
SessionFactory sessionFactory; | ||
|
||
@Test | ||
public void test() { | ||
assertThat(sessionFactory.getMetamodel().getEntities()) | ||
.extracting(Bindable::getBindableJavaType) | ||
// In particular this should not contain the revision entity | ||
.containsExactlyInAnyOrder((Class) MyAuditedEntity.class); | ||
|
||
try (Session session = sessionFactory.openSession()) { | ||
assertThatThrownBy(() -> AuditReaderFactory.get(session).isEntityClassAudited(MyAuditedEntity.class)) | ||
.isInstanceOf(IllegalStateException.class) | ||
.hasMessageContaining("Service is not yet initialized"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
.../main/java/io/quarkus/hibernate/envers/HibernateEnversBuildTimeConfigPersistenceUnit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.quarkus.hibernate.envers; | ||
|
||
import java.util.Optional; | ||
|
||
import io.quarkus.runtime.annotations.ConfigGroup; | ||
import io.quarkus.runtime.annotations.ConfigItem; | ||
|
||
@ConfigGroup | ||
public class HibernateEnversBuildTimeConfigPersistenceUnit { | ||
|
||
/** | ||
* Whether Hibernate Envers should be active for this persistence unit at runtime. | ||
* | ||
* If Hibernate Envers is not active, the audit entities will *still* be added to the Hibernate ORM metamodel | ||
* and to the database schema that Hibernate ORM generates: | ||
* you would need to disable Hibernate Envers at build time (i.e. set `quarkus.hibernate-envers.enabled` to `false`) | ||
* in order to avoid that. | ||
* However, when Hibernate Envers is not active, it will not process entity change events | ||
* nor create new versions of entities. | ||
* and accessing the AuditReader through AuditReaderFactory will not be possible. | ||
* | ||
* Note that if Hibernate Envers is disabled (i.e. `quarkus.hibernate-envers.enabled` is set to `false`), | ||
* it won't be active for any persistence unit, and setting this property to `true` will fail. | ||
* | ||
* @asciidoclet | ||
*/ | ||
@ConfigItem(defaultValueDocumentation = "`true` if Hibernate ORM is enabled; `false` otherwise") | ||
public Optional<Boolean> active = Optional.empty(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters