geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jboy...@apache.org
Subject cvs commit: incubator-geronimo/specs/javamail/src/test/javax/mail URLNameTest.java
Date Mon, 18 Aug 2003 20:53:05 GMT
jboynes     2003/08/18 13:53:05

  Modified:    specs/javamail/src/java/javax/mail URLName.java
               specs/javamail/src/test/javax/mail URLNameTest.java
  Log:
  New impl of URLName and revised test
  
  Revision  Changes    Path
  1.3       +183 -86   incubator-geronimo/specs/javamail/src/java/javax/mail/URLName.java
  
  Index: URLName.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/specs/javamail/src/java/javax/mail/URLName.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- URLName.java	16 Aug 2003 04:29:52 -0000	1.2
  +++ URLName.java	18 Aug 2003 20:53:05 -0000	1.3
  @@ -59,124 +59,221 @@
   // DO NOT add / change / or delete method signatures!
   //
   package javax.mail;
  +
   import java.net.MalformedURLException;
  +import java.net.URI;
  +import java.net.URISyntaxException;
   import java.net.URL;
  +
   /**
    * @version $Revision$ $Date$
    */
   public class URLName {
  -    private String _file;
  -    private String _host;
  -    private String _password;
  -    private int _port = -1;
  -    private String _protocol;
  -    private String _ref;
  -    private String _username;
  +    private String file;
  +    private String host;
  +    private String password;
  +    private int port;
  +    private String protocol;
  +    private String ref;
  +    private String username;
       protected String fullURL;
  +
       public URLName(String url) {
           parseString(url);
       }
  -    public URLName(
  -        String protocol,
  -        String host,
  -        int port,
  -        String file,
  -        String username,
  -        String password) {
  -        _protocol = protocol;
  -        _host = host;
  -        _port = port;
  -        _file = file;
  -        int pos;
  -        if (file != null && (pos = file.indexOf("#")) != -1) {
  -            _file = file.substring(0, pos);
  -            _ref = file.substring(pos + 1);
  +
  +    protected void parseString(String url) {
  +        URI uri;
  +        try {
  +            if (url == null) {
  +                uri = null;
  +            } else {
  +                uri = new URI(url);
  +            }
  +        } catch (URISyntaxException e) {
  +            uri = null;
  +        }
  +        if (uri == null) {
  +            protocol = null;
  +            host = null;
  +            port = -1;
  +            file = null;
  +            ref = null;
  +            username = null;
  +            password = null;
  +            return;
  +        }
  +
  +        protocol = checkBlank(uri.getScheme());
  +        host = checkBlank(uri.getHost());
  +        port = uri.getPort();
  +        file = checkBlank(uri.getPath());
  +        ref = checkBlank(uri.getFragment());
  +        String userInfo = checkBlank(uri.getUserInfo());
  +        if (userInfo == null) {
  +            username = null;
  +            password = null;
  +        } else {
  +            int pos = userInfo.indexOf(':');
  +            if (pos == -1) {
  +                username = userInfo;
  +                password = null;
  +            } else {
  +                username = userInfo.substring(0, pos);
  +                password = userInfo.substring(pos + 1);
  +            }
           }
  -        _username = username;
  -        _password = password;
           updateFullURL();
       }
  +
  +    public URLName(String protocol, String host, int port, String file, String username,
String password) {
  +        this.protocol = checkBlank(protocol);
  +        this.host = checkBlank(host);
  +        this.port = port;
  +        if (file == null || file.length() == 0) {
  +            this.file = null;
  +            ref = null;
  +        } else {
  +            int pos = file.indexOf('#');
  +            if (pos == -1) {
  +                this.file = file;
  +                ref = null;
  +            } else {
  +                this.file = file.substring(0, pos);
  +                ref = file.substring(pos + 1);
  +            }
  +        }
  +        this.username = checkBlank(username);
  +        if (this.username != null) {
  +            this.password = checkBlank(password);
  +        } else {
  +            this.password = null;
  +        }
  +        updateFullURL();
  +    }
  +
       public URLName(URL url) {
  -        parseURL(url);
  +        protocol = checkBlank(url.getProtocol());
  +        host = checkBlank(url.getHost());
  +        port = url.getPort();
  +        file = checkBlank(url.getFile());
  +        ref = checkBlank(url.getRef());
  +        String userInfo = checkBlank(url.getUserInfo());
  +        if (userInfo == null) {
  +            username = null;
  +            password = null;
  +        } else {
  +            int pos = userInfo.indexOf(':');
  +            if (pos == -1) {
  +                username = userInfo;
  +                password = null;
  +            } else {
  +                username = userInfo.substring(0, pos);
  +                password = userInfo.substring(pos + 1);
  +            }
  +        }
  +        updateFullURL();
       }
  -    public boolean equals(Object other) {
  -        if (other == null || other.getClass() != this.getClass()) {
  +
  +    private static String checkBlank(String target) {
  +        if (target == null || target.length() == 0) {
  +            return null;
  +        } else {
  +            return target;
  +        }
  +    }
  +
  +    private void updateFullURL() {
  +        StringBuffer buf = new StringBuffer(100);
  +        if (protocol != null) {
  +            buf.append(protocol).append(':');
  +            if (host != null) {
  +                buf.append("//");
  +                if (username != null) {
  +                    buf.append(username);
  +                    if (password != null) {
  +                        buf.append(':').append(password);
  +                    }
  +                    buf.append('@');
  +                }
  +                buf.append(host);
  +                if (port != -1) {
  +                    buf.append(':').append(port);
  +                }
  +                if (file != null) {
  +                    buf.append(file);
  +                }
  +                if (ref != null) {
  +                    buf.append('#').append(ref);
  +                }
  +            }
  +        }
  +        fullURL = buf.toString();
  +    }
  +
  +    public boolean equals(Object o) {
  +        if (o instanceof URLName == false) {
  +            return false;
  +        }
  +        URLName other = (URLName) o;
  +        // check same protocol - false if either is null
  +        if (protocol == null || other.protocol == null || !protocol.equals(other.protocol))
{
  +            return false;
  +        }
  +
  +        if (port != other.port) {
               return false;
           }
  -        URLName name = (URLName) other;
  -        return fullURL.equals(name.fullURL);
  +
  +        // check host - false if not (both null or both equal)
  +        return areSame(host, other.host) && areSame(file, other.file) &&
areSame(username, other.username) && areSame(password, other.password);
  +    }
  +
  +    private static boolean areSame(String s1, String s2) {
  +        if (s1 == null) {
  +            return s2 == null;
  +        } else {
  +            return s1.equals(s2);
  +        }
       }
  +
  +    public int hashCode() {
  +        return fullURL.hashCode();
  +    }
  +
  +    public String toString() {
  +        return fullURL;
  +    }
  +
       public String getFile() {
  -        return _file;
  +        return file;
       }
  +
       public String getHost() {
  -        return _host;
  +        return host;
       }
  +
       public String getPassword() {
  -        return _password;
  +        return password;
       }
  +
       public int getPort() {
  -        return _port;
  +        return port;
       }
  +
       public String getProtocol() {
  -        return _protocol;
  +        return protocol;
       }
  +
       public String getRef() {
  -        return _ref;
  +        return ref;
       }
  +
       public URL getURL() throws MalformedURLException {
           return new URL(fullURL);
       }
  +
       public String getUsername() {
  -        return _username;
  -    }
  -    public int hashCode() {
  -        return fullURL.hashCode();
  -    }
  -    protected void parseString(String name) {
  -        try {
  -            // TODO Implement this not using URL
  -            // The URL doesn't handle unknown protocol
  -            // types, at least, without registering ahandler
  -            // first of all -- and it may not like user:pw@host
  -            // either
  -            URL url = new URL(name);
  -            parseURL(url);
  -        } catch (MalformedURLException e) {
  -            throw new IllegalArgumentException(e.getMessage());
  -        }
  -    }
  -    private void parseURL(URL url) {
  -        _file = checkBlank(url.getFile());
  -        _host = checkBlank(url.getHost());
  -        _port = url.getPort();
  -        _protocol = checkBlank(url.getProtocol());
  -        _ref = checkBlank(url.getRef());
  -        _username = checkBlank(url.getUserInfo());
  -        updateFullURL();
  -    }
  -    private String checkBlank(String target) {
  -        if (target == null) {
  -            return target;
  -        } else if (target.equals("")) {
  -            return null;
  -        } else {
  -            return target;
  -        }
  -    }
  -    public String toString() {
  -        return fullURL;
  -    }
  -    private void updateFullURL() {
  -        // what happens with user/password? Are they needed here?
  -        fullURL =
  -            _protocol
  -                + "://"
  -                + (_username == null ? "" : _username + "@")
  -                + (_password == null ? "" : ":" + _password)
  -                + _host
  -                + ":"
  -                + (_port == -1 ? "" : String.valueOf(_port))
  -                + (_file == null ? "" : _file + (_ref == null ? "" : "#" + _ref));
  +        return username;
       }
   }
  
  
  
  1.2       +289 -56   incubator-geronimo/specs/javamail/src/test/javax/mail/URLNameTest.java
  
  Index: URLNameTest.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/specs/javamail/src/test/javax/mail/URLNameTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- URLNameTest.java	16 Aug 2003 01:55:49 -0000	1.1
  +++ URLNameTest.java	18 Aug 2003 20:53:05 -0000	1.2
  @@ -59,80 +59,313 @@
   // DO NOT add / change / or delete method signatures!
   //
   package javax.mail;
  +
   import java.net.MalformedURLException;
   import java.net.URL;
  +
   import junit.framework.TestCase;
  +
   /**
    * @version $Revision$ $Date$
    */
   public class URLNameTest extends TestCase {
  -    private String testURL =
  -        "http://alex@www.thing.com:1234/jobby/jobby/jobby#splat";
  -    ;
       public URLNameTest(String name) {
           super(name);
       }
  -    public void testHashCode() {
  -    }
  +
       public void testURLNameString() {
  -        URLName name = new URLName(testURL);
  +        String s;
  +        URLName name;
  +
  +        s = "http://www.apache.org";
  +        name = new URLName(s);
  +        assertEquals(s, name.toString());
  +        assertEquals("http", name.getProtocol());
  +        assertEquals("www.apache.org", name.getHost());
  +        assertEquals(-1, name.getPort());
  +        assertNull(name.getFile());
  +        assertNull(name.getRef());
  +        assertNull(name.getUsername());
  +        assertNull(name.getPassword());
  +        try {
  +            assertEquals(new URL(s), name.getURL());
  +        } catch (MalformedURLException e) {
  +            fail();
  +        }
  +
  +        s = "http://www.apache.org/file/file1#ref";
  +        name = new URLName(s);
  +        assertEquals(s, name.toString());
  +        assertEquals("http", name.getProtocol());
  +        assertEquals("www.apache.org", name.getHost());
  +        assertEquals(-1, name.getPort());
  +        assertEquals("/file/file1", name.getFile());
  +        assertEquals("ref", name.getRef());
  +        assertNull(name.getUsername());
  +        assertNull(name.getPassword());
  +        try {
  +            assertEquals(new URL(s), name.getURL());
  +        } catch (MalformedURLException e) {
  +            fail();
  +        }
  +
  +        s = "http://www.apache.org/file/";
  +        name = new URLName(s);
  +        assertEquals(s, name.toString());
  +        assertEquals("http", name.getProtocol());
  +        assertEquals("www.apache.org", name.getHost());
  +        assertEquals(-1, name.getPort());
  +        assertEquals("/file/", name.getFile());
  +        assertNull(name.getRef());
  +        assertNull(name.getUsername());
  +        assertNull(name.getPassword());
  +        try {
  +            assertEquals(new URL(s), name.getURL());
  +        } catch (MalformedURLException e) {
  +            fail();
  +        }
  +
  +        s = "http://john@www.apache.org/file/";
  +        name = new URLName(s);
  +        assertEquals(s, name.toString());
           assertEquals("http", name.getProtocol());
  -        assertEquals("www.thing.com", name.getHost());
  -        assertEquals(1234, name.getPort());
  -        assertEquals("/jobby/jobby/jobby", name.getFile());
  -        assertEquals("splat", name.getRef());
  -        assertEquals("alex", name.getUsername());
  -        name = new URLName("http://www.thing.com");
  +        assertEquals("www.apache.org", name.getHost());
  +        assertEquals(-1, name.getPort());
  +        assertEquals("/file/", name.getFile());
  +        assertNull(name.getRef());
  +        assertEquals("john", name.getUsername());
  +        assertNull(name.getPassword());
  +        try {
  +            assertEquals(new URL(s), name.getURL());
  +        } catch (MalformedURLException e) {
  +            fail();
  +        }
  +
  +        s = "http://john:doe@www.apache.org/file/";
  +        name = new URLName(s);
  +        assertEquals(s, name.toString());
           assertEquals("http", name.getProtocol());
  -        assertEquals("www.thing.com", name.getHost());
  +        assertEquals("www.apache.org", name.getHost());
  +        assertEquals(-1, name.getPort());
  +        assertEquals("/file/", name.getFile());
  +        assertNull(name.getRef());
  +        assertEquals("john", name.getUsername());
  +        assertEquals("doe", name.getPassword());
  +        try {
  +            assertEquals(new URL(s), name.getURL());
  +        } catch (MalformedURLException e) {
  +            fail();
  +        }
  +
  +        s = "file/file2";
  +        name = new URLName(s);
  +        assertNull(name.getProtocol());
  +        assertNull(name.getHost());
  +        assertEquals(-1, name.getPort());
  +        assertEquals("file/file2", name.getFile());
  +        assertNull(name.getRef());
  +        assertNull(name.getUsername());
  +        assertNull(name.getPassword());
  +        try {
  +            name.getURL();
  +            fail();
  +        } catch (MalformedURLException e) {
  +            // OK
  +        }
  +
  +        name = new URLName((String) null);
  +        assertNull( name.getProtocol());
  +        assertNull(name.getHost());
  +        assertEquals(-1, name.getPort());
  +        assertNull(name.getFile());
  +        assertNull(name.getRef());
  +        assertNull(name.getUsername());
  +        assertNull(name.getPassword());
  +        try {
  +            name.getURL();
  +            fail();
  +        } catch (MalformedURLException e) {
  +            // OK
  +        }
  +
  +        name = new URLName("");
  +        assertNull( name.getProtocol());
  +        assertNull(name.getHost());
           assertEquals(-1, name.getPort());
  -        assertEquals(null, name.getFile());
  -        assertEquals(null, name.getRef());
  +        assertNull(name.getFile());
  +        assertNull(name.getRef());
  +        assertNull(name.getUsername());
  +        assertNull(name.getPassword());
  +        try {
  +            name.getURL();
  +            fail();
  +        } catch (MalformedURLException e) {
  +            // OK
  +        }
       }
  +
       public void testURLNameAll() {
  -        URLName name =
  -            new URLName(
  -                "http",
  -                "www.jobby.com",
  -                99,
  -                "/bottles/of/beer/on/the#wall",
  -                "me",
  -                "me2");
  -        assertEquals("http", name.getProtocol());
  -        assertEquals("www.jobby.com", name.getHost());
  -        assertEquals(99, name.getPort());
  -        assertEquals("/bottles/of/beer/on/the", name.getFile());
  -        assertEquals("wall", name.getRef());
  -        assertEquals("me", name.getUsername());
  -        assertEquals("me2", name.getPassword());
  +        URLName name;
  +        name = new URLName(null, null, -1, null, null, null);
  +        assertNull(name.getProtocol());
  +        assertNull(name.getHost());
  +        assertEquals(-1, name.getPort());
  +        assertNull(name.getFile());
  +        assertNull(name.getRef());
  +        assertNull(name.getUsername());
  +        assertNull(name.getPassword());
  +        try {
  +            name.getURL();
  +            fail();
  +        } catch (MalformedURLException e) {
  +            // OK
  +        }
  +
  +        name = new URLName("", "", -1, "", "", "");
  +        assertNull(name.getProtocol());
  +        assertNull(name.getHost());
  +        assertEquals(-1, name.getPort());
  +        assertNull(name.getFile());
  +        assertNull(name.getRef());
  +        assertNull(name.getUsername());
  +        assertNull(name.getPassword());
  +        try {
  +            name.getURL();
  +            fail();
  +        } catch (MalformedURLException e) {
  +            // OK
  +        }
  +
  +        name = new URLName("http", "www.apache.org", -1, null, null, null);
  +        assertEquals("http://www.apache.org", name.toString());
  +        assertEquals("http", name.getProtocol());
  +        assertEquals("www.apache.org", name.getHost());
  +        assertEquals(-1, name.getPort());
  +        assertNull(name.getFile());
  +        assertNull(name.getRef());
  +        assertNull(name.getUsername());
  +        assertNull(name.getPassword());
  +        try {
  +            assertEquals(new URL("http://www.apache.org"), name.getURL());
  +        } catch (MalformedURLException e) {
  +            fail();
  +        }
  +
  +        name = new URLName("http", "www.apache.org", 8080, "", "", "");
  +        assertEquals("http://www.apache.org:8080", name.toString());
  +        assertEquals("http", name.getProtocol());
  +        assertEquals("www.apache.org", name.getHost());
  +        assertEquals(8080, name.getPort());
  +        assertNull(name.getFile());
  +        assertNull(name.getRef());
  +        assertNull(name.getUsername());
  +        assertNull(name.getPassword());
  +        try {
  +            assertEquals(new URL("http://www.apache.org:8080"), name.getURL());
  +        } catch (MalformedURLException e) {
  +            fail();
  +        }
  +
  +        name = new URLName("http", "www.apache.org", -1, "/file/file2", "", "");
  +        assertEquals("http://www.apache.org/file/file2", name.toString());
  +        assertEquals("http", name.getProtocol());
  +        assertEquals("www.apache.org", name.getHost());
  +        assertEquals(-1, name.getPort());
  +        assertEquals("/file/file2", name.getFile());
  +        assertNull(name.getRef());
  +        assertNull(name.getUsername());
  +        assertNull(name.getPassword());
  +        try {
  +            assertEquals(new URL("http://www.apache.org/file/file2"), name.getURL());
  +        } catch (MalformedURLException e) {
  +            fail();
  +        }
  +
  +        name = new URLName("http", "www.apache.org", -1, "/file/file2", "john", "");
  +        assertEquals("http://john@www.apache.org/file/file2", name.toString());
  +        assertEquals("http", name.getProtocol());
  +        assertEquals("www.apache.org", name.getHost());
  +        assertEquals(-1, name.getPort());
  +        assertEquals("/file/file2", name.getFile());
  +        assertNull(name.getRef());
  +        assertEquals("john", name.getUsername());
  +        assertNull(name.getPassword());
  +        try {
  +            assertEquals(new URL("http://john@www.apache.org/file/file2"), name.getURL());
  +        } catch (MalformedURLException e) {
  +            fail();
  +        }
  +
  +        name = new URLName("http", "www.apache.org", -1, "/file/file2", "john", "doe");
  +        assertEquals("http://john:doe@www.apache.org/file/file2", name.toString());
  +        assertEquals("http", name.getProtocol());
  +        assertEquals("www.apache.org", name.getHost());
  +        assertEquals(-1, name.getPort());
  +        assertEquals("/file/file2", name.getFile());
  +        assertNull(name.getRef());
  +        assertEquals("john", name.getUsername());
  +        assertEquals("doe", name.getPassword());
  +        try {
  +            assertEquals(new URL("http://john:doe@www.apache.org/file/file2"), name.getURL());
  +        } catch (MalformedURLException e) {
  +            fail();
  +        }
  +
  +        name = new URLName("http", "www.apache.org", -1, "/file/file2", "", "doe");
  +        assertEquals("http://www.apache.org/file/file2", name.toString());
  +        assertEquals("http", name.getProtocol());
  +        assertEquals("www.apache.org", name.getHost());
  +        assertEquals(-1, name.getPort());
  +        assertEquals("/file/file2", name.getFile());
  +        assertNull(name.getRef());
  +        assertNull(name.getUsername());
  +        assertNull(name.getPassword());
  +        try {
  +            assertEquals(new URL("http://www.apache.org/file/file2"), name.getURL());
  +        } catch (MalformedURLException e) {
  +            fail();
  +        }
       }
  +
       public void testURLNameURL() throws MalformedURLException {
  -        URL url = new URL(testURL);
  -        URLName name = new URLName(url);
  +        URL url;
  +        URLName name;
  +
  +        url = new URL("http://www.apache.org");
  +        name = new URLName(url);
           assertEquals("http", name.getProtocol());
  -        assertEquals("www.thing.com", name.getHost());
  -        assertEquals(1234, name.getPort());
  -        assertEquals("/jobby/jobby/jobby", name.getFile());
  -        assertEquals("splat", name.getRef());
  -    }
  -    public void testEqualsObject() {
  -        URLName name = new URLName(testURL);
  -        URLName name2 = new URLName(testURL);
  -        assertEquals(name, name2);
  -    }
  -    public void testGetURL() throws MalformedURLException {
  -        URLName name = new URLName(testURL);
  -        URL url = new URL(testURL);
  -        assertEquals(url, name.getURL());
  -    }
  -    public void testParseString() {
  -        URLName name = new URLName("ftp://not.com");
  -        URLName name2 = new URLName(testURL);
  -        name.parseString(testURL);
  -        assertEquals(name, name2);
  +        assertEquals("www.apache.org", name.getHost());
  +        assertEquals(-1, name.getPort());
  +        assertNull(name.getFile());
  +        assertNull(name.getRef());
  +        assertNull(name.getUsername());
  +        assertNull(name.getPassword());
  +        try {
  +            assertEquals(url, name.getURL());
  +        } catch (MalformedURLException e) {
  +            fail();
  +        }
       }
  -    public void testUser() {
  -        URLName name = new URLName("ftp://alex@thing.com");
  -        assertEquals("alex", name.getUsername());
  +
  +    public void testEquals() throws MalformedURLException {
  +        URLName name1 = new URLName("http://www.apache.org");
  +        assertEquals(name1, new URLName("http://www.apache.org"));
  +        assertEquals(name1, new URLName(new URL("http://www.apache.org")));
  +        assertEquals(name1, new URLName("http", "www.apache.org", -1, null, null, null));
  +        assertEquals(name1, new URLName("http://www.apache.org#foo")); // wierd but ref
is not part of the equals contract
  +        assertTrue(!name1.equals(new URLName("http://www.apache.org:8080")));
  +        assertTrue(!name1.equals(new URLName("http://cvs.apache.org")));
  +        assertTrue(!name1.equals(new URLName("https://www.apache.org")));
  +
  +        name1 = new URLName("http://john:doe@www.apache.org");
  +        assertEquals(name1, new URLName(new URL("http://john:doe@www.apache.org")));
  +        assertEquals(name1, new URLName("http", "www.apache.org", -1, null, "john", "doe"));
  +        assertTrue(!name1.equals(new URLName("http://john:xxx@www.apache.org")));
  +        assertTrue(!name1.equals(new URLName("http://xxx:doe@www.apache.org")));
  +        assertTrue(!name1.equals(new URLName("http://www.apache.org")));
  +
  +        assertEquals(new URLName("http://john@www.apache.org"), new URLName("http", "www.apache.org",
-1, null, "john", null));
  +        assertEquals(new URLName("http://www.apache.org"), new URLName("http", "www.apache.org",
-1, null, null, "doe"));
       }
  +
   }
  
  
  

Mime
View raw message