Uncomment previously failing File tests and fix bugs in File implementation

This commit is contained in:
Alexey Andreev 2017-11-15 22:40:59 +03:00
parent 105c188953
commit a96b5912c7
3 changed files with 20 additions and 27 deletions

View File

@ -376,7 +376,7 @@ public class TFile implements Serializable, Comparable<TFile> {
if (parentVirtualFile == null) {
throw new IOException("Can't create file " + getPath() + " since parent directory does not exist");
}
if (!parentVirtualFile.isDirectory()) {
if (!parentVirtualFile.isDirectory() || !parentVirtualFile.canWrite()) {
throw new IOException("Can't create file " + getPath() + " since parent path denotes regular file");
}
@ -389,7 +389,7 @@ public class TFile implements Serializable, Comparable<TFile> {
public boolean mkdir() {
VirtualFile virtualFile = findParentFile();
if (virtualFile == null || !virtualFile.isDirectory()) {
if (virtualFile == null || !virtualFile.isDirectory() || !virtualFile.canWrite()) {
return false;
}
@ -413,6 +413,9 @@ public class TFile implements Serializable, Comparable<TFile> {
String name = path.substring(i, next);
VirtualFile child = virtualFile.getChildFile(name);
if (child == null) {
if (!virtualFile.canWrite()) {
return false;
}
virtualFile = virtualFile.createDirectory(name);
} else if (child.isFile()) {
return false;
@ -428,11 +431,16 @@ public class TFile implements Serializable, Comparable<TFile> {
public boolean delete() {
VirtualFile virtualFile = findVirtualFile();
if (virtualFile == null || virtualFile == fs().getRootFile() || !virtualFile.canWrite()
if (virtualFile == null || virtualFile == fs().getRootFile()
|| (virtualFile.isDirectory() && virtualFile.listFiles().length > 0)) {
return false;
}
VirtualFile parentVirtualFile = findParentFile();
if (parentVirtualFile != null && !parentVirtualFile.canWrite()) {
return false;
}
virtualFile.delete();
return true;
}

View File

@ -43,6 +43,10 @@ public class TFileOutputStream extends OutputStream {
throw new FileNotFoundException();
}
if (!virtualFile.canWrite()) {
throw new FileNotFoundException("File is read-only");
}
accessor = virtualFile.createAccessor();
if (accessor == null) {
throw new FileNotFoundException();

View File

@ -492,8 +492,7 @@ public class FileTest {
}
}
//@Test
// TODO: fix and uncomment
@Test
public void createTempFileLjava_lang_StringLjava_lang_String() throws IOException {
// Error protection against using a suffix without a "."?
File f1 = null;
@ -559,8 +558,7 @@ public class FileTest {
}
}
//@Test
// TODO: fix and uncomment
@Test
public void createTempFileLjava_lang_StringLjava_lang_StringLjava_io_File() throws IOException {
File f1 = null;
File f2 = null;
@ -1753,8 +1751,7 @@ public class FileTest {
}
}
//@Test
// TODO: fix and uncomment
@Test
public void setLastModifiedJ() throws IOException {
File f1 = null;
try {
@ -1805,8 +1802,7 @@ public class FileTest {
}
}
// @Test
// TODO: fix and uncomment
@Test
public void setReadOnly() throws IOException, InterruptedException {
File f1 = null;
File f2 = null;
@ -1826,25 +1822,10 @@ public class FileTest {
// Expected
}
Runtime r = Runtime.getRuntime();
Process p;
boolean onUnix = File.separatorChar == '/';
if (onUnix) {
p = r.exec("chmod +w " + f1.getAbsolutePath());
} else {
p = r.exec("attrib -r \"" + f1.getAbsolutePath() + "\"");
}
p.waitFor();
// Assert is flawed because canWrite does not work.
// assertTrue("File f1 Is Set To ReadOnly." , f1.canWrite());
FileOutputStream fos = new FileOutputStream(f1);
fos.write(fileString.getBytes());
fos.close();
assertTrue("File Was Not Able To Be Written To.", f1.length() == fileString.length());
assertTrue("File f1 Did Not Delete", f1.delete());
// Assert is flawed because canWrite does not work.
// assertTrue("File f2 Is Set To ReadOnly." , f2.canWrite());
fos = new FileOutputStream(f2);
FileOutputStream fos = new FileOutputStream(f2);
// Write to a file.
fos.write(fileString.getBytes());
fos.close();