KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import java.io.*;
24 import java.net.URL JavaDoc;
25 import java.net.JarURLConnection JavaDoc;
26 import java.net.URLConnection JavaDoc;
27 import java.lang.Exception JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.ArrayList JavaDoc;
32
33 public class URLMapperTestHidden extends TestBaseHid {
34     private FileObject root = null;
35
36     protected String JavaDoc[] getResources (String JavaDoc testName) {
37         return new String JavaDoc [] {
38             "mynormaldir/mynormalfile.txt",
39             "my ugly dir/my ugly file.txt"
40         };
41     }
42
43     protected void setUp() throws Exception JavaDoc {
44         super.setUp();
45         Repository.getDefault().addFileSystem(testedFS);
46         root = testedFS.findResource(getResourcePrefix());
47     }
48
49     protected void tearDown() throws Exception JavaDoc {
50         Repository.getDefault().removeFileSystem(testedFS);
51         super.tearDown();
52     }
53     
54     /** Creates new FileObjectTestHidden */
55     public URLMapperTestHidden(String JavaDoc name) {
56         super(name);
57     }
58
59     public void testIfReachable () throws Exception JavaDoc {
60         assertNotNull(root);
61         implTestIfReachable(root);
62                 
63         Enumeration JavaDoc en = root.getChildren(true);
64         while (en.hasMoreElements()) {
65             FileObject fileObject = (FileObject) en.nextElement();
66             implTestIfReachable(fileObject);
67         }
68
69     }
70     
71     public void testConversions () throws Exception JavaDoc {
72         assertNotNull(root);
73         implTestConversions(root);
74                 
75
76         Enumeration JavaDoc en = root.getChildren(true);
77         while (en.hasMoreElements()) {
78             FileObject fileObject = (FileObject) en.nextElement();
79             implTestConversions(fileObject);
80         }
81
82     }
83     
84     public void testForSlashes () throws Exception JavaDoc {
85         assertNotNull(root);
86         implTestForSlashes(root);
87                 
88         Enumeration JavaDoc en = root.getChildren(true);
89         while (en.hasMoreElements()) {
90             FileObject fileObject = (FileObject) en.nextElement();
91             implTestForSlashes(fileObject);
92         }
93     }
94
95     public void testForSlashes2 () throws Exception JavaDoc {
96         assertNotNull(root);
97         if (testedFS.isReadOnly() || root.isReadOnly()) return;
98                 
99         List JavaDoc testedFileObjects = new ArrayList JavaDoc();
100         Enumeration JavaDoc en = root.getChildren(true);
101         while (en.hasMoreElements()) {
102             FileObject fileObject = (FileObject) en.nextElement();
103             testedFileObjects.add(fileObject);
104             implTestForSlashes(fileObject);
105         }
106         
107         FileObject[] directChildren = root.getChildren();
108         for (int i = 0; i < directChildren.length; i++) {
109             FileObject fo = directChildren[i];
110             fo.delete();
111         }
112
113         for (int i = 0; i < testedFileObjects.size(); i++) {
114             FileObject fo = (FileObject) testedFileObjects.get(i);
115             assertFalse (fo.isValid());
116             implTestForSlashes(fo);
117         }
118     }
119     
120     public void testForSpaces () throws Exception JavaDoc {
121         assertNotNull(root);
122         implTestForSpaces(root);
123                 
124         Enumeration JavaDoc en = root.getChildren(true);
125         while (en.hasMoreElements()) {
126             FileObject fileObject = (FileObject) en.nextElement();
127             implTestForSpaces(fileObject);
128         }
129     }
130     
131
132     private void implTestIfReachable(FileObject fo) throws Exception JavaDoc {
133         URL JavaDoc urlFromMapper = URLMapper.findURL(fo, getURLType());
134         if (isNullURLExpected(urlFromMapper, fo)) return;
135         
136         assertNotNull(urlFromMapper);
137         URLConnection JavaDoc fc = urlFromMapper.openConnection();
138         
139         
140         if (fc instanceof JarURLConnection JavaDoc && fo.isFolder()) return;
141         InputStream ic = fc.getInputStream();
142         try {
143             assertNotNull(ic);
144         } finally {
145             if (ic != null) ic.close();
146         }
147     }
148
149     private boolean isNullURLExpected(URL JavaDoc urlFromMapper, FileObject fo) {
150         boolean isNullExpected = false;
151         if (urlFromMapper == null && getURLType() == URLMapper.EXTERNAL) {
152             if (testedFS instanceof XMLFileSystem) {
153                 isNullExpected = true;
154             } else if (testedFS instanceof MultiFileSystem && FileUtil.toFile(fo) == null) {
155                 isNullExpected = true;
156             }
157         }
158         return isNullExpected;
159     }
160
161     private void implTestConversions (FileObject fo) {
162         URL JavaDoc urlFromMapper = URLMapper.findURL(fo, getURLType());
163         if (isNullURLExpected(urlFromMapper, fo)) return;
164         
165         assertNotNull(urlFromMapper);
166
167         FileObject[] all = URLMapper.findFileObjects(urlFromMapper);
168         List JavaDoc/*<FileObject>*/ allList = Arrays.asList(all);
169         assertTrue("found " + fo + " in " + allList + " from " + urlFromMapper, allList.contains(fo));
170         assertEquals("findFileObject works too", fo, URLMapper.findFileObject(urlFromMapper));
171     }
172
173     
174     protected int getURLType() {
175         return URLMapper.EXTERNAL;
176     }
177
178     private void implTestForSlashes(FileObject fo) throws Exception JavaDoc{
179         URL JavaDoc urlFromMapper = URLMapper.findURL(fo, getURLType());
180         if (isNullURLExpected(urlFromMapper, fo)) return;
181         
182         assertNotNull(fo.getPath() + " from: " + fo.getFileSystem().toString(), urlFromMapper);
183         String JavaDoc urlString = urlFromMapper.toExternalForm();
184
185
186         /*test for last slash*/
187         boolean isSlasLastIndex = (urlString.lastIndexOf('/') == (urlString.length()-1));
188         assertTrue(urlString + ": last slash on unexpected position",(fo.isFolder()) ? isSlasLastIndex : !isSlasLastIndex);
189     }
190
191     private void implTestForSpaces(FileObject fo) {
192         URL JavaDoc urlFromMapper = URLMapper.findURL(fo, getURLType());
193         if (isNullURLExpected(urlFromMapper, fo)) return;
194         
195         assertNotNull(urlFromMapper);
196         String JavaDoc urlString = urlFromMapper.toExternalForm();
197
198         /*test for no spaces*/
199         assertEquals(urlString + ": unexpected spaces",-1, urlFromMapper.toExternalForm().indexOf(' ') );
200     }
201 }
202
Popular Tags