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

View File

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

View File

@ -492,8 +492,7 @@ public class FileTest {
} }
} }
//@Test @Test
// TODO: fix and uncomment
public void createTempFileLjava_lang_StringLjava_lang_String() throws IOException { public void createTempFileLjava_lang_StringLjava_lang_String() throws IOException {
// Error protection against using a suffix without a "."? // Error protection against using a suffix without a "."?
File f1 = null; File f1 = null;
@ -559,8 +558,7 @@ public class FileTest {
} }
} }
//@Test @Test
// TODO: fix and uncomment
public void createTempFileLjava_lang_StringLjava_lang_StringLjava_io_File() throws IOException { public void createTempFileLjava_lang_StringLjava_lang_StringLjava_io_File() throws IOException {
File f1 = null; File f1 = null;
File f2 = null; File f2 = null;
@ -1753,8 +1751,7 @@ public class FileTest {
} }
} }
//@Test @Test
// TODO: fix and uncomment
public void setLastModifiedJ() throws IOException { public void setLastModifiedJ() throws IOException {
File f1 = null; File f1 = null;
try { try {
@ -1805,8 +1802,7 @@ public class FileTest {
} }
} }
// @Test @Test
// TODO: fix and uncomment
public void setReadOnly() throws IOException, InterruptedException { public void setReadOnly() throws IOException, InterruptedException {
File f1 = null; File f1 = null;
File f2 = null; File f2 = null;
@ -1826,25 +1822,10 @@ public class FileTest {
// Expected // Expected
} }
Runtime r = Runtime.getRuntime(); 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. // Assert is flawed because canWrite does not work.
// assertTrue("File f2 Is Set To ReadOnly." , f2.canWrite()); // assertTrue("File f2 Is Set To ReadOnly." , f2.canWrite());
fos = new FileOutputStream(f2); FileOutputStream fos = new FileOutputStream(f2);
// Write to a file. // Write to a file.
fos.write(fileString.getBytes()); fos.write(fileString.getBytes());
fos.close(); fos.close();