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