1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package javax.mail;
19
20 import java.net.MalformedURLException;
21 import java.net.URL;
22
23 import junit.framework.TestCase;
24
25 /**
26 * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
27 */
28 public class URLNameTest extends TestCase {
29 public URLNameTest(String name) {
30 super(name);
31 }
32
33 public void testURLNameString() {
34 String s;
35 URLName name;
36
37 s = "http://www.apache.org";
38 name = new URLName(s);
39 assertEquals(s, name.toString());
40 assertEquals("http", name.getProtocol());
41 assertEquals("www.apache.org", name.getHost());
42 assertEquals(-1, name.getPort());
43 assertNull(name.getFile());
44 assertNull(name.getRef());
45 assertNull(name.getUsername());
46 assertNull(name.getPassword());
47 try {
48 assertEquals(new URL(s), name.getURL());
49 } catch (MalformedURLException e) {
50 fail();
51 }
52
53 s = "http://www.apache.org/file/file1#ref";
54 name = new URLName(s);
55 assertEquals(s, name.toString());
56 assertEquals("http", name.getProtocol());
57 assertEquals("www.apache.org", name.getHost());
58 assertEquals(-1, name.getPort());
59 assertEquals("/file/file1", name.getFile());
60 assertEquals("ref", name.getRef());
61 assertNull(name.getUsername());
62 assertNull(name.getPassword());
63 try {
64 assertEquals(new URL(s), name.getURL());
65 } catch (MalformedURLException e) {
66 fail();
67 }
68
69 s = "http://www.apache.org/file/";
70 name = new URLName(s);
71 assertEquals(s, name.toString());
72 assertEquals("http", name.getProtocol());
73 assertEquals("www.apache.org", name.getHost());
74 assertEquals(-1, name.getPort());
75 assertEquals("/file/", name.getFile());
76 assertNull(name.getRef());
77 assertNull(name.getUsername());
78 assertNull(name.getPassword());
79 try {
80 assertEquals(new URL(s), name.getURL());
81 } catch (MalformedURLException e) {
82 fail();
83 }
84
85 s = "http://john@www.apache.org/file/";
86 name = new URLName(s);
87 assertEquals(s, name.toString());
88 assertEquals("http", name.getProtocol());
89 assertEquals("www.apache.org", name.getHost());
90 assertEquals(-1, name.getPort());
91 assertEquals("/file/", name.getFile());
92 assertNull(name.getRef());
93 assertEquals("john", name.getUsername());
94 assertNull(name.getPassword());
95 try {
96 assertEquals(new URL(s), name.getURL());
97 } catch (MalformedURLException e) {
98 fail();
99 }
100
101 s = "http://john:doe@www.apache.org/file/";
102 name = new URLName(s);
103 assertEquals(s, name.toString());
104 assertEquals("http", name.getProtocol());
105 assertEquals("www.apache.org", name.getHost());
106 assertEquals(-1, name.getPort());
107 assertEquals("/file/", name.getFile());
108 assertNull(name.getRef());
109 assertEquals("john", name.getUsername());
110 assertEquals("doe", name.getPassword());
111 try {
112 assertEquals(new URL(s), name.getURL());
113 } catch (MalformedURLException e) {
114 fail();
115 }
116
117 s = "file/file2";
118 name = new URLName(s);
119 assertNull(name.getProtocol());
120 assertNull(name.getHost());
121 assertEquals(-1, name.getPort());
122 assertEquals("file/file2", name.getFile());
123 assertNull(name.getRef());
124 assertNull(name.getUsername());
125 assertNull(name.getPassword());
126 try {
127 name.getURL();
128 fail();
129 } catch (MalformedURLException e) {
130 // OK
131 }
132
133 name = new URLName((String) null);
134 assertNull( name.getProtocol());
135 assertNull(name.getHost());
136 assertEquals(-1, name.getPort());
137 assertNull(name.getFile());
138 assertNull(name.getRef());
139 assertNull(name.getUsername());
140 assertNull(name.getPassword());
141 try {
142 name.getURL();
143 fail();
144 } catch (MalformedURLException e) {
145 // OK
146 }
147
148 name = new URLName("");
149 assertNull( name.getProtocol());
150 assertNull(name.getHost());
151 assertEquals(-1, name.getPort());
152 assertNull(name.getFile());
153 assertNull(name.getRef());
154 assertNull(name.getUsername());
155 assertNull(name.getPassword());
156 try {
157 name.getURL();
158 fail();
159 } catch (MalformedURLException e) {
160 // OK
161 }
162 }
163
164 public void testURLNameAll() {
165 URLName name;
166 name = new URLName(null, null, -1, null, null, null);
167 assertNull(name.getProtocol());
168 assertNull(name.getHost());
169 assertEquals(-1, name.getPort());
170 assertNull(name.getFile());
171 assertNull(name.getRef());
172 assertNull(name.getUsername());
173 assertNull(name.getPassword());
174 try {
175 name.getURL();
176 fail();
177 } catch (MalformedURLException e) {
178 // OK
179 }
180
181 name = new URLName("", "", -1, "", "", "");
182 assertNull(name.getProtocol());
183 assertNull(name.getHost());
184 assertEquals(-1, name.getPort());
185 assertNull(name.getFile());
186 assertNull(name.getRef());
187 assertNull(name.getUsername());
188 assertNull(name.getPassword());
189 try {
190 name.getURL();
191 fail();
192 } catch (MalformedURLException e) {
193 // OK
194 }
195
196 name = new URLName("http", "www.apache.org", -1, null, null, null);
197 assertEquals("http://www.apache.org", name.toString());
198 assertEquals("http", name.getProtocol());
199 assertEquals("www.apache.org", name.getHost());
200 assertEquals(-1, name.getPort());
201 assertNull(name.getFile());
202 assertNull(name.getRef());
203 assertNull(name.getUsername());
204 assertNull(name.getPassword());
205 try {
206 assertEquals(new URL("http://www.apache.org"), name.getURL());
207 } catch (MalformedURLException e) {
208 fail();
209 }
210
211 name = new URLName("http", "www.apache.org", 8080, "", "", "");
212 assertEquals("http://www.apache.org:8080", name.toString());
213 assertEquals("http", name.getProtocol());
214 assertEquals("www.apache.org", name.getHost());
215 assertEquals(8080, name.getPort());
216 assertNull(name.getFile());
217 assertNull(name.getRef());
218 assertNull(name.getUsername());
219 assertNull(name.getPassword());
220 try {
221 assertEquals(new URL("http://www.apache.org:8080"), name.getURL());
222 } catch (MalformedURLException e) {
223 fail();
224 }
225
226 name = new URLName("http", "www.apache.org", -1, "/file/file2", "", "");
227 assertEquals("http://www.apache.org/file/file2", name.toString());
228 assertEquals("http", name.getProtocol());
229 assertEquals("www.apache.org", name.getHost());
230 assertEquals(-1, name.getPort());
231 assertEquals("/file/file2", name.getFile());
232 assertNull(name.getRef());
233 assertNull(name.getUsername());
234 assertNull(name.getPassword());
235 try {
236 assertEquals(new URL("http://www.apache.org/file/file2"), name.getURL());
237 } catch (MalformedURLException e) {
238 fail();
239 }
240
241 name = new URLName("http", "www.apache.org", -1, "/file/file2", "john", "");
242 assertEquals("http://john@www.apache.org/file/file2", name.toString());
243 assertEquals("http", name.getProtocol());
244 assertEquals("www.apache.org", name.getHost());
245 assertEquals(-1, name.getPort());
246 assertEquals("/file/file2", name.getFile());
247 assertNull(name.getRef());
248 assertEquals("john", name.getUsername());
249 assertNull(name.getPassword());
250 try {
251 assertEquals(new URL("http://john@www.apache.org/file/file2"), name.getURL());
252 } catch (MalformedURLException e) {
253 fail();
254 }
255
256 name = new URLName("http", "www.apache.org", -1, "/file/file2", "john", "doe");
257 assertEquals("http://john:doe@www.apache.org/file/file2", name.toString());
258 assertEquals("http", name.getProtocol());
259 assertEquals("www.apache.org", name.getHost());
260 assertEquals(-1, name.getPort());
261 assertEquals("/file/file2", name.getFile());
262 assertNull(name.getRef());
263 assertEquals("john", name.getUsername());
264 assertEquals("doe", name.getPassword());
265 try {
266 assertEquals(new URL("http://john:doe@www.apache.org/file/file2"), name.getURL());
267 } catch (MalformedURLException e) {
268 fail();
269 }
270
271 name = new URLName("http", "www.apache.org", -1, "/file/file2", "", "doe");
272 assertEquals("http://www.apache.org/file/file2", name.toString());
273 assertEquals("http", name.getProtocol());
274 assertEquals("www.apache.org", name.getHost());
275 assertEquals(-1, name.getPort());
276 assertEquals("/file/file2", name.getFile());
277 assertNull(name.getRef());
278 assertNull(name.getUsername());
279 assertNull(name.getPassword());
280 try {
281 assertEquals(new URL("http://www.apache.org/file/file2"), name.getURL());
282 } catch (MalformedURLException e) {
283 fail();
284 }
285 }
286
287 public void testURLNameURL() throws MalformedURLException {
288 URL url;
289 URLName name;
290
291 url = new URL("http://www.apache.org");
292 name = new URLName(url);
293 assertEquals("http", name.getProtocol());
294 assertEquals("www.apache.org", name.getHost());
295 assertEquals(-1, name.getPort());
296 assertNull(name.getFile());
297 assertNull(name.getRef());
298 assertNull(name.getUsername());
299 assertNull(name.getPassword());
300 try {
301 assertEquals(url, name.getURL());
302 } catch (MalformedURLException e) {
303 fail();
304 }
305 }
306
307 public void testEquals() throws MalformedURLException {
308 URLName name1 = new URLName("http://www.apache.org");
309 assertEquals(name1, new URLName("http://www.apache.org"));
310 assertEquals(name1, new URLName(new URL("http://www.apache.org")));
311 assertEquals(name1, new URLName("http", "www.apache.org", -1, null, null, null));
312 assertEquals(name1, new URLName("http://www.apache.org#foo")); // wierd but ref is not part of the equals contract
313 assertTrue(!name1.equals(new URLName("http://www.apache.org:8080")));
314 assertTrue(!name1.equals(new URLName("http://cvs.apache.org")));
315 assertTrue(!name1.equals(new URLName("https://www.apache.org")));
316
317 name1 = new URLName("http://john:doe@www.apache.org");
318 assertEquals(name1, new URLName(new URL("http://john:doe@www.apache.org")));
319 assertEquals(name1, new URLName("http", "www.apache.org", -1, null, "john", "doe"));
320 assertTrue(!name1.equals(new URLName("http://john:xxx@www.apache.org")));
321 assertTrue(!name1.equals(new URLName("http://xxx:doe@www.apache.org")));
322 assertTrue(!name1.equals(new URLName("http://www.apache.org")));
323
324 assertEquals(new URLName("http://john@www.apache.org"), new URLName("http", "www.apache.org", -1, null, "john", null));
325 assertEquals(new URLName("http://www.apache.org"), new URLName("http", "www.apache.org", -1, null, null, "doe"));
326 }
327
328 public void testHashCode() {
329 URLName name1 = new URLName("http://www.apache.org/file");
330 URLName name2 = new URLName("http://www.apache.org/file#ref");
331 assertTrue(name1.equals(name2));
332 assertTrue(name1.hashCode() == name2.hashCode());
333 }
334
335 public void testNullProtocol() {
336 URLName name1 = new URLName(null, "www.apache.org", -1, null, null, null);
337 assertTrue(!name1.equals(name1));
338 }
339
340 public void testOpaqueSchemes() {
341 String s;
342 URLName name;
343
344 // not strictly opaque but no protocol handler installed
345 s = "foo://jdoe@apache.org/INBOX";
346 name = new URLName(s);
347 assertEquals(s, name.toString());
348 assertEquals("foo", name.getProtocol());
349 assertEquals("apache.org", name.getHost());
350 assertEquals(-1, name.getPort());
351 assertEquals("/INBOX", name.getFile());
352 assertNull(name.getRef());
353 assertEquals("jdoe", name.getUsername());
354 assertNull(name.getPassword());
355
356 // TBD as I am not sure what other URL formats to use
357 }
358 }