KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > turbo > TurboTest


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.netbeans.modules.turbo;
21
22 import junit.framework.TestCase;
23 import org.openide.util.Lookup;
24 import org.openide.util.lookup.InstanceContent;
25 import org.openide.util.lookup.AbstractLookup;
26 import org.openide.filesystems.FileObject;
27 import org.openide.filesystems.LocalFileSystem;
28 import org.openide.filesystems.FileSystem;
29
30 import java.io.File JavaDoc;
31
32 /**
33  * Tests Turbo
34  *
35  * @author Petr Kuzel
36  */

37 public class TurboTest extends TestCase {
38
39     private static InstanceContent content = new InstanceContent();
40
41     private FileSystem fs;
42
43     private static TestEnvironment env;
44
45     private int tearDownCounter = countTestCases();
46
47     // per testcase static setup
48
static {
49         env = new TestEnvironment();
50         Turbo.initEnvironment(env);
51     }
52
53     // called before every method
54
protected void setUp() throws Exception JavaDoc {
55
56         // prepare simple LFS
57

58         LocalFileSystem fs = new LocalFileSystem();
59         File JavaDoc tmp = new File JavaDoc(System.getProperty("java.io.tmpdir") + File.separator + "turbo-test");
60         tmp.mkdir();
61         tmp.deleteOnExit();
62         File JavaDoc theFile = new File JavaDoc(tmp, "theFile");
63         theFile.createNewFile();
64         theFile.deleteOnExit();
65         fs.setRootDirectory(tmp);
66
67         System.setProperty("netbeans.experimental.vcsTurboStatistics", "performance");
68         this.fs = fs;
69     }
70
71     protected void tearDown() throws Exception JavaDoc {
72         if (tearDownCounter-- == 1) {
73             try {
74                 Turbo.getDefault().finalize();
75             } catch (Throwable JavaDoc t) {
76                 t.printStackTrace();
77             }
78         }
79     }
80
81
82     /**
83      * Once setting an atribute it must be in memory until
84      * file reference is GCed. Providers must be queried.
85      * Th event must be fired.
86      */

87     public void testHitAfterSet() throws Exception JavaDoc {
88         Turbo faq = Turbo.getDefault();
89
90         FileObject fo = fs.getRoot().getFileObject("theFile");
91         AttributeListener l = new AttributeListener();
92         faq.addTurboListener(l);
93
94         faq.writeEntry(fo, "test", "testValue");
95         assertTrue("Missing Turbo event", l.hit());
96
97         // must not raise fail on providers read layer
98
faq.readEntry(fo, "test");
99
100         // it shoud not fire on equel values
101
l.reset();
102         faq.writeEntry(fo, "test", "testValue");
103         assertFalse("Missing Turbo event", l.hit());
104     }
105
106     /**
107      * <code<null</code> value serves for invalidation purposes.
108      */

109     public void testInvalidation() throws Exception JavaDoc {
110         Turbo faq = Turbo.getDefault();
111
112         FileObject fo = fs.getRoot().getFileObject("theFile");
113
114         faq.writeEntry(fo, "invalidate", "testValue");
115         faq.writeEntry(fo, "invalidate", null);
116         assertFalse(faq.isPrepared(fo, "invalidate"));
117
118         // after reading attribute we know that is unknown, it's prepared
119
faq.readEntry(fo, "invalidate");
120         assertTrue(faq.isPrepared(fo, "invalidate"));
121     }
122
123     private static class TestEnvironment extends Turbo.Environment {
124
125         private Lookup l;
126
127         public TestEnvironment() {
128             content.add(new AssertingTurboProvider());
129             l = new AbstractLookup(content);
130         }
131
132         public Lookup getLookup() {
133             return l;
134         }
135     }
136
137     private static class AssertingTurboProvider implements TurboProvider {
138
139         public boolean recognizesAttribute(String JavaDoc name) {
140             return "test".equals(name);
141         }
142
143         public boolean recognizesEntity(Object JavaDoc key) {
144             return true;
145         }
146
147         public Object JavaDoc readEntry(Object JavaDoc key, String JavaDoc name, MemoryCache memoryCache) {
148             fail();
149             return null;
150         }
151
152         public boolean writeEntry(Object JavaDoc key, String JavaDoc name, Object JavaDoc value) {
153             return true;
154         }
155     }
156
157     private class AttributeListener implements TurboListener {
158
159         private volatile boolean hit = false;
160
161         public void reset() {
162             hit = false;
163         }
164
165         public boolean hit() {
166             return hit;
167         }
168
169         public synchronized void entryChanged(Object JavaDoc key, String JavaDoc name, Object JavaDoc value) {
170             hit = true;
171             notifyAll();
172         }
173
174         public synchronized void waitForHit(int timeout) {
175             long start = System.currentTimeMillis();
176             while (hit == false) {
177                 try {
178                     wait(timeout);
179                     if (System.currentTimeMillis() - start > timeout) return;
180                 } catch (InterruptedException JavaDoc e) {
181                     return;
182                 }
183             }
184         }
185     }
186
187
188     /**
189      * Test provider that so slow that someone else populates memory faster.
190      */

191     public void testSlowProvider() throws Exception JavaDoc {
192         Turbo faq = Turbo.getDefault();
193
194         FileObject fo = fs.getRoot().getFileObject("theFile");
195
196         SlowProvider sp = new SlowProvider();
197         content.add(sp);
198         try {
199             faq.prepareEntry(fo, "slow"); // hey a thread is spawned to load attribute on background
200

201             // simulate faster client
202

203             AttributeListener expecting = new AttributeListener();
204             faq.addTurboListener(expecting);
205             faq.writeEntry(fo, "slow", "result"); // expecting synchronous event
206
faq.removeTurboListener(expecting);
207             assertTrue(expecting.hit());
208
209             // now unblock provider and let it report the same value
210

211             AttributeListener besilent = new AttributeListener();
212             faq.addTurboListener(besilent);
213             sp.notifyReady(); // an event must not be fired because of the same value
214
besilent.waitForHit(100);
215             faq.removeTurboListener(besilent);
216
217             // TODO it's safer to fire than to not fire it's just possibly slow
218
//assertFalse(besilent.hit());
219
} finally {
220             content.remove(sp);
221         }
222     }
223
224     private class SlowProvider implements TurboProvider {
225
226         volatile boolean ready;
227
228         public boolean recognizesAttribute(String JavaDoc name) {
229             return "slow".equals(name);
230         }
231
232         public boolean recognizesEntity(Object JavaDoc key) {
233             return true;
234         }
235
236         public synchronized Object JavaDoc readEntry(Object JavaDoc key, String JavaDoc name, MemoryCache memoryCache) {
237             while (ready == false) {
238                 try {
239                     wait();
240                 } catch (InterruptedException JavaDoc e) {
241                     break;
242                 }
243             }
244             return "result";
245         }
246
247         public boolean writeEntry(Object JavaDoc key, String JavaDoc name, Object JavaDoc value) {
248             // XXX Note this is coming as result of faster FAQ.writeAttribute
249
return true;
250         }
251
252         public synchronized void notifyReady() {
253             ready = true;
254             notifyAll();
255         }
256     }
257
258
259     /**
260      * Prepare must fire event on successfull prepare
261      * XXX yet undefined behaviour otherwise.
262      */

263     public void testPrepare() throws Exception JavaDoc {
264         Turbo faq = Turbo.getDefault();
265         FileObject fo = fs.getRoot().getFileObject("theFile");
266         PrepareProvider pp = new PrepareProvider();
267         content.add(pp);
268         try {
269
270             AttributeListener l = new AttributeListener();
271             faq.addTurboListener(l);
272             faq.prepareEntry(fo, "prepare");
273             l.waitForHit(500);
274             faq.removeTurboListener(l);
275
276             assertTrue(l.hit);
277
278             // be sure that event granularity is given by attr,file pair (not just by file)
279

280             l.reset();
281             faq.addTurboListener(l);
282             faq.prepareEntry(fo, "prepare2");
283             l.waitForHit(500);
284             faq.removeTurboListener(l);
285
286             assertTrue(l.hit);
287         } finally {
288             content.remove(pp);
289         }
290     }
291
292     private class PrepareProvider implements TurboProvider {
293
294         public boolean recognizesAttribute(String JavaDoc name) {
295             return "prepare".equals(name) || "prepare2".equals(name);
296         }
297
298         public boolean recognizesEntity(Object JavaDoc key) {
299             return true;
300         }
301
302         public Object JavaDoc readEntry(Object JavaDoc key, String JavaDoc name, MemoryCache memoryCache) {
303             return "done";
304         }
305
306         public boolean writeEntry(Object JavaDoc key, String JavaDoc name, Object JavaDoc value) {
307             return true;
308         }
309     }
310
311     /**
312      * Register new provider and verify that it's called
313      * without throwing an exception.
314      */

315     public void testWrongProvider() {
316         WrongProvider provider = new WrongProvider();
317
318         Lookup.Template t = new Lookup.Template(TurboProvider.class);
319         Lookup.Result r = env.getLookup().lookup(t);
320         content.add(provider);
321         assert r.allInstances().size() == 2 : "r=" + r.allInstances();
322
323         Turbo faq = Turbo.getDefault();
324
325         FileObject fo = fs.getRoot().getFileObject("theFile");
326
327         try {
328             assertNull(faq.readEntry(fo, "wrong"));
329         } catch (RuntimeException JavaDoc ex) {
330             fail("WrongProvider crashed framework");
331         }
332
333         assertTrue("Provider registration was missed", provider.queried);
334
335         content.remove(provider);
336     }
337
338     private class WrongProvider implements TurboProvider {
339
340         volatile boolean queried;
341
342         public boolean recognizesAttribute(String JavaDoc name) {
343             queried = true;
344             return "wrong".equals(name);
345             // throw new RuntimeException(); XXX too early
346
}
347
348         public boolean recognizesEntity(Object JavaDoc key) {
349             return true;
350         }
351
352         public Object JavaDoc readEntry(Object JavaDoc key, String JavaDoc name, MemoryCache memoryCache) {
353             throw new RuntimeException JavaDoc();
354         }
355
356         public boolean writeEntry(Object JavaDoc key, String JavaDoc name, Object JavaDoc value) {
357             throw new RuntimeException JavaDoc();
358         }
359     }
360
361
362 }
363
Popular Tags