KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > AttributesTestHidden


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

19
20 package org.openide.filesystems;
21
22 import junit.framework.*;
23 //import org.openide.filesystems.hidden.*;
24

25 import java.util.*;
26 import java.io.*;
27 import java.net.URL JavaDoc;
28
29 /**
30  *
31  * @author vs124454
32  * @version
33  *
34  */

35 public class AttributesTestHidden extends TestBaseHid {
36     static final String JavaDoc TEST_ERR = "Not the tested code, but this test contains errors";
37     static final String JavaDoc COMMON_ATTR_STRING = "testAttribute";
38
39     private static String JavaDoc[] resources = new String JavaDoc [] {
40         "/compatibility/filesystem.attributes.backup",//0
41
"/compatibility/Attr.test",//1
42
};
43     
44     //private boolean logIt = false;
45

46     protected String JavaDoc[] getResources (String JavaDoc testName) {
47         return resources;
48     }
49     
50     /** Creates new FileObjectTestHidden */
51     public AttributesTestHidden(String JavaDoc name) {
52         super(name);
53     }
54
55     /**
56      * setAttribute did not write all changes to .nbattrs and after gc() WeakReference
57      * was lost and from .nbattrs was read some old value.
58      * Problem better described in IssueZilla.
59      * BugFix: #10637
60      * Probably problem:
61      */

62     @SuppressWarnings JavaDoc("unchecked")
63     public void testAttribute01() throws IOException {
64         assertTrue (TEST_ERR,testedFS != null);
65         if (testedFS.isReadOnly ()) return;
66         
67         FileObject foTested = testedFS.getRoot ();
68         List<String JavaDoc> list = new LinkedList<String JavaDoc> ();
69         
70         
71         int i = 0;
72         for (i = 0; i < 20; i++) {
73             list.add(COMMON_ATTR_STRING);
74             foTested.setAttribute (COMMON_ATTR_STRING, list);
75         }
76         
77         System.gc ();System.gc ();
78         try {
79             Thread.sleep(300);
80         } catch (InterruptedException JavaDoc iex){}
81         list = (List)foTested.getAttribute (COMMON_ATTR_STRING);
82         assertTrue ("Caching problem in attributes: "+list.size(),list.size() == i);
83     }
84     
85     /** NOI18N specific test. BugFix: #11076, #11127
86      * Probably problem:
87      */

88     public void testAttribute02 () throws IOException {
89         assertTrue (TEST_ERR,testedFS != null);
90         if (testedFS.isReadOnly ()) return;
91         
92         Locale locale = Locale.getDefault();// returns current value
93

94         if (locale != Locale.JAPAN || locale != Locale.JAPANESE)
95             return;
96         
97         /** I`m not sure how japan unicode chars looks like. For present time
98          * I choose some chars > 0xFF */

99         String JavaDoc japanFileName = new String JavaDoc (new char[] {'\u025A','\u012F','\u025A','\u012F'});
100         FileObject foTested = testedFS.getRoot ();
101         FileObject folder = foTested.getFileObject (japanFileName,null);
102         
103         if (folder == null) {
104             try {
105                 folder = foTested.createFolder (japanFileName);
106                 if (folder == null) return;
107             } catch (IOException iox) {
108                 return;
109             }
110         }
111         
112         folder.setAttribute (COMMON_ATTR_STRING,COMMON_ATTR_STRING);
113         String JavaDoc result = (String JavaDoc)folder.getAttribute (COMMON_ATTR_STRING);
114         assertTrue ("Coding problem by writing and reading attributes - .nbattrs",result != null);
115     }
116
117     /** In XML not permited chars should be properly encoded and decode
118      * BugFix: #?
119      * Probably problem:
120      */

121     public void testAttribute03 () throws IOException {
122         assertTrue (TEST_ERR,testedFS != null);
123         if (testedFS.isReadOnly ()) return;
124
125         /** Probably later extend set of chars that should be tested*/
126         String JavaDoc testedStr = new String JavaDoc (new char[] {'&','<','>','\"','\''});
127         String JavaDoc result;
128         
129         result = basicAttributeTest (testedStr,COMMON_ATTR_STRING);
130         assertTrue ("XML not allowed chars are bad handled - key problem",
131         result != null && result.equals (COMMON_ATTR_STRING));
132         
133         result = basicAttributeTest (COMMON_ATTR_STRING,testedStr);
134         assertTrue ("XML not allowed chars are bad handled - value problem",
135         result != null && result.equals (testedStr));
136     }
137
138     /**
139      * Back compatibility. Transforming from serialized file: filesystem.attributes
140      * BugFix: #?
141      * Probably problem:
142      */

143     public void KtestAttribute04 () throws IOException {
144         assertTrue (TEST_ERR,testedFS != null);
145         if (testedFS.isReadOnly ()) return;
146         
147         /** This test expects that gets testedFS where exists next resorce*/
148         FileObject backup = testedFS.findResource ("/compatibility/filesystem.attributes.backup");
149         assertTrue (TEST_ERR,backup != null);
150         FileLock lock = backup.lock();
151         backup.rename(lock,"filesystem","attributes");
152         lock.releaseLock();
153         FileObject testedFo = testedFS.findResource ("/compatibility/Attr.test");
154         assertTrue (TEST_ERR,testedFo != null);
155         /** Name of attribute testAttr must be kept. Becuase this name is saved in old format in
156          * filesystem.attributes*/

157         assertTrue ("Back compatibility problem",testedFo.getAttribute ("testAttr") != null);
158     }
159
160     /**
161      * filsystem.attributes files should be hidden
162      * BugFix: #?
163      * Probably problem:
164      */

165     public void testAttribute05 () throws IOException {
166         assertTrue (TEST_ERR,testedFS != null);
167         if (testedFS.isReadOnly ()) return;
168         
169         /** This test expects that gets testedFS where exists next resorce*/
170         FileObject testedFo = testedFS.findResource ("/compatibility");
171         assertTrue (TEST_ERR,testedFo != null);
172                 
173         assertTrue ("filesystem.attributes must stay hidden for filesystems",testedFo.getFileObject ("filesystem","attributes") == null);
174     }
175
176     
177     /**
178      * .nbattrs files should be hidden
179      * BugFix: #?
180      * Probably problem:
181      */

182     public void testAttribute06 () throws IOException {
183         assertTrue (TEST_ERR,testedFS != null);
184         if (testedFS.isReadOnly ()) return;
185         
186         basicAttributeTest (COMMON_ATTR_STRING,COMMON_ATTR_STRING);
187         assertTrue (".nbattrs must stay hidden for filesystems",testedFS.getRoot ().getFileObject (".nbattrs") == null);
188     }
189     
190     /**
191      * Null as vaule should delete record for key from attributes
192      * BugFix: #?
193      * Probably problem:
194      */

195     public void testAttribute07 () throws IOException {
196         assertTrue (TEST_ERR,testedFS != null);
197         if (testedFS.isReadOnly ()) return;
198         /**This test can be done only on LocalFileSystem - but it is not important
199          * to run this test on other FileSystems
200          */

201         if (!(testedFS instanceof LocalFileSystem)) return;
202         
203         String JavaDoc testAttr = COMMON_ATTR_STRING+"Null";
204         
205         basicAttributeTest (testAttr,testAttr);
206         basicAttributeTest (COMMON_ATTR_STRING,COMMON_ATTR_STRING);
207         
208         String JavaDoc result = basicAttributeTest (testAttr,null);
209         assertTrue ("After setAttribute (,null) - getAttribute must return null",result == null);
210         
211         String JavaDoc content = testAttr;
212         FileInputStream fis = null;
213         try {
214             File parent = FileUtil.toFile(testedFS.getRoot());
215             fis = new FileInputStream(new File(parent,".nbattrs"));
216             byte[] bytes = new byte [fis.available()];
217             fis.read(bytes);
218             content = new String JavaDoc (bytes);
219         } finally {
220             if (fis != null) fis.close();
221         }
222         assertTrue ("After setAttribute (,null) appropriate key cannot be in .nbattrs",content.indexOf("name=\""+testAttr) == -1 );
223         assertTrue ("There was not found expected key: " + COMMON_ATTR_STRING +" in .nbattrs",content.indexOf("name=\""+COMMON_ATTR_STRING) != -1 );//
224
}
225     
226     
227     public void testAttribute08 () throws Exception JavaDoc {
228       URL JavaDoc fsURLDef = this.getClass().getResource ("data/Attributes.xml");
229       assertTrue ("Cannot create XML FS for testing purposes", fsURLDef != null);
230       FileSystem fs = new XMLFileSystem (fsURLDef);
231       FileObject fo = fs.findResource("testMethodValue");
232       assertTrue ("Cannot acces FileObject named testMethodValue", fo != null);
233
234       String JavaDoc testName = "test1";
235       Object JavaDoc obj = fo.getAttribute(testName);
236       assertTrue ("methodValue failed", obj != null);
237       assertTrue ("methodValue doesn't keep order ",
238       obj.equals(getObjectViaMethodValue1 (fo, testName)));
239       
240       testName = "test2";
241       obj = fo.getAttribute(testName);
242       assertTrue ("methodValue failed", obj != null);
243       assertTrue ("methodValue doesn't keep order ",
244       obj.equals(getObjectViaMethodValue2 (testName, fo)));
245
246       testName = "test3";
247       obj = fo.getAttribute(testName);
248       assertTrue ("methodValue failed", obj != null);
249       assertTrue ("methodValue doesn't keep order ",
250       obj.equals(getObjectViaMethodValue3 (fo)));
251
252       testName = "test4";
253       obj = fo.getAttribute(testName);
254       assertTrue ("methodValue failed", obj != null);
255       assertTrue ("methodValue doesn't keep order ",
256       obj.equals(getObjectViaMethodValue4 (testName)));
257
258       testName = "test5";
259       obj = fo.getAttribute(testName);
260       assertTrue ("methodValue failed", obj != null);
261       assertTrue ("methodValue doesn't keep order ",
262       obj.equals(getObjectViaMethodValue5 ()));
263
264       testName = "test6";
265       obj = fo.getAttribute(testName);
266       assertTrue ("methodValue failed", obj != null);
267       assertEquals("even works for java.util.Map", "Ahoj1", obj);
268
269       testName = "test7";
270       obj = fo.getAttribute(testName);
271       assertTrue ("methodValue failed", obj != null);
272       assertEquals("works for map and string", "Ahoj1test7", obj);
273     }
274
275     public void testAttribute09 () throws IOException {
276         assertTrue (TEST_ERR,testedFS != null);
277         if (testedFS.isReadOnly ()) return;
278         /**This test can be done only on LocalFileSystem - but it is not important
279          * to run this test on other FileSystems
280          */

281         if (!(testedFS instanceof LocalFileSystem)) return;
282         FileObject foTested = testedFS.getRoot ();
283         
284         char[] testChars = new char [] {'A',0x0000,0x008F,'B'};
285         String JavaDoc[] atts = new String JavaDoc [] {"c:\\builds\\u3\\AboutExamples",
286             "c:\\builds\\u3A\\AboutExamples",
287             "c:\\builds\\u33\\AboutExamples",
288             "c:\\builds\\u345\\AboutExamples",
289             "c:\\builds\\u3456\\AboutExamples",
290             "c:\\builds\\u3G85\\AboutExamples",
291             "c:\\builds\\au3456\\AboutExamples",
292             String.valueOf(testChars)
293         };
294
295         for (int i = 0; i < atts.length; i++) {
296             String JavaDoc keyAndValue = atts[i];
297             foTested.setAttribute(keyAndValue, keyAndValue);
298             Object JavaDoc testIt = foTested.getAttribute(keyAndValue);
299             assertEquals(keyAndValue, testIt);
300         }
301     }
302     
303     private static String JavaDoc getObjectViaMethodValue1 (FileObject fo, String JavaDoc testName) {
304         return fo.getPath()+testName;
305     }
306
307     private static String JavaDoc getObjectViaMethodValue1 (String JavaDoc testName, FileObject fo) {
308         return testName+fo.getPath();
309     }
310
311     private static String JavaDoc getObjectViaMethodValue1 (FileObject fo) {
312         return fo.getPath();
313     }
314     
315     private static String JavaDoc getObjectViaMethodValue1 (String JavaDoc testName) {
316         return testName;
317     }
318
319     private static String JavaDoc getObjectViaMethodValue1 () {
320         return "";
321     }
322 ///
323
private static String JavaDoc getObjectViaMethodValue2 (String JavaDoc testName, FileObject fo) {
324         return testName+fo.getPath();
325     }
326
327     private static String JavaDoc getObjectViaMethodValue2 (FileObject fo) {
328         return fo.getPath();
329     }
330     
331     private static String JavaDoc getObjectViaMethodValue2 (String JavaDoc testName) {
332         return testName;
333     }
334
335     private static String JavaDoc getObjectViaMethodValue2 () {
336         return "";
337     }
338 ///
339
private static String JavaDoc getObjectViaMethodValue3 (FileObject fo) {
340         return fo.getPath();
341     }
342
343     private static String JavaDoc getObjectViaMethodValue3 (String JavaDoc testName) {
344         return testName;
345     }
346
347     private static String JavaDoc getObjectViaMethodValue3 () {
348         return "";
349     }
350 ///
351
private static String JavaDoc getObjectViaMethodValue4 (String JavaDoc testName) {
352         return testName;
353     }
354
355     private static String JavaDoc getObjectViaMethodValue4 () {
356         return "";
357     }
358 ///
359
private static String JavaDoc getObjectViaMethodValue5 () {
360         return "";
361     }
362             
363     private static Object JavaDoc getObjectViaMethodValue6 (Map attrs) {
364         try {
365             attrs.keySet().iterator().remove();
366             return "UnsupportedOperationException";
367         } catch (UnsupportedOperationException JavaDoc ex) {
368             // ok
369
}
370         try {
371             attrs.put("value1", "nothing");
372             return "UnsupportedOperationException";
373         } catch (UnsupportedOperationException JavaDoc ex) {
374             // ok
375
}
376         try {
377             attrs.remove("value1");
378             return "UnsupportedOperationException";
379         } catch (UnsupportedOperationException JavaDoc ex) {
380             // ok
381
}
382         
383         
384         return attrs.get("value1");
385     }
386     private static Object JavaDoc getObjectViaMethodValue7 (Map<String JavaDoc,Object JavaDoc> attrs, String JavaDoc attrName) {
387         assertEquals(8, attrs.keySet().size());
388         try {
389             attrs.entrySet().remove(null);
390             return "UnsupportedOperationException";
391         } catch (UnsupportedOperationException JavaDoc ex) {
392             // ok
393
}
394         
395         
396         return attrs.get("value1") + attrName;
397     }
398             
399     private String JavaDoc basicAttributeTest (String JavaDoc attrName, String JavaDoc attrValue) throws IOException {
400         FileObject foTested = testedFS.getRoot ();
401         foTested.setAttribute (attrName, attrValue);
402         return (String JavaDoc)foTested.getAttribute (attrName);
403     }
404     /*public void testFindResource () throws IOException {
405         assertTrue (TEST_ERR,testedFS != null);
406     }*/

407
408 }
409
410
411
412
Popular Tags