KickJava   Java API By Example, From Geeks To Geeks.

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


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.framework.AssertionFailedError;
23 import org.netbeans.junit.NbTestCase;
24 import org.openide.filesystems.FileAttributeEvent;
25 import org.openide.filesystems.FileChangeListener;
26 import org.openide.filesystems.FileEvent;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileRenameEvent;
29 import org.openide.filesystems.FileSystem;
30 import org.openide.filesystems.FileUtil;
31 import org.openide.filesystems.Repository;
32 import org.openide.nodes.Node;
33 import org.openide.util.RequestProcessor;
34
35 /** Simulates the deadlock between copy/move operation and the creation
36  * of node.
37  *
38  * @author Jaroslav Tulach
39  */

40 public class Deadlock51637Test extends NbTestCase implements FileChangeListener {
41     FileObject toolbars;
42     FileSystem fs;
43     DataFolder toolbarsFolder;
44     DataFolder anotherFolder;
45     DataObject obj;
46     
47     Node node;
48     Exception JavaDoc assigned;
49     boolean called;
50     boolean enough;
51     
52     public Deadlock51637Test(String JavaDoc testName) {
53         super (testName);
54     }
55     
56     protected void setUp() throws Exception JavaDoc {
57         fs = Repository.getDefault ().getDefaultFileSystem ();
58         FileObject root = fs.getRoot ();
59         toolbars = FileUtil.createFolder (root, "Toolbars");
60         toolbarsFolder = DataFolder.findFolder (toolbars);
61         FileObject[] arr = toolbars.getChildren ();
62         for (int i = 0; i < arr.length; i++) {
63             arr[i].delete ();
64         }
65         FileObject fo = FileUtil.createData (root, "Ahoj.txt");
66         obj = DataObject.find (fo);
67         fo = FileUtil.createFolder (root, "Another");
68         anotherFolder = DataFolder.findFolder (fo);
69         
70         fs.addFileChangeListener (this);
71     }
72
73     protected void tearDown() throws Exception JavaDoc {
74         fs.removeFileChangeListener (this);
75         
76         assertTrue ("The doCreateNode must be called", called);
77         
78         FileObject[] arr = toolbars.getChildren ();
79         for (int i = 0; i < arr.length; i++) {
80             arr[i].delete ();
81         }
82         
83     }
84     
85     private void doCreateNode () {
86         if (enough) {
87             return;
88         }
89         if (node != null) {
90             assertNotNull ("Node is not null, but it was not assigned", assigned);
91             
92             AssertionFailedError a = new AssertionFailedError ("Node cannot be null");
93             a.initCause (assigned);
94             throw a;
95         }
96         // just one event is enough
97
enough = true;
98         fs.removeFileChangeListener (this);
99         
100         called = true;
101         
102         boolean ok;
103         try {
104             final Exception JavaDoc now = new Exception JavaDoc ("Calling to rp");
105             ok = RequestProcessor.getDefault ().post (new Runnable JavaDoc () {
106                 public void run () {
107                     node = obj.getNodeDelegate ();
108                     
109                     assigned = new Exception JavaDoc ("Created in RP");
110                     assigned.initCause (now);
111                 }
112             }).waitFinished (100000);
113         } catch (InterruptedException JavaDoc ex) {
114             AssertionFailedError a = new AssertionFailedError (ex.getMessage ());
115             a.initCause (ex);
116             throw a;
117         }
118         
119         if (node == null) {
120             fail ("Node is still null and the waitFinished was " + ok);
121         }
122     }
123     
124
125     public void testMove () throws Exception JavaDoc {
126         obj.move (anotherFolder);
127     }
128
129     public void testCopy () throws Exception JavaDoc {
130         obj.copy (anotherFolder);
131     }
132     
133     public void testRename () throws Exception JavaDoc {
134         obj.rename ("NewName.txt");
135     }
136     
137     public void testCreateShadow () throws Exception JavaDoc {
138         obj.createShadow (anotherFolder);
139     }
140     
141     public void testTemplate () throws Exception JavaDoc {
142         obj.createFromTemplate (anotherFolder);
143     }
144
145     public void testTemplate2 () throws Exception JavaDoc {
146         obj.createFromTemplate (anotherFolder, "AhojVole.txt");
147     }
148
149     //
150
// Listener triggers creation of the node
151
//
152

153     public void fileRenamed (FileRenameEvent fe) {
154         doCreateNode ();
155     }
156
157     public void fileAttributeChanged (FileAttributeEvent fe) {
158     }
159
160     public void fileFolderCreated (FileEvent fe) {
161         doCreateNode ();
162     }
163
164     public void fileDeleted (FileEvent fe) {
165         doCreateNode ();
166     }
167
168     public void fileDataCreated (FileEvent fe) {
169         doCreateNode ();
170     }
171
172     public void fileChanged (FileEvent fe) {
173         doCreateNode ();
174     }
175     
176 }
177
Popular Tags