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();
}
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);
}
}
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();
}
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"));
}
}
}
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();
}
Aggregations