HHH-12389 - Remove usage of javax.script.ScriptEngine in MixedAccessTest

This commit is contained in:
barreiro 2018-03-14 17:41:11 +00:00 committed by Vlad Mihalcea
parent d736c3fa62
commit ae0dfdc779
1 changed files with 33 additions and 47 deletions

View File

@ -1,6 +1,5 @@
package org.hibernate.test.bytecode.enhancement.access; package org.hibernate.test.bytecode.enhancement.access;
import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext;
import org.hibernate.bytecode.enhance.spi.UnloadedClass; import org.hibernate.bytecode.enhance.spi.UnloadedClass;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
@ -10,7 +9,6 @@ import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -21,31 +19,30 @@ import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Transient; import javax.persistence.Transient;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.joining;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
/** /**
* Requires a custom enhancement context to disable dirty checking as bytecode enhancement is not expected to fully work with AccessType.PROPERTY
* In particular, the properties changed are not marked dirty and therefore not updated in the DB (failing the checks in @After method)
*
* @author Luis Barreiro * @author Luis Barreiro
*/ */
@Ignore( "Property access does not allow dirty tracking, so this test fails (on the cleanup method)" )
@TestForIssue( jiraKey = "HHH-10851" ) @TestForIssue( jiraKey = "HHH-10851" )
@RunWith( BytecodeEnhancerRunner.class ) @RunWith( BytecodeEnhancerRunner.class )
@CustomEnhancementContext( {EnhancerTestContext.class, MixedAccessTest.NoDirtyCheckingContext.class} ) @CustomEnhancementContext( MixedAccessTest.NoDirtyCheckingContext.class )
public class MixedAccessTest extends BaseCoreFunctionalTestCase { public class MixedAccessTest extends BaseCoreFunctionalTestCase {
private static final ScriptEngine SCRIPT_ENGINE = new ScriptEngineManager().getEngineByName( "javascript" ); private static final Pattern PARAM_PATTERN = Pattern.compile( "\\{\\\"(.*)\\\"\\:\\\"(.*)\\\"\\}" );
private static final Function<Map.Entry, String> MAPPING_FUNCTION = e -> "\"" + e.getKey() + "\":\"" + e.getValue() + "\""; private static final Function<Map.Entry, String> MAPPING_FUNCTION = e -> "\"" + e.getKey() + "\":\"" + e.getValue() + "\"";
private static boolean cleanup = false; private static final String ID = "foo", PARAM_KEY = "paramName", PARAM_VAL = "paramValue", PARAMS_AS_STR = "{\"" + PARAM_KEY + "\":\"" + PARAM_VAL + "\"}";
@Override @Override
public Class<?>[] getAnnotatedClasses() { public Class<?>[] getAnnotatedClasses() {
@ -55,12 +52,12 @@ public class MixedAccessTest extends BaseCoreFunctionalTestCase {
@Before @Before
public void prepare() { public void prepare() {
doInHibernate( this::sessionFactory, s -> { doInHibernate( this::sessionFactory, s -> {
TestEntity testEntity = new TestEntity( "foo" ); TestEntity testEntity = new TestEntity( ID );
testEntity.setParamsAsString( "{\"paramName\":\"paramValue\"}" ); testEntity.setParamsAsString( PARAMS_AS_STR );
s.persist( testEntity ); s.persist( testEntity );
TestOtherEntity testOtherEntity = new TestOtherEntity( "foo" ); TestOtherEntity testOtherEntity = new TestOtherEntity( ID );
testOtherEntity.setParamsAsString( "{\"paramName\":\"paramValue\"}" ); testOtherEntity.setParamsAsString( PARAMS_AS_STR );
s.persist( testOtherEntity ); s.persist( testOtherEntity );
} ); } );
} }
@ -68,14 +65,13 @@ public class MixedAccessTest extends BaseCoreFunctionalTestCase {
@Test @Test
public void test() { public void test() {
doInHibernate( this::sessionFactory, s -> { doInHibernate( this::sessionFactory, s -> {
TestEntity testEntity = s.get( TestEntity.class, "foo" ); TestEntity testEntity = s.get( TestEntity.class, ID );
Assert.assertEquals( "{\"paramName\":\"paramValue\"}", testEntity.getParamsAsString() ); Assert.assertEquals( PARAMS_AS_STR, testEntity.getParamsAsString() );
TestOtherEntity testOtherEntity = s.get( TestOtherEntity.class, "foo" ); TestOtherEntity testOtherEntity = s.get( TestOtherEntity.class, ID );
Assert.assertEquals( "{\"paramName\":\"paramValue\"}", testOtherEntity.getParamsAsString() ); Assert.assertEquals( PARAMS_AS_STR, testOtherEntity.getParamsAsString() );
// Clean parameters // Clean parameters
cleanup = true;
testEntity.setParamsAsString( "{}" ); testEntity.setParamsAsString( "{}" );
testOtherEntity.setParamsAsString( "{}" ); testOtherEntity.setParamsAsString( "{}" );
} ); } );
@ -84,10 +80,10 @@ public class MixedAccessTest extends BaseCoreFunctionalTestCase {
@After @After
public void cleanup() { public void cleanup() {
doInHibernate( this::sessionFactory, s -> { doInHibernate( this::sessionFactory, s -> {
TestEntity testEntity = s.get( TestEntity.class, "foo" ); TestEntity testEntity = s.get( TestEntity.class, ID );
Assert.assertTrue( testEntity.getParams().isEmpty() ); Assert.assertTrue( testEntity.getParams().isEmpty() );
TestOtherEntity testOtherEntity = s.get( TestOtherEntity.class, "foo" ); TestOtherEntity testOtherEntity = s.get( TestOtherEntity.class, ID );
Assert.assertTrue( testOtherEntity.getParams().isEmpty() ); Assert.assertTrue( testOtherEntity.getParams().isEmpty() );
} ); } );
} }
@ -117,28 +113,23 @@ public class MixedAccessTest extends BaseCoreFunctionalTestCase {
} }
void setParams(Map<String, String> params) { void setParams(Map<String, String> params) {
this.params = params; this.params.clear();
this.params.putAll( params );
} }
@Column( name = "params", length = 4000 ) @Column( name = "params", length = 4000 )
@Access( AccessType.PROPERTY ) @Access( AccessType.PROPERTY )
String getParamsAsString() { String getParamsAsString() {
return params.isEmpty() ? null : "{" + params.entrySet().stream().map( MAPPING_FUNCTION ).collect( joining( "," ) ) + "}"; return "{" + params.entrySet().stream().map( MAPPING_FUNCTION ).collect( joining( "," ) ) + "}";
} }
@SuppressWarnings( "unchecked" ) @SuppressWarnings( "unchecked" )
void setParamsAsString(String string) { void setParamsAsString(String string) {
params.clear(); Matcher matcher = PARAM_PATTERN.matcher( string );
try { params.clear();
if ( string != null ) { if ( matcher.matches() && matcher.groupCount() > 1 ) {
params.putAll( (Map<String, String>) SCRIPT_ENGINE.eval( "Java.asJSONCompatible(" + string + ")" ) ); params.put( matcher.group( 1 ), matcher.group( 2 ) );
}
} catch ( ScriptException ignore ) {
// JDK 8u60 required --- use hard coded values to pass the test
if ( !cleanup ) {
params.put( "paramName", "paramValue" );
}
} }
} }
} }
@ -167,35 +158,30 @@ public class MixedAccessTest extends BaseCoreFunctionalTestCase {
} }
void setParams(Map<String, String> params) { void setParams(Map<String, String> params) {
this.params = params; this.params.clear();
this.params.putAll( params );
} }
@Column( name = "params", length = 4000 ) @Column( name = "params", length = 4000 )
@Access( AccessType.PROPERTY ) @Access( AccessType.PROPERTY )
String getParamsAsString() { String getParamsAsString() {
return params.isEmpty() ? null : "{" + params.entrySet().stream().map( MAPPING_FUNCTION ).collect( joining( "," ) ) + "}"; return "{" + params.entrySet().stream().map( MAPPING_FUNCTION ).collect( joining( "," ) ) + "}";
} }
@SuppressWarnings( "unchecked" ) @SuppressWarnings( "unchecked" )
void setParamsAsString(String string) { void setParamsAsString(String string) {
params.clear(); Matcher matcher = PARAM_PATTERN.matcher( string );
try { params.clear();
if ( string != null ) { if ( matcher.matches() && matcher.groupCount() > 1 ) {
params.putAll( (Map<String, String>) SCRIPT_ENGINE.eval( "Java.asJSONCompatible(" + string + ")" ) ); params.put( matcher.group( 1 ), matcher.group( 2 ) );
}
} catch ( ScriptException ignore ) {
// JDK 8u60 required --- use hard coded values to pass the test
if ( !cleanup ) {
params.put( "paramName", "paramValue" );
}
} }
} }
} }
// --- // // --- //
public static class NoDirtyCheckingContext extends DefaultEnhancementContext { public static class NoDirtyCheckingContext extends EnhancerTestContext {
@Override @Override
public boolean doDirtyCheckingInline(UnloadedClass classDescriptor) { public boolean doDirtyCheckingInline(UnloadedClass classDescriptor) {