1 /*
   2  * Copyright (c) 2016, 2017, 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  * @bug 8153955
  27  * @summary test the FileHandler's new property
  28  *  "java.util.logging.FileHandler.maxLocks" which will be present in
  29  *  "logging.properties" file with default value of 100. This property can be
  30  *  overriden by specifying this property in the custom config file.
  31  * @library /test/lib
  32  * @author rpatil
  33  * @run main/othervm FileHandlerMaxLocksTest
  34  */
  35 import java.io.File;
  36 import java.io.FileWriter;
  37 import java.io.IOException;
  38 import java.nio.file.Paths;
  39 import java.util.ArrayList;
  40 import java.util.List;
  41 import java.util.logging.FileHandler;
  42 import jdk.test.lib.util.FileUtils;
  43 
  44 public class FileHandlerMaxLocksTest {
  45 
  46     private static final String LOGGER_DIR = "logger-dir";
  47     private static final String MAX_LOCK_PROPERTY = "java.util.logging.FileHandler.maxLocks = 200";
  48     private static final String CONFIG_FILE_NAME = "logging.properties";
  49 
  50     public static void main(String[] args) throws Exception {
  51         File loggerDir = createLoggerDir();
  52         String configFilePath = loggerDir.getPath() + File.separator + CONFIG_FILE_NAME;
  53         File configFile = new File(configFilePath);
  54         createFile(configFile, false);
  55         System.setProperty("java.util.logging.config.file", configFilePath);
  56         List<FileHandler> fileHandlers = new ArrayList<>();
  57         try (FileWriter writer = new FileWriter(configFile)) {
  58             writer.write(MAX_LOCK_PROPERTY);
  59             writer.flush();
  60             // 200 raises the default limit of 100, we try 102 times
  61             for (int i = 0; i < 102; i++) {
  62                 fileHandlers.add(new FileHandler(loggerDir.getPath() + File.separator + "test_%u.log"));
  63             }
  64         } catch (IOException ie) {
  65             throw new RuntimeException("Test Failed: " + ie.getMessage());
  66         } finally {
  67             for (FileHandler fh : fileHandlers) {
  68                 fh.close();
  69             }
  70             FileUtils.deleteFileTreeWithRetry(Paths.get(loggerDir.getPath()));
  71         }
  72     }
  73 
  74     /**
  75      * Create a writable directory in user directory for the test
  76      *
  77      * @return writable directory created that needs to be deleted when done
  78      * @throws RuntimeException
  79      */
  80     private static File createLoggerDir() throws RuntimeException {
  81         String userDir = System.getProperty("user.dir", ".");
  82         File loggerDir = new File(userDir, LOGGER_DIR);
  83         if (!createFile(loggerDir, true)) {
  84             throw new RuntimeException("Test failed: unable to create"
  85                     + " writable working directory "
  86                     + loggerDir.getAbsolutePath());
  87         }
  88         // System.out.println("Created Logger Directory: " + loggerDir.getPath());
  89         return loggerDir;
  90     }
  91 
  92     /**
  93      * @param newFile  File to be created
  94      * @param makeDirectory  is File to be created is directory
  95      * @return true if file already exists or creation succeeded
  96      */
  97     private static boolean createFile(File newFile, boolean makeDirectory) {
  98         if (newFile.exists()) {
  99             return true;
 100         }
 101         if (makeDirectory) {
 102             return newFile.mkdir();
 103         } else {
 104             try {
 105                 return newFile.createNewFile();
 106             } catch (IOException ie) {
 107                 System.err.println("Not able to create file: " + newFile
 108                         + ", IOException: " + ie.getMessage());
 109                 return false;
 110             }
 111         }
 112     }
 113 }