Package org.h2.store.fs

Examples of org.h2.store.fs.FileSystem


        testReadOnly();
        testAdapter();
    }

    private static void testAdapter() {
        TraceSystem ts = new TraceSystem(null);
        ts.setLevelFile(TraceSystem.ADAPTER);
        ts.getTrace("test").info("test");
        ts.close();
    }
View Full Code Here


        ts.getTrace("test").info("test");
        ts.close();
    }

    private void testTraceDebug() {
        TraceSystem ts = new TraceSystem(null);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ts.setSysOut(new PrintStream(out));
        ts.setLevelSystemOut(TraceSystem.DEBUG);
        ts.getTrace("test").debug(new Exception("error"), "test");
        ts.close();
        String outString = new String(out.toByteArray());
        assertContains(outString, "error");
        assertContains(outString, "Exception");
        assertContains(outString, "test");
    }
View Full Code Here

    private void testReadOnly() throws Exception {
        String readOnlyFile = getBaseDir() + "/readOnly.log";
        IOUtils.delete(readOnlyFile);
        IOUtils.openFileOutputStream(readOnlyFile, false).close();
        FileSystem.getInstance(getBaseDir()).setReadOnly(readOnlyFile);
        TraceSystem ts = new TraceSystem(readOnlyFile);
        ts.setLevelFile(TraceSystem.INFO);
        ts.getTrace("test").info("test");
        IOUtils.delete(readOnlyFile);
        ts.close();
    }
View Full Code Here

        FileSystem.getInstance("split:").delete("split:" + getBaseDir() + "/openClose2.h2.db");
        Connection conn;
        conn = DriverManager.getConnection("jdbc:h2:split:18:" + getBaseDir() + "/openClose2");
        conn.createStatement().execute("create table test(id int, name varchar) as select 1, space(1000000)");
        conn.close();
        FileObject f = FileSystem.getInstance(getBaseDir()).openFileObject(getBaseDir() + "/openClose2.h2.db.1.part", "rw");
        f.setFileLength(f.length() * 2);
        f.close();
        assertThrows(ErrorCode.IO_EXCEPTION_2, this).
                getConnection("jdbc:h2:split:18:" + getBaseDir() + "/openClose2");
        FileSystem.getInstance("split:").delete("split:" + getBaseDir() + "/openClose2.h2.db");
    }
View Full Code Here

        conn.close();

        ConvertTraceFile.main("-traceFile", getBaseDir() + "/toolsConvertTraceFile.trace.db",
                "-javaClass", getBaseDir() + "/Test",
                "-script", getBaseDir() + "/test.sql");
        FileSystem fs = FileSystem.getInstance(getBaseDir());
        fs.delete(getBaseDir() + "/Test.java");

        String trace = getBaseDir() + "/toolsConvertTraceFile.trace.db";
        assertTrue(fs.exists(trace));
        String newTrace = getBaseDir() + "/test.trace.db";
        fs.delete(newTrace);
        assertFalse(fs.exists(newTrace));
        fs.rename(trace, newTrace);
        deleteDb("toolsConvertTraceFile");
        Player.main(getBaseDir() + "/test.trace.db");
        testTraceFile(url);

        deleteDb("toolsConvertTraceFile");
View Full Code Here

        stat = conn.createStatement();
        stat.execute("backup to '" + getBaseDir() + "/fsJar.zip'");
        conn.close();

        deleteDb("fsJar");
        FileSystem fs = FileSystem.getInstance("zip:" + getBaseDir() + "/fsJar.zip");
        for (String f : fs.listFiles("zip:" + getBaseDir() + "/fsJar.zip")) {
            assertTrue(fs.isAbsolute(f));
            assertTrue(!fs.isDirectory(f));
            assertTrue(fs.length(f) > 0);
            assertTrue(f.endsWith(fs.getFileName(f)));
        }
        String urlJar = "jdbc:h2:zip:" + getBaseDir() + "/fsJar.zip!/fsJar";
        conn = DriverManager.getConnection(urlJar, "sa", "sa");
        stat = conn.createStatement();
        rs = stat.executeQuery("select * from test");
