From 9a58fe80288c62252b59eefc2819ad22aa4ff67b Mon Sep 17 00:00:00 2001 From: Vincent Bouthinon Date: Fri, 23 Jun 2023 17:36:54 +0200 Subject: [PATCH] HHH-16840 Allow serialization of ScanResultImpl --- .../internal/ByteArrayInputStreamAccess.java | 3 +- .../internal/FileInputStreamAccess.java | 3 +- .../internal/UrlInputStreamAccess.java | 3 +- .../scan/internal/ClassDescriptorImpl.java | 4 +- .../internal/MappingFileDescriptorImpl.java | 4 +- .../scan/internal/PackageDescriptorImpl.java | 4 +- .../archive/scan/internal/ScanResultImpl.java | 3 +- .../test/bootstrap/scanning/ScannerTest.java | 66 +++++++++++++++---- 8 files changed, 72 insertions(+), 18 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/ByteArrayInputStreamAccess.java b/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/ByteArrayInputStreamAccess.java index 29fa89c0fe..627792d2b3 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/ByteArrayInputStreamAccess.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/ByteArrayInputStreamAccess.java @@ -8,6 +8,7 @@ package org.hibernate.boot.archive.internal; import java.io.ByteArrayInputStream; import java.io.InputStream; +import java.io.Serializable; import org.hibernate.boot.archive.spi.InputStreamAccess; @@ -16,7 +17,7 @@ import org.hibernate.boot.archive.spi.InputStreamAccess; * * @author Steve Ebersole */ -public class ByteArrayInputStreamAccess implements InputStreamAccess { +public class ByteArrayInputStreamAccess implements InputStreamAccess, Serializable { private final String name; private final byte[] bytes; diff --git a/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/FileInputStreamAccess.java b/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/FileInputStreamAccess.java index a205f10d67..1037093439 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/FileInputStreamAccess.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/FileInputStreamAccess.java @@ -11,6 +11,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; +import java.io.Serializable; import org.hibernate.HibernateException; import org.hibernate.boot.archive.spi.ArchiveException; @@ -21,7 +22,7 @@ import org.hibernate.boot.archive.spi.InputStreamAccess; * * @author Steve Ebersole */ -public class FileInputStreamAccess implements InputStreamAccess { +public class FileInputStreamAccess implements InputStreamAccess, Serializable { private final String name; private final File file; diff --git a/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/UrlInputStreamAccess.java b/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/UrlInputStreamAccess.java index e1735a4265..28e6df6a0c 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/UrlInputStreamAccess.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/archive/internal/UrlInputStreamAccess.java @@ -7,6 +7,7 @@ package org.hibernate.boot.archive.internal; import java.io.InputStream; +import java.io.Serializable; import java.net.URL; import org.hibernate.HibernateException; @@ -15,7 +16,7 @@ import org.hibernate.boot.archive.spi.InputStreamAccess; /** * @author Steve Ebersole */ -public class UrlInputStreamAccess implements InputStreamAccess { +public class UrlInputStreamAccess implements InputStreamAccess, Serializable { private final URL url; public UrlInputStreamAccess(URL url) { diff --git a/hibernate-core/src/main/java/org/hibernate/boot/archive/scan/internal/ClassDescriptorImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/archive/scan/internal/ClassDescriptorImpl.java index 1ce4d27f67..5198e37eeb 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/archive/scan/internal/ClassDescriptorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/archive/scan/internal/ClassDescriptorImpl.java @@ -6,13 +6,15 @@ */ package org.hibernate.boot.archive.scan.internal; +import java.io.Serializable; + import org.hibernate.boot.archive.scan.spi.ClassDescriptor; import org.hibernate.boot.archive.spi.InputStreamAccess; /** * @author Steve Ebersole */ -public class ClassDescriptorImpl implements ClassDescriptor { +public class ClassDescriptorImpl implements ClassDescriptor, Serializable { private final String name; private final Categorization categorization; private final InputStreamAccess streamAccess; diff --git a/hibernate-core/src/main/java/org/hibernate/boot/archive/scan/internal/MappingFileDescriptorImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/archive/scan/internal/MappingFileDescriptorImpl.java index 83866d26d7..c532d3a07b 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/archive/scan/internal/MappingFileDescriptorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/archive/scan/internal/MappingFileDescriptorImpl.java @@ -6,13 +6,15 @@ */ package org.hibernate.boot.archive.scan.internal; +import java.io.Serializable; + import org.hibernate.boot.archive.spi.InputStreamAccess; import org.hibernate.boot.archive.scan.spi.MappingFileDescriptor; /** * @author Steve Ebersole */ -public class MappingFileDescriptorImpl implements MappingFileDescriptor { +public class MappingFileDescriptorImpl implements MappingFileDescriptor, Serializable { private final String name; private final InputStreamAccess streamAccess; diff --git a/hibernate-core/src/main/java/org/hibernate/boot/archive/scan/internal/PackageDescriptorImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/archive/scan/internal/PackageDescriptorImpl.java index c613dc7acd..2424d6880e 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/archive/scan/internal/PackageDescriptorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/archive/scan/internal/PackageDescriptorImpl.java @@ -6,13 +6,15 @@ */ package org.hibernate.boot.archive.scan.internal; +import java.io.Serializable; + import org.hibernate.boot.archive.spi.InputStreamAccess; import org.hibernate.boot.archive.scan.spi.PackageDescriptor; /** * @author Steve Ebersole */ -public class PackageDescriptorImpl implements PackageDescriptor { +public class PackageDescriptorImpl implements PackageDescriptor, Serializable { private final String name; private final InputStreamAccess streamAccess; diff --git a/hibernate-core/src/main/java/org/hibernate/boot/archive/scan/internal/ScanResultImpl.java b/hibernate-core/src/main/java/org/hibernate/boot/archive/scan/internal/ScanResultImpl.java index 6dd87a99b4..1bfef896ce 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/archive/scan/internal/ScanResultImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/archive/scan/internal/ScanResultImpl.java @@ -6,6 +6,7 @@ */ package org.hibernate.boot.archive.scan.internal; +import java.io.Serializable; import java.util.Set; import org.hibernate.boot.archive.scan.spi.ClassDescriptor; @@ -17,7 +18,7 @@ import org.hibernate.boot.archive.scan.spi.ScanResult; /** * @author Steve Ebersole */ -public class ScanResultImpl implements ScanResult { +public class ScanResultImpl implements ScanResult, Serializable { private final Set packageDescriptorSet; private final Set classDescriptorSet; private final Set mappingFileSet; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/ScannerTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/ScannerTest.java index 2d0773e6c0..e539ed6002 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/ScannerTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/bootstrap/scanning/ScannerTest.java @@ -6,11 +6,13 @@ */ package org.hibernate.orm.test.bootstrap.scanning; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + import java.io.File; import java.io.InputStream; -import java.util.HashMap; +import java.io.Serializable; import java.util.Map; - import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.Persistence; @@ -24,20 +26,16 @@ import org.hibernate.boot.archive.scan.spi.ScanOptions; import org.hibernate.boot.archive.scan.spi.ScanResult; import org.hibernate.boot.archive.scan.spi.Scanner; import org.hibernate.cfg.AvailableSettings; +import org.hibernate.internal.util.SerializationHelper; import org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor; import org.hibernate.jpa.boot.internal.StandardJpaScanEnvironmentImpl; import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor; import org.hibernate.orm.test.jpa.pack.defaultpar.ApplicationServer; import org.hibernate.orm.test.jpa.pack.defaultpar.Version; - +import org.hibernate.testing.orm.junit.JiraKey; import org.hibernate.testing.util.ServiceRegistryUtil; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; - /** * @author Emmanuel Bernard @@ -87,12 +85,12 @@ public class ScannerTest extends PackagingTestCase { File defaultPar = buildDefaultPar(); File explicitPar = buildExplicitPar(); addPackageToClasspath( defaultPar, explicitPar ); - + EntityManagerFactory emf; CustomScanner.resetUsed(); final Map integration = ServiceRegistryUtil.createBaseSettings(); emf = Persistence.createEntityManagerFactory( "defaultpar", integration ); - assertTrue( ! CustomScanner.isUsed() ); + assertTrue( !CustomScanner.isUsed() ); emf.close(); CustomScanner.resetUsed(); @@ -108,7 +106,53 @@ public class ScannerTest extends PackagingTestCase { CustomScanner.resetUsed(); emf = Persistence.createEntityManagerFactory( "defaultpar", ServiceRegistryUtil.createBaseSettings() ); - assertTrue( ! CustomScanner.isUsed() ); + assertTrue( !CustomScanner.isUsed() ); emf.close(); } + + @Test + @JiraKey("HHH-16840") + public void testScanResultSerialization() throws Exception { + final File defaultPar = buildDefaultPar(); + addPackageToClasspath( defaultPar ); + + final PersistenceUnitDescriptor descriptor = new ParsedPersistenceXmlDescriptor( defaultPar.toURL() ); + final ScanEnvironment env = new StandardJpaScanEnvironmentImpl( descriptor ); + final ScanOptions options = new StandardScanOptions( "hbm,class", descriptor.isExcludeUnlistedClasses() ); + final Scanner scanner = new StandardScanner(); + final ScanResult scanResult = scanner.scan( + env, + options, + StandardScanParameters.INSTANCE + ); + + validateDefaultParScanResult( scanResult ); + + final ScanResult scanResultClone = (ScanResult) SerializationHelper.clone( (Serializable) scanResult ); + assertThat( scanResultClone ).isNotSameAs( scanResult ); + validateDefaultParScanResult( scanResultClone ); + } + + private void validateDefaultParScanResult(ScanResult scanResult) { + assertThat( scanResult ).isNotNull(); + + assertThat( scanResult.getLocatedClasses() ).hasSize( 3 ); + assertThat( scanResult.getLocatedClasses() ).allSatisfy( descriptor -> { + assertThat( descriptor.getStreamAccess() ).isNotNull(); + assertThat( descriptor.getCategorization() ).isNotNull(); + assertThat( descriptor.getName() ).isNotBlank(); + } ); + + assertThat( scanResult.getLocatedMappingFiles() ).hasSize( 2 ); + assertThat( scanResult.getLocatedMappingFiles() ).allSatisfy( descriptor -> { + assertThat( descriptor.getStreamAccess() ).isNotNull(); + assertThat( descriptor.getName() ).isNotBlank(); + } ); + + assertThat( scanResult.getLocatedPackages() ).hasSize( 1 ); + assertThat( scanResult.getLocatedPackages() ).allSatisfy( descriptor -> { + assertThat( descriptor.getStreamAccess() ).isNotNull(); + assertThat( descriptor.getName() ).isNotBlank(); + } ); + } }