diff --git a/documentation/src/main/asciidoc/userguide/appendices/Configurations.adoc b/documentation/src/main/asciidoc/userguide/appendices/Configurations.adoc index 8f6a7fe819..15f91e253b 100644 --- a/documentation/src/main/asciidoc/userguide/appendices/Configurations.adoc +++ b/documentation/src/main/asciidoc/userguide/appendices/Configurations.adoc @@ -620,6 +620,7 @@ Therefore, the `org.hibernate.tool.hbm2ddl.UniqueConstraintSchemaUpdateStrategy` Attempts to (re-)create unique constraints, ignoring exceptions thrown if the constraint already existed `SKIP`:: Does not attempt to create unique constraints on a schema update. +|`hibernate.hbm2ddl.charset_name` |`Charset.defaultCharset()` |Defines the charset (encoding) used for all input/output schema generation resources. By default, Hibernate uses the default charset given by `Charset.defaultCharset()`. This configuration property allows you to override the default JVM setting so that you can specify which encoding is used when reading and writing schema generation resources (e.g. File, URL). |=================================================================================================================================================================================================================================== diff --git a/hibernate-core/hibernate-core.gradle b/hibernate-core/hibernate-core.gradle index c55ad391f7..725951f7ba 100644 --- a/hibernate-core/hibernate-core.gradle +++ b/hibernate-core/hibernate-core.gradle @@ -249,4 +249,8 @@ processTestResources { } } -test.dependsOn ":hibernate-orm-modules:prepareWildFlyForTests" \ No newline at end of file +test.dependsOn ":hibernate-orm-modules:prepareWildFlyForTests" + +test { + systemProperty "file.encoding", "utf-8" +} \ No newline at end of file diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java index 8e393666ed..95992eb480 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java @@ -712,6 +712,8 @@ public interface AvailableSettings { /** * Default JDBC TimeZone. Unless specified, the JVM default TimeZone is going to be used by the underlying JDBC Driver. + * + * @since 5.2.3 */ String JDBC_TIME_ZONE = "hibernate.jdbc.time_zone"; @@ -1313,6 +1315,13 @@ public interface AvailableSettings { */ String HBM2DDL_DELIMITER = "hibernate.hbm2ddl.delimiter"; + /** + * The name of the charset used by the schema generation resource. Without specifying this configuration property, the JVM default charset is used. + * + * @since 5.2.3 + */ + String HBM2DDL_CHARSET_NAME = "hibernate.hbm2ddl.charset_name"; + String JMX_ENABLED = "hibernate.jmx.enabled"; String JMX_PLATFORM_SERVER = "hibernate.jmx.usePlatformServer"; diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExport.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExport.java index af8814ecab..ebee65b933 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExport.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExport.java @@ -325,7 +325,8 @@ public class SchemaExport { } scriptTarget = Helper.interpretScriptTargetSetting( outputFile, - serviceRegistry.getService( ClassLoaderService.class ) + serviceRegistry.getService( ClassLoaderService.class ), + (String) serviceRegistry.getService( ConfigurationService.class ).getSettings().get( AvailableSettings.HBM2DDL_CHARSET_NAME ) ); } else { diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/Helper.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/Helper.java index 3fc4159046..a860e4ec48 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/Helper.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/Helper.java @@ -41,7 +41,10 @@ import org.jboss.logging.Logger; public class Helper { private static final Logger log = Logger.getLogger( Helper.class ); - public static ScriptSourceInput interpretScriptSourceSetting(Object scriptSourceSetting, ClassLoaderService classLoaderService) { + public static ScriptSourceInput interpretScriptSourceSetting( + Object scriptSourceSetting, + ClassLoaderService classLoaderService, + String charsetName ) { if ( Reader.class.isInstance( scriptSourceSetting ) ) { return new ScriptSourceInputFromReader( (Reader) scriptSourceSetting ); } @@ -58,16 +61,19 @@ public class Helper { // ClassLoaderService.locateResource() first tries the given resource name as url form... final URL url = classLoaderService.locateResource( scriptSourceSettingString ); if ( url != null ) { - return new ScriptSourceInputFromUrl( url ); + return new ScriptSourceInputFromUrl( url, charsetName ); } // assume it is a File path final File file = new File( scriptSourceSettingString ); - return new ScriptSourceInputFromFile( file ); + return new ScriptSourceInputFromFile( file, charsetName ); } } - public static ScriptTargetOutput interpretScriptTargetSetting(Object scriptTargetSetting, ClassLoaderService classLoaderService) { + public static ScriptTargetOutput interpretScriptTargetSetting( + Object scriptTargetSetting, + ClassLoaderService classLoaderService, + String charsetName ) { if ( scriptTargetSetting == null ) { return null; } @@ -87,12 +93,12 @@ public class Helper { // ClassLoaderService.locateResource() first tries the given resource name as url form... final URL url = classLoaderService.locateResource( scriptTargetSettingString ); if ( url != null ) { - return new ScriptTargetOutputToUrl( url ); + return new ScriptTargetOutputToUrl( url, charsetName ); } // assume it is a File path final File file = new File( scriptTargetSettingString ); - return new ScriptTargetOutputToFile( file ); + return new ScriptTargetOutputToFile( file, charsetName ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaCreatorImpl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaCreatorImpl.java index 79e0a3306b..3963bc0c76 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaCreatorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/SchemaCreatorImpl.java @@ -58,6 +58,7 @@ import org.hibernate.tool.schema.spi.ScriptSourceInput; import org.hibernate.tool.schema.spi.SourceDescriptor; import org.hibernate.tool.schema.spi.TargetDescriptor; +import static org.hibernate.cfg.AvailableSettings.HBM2DDL_CHARSET_NAME; import static org.hibernate.cfg.AvailableSettings.HBM2DDL_LOAD_SCRIPT_SOURCE; import static org.hibernate.tool.schema.internal.Helper.interpretScriptSourceSetting; @@ -457,8 +458,10 @@ public class SchemaCreatorImpl implements SchemaCreator { final Formatter formatter = FormatStyle.NONE.getFormatter(); final Object importScriptSetting = options.getConfigurationValues().get( HBM2DDL_LOAD_SCRIPT_SOURCE ); + String charsetName = (String) options.getConfigurationValues().get( HBM2DDL_CHARSET_NAME ); + if ( importScriptSetting != null ) { - final ScriptSourceInput importScriptInput = interpretScriptSourceSetting( importScriptSetting, classLoaderService ); + final ScriptSourceInput importScriptInput = interpretScriptSourceSetting( importScriptSetting, classLoaderService, charsetName ); log.executingImportScript( importScriptInput.toString() ); importScriptInput.prepare(); try { @@ -479,7 +482,7 @@ public class SchemaCreatorImpl implements SchemaCreator { for ( String currentFile : importFiles.split( "," ) ) { final String resourceName = currentFile.trim(); - final ScriptSourceInput importScriptInput = interpretLegacyImportScriptSetting( resourceName, classLoaderService ); + final ScriptSourceInput importScriptInput = interpretLegacyImportScriptSetting( resourceName, classLoaderService, charsetName ); importScriptInput.prepare(); try { log.executingImportScript( importScriptInput.toString() ); @@ -495,14 +498,15 @@ public class SchemaCreatorImpl implements SchemaCreator { private ScriptSourceInput interpretLegacyImportScriptSetting( String resourceName, - ClassLoaderService classLoaderService) { + ClassLoaderService classLoaderService, + String charsetName) { try { final URL resourceUrl = classLoaderService.locateResource( resourceName ); if ( resourceUrl == null ) { return ScriptSourceInputNonExistentImpl.INSTANCE; } else { - return new ScriptSourceInputFromUrl( resourceUrl ); + return new ScriptSourceInputFromUrl( resourceUrl, charsetName ); } } catch (Exception e) { diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/GenerationTargetToScript.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/GenerationTargetToScript.java index ae1468fb35..98cd8e6fa3 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/GenerationTargetToScript.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/GenerationTargetToScript.java @@ -15,10 +15,13 @@ import org.hibernate.tool.schema.spi.ScriptTargetOutput; * @author Steve Ebersole */ public class GenerationTargetToScript implements GenerationTarget { + private final ScriptTargetOutput scriptTarget; private final String delimiter; - public GenerationTargetToScript(ScriptTargetOutput scriptTarget, String delimiter) { + public GenerationTargetToScript( + ScriptTargetOutput scriptTarget, + String delimiter) { if ( scriptTarget == null ) { throw new SchemaManagementException( "ScriptTargetOutput cannot be null" ); } diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptSourceInputFromFile.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptSourceInputFromFile.java index e4fca40cd7..99dda8ebbd 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptSourceInputFromFile.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptSourceInputFromFile.java @@ -7,8 +7,9 @@ package org.hibernate.tool.schema.internal.exec; import java.io.File; -import java.io.FileReader; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStreamReader; import java.io.Reader; import org.hibernate.tool.schema.spi.SchemaManagementException; @@ -26,15 +27,19 @@ public class ScriptSourceInputFromFile extends AbstractScriptSourceInput impleme private static final Logger log = Logger.getLogger( ScriptSourceInputFromFile.class ); private final File file; + private final String charsetName; + private Reader reader; /** * Constructs a ScriptSourceInputFromFile * * @param file The file to read from + * @param charsetName The charset name */ - public ScriptSourceInputFromFile(File file) { + public ScriptSourceInputFromFile(File file, String charsetName) { this.file = file; + this.charsetName = charsetName; } @Override @@ -48,11 +53,11 @@ public class ScriptSourceInputFromFile extends AbstractScriptSourceInput impleme @Override public void prepare() { super.prepare(); - this.reader = toFileReader( file ); + this.reader = toReader( file, charsetName ); } @SuppressWarnings("ResultOfMethodCallIgnored") - private static Reader toFileReader(File file) { + private static Reader toReader(File file, String charsetName) { if ( ! file.exists() ) { log.warnf( "Specified schema generation script file [%s] did not exist for reading", file ); return new Reader() { @@ -68,7 +73,9 @@ public class ScriptSourceInputFromFile extends AbstractScriptSourceInput impleme } try { - return new FileReader( file ); + return charsetName != null ? + new InputStreamReader( new FileInputStream(file), charsetName ) : + new InputStreamReader( new FileInputStream(file) ); } catch (IOException e) { throw new SchemaManagementException( diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptSourceInputFromUrl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptSourceInputFromUrl.java index f7a3d2df5e..034ec8dad4 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptSourceInputFromUrl.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptSourceInputFromUrl.java @@ -27,15 +27,19 @@ public class ScriptSourceInputFromUrl extends AbstractScriptSourceInput implemen private static final Logger log = Logger.getLogger( ScriptSourceInputFromFile.class ); private final URL url; + private final String charsetName; + private Reader reader; /** * Constructs a ScriptSourceInputFromUrl instance * * @param url The url to read from + * @param charsetName The charset name */ - public ScriptSourceInputFromUrl(URL url) { + public ScriptSourceInputFromUrl(URL url, String charsetName) { this.url = url; + this.charsetName = charsetName; } @Override @@ -50,7 +54,9 @@ public class ScriptSourceInputFromUrl extends AbstractScriptSourceInput implemen public void prepare() { super.prepare(); try { - this.reader = new InputStreamReader( url.openStream() ); + this.reader = charsetName != null ? + new InputStreamReader( url.openStream(), charsetName ) : + new InputStreamReader( url.openStream() ); } catch (IOException e) { throw new SchemaManagementException( diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptTargetOutputToFile.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptTargetOutputToFile.java index 30d3426959..3338377500 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptTargetOutputToFile.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptTargetOutputToFile.java @@ -7,8 +7,9 @@ package org.hibernate.tool.schema.internal.exec; import java.io.File; -import java.io.FileWriter; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStreamWriter; import java.io.Writer; import org.hibernate.internal.CoreLogging; @@ -26,10 +27,19 @@ public class ScriptTargetOutputToFile extends AbstractScriptTargetOutput impleme private static final Logger log = CoreLogging.logger( ScriptTargetOutputToFile.class ); private final File file; + private final String charsetName; + private Writer writer; - public ScriptTargetOutputToFile(File file) { + /** + * Constructs a ScriptTargetOutputToFile instance + * + * @param file The file to read from + * @param charsetName The charset name + */ + public ScriptTargetOutputToFile(File file, String charsetName) { this.file = file; + this.charsetName = charsetName; } @Override @@ -43,7 +53,7 @@ public class ScriptTargetOutputToFile extends AbstractScriptTargetOutput impleme @Override public void prepare() { super.prepare(); - this.writer = toFileWriter( this.file ); + this.writer = toFileWriter( this.file, this.charsetName ); } @Override @@ -62,7 +72,7 @@ public class ScriptTargetOutputToFile extends AbstractScriptTargetOutput impleme } @SuppressWarnings("ResultOfMethodCallIgnored") - static Writer toFileWriter(File file) { + static Writer toFileWriter( File file, String charsetName ) { try { if ( ! file.exists() ) { // best effort, since this is very likely not allowed in EE environments @@ -77,7 +87,15 @@ public class ScriptTargetOutputToFile extends AbstractScriptTargetOutput impleme log.debug( "Exception calling File#createNewFile : " + e.toString() ); } try { - return new FileWriter( file, true ); + return charsetName != null ? + new OutputStreamWriter( + new FileOutputStream( file, true ), + charsetName + ) : + new OutputStreamWriter( new FileOutputStream( + file, + true + ) ); } catch (IOException e) { throw new SchemaManagementException( "Unable to open specified script target file for writing : " + file, e ); diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptTargetOutputToUrl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptTargetOutputToUrl.java index cd351e583f..02fca12742 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptTargetOutputToUrl.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptTargetOutputToUrl.java @@ -27,10 +27,19 @@ public class ScriptTargetOutputToUrl extends AbstractScriptTargetOutput implemen private static final Logger log = CoreLogging.logger( ScriptTargetOutputToUrl.class ); private final URL url; + private final String charsetName; + private Writer writer; - public ScriptTargetOutputToUrl(URL url) { + /** + * Constructs a ScriptTargetOutputToUrl instance + * + * @param url The url to read from + * @param charsetName The charset name + */ + public ScriptTargetOutputToUrl(URL url, String charsetName) { this.url = url; + this.charsetName = charsetName; } @Override @@ -44,7 +53,7 @@ public class ScriptTargetOutputToUrl extends AbstractScriptTargetOutput implemen @Override public void prepare() { super.prepare(); - this.writer = toWriter( url ); + this.writer = toWriter( url, charsetName ); } @Override @@ -58,12 +67,12 @@ public class ScriptTargetOutputToUrl extends AbstractScriptTargetOutput implemen } - private static Writer toWriter(URL url) { + private static Writer toWriter( URL url, String charsetName ) { log.debug( "Attempting to resolve writer for URL : " + url ); // technically only "strings corresponding to file URLs" are supported, which I take to mean URLs whose // protocol is "file" try { - return ScriptTargetOutputToFile.toFileWriter( new File( url.toURI() ) ); + return ScriptTargetOutputToFile.toFileWriter( new File( url.toURI() ), charsetName ); } catch (URISyntaxException e) { throw new SchemaManagementException( diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/spi/SchemaManagementToolCoordinator.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/spi/SchemaManagementToolCoordinator.java index b4a6a789a4..30c88ebf12 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/spi/SchemaManagementToolCoordinator.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/spi/SchemaManagementToolCoordinator.java @@ -11,6 +11,7 @@ import java.util.Map; import org.hibernate.boot.Metadata; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.service.ServiceRegistry; import org.hibernate.tool.schema.Action; import org.hibernate.tool.schema.SourceType; @@ -208,7 +209,11 @@ public class SchemaManagementToolCoordinator { } final ScriptSourceInput scriptSourceInput = includesScripts - ? Helper.interpretScriptSourceSetting( scriptSourceSetting, serviceRegistry.getService( ClassLoaderService.class ) ) + ? Helper.interpretScriptSourceSetting( scriptSourceSetting, + serviceRegistry.getService( + ClassLoaderService.class ), + (String) configurationValues + .get( AvailableSettings.HBM2DDL_CHARSET_NAME ) ) : null; return new JpaTargetAndSourceDescriptor() { @@ -332,13 +337,16 @@ public class SchemaManagementToolCoordinator { ); } + String charsetName = (String) configurationValues.get( AvailableSettings.HBM2DDL_CHARSET_NAME ); + final ScriptSourceInput scriptSourceInput = includesScripts - ? Helper.interpretScriptSourceSetting( scriptSourceSetting, serviceRegistry.getService( ClassLoaderService.class ) ) + ? Helper.interpretScriptSourceSetting( scriptSourceSetting, serviceRegistry.getService( ClassLoaderService.class ), charsetName ) : null; final ScriptTargetOutput scriptTargetOutput = Helper.interpretScriptTargetSetting( settingSelector.getScriptTargetSetting( configurationValues ), - serviceRegistry.getService( ClassLoaderService.class ) + serviceRegistry.getService( ClassLoaderService.class ), + charsetName ); return new JpaTargetAndSourceDescriptor() { diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/JpaFileSchemaGeneratorTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/JpaFileSchemaGeneratorTest.java new file mode 100644 index 0000000000..324b399b7a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/JpaFileSchemaGeneratorTest.java @@ -0,0 +1,41 @@ +/* + * 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 org.hibernate.dialect.H2Dialect; + +import org.hibernate.testing.RequiresDialect; +import org.hibernate.testing.TestForIssue; + +/** + * @author Vlad MIhalcea + */ +@RequiresDialect( H2Dialect.class ) +@TestForIssue( jiraKey = "HHH-10972" ) +public class JpaFileSchemaGeneratorTest extends JpaSchemaGeneratorTest { + + protected String getLoadSqlScript() { + return toFilePath(super.getLoadSqlScript()); + } + + protected String getCreateSqlScript() { + return toFilePath(super.getCreateSqlScript()); + } + + protected String getDropSqlScript() { + return toFilePath(super.getDropSqlScript()); + } + + protected String toFilePath(String relativePath) { + return Thread.currentThread().getContextClassLoader().getResource( relativePath ).getFile(); + } + + @Override + protected String getResourceUrlString(String resource) { + return resource; + } +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/JpaSchemaGeneratorTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/JpaSchemaGeneratorTest.java index 2ecad8b6a2..9a152a2d7f 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/JpaSchemaGeneratorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/JpaSchemaGeneratorTest.java @@ -8,10 +8,11 @@ package org.hibernate.jpa.test.schemagen; import java.net.URL; import java.util.Map; +import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.dialect.H2Dialect; -import org.hibernate.jpa.AvailableSettings; import org.hibernate.jpa.boot.spi.Bootstrap; import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder; import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor; @@ -23,19 +24,21 @@ import org.junit.Assert; import org.junit.Test; /** - * Basic tests for JPA 2.1 schema export - * - * @author Christian Beikov - * @author Steve Ebersole + * @author Vlad Mihalcea */ @RequiresDialect( H2Dialect.class ) public class JpaSchemaGeneratorTest extends BaseEntityManagerFunctionalTestCase { - private static final String LOAD_SQL = "org/hibernate/jpa/test/schemagen/load-script-source.sql"; - private static final String CREATE_SQL = "org/hibernate/jpa/test/schemagen/create-script-source.sql"; - private static final String DROP_SQL = "org/hibernate/jpa/test/schemagen/drop-script-source.sql"; + + private final String LOAD_SQL = getScriptFolderPath() + "load-script-source.sql"; + private final String CREATE_SQL = getScriptFolderPath() + "create-script-source.sql"; + private final String DROP_SQL = getScriptFolderPath() + "drop-script-source.sql"; private static int schemagenNumber = 0; + public String getScriptFolderPath() { + return "org/hibernate/jpa/test/schemagen/"; + } + @Override public Class[] getAnnotatedClasses() { return new Class[] { Item.class }; @@ -46,8 +49,8 @@ public class JpaSchemaGeneratorTest extends BaseEntityManagerFunctionalTestCase @TestForIssue(jiraKey = "HHH-8271") public void testSqlLoadScriptSourceClasspath() throws Exception { Map settings = buildSettings(); - settings.put( AvailableSettings.SCHEMA_GEN_DATABASE_ACTION, "drop-and-create" ); - settings.put( AvailableSettings.SCHEMA_GEN_LOAD_SCRIPT_SOURCE, LOAD_SQL ); + settings.put( AvailableSettings.HBM2DDL_DATABASE_ACTION, "drop-and-create" ); + settings.put( AvailableSettings.HBM2DDL_LOAD_SCRIPT_SOURCE, getLoadSqlScript() ); doTest( settings ); } @@ -57,12 +60,12 @@ public class JpaSchemaGeneratorTest extends BaseEntityManagerFunctionalTestCase @TestForIssue(jiraKey = "HHH-8271") public void testSqlLoadScriptSourceUrl() throws Exception { Map settings = buildSettings(); - settings.put( AvailableSettings.SCHEMA_GEN_DATABASE_ACTION, "drop-and-create" ); - settings.put( AvailableSettings.SCHEMA_GEN_LOAD_SCRIPT_SOURCE, getResourceUrlString( LOAD_SQL ) ); + settings.put( AvailableSettings.HBM2DDL_DATABASE_ACTION, "drop-and-create" ); + settings.put( AvailableSettings.HBM2DDL_LOAD_SCRIPT_SOURCE, getResourceUrlString( getLoadSqlScript() ) ); doTest( settings ); } - private String getResourceUrlString(String resource) { + protected String getResourceUrlString(String resource) { final URL url = getClass().getClassLoader().getResource( resource ); if ( url == null ) { throw new RuntimeException( "Unable to locate requested resource [" + resource + "]" ); @@ -75,9 +78,9 @@ public class JpaSchemaGeneratorTest extends BaseEntityManagerFunctionalTestCase @TestForIssue(jiraKey = "HHH-8271") public void testSqlCreateScriptSourceClasspath() throws Exception { Map settings = buildSettings(); - settings.put( AvailableSettings.SCHEMA_GEN_DATABASE_ACTION, "drop-and-create" ); - settings.put( AvailableSettings.SCHEMA_GEN_CREATE_SOURCE, "metadata-then-script" ); - settings.put( AvailableSettings.SCHEMA_GEN_CREATE_SCRIPT_SOURCE, CREATE_SQL ); + settings.put( AvailableSettings.HBM2DDL_DATABASE_ACTION, "drop-and-create" ); + settings.put( AvailableSettings.HBM2DDL_CREATE_SOURCE, "metadata-then-script" ); + settings.put( AvailableSettings.HBM2DDL_CREATE_SCRIPT_SOURCE, getCreateSqlScript() ); doTest( settings ); } @@ -86,9 +89,9 @@ public class JpaSchemaGeneratorTest extends BaseEntityManagerFunctionalTestCase @TestForIssue(jiraKey = "HHH-8271") public void testSqlCreateScriptSourceUrl() throws Exception { Map settings = buildSettings(); - settings.put( AvailableSettings.SCHEMA_GEN_DATABASE_ACTION, "drop-and-create" ); - settings.put( AvailableSettings.SCHEMA_GEN_CREATE_SOURCE, "metadata-then-script" ); - settings.put( AvailableSettings.SCHEMA_GEN_CREATE_SCRIPT_SOURCE, getResourceUrlString( CREATE_SQL ) ); + settings.put( AvailableSettings.HBM2DDL_DATABASE_ACTION, "drop-and-create" ); + settings.put( AvailableSettings.HBM2DDL_CREATE_SOURCE, "metadata-then-script" ); + settings.put( AvailableSettings.HBM2DDL_CREATE_SCRIPT_SOURCE, getResourceUrlString( getCreateSqlScript() ) ); doTest( settings ); } @@ -98,9 +101,9 @@ public class JpaSchemaGeneratorTest extends BaseEntityManagerFunctionalTestCase @TestForIssue(jiraKey = "HHH-8271") public void testSqlDropScriptSourceClasspath() throws Exception { Map settings = buildSettings(); - settings.put( AvailableSettings.SCHEMA_GEN_DROP_SOURCE, "metadata-then-script" ); - settings.put( AvailableSettings.SCHEMA_GEN_DATABASE_ACTION, "drop" ); - settings.put( AvailableSettings.SCHEMA_GEN_DROP_SCRIPT_SOURCE, DROP_SQL ); + settings.put( AvailableSettings.HBM2DDL_DROP_SOURCE, "metadata-then-script" ); + settings.put( AvailableSettings.HBM2DDL_DATABASE_ACTION, "drop" ); + settings.put( AvailableSettings.HBM2DDL_DROP_SCRIPT_SOURCE, getDropSqlScript() ); doTest( settings ); } @@ -109,12 +112,24 @@ public class JpaSchemaGeneratorTest extends BaseEntityManagerFunctionalTestCase @TestForIssue(jiraKey = "HHH-8271") public void testSqlDropScriptSourceUrl() throws Exception { Map settings = buildSettings(); - settings.put( AvailableSettings.SCHEMA_GEN_DROP_SOURCE, "metadata-then-script" ); - settings.put( AvailableSettings.SCHEMA_GEN_DATABASE_ACTION, "drop" ); - settings.put( AvailableSettings.SCHEMA_GEN_DROP_SCRIPT_SOURCE, getResourceUrlString( DROP_SQL ) ); + settings.put( AvailableSettings.HBM2DDL_DROP_SOURCE, "metadata-then-script" ); + settings.put( AvailableSettings.HBM2DDL_DATABASE_ACTION, "drop" ); + settings.put( AvailableSettings.HBM2DDL_DROP_SCRIPT_SOURCE, getResourceUrlString( getDropSqlScript() ) ); doTest( settings ); } + protected String getLoadSqlScript() { + return LOAD_SQL; + } + + protected String getCreateSqlScript() { + return CREATE_SQL; + } + + protected String getDropSqlScript() { + return DROP_SQL; + } + @SuppressWarnings("unchecked") private void doTest(Map settings) { // We want a fresh db afterQuery emf close @@ -125,8 +140,13 @@ public class JpaSchemaGeneratorTest extends BaseEntityManagerFunctionalTestCase settings ); EntityManagerFactory emf = emfb.build(); try { - Assert.assertNotNull( emf.createEntityManager().find( Item.class, "schemagen-test" ) ); - + EntityManager em = emf.createEntityManager(); + try { + Assert.assertNotNull( em.find( Item.class, encodedName() ) ); + } + finally { + em.close(); + } } finally { emf.close(); @@ -144,4 +164,7 @@ public class JpaSchemaGeneratorTest extends BaseEntityManagerFunctionalTestCase return false; } + protected String encodedName() { + return "sch" + (char) 233 +"magen-test"; + } } \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/SchemaCreateDropUtf8WithoutHbm2DdlCharsetNameTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/SchemaCreateDropUtf8WithoutHbm2DdlCharsetNameTest.java new file mode 100644 index 0000000000..f819e4ceac --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/SchemaCreateDropUtf8WithoutHbm2DdlCharsetNameTest.java @@ -0,0 +1,98 @@ +/* + * 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 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 javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.hibernate.cfg.Environment; +import org.hibernate.jpa.boot.spi.Bootstrap; +import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder; +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.hibernate.testing.TestForIssue; +import org.junit.Before; +import org.junit.Test; + +import static org.wildfly.common.Assert.assertTrue; + +/** + * @author Vlad Mihalcea + */ +public class SchemaCreateDropUtf8WithoutHbm2DdlCharsetNameTest { + + private File createSchema; + private File dropSchema; + + private EntityManagerFactoryBuilder entityManagerFactoryBuilder; + + protected 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; + } + + @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( + new BaseEntityManagerFunctionalTestCase.TestingPersistenceUnitDescriptorImpl( getClass().getSimpleName() ), + getConfig() + ); + } + + @Test + @TestForIssue(jiraKey = "HHH-10972") + public void testEncoding() throws Exception { + + entityManagerFactoryBuilder.generateSchema(); + + final String fileContent = new String( Files.readAllBytes( createSchema.toPath() ) ) + .toLowerCase(); + assertTrue( fileContent.contains( expectedTableName() ) ); + assertTrue( fileContent.contains( expectedFieldName() ) ); + + final String dropFileContent = new String( Files.readAllBytes( + dropSchema.toPath() ) ).toLowerCase(); + assertTrue( dropFileContent.contains( expectedTableName() ) ); + } + + protected String expectedTableName() { + return "test_" + (char) 233 + "ntity"; + } + + protected String expectedFieldName() { + return "fi" + (char) 233 + "ld"; + } + + @Entity + @Table(name = "test_" + (char) 233 +"ntity") + public static class TestEntity { + + @Id + @Column(name = "fi" + (char) 233 + "ld") + private String field; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/iso8859/JpaFileSchemaGeneratorWithHbm2DdlCharsetNameTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/iso8859/JpaFileSchemaGeneratorWithHbm2DdlCharsetNameTest.java new file mode 100644 index 0000000000..908c50c93e --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/iso8859/JpaFileSchemaGeneratorWithHbm2DdlCharsetNameTest.java @@ -0,0 +1,56 @@ +/* + * 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.iso8859; + +import java.util.Map; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.dialect.H2Dialect; +import org.hibernate.jpa.test.schemagen.JpaSchemaGeneratorTest; + +import org.hibernate.testing.RequiresDialect; +import org.hibernate.testing.TestForIssue; + +/** + * @author Vlad MIhalcea + */ +@RequiresDialect( H2Dialect.class ) +@TestForIssue( jiraKey = "HHH-10972" ) +public class JpaFileSchemaGeneratorWithHbm2DdlCharsetNameTest extends JpaSchemaGeneratorTest { + + public String getScriptFolderPath() { + return "org/hibernate/jpa/test/schemagen/iso8859/"; + } + + @Override + protected Map buildSettings() { + Map settings = super.buildSettings(); + settings.put( AvailableSettings.HBM2DDL_CHARSET_NAME, "ISO-8859-1" ); + return settings; + } + + protected String getLoadSqlScript() { + return toFilePath(super.getLoadSqlScript()); + } + + protected String getCreateSqlScript() { + return toFilePath(super.getCreateSqlScript()); + } + + protected String getDropSqlScript() { + return toFilePath(super.getDropSqlScript()); + } + + protected String toFilePath(String relativePath) { + return Thread.currentThread().getContextClassLoader().getResource( relativePath ).getFile(); + } + + @Override + protected String getResourceUrlString(String resource) { + return resource; + } +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/iso8859/JpaSchemaGeneratorWithHbm2DdlCharsetNameTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/iso8859/JpaSchemaGeneratorWithHbm2DdlCharsetNameTest.java new file mode 100644 index 0000000000..aba0172897 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/iso8859/JpaSchemaGeneratorWithHbm2DdlCharsetNameTest.java @@ -0,0 +1,34 @@ +/* + * 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.iso8859; + +import java.util.Map; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.dialect.H2Dialect; +import org.hibernate.jpa.test.schemagen.JpaSchemaGeneratorTest; + +import org.hibernate.testing.RequiresDialect; + +/** + * @author Vlad Mihalcea + */ +@RequiresDialect( H2Dialect.class ) +public class JpaSchemaGeneratorWithHbm2DdlCharsetNameTest + extends JpaSchemaGeneratorTest { + + public String getScriptFolderPath() { + return "org/hibernate/jpa/test/schemagen/iso8859/"; + } + + @Override + protected Map buildSettings() { + Map settings = super.buildSettings(); + settings.put( AvailableSettings.HBM2DDL_CHARSET_NAME, "ISO-8859-1" ); + return settings; + } +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/iso8859/JpaSchemaGeneratorWithoutHbm2DdlCharsetNameTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/iso8859/JpaSchemaGeneratorWithoutHbm2DdlCharsetNameTest.java new file mode 100644 index 0000000000..7768a30ee3 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/iso8859/JpaSchemaGeneratorWithoutHbm2DdlCharsetNameTest.java @@ -0,0 +1,28 @@ +/* + * 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.iso8859; + +import org.hibernate.dialect.H2Dialect; +import org.hibernate.jpa.test.schemagen.JpaSchemaGeneratorTest; + +import org.hibernate.testing.RequiresDialect; + +/** + * @author Vlad Mihalcea + */ +@RequiresDialect( H2Dialect.class ) +public class JpaSchemaGeneratorWithoutHbm2DdlCharsetNameTest + extends JpaSchemaGeneratorTest { + + public String getScriptFolderPath() { + return "org/hibernate/jpa/test/schemagen/iso8859/"; + } + + protected String encodedName() { + return "sch" + String.valueOf( '\uFFFD' ) +"magen-test"; + } +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/iso8859/SchemaCreateDropWithHbm2DdlCharsetNameTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/iso8859/SchemaCreateDropWithHbm2DdlCharsetNameTest.java new file mode 100644 index 0000000000..5f623a068a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/schemagen/iso8859/SchemaCreateDropWithHbm2DdlCharsetNameTest.java @@ -0,0 +1,34 @@ +/* + * 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.iso8859; + +import java.util.Map; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.jpa.test.schemagen.SchemaCreateDropUtf8WithoutHbm2DdlCharsetNameTest; + +/** + * @author Vlad Mihalcea + */ +public class SchemaCreateDropWithHbm2DdlCharsetNameTest extends + SchemaCreateDropUtf8WithoutHbm2DdlCharsetNameTest { + + @Override + protected Map getConfig() { + Map settings = super.getConfig(); + settings.put( AvailableSettings.HBM2DDL_CHARSET_NAME, "ISO-8859-1" ); + return settings; + } + + protected String expectedTableName() { + return "test_" + '\uFFFD' + "ntity"; + } + + protected String expectedFieldName() { + return "fi" + '\uFFFD' + "ld"; + } +} diff --git a/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/create-script-source.sql b/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/create-script-source.sql index 49fb00c312..7cdfb3db93 100644 --- a/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/create-script-source.sql +++ b/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/create-script-source.sql @@ -1 +1 @@ -INSERT INTO Item(name) VALUES('schemagen-test'); +INSERT INTO Item(name) VALUES('schémagen-test'); diff --git a/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/drop-script-source.sql b/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/drop-script-source.sql index 1fe051373b..3f800cb12a 100644 --- a/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/drop-script-source.sql +++ b/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/drop-script-source.sql @@ -1,2 +1,2 @@ create table Item ( name varchar(30) not null, descr varchar(200), primary key (name)); -INSERT INTO Item(name) VALUES('schemagen-test'); +INSERT INTO Item(name) VALUES('schémagen-test'); diff --git a/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/iso8859/create-script-source.sql b/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/iso8859/create-script-source.sql new file mode 100644 index 0000000000..ea0385550d --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/iso8859/create-script-source.sql @@ -0,0 +1 @@ +INSERT INTO Item(name) VALUES('schémagen-test'); diff --git a/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/iso8859/drop-script-source.sql b/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/iso8859/drop-script-source.sql new file mode 100644 index 0000000000..e5cd10f20b --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/iso8859/drop-script-source.sql @@ -0,0 +1,2 @@ +create table Item ( name varchar(30) not null, descr varchar(200), primary key (name)); +INSERT INTO Item(name) VALUES('schémagen-test'); diff --git a/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/iso8859/load-script-source.sql b/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/iso8859/load-script-source.sql new file mode 100644 index 0000000000..ea0385550d --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/iso8859/load-script-source.sql @@ -0,0 +1 @@ +INSERT INTO Item(name) VALUES('schémagen-test'); diff --git a/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/load-script-source.sql b/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/load-script-source.sql index 49fb00c312..7cdfb3db93 100644 --- a/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/load-script-source.sql +++ b/hibernate-core/src/test/resources/org/hibernate/jpa/test/schemagen/load-script-source.sql @@ -1 +1 @@ -INSERT INTO Item(name) VALUES('schemagen-test'); +INSERT INTO Item(name) VALUES('schémagen-test');