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.openide.loaders;
043:
044: import org.netbeans.junit.NbTestCase;
045: import org.openide.filesystems.FileAttributeEvent;
046: import org.openide.filesystems.FileChangeListener;
047: import org.openide.filesystems.FileEvent;
048: import org.openide.filesystems.FileObject;
049: import org.openide.filesystems.FileRenameEvent;
050: import org.openide.filesystems.FileSystem;
051: import org.openide.filesystems.FileUtil;
052: import org.openide.filesystems.Repository;
053: import org.openide.util.RequestProcessor;
054:
055: /** MDR listens on FileChanges and wants to acquire its lock then.
056: * Also it does rename under holding its lock in other thread.
057: *
058: * @author Jaroslav Tulach
059: */
060: public class Deadlock59522Test extends NbTestCase implements
061: FileChangeListener {
062: FileObject toolbars;
063: FileSystem fs;
064: DataFolder toolbarsFolder;
065: DataFolder anotherFolder;
066: DataObject obj;
067: DataObject anotherObj;
068:
069: Exception assigned;
070: boolean called;
071: boolean ok;
072:
073: private Object BIG_MDR_LOCK = new Object();
074:
075: public Deadlock59522Test(String testName) {
076: super (testName);
077: }
078:
079: protected void setUp() throws Exception {
080: fs = Repository.getDefault().getDefaultFileSystem();
081: FileObject root = fs.getRoot();
082: toolbars = FileUtil.createFolder(root, "Toolbars");
083: toolbarsFolder = DataFolder.findFolder(toolbars);
084: FileObject[] arr = toolbars.getChildren();
085: for (int i = 0; i < arr.length; i++) {
086: arr[i].delete();
087: }
088: FileObject fo = FileUtil.createData(root, "Ahoj.txt");
089: obj = DataObject.find(fo);
090: fo = FileUtil.createFolder(root, "Another");
091: anotherFolder = DataFolder.findFolder(fo);
092: fo = FileUtil.createData(root, "Another.txt");
093: anotherObj = DataObject.find(fo);
094:
095: fs.addFileChangeListener(this );
096: }
097:
098: protected void tearDown() throws Exception {
099: fs.removeFileChangeListener(this );
100:
101: assertTrue(
102: "The doRenameAObjectWhileHoldingMDRLock must be called",
103: called);
104:
105: FileObject[] arr = toolbars.getChildren();
106: for (int i = 0; i < arr.length; i++) {
107: arr[i].delete();
108: }
109:
110: if (assigned != null) {
111: throw assigned;
112: }
113: }
114:
115: private static int cnt = 0;
116:
117: private void startRename() throws Exception {
118: synchronized (BIG_MDR_LOCK) {
119: RequestProcessor.getDefault().post(new Runnable() {
120: public void run() {
121: synchronized (BIG_MDR_LOCK) {
122: try {
123: called = true;
124: BIG_MDR_LOCK.notify();
125: BIG_MDR_LOCK.wait(); // for notification
126: // in some thread try to rename some object while holding mdr lock
127: anotherObj.rename("mynewname" + cnt++);
128: ok = true;
129: } catch (Exception ex) {
130: assigned = ex;
131: } finally {
132: // end this all
133: BIG_MDR_LOCK.notifyAll();
134: }
135: }
136: }
137: });
138: BIG_MDR_LOCK.wait();
139: }
140: }
141:
142: private boolean wasHereOnce;
143:
144: private void lockMdr() {
145: if (wasHereOnce) {
146: return;
147: }
148:
149: wasHereOnce = true;
150: // no more callbacks
151: fs.removeFileChangeListener(this );
152:
153: synchronized (BIG_MDR_LOCK) {
154: BIG_MDR_LOCK.notify(); // notified from herer
155: try {
156: BIG_MDR_LOCK.wait();
157: } catch (InterruptedException ex) {
158: ex.printStackTrace();
159: fail("No InterruptedExceptions");
160: }
161: assertTrue("Rename finished ok", ok);
162: }
163: }
164:
165: public void testMove() throws Exception {
166: synchronized (BIG_MDR_LOCK) {
167: startRename();
168: obj.move(anotherFolder);
169: }
170: }
171:
172: public void testCopy() throws Exception {
173: synchronized (BIG_MDR_LOCK) {
174: startRename();
175: obj.copy(anotherFolder);
176: }
177: }
178:
179: public void testRename() throws Exception {
180: synchronized (BIG_MDR_LOCK) {
181: startRename();
182: obj.rename("NewName.txt");
183: }
184: }
185:
186: public void testCreateShadow() throws Exception {
187: synchronized (BIG_MDR_LOCK) {
188: startRename();
189: obj.createShadow(anotherFolder);
190: }
191: }
192:
193: public void testTemplate() throws Exception {
194: synchronized (BIG_MDR_LOCK) {
195: startRename();
196: obj.createFromTemplate(anotherFolder);
197: }
198: }
199:
200: public void testTemplate2() throws Exception {
201: synchronized (BIG_MDR_LOCK) {
202: startRename();
203: obj.createFromTemplate(anotherFolder, "AhojVole.txt");
204: }
205: }
206:
207: //
208: // Listener triggers creation of the node
209: //
210:
211: public void fileRenamed(FileRenameEvent fe) {
212: lockMdr();
213: }
214:
215: public void fileAttributeChanged(FileAttributeEvent fe) {
216: }
217:
218: public void fileFolderCreated(FileEvent fe) {
219: lockMdr();
220: }
221:
222: public void fileDeleted(FileEvent fe) {
223: lockMdr();
224: }
225:
226: public void fileDataCreated(FileEvent fe) {
227: lockMdr();
228: }
229:
230: public void fileChanged(FileEvent fe) {
231: lockMdr();
232: }
233:
234: }
|