From a96b5912c758cabae3b4bc2feb336f33e912c928 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Wed, 15 Nov 2017 22:40:59 +0300 Subject: [PATCH] Uncomment previously failing File tests and fix bugs in File implementation --- .../org/teavm/classlib/java/io/TFile.java | 14 +++++++-- .../classlib/java/io/TFileOutputStream.java | 4 +++ .../org/teavm/classlib/java/io/FileTest.java | 29 ++++--------------- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/io/TFile.java b/classlib/src/main/java/org/teavm/classlib/java/io/TFile.java index ddf384887..118d0fa2c 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/io/TFile.java +++ b/classlib/src/main/java/org/teavm/classlib/java/io/TFile.java @@ -376,7 +376,7 @@ public class TFile implements Serializable, Comparable { 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 { 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 { 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 { 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; } diff --git a/classlib/src/main/java/org/teavm/classlib/java/io/TFileOutputStream.java b/classlib/src/main/java/org/teavm/classlib/java/io/TFileOutputStream.java index 13c4f5909..a833d4c41 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/io/TFileOutputStream.java +++ b/classlib/src/main/java/org/teavm/classlib/java/io/TFileOutputStream.java @@ -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(); diff --git a/tests/src/test/java/org/teavm/classlib/java/io/FileTest.java b/tests/src/test/java/org/teavm/classlib/java/io/FileTest.java index 94b0a2cd6..ebecad56f 100644 --- a/tests/src/test/java/org/teavm/classlib/java/io/FileTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/io/FileTest.java @@ -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();