HHH-12357 - NamingHelper uses system default encoding

Add replicating test case
This commit is contained in:
Roland Illig 2018-03-05 18:15:49 +01:00 committed by Vlad Mihalcea
parent b599c770af
commit 5653262812
2 changed files with 98 additions and 0 deletions

View File

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

View File

@ -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> 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 * Set target Object field to a certain value
* @param target Object whose field is being set * @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 * New target Object instance using the given arguments
* @param constructorSupplier constructor supplier * @param constructorSupplier constructor supplier