001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core.lookup;
043:
044: import org.netbeans.core.LoaderPoolNode;
045: import org.netbeans.junit.*;
046: import junit.textui.TestRunner;
047:
048: import java.io.File;
049: import org.netbeans.Module;
050: import org.netbeans.ModuleManager;
051: import org.netbeans.core.NbTopManager;
052: import org.netbeans.core.startup.ModuleHistory;
053: import org.openide.util.Lookup;
054: import javax.swing.Action;
055: import java.util.Iterator;
056: import org.openide.loaders.DataObject;
057: import org.openide.filesystems.FileObject;
058: import org.openide.filesystems.Repository;
059: import org.openide.util.Mutex;
060: import org.openide.cookies.InstanceCookie;
061: import org.openide.util.MutexException;
062: import org.openide.util.LookupListener;
063: import org.openide.util.LookupEvent;
064:
065: /** A test.
066: * @author Jesse Glick
067: * @see InstanceDataObjectModuleTestHid
068: */
069: public class InstanceDataObjectModuleTest4 extends
070: InstanceDataObjectModuleTestHid {
071:
072: public InstanceDataObjectModuleTest4(String name) {
073: super (name);
074: }
075:
076: /** Currently fails (lookup gets a result not assignable to its template),
077: * probably because this is not supported with *.instance (?).
078: */
079: public void testReloadDotInstanceSwitchesLookupByNewClass()
080: throws Exception {
081: twiddle(m1, TWIDDLE_ENABLE);
082: ClassLoader l1 = null, l2 = null;
083: try {
084: l1 = m1.getClassLoader();
085: Class c1 = l1.loadClass("test1.SomeAction");
086: assertEquals("Correct loader", l1, c1.getClassLoader());
087: assertTrue(
088: "SomeAction<1> instance found after module installation",
089: existsSomeAction(c1));
090:
091: ClassLoader g1 = Lookup.getDefault().lookup(
092: ClassLoader.class);
093: ERR.log("Before reload: " + g1);
094: twiddle(m1, TWIDDLE_RELOAD);
095: ClassLoader g2 = Lookup.getDefault().lookup(
096: ClassLoader.class);
097: ERR.log("After reload: " + g2);
098: // Sleeping for a few seconds here does *not* help.
099: l2 = m1.getClassLoader();
100: assertTrue("ClassLoader really changed", l1 != l2);
101: Class c2 = l2.loadClass("test1.SomeAction");
102: assertTrue("Class really changed", c1 != c2);
103:
104: assertTrue("Glboal Class loaders really changed", g1 != g2);
105:
106: LoaderPoolNode.waitFinished();
107: ERR.log("After waitFinished");
108: assertTrue(
109: "SomeAction<1> instance not found after module reload",
110: !existsSomeAction(c1));
111: assertTrue(
112: "SomeAction<2> instance found after module reload",
113: existsSomeAction(c2));
114: } finally {
115: ERR.log("Verify why it failed");
116: FileObject fo = Repository.getDefault()
117: .getDefaultFileSystem().findResource(
118: "Services/Misc/inst-1.instance");
119: ERR.log("File object found: " + fo);
120: if (fo != null) {
121: DataObject obj = DataObject.find(fo);
122: ERR.log("data object found: " + obj);
123: InstanceCookie ic = (InstanceCookie) obj
124: .getCookie(InstanceCookie.class);
125: ERR.log("InstanceCookie: " + ic);
126: if (ic != null) {
127: ERR.log("value: " + ic.instanceCreate());
128: ERR.log(" cl : "
129: + ic.instanceCreate().getClass()
130: .getClassLoader());
131: ERR.log(" l1 : " + l1);
132: ERR.log(" l2 : " + l2);
133: }
134: }
135:
136: ERR.log("Before disable");
137: twiddle(m1, TWIDDLE_DISABLE);
138: }
139: }
140:
141: /** Though this works in test #5, seems to get "poisoned" here by running
142: * in the same VM as the previous test.
143: */
144: public void testReloadSettingsSwitchesLookupByNewClass()
145: throws Exception {
146: assertTrue("There is initially nothing in lookup",
147: !existsSomeAction(Action.class));
148: twiddle(m2, TWIDDLE_ENABLE);
149: try {
150: ClassLoader l1 = m2.getClassLoader();
151: Class c1 = l1.loadClass("test2.SomeAction");
152: assertEquals("Correct loader", l1, c1.getClassLoader());
153: assertTrue(
154: "SomeAction<1> instance found after module installation",
155: existsSomeAction(c1));
156:
157: ERR.log("Before reload");
158: twiddle(m2, TWIDDLE_RELOAD);
159: ERR.log("After reload");
160: ClassLoader l2 = m2.getClassLoader();
161: assertTrue("ClassLoader really changed", l1 != l2);
162: Class c2 = l2.loadClass("test2.SomeAction");
163: assertTrue("Class really changed", c1 != c2);
164: // Make sure the changes take effect
165: LoaderPoolNode.waitFinished();
166: ERR.log("After waitFinished");
167:
168: assertTrue(
169: "SomeAction<1> instance not found after module reload",
170: !existsSomeAction(c1));
171: assertTrue(
172: "SomeAction<2> instance found after module reload",
173: existsSomeAction(c2));
174: } finally {
175: ERR.log("Finally disable");
176: twiddle(m2, TWIDDLE_DISABLE);
177: }
178: }
179:
180: }
|