提交 1479dc06 authored 作者: Thomas Mueller's avatar Thomas Mueller

Improved compatibility with the Java 7 FileSystem abstraction (improved tests).

上级 1dd6fdda
......@@ -54,6 +54,9 @@ public class FilePathDisk extends FilePath {
*/
protected static String translateFileName(String fileName) {
fileName = fileName.replace('\\', '/');
if (fileName.startsWith("file:")) {
fileName = fileName.substring("file:".length());
}
return expandUserHomeDirectory(fileName);
}
......@@ -65,16 +68,11 @@ public class FilePathDisk extends FilePath {
* @return the native file name
*/
public static String expandUserHomeDirectory(String fileName) {
boolean prefix = false;
if (fileName.startsWith("file:")) {
prefix = true;
fileName = fileName.substring("file:".length());
}
if (fileName.startsWith("~") && (fileName.length() == 1 || fileName.startsWith("~/") || fileName.startsWith("~\\"))) {
if (fileName.startsWith("~") && (fileName.length() == 1 || fileName.startsWith("~/"))) {
String userDir = SysProperties.USER_HOME;
fileName = userDir + fileName.substring(1);
}
return prefix ? "file:" + fileName : fileName;
return fileName;
}
public void moveTo(FilePath newName) {
......
......@@ -314,7 +314,6 @@ class FileMemData {
private byte[][] data;
private long lastModified;
private boolean isReadOnly;
private volatile boolean locked;
static {
byte[] n = new byte[BLOCK_SIZE];
......@@ -564,26 +563,6 @@ class FileMemData {
return true;
}
/**
* Lock the file.
*
* @return if successful
*/
synchronized boolean tryLock() {
if (locked) {
return false;
}
locked = true;
return true;
}
/**
* Unlock the file.
*/
public synchronized void releaseLock() {
locked = false;
}
}
......@@ -151,8 +151,7 @@ public class FilePathSplit extends FilePathWrapper {
closeAndThrow(array.length - 1, array, c, maxLength);
}
}
FileSplit fo = new FileSplit(this, mode, array, length, maxLength);
return fo;
return new FileSplit(this, mode, array, length, maxLength);
}
private long getDefaultMaxLength() {
......
......@@ -174,7 +174,7 @@ public class TestFileSystem extends TestBase {
FileUtils.delete(getBaseDir() + "/fsMem.zip");
}
private void testDatabaseInJar() throws SQLException {
private void testDatabaseInJar() throws Exception {
if (getBaseDir().indexOf(':') > 0) {
return;
}
......@@ -204,6 +204,16 @@ public class TestFileSystem extends TestBase {
assertTrue(!FileUtils.isDirectory(f));
assertTrue(FileUtils.size(f) > 0);
assertTrue(f.endsWith(FileUtils.getName(f)));
assertEquals(0, FileUtils.lastModified(f));
FileUtils.setReadOnly(f);
assertFalse(FileUtils.canWrite(f));
InputStream in = FileUtils.newInputStream(f);
int len = 0;
while (in.read() >= 0) {
len++;
}
assertEquals(len, FileUtils.size(f));
testReadOnly(f);
}
String urlJar = "jdbc:h2:zip:" + getBaseDir() + "/fsJar.zip!/fsJar";
conn = DriverManager.getConnection(urlJar, "sa", "sa");
......@@ -223,10 +233,35 @@ public class TestFileSystem extends TestBase {
FileUtils.delete(getBaseDir() + "/fsJar.zip");
}
private void testReadOnly(final String f) throws IOException {
new AssertThrows(DbException.class) { public void test() {
FileUtils.newOutputStream(f, false);
}};
new AssertThrows(DbException.class) { public void test() {
FileUtils.moveTo(f, f);
}};
new AssertThrows(DbException.class) { public void test() {
FileUtils.moveTo(f, f);
}};
new AssertThrows(IOException.class) { public void test() throws IOException {
FileUtils.createTempFile(f, ".tmp", false, false);
}};
final FileChannel channel = FileUtils.open(f, "r");
new AssertThrows(IOException.class) { public void test() throws IOException {
channel.write(ByteBuffer.allocate(1));
}};
new AssertThrows(IOException.class) { public void test() throws IOException {
channel.truncate(0);
}};
assertTrue(null == channel.tryLock());
channel.force(false);
channel.close();
}
private void testUserHome() {
String fileName = FileUtils.toRealPath("~/test");
String userDir = System.getProperty("user.home").replace('\\', '/');
assertTrue(fileName.startsWith(userDir));
assertTrue(FileUtils.toRealPath("~/test").startsWith(userDir));
assertTrue(FileUtils.toRealPath("file:~/test").startsWith(userDir));
}
private void testFileSystem(String fsBase) throws Exception {
......@@ -313,6 +348,9 @@ public class TestFileSystem extends TestBase {
new AssertThrows(UnsupportedOperationException.class) { public void test() throws IOException {
channel.transferTo(0, 0, channel);
}};
new AssertThrows(UnsupportedOperationException.class) { public void test() throws IOException {
channel.lock();
}};
channel.close();
FileUtils.delete(fileName);
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论