use of org.graalvm.polyglot.Context in project graal by oracle.
the class ContextPreInitializationTest method testSystemPropertiesOptionsSuccessfulPatch.
@Test
public void testSystemPropertiesOptionsSuccessfulPatch() throws Exception {
System.setProperty(SYS_OPTION1_KEY, "true");
setPatchable(FIRST);
doContextPreinitialize(FIRST);
List<CountingContext> contexts = new ArrayList<>(emittedContexts);
final CountingContext firstLangCtx = findContext(FIRST, contexts);
assertNotNull(firstLangCtx);
assertFalse(firstLangCtx.optionValues.get(ContextPreInitializationTestFirstLanguage.Option1));
assertFalse(firstLangCtx.optionValues.get(ContextPreInitializationTestFirstLanguage.Option2));
firstLangCtx.optionValues.clear();
System.getProperties().remove(SYS_OPTION1_KEY);
System.setProperty(SYS_OPTION2_KEY, "true");
Context ctx = Context.create();
Value res = ctx.eval(Source.create(FIRST, "test"));
assertEquals("test", res.asString());
assertFalse(firstLangCtx.optionValues.get(ContextPreInitializationTestFirstLanguage.Option1));
assertTrue(firstLangCtx.optionValues.get(ContextPreInitializationTestFirstLanguage.Option2));
ctx.close();
ctx = Context.create();
res = ctx.eval(Source.create(FIRST, "test"));
assertEquals("test", res.asString());
assertFalse(firstLangCtx.optionValues.get(ContextPreInitializationTestFirstLanguage.Option1));
assertTrue(firstLangCtx.optionValues.get(ContextPreInitializationTestFirstLanguage.Option2));
ctx.close();
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class ContextPreInitializationTest method testOutputNoLanguagePreInitialization.
@Test
public void testOutputNoLanguagePreInitialization() throws Exception {
setPatchable();
final String stdOutContent = "output";
final String stdErrContent = "error";
BaseLanguage.parseStdOutOutput.put(FIRST, stdOutContent);
BaseLanguage.parseStdErrOutput.put(FIRST, stdErrContent);
doContextPreinitialize();
List<CountingContext> contexts = new ArrayList<>(emittedContexts);
assertEquals(0, contexts.size());
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final ByteArrayOutputStream err = new ByteArrayOutputStream();
try (Context ctx = Context.newBuilder().out(out).err(err).build()) {
final Value res = ctx.eval(Source.create(FIRST, "test"));
assertEquals("test", res.asString());
assertEquals(stdOutContent, new String(out.toByteArray(), "UTF-8"));
assertEquals(stdErrContent, new String(err.toByteArray(), "UTF-8"));
}
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class ContextPreInitializationTest method testOutputSingleLanguagePreInitialization.
@Test
public void testOutputSingleLanguagePreInitialization() throws Exception {
setPatchable(FIRST);
final String firstStdOutContent = "first-output";
final String firstStdErrContent = "first-error";
final String secondStdOutContent = "second-output";
final String secondStdErrContent = "second-error";
BaseLanguage.parseStdOutOutput.put(FIRST, firstStdOutContent);
BaseLanguage.parseStdErrOutput.put(FIRST, firstStdErrContent);
BaseLanguage.parseStdOutOutput.put(SECOND, secondStdOutContent);
BaseLanguage.parseStdErrOutput.put(SECOND, secondStdErrContent);
doContextPreinitialize(FIRST);
List<CountingContext> contexts = new ArrayList<>(emittedContexts);
assertEquals(1, contexts.size());
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final ByteArrayOutputStream err = new ByteArrayOutputStream();
try (Context ctx = Context.newBuilder().out(out).err(err).build()) {
Value res = ctx.eval(Source.create(FIRST, "test"));
assertEquals("test", res.asString());
contexts = new ArrayList<>(emittedContexts);
assertEquals(1, contexts.size());
assertEquals(firstStdOutContent, new String(out.toByteArray(), "UTF-8"));
assertEquals(firstStdErrContent, new String(err.toByteArray(), "UTF-8"));
out.reset();
err.reset();
res = ctx.eval(Source.create(SECOND, "test"));
assertEquals("test", res.asString());
contexts = new ArrayList<>(emittedContexts);
assertEquals(2, contexts.size());
assertEquals(secondStdOutContent, new String(out.toByteArray(), "UTF-8"));
assertEquals(secondStdErrContent, new String(err.toByteArray(), "UTF-8"));
}
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class ContextPreInitializationTest method testArgumentsSingleLanguPreInitialization.
@Test
public void testArgumentsSingleLanguPreInitialization() throws Exception {
setPatchable(FIRST);
doContextPreinitialize(FIRST);
List<CountingContext> contexts = new ArrayList<>(emittedContexts);
assertEquals(1, contexts.size());
try (Context ctx = Context.newBuilder().arguments(FIRST, new String[] { "a", "b" }).arguments(SECOND, new String[] { "c", "d" }).build()) {
Value res = ctx.eval(Source.create(FIRST, "test"));
assertEquals("test", res.asString());
contexts = new ArrayList<>(emittedContexts);
assertEquals(1, contexts.size());
CountingContext context = findContext(FIRST, contexts);
assertNotNull(context);
assertEquals(Arrays.asList("a", "b"), context.arguments);
res = ctx.eval(Source.create(SECOND, "test"));
assertEquals("test", res.asString());
contexts = new ArrayList<>(emittedContexts);
assertEquals(2, contexts.size());
context = findContext(SECOND, contexts);
assertNotNull(context);
assertEquals(Arrays.asList("c", "d"), context.arguments);
}
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class ContextPreInitializationTest method testContextOptionsNoLanguagePreInitialization.
@Test
public void testContextOptionsNoLanguagePreInitialization() throws Exception {
setPatchable();
doContextPreinitialize();
List<CountingContext> contexts = new ArrayList<>(emittedContexts);
assertEquals(0, contexts.size());
final Context ctx = Context.newBuilder().option(FIRST + ".Option1", "true").build();
final Value res = ctx.eval(Source.create(FIRST, "test"));
assertEquals("test", res.asString());
contexts = new ArrayList<>(emittedContexts);
assertEquals(1, contexts.size());
CountingContext context = findContext(FIRST, contexts);
assertNotNull(context);
assertTrue(context.optionValues.get(ContextPreInitializationTestFirstLanguage.Option1));
assertFalse(context.optionValues.get(ContextPreInitializationTestFirstLanguage.Option2));
ctx.close();
}
Aggregations