From 3267a9221fe4924dd46e54dfba1a1bd93c10e957 Mon Sep 17 00:00:00 2001 From: Koen Aers Date: Fri, 4 Dec 2015 18:33:26 +0100 Subject: [PATCH] HHH-10158: SchemaUpdate does not properly support formatter and delimiter anymore --- .../hibernate/tool/hbm2ddl/SchemaUpdate.java | 8 +- .../tool/schema/internal/TargetFileImpl.java | 15 +++- .../schema/internal/TargetStdoutImpl.java | 13 ++- .../SchemaUpdateDelimiterTest.java | 2 +- .../SchemaUpdateFormatterTest.java | 80 +++++++++++++++++++ .../test/tool/schema/TargetFileImplTest.java | 46 +++++++++++ .../tool/schema/TargetStdoutImplTest.java | 50 ++++++++++++ 7 files changed, 208 insertions(+), 6 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateFormatterTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/tool/schema/TargetFileImplTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/tool/schema/TargetStdoutImplTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java index 45d12f0219..90e40ef05d 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java @@ -27,6 +27,8 @@ import org.hibernate.boot.spi.MetadataImplementor; import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; +import org.hibernate.engine.jdbc.internal.FormatStyle; +import org.hibernate.engine.jdbc.internal.Formatter; import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreMessageLogger; @@ -56,6 +58,7 @@ public class SchemaUpdate { private final List exceptions = new ArrayList(); private String outputFile; private String delimiter; + private Formatter formatter; /** * Creates a SchemaUpdate object. This form is intended for use from tooling @@ -134,7 +137,7 @@ public class SchemaUpdate { List toolTargets = new ArrayList(); if ( target.doScript() ) { - toolTargets.add( new TargetStdoutImpl( delimiter ) ); + toolTargets.add( new TargetStdoutImpl( delimiter, formatter ) ); } if ( target.doExport() ) { @@ -143,7 +146,7 @@ public class SchemaUpdate { if ( outputFile != null ) { LOG.writingGeneratedSchemaToFile( outputFile ); - toolTargets.add( new TargetFileImpl( outputFile, delimiter ) ); + toolTargets.add( new TargetFileImpl( outputFile, delimiter, formatter ) ); } return toolTargets; @@ -162,6 +165,7 @@ public class SchemaUpdate { } public void setFormat(boolean format) { + formatter = (format ? FormatStyle.DDL : FormatStyle.NONE).getFormatter(); } public void setOutputFile(String outputFile) { diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/TargetFileImpl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/TargetFileImpl.java index 65b0982191..ad964ef945 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/TargetFileImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/TargetFileImpl.java @@ -9,6 +9,8 @@ package org.hibernate.tool.schema.internal; import java.io.FileWriter; import java.io.IOException; +import org.hibernate.engine.jdbc.internal.FormatStyle; +import org.hibernate.engine.jdbc.internal.Formatter; import org.hibernate.tool.schema.spi.SchemaManagementException; import org.hibernate.tool.schema.spi.Target; @@ -18,11 +20,17 @@ import org.hibernate.tool.schema.spi.Target; public class TargetFileImpl implements Target { final private FileWriter fileWriter; final private String delimiter; + final private Formatter formatter; - public TargetFileImpl(String outputFile, String delimter) { + public TargetFileImpl(String outputFile, String delimiter) { + this( outputFile, delimiter, FormatStyle.NONE.getFormatter()); + } + + public TargetFileImpl(String outputFile, String delimiter, Formatter formatter) { try { - this.delimiter = delimter; + this.delimiter = delimiter; this.fileWriter = new FileWriter( outputFile ); + this.formatter = formatter; } catch (IOException e) { throw new SchemaManagementException( "Unable to open FileWriter [" + outputFile + "]", e ); @@ -41,6 +49,9 @@ public class TargetFileImpl implements Target { @Override public void accept(String action) { try { + if (formatter != null) { + action = formatter.format(action); + } fileWriter.write( action ); if ( delimiter != null ) { fileWriter.write( delimiter ); diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/TargetStdoutImpl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/TargetStdoutImpl.java index 6da44666db..6a75afea40 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/TargetStdoutImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/TargetStdoutImpl.java @@ -6,6 +6,8 @@ */ package org.hibernate.tool.schema.internal; +import org.hibernate.engine.jdbc.internal.FormatStyle; +import org.hibernate.engine.jdbc.internal.Formatter; import org.hibernate.tool.schema.spi.Target; /** @@ -13,12 +15,18 @@ import org.hibernate.tool.schema.spi.Target; */ public class TargetStdoutImpl implements Target { final private String delimiter; + final private Formatter formatter; public TargetStdoutImpl() { this( null ); } - + public TargetStdoutImpl(String delimiter) { + this( delimiter, FormatStyle.NONE.getFormatter()); + } + + public TargetStdoutImpl(String delimiter, Formatter formatter) { + this.formatter = formatter; this.delimiter = delimiter; } @@ -33,6 +41,9 @@ public class TargetStdoutImpl implements Target { @Override public void accept(String action) { + if (formatter != null) { + action = formatter.format(action); + } if ( delimiter != null ) { action += delimiter; } diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateDelimiterTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateDelimiterTest.java index 29f3d2c617..d784cf72c6 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateDelimiterTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateDelimiterTest.java @@ -54,7 +54,7 @@ public class SchemaUpdateDelimiterTest { su.setHaltOnError( true ); su.setOutputFile( output.getAbsolutePath() ); su.setDelimiter( EXPECTED_DELIMITER ); - su.setFormat( true ); + su.setFormat( false ); su.execute( Target.SCRIPT ); List sqlLines = Files.readAllLines( output.toPath() ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateFormatterTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateFormatterTest.java new file mode 100644 index 0000000000..4e2cb4f2ec --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateFormatterTest.java @@ -0,0 +1,80 @@ +package org.hibernate.test.schemaupdate; + +import java.io.File; +import java.nio.file.Files; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +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.cfg.Environment; +import org.hibernate.testing.TestForIssue; +import org.hibernate.tool.hbm2ddl.SchemaUpdate; +import org.hibernate.tool.hbm2ddl.Target; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Koen Aers + */ +@TestForIssue(jiraKey = "HHH-10158") +public class SchemaUpdateFormatterTest { + + private static final String AFTER_FORMAT = + System.lineSeparator() + + " create table test_entity (" + System.lineSeparator() + + " field varchar(255) not null," + System.lineSeparator() + + " primary key (field)" + System.lineSeparator() + + " );" + System.lineSeparator(); + private static final String DELIMITER = ";"; + + @Test + public void testSetFormat() throws Exception { + StandardServiceRegistry ssr = new StandardServiceRegistryBuilder() + .applySetting( Environment.HBM2DDL_AUTO, "none" ) + .build(); + try { + File output = File.createTempFile( "update_script", ".sql" ); + output.deleteOnExit(); + + final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) + .addAnnotatedClass( TestEntity.class ) + .buildMetadata(); + metadata.validate(); + + SchemaUpdate su = new SchemaUpdate( ssr, metadata ); + su.setHaltOnError( true ); + su.setOutputFile( output.getAbsolutePath() ); + su.setDelimiter( DELIMITER ); + su.setFormat( true ); + su.execute( Target.SCRIPT ); + + Assert.assertEquals( + AFTER_FORMAT, + new String(Files.readAllBytes(output.toPath()))); + } + finally { + StandardServiceRegistryBuilder.destroy( ssr ); + } + } + + @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; + } + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/tool/schema/TargetFileImplTest.java b/hibernate-core/src/test/java/org/hibernate/test/tool/schema/TargetFileImplTest.java new file mode 100644 index 0000000000..b97cda1343 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/tool/schema/TargetFileImplTest.java @@ -0,0 +1,46 @@ +package org.hibernate.test.tool.schema; + +import java.io.File; +import java.nio.file.Files; + +import org.hibernate.engine.jdbc.internal.FormatStyle; +import org.hibernate.engine.jdbc.internal.Formatter; +import org.hibernate.tool.schema.internal.TargetFileImpl; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author Koen Aers + */ +public class TargetFileImplTest { + + private static final Formatter FORMATTER = FormatStyle.DDL.getFormatter(); + private static final String BEFORE_FORMAT = + "create table test_entity (field varchar(255) not null, primary key (field))"; + private static final String AFTER_FORMAT = FORMATTER.format(BEFORE_FORMAT); + private static final String DELIMITER = "---"; + + private File file = null; + private String path = null; + private TargetFileImpl target = null; + + @Before + public void setUp() throws Exception { + file = File.createTempFile("temp", "sql"); + file.deleteOnExit(); + path = file.getAbsolutePath(); + } + + @Test + public void testAcceptWithFormatter() throws Exception { + target = new TargetFileImpl(path, DELIMITER, FORMATTER); + target.prepare(); + target.accept(BEFORE_FORMAT); + target.release(); + Assert.assertEquals( + AFTER_FORMAT + DELIMITER + System.lineSeparator(), + new String(Files.readAllBytes(file.toPath()))); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/tool/schema/TargetStdoutImplTest.java b/hibernate-core/src/test/java/org/hibernate/test/tool/schema/TargetStdoutImplTest.java new file mode 100644 index 0000000000..c7ca589cee --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/tool/schema/TargetStdoutImplTest.java @@ -0,0 +1,50 @@ +package org.hibernate.test.tool.schema; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import org.hibernate.engine.jdbc.internal.FormatStyle; +import org.hibernate.engine.jdbc.internal.Formatter; +import org.hibernate.tool.schema.internal.TargetStdoutImpl; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author Koen Aers + */ +public class TargetStdoutImplTest { + + private static final Formatter FORMATTER = FormatStyle.DDL.getFormatter(); + private static final String BEFORE_FORMAT = + "create table test_entity (field varchar(255) not null, primary key (field))"; + private static final String AFTER_FORMAT = FORMATTER.format(BEFORE_FORMAT); + private static final String DELIMITER = "---"; + + private PrintStream savedSystemOut = null; + private ByteArrayOutputStream baos = null; + private TargetStdoutImpl target = null; + + @Before + public void setUp() { + savedSystemOut = System.out; + baos = new ByteArrayOutputStream(); + System.setOut(new PrintStream(baos)); + } + + @After + public void tearDown() { + System.setOut(savedSystemOut); + } + + @Test + public void testAcceptWithFormatter() { + target = new TargetStdoutImpl(DELIMITER, FORMATTER); + target.accept(BEFORE_FORMAT); + Assert.assertEquals( + AFTER_FORMAT + DELIMITER + System.lineSeparator(), + baos.toString()); + } + +}