use of com.oracle.truffle.api.TruffleFile in project graal by oracle.
the class VirtualizedFileSystemTest method testReadUsingStream.
@Test
public void testReadUsingStream() {
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 StringBuilder content = new StringBuilder();
try (final BufferedReader in = file.newBufferedReader()) {
final char[] buffer = new char[512];
while (true) {
int len = in.read(buffer);
if (len < 0) {
break;
} else {
content.append(buffer, 0, len);
}
}
}
Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
Assert.assertEquals(cfg.formatErrorMessage("Expected file content"), FILE_EXISTING_CONTENT, content.toString());
} 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 com.oracle.truffle.api.TruffleFile in project graal by oracle.
the class VirtualizedFileSystemTest method testRename.
@Test
public void testRename() {
final Context ctx = cfg.getContext();
final Path path = cfg.getPath();
final boolean canRead = cfg.canRead();
final boolean canWrite = cfg.canWrite();
languageAction = (Env env) -> {
final TruffleFile root = cfg.needsURI() ? env.getTruffleFile(path.toUri()) : env.getTruffleFile(path.toString());
try {
final TruffleFile file = root.resolve(FILE_EXISTING_RENAME);
final TruffleFile target = root.resolve(FILE_NEW_RENAME);
file.move(target);
Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canWrite);
if (canRead) {
final Collection<? extends TruffleFile> children = root.list();
final boolean hasRenameSource = children.stream().filter((TruffleFile truffleFile) -> FILE_EXISTING_RENAME.equals(truffleFile.getName())).findAny().isPresent();
final boolean hasRenameTarget = children.stream().filter((TruffleFile truffleFile) -> FILE_NEW_RENAME.equals(truffleFile.getName())).findAny().isPresent();
Assert.assertFalse(cfg.formatErrorMessage("Renamed file should not exist"), hasRenameSource);
Assert.assertTrue(cfg.formatErrorMessage("Rename target file should exist"), hasRenameTarget);
}
} 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 com.oracle.truffle.api.TruffleFile in project graal by oracle.
the class VirtualizedFileSystemTest method testCreateFileTest.
@Test
public void testCreateFileTest() {
final Context ctx = cfg.getContext();
final Path path = cfg.getPath();
final boolean canRead = cfg.canRead();
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_NEW_CREATE_FILE);
toCreate.createFile();
Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canWrite);
if (canRead) {
Assert.assertTrue(cfg.formatErrorMessage("Expected file exists"), toCreate.exists());
}
} 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 com.oracle.truffle.api.TruffleFile in project graal by oracle.
the class VirtualizedFileSystemTest method testCreateDirectoryTest.
@Test
public void testCreateDirectoryTest() {
final Context ctx = cfg.getContext();
final Path path = cfg.getPath();
final boolean canRead = cfg.canRead();
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_NEW_CREATE_DIR);
toCreate.createDirectories();
Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canWrite);
if (canRead) {
Assert.assertTrue(cfg.formatErrorMessage("Expected dir exists"), toCreate.exists());
}
} 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 com.oracle.truffle.api.TruffleFile in project graal by oracle.
the class VirtualizedFileSystemTest method testGetAbsoluteFile.
@Test
public void testGetAbsoluteFile() {
final Context ctx = cfg.getContext();
final boolean allowsUserDir = cfg.allowsUserDir();
if (cfg.needsURI()) {
// Nothing to test for URI path
return;
}
languageAction = (Env env) -> {
final TruffleFile file = env.getTruffleFile(FOLDER_EXISTING).resolve(FILE_EXISTING);
try {
final TruffleFile absolute = file.getAbsoluteFile();
Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), allowsUserDir);
Assert.assertEquals(absolute.getPath(), cfg.getUserDir().resolve(FOLDER_EXISTING).resolve(FILE_EXISTING).toString());
} catch (SecurityException se) {
Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), allowsUserDir);
}
};
ctx.eval(LANGAUGE_ID, "");
}
Aggregations