KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > lookup > InstanceDataObjectModuleTestHid


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
20 package org.netbeans.core.lookup;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import javax.swing.Action JavaDoc;
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 /** Test InstanceDataObject's behavior in conjunction with module
42  * installation and uninstallation.
43  * Run each test in its own VM, since otherwise tests can affect
44  * their siblings (static vars are evil!).
45  * @author Jesse Glick
46  * @see issue #16327
47  */

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 JavaDoc name) {
57         super(name);
58     }
59     
60     private ModuleManager mgr;
61     protected Module m1, m2;
62
63     protected void runTest () throws Throwable JavaDoc {
64         ErrManager.messages.setLength (0);
65         ErrManager.log = getLog ();
66         try {
67             super.runTest();
68         } catch (Error JavaDoc 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 JavaDoc {
77         ERR = ErrManager.getDefault().getInstance("TEST-" + getName());
78         
79         mgr = org.netbeans.core.startup.Main.getModuleSystem().getManager();
80         final File JavaDoc jar1 = toFile (InstanceDataObjectModuleTestHid.class.getResource("data/test1.jar"));
81         final File JavaDoc jar2 = toFile (InstanceDataObjectModuleTestHid.class.getResource("data/test2.jar"));
82         try {
83             mgr.mutex().writeAccess(new Mutex.ExceptionAction() {
84                 public Object JavaDoc run() throws Exception JavaDoc {
85                     m1 = mgr.create(jar1, new ModuleHistory(jar1.getAbsolutePath()), false, false, false);
86                     if (!m1.getProblems().isEmpty()) throw new IllegalStateException JavaDoc("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 JavaDoc toFile (java.net.URL JavaDoc url) throws java.io.IOException JavaDoc {
99         File JavaDoc f = new File JavaDoc (url.getPath ());
100         if (f.exists ()) {
101             return f;
102         }
103         
104         String JavaDoc 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 JavaDoc out = new java.io.FileOutputStream JavaDoc (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 JavaDoc {
121         ERR.log("going to teardown");
122         try {
123             mgr.mutex().writeAccess(new Mutex.ExceptionAction() {
124                 public Object JavaDoc run() throws Exception JavaDoc {
125                     del(m1);
126                     del(m2);
127                     return null;
128                 }
129                 private void del(Module m) throws Exception JavaDoc {
130                     if (m.isEnabled()) {
131                         // Test presumably failed halfway.
132
if (m.isAutoload() || m.isEager() || m.isFixed()) {
133                             // Tough luck, can't get rid of it easily.
134
return;
135                         }
136                         mgr.disable(m);
137                     }
138                     mgr.delete(m);
139                 }
140             });
141         } catch (MutexException me) {
142             Exception JavaDoc e = me.getException();
143             throw e/*new Exception(e + " [Messages:" + ErrManager.messages + "]", e)*/;
144         } catch (RuntimeException JavaDoc e) {
145             // Debugging for #52689:
146
throw e/*new Exception(e + " [Messages:" + ErrManager.messages + "]", 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 JavaDoc {
157         try {
158             mgr.mutex().writeAccess(new Mutex.ExceptionAction() {
159                 public Object JavaDoc run() throws Exception JavaDoc {
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 JavaDoc("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 JavaDoc c) {
183         return existsSomeAction(c, Lookup.getDefault().lookupResult(c));
184     }
185     
186     protected boolean existsSomeAction(Class JavaDoc c, Lookup.Result r) {
187         assertTrue(Action JavaDoc.class.isAssignableFrom(c));
188         boolean found = false;
189         ERR.log("Begin iterate");
190         Iterator JavaDoc it = r.allInstances().iterator();
191         while (it.hasNext()) {
192             Action JavaDoc a = (Action JavaDoc)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 JavaDoc name) throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc {
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 JavaDoc f, boolean thistoo) throws IOException JavaDoc {
238         if (f.isDirectory()) {
239             File JavaDoc[] kids = f.listFiles();
240             if (kids == null) throw new IOException JavaDoc("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 JavaDoc("Could not delete: " + f);
246     }
247     
248     private static final class ErrManager extends org.openide.ErrorManager {
249         public static final StringBuffer JavaDoc messages = new StringBuffer JavaDoc ();
250         public static java.io.PrintStream JavaDoc log = System.err;
251         
252         private String JavaDoc prefix;
253         
254         public ErrManager () {
255             this ("");
256         }
257         private ErrManager (String JavaDoc s) {
258             prefix = s;
259         }
260         
261         public Throwable JavaDoc annotate (Throwable JavaDoc t, int severity, String JavaDoc message, String JavaDoc localizedMessage, Throwable JavaDoc stackTrace, java.util.Date JavaDoc date) {
262             return t;
263         }
264         
265         public Throwable JavaDoc attachAnnotations (Throwable JavaDoc t, org.openide.ErrorManager.Annotation[] arr) {
266             return t;
267         }
268         
269         public org.openide.ErrorManager.Annotation[] findAnnotations (Throwable JavaDoc t) {
270             return null;
271         }
272         
273         public org.openide.ErrorManager getInstance (String JavaDoc name) {
274             return new ErrManager (prefix + name);
275         }
276         
277         public void log (int severity, String JavaDoc 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 JavaDoc 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