KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > UtilitiesFileURLConvertorTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.util;
21
22 import java.io.*;
23 import java.net.URL JavaDoc;
24 import java.net.URLConnection JavaDoc;
25 import org.netbeans.junit.*;
26 import junit.textui.TestRunner;
27
28 /** Test <code>File</code> &#8596; <code>URL</code> conversion.
29  * @author Jesse Glick
30  * @see "#29711"
31  */

32 public class UtilitiesFileURLConvertorTest extends NbTestCase {
33
34     public UtilitiesFileURLConvertorTest(String JavaDoc name) {
35         super(name);
36     }
37
38     public static void main(String JavaDoc[] args) {
39         TestRunner.run(new NbTestSuite(UtilitiesFileURLConvertorTest.class));
40     }
41     
42     public void testFileURLConversion() throws Exception JavaDoc {
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         // May seem strange but (#27330) hashes in paths are not so unheard-of.
49
// Importantly, some Unix VC system (which?) uses them.
50
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 JavaDoc {
57         f.deleteOnExit();
58         URL JavaDoc u = Utilities.toURL(f);
59         assertEquals("correct protocol", "file", u.getProtocol());
60         System.err.println("URL=" + u);
61         //assertTrue(u.toExternalForm().indexOf(f.getName()) != -1);
62
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             // OK, OS does not permit such a file
71
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 JavaDoc conn = u.openConnection();
80         conn.connect();
81         assertEquals("URL connection works", 5, conn.getContentLength());
82     }
83     
84 }
85
Popular Tags