View Full Code Here

        conn.close();
        IOUtils.delete(getBaseDir() + "/fsJar.zip");
    }

    private void testUserHome() {
        FileSystem fs = FileSystem.getInstance("~/test");
        String fileName = fs.getCanonicalPath("~/test");
        String userDir = System.getProperty("user.home");
        assertTrue(fileName.startsWith(userDir));
    }
View Full Code Here

        testTempFile(fsBase);
        testRandomAccess(fsBase);
    }

    private void testSimple(String fsBase) throws Exception {
        FileSystem fs = FileSystem.getInstance(fsBase);
        long time = System.currentTimeMillis();
        for (String s : fs.listFiles(fsBase)) {
            fs.delete(s);
        }
        fs.createDirs(fsBase + "/test/x");
        fs.delete(fsBase + "/test");
        fs.delete(fsBase + "/test2");
        assertTrue(fs.createNewFile(fsBase + "/test"));
        assertTrue(fs.canWrite(fsBase + "/test"));
        FileObject fo = fs.openFileObject(fsBase + "/test", "rw");
        byte[] buffer = new byte[10000];
        Random random = new Random(1);
        random.nextBytes(buffer);
        fo.write(buffer, 0, 10000);
        assertEquals(10000, fo.length());
        fo.seek(20000);
        assertEquals(20000, fo.getFilePointer());
        assertThrows(EOFException.class, fo).readFully(buffer, 0, 1);
        assertEquals(fsBase + "/test", fo.getName().replace('\\', '/'));
        assertEquals("test", fs.getFileName(fo.getName()));
        assertEquals(fsBase, fs.getParent(fo.getName()).replace('\\', '/'));
        fo.tryLock();
        fo.releaseLock();
        assertEquals(10000, fo.length());
        fo.close();
        assertEquals(10000, fs.length(fsBase + "/test"));
        fo = fs.openFileObject(fsBase + "/test", "r");
        byte[] test = new byte[10000];
        fo.readFully(test, 0, 10000);
        assertEquals(buffer, test);
        assertThrows(IOException.class, fo).write(test, 0, 10);
        assertThrows(IOException.class, fo).setFileLength(10);
        fo.close();
        long lastMod = fs.getLastModified(fsBase + "/test");
        if (lastMod < time - 1999) {
            // at most 2 seconds difference
            assertEquals(time, lastMod);
        }
        assertEquals(10000, fs.length(fsBase + "/test"));
        String[] list = fs.listFiles(fsBase);
        assertEquals(1, list.length);
        assertTrue(list[0].endsWith("test"));
        IOUtils.copy(fsBase + "/test", fsBase + "/test3");
        fs.rename(fsBase + "/test3", fsBase + "/test2");
        assertTrue(!fs.exists(fsBase + "/test3"));
        assertTrue(fs.exists(fsBase + "/test2"));
        assertEquals(10000, fs.length(fsBase + "/test2"));
        byte[] buffer2 = new byte[10000];
        InputStream in = fs.openFileInputStream(fsBase + "/test2");
        int pos = 0;
        while (true) {
            int l = in.read(buffer2, pos, Math.min(10000 - pos, 1000));
            if (l <= 0) {
                break;
            }
            pos += l;
        }
        in.close();
        assertEquals(10000, pos);
        assertEquals(buffer, buffer2);

        assertTrue(fs.tryDelete(fsBase + "/test2"));
        fs.delete(fsBase + "/test");
        if (fsBase.indexOf(FileSystemMemory.PREFIX) < 0 && fsBase.indexOf(FileSystemMemory.PREFIX_LZF) < 0) {
            fs.createDirs(fsBase + "/testDir/test");
            assertTrue(fs.isDirectory(fsBase + "/testDir"));
            if (!fsBase.startsWith("jdbc:")) {
                fs.deleteRecursive(fsBase + "/testDir", false);
                assertTrue(!fs.exists(fsBase + "/testDir"));
            }
        }
    }
