HHH-11183 - Mixed line endings in schema creation script

This commit is contained in:
Andrea Boriero 2016-12-14 11:33:17 +00:00 committed by Vlad Mihalcea
parent b2df137ed6
commit 9976edc853
7 changed files with 76 additions and 23 deletions

View File

@ -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 );
}

View File

@ -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 );
}
}
}

View File

@ -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) {

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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() );
}
}

View File

@ -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();
}

View File

@ -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<String, String> PRIMITIVES = new HashMap<String, String>();
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() );
}
}
}

View File

@ -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 {