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 javax.swing.Action;
045: import org.openide.cookies.InstanceCookie;
046: import org.openide.filesystems.FileAttributeEvent;
047: import org.openide.filesystems.FileChangeListener;
048: import org.openide.filesystems.FileEvent;
049: import org.openide.filesystems.FileObject;
050: import org.openide.filesystems.FileRenameEvent;
051: import org.openide.filesystems.FileUtil;
052: import org.openide.filesystems.Repository;
053: import org.openide.loaders.DataObject;
054:
055: /** A test.
056: * @author Jesse Glick
057: * @see InstanceDataObjectModuleTestHid
058: */
059: public class InstanceDataObjectModuleTest7 extends
060: InstanceDataObjectModuleTestHid implements FileChangeListener {
061: private boolean instanceSaved;
062:
063: public InstanceDataObjectModuleTest7(String name) {
064: super (name);
065: }
066:
067: protected void setUp() throws Exception {
068: Repository.getDefault().getDefaultFileSystem()
069: .addFileChangeListener(this );
070: assertNotNull(
071: "have org-netbeans-modules-settings.jar in CP",
072: InstanceDataObjectModuleTest7.class
073: .getResource("/org/netbeans/modules/settings/resources/Bundle.properties"));
074: super .setUp();
075: }
076:
077: protected void tearDown() throws Exception {
078: Repository.getDefault().getDefaultFileSystem()
079: .removeFileChangeListener(this );
080: super .tearDown();
081: }
082:
083: public void testFixedSettingsChangeInstanceAfterSlowReload()
084: throws Exception {
085: twiddle(m2, TWIDDLE_ENABLE);
086: assertTrue("m2 is enabled", m2.isEnabled());
087: DataObject obj1;
088: obj1 = findIt("Services/Misc/inst-2.settings");
089: assertEquals("No saved state for inst-2.settings", null,
090: FileUtil.toFile(obj1.getPrimaryFile()));
091: org.openide.ErrorManager.getDefault().log(
092: "BEFORE THE COOKIE QUERY");
093: InstanceCookie inst1 = (InstanceCookie) obj1
094: .getCookie(InstanceCookie.class);
095: org.openide.ErrorManager.getDefault().log(
096: "AFTER THE COOKIE QUERY");
097: {
098: int debug = 5;
099: while (inst1 == null && debug-- > 0) {
100: Thread.sleep(300);
101: inst1 = (InstanceCookie) obj1
102: .getCookie(InstanceCookie.class);
103: org.openide.ErrorManager.getDefault().log(
104: " SLEEP[300ms]: " + inst1);
105: }
106: }
107: assertNotNull("Had an instance from " + obj1, inst1);
108: Action a1 = (Action) inst1.instanceCreate();
109: assertTrue("Old version of action", a1.isEnabled());
110: // Make some change which should cause it to be written to disk:
111: synchronized (this ) {
112: a1.setEnabled(false);
113: // Cf. InstanceDataObject.SettingsInstance.SAVE_DELAY = 2000:
114: wait(60000);
115: assertTrue("Really was saved", instanceSaved);
116: }
117: twiddle(m2, TWIDDLE_DISABLE);
118: // Just in case it is needed:
119: Thread.sleep(1000);
120:
121: assertTrue("Data object is still valid", obj1.isValid());
122:
123: // Yarda's patch:
124: InstanceCookie.Of notExists = (InstanceCookie.Of) obj1
125: .getCookie(InstanceCookie.class);
126: if (notExists != null && notExists.instanceOf(Action.class)) {
127: fail("Module is disabled, so " + obj1
128: + " should have no instance cookie " + notExists
129: + " with " + notExists.instanceClass());
130: }
131: // it is OK for there to be an instance of BrokenSettings...
132:
133: twiddle(m2, TWIDDLE_ENABLE);
134: // Make sure there is time for changes to take effect:
135: Thread.sleep(2000);
136: DataObject obj2 = findIt("Services/Misc/inst-2.settings");
137: assertSameDataObject("same data object", obj1, obj2);
138: InstanceCookie inst2 = (InstanceCookie) obj2
139: .getCookie(InstanceCookie.class);
140: assertNotNull("Had an instance", inst2);
141: assertTrue("InstanceCookie changed", inst1 != inst2);
142: Action a2 = (Action) inst2.instanceCreate();
143: assertTrue("Action changed", a1 != a2);
144: assertTrue("Correct action", "SomeAction".equals(a2
145: .getValue(Action.NAME)));
146: assertTrue("New version of action", !a2.isEnabled());
147: assertTrue("module still enabled", m2.isEnabled());
148: twiddle(m2, TWIDDLE_DISABLE);
149: // Now make sure it has no cookie.
150: Thread.sleep(1000);
151: DataObject obj3 = findIt("Services/Misc/inst-2.settings");
152: assertSameDataObject("same data object3", obj1, obj3);
153: InstanceCookie inst3 = (InstanceCookie) obj3
154: .getCookie(InstanceCookie.class);
155: assertNull("Had instance", inst3);
156: }
157:
158: public void fileAttributeChanged(FileAttributeEvent fe) {
159: }
160:
161: public synchronized void fileChanged(FileEvent fe) {
162: if ("inst-2.settings".equals(fe.getFile().getNameExt())) {
163: instanceSaved = true;
164: notifyAll();
165: }
166: }
167:
168: public void fileDataCreated(FileEvent fe) {
169: }
170:
171: public void fileDeleted(FileEvent fe) {
172: if ("inst-2.settings".equals(fe.getFile().getNameExt())) {
173: FileObject isThere = Repository.getDefault()
174: .getDefaultFileSystem().findResource(
175: fe.getFile().getPath());
176:
177: fail("File "
178: + fe.getFile()
179: + " should not be deleted as this will discard the data object. Moreover it is expected that similar file is still there: "
180: + isThere);
181: }
182: }
183:
184: public void fileFolderCreated(FileEvent fe) {
185: }
186:
187: public void fileRenamed(FileRenameEvent fe) {
188: }
189:
190: }
|