Search in sources :

Example 16 with TruffleFile

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, "");
}
Also used : Context(org.graalvm.polyglot.Context) Path(java.nio.file.Path) BufferedReader(java.io.BufferedReader) TruffleFile(com.oracle.truffle.api.TruffleFile) IOException(java.io.IOException) Env(com.oracle.truffle.api.TruffleLanguage.Env) Test(org.junit.Test)

Example 17 with TruffleFile

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, "");
}
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 18 with TruffleFile

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, "");
}
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 19 with TruffleFile

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, "");
}
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 20 with TruffleFile

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, "");
}
Also used : Context(org.graalvm.polyglot.Context) 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