KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.core.LoaderPoolNode;
23 import org.netbeans.junit.*;
24 import junit.textui.TestRunner;
25
26 import java.io.File JavaDoc;
27 import org.netbeans.Module;
28 import org.netbeans.ModuleManager;
29 import org.netbeans.core.NbTopManager;
30 import org.netbeans.core.startup.ModuleHistory;
31 import org.openide.util.Lookup;
32 import javax.swing.Action JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import org.openide.loaders.DataObject;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.Repository;
37 import org.openide.util.Mutex;
38 import org.openide.cookies.InstanceCookie;
39 import org.openide.util.MutexException;
40 import org.openide.util.LookupListener;
41 import org.openide.util.LookupEvent;
42
43 /** A test.
44  * @author Jesse Glick
45  * @see InstanceDataObjectModuleTestHid
46  */

47 public class InstanceDataObjectModuleTest4 extends InstanceDataObjectModuleTestHid {
48
49     public InstanceDataObjectModuleTest4(String JavaDoc name) {
50         super(name);
51     }
52     
53     /** Currently fails (lookup gets a result not assignable to its template),
54      * probably because this is not supported with *.instance (?).
55      */

56     public void testReloadDotInstanceSwitchesLookupByNewClass() throws Exception JavaDoc {
57         twiddle(m1, TWIDDLE_ENABLE);
58         ClassLoader JavaDoc l1 = null, l2 = null;
59         try {
60             l1 = m1.getClassLoader();
61             Class JavaDoc c1 = l1.loadClass("test1.SomeAction");
62             assertEquals("Correct loader", l1, c1.getClassLoader());
63             assertTrue("SomeAction<1> instance found after module installation",
64                 existsSomeAction(c1));
65             
66             ClassLoader JavaDoc g1 = Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
67             ERR.log("Before reload: " + g1);
68             twiddle(m1, TWIDDLE_RELOAD);
69             ClassLoader JavaDoc g2 = Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
70             ERR.log("After reload: " + g2);
71             // Sleeping for a few seconds here does *not* help.
72
l2 = m1.getClassLoader();
73             assertTrue("ClassLoader really changed", l1 != l2);
74             Class JavaDoc c2 = l2.loadClass("test1.SomeAction");
75             assertTrue("Class really changed", c1 != c2);
76             
77             assertTrue("Glboal Class loaders really changed", g1 != g2);
78             
79             
80             LoaderPoolNode.waitFinished();
81             ERR.log("After waitFinished");
82             assertTrue("SomeAction<1> instance not found after module reload",
83                 !existsSomeAction(c1));
84             assertTrue("SomeAction<2> instance found after module reload",
85                 existsSomeAction(c2));
86         } finally {
87             ERR.log("Verify why it failed");
88             FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource("Services/Misc/inst-1.instance");
89             ERR.log("File object found: " + fo);
90             if (fo != null) {
91                 DataObject obj = DataObject.find(fo);
92                 ERR.log("data object found: " + obj);
93                 InstanceCookie ic = (InstanceCookie)obj.getCookie(InstanceCookie.class);
94                 ERR.log("InstanceCookie: " + ic);
95                 if (ic != null) {
96                     ERR.log("value: " + ic.instanceCreate());
97                     ERR.log(" cl : " + ic.instanceCreate().getClass().getClassLoader());
98                     ERR.log(" l1 : " + l1);
99                     ERR.log(" l2 : " + l2);
100                 }
101             }
102             
103             ERR.log("Before disable");
104             twiddle(m1, TWIDDLE_DISABLE);
105         }
106     }
107     
108     /** Though this works in test #5, seems to get "poisoned" here by running
109      * in the same VM as the previous test.
110      */

111     public void testReloadSettingsSwitchesLookupByNewClass() throws Exception JavaDoc {
112         assertTrue("There is initially nothing in lookup",
113             !existsSomeAction(Action JavaDoc.class));
114         twiddle(m2, TWIDDLE_ENABLE);
115         try {
116             ClassLoader JavaDoc l1 = m2.getClassLoader();
117             Class JavaDoc c1 = l1.loadClass("test2.SomeAction");
118             assertEquals("Correct loader", l1, c1.getClassLoader());
119             assertTrue("SomeAction<1> instance found after module installation",
120                 existsSomeAction(c1));
121             
122             ERR.log("Before reload");
123             twiddle(m2, TWIDDLE_RELOAD);
124             ERR.log("After reload");
125             ClassLoader JavaDoc l2 = m2.getClassLoader();
126             assertTrue("ClassLoader really changed", l1 != l2);
127             Class JavaDoc c2 = l2.loadClass("test2.SomeAction");
128             assertTrue("Class really changed", c1 != c2);
129             // Make sure the changes take effect
130
LoaderPoolNode.waitFinished();
131             ERR.log("After waitFinished");
132             
133             assertTrue("SomeAction<1> instance not found after module reload",
134                 !existsSomeAction(c1));
135             assertTrue("SomeAction<2> instance found after module reload",
136                 existsSomeAction(c2));
137         } finally {
138             ERR.log("Finally disable");
139             twiddle(m2, TWIDDLE_DISABLE);
140         }
141     }
142     
143 }
144
Popular Tags