Skip to content

Commit

Permalink
[GR-32346] Simplify test for GR-32346.
Browse files Browse the repository at this point in the history
PullRequest: graal/9906
  • Loading branch information
fniephaus committed Sep 29, 2021
2 parents 4695539 + 75f68c4 commit 833c2e7
Showing 1 changed file with 9 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@
import org.graalvm.polyglot.PolyglotException;
import org.graalvm.polyglot.TypeLiteral;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.impl.AbstractPolyglotImpl;
import org.graalvm.polyglot.impl.AbstractPolyglotImpl.APIAccess;
import org.graalvm.polyglot.proxy.ProxyArray;
import org.graalvm.polyglot.proxy.ProxyExecutable;
import org.graalvm.polyglot.proxy.ProxyObject;
Expand Down Expand Up @@ -417,45 +415,23 @@ public void testBufferAccessDisabled() {
*/
@Test
public void testBuilderCannotChangeMembersAndTargetMappingsOfHostAccess() throws Exception {
APIAccess apiAccess = findAPIAccess();

// Set up hostAccess
Builder builder = HostAccess.newBuilder();
Field okField = OK.class.getField("value");
Field banField = Ban.class.getField("value");
builder.allowAccess(okField);
builder.targetTypeMapping(Integer.class, Integer.class, null, (v) -> 42);
builder.allowAccess(OK.class.getField("value"));
builder.targetTypeMapping(Value.class, String.class, (v) -> v.isString(), (v) -> "foo");
HostAccess hostAccess = builder.build();
List<Object> targetMappings = apiAccess.getTargetMappings(hostAccess);

// Verify hostAccess
assertTrue(apiAccess.allowsAccess(hostAccess, okField));
assertFalse(apiAccess.allowsAccess(hostAccess, banField));
assertEquals(1, targetMappings.size());

// Try to change members or targetMappings through child builder
Builder childBuilder = HostAccess.newBuilder(hostAccess);
childBuilder.allowAccess(banField);
childBuilder.targetTypeMapping(Integer.class, Character.class, null, (v) -> (char) 42);
HostAccess childHostAccess = childBuilder.build();
List<Object> childTargetMappings = apiAccess.getTargetMappings(childHostAccess);

// Verify childHostAccess
assertTrue(apiAccess.allowsAccess(childHostAccess, okField));
assertTrue(apiAccess.allowsAccess(childHostAccess, banField));
assertEquals(2, childTargetMappings.size());
childBuilder.allowAccess(Ban.class.getField("value"));
childBuilder.targetTypeMapping(Value.class, Integer.class, null, (v) -> 42);

// Ensure hostAccess has not been altered by child builder
assertTrue(apiAccess.allowsAccess(hostAccess, okField));
assertFalse(apiAccess.allowsAccess(hostAccess, banField));
assertEquals(1, targetMappings.size());
assertNotSame(targetMappings, childTargetMappings);
}

APIAccess findAPIAccess() throws ReflectiveOperationException {
Method getImplMethod = Engine.class.getDeclaredMethod("getImpl");
getImplMethod.setAccessible(true);
return ((AbstractPolyglotImpl) getImplMethod.invoke(null)).getAPIAccess();
try (Context c = Context.newBuilder().allowHostAccess(hostAccess).build()) {
assertAccess(c);
assertEquals("foo", c.asValue("a string").as(String.class));
assertEquals(123, (int) c.asValue(123).as(Integer.class));
}
}

@Test
Expand Down

0 comments on commit 833c2e7

Please sign in to comment.