KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > IconManagerGetLoaderTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.util;
20
21 import java.net.URL JavaDoc;
22 import java.net.URLClassLoader JavaDoc;
23 import java.util.logging.Handler JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.LogRecord JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import junit.framework.*;
28 import java.lang.ref.*;
29 import java.util.*;
30
31 /**
32  *
33  * @author Jaroslav Tulach
34  */

35 public class IconManagerGetLoaderTest extends TestCase {
36     static {
37         System.setProperty("org.openide.util.Lookup", "org.openide.util.IconManagerGetLoaderTest$Lkp");
38
39
40         Logger JavaDoc l = Logger.getLogger("");
41         Handler JavaDoc[] arr = l.getHandlers();
42         for (int i = 0; i < arr.length; i++) {
43             l.removeHandler(arr[i]);
44         }
45         l.addHandler(new ErrMgr());
46         l.setLevel(Level.ALL);
47     }
48     
49     
50     public IconManagerGetLoaderTest (String JavaDoc testName) {
51         super (testName);
52     }
53
54     protected void setUp () throws Exception JavaDoc {
55     }
56
57     protected void tearDown () throws Exception JavaDoc {
58     }
59
60     public static Test suite () {
61         TestSuite suite = new TestSuite(IconManagerGetLoaderTest.class);
62         return suite;
63     }
64     
65     
66     public void testWrongImplOfGetLoaderIssue62194() throws Exception JavaDoc {
67         ClassLoader JavaDoc l = IconManager.getLoader ();
68         assertTrue("Error manager race condition activated", ErrMgr.switchDone);
69         assertEquals("c1 the original one", Lkp.c1, l);
70         
71         ClassLoader JavaDoc n = IconManager.getLoader ();
72         assertEquals("c2 the new one", Lkp.c2, n);
73     }
74     
75     
76     
77     public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
78         private org.openide.util.lookup.InstanceContent ic;
79         static ClassLoader JavaDoc c1 = new URLClassLoader JavaDoc(new URL JavaDoc[0]);
80         static ClassLoader JavaDoc c2 = new URLClassLoader JavaDoc(new URL JavaDoc[0]);
81         
82         public Lkp () {
83             this (new org.openide.util.lookup.InstanceContent ());
84         }
85         
86         private Lkp (org.openide.util.lookup.InstanceContent ic) {
87             super (ic);
88             this.ic = ic;
89             
90             turn(c1);
91         }
92         
93         public void turn (ClassLoader JavaDoc c) {
94             ArrayList l = new ArrayList();
95             l.add(c);
96             ic.set (l, null);
97         }
98     }
99     
100     
101     private static class ErrMgr extends Handler JavaDoc {
102         public static boolean switchDone;
103         
104         public void log (String JavaDoc s) {
105             if (s == null) return;
106
107             if (s.startsWith ("Loader computed")) {
108                 switchDone = true;
109                 Lkp lkp = (Lkp)org.openide.util.Lookup.getDefault ();
110                 lkp.turn (Lkp.c2);
111             }
112         }
113
114         public void publish(LogRecord JavaDoc record) {
115             log(record.getMessage());
116         }
117
118         public void flush() {
119         }
120
121         public void close() throws SecurityException JavaDoc {
122         }
123         
124     }
125     
126 }
127
Popular Tags