1 19 20 package org.netbeans.modules.httpserver; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.net.URL ; 25 import java.util.HashSet ; 26 import junit.framework.TestSuite; 27 import org.netbeans.junit.NbTestCase; 28 import org.netbeans.junit.NbTestSuite; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.FileUtil; 31 import org.openide.filesystems.Repository; 32 import org.openide.filesystems.URLMapper; 33 34 38 public class URLMapperTest extends NbTestCase { 39 40 43 public URLMapperTest(String testName) { 44 super (testName); 45 } 46 47 49 public static junit.framework.Test suite() { 50 TestSuite suite = new NbTestSuite(URLMapperTest.class); 51 return suite; 52 } 53 54 55 protected void setUp() throws IOException { 56 } 57 58 private FileObject getTestFSRoot() { 59 log("Data dir: " + getDataDir()); 60 return FileUtil.toFileObject(getDataDir()); 61 } 62 63 public void testFSRoot() throws Exception { 64 assertNotNull("Test FS Root is null", getTestFSRoot()); 65 } 66 67 69 public void testFileURLMapping() throws Exception { 70 FileObject fo = getTestFSRoot().getFileObject("org/netbeans/test/httpserver/testResource.txt"); 71 72 URLMapper mapper = getMapper(); 73 URL url = mapper.getURL(fo, URLMapper.NETWORK); 74 checkFileObjectURLMapping(fo, url, getMapper()); 75 } 76 77 public void testEmptyFileURLMapping() throws Exception { 78 FileObject fo = getTestFSRoot().getFileObject("org/netbeans/test/httpserver/empty"); 79 80 URLMapper mapper = getMapper(); 81 URL url = mapper.getURL(fo, URLMapper.NETWORK); 82 checkFileObjectURLMapping(fo, url, getMapper()); 83 } 84 85 public void testDirURLMapping() throws Exception { 86 FileObject fo = getTestFSRoot().getFileObject("org/netbeans/test/httpserver"); 87 88 URLMapper mapper = getMapper(); 89 URL url = mapper.getURL(fo, URLMapper.NETWORK); 90 checkFileObjectURLMapping(fo, url, getMapper()); 91 } 92 93 public void testFileWithSpacesURLMapping() throws Exception { 94 FileObject fo = getTestFSRoot().getFileObject("org/netbeans/test/httpserver/dir with spaces/file with spaces.txt"); 95 96 URLMapper mapper = getMapper(); 97 URL url = mapper.getURL(fo, URLMapper.NETWORK); 98 if (url != null) { 99 if (url.toExternalForm().indexOf(' ') != -1) { 101 fail("External URL contains spaces: " + url); 102 } 103 } 104 checkFileObjectURLMapping(fo, url, getMapper()); 105 } 106 107 117 118 public void testPageWithAnchorMapping() throws Exception { 119 FileObject fo = getTestFSRoot().getFileObject("org/netbeans/test/httpserver/Page.html"); 120 121 URLMapper mapper = getMapper(); 122 URL url = mapper.getURL(fo, URLMapper.NETWORK); 123 if (url != null) { 124 url = new URL (url, "#A(a, b, c)"); 126 } 127 checkFileObjectURLMapping(fo, url, getMapper()); 128 } 129 130 public void testInternalMapping() throws Exception { 131 FileObject fo = getTestFSRoot().getFileObject("org/netbeans/test/httpserver/testResource.txt"); 132 assertNotNull("File tested is null " + fo, fo); 133 134 URLMapper mapper = getMapper(); 135 URL url = mapper.getURL(fo, URLMapper.INTERNAL); 136 assertNull("Internal mapping for file " + fo + " should be null: " + url, url); 138 } 139 140 141 private URLMapper getMapper() { 142 return new HttpServerURLMapper(); 143 } 144 145 private void checkFileObjectURLMapping(FileObject fo, URL url, URLMapper mapper) throws Exception { 146 log ("Testing " + fo); 147 log (" -> " + url); 148 assertNotNull("The file tested is null.", fo); 149 assertNotNull("Mapper does not produce a URL for file " + fo, url); 150 FileObject newFo[] = mapper.getFileObjects(url); 151 assertNotNull("Mapper does not produce file for URL " + url, newFo); 152 if (newFo.length != 1) { 153 fail("Mapper returned array of size " + newFo.length + " for URL " + url); 154 } 155 assertEquals("Mapping does not produce the original object: " + fo + " != " + newFo, fo, newFo[0]); 156 URL u2 = fo.getURL(); 158 compareStream(url.openStream(), u2.openStream()); 159 } 160 161 163 private static void compareStream (InputStream i1, InputStream i2) throws Exception { 164 for (int i = 0; true; i++) { 165 int c1 = i1.read (); 166 int c2 = i2.read (); 167 168 assertEquals (i + "th bytes are different", c1, c2); 169 170 if (c1 == -1) return; 171 } 172 } 173 } 174 | Popular Tags |