KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > httpserver > URLMapperTest


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.netbeans.modules.httpserver;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.HashSet JavaDoc;
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 /** Test for HttpServerURLMapper.
35  *
36  * @author Radim Kubacki, Petr Jiricka
37  */

38 public class URLMapperTest extends NbTestCase {
39     
40     /** constructor required by JUnit
41      * @param testName method name to be used as testcase
42      */

43     public URLMapperTest(String JavaDoc testName) {
44         super (testName);
45     }
46     
47     /** method used for explicit testsuite definition
48      */

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 JavaDoc {
56     }
57     
58     private FileObject getTestFSRoot() {
59         log("Data dir: " + getDataDir());
60         return FileUtil.toFileObject(getDataDir());
61     }
62     
63     public void testFSRoot() throws Exception JavaDoc {
64         assertNotNull("Test FS Root is null", getTestFSRoot());
65     }
66     
67     /** simple test case
68      */

69     public void testFileURLMapping() throws Exception JavaDoc {
70         FileObject fo = getTestFSRoot().getFileObject("org/netbeans/test/httpserver/testResource.txt");
71
72         URLMapper mapper = getMapper();
73         URL JavaDoc url = mapper.getURL(fo, URLMapper.NETWORK);
74         checkFileObjectURLMapping(fo, url, getMapper());
75     }
76     
77     public void testEmptyFileURLMapping() throws Exception JavaDoc {
78         FileObject fo = getTestFSRoot().getFileObject("org/netbeans/test/httpserver/empty");
79         
80         URLMapper mapper = getMapper();
81         URL JavaDoc url = mapper.getURL(fo, URLMapper.NETWORK);
82         checkFileObjectURLMapping(fo, url, getMapper());
83     }
84     
85     public void testDirURLMapping() throws Exception JavaDoc {
86         FileObject fo = getTestFSRoot().getFileObject("org/netbeans/test/httpserver");
87         
88         URLMapper mapper = getMapper();
89         URL JavaDoc url = mapper.getURL(fo, URLMapper.NETWORK);
90         checkFileObjectURLMapping(fo, url, getMapper());
91     }
92     
93     public void testFileWithSpacesURLMapping() throws Exception JavaDoc {
94         FileObject fo = getTestFSRoot().getFileObject("org/netbeans/test/httpserver/dir with spaces/file with spaces.txt");
95         
96         URLMapper mapper = getMapper();
97         URL JavaDoc url = mapper.getURL(fo, URLMapper.NETWORK);
98         if (url != null) {
99             // the case that the URL is null will be caught anyhow later
100
if (url.toExternalForm().indexOf(' ') != -1) {
101                 fail("External URL contains spaces: " + url);
102             }
103         }
104         checkFileObjectURLMapping(fo, url, getMapper());
105     }
106
107     /**
108      * with MasterFS this one does not work - should we expect it to work?
109     public void testFSRootURLMapping() throws Exception {
110         FileObject fo = getTestFSRoot().getFileObject("org/netbeans/test/httpserver/testResource.txt").getFileSystem().getRoot();
111         
112         URLMapper mapper = getMapper();
113         URL url = mapper.getURL(fo, URLMapper.NETWORK);
114         checkFileObjectURLMapping(fo, url, getMapper());
115     }
116      */

117     
118     public void testPageWithAnchorMapping() throws Exception JavaDoc {
119         FileObject fo = getTestFSRoot().getFileObject("org/netbeans/test/httpserver/Page.html");
120         
121         URLMapper mapper = getMapper();
122         URL JavaDoc url = mapper.getURL(fo, URLMapper.NETWORK);
123         if (url != null) {
124             // the case that the URL is null will be caught anyhow later
125
url = new URL JavaDoc (url, "#A(a, b, c)");
126         }
127         checkFileObjectURLMapping(fo, url, getMapper());
128     }
129     
130     public void testInternalMapping() throws Exception JavaDoc {
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 JavaDoc url = mapper.getURL(fo, URLMapper.INTERNAL);
136         // our mapper does not provide mapping for these
137
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 JavaDoc url, URLMapper mapper) throws Exception JavaDoc {
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         // compare the streams
157
URL JavaDoc u2 = fo.getURL();
158         compareStream(url.openStream(), u2.openStream());
159     }
160     
161     /** Compares content of two streams.
162      */

163     private static void compareStream (InputStream JavaDoc i1, InputStream JavaDoc i2) throws Exception JavaDoc {
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