1 19 20 package org.openide.filesystems; 21 22 23 import java.io.*; 24 import java.net.URL ; 25 import java.net.JarURLConnection ; 26 import java.net.URLConnection ; 27 import java.lang.Exception ; 28 import java.util.Arrays ; 29 import java.util.Enumeration ; 30 import java.util.List ; 31 import java.util.ArrayList ; 32 33 public class URLMapperTestHidden extends TestBaseHid { 34 private FileObject root = null; 35 36 protected String [] getResources (String testName) { 37 return new String [] { 38 "mynormaldir/mynormalfile.txt", 39 "my ugly dir/my ugly file.txt" 40 }; 41 } 42 43 protected void setUp() throws Exception { 44 super.setUp(); 45 Repository.getDefault().addFileSystem(testedFS); 46 root = testedFS.findResource(getResourcePrefix()); 47 } 48 49 protected void tearDown() throws Exception { 50 Repository.getDefault().removeFileSystem(testedFS); 51 super.tearDown(); 52 } 53 54 55 public URLMapperTestHidden(String name) { 56 super(name); 57 } 58 59 public void testIfReachable () throws Exception { 60 assertNotNull(root); 61 implTestIfReachable(root); 62 63 Enumeration 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 { 72 assertNotNull(root); 73 implTestConversions(root); 74 75 76 Enumeration 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 { 85 assertNotNull(root); 86 implTestForSlashes(root); 87 88 Enumeration 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 { 96 assertNotNull(root); 97 if (testedFS.isReadOnly() || root.isReadOnly()) return; 98 99 List testedFileObjects = new ArrayList (); 100 Enumeration 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 { 121 assertNotNull(root); 122 implTestForSpaces(root); 123 124 Enumeration 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 { 133 URL urlFromMapper = URLMapper.findURL(fo, getURLType()); 134 if (isNullURLExpected(urlFromMapper, fo)) return; 135 136 assertNotNull(urlFromMapper); 137 URLConnection fc = urlFromMapper.openConnection(); 138 139 140 if (fc instanceof JarURLConnection && 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 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 urlFromMapper = URLMapper.findURL(fo, getURLType()); 163 if (isNullURLExpected(urlFromMapper, fo)) return; 164 165 assertNotNull(urlFromMapper); 166 167 FileObject[] all = URLMapper.findFileObjects(urlFromMapper); 168 List 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 { 179 URL urlFromMapper = URLMapper.findURL(fo, getURLType()); 180 if (isNullURLExpected(urlFromMapper, fo)) return; 181 182 assertNotNull(fo.getPath() + " from: " + fo.getFileSystem().toString(), urlFromMapper); 183 String urlString = urlFromMapper.toExternalForm(); 184 185 186 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 urlFromMapper = URLMapper.findURL(fo, getURLType()); 193 if (isNullURLExpected(urlFromMapper, fo)) return; 194 195 assertNotNull(urlFromMapper); 196 String urlString = urlFromMapper.toExternalForm(); 197 198 199 assertEquals(urlString + ": unexpected spaces",-1, urlFromMapper.toExternalForm().indexOf(' ') ); 200 } 201 } 202 | Popular Tags |