HHH-17011 Add test for issue

This commit is contained in:
Andrea Boriero 2023-08-03 11:34:10 +02:00 committed by Andrea Boriero
parent b57fbb1245
commit db602cb535
1 changed files with 156 additions and 0 deletions

View File

@ -0,0 +1,156 @@
package org.hibernate.orm.test.schemaupdate;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.util.EnumSet;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
import org.hibernate.tool.schema.TargetType;
import org.hibernate.testing.orm.junit.JiraKey;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@JiraKey("HHH-17011")
public class LocalDateTimeAndEnumSchemaUpdateTest {
private File output;
private StandardServiceRegistry ssr;
private MetadataImplementor metadata;
@BeforeEach
public void setUp() throws IOException {
output = File.createTempFile( "update_script", ".sql" );
output.deleteOnExit();
ssr = new StandardServiceRegistryBuilder().build();
}
@AfterEach
public void tearsDown() {
dropDatabase( User.class );
output.delete();
StandardServiceRegistryBuilder.destroy( ssr );
}
@Test
public void testUpdateIsNotExecuted() throws Exception {
createSchema( User.class );
updateSchema( User.class );
String fileContent = new String( Files.readAllBytes( output.toPath() ) ).toLowerCase()
.replace( System.lineSeparator(), "" );
assertThat( fileContent ).isEmpty();
}
private void createSchema(Class... annotatedClasses) {
final MetadataSources metadataSources = new MetadataSources( ssr );
for ( Class c : annotatedClasses ) {
metadataSources.addAnnotatedClass( c );
}
metadata = (MetadataImplementor) metadataSources.buildMetadata();
metadata.orderColumns( false );
metadata.validate();
new SchemaExport()
.setHaltOnError( false )
.setFormat( false )
.create( EnumSet.of( TargetType.DATABASE ), metadata );
}
private void updateSchema(Class... annotatedClasses) {
final MetadataSources metadataSources = new MetadataSources( ssr );
for ( Class c : annotatedClasses ) {
metadataSources.addAnnotatedClass( c );
}
metadata = (MetadataImplementor) metadataSources.buildMetadata();
metadata.orderColumns( false );
metadata.validate();
new SchemaUpdate()
.setHaltOnError( true )
.setOutputFile( output.getAbsolutePath() )
.setFormat( false )
.execute( EnumSet.of( TargetType.SCRIPT, TargetType.DATABASE ), metadata );
}
private void dropDatabase(Class... annotatedClasses){
final MetadataSources metadataSources = new MetadataSources( ssr );
for ( Class c : annotatedClasses ) {
metadataSources.addAnnotatedClass( c );
}
metadata = (MetadataImplementor) metadataSources.buildMetadata();
metadata.orderColumns( false );
metadata.validate();
new SchemaExport()
.setHaltOnError( false )
.setFormat( false )
.drop( EnumSet.of( TargetType.DATABASE ), metadata );
}
@Entity(name = "User")
@Table(name = "`user`")
public static class User {
@Id
private Long id;
private String name;
private LocalDateTime birthday;
@Enumerated(EnumType.STRING)
private Gender gender;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDateTime getBirthday() {
return birthday;
}
public void setBirthday(LocalDateTime birthday) {
this.birthday = birthday;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
}
public enum Gender {
Male,
Female
}
}