import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Files {
  private static void expect(boolean v) {
    if (! v) throw new RuntimeException();
  }
  
  private static void isAbsoluteTest(boolean absolutePath) {
    File file = new File("test.txt");
    if (absolutePath) {
      file = file.getAbsoluteFile();
    }
    
    boolean isAbsolute = file.isAbsolute();
    
    if (absolutePath) {
      expect(isAbsolute);
    } else {
      expect(!isAbsolute);
    }
    
  }

  private static void setExecutableTestWithPermissions(boolean executable)
    throws Exception
  {
    File file = File.createTempFile("avian.", null);
    file.setExecutable(executable);
    if (executable) {
      expect(file.canExecute());
    } else {
      // Commented out because this will fail on Windows - both on Avian and on OpenJDK
      // The implementation for Windows considers canExecute() to be the same as canRead()
      // expect(!file.canExecute());
    }
  }
  
  public static void main(String[] args) throws Exception {
    isAbsoluteTest(true);
    isAbsoluteTest(false);
    setExecutableTestWithPermissions(true);
    setExecutableTestWithPermissions(false);
  
    { File f = new File("test.txt");
      f.createNewFile();
      expect(! f.createNewFile());
      f.delete();
    }

    { File f = new File("test.txt");
      FileOutputStream out = new FileOutputStream(f);
      try {
        byte[] message = "hello, world!\n".getBytes();
        out.write(message);
        out.close();

        expect(f.lastModified() > 0);

        FileInputStream in = new FileInputStream(f);
        try {
          expect(in.available() == message.length);
          
          for (int i = 0; i < message.length; ++i) {
            in.read();
            expect(in.available() == message.length - i - 1);
          }
          
          expect(in.read() == -1);
          expect(in.available() == 0);
        } finally {
          in.close();
        }
      } finally {
        f.delete();
      }
    }

    expect(new File("foo/bar").getParent().equals("foo"));
    expect(new File("foo/bar/").getParent().equals("foo"));
    expect(new File("foo/bar//").getParent().equals("foo"));

    expect(new File("foo/nonexistent-directory").listFiles() == null);
  }

}

@@ -672,16 +672,28 @@
  null);%0A
+    try %7B%0A  
     file
@@ -720,24 +720,26 @@
 table);%0A    
+  
 if (executab
@@ -746,24 +746,26 @@
 le) %7B%0A      
+ 
+ 
 expect(file.
@@ -779,24 +779,26 @@
 ute());%0A    
+  
 %7D else %7B%0A   
@@ -790,24 +790,26 @@
    %7D else %7B%0A
+  
       // Com
@@ -890,16 +890,18 @@
 K%0A      
+  
 // The i
@@ -983,16 +983,18 @@
 )%0A      
+  
 // expec
@@ -1012,24 +1012,77 @@
 Execute());%0A
+      %7D%0A    %7D finally %7B%0A      expect(file.delete());%0A
     %7D%0A  %7D%0A  

Delete temporary test file afterwards