1 19 20 package org.openide.util; 21 22 import java.io.*; 23 import java.net.URL ; 24 import java.net.URLConnection ; 25 import org.netbeans.junit.*; 26 import junit.textui.TestRunner; 27 28 32 public class UtilitiesFileURLConvertorTest extends NbTestCase { 33 34 public UtilitiesFileURLConvertorTest(String name) { 35 super(name); 36 } 37 38 public static void main(String [] args) { 39 TestRunner.run(new NbTestSuite(UtilitiesFileURLConvertorTest.class)); 40 } 41 42 public void testFileURLConversion() throws Exception { 43 File f = File.createTempFile("foo", ".txt"); 44 check(f); 45 File tmp = f.getParentFile(); 46 f = new File(tmp, "with a space.txt"); 47 check(f); 48 f = new File(tmp, "strange#characters.txt"); 51 check(f); 52 f = new File(tmp, "stranger?characters.txt"); 53 check(f); 54 } 55 56 private void check(File f) throws Exception { 57 f.deleteOnExit(); 58 URL u = Utilities.toURL(f); 59 assertEquals("correct protocol", "file", u.getProtocol()); 60 System.err.println("URL=" + u); 61 File f2 = Utilities.toFile(u); 63 assertEquals("converts back to same file", f, f2); 64 assertEquals("no dangling references", null, u.getRef()); 65 assertEquals("no dangling queries", null, u.getQuery()); 66 OutputStream os = null; 67 try { 68 os = new FileOutputStream(f); 69 } catch (FileNotFoundException e) { 70 return; 72 } 73 os.write(1); 74 os.write(2); 75 os.write(3); 76 os.write(4); 77 os.write(5); 78 os.close(); 79 URLConnection conn = u.openConnection(); 80 conn.connect(); 81 assertEquals("URL connection works", 5, conn.getContentLength()); 82 } 83 84 } 85 | Popular Tags |