1 6 7 package FileSystemTest; 8 9 import java.beans.PropertyVetoException ; 10 import java.io.*; 11 import java.util.Enumeration ; 12 import junit.framework.Test; 13 import org.netbeans.junit.NbTestCase; 14 import org.netbeans.junit.NbTestSuite; 15 import org.openide.filesystems.*; 16 17 21 public class AttrTest extends NbTestCase { 22 File fileSystemDir; 24 LocalFileSystem fileSystem; 25 public AttrTest(String testName) { 26 super(testName); 27 } 28 29 33 public void testSpecialNamedAttr() throws IOException,PropertyVetoException { 34 preprocess(); 35 FileObject fo = getAnyFileObject() ; 36 setAttribute(fo,"\"", "1"); 37 setAttribute(fo,"h&", "2"); 38 setAttribute(fo,"<","3"); 39 setAttribute(fo,">","4"); 40 setAttribute(fo,"-", "5"); 41 setAttribute(fo,"*","6"); 42 System.gc(); 43 44 getAttribute(fo,"\"","1"); 45 getAttribute(fo,"h&","2"); 46 getAttribute(fo,"<","3"); 47 getAttribute(fo,">","4"); 48 getAttribute(fo,"-","5"); 49 getAttribute(fo,"*","6"); 50 postprocess(); 51 } 52 53 55 private void setAttribute(FileObject fo,String name,String value) { 56 try { 57 fo.setAttribute(name, value); 58 log ("attribute (name = " + name + ", value = " + value + ") setted" ); 59 } catch (Exception e) { 60 String msg = "failed on set attribute name = " + name + " , value = " + value; 61 log (msg); 62 assertTrue(msg,false); 63 } 64 } 65 67 private String getAttribute(FileObject fo,String name, String refValue) { 68 String value = (String ) fo.getAttribute(name); 69 if (value == null ) { 70 assertTrue("File object doesn't contain attribute (name = " + name + ", value = " + value + " ",false); 71 } else { 72 if (!value.equals(refValue)) { 73 assertTrue("FileObject read wrong attr value ( name = " + name + 74 ",correct value = " + refValue + " , read value = " + value, false ); 75 } 76 } 77 return value; 78 } 79 81 private void preprocess() throws IOException,PropertyVetoException { 82 fileSystemDir = new File(getWorkDir(), "testAtt123rDir"); 84 if(fileSystemDir.mkdir() == false || fileSystemDir.isDirectory() == false) { 85 throw new IOException (fileSystemDir.toString() + " is not directory"); 86 } 87 Enumeration en = Repository.getDefault().fileSystems(); 88 while (en.hasMoreElements()) { 89 FileSystem fs = (FileSystem) en.nextElement(); 90 if (fs instanceof LocalFileSystem) { 91 LocalFileSystem lfs = (LocalFileSystem) fs; 92 if (lfs.getRootDirectory().equals(fileSystemDir)) { 93 fileSystem = lfs; 94 break; 95 } 96 } 97 } 98 if (fileSystem == null ) { 99 fileSystem = new LocalFileSystem(); 100 fileSystem.setRootDirectory(fileSystemDir); 101 Repository.getDefault().addFileSystem(fileSystem); 102 } 103 104 } 105 106 108 private void postprocess() { 109 Repository.getDefault().getDefault().removeFileSystem(fileSystem); 110 fileSystemDir.deleteOnExit(); 111 } 113 114 private FileObject getAnyFileObject() { 115 return fileSystem.getRoot(); 116 } 117 118 public static Test suite() { 119 NbTestSuite suite = new NbTestSuite(AttrTest.class); 120 return suite; 121 } 122 123 125 public void testSetBackslashValue() throws IOException, PropertyVetoException { 126 preprocess(); 127 FileObject fo = getAnyFileObject(); 128 try { 129 setAttribute(fo, "\\", "2"); 130 getAttribute(fo, "\\", "2"); 131 } catch(Exception e) { 132 assertTrue(" failed:no attribute setted " + e,false ); 133 } 134 135 136 postprocess(); 137 } 138 139 140 143 public static void main(String [] args) { 144 junit.textui.TestRunner.run(suite()); 145 } 146 147 148 } 149 | Popular Tags |