KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > settings > ScheduledRequestTest


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 2004 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.settings;
21
22 import java.io.IOException JavaDoc;
23
24 import org.netbeans.junit.NbTestCase;
25 import org.netbeans.junit.NbTestSuite;
26
27 import junit.textui.TestRunner;
28
29 import org.openide.filesystems.*;
30 import org.openide.filesystems.FileSystem;
31
32 /** JUnit tests
33  *
34  * @author Jan Pokorsky
35  */

36 public final class ScheduledRequestTest extends NbTestCase {
37     FileSystem fs;
38
39     /** Creates a new instance of ScheduledRequestTest */
40     public ScheduledRequestTest(String JavaDoc name) {
41         super(name);
42     }
43
44     protected void setUp() throws Exception JavaDoc {
45         super.setUp();
46         
47         LocalFileSystem lfs = new LocalFileSystem();
48         clearWorkDir();
49         lfs.setRootDirectory(this.getWorkDir());
50         fs = lfs;
51     }
52     
53     public void testSchedule() throws Exception JavaDoc {
54         FSA toRun = new FSA();
55         FileObject fo = fs.getRoot();
56         ScheduledRequest sr = new ScheduledRequest(fo, toRun);
57         Object JavaDoc obj1 = new Object JavaDoc();
58         sr.schedule(obj1);
59         assertNotNull("none file lock", sr.getFileLock());
60         for (int i = 0; i < 2 && !toRun.finished; i++) {
61             Thread.sleep(2500);
62         }
63         assertTrue("scheduled request was not performed yet", toRun.finished);
64         assertNull("file is still locked", sr.getFileLock());
65     }
66     
67     public void testCancel() throws Exception JavaDoc {
68         FSA toRun = new FSA();
69         FileObject fo = fs.getRoot();
70         ScheduledRequest sr = new ScheduledRequest(fo, toRun);
71         Object JavaDoc obj1 = new Object JavaDoc();
72         sr.schedule(obj1);
73         assertNotNull("none file lock", sr.getFileLock());
74         sr.cancel();
75         assertNull("file lock", sr.getFileLock());
76         Thread.sleep(2500);
77         assertTrue("scheduled request was performed", !toRun.finished);
78         
79         Object JavaDoc obj2 = new Object JavaDoc();
80         sr.schedule(obj2);
81         assertNotNull("none file lock", sr.getFileLock());
82         Thread.sleep(2500);
83         assertNull("file lock", sr.getFileLock());
84         assertTrue("scheduled request was not performed yet", toRun.finished);
85     }
86     
87     public void testForceToFinish() throws Exception JavaDoc {
88         FSA toRun = new FSA();
89         FileObject fo = fs.getRoot();
90         ScheduledRequest sr = new ScheduledRequest(fo, toRun);
91         Object JavaDoc obj1 = new Object JavaDoc();
92         sr.schedule(obj1);
93         assertNotNull("none file lock", sr.getFileLock());
94         sr.forceToFinish();
95         assertTrue("scheduled request was not performed yet", toRun.finished);
96         assertNull("file lock", sr.getFileLock());
97     }
98     
99     public void testRunAndWait() throws Exception JavaDoc {
100         FSA toRun = new FSA();
101         FileObject fo = fs.getRoot();
102         ScheduledRequest sr = new ScheduledRequest(fo, toRun);
103         sr.runAndWait();
104         assertTrue("scheduled request was not performed yet", toRun.finished);
105         assertNull("file lock", sr.getFileLock());
106     }
107     
108     private static class FSA implements org.openide.filesystems.FileSystem.AtomicAction {
109         boolean finished = false;
110         public void run() throws IOException JavaDoc {
111             finished = true;
112         }
113         
114     }
115 }
116
Popular Tags