From 9976edc8538fcb86ed2fabc281d7ad181b1ebd39 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Wed, 14 Dec 2016 11:33:17 +0000 Subject: [PATCH] HHH-11183 - Mixed line endings in schema creation script --- .../jdbc/internal/BasicFormatterImpl.java | 4 +- .../jdbc/internal/DDLFormatterImpl.java | 21 ++++--- .../exec/AbstractScriptTargetOutput.java | 8 +-- .../hibernate/jdbc/util/DdlFormatterTest.java | 57 +++++++++++++++++++ .../functional/ConcurrentWriteTest.java | 2 +- .../jpamodelgen/ImportContextImpl.java | 5 +- .../jpamodelgen/test/util/TestUtil.java | 2 +- 7 files changed, 76 insertions(+), 23 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/jdbc/util/DdlFormatterTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/BasicFormatterImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/BasicFormatterImpl.java index b97cff290b..29e8e56ad4 100755 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/BasicFormatterImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/BasicFormatterImpl.java @@ -68,7 +68,7 @@ public class BasicFormatterImpl implements Formatter { } private static final String INDENT_STRING = " "; - private static final String INITIAL = "\n "; + private static final String INITIAL = System.lineSeparator() + INDENT_STRING; @Override public String format(String source) { @@ -371,7 +371,7 @@ public class BasicFormatterImpl implements Formatter { } private void newline() { - result.append( "\n" ); + result.append( System.lineSeparator() ); for ( int i = 0; i < indent; i++ ) { result.append( INDENT_STRING ); } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/DDLFormatterImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/DDLFormatterImpl.java index ac7e52fa78..be5128c4d8 100755 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/DDLFormatterImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/DDLFormatterImpl.java @@ -18,6 +18,9 @@ import org.hibernate.internal.util.StringHelper; * @author Steve Ebersole */ public class DDLFormatterImpl implements Formatter { + + private static final String INITIAL_LINE = System.lineSeparator() + " "; + private static final String OTHER_LINES = System.lineSeparator() + " "; /** * Singleton access */ @@ -42,12 +45,12 @@ public class DDLFormatterImpl implements Formatter { return formatCommentOn( sql ); } else { - return "\n " + sql; + return INITIAL_LINE + sql; } } private String formatCommentOn(String sql) { - final StringBuilder result = new StringBuilder( 60 ).append( "\n " ); + final StringBuilder result = new StringBuilder( 60 ).append( INITIAL_LINE ); final StringTokenizer tokens = new StringTokenizer( sql, " '[]\"", true ); boolean quoted = false; @@ -59,7 +62,7 @@ public class DDLFormatterImpl implements Formatter { } else if ( !quoted ) { if ( "is".equals( token ) ) { - result.append( "\n " ); + result.append( OTHER_LINES ); } } } @@ -68,7 +71,7 @@ public class DDLFormatterImpl implements Formatter { } private String formatAlterTable(String sql) { - final StringBuilder result = new StringBuilder( 60 ).append( "\n " ); + final StringBuilder result = new StringBuilder( 60 ).append( INITIAL_LINE ); final StringTokenizer tokens = new StringTokenizer( sql, " (,)'[]\"", true ); boolean quoted = false; @@ -79,7 +82,7 @@ public class DDLFormatterImpl implements Formatter { } else if ( !quoted ) { if ( isBreak( token ) ) { - result.append( "\n " ); + result.append( OTHER_LINES ); } } result.append( token ); @@ -89,7 +92,7 @@ public class DDLFormatterImpl implements Formatter { } private String formatCreateTable(String sql) { - final StringBuilder result = new StringBuilder( 60 ).append( "\n " ); + final StringBuilder result = new StringBuilder( 60 ).append( INITIAL_LINE ); final StringTokenizer tokens = new StringTokenizer( sql, "(,)'[]\"", true ); int depth = 0; @@ -107,17 +110,17 @@ public class DDLFormatterImpl implements Formatter { if ( ")".equals( token ) ) { depth--; if ( depth == 0 ) { - result.append( "\n " ); + result.append( INITIAL_LINE ); } } result.append( token ); if ( ",".equals( token ) && depth == 1 ) { - result.append( "\n " ); + result.append( OTHER_LINES ); } if ( "(".equals( token ) ) { depth++; if ( depth == 1 ) { - result.append( "\n " ); + result.append( OTHER_LINES ); } } } diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/AbstractScriptTargetOutput.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/AbstractScriptTargetOutput.java index e47f83e001..2bb63eee87 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/AbstractScriptTargetOutput.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/AbstractScriptTargetOutput.java @@ -9,7 +9,6 @@ package org.hibernate.tool.schema.internal.exec; import java.io.IOException; import java.io.Writer; -import org.hibernate.internal.util.StringHelper; import org.hibernate.tool.schema.spi.CommandAcceptanceException; import org.hibernate.tool.schema.spi.ScriptTargetOutput; @@ -17,11 +16,6 @@ import org.hibernate.tool.schema.spi.ScriptTargetOutput; * @author Steve Ebersole */ public abstract class AbstractScriptTargetOutput implements ScriptTargetOutput { - protected static final String NEWLINE; - static { - final String systemNewLine = System.getProperty( "line.separator" ); - NEWLINE = StringHelper.isNotEmpty( systemNewLine ) ? systemNewLine : "\n"; - } protected abstract Writer writer(); @@ -33,7 +27,7 @@ public abstract class AbstractScriptTargetOutput implements ScriptTargetOutput { public void accept(String command) { try { writer().write( command ); - writer().write( NEWLINE ); + writer().write( System.lineSeparator() ); writer().flush(); } catch (IOException e) { diff --git a/hibernate-core/src/test/java/org/hibernate/jdbc/util/DdlFormatterTest.java b/hibernate-core/src/test/java/org/hibernate/jdbc/util/DdlFormatterTest.java new file mode 100644 index 0000000000..f3767224cd --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jdbc/util/DdlFormatterTest.java @@ -0,0 +1,57 @@ +/* + * 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.jdbc.util; + +import java.util.StringTokenizer; + +import org.hibernate.engine.jdbc.internal.FormatStyle; + +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +/** + * @author Vlad Mihalcea + */ +public class DdlFormatterTest extends BaseUnitTestCase { + @Test + public void testNoLoss() { + + assertNoLoss( "drop table post if exists" ); + assertNoLoss( "drop table post_comment if exists" ); + assertNoLoss( "drop table post_details if exists" ); + assertNoLoss( "drop table post_tag if exists" ); + assertNoLoss( "drop table tag if exists" ); + assertNoLoss( "create table post (id bigint not null, title varchar(255), primary key (id))" ); + assertNoLoss( "create table post_comment (id bigint not null, review varchar(255), post_id bigint, primary key (id))" ); + assertNoLoss( "create table post_details (id bigint not null, created_by varchar(255), created_on timestamp, primary key (id))" ); + assertNoLoss( "create table post_tag (post_id bigint not null, tag_id bigint not null)" ); + assertNoLoss( "create table tag (id bigint not null, name varchar(255), primary key (id))" ); + assertNoLoss( "alter table post_comment add constraint FKna4y825fdc5hw8aow65ijexm0 foreign key (post_id) references post" ); + assertNoLoss( "alter table post_details add constraint FKkl5eik513p1xiudk2kxb0v92u foreign key (id) references post" ); + assertNoLoss( "alter table post_tag add constraint FKac1wdchd2pnur3fl225obmlg0 foreign key (tag_id) references tag" ); + assertNoLoss( "alter table post_tag add constraint FKc2auetuvsec0k566l0eyvr9cs foreign key (post_id) references post" ); + } + + private void assertNoLoss(String query) { + String formattedQuery = FormatStyle.DDL.getFormatter().format( query ); + StringTokenizer formatted = new StringTokenizer( formattedQuery, " \t\n\r\f()" ); + StringTokenizer plain = new StringTokenizer( query, " \t\n\r\f()" ); + + System.out.println( "Original: " + query ); + System.out.println( "Formatted: " + formattedQuery ); + while ( formatted.hasMoreTokens() && plain.hasMoreTokens() ) { + String plainToken = plain.nextToken(); + String formattedToken = formatted.nextToken(); + assertEquals( "formatter did not return the same token", plainToken, formattedToken ); + } + assertFalse( formatted.hasMoreTokens() ); + assertFalse( plain.hasMoreTokens() ); + } +} diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/ConcurrentWriteTest.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/ConcurrentWriteTest.java index 4a125bdbed..fbe76069e7 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/ConcurrentWriteTest.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/ConcurrentWriteTest.java @@ -309,7 +309,7 @@ public class ConcurrentWriteTest extends SingleNodeTest { ); for ( UserRunner r : runners ) { - sb.append( r.toString() + System.getProperty( "line.separator" ) ); + sb.append( r.toString() ).append( System.lineSeparator() ); } return sb.toString(); } diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ImportContextImpl.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ImportContextImpl.java index 2c11a38b2c..b1ee3dd110 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ImportContextImpl.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ImportContextImpl.java @@ -27,7 +27,6 @@ public class ImportContextImpl implements ImportContext { private String basePackage = ""; - private static final String LINE_SEPARATOR = System.getProperty( "line.separator" ); private static final Map PRIMITIVES = new HashMap(); static { @@ -152,10 +151,10 @@ public class ImportContextImpl implements ImportContext { // don't add automatically "imported" stuff if ( !isAutoImported( next ) ) { if ( staticImports.contains( next ) ) { - builder.append( "import static " ).append( next ).append( ";" ).append( LINE_SEPARATOR ); + builder.append( "import static " ).append( next ).append( ";" ).append( System.lineSeparator() ); } else { - builder.append( "import " ).append( next ).append( ";" ).append( LINE_SEPARATOR ); + builder.append( "import " ).append( next ).append( ";" ).append( System.lineSeparator() ); } } } diff --git a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/TestUtil.java b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/TestUtil.java index 9d91eb51bc..de135e90ca 100644 --- a/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/TestUtil.java +++ b/tooling/metamodel-generator/src/test/java/org/hibernate/jpamodelgen/test/util/TestUtil.java @@ -200,7 +200,7 @@ public class TestUtil { */ while ( ( line = input.readLine() ) != null ) { contents.append( line ); - contents.append( System.getProperty( "line.separator" ) ); + contents.append( System.lineSeparator() ); } } finally {