1 19 20 package org.netbeans.core.lookup; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.Iterator ; 25 import javax.swing.Action ; 26 import junit.framework.AssertionFailedError; 27 import org.netbeans.Module; 28 import org.netbeans.ModuleManager; 29 import org.netbeans.core.startup.ModuleHistory; 30 import org.netbeans.junit.NbTestCase; 31 import org.openide.ErrorManager; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.Repository; 34 import org.openide.loaders.DataObject; 35 import org.openide.util.Lookup; 36 import org.openide.util.LookupEvent; 37 import org.openide.util.LookupListener; 38 import org.openide.util.Mutex; 39 import org.openide.util.MutexException; 40 41 48 public abstract class InstanceDataObjectModuleTestHid extends NbTestCase { 49 protected ErrorManager ERR; 50 51 static { 52 org.netbeans.core.startup.MainLookup.register (new ErrManager ()); 53 } 54 55 56 protected InstanceDataObjectModuleTestHid(String name) { 57 super(name); 58 } 59 60 private ModuleManager mgr; 61 protected Module m1, m2; 62 63 protected void runTest () throws Throwable { 64 ErrManager.messages.setLength (0); 65 ErrManager.log = getLog (); 66 try { 67 super.runTest(); 68 } catch (Error err) { 69 AssertionFailedError newErr = new AssertionFailedError (err.getMessage () + "\n" + ErrManager.messages); 70 newErr.initCause (err); 71 throw newErr; 72 } 73 } 74 75 76 protected void setUp() throws Exception { 77 ERR = ErrManager.getDefault().getInstance("TEST-" + getName()); 78 79 mgr = org.netbeans.core.startup.Main.getModuleSystem().getManager(); 80 final File jar1 = toFile (InstanceDataObjectModuleTestHid.class.getResource("data/test1.jar")); 81 final File jar2 = toFile (InstanceDataObjectModuleTestHid.class.getResource("data/test2.jar")); 82 try { 83 mgr.mutex().writeAccess(new Mutex.ExceptionAction() { 84 public Object run() throws Exception { 85 m1 = mgr.create(jar1, new ModuleHistory(jar1.getAbsolutePath()), false, false, false); 86 if (!m1.getProblems().isEmpty()) throw new IllegalStateException ("m1 is uninstallable: " + m1.getProblems()); 87 m2 = mgr.create(jar2, new ModuleHistory(jar2.getAbsolutePath()), false, false, false); 88 return null; 89 } 90 }); 91 } catch (MutexException me) { 92 throw me.getException(); 93 } 94 95 ERR.log("setup finished"); 96 } 97 98 protected static File toFile (java.net.URL url) throws java.io.IOException { 99 File f = new File (url.getPath ()); 100 if (f.exists ()) { 101 return f; 102 } 103 104 String n = url.getPath (); 105 int indx = n.lastIndexOf ('/'); 106 if (indx != -1) { 107 n = n.substring (indx + 1); 108 } 109 n = n + url.getPath ().hashCode (); 110 111 f = File.createTempFile (n, ".jar"); 112 java.io.FileOutputStream out = new java.io.FileOutputStream (f); 113 org.openide.filesystems.FileUtil.copy (url.openStream (), out); 114 out.close (); 115 f.deleteOnExit (); 116 117 return f; 118 } 119 120 protected void tearDown() throws Exception { 121 ERR.log("going to teardown"); 122 try { 123 mgr.mutex().writeAccess(new Mutex.ExceptionAction() { 124 public Object run() throws Exception { 125 del(m1); 126 del(m2); 127 return null; 128 } 129 private void del(Module m) throws Exception { 130 if (m.isEnabled()) { 131 if (m.isAutoload() || m.isEager() || m.isFixed()) { 133 return; 135 } 136 mgr.disable(m); 137 } 138 mgr.delete(m); 139 } 140 }); 141 } catch (MutexException me) { 142 Exception e = me.getException(); 143 throw e; 144 } catch (RuntimeException e) { 145 throw e; 147 } 148 m1 = null; 149 m2 = null; 150 mgr = null; 151 } 152 153 protected static final int TWIDDLE_ENABLE = 0; 154 protected static final int TWIDDLE_DISABLE = 1; 155 protected static final int TWIDDLE_RELOAD = 2; 156 protected void twiddle(final Module m, final int action) throws Exception { 157 try { 158 mgr.mutex().writeAccess(new Mutex.ExceptionAction() { 159 public Object run() throws Exception { 160 switch (action) { 161 case TWIDDLE_ENABLE: 162 mgr.enable(m); 163 break; 164 case TWIDDLE_DISABLE: 165 mgr.disable(m); 166 break; 167 case TWIDDLE_RELOAD: 168 mgr.disable(m); 169 mgr.enable(m); 170 break; 171 default: 172 throw new IllegalArgumentException ("bad action: " + action); 173 } 174 return null; 175 } 176 }); 177 } catch (MutexException me) { 178 throw me.getException(); 179 } 180 } 181 182 protected boolean existsSomeAction(Class c) { 183 return existsSomeAction(c, Lookup.getDefault().lookupResult(c)); 184 } 185 186 protected boolean existsSomeAction(Class c, Lookup.Result r) { 187 assertTrue(Action .class.isAssignableFrom(c)); 188 boolean found = false; 189 ERR.log("Begin iterate"); 190 Iterator it = r.allInstances().iterator(); 191 while (it.hasNext()) { 192 Action a = (Action )it.next(); 193 ERR.log("First action read: " + a); 194 assertTrue("Assignable to template class: c=" + c.getName() + " a.class=" + a.getClass().getName() + 195 " loaders: c1=" + c.getClassLoader() + " a.class=" + a.getClass().getClassLoader(), 196 c.isInstance(a) 197 ); 198 if ("SomeAction".equals(a.getValue(Action.NAME))) { 199 found = true; 200 ERR.log("Action with correct name found: " + a); 201 break; 202 } 203 } 204 ERR.log("existsSomeAction finished: " + found); 205 return found; 206 } 207 208 protected DataObject findIt(String name) throws Exception { 209 FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource(name); 210 assertNotNull("Found " + name, fo); 211 return DataObject.find(fo); 212 } 213 214 protected static void assertSameDataObject (String msg, DataObject obj1, DataObject obj2) { 215 assertNotNull (msg, obj1); 216 assertNotNull (msg, obj2); 217 assertEquals ("They have the same primary file: " + msg, obj1.getPrimaryFile (), obj2.getPrimaryFile ()); 218 assertSame (msg, obj1, obj2); 219 } 220 221 protected static final class LookupL implements LookupListener { 222 public int count = 0; 223 public synchronized void resultChanged(LookupEvent ev) { 224 count++; 225 notifyAll(); 226 } 227 public synchronized boolean gotSomething() throws InterruptedException { 228 ErrorManager.getDefault().log("gotSomething: " + count); 229 if (count > 0) return true; 230 ErrorManager.getDefault().log("gotSomething: do wait"); 231 wait(23000); 232 ErrorManager.getDefault().log("gotSomething: wait done: " + count); 233 return count > 0; 234 } 235 } 236 237 protected static void deleteRec(File f, boolean thistoo) throws IOException { 238 if (f.isDirectory()) { 239 File [] kids = f.listFiles(); 240 if (kids == null) throw new IOException ("Could not list: " + f); 241 for (int i = 0; i < kids.length; i++) { 242 deleteRec(kids[i], true); 243 } 244 } 245 if (thistoo && !f.delete()) throw new IOException ("Could not delete: " + f); 246 } 247 248 private static final class ErrManager extends org.openide.ErrorManager { 249 public static final StringBuffer messages = new StringBuffer (); 250 public static java.io.PrintStream log = System.err; 251 252 private String prefix; 253 254 public ErrManager () { 255 this (""); 256 } 257 private ErrManager (String s) { 258 prefix = s; 259 } 260 261 public Throwable annotate (Throwable t, int severity, String message, String localizedMessage, Throwable stackTrace, java.util.Date date) { 262 return t; 263 } 264 265 public Throwable attachAnnotations (Throwable t, org.openide.ErrorManager.Annotation[] arr) { 266 return t; 267 } 268 269 public org.openide.ErrorManager.Annotation[] findAnnotations (Throwable t) { 270 return null; 271 } 272 273 public org.openide.ErrorManager getInstance (String name) { 274 return new ErrManager (prefix + name); 275 } 276 277 public void log (int severity, String s) { 278 if (prefix == null) { 279 return; 280 } 281 282 if (messages.length () > 500000) { 283 messages.delete (0, 250000); 284 } 285 messages.append ('['); log.print ('['); 286 messages.append (prefix); log.print (prefix); 287 messages.append (']'); log.print (']'); 288 messages.append (s); log.print (s); 289 messages.append ('\n'); log.println (); 290 } 291 292 public void notify (int severity, Throwable t) { 293 if (prefix == null) { 294 return; 295 } 296 297 messages.append (t.getMessage ()); 298 t.printStackTrace (log); 299 } 300 301 } 302 303 } 304 | Popular Tags |