From 56532628125ac96f1b1794c7f2448cdb8f6534f4 Mon Sep 17 00:00:00 2001 From: Roland Illig Date: Mon, 5 Mar 2018 18:15:49 +0100 Subject: [PATCH] HHH-12357 - NamingHelper uses system default encoding Add replicating test case --- .../boot/model/naming/NamingHelperTest.java | 66 +++++++++++++++++++ .../hibernate/test/util/ReflectionUtil.java | 32 +++++++++ 2 files changed, 98 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/boot/model/naming/NamingHelperTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/boot/model/naming/NamingHelperTest.java b/hibernate-core/src/test/java/org/hibernate/boot/model/naming/NamingHelperTest.java new file mode 100644 index 0000000000..c62d72c354 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/boot/model/naming/NamingHelperTest.java @@ -0,0 +1,66 @@ +/* + * 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.boot.model.naming; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.hibernate.test.util.ReflectionUtil; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExternalResource; + +import static org.junit.Assert.*; + +public class NamingHelperTest extends BaseUnitTestCase { + + @Rule + public DefaultCharset defaultCharset = new DefaultCharset(); + + @Test + @TestForIssue(jiraKey = "HHH-12357") + public void generateHashedFkName() { + Identifier booksDe = new Identifier( "Bücher", false ); + Identifier authorsDe = new Identifier( "Autoren", false ); + Identifier authorId = new Identifier( "autor_id", false ); + + defaultCharset.set( StandardCharsets.ISO_8859_1 ); + + String fkNameLatin1 = NamingHelper.INSTANCE.generateHashedFkName( "FK", booksDe, authorsDe, authorId ); + + assertEquals( "FKpvm24wh1qwbmx6xjcbc7uv5f7", fkNameLatin1 ); + + defaultCharset.set( StandardCharsets.UTF_8 ); + + String fkNameUtf8 = NamingHelper.INSTANCE.generateHashedFkName( "FK", booksDe, authorsDe, authorId ); + + assertEquals( "FKdgopp1hqnm8c1o6sfbb3tbeh", fkNameUtf8 ); + } + + public static class DefaultCharset extends ExternalResource { + + private Charset prev; + + @Override + protected void before() { + prev = ReflectionUtil.getStaticFieldValue( Charset.class, "defaultCharset" ); + } + + @Override + protected void after() { + set( prev ); + } + + public void set(Charset charset) { + ReflectionUtil.setStaticField( Charset.class, "defaultCharset", charset ); + } + + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/util/ReflectionUtil.java b/hibernate-core/src/test/java/org/hibernate/test/util/ReflectionUtil.java index 93cd0a84c6..299d982c50 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/util/ReflectionUtil.java +++ b/hibernate-core/src/test/java/org/hibernate/test/util/ReflectionUtil.java @@ -48,6 +48,22 @@ public class ReflectionUtil { } } + /** + * Get a field value from a given class + * @param target Class whose field is being read + * @param name field name + * @return field value + */ + public static T getStaticFieldValue(Class target, String name) { + try { + Field field = getField(target, name); + return (T) field.get( null ); + } + catch ( IllegalAccessException e ) { + throw new IllegalArgumentException( "Cannot set field " + name, e); + } + } + /** * Set target Object field to a certain value * @param target Object whose field is being set @@ -79,6 +95,22 @@ public class ReflectionUtil { } } + /** + * Set target Class field to a certain value + * @param target Class whose field is being set + * @param fieldName Class field name to set + * @param value the new value for the given field + */ + public static void setStaticField(Class target, String fieldName, Object value) { + try { + Field field = getField(target, fieldName); + field.set( null, value ); + } + catch ( IllegalAccessException e ) { + throw new IllegalArgumentException("Field " + fieldName + " could not be set", e ); + } + } + /** * New target Object instance using the given arguments * @param constructorSupplier constructor supplier