View Full Code Here

        testRandomAccess(fsBase, 1);
    }

    private void testRandomAccess(String fsBase, int seed) throws Exception {
        StringBuilder buff = new StringBuilder();
        FileSystem fs = FileSystem.getInstance(fsBase);
        String s = fs.createTempFile(fsBase + "/tmp", ".tmp", false, false);
        File file = new File(TestBase.BASE_TEST_DIR + "/tmp");
        file.getParentFile().mkdirs();
        file.delete();
        RandomAccessFile ra = new RandomAccessFile(file, "rw");
        fs.delete(s);
        FileObject f = fs.openFileObject(s, "rw");
        assertThrows(EOFException.class, f).readFully(new byte[1], 0, 1);
        f.sync();
        Random random = new Random(seed);
        int size = getSize(100, 500);
        try {
            for (int i = 0; i < size; i++) {
                trace("op " + i);
                int pos = random.nextInt(10000);
                switch(random.nextInt(7)) {
                case 0: {
                    pos = (int) Math.min(pos, ra.length());
                    trace("seek " + pos);
                    buff.append("seek " + pos + "\n");
                    f.seek(pos);
                    ra.seek(pos);
                    break;
                }
                case 1: {
                    byte[] buffer = new byte[random.nextInt(1000)];
                    random.nextBytes(buffer);
                    trace("write " + buffer.length);
                    buff.append("write " + buffer.length + "\n");
                    f.write(buffer, 0, buffer.length);
                    ra.write(buffer, 0, buffer.length);
                    break;
                }
                case 2: {
                    trace("setLength " + pos);
                    f.setFileLength(pos);
                    ra.setLength(pos);
                    if (ra.getFilePointer() > pos) {
                        f.seek(0);
                        ra.seek(0);
                    }
                    buff.append("setLength " + pos + "\n");
                    break;
                }
                case 3: {
                    int len = random.nextInt(1000);
                    len = (int) Math.min(len, ra.length() - ra.getFilePointer());
                    byte[] b1 = new byte[len];
                    byte[] b2 = new byte[len];
                    trace("readFully " + len);
                    ra.readFully(b1, 0, len);
                    f.readFully(b2, 0, len);
                    buff.append("readFully " + len + "\n");
                    assertEquals(b1, b2);
                    break;
                }
                case 4: {
                    trace("getFilePointer");
                    buff.append("getFilePointer\n");
                    assertEquals(ra.getFilePointer(), f.getFilePointer());
                    break;
                }
                case 5: {
                    trace("length " + ra.length());
                    buff.append("length " + ra.length() + "\n");
                    assertEquals(ra.length(), f.length());
                    break;
                }
                case 6: {
                    trace("reopen");
                    buff.append("reopen\n");
                    f.close();
                    ra.close();
                    ra = new RandomAccessFile(file, "rw");
                    f = fs.openFileObject(s, "rw");
                    assertEquals(ra.length(), f.length());
                    break;
                }
                default:
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
            fail("Exception: " + e + "\n"+ buff.toString());
        } finally {
            f.close();
            ra.close();
            file.delete();
            fs.delete(s);
        }
    }
View Full Code Here

        }
    }

    private void testTempFile(String fsBase) throws Exception {
        int len = 10000;
        FileSystem fs = FileSystem.getInstance(fsBase);
        String s = fs.createTempFile(fsBase + "/tmp", ".tmp", false, false);
        OutputStream out = fs.openFileOutputStream(s, false);
        byte[] buffer = new byte[len];
        out.write(buffer);
        out.close();
        out = fs.openFileOutputStream(s, true);
        out.write(1);
        out.close();
        InputStream in = fs.openFileInputStream(s);
        for (int i = 0; i < len; i++) {
            assertEquals(0, in.read());
        }
        assertEquals(1, in.read());
        assertEquals(-1, in.read());
        in.close();
        out.close();
        fs.delete(s);
    }
View Full Code Here

TOP

Related Classes of org.h2.store.fs.FileSystem

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.