Skip to content

Package: URLName

URLName

nameinstructionbranchcomplexitylinemethod
URLName(String)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
URLName(String, String, int, String, String, String)
M: 66 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 15 C: 0
0%
M: 1 C: 0
0%
URLName(URL)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
_encode(String)
M: 117 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 29 C: 0
0%
M: 1 C: 0
0%
decode(String)
M: 89 C: 0
0%
M: 9 C: 0
0%
M: 6 C: 0
0%
M: 25 C: 0
0%
M: 1 C: 0
0%
encode(String)
M: 28 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
equals(Object)
M: 108 C: 0
0%
M: 36 C: 0
0%
M: 19 C: 0
0%
M: 25 C: 0
0%
M: 1 C: 0
0%
getFile()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getHost()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getHostAddress()
M: 27 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
getPassword()
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getPort()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getProtocol()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getRef()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getURL()
M: 28 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getUsername()
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
hashCode()
M: 75 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 15 C: 0
0%
M: 1 C: 0
0%
indexOfAny(String, String)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
indexOfAny(String, String, int)
M: 23 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
parseString(String)
M: 221 C: 0
0%
M: 26 C: 0
0%
M: 14 C: 0
0%
M: 44 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 62 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%
toString()
M: 109 C: 0
0%
M: 22 C: 0
0%
M: 12 C: 0
0%
M: 27 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package jakarta.mail;
18:
19: import java.io.ByteArrayOutputStream;
20: import java.io.IOException;
21: import java.io.OutputStreamWriter;
22: import java.io.UnsupportedEncodingException;
23: import java.net.InetAddress;
24: import java.net.MalformedURLException;
25: import java.net.URL;
26: import java.net.UnknownHostException;
27: import java.util.BitSet;
28: import java.util.Locale;
29:
30:
31: /**
32: * The name of a URL. This class represents a URL name and also
33: * provides the basic parsing functionality to parse most internet
34: * standard URL schemes. <p>
35: *
36: * Note that this class differs from <code>java.net.URL</code>
37: * in that this class just represents the name of a URL, it does
38: * not model the connection to a URL.
39: *
40: * @author Christopher Cotton
41: * @author Bill Shannon
42: */
43:
44: public class URLName {
45:
46: /**
47: * The full version of the URL
48: */
49: protected String fullURL;
50:
51: /**
52: * The protocol to use (ftp, http, nntp, imap, pop3 ... etc.) .
53: */
54: private String protocol;
55:
56: /**
57: * The username to use when connecting
58: */
59: private String username;
60:
61: /**
62: * The password to use when connecting.
63: */
64: private String password;
65:
66: /**
67: * The host name to which to connect.
68: */
69: private String host;
70:
71: /**
72: * The host's IP address, used in equals and hashCode.
73: * Computed on demand.
74: */
75: private InetAddress hostAddress;
76: private boolean hostAddressKnown = false;
77:
78: /**
79: * The protocol port to connect to.
80: */
81: private int port = -1;
82:
83: /**
84: * The specified file name on that host.
85: */
86: private String file;
87:
88: /**
89: * # reference.
90: */
91: private String ref;
92:
93: /**
94: * Our hash code.
95: */
96: private int hashCode = 0;
97:
98: /**
99: * A way to turn off encoding, just in case...
100: */
101: private static boolean doEncode = true;
102:
103: static {
104: try {
105:• doEncode = !Boolean.getBoolean("mail.URLName.dontencode");
106: } catch (Exception ex) {
107: // ignore any errors
108: }
109: }
110:
111: /**
112: * Creates a URLName object from the specified protocol,
113: * host, port number, file, username, and password. Specifying a port
114: * number of -1 indicates that the URL should use the default port for
115: * the protocol.
116: *
117: * @param protocol the protocol
118: * @param host the host name
119: * @param port the port number
120: * @param file the file
121: * @param username the user name
122: * @param password the password
123: */
124: public URLName(
125: String protocol,
126: String host,
127: int port,
128: String file,
129: String username,
130: String password
131: ) {
132: this.protocol = protocol;
133: this.host = host;
134: this.port = port;
135: int refStart;
136:• if (file != null && (refStart = file.indexOf('#')) != -1) {
137: this.file = file.substring(0, refStart);
138: this.ref = file.substring(refStart + 1);
139: } else {
140: this.file = file;
141: this.ref = null;
142: }
143:• this.username = doEncode ? encode(username) : username;
144:• this.password = doEncode ? encode(password) : password;
145: }
146:
147: /**
148: * Construct a URLName from a java.net.URL object.
149: *
150: * @param url the URL
151: */
152: public URLName(URL url) {
153: this(url.toString());
154: }
155:
156: /**
157: * Construct a URLName from the string. Parses out all the possible
158: * information (protocol, host, port, file, username, password).
159: *
160: * @param url the URL string
161: */
162: public URLName(String url) {
163: parseString(url);
164: }
165:
166: /**
167: * Constructs a string representation of this URLName.
168: */
169: @Override
170: public String toString() {
171:• if (fullURL == null) {
172: // add the "protocol:"
173: StringBuilder tempURL = new StringBuilder();
174:• if (protocol != null) {
175: tempURL.append(protocol);
176: tempURL.append(":");
177: }
178:
179:• if (username != null || host != null) {
180: // add the "//"
181: tempURL.append("//");
182:
183: // add the user:password@
184: // XXX - can you just have a password? without a username?
185:• if (username != null) {
186: tempURL.append(username);
187:
188:• if (password != null) {
189: tempURL.append(":");
190: tempURL.append(password);
191: }
192:
193: tempURL.append("@");
194: }
195:
196: // add host
197:• if (host != null) {
198: tempURL.append(host);
199: }
200:
201: // add port (if needed)
202:• if (port != -1) {
203: tempURL.append(":");
204: tempURL.append(Integer.toString(port));
205: }
206:• if (file != null)
207: tempURL.append("/");
208: }
209:
210: // add the file
211:• if (file != null) {
212: tempURL.append(file);
213: }
214:
215: // add the ref
216:• if (ref != null) {
217: tempURL.append("#");
218: tempURL.append(ref);
219: }
220:
221: // create the fullURL now
222: fullURL = tempURL.toString();
223: }
224:
225: return fullURL;
226: }
227:
228: /**
229: * Method which does all of the work of parsing the string.
230: *
231: * @param url the URL string to parse
232: */
233: protected void parseString(String url) {
234: // initialize everything in case called from subclass
235: // (URLName really should be a final class)
236: protocol = file = ref = host = username = password = null;
237: port = -1;
238:
239: int len = url.length();
240:
241: // find the protocol
242: // XXX - should check for only legal characters before the colon
243: // (legal: a-z, A-Z, 0-9, "+", ".", "-")
244: int protocolEnd = url.indexOf(':');
245:• if (protocolEnd != -1)
246: protocol = url.substring(0, protocolEnd);
247:
248: // is this an Internet standard URL that contains a host name?
249:• if (url.regionMatches(protocolEnd + 1, "//", 0, 2)) {
250: // find where the file starts
251: String fullhost = null;
252: int fileStart = url.indexOf('/', protocolEnd + 3);
253:• if (fileStart != -1) {
254: fullhost = url.substring(protocolEnd + 3, fileStart);
255:• if (fileStart + 1 < len)
256: file = url.substring(fileStart + 1);
257: else
258: file = "";
259: } else
260: fullhost = url.substring(protocolEnd + 3);
261:
262: // examine the fullhost, for username password etc.
263: int i = fullhost.indexOf('@');
264:• if (i != -1) {
265: String fulluserpass = fullhost.substring(0, i);
266: fullhost = fullhost.substring(i + 1);
267:
268: // get user and password
269: int passindex = fulluserpass.indexOf(':');
270:• if (passindex != -1) {
271: username = fulluserpass.substring(0, passindex);
272: password = fulluserpass.substring(passindex + 1);
273: } else {
274: username = fulluserpass;
275: }
276: }
277:
278: // get the port (if there)
279: int portindex;
280:• if (fullhost.length() > 0 && fullhost.charAt(0) == '[') {
281: // an IPv6 address?
282: portindex = fullhost.indexOf(':', fullhost.indexOf(']'));
283: } else {
284: portindex = fullhost.indexOf(':');
285: }
286:• if (portindex != -1) {
287: String portstring = fullhost.substring(portindex + 1);
288:• if (portstring.length() > 0) {
289: try {
290: port = Integer.parseInt(portstring);
291: } catch (NumberFormatException nfex) {
292: port = -1;
293: }
294: }
295:
296: host = fullhost.substring(0, portindex);
297: } else {
298: host = fullhost;
299: }
300: } else {
301:• if (protocolEnd + 1 < len)
302: file = url.substring(protocolEnd + 1);
303: }
304:
305: // extract the reference from the file name, if any
306: int refStart;
307:• if (file != null && (refStart = file.indexOf('#')) != -1) {
308: ref = file.substring(refStart + 1);
309: file = file.substring(0, refStart);
310: }
311: }
312:
313: /**
314: * Returns the port number of this URLName.
315: * Returns -1 if the port is not set.
316: *
317: * @return the port number
318: */
319: public int getPort() {
320: return port;
321: }
322:
323: /**
324: * Returns the protocol of this URLName.
325: * Returns null if this URLName has no protocol.
326: *
327: * @return the protocol
328: */
329: public String getProtocol() {
330: return protocol;
331: }
332:
333: /**
334: * Returns the file name of this URLName.
335: * Returns null if this URLName has no file name.
336: *
337: * @return the file name of this URLName
338: */
339: public String getFile() {
340: return file;
341: }
342:
343: /**
344: * Returns the reference of this URLName.
345: * Returns null if this URLName has no reference.
346: *
347: * @return the reference part of the URLName
348: */
349: public String getRef() {
350: return ref;
351: }
352:
353: /**
354: * Returns the host of this URLName.
355: * Returns null if this URLName has no host.
356: *
357: * @return the host name
358: */
359: public String getHost() {
360: return host;
361: }
362:
363: /**
364: * Returns the user name of this URLName.
365: * Returns null if this URLName has no user name.
366: *
367: * @return the user name
368: */
369: public String getUsername() {
370:• return doEncode ? decode(username) : username;
371: }
372:
373: /**
374: * Returns the password of this URLName.
375: * Returns null if this URLName has no password.
376: *
377: * @return the password
378: */
379: public String getPassword() {
380:• return doEncode ? decode(password) : password;
381: }
382:
383: /**
384: * Constructs a URL from the URLName.
385: *
386: * @return the URL
387: * @throws MalformedURLException if the URL is malformed
388: */
389: public URL getURL() throws MalformedURLException {
390: // URL expects the file to include the separating "/"
391: String f = getFile();
392:• if (f == null)
393: f = "";
394: else
395: f = "/" + f;
396: return new URL(getProtocol(), getHost(), getPort(), f);
397: }
398:
399: /**
400: * Compares two URLNames. The result is true if and only if the
401: * argument is not null and is a URLName object that represents the
402: * same URLName as this object. Two URLName objects are equal if
403: * they have the same protocol and the same host,
404: * the same port number on the host, the same username,
405: * and the same file on the host. The fields (host, username,
406: * file) are also considered the same if they are both
407: * null. <p>
408: *
409: * Hosts are considered equal if the names are equal (case independent)
410: * or if host name lookups for them both succeed and they both reference
411: * the same IP address. <p>
412: *
413: * Note that URLName has no knowledge of default port numbers for
414: * particular protocols, so "imap://host" and "imap://host:143"
415: * would not compare as equal. <p>
416: *
417: * Note also that the password field is not included in the comparison,
418: * nor is any reference field appended to the filename.
419: */
420: @Override
421: public boolean equals(Object obj) {
422:• if (!(obj instanceof URLName))
423: return false;
424: URLName u2 = (URLName) obj;
425:
426: // compare protocols
427:• if (!(protocol == u2.protocol ||
428:• (protocol != null && protocol.equals(u2.protocol))))
429: return false;
430:
431: // compare hosts
432: InetAddress a1 = getHostAddress(), a2 = u2.getHostAddress();
433: // if we have internet address for both, and they're not the same, fail
434:• if (a1 != null && a2 != null) {
435:• if (!a1.equals(a2))
436: return false;
437: // else, if we have host names for both, and they're not the same, fail
438:• } else if (host != null && u2.host != null) {
439:• if (!host.equalsIgnoreCase(u2.host))
440: return false;
441: // else, if not both null
442:• } else if (host != u2.host) {
443: return false;
444: }
445: // at this point, hosts match
446:
447: // compare usernames
448:• if (!(username == u2.username ||
449:• (username != null && username.equals(u2.username))))
450: return false;
451:
452: // Forget about password since it doesn't
453: // really denote a different store.
454:
455: // compare files
456:• String f1 = file == null ? "" : file;
457:• String f2 = u2.file == null ? "" : u2.file;
458:
459:• if (!f1.equals(f2))
460: return false;
461:
462: // compare ports
463:• if (port != u2.port)
464: return false;
465:
466: // all comparisons succeeded, they're equal
467: return true;
468: }
469:
470: /**
471: * Compute the hash code for this URLName.
472: */
473: @Override
474: public int hashCode() {
475:• if (hashCode != 0)
476: return hashCode;
477:• if (protocol != null)
478: hashCode += protocol.hashCode();
479: InetAddress addr = getHostAddress();
480:• if (addr != null)
481: hashCode += addr.hashCode();
482:• else if (host != null)
483: hashCode += host.toLowerCase(Locale.ENGLISH).hashCode();
484:• if (username != null)
485: hashCode += username.hashCode();
486:• if (file != null)
487: hashCode += file.hashCode();
488: hashCode += port;
489: return hashCode;
490: }
491:
492: /**
493: * Get the IP address of our host. Look up the
494: * name the first time and remember that we've done
495: * so, whether the lookup fails or not.
496: */
497: private synchronized InetAddress getHostAddress() {
498:• if (hostAddressKnown)
499: return hostAddress;
500:• if (host == null)
501: return null;
502: try {
503: hostAddress = InetAddress.getByName(host);
504: } catch (UnknownHostException ex) {
505: hostAddress = null;
506: }
507: hostAddressKnown = true;
508: return hostAddress;
509: }
510:
511: /**
512: * The class contains a utility method for converting a
513: * <code>String</code> into a MIME format called
514: * "<code>x-www-form-urlencoded</code>" format.
515: * <p>
516: * To convert a <code>String</code>, each character is examined in turn:
517: * <ul>
518: * <li>The ASCII characters '<code>a</code>' through '<code>z</code>',
519: * '<code>A</code>' through '<code>Z</code>', '<code>0</code>'
520: * through '<code>9</code>', and ".", "-",
521: * "*", "_" remain the same.
522: * <li>The space character '<code> </code>' is converted into a
523: * plus sign '<code>+</code>'.
524: * <li>All other characters are converted into the 3-character string
525: * "<code>%<i>xy</i></code>", where <i>xy</i> is the two-digit
526: * hexadecimal representation of the lower 8-bits of the character.
527: * </ul>
528: *
529: * @author Herb Jellinek
530: * @since JDK1.0
531: */
532: static BitSet dontNeedEncoding;
533: static final int caseDiff = ('a' - 'A');
534:
535: /* The list of characters that are not encoded have been determined by
536: referencing O'Reilly's "HTML: The Definitive Guide" (page 164). */
537:
538: static {
539: dontNeedEncoding = new BitSet(256);
540: int i;
541:• for (i = 'a'; i <= 'z'; i++) {
542: dontNeedEncoding.set(i);
543: }
544:• for (i = 'A'; i <= 'Z'; i++) {
545: dontNeedEncoding.set(i);
546: }
547:• for (i = '0'; i <= '9'; i++) {
548: dontNeedEncoding.set(i);
549: }
550: /* encoding a space to a + is done in the encode() method */
551: dontNeedEncoding.set(' ');
552: dontNeedEncoding.set('-');
553: dontNeedEncoding.set('_');
554: dontNeedEncoding.set('.');
555: dontNeedEncoding.set('*');
556: }
557:
558: /**
559: * Translates a string into <code>x-www-form-urlencoded</code> format.
560: *
561: * @param s <code>String</code> to be translated.
562: * @return the translated <code>String</code>.
563: */
564: static String encode(String s) {
565:• if (s == null)
566: return null;
567: // the common case is no encoding is needed
568:• for (int i = 0; i < s.length(); i++) {
569: int c = (int) s.charAt(i);
570:• if (c == ' ' || !dontNeedEncoding.get(c))
571: return _encode(s);
572: }
573: return s;
574: }
575:
576: private static String _encode(String s) {
577: int maxBytesPerChar = 10;
578: StringBuilder out = new StringBuilder(s.length());
579: ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);
580: OutputStreamWriter writer = new OutputStreamWriter(buf);
581:
582:• for (int i = 0; i < s.length(); i++) {
583: int c = (int) s.charAt(i);
584:• if (dontNeedEncoding.get(c)) {
585:• if (c == ' ') {
586: c = '+';
587: }
588: out.append((char) c);
589: } else {
590: // convert to external encoding before hex conversion
591: try {
592: writer.write(c);
593: writer.flush();
594: } catch (IOException e) {
595: buf.reset();
596: continue;
597: }
598: byte[] ba = buf.toByteArray();
599:• for (int j = 0; j < ba.length; j++) {
600: out.append('%');
601: char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);
602: // converting to use uppercase letter as part of
603: // the hex value if ch is a letter.
604:• if (Character.isLetter(ch)) {
605: ch -= caseDiff;
606: }
607: out.append(ch);
608: ch = Character.forDigit(ba[j] & 0xF, 16);
609:• if (Character.isLetter(ch)) {
610: ch -= caseDiff;
611: }
612: out.append(ch);
613: }
614: buf.reset();
615: }
616: }
617:
618: return out.toString();
619: }
620:
621:
622: /*
623: * The class contains a utility method for converting from
624: * a MIME format called "<code>x-www-form-urlencoded</code>"
625: * to a <code>String</code>
626: * <p>
627: * To convert to a <code>String</code>, each character is examined in turn:
628: * <ul>
629: * <li>The ASCII characters '<code>a</code>' through '<code>z</code>',
630: * '<code>A</code>' through '<code>Z</code>', and '<code>0</code>'
631: * through '<code>9</code>' remain the same.
632: * <li>The plus sign '<code>+</code>'is converted into a
633: * space character '<code> </code>'.
634: * <li>The remaining characters are represented by 3-character
635: * strings which begin with the percent sign,
636: * "<code>%<i>xy</i></code>", where <i>xy</i> is the two-digit
637: * hexadecimal representation of the lower 8-bits of the character.
638: * </ul>
639: *
640: * @author Mark Chamness
641: * @author Michael McCloskey
642: * @since 1.2
643: */
644:
645: /**
646: * Decodes a "x-www-form-urlencoded"
647: * to a <code>String</code>.
648: *
649: * @param s the <code>String</code> to decode
650: * @return the newly decoded <code>String</code>
651: */
652: static String decode(String s) {
653:• if (s == null)
654: return null;
655:• if (indexOfAny(s, "+%") == -1)
656: return s; // the common case
657:
658: StringBuilder sb = new StringBuilder();
659:• for (int i = 0; i < s.length(); i++) {
660: char c = s.charAt(i);
661:• switch (c) {
662: case '+':
663: sb.append(' ');
664: break;
665: case '%':
666: try {
667: sb.append((char) Integer.parseInt(
668: s.substring(i + 1, i + 3), 16));
669: } catch (NumberFormatException e) {
670: throw new IllegalArgumentException(
671: "Illegal URL encoded value: " +
672: s.substring(i, i + 3));
673: }
674: i += 2;
675: break;
676: default:
677: sb.append(c);
678: break;
679: }
680: }
681: // Undo conversion to external encoding
682: String result = sb.toString();
683: try {
684: byte[] inputBytes = result.getBytes("8859_1");
685: result = new String(inputBytes);
686: } catch (UnsupportedEncodingException e) {
687: // The system should always have 8859_1
688: }
689: return result;
690: }
691:
692: /**
693: * Return the first index of any of the characters in "any" in "s",
694: * or -1 if none are found.
695: *
696: * This should be a method on String.
697: */
698: private static int indexOfAny(String s, String any) {
699: return indexOfAny(s, any, 0);
700: }
701:
702: private static int indexOfAny(String s, String any, int start) {
703: try {
704: int len = s.length();
705:• for (int i = start; i < len; i++) {
706:• if (any.indexOf(s.charAt(i)) >= 0)
707: return i;
708: }
709: return -1;
710: } catch (StringIndexOutOfBoundsException e) {
711: return -1;
712: }
713: }
714:
715: /*
716: // Do not remove, this is needed when testing new URL cases
717: public static void main(String[] argv) {
718:         String [] testURLNames = {
719:          "protocol://userid:password@host:119/file",
720:          "http://funny/folder/file.html",
721:          "http://funny/folder/file.html#ref",
722:          "http://funny/folder/file.html#",
723:          "http://funny/#ref",
724:          "imap://jmr:secret@labyrinth//var/mail/jmr",
725:          "nntp://fred@labyrinth:143/save/it/now.mbox",
726:          "imap://jmr@labyrinth/INBOX",
727:          "imap://labryrinth",
728:          "imap://labryrinth/",
729:          "file:",
730:          "file:INBOX",
731:          "file:/home/shannon/mail/foo",
732:          "/tmp/foo",
733:          "//host/tmp/foo",
734:          ":/tmp/foo",
735:          "/really/weird:/tmp/foo#bar",
736:          ""
737:         };
738:
739:         URLName url =
740:          new URLName("protocol", "host", 119, "file", "userid", "password");
741:         System.out.println("Test URL: " + url.toString());
742:         if (argv.length == 0) {
743:          for (int i = 0; i < testURLNames.length; i++) {
744:                 print(testURLNames[i]);
745:                 System.out.println();
746:          }
747:         } else {
748:          for (int i = 0; i < argv.length; i++) {
749:                 print(argv[i]);
750:                 System.out.println();
751:          }
752:          if (argv.length == 2) {
753:                 URLName u1 = new URLName(argv[0]);
754:                 URLName u2 = new URLName(argv[1]);
755:                 System.out.println("URL1 hash code: " + u1.hashCode());
756:                 System.out.println("URL2 hash code: " + u2.hashCode());
757:                 if (u1.equals(u2))
758:                  System.out.println("success, equal");
759:                 else
760:                  System.out.println("fail, not equal");
761:                 if (u2.equals(u1))
762:                  System.out.println("success, equal");
763:                 else
764:                  System.out.println("fail, not equal");
765:                 if (u1.hashCode() == u2.hashCode())
766:                  System.out.println("success, hashCodes equal");
767:                 else
768:                  System.out.println("fail, hashCodes not equal");
769:          }
770:         }
771: }
772:
773: private static void print(String name) {
774:         URLName url = new URLName(name);
775:         System.out.println("Original URL: " + name);
776:         System.out.println("The fullUrl : " + url.toString());
777:         if (!name.equals(url.toString()))
778:          System.out.println(" : NOT EQUAL!");
779:         System.out.println("The protocol is: " + url.getProtocol());
780:         System.out.println("The host is: " + url.getHost());
781:         System.out.println("The port is: " + url.getPort());
782:         System.out.println("The user is: " + url.getUsername());
783:         System.out.println("The password is: " + url.getPassword());
784:         System.out.println("The file is: " + url.getFile());
785:         System.out.println("The ref is: " + url.getRef());
786: }
787: */
788: }