Skip to content

Commit

Permalink
HHH-12389 - Remove usage of javax.script.ScriptEngine in MixedAccessTest
Browse files Browse the repository at this point in the history
  • Loading branch information
barreiro authored and vladmihalcea committed Apr 3, 2018
1 parent d736c3f commit ae0dfdc
Showing 1 changed file with 33 additions and 47 deletions.
@@ -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;
Expand All @@ -10,7 +9,6 @@
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;

Expand All @@ -21,31 +19,30 @@
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() {
Expand All @@ -55,27 +52,26 @@ public Class<?>[] getAnnotatedClasses() {
@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 );
} );
}

@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( "{}" );
} );
Expand All @@ -84,10 +80,10 @@ public void test() {
@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() );
} );
}
Expand Down Expand Up @@ -117,28 +113,23 @@ Map<String, String> getParams() {
}

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 ) );
}
}
}
Expand Down Expand Up @@ -167,35 +158,30 @@ Map<String, String> getParams() {
}

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) {
Expand Down

0 comments on commit ae0dfdc

Please sign in to comment.