1 16 package org.apache.commons.vfs.test; 17 18 import org.apache.commons.vfs.Capability; 19 import org.apache.commons.vfs.FileObject; 20 21 import java.io.IOException ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.net.URLConnection ; 25 26 31 public class UrlTests 32 extends AbstractProviderTestCase 33 { 34 42 protected Capability[] getRequiredCaps() 43 { 44 return new Capability[]{Capability.URI}; 45 } 46 47 50 public void testURL() throws Exception 51 { 52 final FileObject file = getReadFolder().resolveFile("some-dir/"); 53 final URL url = file.getURL(); 54 55 assertEquals(file.getName().getURI(), url.toExternalForm()); 56 57 final URL parentURL; 58 try 59 { 60 parentURL = new URL (url, ".."); 61 } 62 catch (MalformedURLException e) 63 { 64 throw e; 65 } 66 assertEquals(file.getParent().getURL(), parentURL); 67 68 final URL rootURL = new URL (url, "/"); 69 assertEquals(file.getFileSystem().getRoot().getURL(), rootURL); 70 } 71 72 75 public void testURLContent() throws Exception 76 { 77 FileObject file = getReadFolder().resolveFile("file1.txt"); 79 assertTrue(file.exists()); 80 81 URLConnection urlCon = file.getURL().openConnection(); 82 assertSameURLContent(FILE1_CONTENT, urlCon); 83 84 file = getReadFolder().resolveFile("empty.txt"); 86 assertTrue(file.exists()); 87 88 urlCon = file.getURL().openConnection(); 89 assertSameURLContent("", urlCon); 90 } 91 92 95 public void testUnknownURL() throws Exception 96 { 97 final FileObject unknownFile = getReadFolder().resolveFile("unknown-file"); 99 assertFalse(unknownFile.exists()); 100 101 final URLConnection connection = unknownFile.getURL().openConnection(); 102 try 103 { 104 connection.getInputStream(); 105 fail(); 106 } 107 catch (final IOException e) 108 { 109 assertSameMessage("vfs.provider/read-not-file.error", unknownFile, e); 110 } 111 assertEquals(-1, connection.getContentLength()); 112 } 113 114 } 115 | Popular Tags |