1 19 package org.openide.util; 20 21 import java.net.URL ; 22 import java.net.URLClassLoader ; 23 import java.util.logging.Handler ; 24 import java.util.logging.Level ; 25 import java.util.logging.LogRecord ; 26 import java.util.logging.Logger ; 27 import junit.framework.*; 28 import java.lang.ref.*; 29 import java.util.*; 30 31 35 public class IconManagerGetLoaderTest extends TestCase { 36 static { 37 System.setProperty("org.openide.util.Lookup", "org.openide.util.IconManagerGetLoaderTest$Lkp"); 38 39 40 Logger l = Logger.getLogger(""); 41 Handler [] 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 testName) { 51 super (testName); 52 } 53 54 protected void setUp () throws Exception { 55 } 56 57 protected void tearDown () throws Exception { 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 { 67 ClassLoader l = IconManager.getLoader (); 68 assertTrue("Error manager race condition activated", ErrMgr.switchDone); 69 assertEquals("c1 the original one", Lkp.c1, l); 70 71 ClassLoader 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 c1 = new URLClassLoader (new URL [0]); 80 static ClassLoader c2 = new URLClassLoader (new URL [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 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 { 102 public static boolean switchDone; 103 104 public void log (String 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 record) { 115 log(record.getMessage()); 116 } 117 118 public void flush() { 119 } 120 121 public void close() throws SecurityException { 122 } 123 124 } 125 126 } 127 | Popular Tags |