1 19 20 package org.netbeans.modules.settings; 21 22 import java.io.IOException ; 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 36 public final class ScheduledRequestTest extends NbTestCase { 37 FileSystem fs; 38 39 40 public ScheduledRequestTest(String name) { 41 super(name); 42 } 43 44 protected void setUp() throws Exception { 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 { 54 FSA toRun = new FSA(); 55 FileObject fo = fs.getRoot(); 56 ScheduledRequest sr = new ScheduledRequest(fo, toRun); 57 Object obj1 = new Object (); 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 { 68 FSA toRun = new FSA(); 69 FileObject fo = fs.getRoot(); 70 ScheduledRequest sr = new ScheduledRequest(fo, toRun); 71 Object obj1 = new Object (); 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 obj2 = new Object (); 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 { 88 FSA toRun = new FSA(); 89 FileObject fo = fs.getRoot(); 90 ScheduledRequest sr = new ScheduledRequest(fo, toRun); 91 Object obj1 = new Object (); 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 { 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 { 111 finished = true; 112 } 113 114 } 115 } 116 | Popular Tags |