HHH-10972 Use UTF-8 charset for reading file content of scripts that are used for initializing databases

This commit is contained in:
Jaikiran Pai 2016-07-20 13:11:18 +05:30 committed by Vlad Mihalcea
parent c9c50d8afd
commit 7247f05a21
25 changed files with 456 additions and 62 deletions

View File

@ -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).
|===================================================================================================================================================================================================================================

View File

@ -250,3 +250,7 @@ processTestResources {
}
test.dependsOn ":hibernate-orm-modules:prepareWildFlyForTests"
test {
systemProperty "file.encoding", "utf-8"
}

View File

@ -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";

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<Object, Object> 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<Class> classes = new ArrayList<Class>();
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;
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -1 +1 @@
INSERT INTO Item(name) VALUES('schemagen-test');
INSERT INTO Item(name) VALUES('schémagen-test');

View File

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

View File

@ -0,0 +1 @@
INSERT INTO Item(name) VALUES('schémagen-test');

View File

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

View File

@ -0,0 +1 @@
INSERT INTO Item(name) VALUES('schémagen-test');

View File

@ -1 +1 @@
INSERT INTO Item(name) VALUES('schemagen-test');
INSERT INTO Item(name) VALUES('schémagen-test');