Clover coverage report - Maven Clover report
Coverage timestamp: Sun Aug 20 2006 04:01:04 PDT
file stats: LOC: 239   Methods: 18
NCLOC: 194   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
URLName.java 90.5% 94.5% 100% 94.1%
coverage coverage
 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.URI;
 22    import java.net.URISyntaxException;
 23    import java.net.URL;
 24   
 25    /**
 26    * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
 27    */
 28    public class URLName {
 29    private String file;
 30    private String host;
 31    private String password;
 32    private int port;
 33    private String protocol;
 34    private String ref;
 35    private String username;
 36    protected String fullURL;
 37    private int hashCode;
 38   
 39  37 public URLName(String url) {
 40  37 parseString(url);
 41    }
 42   
 43  37 protected void parseString(String url) {
 44  37 URI uri;
 45  37 try {
 46  37 if (url == null) {
 47  1 uri = null;
 48    } else {
 49  36 uri = new URI(url);
 50    }
 51    } catch (URISyntaxException e) {
 52  0 uri = null;
 53    }
 54  37 if (uri == null) {
 55  1 protocol = null;
 56  1 host = null;
 57  1 port = -1;
 58  1 file = null;
 59  1 ref = null;
 60  1 username = null;
 61  1 password = null;
 62  1 return;
 63    }
 64   
 65  36 protocol = checkBlank(uri.getScheme());
 66  36 host = checkBlank(uri.getHost());
 67  36 port = uri.getPort();
 68  36 file = checkBlank(uri.getPath());
 69  36 ref = checkBlank(uri.getFragment());
 70  36 String userInfo = checkBlank(uri.getUserInfo());
 71  36 if (userInfo == null) {
 72  18 username = null;
 73  18 password = null;
 74    } else {
 75  18 int pos = userInfo.indexOf(':');
 76  18 if (pos == -1) {
 77  14 username = userInfo;
 78  14 password = null;
 79    } else {
 80  4 username = userInfo.substring(0, pos);
 81  4 password = userInfo.substring(pos + 1);
 82    }
 83    }
 84  36 updateFullURL();
 85    }
 86   
 87  13 public URLName(String protocol, String host, int port, String file, String username, String password) {
 88  13 this.protocol = checkBlank(protocol);
 89  13 this.host = checkBlank(host);
 90  13 this.port = port;
 91  13 if (file == null || file.length() == 0) {
 92  9 this.file = null;
 93  9 ref = null;
 94    } else {
 95  4 int pos = file.indexOf('#');
 96  4 if (pos == -1) {
 97  4 this.file = file;
 98  4 ref = null;
 99    } else {
 100  0 this.file = file.substring(0, pos);
 101  0 ref = file.substring(pos + 1);
 102    }
 103    }
 104  13 this.username = checkBlank(username);
 105  13 if (this.username != null) {
 106  4 this.password = checkBlank(password);
 107    } else {
 108  9 this.password = null;
 109    }
 110  13 updateFullURL();
 111    }
 112   
 113  3 public URLName(URL url) {
 114  3 protocol = checkBlank(url.getProtocol());
 115  3 host = checkBlank(url.getHost());
 116  3 port = url.getPort();
 117  3 file = checkBlank(url.getFile());
 118  3 ref = checkBlank(url.getRef());
 119  3 String userInfo = checkBlank(url.getUserInfo());
 120  3 if (userInfo == null) {
 121  2 username = null;
 122  2 password = null;
 123    } else {
 124  1 int pos = userInfo.indexOf(':');
 125  1 if (pos == -1) {
 126  0 username = userInfo;
 127  0 password = null;
 128    } else {
 129  1 username = userInfo.substring(0, pos);
 130  1 password = userInfo.substring(pos + 1);
 131    }
 132    }
 133  3 updateFullURL();
 134    }
 135   
 136  238 private static String checkBlank(String target) {
 137  238 if (target == null || target.length() == 0) {
 138  107 return null;
 139    } else {
 140  131 return target;
 141    }
 142    }
 143   
 144  52 private void updateFullURL() {
 145  52 hashCode = 0;
 146  52 StringBuffer buf = new StringBuffer(100);
 147  52 if (protocol != null) {
 148  47 buf.append(protocol).append(':');
 149  47 if (host != null) {
 150  47 buf.append("//");
 151  47 if (username != null) {
 152  23 buf.append(username);
 153  23 if (password != null) {
 154  7 buf.append(':').append(password);
 155    }
 156  23 buf.append('@');
 157    }
 158  47 buf.append(host);
 159  47 if (port != -1) {
 160  2 buf.append(':').append(port);
 161    }
 162  47 if (file != null) {
 163  11 buf.append(file);
 164    }
 165  47 hashCode = buf.toString().hashCode();
 166  47 if (ref != null) {
 167  3 buf.append('#').append(ref);
 168    }
 169    }
 170    }
 171  52 fullURL = buf.toString();
 172    }
 173   
 174  16 public boolean equals(Object o) {
 175  16 if (o instanceof URLName == false) {
 176  0 return false;
 177    }
 178  16 URLName other = (URLName) o;
 179    // check same protocol - false if either is null
 180  16 if (protocol == null || other.protocol == null || !protocol.equals(other.protocol)) {
 181  2 return false;
 182    }
 183   
 184  14 if (port != other.port) {
 185  1 return false;
 186    }
 187   
 188    // check host - false if not (both null or both equal)
 189  13 return areSame(host, other.host) && areSame(file, other.file) && areSame(username, other.username) && areSame(password, other.password);
 190    }
 191   
 192  47 private static boolean areSame(String s1, String s2) {
 193  47 if (s1 == null) {
 194  24 return s2 == null;
 195    } else {
 196  23 return s1.equals(s2);
 197    }
 198    }
 199   
 200  2 public int hashCode() {
 201  2 return hashCode;
 202    }
 203   
 204  12 public String toString() {
 205  12 return fullURL;
 206    }
 207   
 208  18 public String getFile() {
 209  18 return file;
 210    }
 211   
 212  18 public String getHost() {
 213  18 return host;
 214    }
 215   
 216  18 public String getPassword() {
 217  18 return password;
 218    }
 219   
 220  18 public int getPort() {
 221  18 return port;
 222    }
 223   
 224  18 public String getProtocol() {
 225  18 return protocol;
 226    }
 227   
 228  18 public String getRef() {
 229  18 return ref;
 230    }
 231   
 232  17 public URL getURL() throws MalformedURLException {
 233  17 return new URL(fullURL);
 234    }
 235   
 236  18 public String getUsername() {
 237  18 return username;
 238    }
 239    }