001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.openide.util;
042:
043: import java.net.URL;
044: import java.net.URLClassLoader;
045: import java.util.logging.Handler;
046: import java.util.logging.Level;
047: import java.util.logging.LogRecord;
048: import java.util.logging.Logger;
049: import junit.framework.*;
050: import java.lang.ref.*;
051: import java.util.*;
052:
053: /**
054: *
055: * @author Jaroslav Tulach
056: */
057: public class IconManagerGetLoaderTest extends TestCase {
058: static {
059: System.setProperty("org.openide.util.Lookup",
060: "org.openide.util.IconManagerGetLoaderTest$Lkp");
061:
062: Logger l = Logger.getLogger("");
063: Handler[] arr = l.getHandlers();
064: for (int i = 0; i < arr.length; i++) {
065: l.removeHandler(arr[i]);
066: }
067: l.addHandler(new ErrMgr());
068: l.setLevel(Level.ALL);
069: }
070:
071: public IconManagerGetLoaderTest(String testName) {
072: super (testName);
073: }
074:
075: protected void setUp() throws Exception {
076: }
077:
078: protected void tearDown() throws Exception {
079: }
080:
081: public static Test suite() {
082: TestSuite suite = new TestSuite(IconManagerGetLoaderTest.class);
083: return suite;
084: }
085:
086: public void testWrongImplOfGetLoaderIssue62194() throws Exception {
087: ClassLoader l = IconManager.getLoader();
088: assertTrue("Error manager race condition activated",
089: ErrMgr.switchDone);
090: assertEquals("c1 the original one", Lkp.c1, l);
091:
092: ClassLoader n = IconManager.getLoader();
093: assertEquals("c2 the new one", Lkp.c2, n);
094: }
095:
096: public static final class Lkp extends
097: org.openide.util.lookup.AbstractLookup {
098: private org.openide.util.lookup.InstanceContent ic;
099: static ClassLoader c1 = new URLClassLoader(new URL[0]);
100: static ClassLoader c2 = new URLClassLoader(new URL[0]);
101:
102: public Lkp() {
103: this (new org.openide.util.lookup.InstanceContent());
104: }
105:
106: private Lkp(org.openide.util.lookup.InstanceContent ic) {
107: super (ic);
108: this .ic = ic;
109:
110: turn(c1);
111: }
112:
113: public void turn(ClassLoader c) {
114: ArrayList l = new ArrayList();
115: l.add(c);
116: ic.set(l, null);
117: }
118: }
119:
120: private static class ErrMgr extends Handler {
121: public static boolean switchDone;
122:
123: public void log(String s) {
124: if (s == null)
125: return;
126:
127: if (s.startsWith("Loader computed")) {
128: switchDone = true;
129: Lkp lkp = (Lkp) org.openide.util.Lookup.getDefault();
130: lkp.turn(Lkp.c2);
131: }
132: }
133:
134: public void publish(LogRecord record) {
135: log(record.getMessage());
136: }
137:
138: public void flush() {
139: }
140:
141: public void close() throws SecurityException {
142: }
143:
144: }
145:
146: }
|