use of org.graalvm.polyglot.Context in project graal by oracle.
the class ContextPreInitializationTest method testMoreLanguagesPreInitialization.
@Test
public void testMoreLanguagesPreInitialization() throws Exception {
setPatchable(FIRST, SECOND);
doContextPreinitialize(FIRST, SECOND);
List<CountingContext> contexts = new ArrayList<>(emittedContexts);
assertEquals(2, contexts.size());
final CountingContext firstLangCtx = findContext(FIRST, contexts);
assertNotNull(firstLangCtx);
final CountingContext secondLangCtx = findContext(SECOND, contexts);
assertNotNull(secondLangCtx);
assertEquals(1, firstLangCtx.createContextCount);
assertEquals(1, firstLangCtx.initializeContextCount);
assertEquals(0, firstLangCtx.patchContextCount);
assertEquals(0, firstLangCtx.disposeContextCount);
assertEquals(0, firstLangCtx.initializeThreadCount);
assertEquals(0, firstLangCtx.disposeThreadCount);
assertEquals(1, secondLangCtx.createContextCount);
assertEquals(1, secondLangCtx.initializeContextCount);
assertEquals(0, secondLangCtx.patchContextCount);
assertEquals(0, secondLangCtx.disposeContextCount);
assertEquals(0, secondLangCtx.initializeThreadCount);
assertEquals(0, secondLangCtx.disposeThreadCount);
final Context ctx = Context.create();
Value res = ctx.eval(Source.create(FIRST, "test"));
assertEquals("test", res.asString());
contexts = new ArrayList<>(emittedContexts);
assertEquals(2, contexts.size());
assertEquals(1, firstLangCtx.createContextCount);
assertEquals(1, firstLangCtx.initializeContextCount);
assertEquals(1, firstLangCtx.patchContextCount);
assertEquals(0, firstLangCtx.disposeContextCount);
assertEquals(1, firstLangCtx.initializeThreadCount);
assertEquals(0, firstLangCtx.disposeThreadCount);
assertEquals(1, secondLangCtx.createContextCount);
assertEquals(1, secondLangCtx.initializeContextCount);
assertEquals(1, secondLangCtx.patchContextCount);
assertEquals(0, secondLangCtx.disposeContextCount);
assertEquals(1, secondLangCtx.initializeThreadCount);
assertEquals(0, secondLangCtx.disposeThreadCount);
res = ctx.eval(Source.create(SECOND, "test"));
assertEquals("test", res.asString());
contexts = new ArrayList<>(emittedContexts);
assertEquals(2, contexts.size());
assertEquals(1, firstLangCtx.createContextCount);
assertEquals(1, firstLangCtx.initializeContextCount);
assertEquals(1, firstLangCtx.patchContextCount);
assertEquals(0, firstLangCtx.disposeContextCount);
assertEquals(1, firstLangCtx.initializeThreadCount);
assertEquals(0, firstLangCtx.disposeThreadCount);
assertEquals(1, secondLangCtx.createContextCount);
assertEquals(1, secondLangCtx.initializeContextCount);
assertEquals(1, secondLangCtx.patchContextCount);
assertEquals(0, secondLangCtx.disposeContextCount);
assertEquals(1, secondLangCtx.initializeThreadCount);
assertEquals(0, secondLangCtx.disposeThreadCount);
ctx.close();
contexts = new ArrayList<>(emittedContexts);
assertEquals(2, contexts.size());
assertEquals(1, firstLangCtx.createContextCount);
assertEquals(1, firstLangCtx.initializeContextCount);
assertEquals(1, firstLangCtx.patchContextCount);
assertEquals(1, firstLangCtx.disposeContextCount);
assertEquals(1, firstLangCtx.initializeThreadCount);
assertEquals(1, firstLangCtx.disposeThreadCount);
assertEquals(1, secondLangCtx.createContextCount);
assertEquals(1, secondLangCtx.initializeContextCount);
assertEquals(1, secondLangCtx.patchContextCount);
assertEquals(1, secondLangCtx.disposeContextCount);
assertEquals(1, secondLangCtx.initializeThreadCount);
assertEquals(1, secondLangCtx.disposeThreadCount);
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class EngineAPITest method testCreateContextWithAutomaticEngine.
@Test
public void testCreateContextWithAutomaticEngine() {
Context context = Context.create();
try {
Context.newBuilder().engine(context.getEngine()).build();
fail();
} catch (IllegalArgumentException e) {
}
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class VirtualizedFileSystemTest method testReadUsingChannel.
@Test
public void testReadUsingChannel() {
final Context ctx = cfg.getContext();
final Path path = cfg.getPath();
final boolean canRead = cfg.canRead();
languageAction = (Env env) -> {
final TruffleFile root = cfg.needsURI() ? env.getTruffleFile(path.toUri()) : env.getTruffleFile(path.toString());
try {
final TruffleFile file = root.resolve(FOLDER_EXISTING).resolve(FILE_EXISTING);
final String content = new String(file.readAllBytes(), StandardCharsets.UTF_8);
Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
Assert.assertEquals(cfg.formatErrorMessage("Expected file content"), FILE_EXISTING_CONTENT, content);
} catch (SecurityException se) {
Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), canRead);
} catch (IOException ioe) {
throw new AssertionError(cfg.formatErrorMessage(ioe.getMessage()), ioe);
}
};
ctx.eval(LANGAUGE_ID, "");
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class VirtualizedFileSystemTest method testDelete.
@Test
public void testDelete() {
final Context ctx = cfg.getContext();
final Path path = cfg.getPath();
final boolean canWrite = cfg.canWrite();
languageAction = (Env env) -> {
final TruffleFile root = cfg.needsURI() ? env.getTruffleFile(path.toUri()) : env.getTruffleFile(path.toString());
try {
final TruffleFile toCreate = root.resolve(FILE_EXISTING_DELETE);
toCreate.delete();
Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canWrite);
} catch (SecurityException se) {
Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), canWrite);
} catch (IOException ioe) {
throw new AssertionError(cfg.formatErrorMessage(ioe.getMessage()), ioe);
}
};
ctx.eval(LANGAUGE_ID, "");
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class VirtualizedFileSystemTest method isWritable.
@Test
public void isWritable() {
final Context ctx = cfg.getContext();
final Path path = cfg.getPath();
final boolean canRead = cfg.canRead();
languageAction = (Env env) -> {
final TruffleFile root = cfg.needsURI() ? env.getTruffleFile(path.toUri()) : env.getTruffleFile(path.toString());
try {
final TruffleFile file = root.resolve(FOLDER_EXISTING).resolve(FILE_EXISTING);
final boolean writable = file.isWritable();
Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
Assert.assertTrue(cfg.formatErrorMessage("Is writable"), writable);
} catch (SecurityException se) {
Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), canRead);
}
};
ctx.eval(LANGAUGE_ID, "");
}
Aggregations