Search in sources :

Example 31 with AssertThrows

use of org.h2.test.utils.AssertThrows in project h2database by h2database.

the class TestLobApi method testInputStreamThrowsException.

private void testInputStreamThrowsException(final boolean ioException) throws Exception {
    Connection conn = getConnection(getTestName());
    stat = conn.createStatement();
    stat.execute("create table test(id identity, c clob, b blob)");
    PreparedStatement prep = conn.prepareStatement("insert into test values(null, ?, ?)");
    assertThrows(ErrorCode.IO_EXCEPTION_1, prep).setCharacterStream(1, new Reader() {

        int pos;

        @Override
        public int read(char[] buff, int off, int len) throws IOException {
            pos += len;
            if (pos > 100001) {
                if (ioException) {
                    throw new IOException("");
                }
                throw new IllegalStateException();
            }
            return len;
        }

        @Override
        public void close() throws IOException {
        // nothing to do
        }
    }, -1);
    prep.setString(1, new String(new char[10000]));
    prep.setBytes(2, new byte[0]);
    prep.execute();
    prep.setString(1, "");
    assertThrows(ErrorCode.IO_EXCEPTION_1, prep).setBinaryStream(2, new InputStream() {

        int pos;

        @Override
        public int read() throws IOException {
            pos++;
            if (pos > 100001) {
                if (ioException) {
                    throw new IOException("");
                }
                throw new IllegalStateException();
            }
            return 0;
        }
    }, -1);
    prep.setBytes(2, new byte[10000]);
    prep.execute();
    ResultSet rs = stat.executeQuery("select c, b from test order by id");
    rs.next();
    assertEquals(new String(new char[10000]), rs.getString(1));
    assertEquals(new byte[0], rs.getBytes(2));
    rs.next();
    assertEquals("", rs.getString(1));
    assertEquals(new byte[10000], rs.getBytes(2));
    stat.execute("drop table test");
    conn.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) Reader(java.io.Reader) StringReader(java.io.StringReader) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 32 with AssertThrows

use of org.h2.test.utils.AssertThrows in project h2database by h2database.

the class TestFileSystem method testUnsupportedFeatures.

private static void testUnsupportedFeatures(String fsBase) throws IOException {
    final String fileName = fsBase + "/testFile";
    if (FileUtils.exists(fileName)) {
        FileUtils.delete(fileName);
    }
    if (FileUtils.createFile(fileName)) {
        final FileChannel channel = FileUtils.open(fileName, "rw");
        new AssertThrows(UnsupportedOperationException.class) {

            @Override
            public void test() throws IOException {
                channel.map(MapMode.PRIVATE, 0, channel.size());
            }
        };
        new AssertThrows(UnsupportedOperationException.class) {

            @Override
            public void test() throws IOException {
                channel.read(new ByteBuffer[] { ByteBuffer.allocate(10) }, 0, 0);
            }
        };
        new AssertThrows(UnsupportedOperationException.class) {

            @Override
            public void test() throws IOException {
                channel.write(new ByteBuffer[] { ByteBuffer.allocate(10) }, 0, 0);
            }
        };
        new AssertThrows(UnsupportedOperationException.class) {

            @Override
            public void test() throws IOException {
                channel.transferFrom(channel, 0, 0);
            }
        };
        new AssertThrows(UnsupportedOperationException.class) {

            @Override
            public void test() throws IOException {
                channel.transferTo(0, 0, channel);
            }
        };
        new AssertThrows(UnsupportedOperationException.class) {

            @Override
            public void test() throws IOException {
                channel.lock();
            }
        };
        channel.close();
        FileUtils.delete(fileName);
    }
}
Also used : AssertThrows(org.h2.test.utils.AssertThrows) FileChannel(java.nio.channels.FileChannel)

Example 33 with AssertThrows

use of org.h2.test.utils.AssertThrows in project h2database by h2database.

the class TestFileSystem method testReadOnly.

private void testReadOnly(final String f) throws IOException {
    new AssertThrows(IOException.class) {

        @Override
        public void test() throws IOException {
            FileUtils.newOutputStream(f, false);
        }
    };
    new AssertThrows(DbException.class) {

        @Override
        public void test() {
            FileUtils.move(f, f);
        }
    };
    new AssertThrows(DbException.class) {

        @Override
        public void test() {
            FileUtils.move(f, f);
        }
    };
    new AssertThrows(IOException.class) {

        @Override
        public void test() throws IOException {
            FileUtils.createTempFile(f, ".tmp", false, false);
        }
    };
    final FileChannel channel = FileUtils.open(f, "r");
    new AssertThrows(IOException.class) {

        @Override
        public void test() throws IOException {
            channel.write(ByteBuffer.allocate(1));
        }
    };
    new AssertThrows(IOException.class) {

        @Override
        public void test() throws IOException {
            channel.truncate(0);
        }
    };
    assertTrue(null == channel.tryLock());
    channel.force(false);
    channel.close();
}
Also used : AssertThrows(org.h2.test.utils.AssertThrows) FileChannel(java.nio.channels.FileChannel)

Example 34 with AssertThrows

use of org.h2.test.utils.AssertThrows in project h2database by h2database.

the class TestFileSystem method testSimple.

