Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2011 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2011 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.masterfs.watcher; |
43 |
|
44 |
import java.io.File; |
45 |
import org.netbeans.modules.masterfs.providers.Notifier; |
46 |
import java.io.IOException; |
47 |
import java.lang.ref.Reference; |
48 |
import java.lang.ref.WeakReference; |
49 |
import java.util.LinkedList; |
50 |
import java.util.List; |
51 |
import java.util.Queue; |
52 |
import java.util.concurrent.ArrayBlockingQueue; |
53 |
import java.util.concurrent.BlockingQueue; |
54 |
import org.netbeans.junit.MockServices; |
55 |
import org.netbeans.junit.NbTestCase; |
56 |
import org.netbeans.modules.masterfs.filebasedfs.fileobjects.FileObjectFactory; |
57 |
import org.netbeans.modules.masterfs.filebasedfs.naming.NamingFactory; |
58 |
import org.openide.filesystems.FileChangeAdapter; |
59 |
import org.openide.filesystems.FileEvent; |
60 |
import org.openide.filesystems.FileObject; |
61 |
import org.openide.filesystems.FileUtil; |
62 |
import org.openide.util.Lookup; |
63 |
|
64 |
/** Test the behavior of Watcher. |
65 |
* |
66 |
* @author Jaroslav Tulach <jtulach@netbeans.org> |
67 |
*/ |
68 |
public class WatcherSuspendTest extends NbTestCase { |
69 |
private TestNotifier notify; |
70 |
private L listener; |
71 |
private Watcher watcher; |
72 |
|
73 |
public WatcherSuspendTest(String s) { |
74 |
super(s); |
75 |
} |
76 |
|
77 |
@Override |
78 |
protected void setUp() throws Exception { |
79 |
clearWorkDir(); |
80 |
MockServices.setServices(TestNotifier.class); |
81 |
listener = new L(); |
82 |
watcher = Lookup.getDefault().lookup(Watcher.class); |
83 |
notify = Lookup.getDefault().lookup(TestNotifier.class); |
84 |
notify.start(); |
85 |
} |
86 |
|
87 |
@Override |
88 |
protected void tearDown() throws Exception { |
89 |
} |
90 |
|
91 |
public void testSuspendRequests() throws Exception { |
92 |
FileObject root = FileUtil.toFileObject(getWorkDir()); |
93 |
FileObject folder = root.createFolder("dir"); |
94 |
File dir = FileUtil.toFile(folder); |
95 |
|
96 |
FileObject[] arr = folder.getChildren(); |
97 |
folder.addFileChangeListener(listener); |
98 |
assertEquals("Empty ", 0, arr.length); |
99 |
|
100 |
final String prop = "org.netbeans.io.suspend".intern(); |
101 |
synchronized (prop) { |
102 |
int prev = Integer.getInteger(prop, 0); |
103 |
System.setProperty(prop, "" + (prev + 1)); |
104 |
prop.notifyAll(); |
105 |
} |
106 |
|
107 |
new File(dir, "data.txt").createNewFile(); |
108 |
notify.event.offer(dir.getPath()); |
109 |
|
110 |
assertProperty("One path waiting", "1", "org.netbeans.io.pending"); |
111 |
listener.assertEvents("No event yet", 0, 200); |
112 |
|
113 |
synchronized (prop) { |
114 |
int prev = Integer.getInteger(prop, 0); |
115 |
System.setProperty(prop, "" + (prev - 1)); |
116 |
prop.notifyAll(); |
117 |
} |
118 |
assertProperty("Pending status is cleared", null, "org.netbeans.io.pending"); |
119 |
|
120 |
listener.assertEvents("One event delivered", 1, 5000); |
121 |
|
122 |
arr = folder.getChildren(); |
123 |
assertEquals("One child", 1, arr.length); |
124 |
assertEquals("data.txt", arr[0].getNameExt()); |
125 |
} |
126 |
|
127 |
private void assertProperty(String msg, String expVal, String propName) throws InterruptedException { |
128 |
String val = null; |
129 |
for (int i = 0; i < 100; i++) { |
130 |
val = System.getProperty(propName); |
131 |
if (expVal == null && val == null) { |
132 |
return; |
133 |
} |
134 |
if (expVal != null && expVal.equals(val)) { |
135 |
return; |
136 |
} |
137 |
Thread.sleep(100); |
138 |
} |
139 |
fail(msg + " exp: " + expVal + " was: " + val); |
140 |
} |
141 |
|
142 |
|
143 |
private static final class L extends FileChangeAdapter { |
144 |
private int cnt; |
145 |
|
146 |
@Override |
147 |
public synchronized void fileDataCreated(FileEvent fe) { |
148 |
cnt++; |
149 |
} |
150 |
|
151 |
@Override |
152 |
public void fileFolderCreated(FileEvent fe) { |
153 |
cnt++; |
154 |
} |
155 |
|
156 |
private synchronized void assertEvents( |
157 |
String msg, int cnt, int timeOut |
158 |
) throws InterruptedException { |
159 |
if (this.cnt != cnt) { |
160 |
wait(timeOut); |
161 |
} |
162 |
assertEquals(msg, cnt, this.cnt); |
163 |
} |
164 |
|
165 |
} |
166 |
|
167 |
public static final class TestNotifier extends Notifier<Integer> { |
168 |
final List<String> registered = new LinkedList<String>(); |
169 |
final BlockingQueue<String> event = new ArrayBlockingQueue<String>(10); |
170 |
|
171 |
@Override |
172 |
public Integer addWatch(String path) throws IOException { |
173 |
int size = registered.size(); |
174 |
registered.add(path); |
175 |
return size; |
176 |
} |
177 |
|
178 |
@Override |
179 |
public void removeWatch(Integer key) throws IOException { |
180 |
registered.set(key, null); |
181 |
} |
182 |
|
183 |
@Override |
184 |
public String nextEvent() throws IOException, InterruptedException { |
185 |
return event.take(); |
186 |
} |
187 |
|
188 |
@Override |
189 |
protected void start() throws IOException { |
190 |
registered.clear(); |
191 |
} |
192 |
} |
193 |
} |