HHH-16840 Allow serialization of ScanResultImpl

This commit is contained in:
Vincent Bouthinon 2023-06-23 17:36:54 +02:00 committed by Steve Ebersole
parent 96a000e8ab
commit 9a58fe8028
8 changed files with 72 additions and 18 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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) {

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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<PackageDescriptor> packageDescriptorSet;
private final Set<ClassDescriptor> classDescriptorSet;
private final Set<MappingFileDescriptor> mappingFileSet;

View File

@ -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<String, Object> 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();
} );
}
}