testTempFile(fsBase);
testRandomAccess(fsBase);
}
private void testSimple(String fsBase) throws Exception {
FileSystem fs = FileSystem.getInstance(fsBase);
long time = System.currentTimeMillis();
for (String s : fs.listFiles(fsBase)) {
fs.delete(s);
}
fs.createDirs(fsBase + "/test/x");
fs.delete(fsBase + "/test");
fs.delete(fsBase + "/test2");
assertTrue(fs.createNewFile(fsBase + "/test"));
assertTrue(fs.canWrite(fsBase + "/test"));
FileObject fo = fs.openFileObject(fsBase + "/test", "rw");
byte[] buffer = new byte[10000];
Random random = new Random(1);
random.nextBytes(buffer);
fo.write(buffer, 0, 10000);
assertEquals(10000, fo.length());
fo.seek(20000);
assertEquals(20000, fo.getFilePointer());
assertThrows(EOFException.class, fo).readFully(buffer, 0, 1);
assertEquals(fsBase + "/test", fo.getName().replace('\\', '/'));
assertEquals("test", fs.getFileName(fo.getName()));
assertEquals(fsBase, fs.getParent(fo.getName()).replace('\\', '/'));
fo.tryLock();
fo.releaseLock();
assertEquals(10000, fo.length());
fo.close();
assertEquals(10000, fs.length(fsBase + "/test"));
fo = fs.openFileObject(fsBase + "/test", "r");
byte[] test = new byte[10000];
fo.readFully(test, 0, 10000);
assertEquals(buffer, test);
assertThrows(IOException.class, fo).write(test, 0, 10);
assertThrows(IOException.class, fo).setFileLength(10);
fo.close();
long lastMod = fs.getLastModified(fsBase + "/test");
if (lastMod < time - 1999) {
// at most 2 seconds difference
assertEquals(time, lastMod);
}
assertEquals(10000, fs.length(fsBase + "/test"));
String[] list = fs.listFiles(fsBase);
assertEquals(1, list.length);
assertTrue(list[0].endsWith("test"));
IOUtils.copy(fsBase + "/test", fsBase + "/test3");
fs.rename(fsBase + "/test3", fsBase + "/test2");
assertTrue(!fs.exists(fsBase + "/test3"));
assertTrue(fs.exists(fsBase + "/test2"));
assertEquals(10000, fs.length(fsBase + "/test2"));
byte[] buffer2 = new byte[10000];
InputStream in = fs.openFileInputStream(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(fs.tryDelete(fsBase + "/test2"));
fs.delete(fsBase + "/test");
if (fsBase.indexOf(FileSystemMemory.PREFIX) < 0 && fsBase.indexOf(FileSystemMemory.PREFIX_LZF) < 0) {
fs.createDirs(fsBase + "/testDir/test");
assertTrue(fs.isDirectory(fsBase + "/testDir"));
if (!fsBase.startsWith("jdbc:")) {
fs.deleteRecursive(fsBase + "/testDir", false);
assertTrue(!fs.exists(fsBase + "/testDir"));
}
}
}