< prev index next >

test/jdk/java/util/logging/LogManager/Configuration/updateConfiguration/HandlersOnComplexUpdate.java

Print this page


   1 /*
   2  * Copyright (c) 2015, 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  */


  63  * @author danielfuchs
  64  */
  65 public class HandlersOnComplexUpdate {
  66 
  67     /**
  68      * We will test the handling of abstract logger nodes with file handlers in
  69      * two configurations:
  70      * UNSECURE: No security manager.
  71      * SECURE: With the security manager present - and the required
  72      *         permissions granted.
  73      */
  74     public static enum TestCase {
  75         UNSECURE, SECURE;
  76         public void run(List<Properties> properties) throws Exception {
  77             System.out.println("Running test case: " + name());
  78             Configure.setUp(this, properties.get(0));
  79             test(this.name(), properties);
  80         }
  81     }
  82 








  83 
  84     private static final String PREFIX =
  85             "FileHandler-" + UUID.randomUUID() + ".log";
  86     private static final String userDir = System.getProperty("user.dir", ".");
  87     private static final boolean userDirWritable = Files.isWritable(Paths.get(userDir));
  88 
  89     private static final List<Properties> properties;
  90     static {
  91         // The test will call updateConfiguration() with each of these
  92         // properties in sequence. The child logger is not released between each
  93         // configuration. What is interesting here is mostly what happens between
  94         // props4 and props5:
  95         //
  96         // In step 4 (props4) the configuration defines a handler for the
  97         // logger com.foo (the direct parent of com.foo.child - which is the
  98         // logger we hold on to).
  99         //
 100         // In step 5 (props5) the configuration has nothing concerning
 101         // 'com.foo', but the handler has been migrated to 'com'.
 102         // Since there doesn't exist any logger for 'com' (the previous


 196         }
 197 
 198         // Then create a child of the com.foo logger.
 199         Logger fooChild = Logger.getLogger("com.foo.child");
 200         fooChild.info("hello world");
 201         Logger barChild = Logger.getLogger("com.bar.child");
 202         barChild.info("hello world");
 203 
 204         ReferenceQueue<Logger> queue = new ReferenceQueue();
 205         WeakReference<Logger> fooRef = new WeakReference<>(Logger.getLogger("com.foo"), queue);
 206         if (fooRef.get() != fooChild.getParent()) {
 207             throw new RuntimeException("Unexpected parent logger: "
 208                     + fooChild.getParent() +"\n\texpected: " + fooRef.get());
 209         }
 210         WeakReference<Logger> barRef = new WeakReference<>(Logger.getLogger("com.bar"), queue);
 211         if (barRef.get() != barChild.getParent()) {
 212             throw new RuntimeException("Unexpected parent logger: "
 213                     + barChild.getParent() +"\n\texpected: " + barRef.get());
 214         }
 215         Reference<? extends Logger> ref2;
 216         int max = 3;
 217         barChild = null;
 218         while ((ref2 = queue.poll()) == null) {
 219             System.gc();
 220             Thread.sleep(100);
 221             if (--max == 0) break;
 222         }
 223 
 224         Throwable failed = null;
 225         try {
 226             if (ref2 != null) {
 227                 String refName = ref2 == fooRef ? "fooRef" : ref2 == barRef ? "barRef" : "unknown";
 228                 if (ref2 != barRef) {
 229                     throw new RuntimeException("Unexpected logger reference cleared: " + refName);
 230                 } else {
 231                     System.out.println("Reference " + refName + " cleared as expected");
 232                 }
 233             } else if (ref2 == null) {
 234                 throw new RuntimeException("Expected 'barRef' to be cleared");
 235             }
 236             // Now lets try to check handlers, and
 237             // attempt to update the configuration again.
 238             Properties previousProps  = properties.get(0);
 239             int expectedHandlersCount = 1;
 240             boolean checkHandlersOnParent = Boolean.parseBoolean(


 299                 try {
 300                     StringBuilder builder = new StringBuilder();
 301                     Files.list(Paths.get(userDir))
 302                         .filter((f) -> f.toString().contains(PREFIX))
 303                         .filter((f) -> f.toString().endsWith(".lck"))
 304                         .forEach((f) -> {
 305                                 builder.append(f.toString()).append('\n');
 306                         });
 307                     if (!builder.toString().isEmpty()) {
 308                         throw new RuntimeException("Lock files not cleaned:\n"
 309                                 + builder.toString());
 310                     }
 311                 } catch(RuntimeException | Error x) {
 312                     if (suppressed != null) x.addSuppressed(suppressed);
 313                     throw x;
 314                 } catch(Exception x) {
 315                     if (suppressed != null) x.addSuppressed(suppressed);
 316                     throw new RuntimeException(x);
 317                 }
 318             });

 319             fooChild = null;
 320             System.out.println("Setting fooChild to: " + fooChild);
 321             while ((ref2 = queue.poll()) == null) {
 322                 System.gc();
 323                 Thread.sleep(1000);
 324             }
 325             if (ref2 != fooRef) {
 326                 throw new RuntimeException("Unexpected reference: "
 327                         + ref2 +"\n\texpected: " + fooRef);
 328             }
 329             if (ref2.get() != null) {
 330                 throw new RuntimeException("Referent not cleared: " + ref2.get());
 331             }
 332             System.out.println("Got fooRef after reset(), fooChild is " + fooChild);
 333 



 334         }
 335         if (failed != null) {
 336             // should rarely happen...
 337             throw new RuntimeException(failed);
 338         }
 339 
 340     }
 341 
 342     public static void main(String... args) throws Exception {
 343 
 344 
 345         if (args == null || args.length == 0) {
 346             args = new String[] {
 347                 TestCase.UNSECURE.name(),
 348                 TestCase.SECURE.name(),
 349             };
 350         }
 351 
 352         try {
 353             for (String testName : args) {


   1 /*
   2  * Copyright (c) 2015, 2019, 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  */


  63  * @author danielfuchs
  64  */
  65 public class HandlersOnComplexUpdate {
  66 
  67     /**
  68      * We will test the handling of abstract logger nodes with file handlers in
  69      * two configurations:
  70      * UNSECURE: No security manager.
  71      * SECURE: With the security manager present - and the required
  72      *         permissions granted.
  73      */
  74     public static enum TestCase {
  75         UNSECURE, SECURE;
  76         public void run(List<Properties> properties) throws Exception {
  77             System.out.println("Running test case: " + name());
  78             Configure.setUp(this, properties.get(0));
  79             test(this.name(), properties);
  80         }
  81     }
  82 
  83     public static final double TIMEOUT_FACTOR;
  84     static {
  85         String toFactor = System.getProperty("test.timeout.factor", "1.0");
  86         TIMEOUT_FACTOR = Double.parseDouble(toFactor);
  87     }
  88     static int adjustCount(int count) {
  89         return (int) Math.ceil(TIMEOUT_FACTOR * count);
  90     }
  91 
  92     private static final String PREFIX =
  93             "FileHandler-" + UUID.randomUUID() + ".log";
  94     private static final String userDir = System.getProperty("user.dir", ".");
  95     private static final boolean userDirWritable = Files.isWritable(Paths.get(userDir));
  96 
  97     private static final List<Properties> properties;
  98     static {
  99         // The test will call updateConfiguration() with each of these
 100         // properties in sequence. The child logger is not released between each
 101         // configuration. What is interesting here is mostly what happens between
 102         // props4 and props5:
 103         //
 104         // In step 4 (props4) the configuration defines a handler for the
 105         // logger com.foo (the direct parent of com.foo.child - which is the
 106         // logger we hold on to).
 107         //
 108         // In step 5 (props5) the configuration has nothing concerning
 109         // 'com.foo', but the handler has been migrated to 'com'.
 110         // Since there doesn't exist any logger for 'com' (the previous


 204         }
 205 
 206         // Then create a child of the com.foo logger.
 207         Logger fooChild = Logger.getLogger("com.foo.child");
 208         fooChild.info("hello world");
 209         Logger barChild = Logger.getLogger("com.bar.child");
 210         barChild.info("hello world");
 211 
 212         ReferenceQueue<Logger> queue = new ReferenceQueue();
 213         WeakReference<Logger> fooRef = new WeakReference<>(Logger.getLogger("com.foo"), queue);
 214         if (fooRef.get() != fooChild.getParent()) {
 215             throw new RuntimeException("Unexpected parent logger: "
 216                     + fooChild.getParent() +"\n\texpected: " + fooRef.get());
 217         }
 218         WeakReference<Logger> barRef = new WeakReference<>(Logger.getLogger("com.bar"), queue);
 219         if (barRef.get() != barChild.getParent()) {
 220             throw new RuntimeException("Unexpected parent logger: "
 221                     + barChild.getParent() +"\n\texpected: " + barRef.get());
 222         }
 223         Reference<? extends Logger> ref2;
 224         int max = adjustCount(3);
 225         barChild = null;
 226         while ((ref2 = queue.poll()) == null) {
 227             System.gc();
 228             Thread.sleep(1000);
 229             if (--max == 0) break;
 230         }
 231 
 232         Throwable failed = null;
 233         try {
 234             if (ref2 != null) {
 235                 String refName = ref2 == fooRef ? "fooRef" : ref2 == barRef ? "barRef" : "unknown";
 236                 if (ref2 != barRef) {
 237                     throw new RuntimeException("Unexpected logger reference cleared: " + refName);
 238                 } else {
 239                     System.out.println("Reference " + refName + " cleared as expected");
 240                 }
 241             } else if (ref2 == null) {
 242                 throw new RuntimeException("Expected 'barRef' to be cleared");
 243             }
 244             // Now lets try to check handlers, and
 245             // attempt to update the configuration again.
 246             Properties previousProps  = properties.get(0);
 247             int expectedHandlersCount = 1;
 248             boolean checkHandlersOnParent = Boolean.parseBoolean(


 307                 try {
 308                     StringBuilder builder = new StringBuilder();
 309                     Files.list(Paths.get(userDir))
 310                         .filter((f) -> f.toString().contains(PREFIX))
 311                         .filter((f) -> f.toString().endsWith(".lck"))
 312                         .forEach((f) -> {
 313                                 builder.append(f.toString()).append('\n');
 314                         });
 315                     if (!builder.toString().isEmpty()) {
 316                         throw new RuntimeException("Lock files not cleaned:\n"
 317                                 + builder.toString());
 318                     }
 319                 } catch(RuntimeException | Error x) {
 320                     if (suppressed != null) x.addSuppressed(suppressed);
 321                     throw x;
 322                 } catch(Exception x) {
 323                     if (suppressed != null) x.addSuppressed(suppressed);
 324                     throw new RuntimeException(x);
 325                 }
 326             });
 327             try {
 328                 fooChild = null;
 329                 System.out.println("Setting fooChild to: " + fooChild);
 330                 while ((ref2 = queue.poll()) == null) {
 331                     System.gc();
 332                     Thread.sleep(1000);
 333                 }
 334                 if (ref2 != fooRef) {
 335                     throw new RuntimeException("Unexpected reference: "
 336                             + ref2 +"\n\texpected: " + fooRef);
 337                 }
 338                 if (ref2.get() != null) {
 339                     throw new RuntimeException("Referent not cleared: " + ref2.get());
 340                 }
 341                 System.out.println("Got fooRef after reset(), fooChild is " + fooChild);
 342            } catch (Throwable t) {
 343                if (failed != null) t.addSuppressed(failed);
 344                throw t;
 345            }
 346         }
 347         if (failed != null) {
 348             // should rarely happen...
 349             throw new RuntimeException(failed);
 350         }
 351 
 352     }
 353 
 354     public static void main(String... args) throws Exception {
 355 
 356 
 357         if (args == null || args.length == 0) {
 358             args = new String[] {
 359                 TestCase.UNSECURE.name(),
 360                 TestCase.SECURE.name(),
 361             };
 362         }
 363 
 364         try {
 365             for (String testName : args) {


< prev index next >