test/java/util/logging/CheckLockLocationTest.java

Print this page
rev 6154 : 8003596: TEST_BUG: java/util/logging/CheckLockLocationTest.java failing [win]
Summary: Remove test that requires setWritable when running on Windows
Reviewed-by: alanb


  25  * @test
  26  * @bug     6244047
  27  * @author Jim Gish
  28  * @summary throw more precise IOException when pattern specifies invalid directory
  29  *
  30  * @run  main/othervm CheckLockLocationTest
  31  */
  32 import java.io.File;
  33 import java.io.FileNotFoundException;
  34 import java.io.IOException;
  35 import java.nio.file.AccessDeniedException;
  36 import java.nio.file.FileSystemException;
  37 import java.nio.file.NoSuchFileException;
  38 import java.util.logging.FileHandler;
  39 public class CheckLockLocationTest {
  40 
  41     private static final String NON_WRITABLE_DIR = "non-writable-dir";
  42     private static final String NOT_A_DIR = "not-a-dir";
  43     private static final String WRITABLE_DIR = "writable-dir";
  44     private static final String NON_EXISTENT_DIR = "non-existent-dir";

  45 
  46     public static void main(String... args) throws IOException {
  47         // we'll base all file creation attempts on the system temp directory,
  48         // %t and also try specifying non-existent directories and plain files
  49         // that should be directories, and non-writable directories,
  50         // to exercise all code paths of checking the lock location




  51         File writableDir = setup();
  52         // we now have three files/directories to work with:
  53         //    writableDir
  54         //    notAdir
  55         //    nonWritableDir
  56         //    nonExistentDir (which doesn't exist)
  57         runTests(writableDir);
  58     }
  59 
  60     /**
  61      * @param writableDir in which log and lock file are created
  62      * @throws SecurityException
  63      * @throws RuntimeException
  64      * @throws IOException
  65      */
  66     private static void runTests(File writableDir) throws SecurityException,
  67             RuntimeException, IOException {
  68         // Test 1: make sure we can create FileHandler in writable directory
  69         try {
  70             new FileHandler("%t/" + WRITABLE_DIR + "/log.log");
  71         } catch (IOException ex) {
  72             throw new RuntimeException("Test failed: should have been able"
  73                     + " to create FileHandler for " + "%t/" + WRITABLE_DIR
  74                     + "/log.log in writable directory.", ex);
  75         } finally {
  76             // the above test leaves files in the directory.  Get rid of the
  77             // files created and the directory
  78             delete(writableDir);
  79         }
  80 
  81         // Test 2: creating FileHandler in non-writable directory should fail

  82         try {
  83             new FileHandler("%t/" + NON_WRITABLE_DIR + "/log.log");
  84             throw new RuntimeException("Test failed: should not have been able"
  85                     + " to create FileHandler for " + "%t/" + NON_WRITABLE_DIR
  86                     + "/log.log in non-writable directory.");
  87         } catch (IOException ex) {
  88             // check for the right exception
  89             if (!(ex instanceof AccessDeniedException)) {
  90                 throw new RuntimeException("Test failed: Expected exception was not an AccessDeniedException", ex);



  91             }
  92         }
  93 
  94         // Test 3: creating FileHandler in non-directory should fail
  95         try {
  96             new FileHandler("%t/" + NOT_A_DIR + "/log.log");
  97             throw new RuntimeException("Test failed: should not have been able"
  98                     + " to create FileHandler for " + "%t/" + NOT_A_DIR
  99                     + "/log.log in non-directory.");
 100         } catch (IOException ex) {
 101             // check for the right exception
 102             if (!(ex instanceof FileSystemException && ex.getMessage().contains("Not a directory"))) {
 103                 throw new RuntimeException("Test failed: Expected exception was not a FileSystemException", ex);



 104             }
 105         }
 106 
 107         // Test 4: make sure we can't create a FileHandler in a non-existent dir
 108         try {
 109             new FileHandler("%t/" + NON_EXISTENT_DIR + "/log.log");
 110             throw new RuntimeException("Test failed: should not have been able"
 111                     + " to create FileHandler for " + "%t/" + NON_EXISTENT_DIR
 112                     + "/log.log in a non-existent directory.");
 113         } catch (IOException ex) {
 114             // check for the right exception
 115             if (!(ex instanceof NoSuchFileException)) {
 116                 throw new RuntimeException("Test failed: Expected exception was not a NoSuchFileException", ex);

 117             }
 118         }
 119     }
 120 
 121     /**
 122      * Setup all the files and directories needed for the tests
 123      *
 124      * @return writable directory created that needs to be deleted when done
 125      * @throws RuntimeException
 126      */
 127     private static File setup() throws RuntimeException {
 128         // First do some setup in the temporary directory (using same logic as
 129         // FileHandler for %t pattern)
 130         String tmpDir = System.getProperty("java.io.tmpdir"); // i.e. %t
 131         if (tmpDir == null) {
 132             tmpDir = System.getProperty("user.home");
 133         }
 134         File tmpOrHomeDir = new File(tmpDir);
 135         // Create a writable directory here (%t/writable-dir)
 136         File writableDir = new File(tmpOrHomeDir, WRITABLE_DIR);


 145         // Create a plain file which we will attempt to use as a directory
 146         // (%t/not-a-dir)
 147         File notAdir = new File(tmpOrHomeDir, NOT_A_DIR);
 148         if (!createFile(notAdir, false)) {
 149             throw new RuntimeException("Test setup failed: unable to a plain"
 150                     + " working file " + notAdir.getAbsolutePath() );
 151         }
 152         notAdir.deleteOnExit();
 153 
 154         // Create a non-writable directory (%t/non-writable-dir)
 155         File nonWritableDir = new File(tmpOrHomeDir, NON_WRITABLE_DIR);
 156         if (!createFile(nonWritableDir, true)) {
 157             throw new RuntimeException("Test setup failed: unable to create"
 158                     + " a non-"
 159                     + "writable working directory "
 160                     + nonWritableDir.getAbsolutePath() );
 161         }
 162         nonWritableDir.deleteOnExit();
 163 
 164         // make it non-writable
 165         if (!nonWritableDir.setWritable(false)) {
 166             throw new RuntimeException("Test setup failed: unable to make"



 167                     + " working directory " + nonWritableDir.getAbsolutePath()
 168                     + " non-writable.");

 169         }
 170 
 171         // make sure non-existent directory really doesn't exist
 172         File nonExistentDir = new File(tmpOrHomeDir, NON_EXISTENT_DIR);
 173         if (nonExistentDir.exists()) {
 174             nonExistentDir.delete();
 175         }
 176         return writableDir;
 177     }
 178 
 179     /**
 180      * @param newFile
 181      * @return true if file already exists or creation succeeded
 182      */
 183     private static boolean createFile(File newFile, boolean makeDirectory) {
 184         if (newFile.exists()) {
 185             return true;
 186         }
 187         if (makeDirectory) {
 188             return newFile.mkdir();




  25  * @test
  26  * @bug     6244047
  27  * @author Jim Gish
  28  * @summary throw more precise IOException when pattern specifies invalid directory
  29  *
  30  * @run  main/othervm CheckLockLocationTest
  31  */
  32 import java.io.File;
  33 import java.io.FileNotFoundException;
  34 import java.io.IOException;
  35 import java.nio.file.AccessDeniedException;
  36 import java.nio.file.FileSystemException;
  37 import java.nio.file.NoSuchFileException;
  38 import java.util.logging.FileHandler;
  39 public class CheckLockLocationTest {
  40 
  41     private static final String NON_WRITABLE_DIR = "non-writable-dir";
  42     private static final String NOT_A_DIR = "not-a-dir";
  43     private static final String WRITABLE_DIR = "writable-dir";
  44     private static final String NON_EXISTENT_DIR = "non-existent-dir";
  45     private static boolean runNonWritableDirTest;
  46 
  47     public static void main(String... args) throws IOException {
  48         // we'll base all file creation attempts on the system temp directory,
  49         // %t and also try specifying non-existent directories and plain files
  50         // that should be directories, and non-writable directories,
  51         // to exercise all code paths of checking the lock location
  52         // Note that on platforms like Windows that don't support
  53         // setWritable() on a directory, we'll skip the non-writable
  54         // directory test if setWritable(false) returns false.
  55         //
  56         File writableDir = setup();
  57         // we now have three files/directories to work with:
  58         //    writableDir
  59         //    notAdir
  60         //    nonWritableDir (may not be possible on some platforms)
  61         //    nonExistentDir (which doesn't exist)
  62         runTests(writableDir);
  63     }
  64 
  65     /**
  66      * @param writableDir in which log and lock file are created
  67      * @throws SecurityException
  68      * @throws RuntimeException
  69      * @throws IOException
  70      */
  71     private static void runTests(File writableDir) throws SecurityException,
  72             RuntimeException, IOException {
  73         // Test 1: make sure we can create FileHandler in writable directory
  74         try {
  75             new FileHandler("%t/" + WRITABLE_DIR + "/log.log");
  76         } catch (IOException ex) {
  77             throw new RuntimeException("Test failed: should have been able"
  78                     + " to create FileHandler for " + "%t/" + WRITABLE_DIR
  79                     + "/log.log in writable directory.", ex);
  80         } finally {
  81             // the above test leaves files in the directory.  Get rid of the
  82             // files created and the directory
  83             delete(writableDir);
  84         }
  85 
  86         // Test 2: creating FileHandler in non-writable directory should fail
  87         if (runNonWritableDirTest) {
  88             try {
  89                 new FileHandler("%t/" + NON_WRITABLE_DIR + "/log.log");
  90                 throw new RuntimeException("Test failed: should not have been able"
  91                         + " to create FileHandler for " + "%t/" + NON_WRITABLE_DIR
  92                         + "/log.log in non-writable directory.");
  93             } catch (IOException ex) {
  94                 // check for the right exception
  95                 if (!(ex instanceof AccessDeniedException)) {
  96                     throw new RuntimeException(
  97                         "Test failed: Expected exception was not an "
  98                                 + "AccessDeniedException", ex);
  99                 }
 100             }
 101         }
 102 
 103         // Test 3: creating FileHandler in non-directory should fail
 104         try {
 105             new FileHandler("%t/" + NOT_A_DIR + "/log.log");
 106             throw new RuntimeException("Test failed: should not have been able"
 107                     + " to create FileHandler for " + "%t/" + NOT_A_DIR
 108                     + "/log.log in non-directory.");
 109         } catch (IOException ex) {
 110             // check for the right exception
 111             if (!(ex instanceof FileSystemException
 112                     && ex.getMessage().contains("Not a directory"))) {
 113                 throw new RuntimeException(
 114                         "Test failed: Expected exception was not a "
 115                         + "FileSystemException", ex);
 116             }
 117         }
 118 
 119         // Test 4: make sure we can't create a FileHandler in a non-existent dir
 120         try {
 121             new FileHandler("%t/" + NON_EXISTENT_DIR + "/log.log");
 122             throw new RuntimeException("Test failed: should not have been able"
 123                     + " to create FileHandler for " + "%t/" + NON_EXISTENT_DIR
 124                     + "/log.log in a non-existent directory.");
 125         } catch (IOException ex) {
 126             // check for the right exception
 127             if (!(ex instanceof NoSuchFileException)) {
 128                 throw new RuntimeException("Test failed: Expected exception "
 129                         + "was not a NoSuchFileException", ex);
 130             }
 131         }
 132     }
 133 
 134     /**
 135      * Setup all the files and directories needed for the tests
 136      *
 137      * @return writable directory created that needs to be deleted when done
 138      * @throws RuntimeException
 139      */
 140     private static File setup() throws RuntimeException {
 141         // First do some setup in the temporary directory (using same logic as
 142         // FileHandler for %t pattern)
 143         String tmpDir = System.getProperty("java.io.tmpdir"); // i.e. %t
 144         if (tmpDir == null) {
 145             tmpDir = System.getProperty("user.home");
 146         }
 147         File tmpOrHomeDir = new File(tmpDir);
 148         // Create a writable directory here (%t/writable-dir)
 149         File writableDir = new File(tmpOrHomeDir, WRITABLE_DIR);


 158         // Create a plain file which we will attempt to use as a directory
 159         // (%t/not-a-dir)
 160         File notAdir = new File(tmpOrHomeDir, NOT_A_DIR);
 161         if (!createFile(notAdir, false)) {
 162             throw new RuntimeException("Test setup failed: unable to a plain"
 163                     + " working file " + notAdir.getAbsolutePath() );
 164         }
 165         notAdir.deleteOnExit();
 166 
 167         // Create a non-writable directory (%t/non-writable-dir)
 168         File nonWritableDir = new File(tmpOrHomeDir, NON_WRITABLE_DIR);
 169         if (!createFile(nonWritableDir, true)) {
 170             throw new RuntimeException("Test setup failed: unable to create"
 171                     + " a non-"
 172                     + "writable working directory "
 173                     + nonWritableDir.getAbsolutePath() );
 174         }
 175         nonWritableDir.deleteOnExit();
 176 
 177         // make it non-writable
 178         if (nonWritableDir.setWritable(false)) {
 179             runNonWritableDirTest = true;
 180         } else {
 181             runNonWritableDirTest = false;
 182             System.out.println( "Test Setup WARNING: unable to make"
 183                     + " working directory " + nonWritableDir.getAbsolutePath()
 184                     + " non-writable on platform " + System.getProperty("os.name"));
 185 
 186         }
 187 
 188         // make sure non-existent directory really doesn't exist
 189         File nonExistentDir = new File(tmpOrHomeDir, NON_EXISTENT_DIR);
 190         if (nonExistentDir.exists()) {
 191             nonExistentDir.delete();
 192         }
 193         return writableDir;
 194     }
 195 
 196     /**
 197      * @param newFile
 198      * @return true if file already exists or creation succeeded
 199      */
 200     private static boolean createFile(File newFile, boolean makeDirectory) {
 201         if (newFile.exists()) {
 202             return true;
 203         }
 204         if (makeDirectory) {
 205             return newFile.mkdir();