Skip to content

Commit

Permalink
GH-970 - Prevent archiving JPA entity from being included in non-arch…
Browse files Browse the repository at this point in the history
…iving setups.\n\nFixes GH-970.
  • Loading branch information
odrotbohm committed Nov 25, 2024
1 parent 283e350 commit 43022fb
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -37,15 +37,15 @@
* @author Cora Iberkleid
*/
@MappedSuperclass
abstract class JpaEventPublication {
public abstract class JpaEventPublication {

final @Id @Column(length = 16) UUID id;
final Instant publicationDate;
final String listenerId;
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
Expand All @@ -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!");
Expand Down Expand Up @@ -128,30 +128,4 @@ public boolean equals(Object obj) {
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() {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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
Expand All @@ -29,5 +30,5 @@
*/
@AutoConfiguration
@AutoConfigureBefore({ HibernateJpaAutoConfiguration.class, EventPublicationAutoConfiguration.class })
@AutoConfigurationPackage
@AutoConfigurationPackage(basePackageClasses = DefaultJpaEventPublication.class)
class JpaEventPublicationAutoConfiguration extends JpaEventPublicationConfiguration {}
Original file line number Diff line number Diff line change
@@ -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() {}
}
Original file line number Diff line number Diff line change
@@ -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 {}
Original file line number Diff line number Diff line change
@@ -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() {}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.springframework.modulith.events.jpa.JpaEventPublicationAutoConfiguration
org.springframework.modulith.events.jpa.archiving.ArchivingAutoConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -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;
@Test // GH-10
void registersJpaEventPublicationPackageForAutoConfiguration() {
assertAutoConfigurationPackages(null, examplePackage, eventPublicationPackage);
}

JpaEventPublicationAutoConfigurationIntegrationTests(BeanFactory factory) {
this.factory = factory;
}
@Test // GH-964
void registersArchivingJpaEventPublicationPackageForAutoConfiguration() {
assertAutoConfigurationPackages("ARCHIVE", examplePackage, eventPublicationPackage, archivingPackage);
}

@Test // GH-10
void registersJpaEventPublicationPackageForAutoConfiguration() {
private void assertAutoConfigurationPackages(String propertyValue, String... packages) {

var runner = new ApplicationContextRunner();

var examplePackage = ExampleApplication.class.getPackageName();
var eventPublicationPackage = JpaEventPublication.class.getPackageName();
if (propertyValue != null) {
runner = runner.withPropertyValues(CompletionMode.PROPERTY + "=" + propertyValue);
}

assertThat(AutoConfigurationPackages.get(factory))
.containsExactlyInAnyOrder(examplePackage, eventPublicationPackage);
runner.withBean(EventSerializer.class, () -> mock(EventSerializer.class))
.withUserConfiguration(ExampleApplication.class)
.run(context -> {
assertThat(AutoConfigurationPackages.get(context)).containsExactlyInAnyOrder(packages);
});
}
}

0 comments on commit 43022fb

Please sign in to comment.