1 16 package org.apache.commons.vfs.provider.test; 17 18 import org.apache.commons.AbstractVfsTestCase; 19 import org.apache.commons.vfs.FileSystemException; 20 import org.apache.commons.vfs.provider.GenericFileName; 21 import org.apache.commons.vfs.provider.URLFileNameParser; 22 23 28 public class GenericFileNameTestCase 29 extends AbstractVfsTestCase 30 { 31 34 public void testParseUri() throws Exception 35 { 36 URLFileNameParser urlParser = new URLFileNameParser(21); 37 GenericFileName name = (GenericFileName) urlParser.parseUri(null, null, "ftp://hostname/file"); 39 assertEquals("ftp", name.getScheme()); 40 assertNull(name.getUserName()); 41 assertNull(name.getPassword()); 42 assertEquals("hostname", name.getHostName()); 43 assertEquals(21, name.getPort()); 44 assertEquals(name.getDefaultPort(), name.getPort()); 45 assertEquals("/file", name.getPath()); 46 assertEquals("ftp://hostname/", name.getRootURI()); 47 assertEquals("ftp://hostname/file", name.getURI()); 48 49 name = (GenericFileName) urlParser.parseUri(null, null, "ftp://hostname:9090/file"); 51 assertEquals("ftp", name.getScheme()); 52 assertNull(name.getUserName()); 53 assertNull(name.getPassword()); 54 assertEquals("hostname", name.getHostName()); 55 assertEquals(9090, name.getPort()); 56 assertEquals("/file", name.getPath()); 57 assertEquals("ftp://hostname:9090/", name.getRootURI()); 58 assertEquals("ftp://hostname:9090/file", name.getURI()); 59 60 name = (GenericFileName) urlParser.parseUri(null, null, "ftp://hostname"); 62 assertEquals("ftp", name.getScheme()); 63 assertNull(name.getUserName()); 64 assertNull(name.getPassword()); 65 assertEquals("hostname", name.getHostName()); 66 assertEquals(21, name.getPort()); 67 assertEquals("/", name.getPath()); 68 assertEquals("ftp://hostname/", name.getRootURI()); 69 assertEquals("ftp://hostname/", name.getURI()); 70 71 name = (GenericFileName) urlParser.parseUri(null, null, "ftp://user@hostname/file"); 73 assertEquals("ftp", name.getScheme()); 74 assertEquals("user", name.getUserName()); 75 assertNull(name.getPassword()); 76 assertEquals("hostname", name.getHostName()); 77 assertEquals(21, name.getPort()); 78 assertEquals("/file", name.getPath()); 79 assertEquals("ftp://user@hostname/", name.getRootURI()); 80 assertEquals("ftp://user@hostname/file", name.getURI()); 81 82 name = (GenericFileName) urlParser.parseUri(null, null, "ftp://user:password@hostname/file"); 84 assertEquals("ftp", name.getScheme()); 85 assertEquals("user", name.getUserName()); 86 assertEquals("password", name.getPassword()); 87 assertEquals("hostname", name.getHostName()); 88 assertEquals(21, name.getPort()); 89 assertEquals("/file", name.getPath()); 90 assertEquals("ftp://user:password@hostname/", name.getRootURI()); 91 assertEquals("ftp://user:password@hostname/file", name.getURI()); 92 93 name = (GenericFileName) urlParser.parseUri(null, null, "ftp://%75ser%3A:%40@hostname"); 95 assertEquals("ftp", name.getScheme()); 96 assertEquals("user:", name.getUserName()); 97 assertEquals("@", name.getPassword()); 98 assertEquals("hostname", name.getHostName()); 99 assertEquals(21, name.getPort()); 100 assertEquals("/", name.getPath()); 101 assertEquals("ftp://user%3a:%40@hostname/", name.getRootURI()); 102 assertEquals("ftp://user%3a:%40@hostname/", name.getURI()); 103 } 104 105 108 public void testBadlyFormedUri() throws Exception 109 { 110 testBadlyFormedUri("ftp:", "vfs.provider/missing-double-slashes.error"); 112 testBadlyFormedUri("ftp:/", "vfs.provider/missing-double-slashes.error"); 113 testBadlyFormedUri("ftp:a", "vfs.provider/missing-double-slashes.error"); 114 115 testBadlyFormedUri("ftp://", "vfs.provider/missing-hostname.error"); 117 testBadlyFormedUri("ftp://:21/file", "vfs.provider/missing-hostname.error"); 118 testBadlyFormedUri("ftp:///file", "vfs.provider/missing-hostname.error"); 119 120 testBadlyFormedUri("ftp://host:", "vfs.provider/missing-port.error"); 122 testBadlyFormedUri("ftp://host:/file", "vfs.provider/missing-port.error"); 123 testBadlyFormedUri("ftp://host:port/file", "vfs.provider/missing-port.error"); 124 125 testBadlyFormedUri("ftp://host:90a", "vfs.provider/missing-hostname-path-sep.error"); 127 testBadlyFormedUri("ftp://host?a", "vfs.provider/missing-hostname-path-sep.error"); 128 } 129 130 133 private void testBadlyFormedUri(final String uri, final String errorMsg) 134 { 135 try 136 { 137 new URLFileNameParser(80).parseUri(null, null, uri); 138 fail(); 139 } 140 catch (final FileSystemException e) 141 { 142 assertSameMessage(errorMsg, uri, e); 143 } 144 } 145 } 146 | Popular Tags |