Path: blob/master/test/jdk/sun/awt/font/ClassLoaderLeakTest.java
41149 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/**23* @test24* @bug 693638925*26* @summary Test verifes that LogManager shutdown hook does not cause27* an application classloader leaks.28*29* @run main/othervm ClassLoaderLeakTest FontManagerTest30*/3132import java.awt.Font;33import java.awt.Graphics;34import java.io.File;35import java.lang.ref.WeakReference;36import java.lang.reflect.Constructor;37import java.lang.reflect.Method;38import java.net.MalformedURLException;39import java.net.URL;40import java.net.URLClassLoader;41import java.util.concurrent.CountDownLatch;4243public class ClassLoaderLeakTest {4445private static CountDownLatch doneSignal;46private static CountDownLatch launchSignal;47private static Throwable launchFailure = null;4849public static void main(String[] args) {50doneSignal = new CountDownLatch(1);51launchSignal = new CountDownLatch(1);5253String testcase = "FontManagerTest";5455if (args.length > 0) {56testcase = args[0];57}5859/* prepare test class loader */60URL pwd = null;61try {6263pwd = new File(System.getProperty("test.classes", ".")).toURL();64} catch (MalformedURLException e) {65throw new RuntimeException("Test failed.", e);66}67System.out.println("PWD: " + pwd);68URL[] urls = new URL[]{pwd};6970MyClassLoader appClassLoader = new MyClassLoader(urls, "test0");71WeakReference<MyClassLoader> ref =72new WeakReference<MyClassLoader>(appClassLoader);7374ThreadGroup appsThreadGroup = new ThreadGroup("MyAppsThreadGroup");7576Runnable launcher = new TestLauncher(testcase);7778Thread appThread = new Thread(appsThreadGroup, launcher, "AppThread-0");79appThread.setContextClassLoader(appClassLoader);8081appThread.start();82appsThreadGroup = null;83appClassLoader = null;84launcher = null;85appThread = null;8687/* wait for laucnh completion */88try {89launchSignal.await();90} catch (InterruptedException e) {91}9293/* check if launch failed */94if (launchFailure != null) {95throw new RuntimeException("Test failed.", launchFailure);96}9798/* wait for test app excution completion */99try {100doneSignal.await();101} catch (InterruptedException e) {102}103104/* give a chance to GC */105waitAndGC(9);106107if (ref.get() != null) {108throw new RuntimeException("Test failed: classloader is still alive");109}110111112System.out.println("Test passed.");113}114115private static class TestLauncher implements Runnable {116117private String className;118119public TestLauncher(String name) {120className = name;121}122123public void run() {124try {125ClassLoader cl =126Thread.currentThread().getContextClassLoader();127Class appMain = cl.loadClass(className);128Method launch =129appMain.getMethod("launch", doneSignal.getClass());130131Constructor c = appMain.getConstructor();132133Object o = c.newInstance();134135launch.invoke(o, doneSignal);136137} catch (Throwable e) {138launchFailure = e;139} finally {140launchSignal.countDown();141}142}143}144145private static class MyClassLoader extends URLClassLoader {146147private static boolean verbose =148Boolean.getBoolean("verboseClassLoading");149private String uniqClassName;150151public MyClassLoader(URL[] urls, String uniq) {152super(urls);153154uniqClassName = uniq;155}156157public Class loadClass(String name) throws ClassNotFoundException {158if (verbose) {159System.out.printf("%s: load class %s\n", uniqClassName, name);160}161if (uniqClassName.equals(name)) {162return Object.class;163}164return super.loadClass(name);165}166167public String toString() {168return "MyClassLoader(" + uniqClassName + ")";169}170}171172private static void waitAndGC(int sec) {173int cnt = sec;174System.out.print("Wait ");175while (cnt-- > 0) {176try {177Thread.sleep(1000);178} catch (InterruptedException e) {179}180// do GC every 3 seconds181if (cnt % 3 == 2) {182System.gc();183System.out.print("+");184} else {185System.out.print(".");186}187//checkErrors();188}189System.out.println("");190}191}192193abstract class AppTest {194195public AppTest() {196}197198protected abstract void doTest();199200public void launch(CountDownLatch done) {201System.out.println("Testcase: " + this.getClass().getName());202try {203doTest();204} finally {205done.countDown();206}207}208}209210class FontManagerTest extends AppTest {211212public FontManagerTest() {213}214215protected void doTest() {216Font f = new Font(Font.SANS_SERIF, Font.ITALIC, 24);217f.getNumGlyphs();218}219}220221222