1 6 7 package FileSystemTest; 8 9 import java.beans.PropertyVetoException ; 10 import java.io.*; 11 import java.util.Iterator ; 12 import java.util.Set ; 13 14 import org.openide.nodes.Children; 15 import org.openide.nodes.Node; 16 import org.openide.windows.Mode; 17 import org.openide.windows.TopComponent; 18 import org.openide.windows.WindowManager; 19 import org.openide.windows.Workspace; 20 import org.openide.filesystems.*; 21 import org.openide.loaders.DataObject; 22 import org.openide.loaders.DataObjectNotFoundException; 23 import org.openide.loaders.DataFilter; 24 import org.openide.loaders.RepositoryNodeFactory; 25 26 import junit.framework.*; 27 import org.netbeans.junit.*; 28 import org.openide.util.Lookup; 29 30 34 public class FSTest extends NbTestCase { 35 File testDir; 36 LocalFileSystem testFS; 37 String TEST_FILE_OBJECT_NAME = "testedfile.java"; 38 static class FailException extends Exception { 39 FailException () { 40 super (); 41 } 42 FailException (String msg) { 43 super(msg); 44 } 45 } 46 47 static class ErrorException extends Exception { 48 ErrorException() { 49 super(); 50 } 51 ErrorException(String name) { 52 super(name); 53 } 54 } 55 56 57 public FSTest(String testName) { 58 super(testName); 59 } 60 61 64 public static void main(String [] args) { 65 junit.textui.TestRunner.run(suite()); 66 } 67 68 69 public static Test suite() { 70 NbTestSuite suite = new NbTestSuite(FSTest.class); 71 return suite; 72 } 73 76 public void testHide () throws IOException { 77 Node node = null; 78 FileObject fo = null; 79 FileSystem fs = null; 80 try { 81 try { 82 84 86 fo = getRefFO(); 87 fs = fo.getFileSystem(); 88 fs.setHidden(false); 89 System.out.println(fs); 90 failTest("file system is hidden", fs.isHidden() ==false); 91 DataObject dobj = DataObject.find(fo); 92 node = dobj.getNodeDelegate(); 93 } catch (DataObjectNotFoundException donfe ) { 94 failTest (donfe); 95 } catch (FileStateInvalidException fsie ) { 96 failTest (fsie); 97 } 98 Node repNode = RepositoryNodeFactory.getDefault().repository(DataFilter.ALL); 99 fs.setHidden(false); 100 if (findNode(repNode,node,2) == false ) { 102 failTest("filesystem is hidden (must be shown)", false); 103 } 104 fs.setHidden(true); 105 if (findNode(repNode,node,2) == true ) { 106 failTest("filesystem is shown (must be hidden)", false); 107 } 108 fs.setHidden(false); 109 if (findNode(repNode,node,2) == false ) { 110 failTest("filesystem is hidden (must be shown)", false); 111 } 112 } catch (FailException fe) { 113 System.out.println("failed"); 114 } 115 System.out.println("passed"); 116 } 117 118 120 protected FileObject getRefFO() throws java.io.IOException { 121 Repository rep = Repository.getDefault(); 122 FileObject fo = rep.findResource(TEST_FILE_OBJECT_NAME); 123 if (fo == null) { 124 fo = testFS.getRoot().createData(TEST_FILE_OBJECT_NAME) ; 125 } 126 return fo; 127 } 128 129 132 private void failTest(Exception e) throws FailException { 133 e.printStackTrace (); 134 throw new FailException (); 135 136 } 137 138 private void failTest(String str, boolean status) throws FailException { 139 if (status == false ) { 140 System.out.println(str); 141 throw new FailException(); 142 } 143 144 } 145 146 private boolean findNode(Node parent, Node child, int depth) { 147 if (parent.getName().equals(child.getName()) ) { 148 return true; 149 } 150 if (depth == 0 ) { 151 return false; 152 } 153 Children children = parent.getChildren(); 154 Node childNodes[] = children.getNodes(); 155 for (int i = 0 ; i < childNodes.length ; i++ ) { 156 if (childNodes[i] == child || findNode(childNodes[i],child, depth - 1 ) ) { 157 return true; 158 } 159 } 161 return false; 162 } 163 164 private void sleepCurThread(int milis) { 165 try { 166 Thread.currentThread().sleep(milis); 167 } catch (InterruptedException exception) { 168 exception.printStackTrace(); 169 } 170 } 171 172 175 public void testReadOnLocalFS() throws IOException { 176 try { 177 FileObject fo = getRefFO(); 178 FileSystem fs = null; 179 try { 180 fs = fo.getFileSystem(); 181 }catch (FileStateInvalidException fse) { 182 fse.printStackTrace(); 183 throw new FailException("fo.getFileSystem() failed"); 184 } 185 if (! (fs instanceof LocalFileSystem)) { 186 throw new ErrorException("tested filesystem is not instance of LocalFileSystem"); 187 } 188 LocalFileSystem lfs =(LocalFileSystem )fs; 189 boolean tmpROValue = lfs.isReadOnly(); 190 lfs.setReadOnly(true ); 192 FileObject root = fs.getRoot(); 193 194 try { 196 root.createData("MyNewFile.txt"); 197 throw new FailException ("Create FileObject on read only filesystem failed."); 198 } catch (IOException ioe) { 199 System.out.println("Create FileObject on read only filesystem file passed."); 200 } 201 try { 203 root.createFolder("MyNewFolder"); 204 throw new FailException ("Create folder on read only filesytem failed."); 205 } catch (IOException ioe) { 206 System.out.println("Create folder on read only filesystem passed"); 207 } 208 209 FileLock lock = null; 211 try { 212 213 lock = fo.lock(); 214 OutputStream os = fo.getOutputStream(lock); 215 PrintStream ps = new PrintStream(os); 216 ps.println("import ahoj;"); 217 lock.releaseLock(); 218 throw new FailException("write to file object on read only filesystem failed."); 219 } catch (IOException ioe) { 220 System.out.println("write to file object on read only filesystem passed."); 221 } 222 223 224 lfs.setReadOnly(false); 227 228 try { 230 FileObject fo2 = root.createData("MyNewFile.txt"); 231 fo2.delete(); 232 System.out.println("Create FileObject on read/write filesystem passed."); 233 } catch (IOException ioe) { 234 ioe.printStackTrace(); 235 throw new FSTest.FailException("Create FileObject on read/write filesystem file failed."); 236 } 237 try { 239 FileObject folder = root.createFolder("MyNewFolder"); 240 folder.delete(); 241 System.out.println("Create folder on read only filesytem passed."); 242 } catch (IOException ioe) { 243 ioe.printStackTrace(); 244 throw new FSTest.FailException("Create folder on read/write filesystem failed."); 245 } 246 247 lock = fo.lock(); 249 try { 250 OutputStream os = fo.getOutputStream(lock); 251 PrintStream ps = new PrintStream(os); 252 ps.println("import ahoj;"); 253 System.out.println("write to file object on read/write filesystem passed."); 254 lock.releaseLock(); 255 } catch (IOException ioe) { 256 ioe.printStackTrace(); 257 lock.releaseLock(); 258 throw new FSTest.FailException("write to file object on read/write filesystem failed."); 259 } 260 } catch (FailException fe) { 261 fe.printStackTrace(); 262 assertTrue(fe.getMessage(), false); 263 264 } catch (ErrorException ee) { 265 ee.printStackTrace(); 266 assertTrue(ee.getMessage(),false); 267 } 268 } 269 protected void setUp() throws IOException, PropertyVetoException { 270 testDir = new File(File.createTempFile("ssadfasdfsadf","6346436").getParentFile(),"fgsagkjasdhgksa"); 271 if (testDir.mkdir() == false && testDir.isDirectory() == false) { 273 throw new IOException ("Error, temporary directory is not created"); 274 } 275 testFS = new LocalFileSystem(); 276 testFS.setRootDirectory(testDir); 277 FileSystem [] fss = Repository.getDefault().toArray(); 278 for (int i = 0 ; i < fss.length ; i++ ) { 279 FileSystem fs = fss[i]; 280 if (fs instanceof LocalFileSystem && ((LocalFileSystem) fs).getRootDirectory().equals(testDir)) { 281 testFS = (LocalFileSystem) fs; 283 return; 284 } 285 } 286 FileObject root = testFS.getRoot(); 287 288 289 Repository.getDefault().addFileSystem(testFS); 290 } 291 292 } | Popular Tags |