|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.test.schemaupdate; |
| 8 | + |
| 9 | +import javax.persistence.Entity; |
| 10 | +import javax.persistence.Id; |
| 11 | +import javax.persistence.Table; |
| 12 | +import java.io.File; |
| 13 | +import java.nio.file.Files; |
| 14 | + |
| 15 | +import org.hibernate.boot.MetadataSources; |
| 16 | +import org.hibernate.boot.registry.StandardServiceRegistry; |
| 17 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 18 | +import org.hibernate.boot.spi.MetadataImplementor; |
| 19 | +import org.hibernate.cfg.Environment; |
| 20 | +import org.hibernate.tool.hbm2ddl.SchemaUpdate; |
| 21 | + |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import org.hibernate.testing.TestForIssue; |
| 25 | + |
| 26 | +import static org.hamcrest.core.Is.is; |
| 27 | +import static org.junit.Assert.assertThat; |
| 28 | + |
| 29 | +/** |
| 30 | + * @author Andrea Boriero |
| 31 | + */ |
| 32 | +@TestForIssue(jiraKey = "10180") |
| 33 | +public class SchemaUpdateGeneratingOnlyScriptFileTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testSchemaUpdateScriptGeneration() throws Exception { |
| 37 | + StandardServiceRegistry ssr = new StandardServiceRegistryBuilder() |
| 38 | + .applySetting( Environment.HBM2DDL_AUTO, "none" ) |
| 39 | + .build(); |
| 40 | + try { |
| 41 | + File output = File.createTempFile( "update_script", ".sql" ); |
| 42 | + output.deleteOnExit(); |
| 43 | + |
| 44 | + final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) |
| 45 | + .addAnnotatedClass( TestEntity.class ) |
| 46 | + .buildMetadata(); |
| 47 | + metadata.validate(); |
| 48 | + |
| 49 | + SchemaUpdate su = new SchemaUpdate( ssr, metadata ); |
| 50 | + su.setHaltOnError( true ); |
| 51 | + su.setOutputFile( output.getAbsolutePath() ); |
| 52 | + su.setDelimiter( ";" ); |
| 53 | + su.setFormat( true ); |
| 54 | + su.execute( true, false ); |
| 55 | + |
| 56 | + String fileContent = new String( Files.readAllBytes( output.toPath() ) ); |
| 57 | + assertThat( fileContent.toLowerCase().contains( "create table test_entity" ), is( true ) ); |
| 58 | + } |
| 59 | + finally { |
| 60 | + StandardServiceRegistryBuilder.destroy( ssr ); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Entity |
| 65 | + @Table(name = "test_entity") |
| 66 | + public static class TestEntity { |
| 67 | + @Id |
| 68 | + private String field; |
| 69 | + |
| 70 | + public String getField() { |
| 71 | + return field; |
| 72 | + } |
| 73 | + |
| 74 | + public void setField(String field) { |
| 75 | + this.field = field; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments