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 HandlersOnComplexResetUpdate {
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 reset() and 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 reset, check that ref2 has no handlers, and
237 // attempt to configure again.
238 Properties previousProps = properties.get(0);
239 int expectedHandlersCount = 1;
240 boolean checkHandlersOnParent = Boolean.parseBoolean(
311 try {
312 StringBuilder builder = new StringBuilder();
313 Files.list(Paths.get(userDir))
314 .filter((f) -> f.toString().contains(PREFIX))
315 .filter((f) -> f.toString().endsWith(".lck"))
316 .forEach((f) -> {
317 builder.append(f.toString()).append('\n');
318 });
319 if (!builder.toString().isEmpty()) {
320 throw new RuntimeException("Lock files not cleaned:\n"
321 + builder.toString());
322 }
323 } catch(RuntimeException | Error x) {
324 if (suppressed != null) x.addSuppressed(suppressed);
325 throw x;
326 } catch(Exception x) {
327 if (suppressed != null) x.addSuppressed(suppressed);
328 throw new RuntimeException(x);
329 }
330 });
331 fooChild = null;
332 System.out.println("Setting fooChild to: " + fooChild);
333 while ((ref2 = queue.poll()) == null) {
334 System.gc();
335 Thread.sleep(1000);
336 }
337 if (ref2 != fooRef) {
338 throw new RuntimeException("Unexpected reference: "
339 + ref2 +"\n\texpected: " + fooRef);
340 }
341 if (ref2.get() != null) {
342 throw new RuntimeException("Referent not cleared: " + ref2.get());
343 }
344 System.out.println("Got fooRef after reset(), fooChild is " + fooChild);
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) {
|
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 HandlersOnComplexResetUpdate {
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 reset() and 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 reset, check that ref2 has no handlers, and
245 // attempt to configure again.
246 Properties previousProps = properties.get(0);
247 int expectedHandlersCount = 1;
248 boolean checkHandlersOnParent = Boolean.parseBoolean(
319 try {
320 StringBuilder builder = new StringBuilder();
321 Files.list(Paths.get(userDir))
322 .filter((f) -> f.toString().contains(PREFIX))
323 .filter((f) -> f.toString().endsWith(".lck"))
324 .forEach((f) -> {
325 builder.append(f.toString()).append('\n');
326 });
327 if (!builder.toString().isEmpty()) {
328 throw new RuntimeException("Lock files not cleaned:\n"
329 + builder.toString());
330 }
331 } catch(RuntimeException | Error x) {
332 if (suppressed != null) x.addSuppressed(suppressed);
333 throw x;
334 } catch(Exception x) {
335 if (suppressed != null) x.addSuppressed(suppressed);
336 throw new RuntimeException(x);
337 }
338 });
339 try {
340 fooChild = null;
341 System.out.println("Setting fooChild to: " + fooChild);
342 while ((ref2 = queue.poll()) == null) {
343 System.gc();
344 Thread.sleep(1000);
345 }
346 if (ref2 != fooRef) {
347 throw new RuntimeException("Unexpected reference: "
348 + ref2 +"\n\texpected: " + fooRef);
349 }
350 if (ref2.get() != null) {
351 throw new RuntimeException("Referent not cleared: " + ref2.get());
352 }
353 System.out.println("Got fooRef after reset(), fooChild is " + fooChild);
354 } catch(Throwable t) {
355 if (failed != null) t.addSuppressed(failed);
356 throw t;
357 }
358 }
359 if (failed != null) {
360 // should rarely happen...
361 throw new RuntimeException(failed);
362 }
363
364 }
365
366 public static void main(String... args) throws Exception {
367
368
369 if (args == null || args.length == 0) {
370 args = new String[] {
371 TestCase.UNSECURE.name(),
372 TestCase.SECURE.name(),
373 };
374 }
375
376 try {
377 for (String testName : args) {
|