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.netbeans.core.LoaderPoolNode;
046: import org.openide.cookies.InstanceCookie;
047: import org.openide.filesystems.FileAttributeEvent;
048: import org.openide.filesystems.FileChangeListener;
049: import org.openide.filesystems.FileEvent;
050: import org.openide.filesystems.FileObject;
051: import org.openide.filesystems.FileRenameEvent;
052: import org.openide.filesystems.FileUtil;
053: import org.openide.filesystems.Repository;
054: import org.openide.loaders.DataObject;
055:
056: /** A test.
057: * @author Jesse Glick
058: * @see InstanceDataObjectModuleTestHid
059: */
060: public class InstanceDataObjectModuleTest8 extends
061: InstanceDataObjectModuleTestHid implements FileChangeListener {
062:
063: /*
064: static {
065: // Turn on verbose logging while developing tests:
066: System.setProperty("org.netbeans.core.modules", "0");
067: }
068: */
069:
070: private boolean instanceSaved;
071:
072: public InstanceDataObjectModuleTest8(String name) {
073: super (name);
074: }
075:
076: protected void setUp() throws Exception {
077: Repository.getDefault().getDefaultFileSystem()
078: .addFileChangeListener(this );
079: assertNotNull(
080: "have org-netbeans-modules-settings.jar in CP",
081: InstanceDataObjectModuleTest7.class
082: .getResource("/org/netbeans/modules/settings/resources/Bundle.properties"));
083: super .setUp();
084: }
085:
086: protected void tearDown() throws java.lang.Exception {
087: Repository.getDefault().getDefaultFileSystem()
088: .removeFileChangeListener(this );
089: super .tearDown();
090: }
091:
092: /** Currently fails.
093: * Same as #7, but reloading is done quickly (one write mutex, no pause).
094: */
095: public void testFixedSettingsChangeInstanceAfterFastReload()
096: throws Exception {
097: twiddle(m2, TWIDDLE_ENABLE);
098: DataObject obj1;
099: try {
100: obj1 = findIt("Services/Misc/inst-8.settings");
101: ERR.log("Found obj1: " + obj1);
102: assertEquals("No saved state for inst-8.settings", null,
103: FileUtil.toFile(obj1.getPrimaryFile()));
104: InstanceCookie inst1 = (InstanceCookie) obj1
105: .getCookie(InstanceCookie.class);
106: ERR.log("There is a cookie: " + inst1);
107: assertNotNull("Had an instance", inst1);
108: Action a1 = (Action) inst1.instanceCreate();
109: assertEquals("Correct action class", "test2.SomeAction", a1
110: .getClass().getName());
111: assertTrue("Old version of action", a1.isEnabled());
112:
113: ERR.log("Action created" + a1);
114:
115: // Make some change which should cause it to be written to disk:
116: synchronized (this ) {
117: ERR.log("In sync block");
118: a1.setEnabled(false);
119: ERR.log("setEnabled(false)");
120: // Cf. InstanceDataObject.SettingsInstance.SAVE_DELAY = 2000:
121: ERR.log("Waiting");
122: wait(60000);
123: ERR.log("Waiting done");
124: assertTrue("Really was saved", instanceSaved);
125: }
126: /*
127: File saved = new File(new File(new File(systemDir, "Services"), "Misc"), "inst-8.settings");
128: assertTrue("Wrote to disk: " + saved, saved.isFile());
129: */
130: /*
131: File saved = FileUtil.toFile(obj1.getPrimaryFile());
132: assertNotNull("Wrote to disk; expecting: " + new File(new File(new File(systemDir, "Services"), "Misc"), "inst-8.settings"),
133: saved);
134: */
135: ERR.log("Twidle reload");
136: twiddle(m2, TWIDDLE_RELOAD);
137: ERR.log("TWIDDLE_RELOAD done");
138: LoaderPoolNode.waitFinished();
139: ERR.log("pool refeshed");
140: DataObject obj2 = findIt("Services/Misc/inst-8.settings");
141: ERR.log("Data object for inst-8: " + obj2);
142: assertSameDataObject("same data object", obj1, obj2);
143: InstanceCookie inst2 = (InstanceCookie) obj2
144: .getCookie(InstanceCookie.class);
145: ERR.log("Cookie from the object: " + inst2);
146: assertNotNull("Had an instance", inst2);
147: assertTrue("InstanceCookie changed", inst1 != inst2);
148: Action a2 = (Action) inst2.instanceCreate();
149: ERR.log("Action2 created: " + a2);
150: assertTrue("Action changed", a1 != a2);
151: assertTrue("Correct action", "SomeAction".equals(a2
152: .getValue(Action.NAME)));
153: assertTrue("New version of action", !a2.isEnabled());
154: } finally {
155: ERR.log("Final disable");
156: twiddle(m2, TWIDDLE_DISABLE);
157: ERR.log("Final disable done");
158: }
159: // Now make sure it has no cookie.
160: LoaderPoolNode.waitFinished();
161: ERR.log("loader pool node refreshed");
162: DataObject obj3 = findIt("Services/Misc/inst-8.settings");
163: ERR.log("Third data object: " + obj3);
164: assertSameDataObject("same data object2", obj1, obj3);
165: InstanceCookie inst3 = (InstanceCookie) obj3
166: .getCookie(InstanceCookie.class);
167: ERR.log("Cookie is here: " + inst3);
168: assertNull("Had instance", inst3);
169: }
170:
171: public void fileAttributeChanged(FileAttributeEvent fe) {
172: }
173:
174: public synchronized void fileChanged(FileEvent fe) {
175: if ("inst-8.settings".equals(fe.getFile().getNameExt())) {
176: instanceSaved = true;
177: notifyAll();
178: }
179: }
180:
181: public void fileDataCreated(FileEvent fe) {
182: }
183:
184: public void fileDeleted(FileEvent fe) {
185: if ("inst-8.settings".equals(fe.getFile().getNameExt())) {
186: FileObject isThere = Repository.getDefault()
187: .getDefaultFileSystem().findResource(
188: fe.getFile().getPath());
189: fail("File "
190: + fe.getFile()
191: + " should not be deleted as this will discard the data object. Moreover it is expected that similar file is still there: "
192: + isThere);
193: }
194: }
195:
196: public void fileFolderCreated(FileEvent fe) {
197: }
198:
199: public void fileRenamed(FileRenameEvent fe) {
200: }
201:
202: }
|