KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > FileSystemTest > AttrTest


1 /*
2  * AttrTest.java
3  *
4  * Created on August 15, 2002, 4:09 PM
5  */

6
7 package FileSystemTest;
8
9 import java.beans.PropertyVetoException JavaDoc;
10 import java.io.*;
11 import java.util.Enumeration JavaDoc;
12 import junit.framework.Test;
13 import org.netbeans.junit.NbTestCase;
14 import org.netbeans.junit.NbTestSuite;
15 import org.openide.filesystems.*;
16
17 /**
18  *
19  * @author pz97949
20  */

21 public class AttrTest extends NbTestCase {
22 // File fileSystemFile;
23
File fileSystemDir;
24     LocalFileSystem fileSystem;
25      public AttrTest(String JavaDoc testName) {
26         super(testName);
27     }
28    
29      /** tests set/get attribute to fileobject special named:
30       * "\"
31       * see to bug http://installer.netbeans.org/issues/show_bug.cgi?id=8976
32       */

33     public void testSpecialNamedAttr() throws IOException,PropertyVetoException JavaDoc {
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     /** set attribute to FileObject
54      */

55     private void setAttribute(FileObject fo,String JavaDoc name,String JavaDoc value) {
56         try {
57           fo.setAttribute(name, value);
58           log ("attribute (name = " + name + ", value = " + value + ") setted" );
59         } catch (Exception JavaDoc e) {
60             String JavaDoc msg = "failed on set attribute name = " + name + " , value = " + value;
61             log (msg);
62             assertTrue(msg,false);
63         }
64     }
65     /** read attribude from fileobject and tests if is correct
66      */

67     private String JavaDoc getAttribute(FileObject fo,String JavaDoc name, String JavaDoc refValue) {
68         String JavaDoc value = (String JavaDoc) 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     /** it mounts LocalFileSystem in temorary directory
80      */

81     private void preprocess() throws IOException,PropertyVetoException JavaDoc {
82 // fileSystemFile.mkdir();
83
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 JavaDoc 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     /** unmount temporary filesystem
107      **/

108     private void postprocess() {
109         Repository.getDefault().getDefault().removeFileSystem(fileSystem);
110         fileSystemDir.deleteOnExit();
111       // fileSystemFile.deleteOnExit();
112
}
113         
114     private FileObject getAnyFileObject() {
115         return fileSystem.getRoot();
116     }
117    /**This suite*/
118     public static Test suite() {
119         NbTestSuite suite = new NbTestSuite(AttrTest.class);
120         return suite;
121     }
122     
123     /** test set "\\" attr value, see to : 8977 in Issuezila
124      */

125     public void testSetBackslashValue() throws IOException, PropertyVetoException JavaDoc {
126         preprocess();
127         FileObject fo = getAnyFileObject();
128         try {
129              setAttribute(fo, "\\", "2");
130              getAttribute(fo, "\\", "2");
131         } catch(Exception JavaDoc e) {
132             assertTrue(" failed:no attribute setted " + e,false );
133         }
134             
135        
136         postprocess();
137     }
138     
139     
140       /**
141      * @param args the command line arguments
142      */

143     public static void main(String JavaDoc[] args) {
144         junit.textui.TestRunner.run(suite());
145     }
146     
147     
148 }
149
Popular Tags