GH-970 - Prevent archiving JPA entity from being included in non-archiving setups.
This commit is contained in:
@@ -16,15 +16,15 @@
|
||||
package org.springframework.modulith.events.jpa;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.modulith.events.jpa.archiving.ArchivedJpaEventPublication;
|
||||
import org.springframework.modulith.events.jpa.updating.DefaultJpaEventPublication;
|
||||
import org.springframework.modulith.events.support.CompletionMode;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -37,7 +37,7 @@ import org.springframework.util.Assert;
|
||||
* @author Cora Iberkleid
|
||||
*/
|
||||
@MappedSuperclass
|
||||
abstract class JpaEventPublication {
|
||||
public abstract class JpaEventPublication {
|
||||
|
||||
final @Id @Column(length = 16) UUID id;
|
||||
final Instant publicationDate;
|
||||
@@ -45,7 +45,7 @@ abstract class JpaEventPublication {
|
||||
final String serializedEvent;
|
||||
final Class<?> eventType;
|
||||
|
||||
Instant completionDate;
|
||||
protected Instant completionDate;
|
||||
|
||||
/**
|
||||
* Creates a new {@link JpaEventPublication} for the given publication date, listener id, serialized event and event
|
||||
@@ -56,7 +56,7 @@ abstract class JpaEventPublication {
|
||||
* @param serializedEvent must not be {@literal null} or empty.
|
||||
* @param eventType must not be {@literal null}.
|
||||
*/
|
||||
private JpaEventPublication(UUID id, Instant publicationDate, String listenerId, String serializedEvent,
|
||||
protected JpaEventPublication(UUID id, Instant publicationDate, String listenerId, String serializedEvent,
|
||||
Class<?> eventType) {
|
||||
|
||||
Assert.notNull(id, "Identifier must not be null!");
|
||||
@@ -128,30 +128,4 @@ abstract class JpaEventPublication {
|
||||
public int hashCode() {
|
||||
return id.hashCode();
|
||||
}
|
||||
|
||||
@Entity(name = "DefaultJpaEventPublication")
|
||||
@Table(name = "EVENT_PUBLICATION")
|
||||
private static class DefaultJpaEventPublication extends JpaEventPublication {
|
||||
|
||||
private DefaultJpaEventPublication(UUID id, Instant publicationDate, String listenerId, String serializedEvent,
|
||||
Class<?> eventType) {
|
||||
super(id, publicationDate, listenerId, serializedEvent, eventType);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
DefaultJpaEventPublication() {}
|
||||
}
|
||||
|
||||
@Entity(name = "ArchivedJpaEventPublication")
|
||||
@Table(name = "EVENT_PUBLICATION_ARCHIVE")
|
||||
private static class ArchivedJpaEventPublication extends JpaEventPublication {
|
||||
|
||||
private ArchivedJpaEventPublication(UUID id, Instant publicationDate, String listenerId, String serializedEvent,
|
||||
Class<?> eventType) {
|
||||
super(id, publicationDate, listenerId, serializedEvent, eventType);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
ArchivedJpaEventPublication() {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
||||
import org.springframework.modulith.events.config.EventPublicationAutoConfiguration;
|
||||
import org.springframework.modulith.events.jpa.updating.DefaultJpaEventPublication;
|
||||
|
||||
/**
|
||||
* Auto-configuration for JPA based event publication. Registers this class' package as auto-configuration package, so
|
||||
@@ -29,5 +30,5 @@ import org.springframework.modulith.events.config.EventPublicationAutoConfigurat
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@AutoConfigureBefore({ HibernateJpaAutoConfiguration.class, EventPublicationAutoConfiguration.class })
|
||||
@AutoConfigurationPackage
|
||||
@AutoConfigurationPackage(basePackageClasses = DefaultJpaEventPublication.class)
|
||||
class JpaEventPublicationAutoConfiguration extends JpaEventPublicationConfiguration {}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.modulith.events.jpa.archiving;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.modulith.events.jpa.JpaEventPublication;
|
||||
|
||||
@Entity(name = "ArchivedJpaEventPublication")
|
||||
@Table(name = "EVENT_PUBLICATION_ARCHIVE")
|
||||
public class ArchivedJpaEventPublication extends JpaEventPublication {
|
||||
|
||||
public ArchivedJpaEventPublication(UUID id, Instant publicationDate, String listenerId, String serializedEvent,
|
||||
Class<?> eventType) {
|
||||
super(id, publicationDate, listenerId, serializedEvent, eventType);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
ArchivedJpaEventPublication() {}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.modulith.events.jpa.archiving;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
||||
import org.springframework.modulith.events.config.EventPublicationAutoConfiguration;
|
||||
import org.springframework.modulith.events.support.CompletionMode;
|
||||
|
||||
/**
|
||||
* Auto-configuration adding the current package as auto-configuration package for {@link ArchivedJpaEventPublication}
|
||||
* to be included in the JPA setup.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
* @since 1.3.1
|
||||
*/
|
||||
@ConditionalOnProperty(name = CompletionMode.PROPERTY, havingValue = "archive")
|
||||
@AutoConfiguration
|
||||
@AutoConfigureBefore({ HibernateJpaAutoConfiguration.class, EventPublicationAutoConfiguration.class })
|
||||
@AutoConfigurationPackage
|
||||
class ArchivingAutoConfiguration {}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.modulith.events.jpa.updating;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.modulith.events.jpa.JpaEventPublication;
|
||||
|
||||
@Entity(name = "DefaultJpaEventPublication")
|
||||
@Table(name = "EVENT_PUBLICATION")
|
||||
public class DefaultJpaEventPublication extends JpaEventPublication {
|
||||
|
||||
public DefaultJpaEventPublication(UUID id, Instant publicationDate, String listenerId, String serializedEvent,
|
||||
Class<?> eventType) {
|
||||
super(id, publicationDate, listenerId, serializedEvent, eventType);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
DefaultJpaEventPublication() {}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
org.springframework.modulith.events.jpa.JpaEventPublicationAutoConfiguration
|
||||
org.springframework.modulith.events.jpa.archiving.ArchivingAutoConfiguration
|
||||
|
||||
@@ -16,42 +16,49 @@
|
||||
package org.springframework.modulith.events.jpa;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import example.ExampleApplication;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.modulith.events.core.EventSerializer;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestConstructor;
|
||||
import org.springframework.test.context.TestConstructor.AutowireMode;
|
||||
import org.springframework.modulith.events.jpa.archiving.ArchivedJpaEventPublication;
|
||||
import org.springframework.modulith.events.jpa.updating.DefaultJpaEventPublication;
|
||||
import org.springframework.modulith.events.support.CompletionMode;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
@SpringBootTest
|
||||
@ContextConfiguration(classes = ExampleApplication.class)
|
||||
@TestConstructor(autowireMode = AutowireMode.ALL)
|
||||
class JpaEventPublicationAutoConfigurationIntegrationTests {
|
||||
|
||||
private final BeanFactory factory;
|
||||
String examplePackage = ExampleApplication.class.getPackageName();
|
||||
String eventPublicationPackage = DefaultJpaEventPublication.class.getPackageName();
|
||||
String archivingPackage = ArchivedJpaEventPublication.class.getPackageName();
|
||||
|
||||
@MockitoBean EventSerializer serializer;
|
||||
|
||||
JpaEventPublicationAutoConfigurationIntegrationTests(BeanFactory factory) {
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
@Test // GH-10
|
||||
@Test // GH-10
|
||||
void registersJpaEventPublicationPackageForAutoConfiguration() {
|
||||
assertAutoConfigurationPackages(null, examplePackage, eventPublicationPackage);
|
||||
}
|
||||
|
||||
var examplePackage = ExampleApplication.class.getPackageName();
|
||||
var eventPublicationPackage = JpaEventPublication.class.getPackageName();
|
||||
@Test // GH-964
|
||||
void registersArchivingJpaEventPublicationPackageForAutoConfiguration() {
|
||||
assertAutoConfigurationPackages("ARCHIVE", examplePackage, eventPublicationPackage, archivingPackage);
|
||||
}
|
||||
|
||||
assertThat(AutoConfigurationPackages.get(factory))
|
||||
.containsExactlyInAnyOrder(examplePackage, eventPublicationPackage);
|
||||
private void assertAutoConfigurationPackages(String propertyValue, String... packages) {
|
||||
|
||||
var runner = new ApplicationContextRunner();
|
||||
|
||||
if (propertyValue != null) {
|
||||
runner = runner.withPropertyValues(CompletionMode.PROPERTY + "=" + propertyValue);
|
||||
}
|
||||
|
||||
runner.withBean(EventSerializer.class, () -> mock(EventSerializer.class))
|
||||
.withUserConfiguration(ExampleApplication.class)
|
||||
.run(context -> {
|
||||
assertThat(AutoConfigurationPackages.get(context)).containsExactlyInAnyOrder(packages);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user