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;
import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext;
import org.hibernate.bytecode.enhance.spi.UnloadedClass;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
@ -10,7 +9,6 @@ import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -21,31 +19,30 @@ import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.stream.Collectors.joining;
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
*/
@Ignore( "Property access does not allow dirty tracking, so this test fails (on the cleanup method)" )
@TestForIssue( jiraKey = "HHH-10851" )
@RunWith( BytecodeEnhancerRunner.class )
@CustomEnhancementContext( {EnhancerTestContext.class, MixedAccessTest.NoDirtyCheckingContext.class} )
@CustomEnhancementContext( MixedAccessTest.NoDirtyCheckingContext.class )
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 boolean cleanup = false;
private static final String ID = "foo", PARAM_KEY = "paramName", PARAM_VAL = "paramValue", PARAMS_AS_STR = "{\"" + PARAM_KEY + "\":\"" + PARAM_VAL + "\"}";
@Override
public Class<?>[] getAnnotatedClasses() {
@ -55,12 +52,12 @@ public class MixedAccessTest extends BaseCoreFunctionalTestCase {
@Before
public void prepare() {
doInHibernate( this::sessionFactory, s -> {
TestEntity testEntity = new TestEntity( "foo" );
testEntity.setParamsAsString( "{\"paramName\":\"paramValue\"}" );
TestEntity testEntity = new TestEntity( ID );
testEntity.setParamsAsString( PARAMS_AS_STR );
s.persist( testEntity );
TestOtherEntity testOtherEntity = new TestOtherEntity( "foo" );
testOtherEntity.setParamsAsString( "{\"paramName\":\"paramValue\"}" );
TestOtherEntity testOtherEntity = new TestOtherEntity( ID );
testOtherEntity.setParamsAsString( PARAMS_AS_STR );
s.persist( testOtherEntity );
} );
}
@ -68,14 +65,13 @@ public class MixedAccessTest extends BaseCoreFunctionalTestCase {
@Test
public void test() {
doInHibernate( this::sessionFactory, s -> {
TestEntity testEntity = s.get( TestEntity.class, "foo" );
Assert.assertEquals( "{\"paramName\":\"paramValue\"}", testEntity.getParamsAsString() );
TestEntity testEntity = s.get( TestEntity.class, ID );
Assert.assertEquals( PARAMS_AS_STR, testEntity.getParamsAsString() );
TestOtherEntity testOtherEntity = s.get( TestOtherEntity.class, "foo" );
Assert.assertEquals( "{\"paramName\":\"paramValue\"}", testOtherEntity.getParamsAsString() );
TestOtherEntity testOtherEntity = s.get( TestOtherEntity.class, ID );
Assert.assertEquals( PARAMS_AS_STR, testOtherEntity.getParamsAsString() );
// Clean parameters
cleanup = true;
testEntity.setParamsAsString( "{}" );
testOtherEntity.setParamsAsString( "{}" );
} );
@ -84,10 +80,10 @@ public class MixedAccessTest extends BaseCoreFunctionalTestCase {
@After
public void cleanup() {
doInHibernate( this::sessionFactory, s -> {
TestEntity testEntity = s.get( TestEntity.class, "foo" );
TestEntity testEntity = s.get( TestEntity.class, ID );
Assert.assertTrue( testEntity.getParams().isEmpty() );
TestOtherEntity testOtherEntity = s.get( TestOtherEntity.class, "foo" );
TestOtherEntity testOtherEntity = s.get( TestOtherEntity.class, ID );
Assert.assertTrue( testOtherEntity.getParams().isEmpty() );
} );
}
@ -117,28 +113,23 @@ public class MixedAccessTest extends BaseCoreFunctionalTestCase {
}
void setParams(Map<String, String> params) {
this.params = params;
this.params.clear();
this.params.putAll( params );
}
@Column( name = "params", length = 4000 )
@Access( AccessType.PROPERTY )
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" )
void setParamsAsString(String string) {
params.clear();
Matcher matcher = PARAM_PATTERN.matcher( string );
try {
if ( string != null ) {
params.putAll( (Map<String, String>) SCRIPT_ENGINE.eval( "Java.asJSONCompatible(" + string + ")" ) );
}
} catch ( ScriptException ignore ) {
// JDK 8u60 required --- use hard coded values to pass the test
if ( !cleanup ) {
params.put( "paramName", "paramValue" );
}
params.clear();
if ( matcher.matches() && matcher.groupCount() > 1 ) {
params.put( matcher.group( 1 ), matcher.group( 2 ) );
}
}
}
@ -167,35 +158,30 @@ public class MixedAccessTest extends BaseCoreFunctionalTestCase {
}
void setParams(Map<String, String> params) {
this.params = params;
this.params.clear();
this.params.putAll( params );
}
@Column( name = "params", length = 4000 )
@Access( AccessType.PROPERTY )
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" )
void setParamsAsString(String string) {
params.clear();
Matcher matcher = PARAM_PATTERN.matcher( string );
try {
if ( string != null ) {
params.putAll( (Map<String, String>) SCRIPT_ENGINE.eval( "Java.asJSONCompatible(" + string + ")" ) );
}
} catch ( ScriptException ignore ) {
// JDK 8u60 required --- use hard coded values to pass the test
if ( !cleanup ) {
params.put( "paramName", "paramValue" );
}
params.clear();
if ( matcher.matches() && matcher.groupCount() > 1 ) {
params.put( matcher.group( 1 ), matcher.group( 2 ) );
}
}
}
// --- //
public static class NoDirtyCheckingContext extends DefaultEnhancementContext {
public static class NoDirtyCheckingContext extends EnhancerTestContext {
@Override
public boolean doDirtyCheckingInline(UnloadedClass classDescriptor) {