提交 1dd6fdda authored 作者: Thomas Mueller's avatar Thomas Mueller

Improved tests.

上级 413aeb82
......@@ -65,9 +65,6 @@ public class FilePathDisk extends FilePath {
* @return the native file name
*/
public static String expandUserHomeDirectory(String fileName) {
if (fileName == null) {
return null;
}
boolean prefix = false;
if (fileName.startsWith("file:")) {
prefix = true;
......@@ -236,7 +233,12 @@ public class FilePathDisk extends FilePath {
public void createDirectory() {
File f = new File(name);
if (!f.exists()) {
if (f.exists()) {
if (f.isDirectory()) {
return;
}
throw DbException.get(ErrorCode.FILE_CREATION_FAILED_1, name + " (a file with this name already exists)");
}
File dir = new File(name);
for (int i = 0; i < SysProperties.MAX_FILE_RETRY; i++) {
if ((dir.exists() && dir.isDirectory()) || dir.mkdir()) {
......@@ -246,7 +248,6 @@ public class FilePathDisk extends FilePath {
}
throw DbException.get(ErrorCode.FILE_CREATION_FAILED_1, name);
}
}
public OutputStream newOutputStream(boolean append) {
try {
......
......@@ -18,6 +18,7 @@ import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.h2.compress.CompressLZF;
import org.h2.constant.ErrorCode;
import org.h2.message.DbException;
import org.h2.util.MathUtils;
import org.h2.util.New;
......@@ -105,9 +106,14 @@ public class FilePathMem extends FilePath {
}
public boolean isDirectory() {
if (isRoot()) {
return true;
}
// TODO in memory file system currently
// does not really support directories
return false;
synchronized (MEMORY_FILES) {
return MEMORY_FILES.get(name) == null;
}
}
public boolean isAbsolute() {
......@@ -124,6 +130,9 @@ public class FilePathMem extends FilePath {
}
public void createDirectory() {
if (exists() && isDirectory()) {
throw DbException.get(ErrorCode.FILE_CREATION_FAILED_1, name + " (a file with this name already exists)");
}
// TODO directories are not really supported
}
......@@ -276,6 +285,10 @@ class FileMem extends FileBase {
return null;
}
public String toString() {
return data.getName();
}
}
/**
......
......@@ -94,7 +94,7 @@ class FileNio extends FileBase {
}
public String toString() {
return name;
return "nio:" + name;
}
}
......@@ -374,4 +374,8 @@ class FileSplit extends FileBase {
return list[0].tryLock();
}
public String toString() {
return file.toString();
}
}
......@@ -363,5 +363,4 @@ public class FileUtils {
} while (src.remaining() > 0);
}
}
......@@ -15,6 +15,7 @@ import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.FileChannel.MapMode;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
......@@ -23,6 +24,7 @@ import java.sql.Statement;
import java.util.List;
import java.util.Random;
import org.h2.dev.fs.FilePathCrypt;
import org.h2.message.DbException;
import org.h2.store.fs.FilePath;
import org.h2.store.fs.FileUtils;
import org.h2.test.TestBase;
......@@ -48,6 +50,12 @@ public class TestFileSystem extends TestBase {
}
public void test() throws Exception {
testFileSystem(getBaseDir() + "/fs");
testAbsoluteRelative();
testDirectories(getBaseDir());
testMoveTo(getBaseDir());
testUnsupportedFeatures(getBaseDir());
testMemFsDir();
testClasspath();
FilePathCrypt.register();
......@@ -85,6 +93,12 @@ public class TestFileSystem extends TestBase {
}
}
private void testAbsoluteRelative() {
assertTrue(FileUtils.isAbsolute("/test/abc"));
assertFalse(FileUtils.isAbsolute("test/abc"));
assertTrue(FileUtils.isAbsolute("~/test/abc"));
}
private void testMemFsDir() throws IOException {
FileUtils.newOutputStream("memFS:data/test/a.txt", false).close();
assertEquals(1, FileUtils.newDirectoryStream("memFS:data/test").size());
......@@ -216,11 +230,114 @@ public class TestFileSystem extends TestBase {
}
private void testFileSystem(String fsBase) throws Exception {
testSetReadOnly(fsBase);
testParentEventuallyReturnsNull(fsBase);
testSimple(fsBase);
testTempFile(fsBase);
testRandomAccess(fsBase);
}
private void testSetReadOnly(String fsBase) {
String fileName = fsBase + "/testFile";
if (FileUtils.exists(fileName)) {
FileUtils.delete(fileName);
}
if (FileUtils.createFile(fileName)) {
FileUtils.setReadOnly(fileName);
assertFalse(FileUtils.canWrite(fileName));
FileUtils.delete(fileName);
}
}
private void testDirectories(String fsBase) {
final String fileName = fsBase + "/testFile";
if (FileUtils.exists(fileName)) {
FileUtils.delete(fileName);
}
if (FileUtils.createFile(fileName)) {
new AssertThrows(DbException.class) { public void test() {
FileUtils.createDirectory(fileName);
}};
new AssertThrows(DbException.class) { public void test() {
FileUtils.createDirectories(fileName + "/test");
}};
FileUtils.delete(fileName);
}
}
private void testMoveTo(String fsBase) {
final String fileName = fsBase + "/testFile";
final String fileName2 = fsBase + "/testFile2";
if (FileUtils.exists(fileName)) {
FileUtils.delete(fileName);
}
if (FileUtils.createFile(fileName)) {
FileUtils.moveTo(fileName, fileName2);
FileUtils.createFile(fileName);
new AssertThrows(DbException.class) { public void test() {
FileUtils.moveTo(fileName2, fileName);
}};
FileUtils.delete(fileName);
FileUtils.delete(fileName2);
new AssertThrows(DbException.class) { public void test() {
FileUtils.moveTo(fileName, fileName2);
}};
}
}
private 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) { public void test() throws IOException {
channel.map(MapMode.PRIVATE, 0, channel.size());
}};
new AssertThrows(UnsupportedOperationException.class) { public void test() throws IOException {
channel.read(ByteBuffer.allocate(10), 0);
}};
new AssertThrows(UnsupportedOperationException.class) { public void test() throws IOException {
channel.read(new ByteBuffer[]{ByteBuffer.allocate(10)}, 0, 0);
}};
new AssertThrows(UnsupportedOperationException.class) { public void test() throws IOException {
channel.write(ByteBuffer.allocate(10), 0);
}};
new AssertThrows(UnsupportedOperationException.class) { public void test() throws IOException {
channel.write(new ByteBuffer[]{ByteBuffer.allocate(10)}, 0, 0);
}};
new AssertThrows(UnsupportedOperationException.class) { public void test() throws IOException {
channel.transferFrom(channel, 0, 0);
}};
new AssertThrows(UnsupportedOperationException.class) { public void test() throws IOException {
channel.transferTo(0, 0, channel);
}};
channel.close();
FileUtils.delete(fileName);
}
}
private void testParentEventuallyReturnsNull(String fsBase) {
FilePath p = FilePath.get(fsBase + "/testFile");
assertTrue(p.getScheme().length() > 0);
for (int i = 0; i < 100; i++) {
if (p == null) {
return;
}
p = p.getParent();
}
fail("Parent is not null: " + p);
String path = fsBase + "/testFile";
for (int i = 0; i < 100; i++) {
if (path == null) {
return;
}
path = FileUtils.getParent(path);
}
fail("Parent is not null: " + path);
}
private void testSimple(final String fsBase) throws Exception {
long time = System.currentTimeMillis();
for (String s : FileUtils.newDirectoryStream(fsBase)) {
......@@ -326,6 +443,7 @@ public class TestFileSystem extends TestBase {
RandomAccessFile ra = new RandomAccessFile(file, "rw");
FileUtils.delete(s);
FileChannel f = FileUtils.open(s, "rw");
assertEquals(s, f.toString());
assertEquals(-1, f.read(ByteBuffer.wrap(new byte[1])));
f.force(true);
Random random = new Random(seed);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论