Search in sources :

Example 11 with TruffleFile

use of com.oracle.truffle.api.TruffleFile in project graal by oracle.

the class VirtualizedFileSystemTest method testGetCanonicalFile.

@Test
public void testGetCanonicalFile() {
    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 canonical = root.resolve(FOLDER_EXISTING).resolve(FILE_EXISTING).getCanonicalFile();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
            Assert.assertNotNull(cfg.formatErrorMessage("Canonical file"), canonical);
        } 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, "");
}
Also used : Context(org.graalvm.polyglot.Context) Path(java.nio.file.Path) TruffleFile(com.oracle.truffle.api.TruffleFile) IOException(java.io.IOException) Env(com.oracle.truffle.api.TruffleLanguage.Env) Test(org.junit.Test)

Example 12 with TruffleFile

use of com.oracle.truffle.api.TruffleFile in project graal by oracle.

the class VirtualizedFileSystemTest method testToUri.

@Test
public void testToUri() {
    final Context ctx = cfg.getContext();
    final Path userDir = cfg.getUserDir();
    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 URI uri = file.toUri();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), allowsUserDir);
            Assert.assertEquals(cfg.formatErrorMessage("URI"), userDir.resolve(FOLDER_EXISTING).resolve(FILE_EXISTING).toUri(), uri);
        } catch (SecurityException se) {
            Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), allowsUserDir);
        }
    };
    ctx.eval(LANGAUGE_ID, "");
}
Also used : Context(org.graalvm.polyglot.Context) Path(java.nio.file.Path) TruffleFile(com.oracle.truffle.api.TruffleFile) Env(com.oracle.truffle.api.TruffleLanguage.Env) URI(java.net.URI) Test(org.junit.Test)

Example 13 with TruffleFile

use of com.oracle.truffle.api.TruffleFile in project graal by oracle.

the class VirtualizedFileSystemTest method testList.

@Test
public void testList() {
    final Context ctx = cfg.getContext();
    final Path path = cfg.getPath();
    final boolean canRead = cfg.canRead();
    languageAction = (Env env) -> {
        final Path folderExisting = path.resolve(FOLDER_EXISTING);
        final TruffleFile file = cfg.needsURI() ? env.getTruffleFile(folderExisting.toUri()) : env.getTruffleFile(folderExisting.toString());
        try {
            final String expected = path.resolve(FOLDER_EXISTING).resolve(FILE_EXISTING).toString();
            final Collection<? extends TruffleFile> children = file.list();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
            final Optional<String> expectedFile = children.stream().map(TruffleFile::getAbsoluteFile).map(TruffleFile::getPath).filter(expected::equals).findAny();
            Assert.assertTrue(cfg.formatErrorMessage("Expected child"), expectedFile.isPresent());
        } 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, "");
}
Also used : Context(org.graalvm.polyglot.Context) Path(java.nio.file.Path) TruffleFile(com.oracle.truffle.api.TruffleFile) IOException(java.io.IOException) Env(com.oracle.truffle.api.TruffleLanguage.Env) Test(org.junit.Test)

Example 14 with TruffleFile

use of com.oracle.truffle.api.TruffleFile in project graal by oracle.

the class VirtualizedFileSystemTest method isReadable.

@Test
public void isReadable() {
    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 readable = file.isReadable();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
            Assert.assertTrue(cfg.formatErrorMessage("Is readable"), readable);
        } catch (SecurityException se) {
            Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), canRead);
        }
    };
    ctx.eval(LANGAUGE_ID, "");
}
Also used : Context(org.graalvm.polyglot.Context) Path(java.nio.file.Path) TruffleFile(com.oracle.truffle.api.TruffleFile) Env(com.oracle.truffle.api.TruffleLanguage.Env) Test(org.junit.Test)

Example 15 with TruffleFile

use of com.oracle.truffle.api.TruffleFile in project graal by oracle.

the class VirtualizedFileSystemTest method testIsDirectory.

@Test
public void testIsDirectory() {
    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 isDir = file.isDirectory();
            Assert.assertTrue(cfg.formatErrorMessage("Expected SecurityException"), canRead);
            Assert.assertFalse(cfg.formatErrorMessage("Not directory"), isDir);
        } catch (SecurityException se) {
            Assert.assertFalse(cfg.formatErrorMessage("Unexpected SecurityException"), canRead);
        }
    };
    ctx.eval(LANGAUGE_ID, "");
}
Also used : Context(org.graalvm.polyglot.Context) Path(java.nio.file.Path) TruffleFile(com.oracle.truffle.api.TruffleFile) Env(com.oracle.truffle.api.TruffleLanguage.Env) Test(org.junit.Test)

Aggregations

TruffleFile (com.oracle.truffle.api.TruffleFile)20 Env (com.oracle.truffle.api.TruffleLanguage.Env)20 Context (org.graalvm.polyglot.Context)20 Test (org.junit.Test)20 Path (java.nio.file.Path)19 IOException (java.io.IOException)12 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 ByteChannel (java.nio.channels.ByteChannel)1 SeekableByteChannel (java.nio.channels.SeekableByteChannel)1 FileTime (java.nio.file.attribute.FileTime)1