KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > Deadlock38554Test


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.openide.loaders;
21
22 import junit.textui.TestRunner;
23
24 import org.openide.filesystems.*;
25 import org.openide.util.Lookup;
26 import java.io.IOException JavaDoc;
27 import java.util.*;
28 import org.netbeans.junit.*;
29 import java.beans.PropertyChangeListener JavaDoc;
30
31 /** Simulates the deadlock from issue 38554.
32  * @author Jaroslav Tulach
33  */

34 public class Deadlock38554Test extends NbTestCase implements FileChangeListener {
35     private FileSystem lfs;
36     private DataObject src;
37     private DataFolder tgz;
38     private Uni loader;
39
40     public Deadlock38554Test (String JavaDoc name) {
41         super(name);
42     }
43     
44     protected void setUp() throws Exception JavaDoc {
45         clearWorkDir();
46         lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), new String JavaDoc[] {
47             "folder/",
48             "folder/f.uni",
49             "target/"
50         });
51         
52         loader = (Uni)Uni.getLoader (Uni.class);
53         AddLoaderManuallyHid.addRemoveLoader (loader, true);
54         
55         src = DataObject.find (lfs.findResource("folder/f.uni"));
56         tgz = DataFolder.findFolder(lfs.findResource("target"));
57         lfs.addFileChangeListener (this);
58     }
59     
60     protected void tearDown() throws Exception JavaDoc {
61         lfs.removeFileChangeListener (this);
62         TestUtilHid.destroyLocalFileSystem(getName());
63         AddLoaderManuallyHid.addRemoveLoader (loader, false);
64     }
65     
66     public void testEvilSynchronziedFileChangeListenerWhenCopy () throws Exception JavaDoc {
67         src.copy (tgz);
68     }
69     public void testEvilSynchronziedFileChangeListenerWhenMove () throws Exception JavaDoc {
70         src.move (tgz);
71     }
72     public void testEvilSynchronziedFileChangeListenerWhenCreateFromTemplate () throws Exception JavaDoc {
73         src.createFromTemplate (tgz);
74     }
75     public void testEvilSynchronziedFileChangeListenerWhenCreateFromTemplateWithName () throws Exception JavaDoc {
76         src.createFromTemplate (tgz, "myNewNameOfTheObject");
77     }
78     public void testEvilSynchronziedFileChangeListenerWhenDelete () throws Exception JavaDoc {
79         src.delete ();
80     }
81     public void testEvilSynchronziedFileChangeListenerWhenRename () throws Exception JavaDoc {
82         src.rename ("myNewName");
83     }
84     
85     //
86
// synchronized listeners
87
//
88

89     private void block (final FileObject fo) {
90         class OtherThreadAccess implements Runnable JavaDoc {
91             public void run () {
92                 try {
93                     if (fo.isValid ()) {
94                         DataObject.find (fo); // should deadlock
95
}
96                 } catch (DataObjectNotFoundException ex) {
97                     ex.printStackTrace();
98                     fail ("Error");
99                 }
100             }
101         }
102         
103         OtherThreadAccess other = new OtherThreadAccess ();
104         org.openide.util.RequestProcessor.getDefault ().post (other).waitFinished (); // should deadlock
105
}
106     
107     public void fileAttributeChanged (FileAttributeEvent fe) {
108         block (fe.getFile ());
109     }
110
111     public void fileChanged (FileEvent fe) {
112         block (fe.getFile ());
113     }
114     
115     public void fileDataCreated (FileEvent fe) {
116         block (fe.getFile ());
117     }
118     
119     public void fileDeleted (FileEvent fe) {
120         block (fe.getFile ());
121     }
122     
123     public void fileFolderCreated (FileEvent fe) {
124         block (fe.getFile ());
125     }
126     
127     public void fileRenamed (FileRenameEvent fe) {
128         block (fe.getFile ());
129     }
130     
131     public static final class Uni extends UniFileLoader {
132         public Uni () {
133             super(MultiDataObject.class);
134         }
135         protected String JavaDoc displayName() {
136             return "UniLoader";
137         }
138         /** Recognizes just two files - .forget and .keep at once, only in non-forgetable mode
139          */

140         protected synchronized FileObject findPrimaryFile(FileObject fo) {
141             return fo.hasExt ("uni") ? fo : null;
142         }
143         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
144             return new NotifyObject (primaryFile, this);
145         }
146         protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) {
147             return new FileEntry (obj, primaryFile);
148         }
149         
150         private class NotifyObject extends MultiDataObject {
151             public NotifyObject (FileObject pf, Uni uni) throws DataObjectExistsException {
152                 super (pf, uni);
153             }
154             
155             
156             protected FileObject handleRename (String JavaDoc name) throws IOException JavaDoc {
157                 return getPrimaryEntry ().rename (name);
158             }
159             
160             protected void handleDelete () throws IOException JavaDoc {
161                 getPrimaryEntry ().delete ();
162             }
163             
164             protected org.openide.loaders.DataObject handleCopy (org.openide.loaders.DataFolder f) throws IOException JavaDoc {
165                 FileObject retValue = getPrimaryEntry ().copy (f.getPrimaryFile (), "suffix");
166                 return new NotifyObject (retValue, (Uni)getLoader ());
167             }
168             
169             protected org.openide.loaders.DataShadow handleCreateShadow (org.openide.loaders.DataFolder f) throws IOException JavaDoc {
170                 DataShadow retValue;
171                 retValue = super.handleCreateShadow (f);
172                 return retValue;
173             }
174             
175             protected org.openide.loaders.DataObject handleCreateFromTemplate (org.openide.loaders.DataFolder df, String JavaDoc name) throws IOException JavaDoc {
176                 FileObject retValue = getPrimaryEntry ().createFromTemplate (df.getPrimaryFile (), name);
177                 return new NotifyObject (retValue, (Uni)getLoader ());
178             }
179             
180             protected FileObject handleMove (org.openide.loaders.DataFolder df) throws IOException JavaDoc {
181                 FileObject retValue = FileUtil.moveFile (getPrimaryFile (), df.getPrimaryFile (), getPrimaryFile ().getNameExt ());
182                 return retValue;
183             }
184         }
185     }
186     
187   
188 }
189
Popular Tags