private void testSimple(final String fsBase) throws Exception {
    long time = System.currentTimeMillis();
    for (String s : FileUtils.newDirectoryStream(fsBase)) {
        FileUtils.delete(s);
    }
    FileUtils.createDirectories(fsBase + "/test");
    assertTrue(FileUtils.exists(fsBase));
    FileUtils.delete(fsBase + "/test");
    FileUtils.delete(fsBase + "/test2");
    assertTrue(FileUtils.createFile(fsBase + "/test"));
    List<FilePath> p = FilePath.get(fsBase).newDirectoryStream();
    assertEquals(1, p.size());
    String can = FilePath.get(fsBase + "/test").toRealPath().toString();
    assertEquals(can, p.get(0).toString());
    assertTrue(FileUtils.canWrite(fsBase + "/test"));
    FileChannel channel = FileUtils.open(fsBase + "/test", "rw");
    byte[] buffer = new byte[10000];
    Random random = new Random(1);
    random.nextBytes(buffer);
    channel.write(ByteBuffer.wrap(buffer));
    assertEquals(10000, channel.size());
    channel.position(20000);
    assertEquals(20000, channel.position());
    assertEquals(-1, channel.read(ByteBuffer.wrap(buffer, 0, 1)));
    String path = fsBase + "/test";
    assertEquals("test", FileUtils.getName(path));
    can = FilePath.get(fsBase).toRealPath().toString();
    String can2 = FileUtils.toRealPath(FileUtils.getParent(path));
    assertEquals(can, can2);
    FileLock lock = channel.tryLock();
    if (lock != null) {
        lock.release();
    }
    assertEquals(10000, channel.size());
    channel.close();
    assertEquals(10000, FileUtils.size(fsBase + "/test"));
    channel = FileUtils.open(fsBase + "/test", "r");
    final byte[] test = new byte[10000];
    FileUtils.readFully(channel, ByteBuffer.wrap(test, 0, 10000));
    assertEquals(buffer, test);
    final FileChannel fc = channel;
    new AssertThrows(IOException.class) {

        @Override
        public void test() throws Exception {
            fc.write(ByteBuffer.wrap(test, 0, 10));
        }
    };
    new AssertThrows(NonWritableChannelException.class) {

        @Override
        public void test() throws Exception {
            fc.truncate(10);
        }
    };
    channel.close();
    long lastMod = FileUtils.lastModified(fsBase + "/test");
    if (lastMod < time - 1999) {
        // at most 2 seconds difference
        assertEquals(time, lastMod);
    }
    assertEquals(10000, FileUtils.size(fsBase + "/test"));
    List<String> list = FileUtils.newDirectoryStream(fsBase);
    assertEquals(1, list.size());
    assertTrue(list.get(0).endsWith("test"));
    IOUtils.copyFiles(fsBase + "/test", fsBase + "/test3");
    FileUtils.move(fsBase + "/test3", fsBase + "/test2");
    FileUtils.move(fsBase + "/test2", fsBase + "/test2");
    assertFalse(FileUtils.exists(fsBase + "/test3"));
    assertTrue(FileUtils.exists(fsBase + "/test2"));
    assertEquals(10000, FileUtils.size(fsBase + "/test2"));
    byte[] buffer2 = new byte[10000];
    InputStream in = FileUtils.newInputStream(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(FileUtils.tryDelete(fsBase + "/test2"));
    FileUtils.delete(fsBase + "/test");
    if (fsBase.indexOf("memFS:") < 0 && fsBase.indexOf("memLZF:") < 0 && fsBase.indexOf("nioMemFS:") < 0 && fsBase.indexOf("nioMemLZF:") < 0) {
        FileUtils.createDirectories(fsBase + "/testDir");
        assertTrue(FileUtils.isDirectory(fsBase + "/testDir"));
        if (!fsBase.startsWith("jdbc:")) {
            FileUtils.deleteRecursive(fsBase + "/testDir", false);
            assertFalse(FileUtils.exists(fsBase + "/testDir"));
        }
    }
}
Also used : FilePath(org.h2.store.fs.FilePath) AssertThrows(org.h2.test.utils.AssertThrows) Random(java.util.Random) FileChannel(java.nio.channels.FileChannel) InputStream(java.io.InputStream) FileLock(java.nio.channels.FileLock)

Example 35 with AssertThrows

use of org.h2.test.utils.AssertThrows in project h2database by h2database.

the class TestFileLock method testSimple.

private void testSimple() {
    FileLock lock1 = new FileLock(new TraceSystem(null), getFile(), Constants.LOCK_SLEEP);
    FileLock lock2 = new FileLock(new TraceSystem(null), getFile(), Constants.LOCK_SLEEP);
    lock1.lock(FileLockMethod.FILE);
    createClassProxy(FileLock.class);
    assertThrows(ErrorCode.DATABASE_ALREADY_OPEN_1, lock2).lock(FileLockMethod.FILE);
    lock1.unlock();
    lock2 = new FileLock(new TraceSystem(null), getFile(), Constants.LOCK_SLEEP);
    lock2.lock(FileLockMethod.FILE);
    lock2.unlock();
}
Also used : FileLock(org.h2.store.FileLock) TraceSystem(org.h2.message.TraceSystem)

Aggregations

Connection (java.sql.Connection)40 Statement (java.sql.Statement)37 PreparedStatement (java.sql.PreparedStatement)35 ResultSet (java.sql.ResultSet)17 JdbcConnection (org.h2.jdbc.JdbcConnection)16 AssertThrows (org.h2.test.utils.AssertThrows)12 SQLException (java.sql.SQLException)8 JdbcSQLException (org.h2.jdbc.JdbcSQLException)8 SimpleResultSet (org.h2.tools.SimpleResultSet)8 CallableStatement (java.sql.CallableStatement)7 Server (org.h2.tools.Server)7 IOException (java.io.IOException)4 Clob (java.sql.Clob)4 Task (org.h2.util.Task)4 Reader (java.io.Reader)3 StringReader (java.io.StringReader)3 Method (java.lang.reflect.Method)3 BigDecimal (java.math.BigDecimal)3 FileChannel (java.nio.channels.FileChannel)3 Savepoint (java.sql.Savepoint)3