diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/schemagen/SchemaScriptFileGenerationTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/schemagen/SchemaScriptFileGenerationTest.java new file mode 100644 index 0000000000..b431df74fc --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/schemagen/SchemaScriptFileGenerationTest.java @@ -0,0 +1,109 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.jpa.test.schemagen; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Map; + +import org.hibernate.cfg.Environment; +import org.hibernate.jpa.boot.spi.Bootstrap; +import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder; +import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor; +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.junit.Before; +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +/** + * @author Andrea Boriero + */ +public class SchemaScriptFileGenerationTest { + private File createSchema; + private File dropSchema; + private EntityManagerFactoryBuilder entityManagerFactoryBuilder; + + @Before + public void setUp() throws IOException { + createSchema = File.createTempFile( "create_schema", ".sql" ); + dropSchema = File.createTempFile( "drop_schema", ".sql" ); + createSchema.deleteOnExit(); + dropSchema.deleteOnExit(); + + entityManagerFactoryBuilder = Bootstrap.getEntityManagerFactoryBuilder( + buildPersistenceUnitDescriptor(), + getConfig() + ); + } + + @Test + @TestForIssue(jiraKey = "10601") + public void testGenerateSchemaDoesNotProduceTheSameStatementTwice() throws Exception { + + entityManagerFactoryBuilder.generateSchema(); + + final String fileContent = new String( Files.readAllBytes( createSchema.toPath() ) ).toLowerCase(); + + assertThat( fileContent.contains( "create table test_entity" ), is( true ) ); + assertThat( + "The statement 'create table test_entity' is generated twice", + fileContent.replaceFirst( "create table test_entity", "" ).contains( "create table test_entity" ), + is( false ) + ); + + final String dropFileContent = new String( Files.readAllBytes( dropSchema.toPath() ) ).toLowerCase(); + assertThat( dropFileContent.contains( "drop table test_entity" ), is( true ) ); + assertThat( + "The statement 'drop table test_entity' is generated twice", + dropFileContent.replaceFirst( "drop table test_entity", "" ).contains( "drop table test_entity" ), + is( false ) + ); + } + + @Entity + @Table(name = "test_entity") + public static class TestEntity { + @Id + private String field; + + public String getField() { + return field; + } + + public void setField(String field) { + this.field = field; + } + } + + private PersistenceUnitDescriptor buildPersistenceUnitDescriptor() { + return new BaseEntityManagerFunctionalTestCase.TestingPersistenceUnitDescriptorImpl( getClass().getSimpleName() ); + } + + private Map getConfig() { + final Map config = Environment.getProperties(); + config.put( org.hibernate.cfg.AvailableSettings.HBM2DDL_SCRIPTS_CREATE_TARGET, createSchema.toPath() ); + config.put( org.hibernate.cfg.AvailableSettings.HBM2DDL_SCRIPTS_DROP_TARGET, dropSchema.toPath() ); + config.put( org.hibernate.cfg.AvailableSettings.HBM2DDL_SCRIPTS_ACTION, "drop-and-create" ); + ArrayList classes = new ArrayList(); + + classes.addAll( Arrays.asList( new Class[] {TestEntity.class} ) ); + config.put( org.hibernate.jpa.AvailableSettings.LOADED_CLASSES, classes ); + return config; + } + +}