1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  @test
  26  @summary Tests FileManager API
  27  @summary com.apple.junit.apple.eio;
  28  @run junit FileManagerTests
  29  */
  30 import com.apple.eawt.Application;
  31 import com.apple.eio.FileManager;
  32 import junit.framework.*;
  33 import java.io.File;
  34 
  35 public class FileManagerTests extends TestCase {
  36 
  37     public static Test suite() {
  38         return new TestSuite(FileManagerTests.class);
  39     }
  40 
  41     public static void main (String[] args) throws RuntimeException {
  42         TestResult tr = junit.textui.TestRunner.run(suite());
  43         if((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  44             throw new RuntimeException("### Unexpected junit errors or failures.");
  45         }
  46     }
  47 
  48     protected void tearDown() {
  49         try {
  50             // Close all Finder windows.
  51             Runtime.getRuntime().exec(new String[] {  "/usr/bin/osascript", "-e", "tell application \"Finder\" to close windows"});
  52             // Activate the harness
  53             Application.getApplication().requestForeground(true);
  54             Thread.sleep(1000);
  55         }
  56         catch (Exception e) {
  57             // e.printStackTrace();
  58         }
  59     }
  60 
  61 
  62     /**
  63      * Tests the API FileManager.revealInFinder
  64      * @throws Exception
  65      */
  66     public void testRevealInFinder() throws Exception {
  67         File f  = File.createTempFile("testRevealInFinder", null);
  68         assertTrue( "Tempfile should have been revealed" , FileManager.revealInFinder(f));
  69         Thread.sleep(1000);
  70         f.delete();
  71         File nonExistentFile = new File("/tmp/testRevealInFinder" + ((int)Math.random() * 100000));
  72         if (nonExistentFile.exists()) {
  73             nonExistentFile.delete();
  74         }
  75         try {
  76             FileManager.revealInFinder(nonExistentFile);
  77             fail("Should throw FileNotFoundException");
  78         }
  79         catch (Exception e) {
  80             //e.printStackTrace();
  81         }
  82         // TODO add more comprehensive testcases..
  83     }
  84 
  85     /**
  86      * Tests the API FileManager.moveToTrash
  87      * @throws Exception
  88      */
  89     public void testMoveToTrash() throws Exception {
  90         File f  = File.createTempFile("testMoveToTrash", null);
  91         assertTrue( "Tempfile should have been moved to trash" , FileManager.moveToTrash(f));
  92 
  93         File nonExistentFile = new File("/tmp/testRevealInFinder" + ((int)Math.random() * 100000));
  94         if (nonExistentFile.exists()) {
  95             nonExistentFile.delete();
  96         }
  97         try {
  98             FileManager.moveToTrash(nonExistentFile);
  99             fail("Should throw FileNotFoundException");
 100         }
 101         catch (Exception e) {
 102             //e.printStackTrace();
 103         }
 104         // TODO add more comprehensive testcases..
 105     }
 106 
 107 }
 108 
 109