| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.util; |
| 16 |
|
| 17 |
import junit.framework.TestCase; |
| 18 |
import org.globus.util.GlobusURL; |
| 19 |
|
| 20 |
public class GlobusURLTest extends TestCase { |
| 21 |
|
| 22 |
public void testParse() { |
| 23 |
GlobusURL url = null; |
| 24 |
|
| 25 |
try { |
| 26 |
url = new GlobusURL("file://host1"); |
| 27 |
} catch (Exception e) { |
| 28 |
fail("Parse failed: " + e.getMessage()); |
| 29 |
} |
| 30 |
checkUrl(url, "file", "host1", -1, null, null, null); |
| 31 |
|
| 32 |
try { |
| 33 |
url = new GlobusURL("http:///file1"); |
| 34 |
} catch (Exception e) { |
| 35 |
fail("Parse failed: " + e.getMessage()); |
| 36 |
} |
| 37 |
checkUrl(url, "http", "", 80, "file1", null, null); |
| 38 |
|
| 39 |
try { |
| 40 |
url = new GlobusURL("http://host1:124"); |
| 41 |
} catch (Exception e) { |
| 42 |
fail("Parse failed: " + e.getMessage()); |
| 43 |
} |
| 44 |
checkUrl(url, "http", "host1", 124, null, null, null); |
| 45 |
|
| 46 |
try { |
| 47 |
url = new GlobusURL("http://host1:124/"); |
| 48 |
} catch (Exception e) { |
| 49 |
fail("Parse failed: " + e.getMessage()); |
| 50 |
} |
| 51 |
checkUrl(url, "http", "host1", 124, null, null, null); |
| 52 |
|
| 53 |
try { |
| 54 |
url = new GlobusURL("http://host1/mis/ptys"); |
| 55 |
} catch (Exception e) { |
| 56 |
fail("Parse failed: " + e.getMessage()); |
| 57 |
} |
| 58 |
checkUrl(url, "http", "host1", 80, "mis/ptys", null, null); |
| 59 |
|
| 60 |
try { |
| 61 |
url = new GlobusURL("http://usr@host1"); |
| 62 |
} catch (Exception e) { |
| 63 |
fail("Parse failed: " + e.getMessage()); |
| 64 |
} |
| 65 |
checkUrl(url, "http", "host1", 80, null, "usr", null); |
| 66 |
|
| 67 |
try { |
| 68 |
url = new GlobusURL("http://usr:@host1:124"); |
| 69 |
} catch (Exception e) { |
| 70 |
fail("Parse failed: " + e.getMessage()); |
| 71 |
} |
| 72 |
checkUrl(url, "http", "host1", 124, null, "usr", ""); |
| 73 |
|
| 74 |
try { |
| 75 |
url = new GlobusURL("http://usr:pwd@host1:124//mis"); |
| 76 |
} catch (Exception e) { |
| 77 |
fail("Parse failed: " + e.getMessage()); |
| 78 |
} |
| 79 |
checkUrl(url, "http", "host1", 124, "/mis", "usr", "pwd"); |
| 80 |
|
| 81 |
try { |
| 82 |
url = new GlobusURL(" gsiftp://localhost/foo"); |
| 83 |
} catch (Exception e) { |
| 84 |
fail("Parse failed: " + e.getMessage()); |
| 85 |
} |
| 86 |
checkUrl(url, "gsiftp", "localhost", 2811, "foo", null, null); |
| 87 |
} |
| 88 |
|
| 89 |
private void checkUrl(GlobusURL url, String protocol, String host, int port, String urlPath, String user, String pwd) { |
| 90 |
assertEquals("protocol", protocol, url.getProtocol()); |
| 91 |
assertEquals("host", host, url.getHost()); |
| 92 |
assertEquals("port", port, url.getPort()); |
| 93 |
assertEquals("urlpath", urlPath, url.getPath()); |
| 94 |
assertEquals("user", user, url.getUser()); |
| 95 |
assertEquals("pwd", pwd, url.getPwd()); |
| 96 |
} |
| 97 |
|
| 98 |
public void testParseBad() { |
| 99 |
try { |
| 100 |
new GlobusURL("http:/host1"); |
| 101 |
fail("The url was parsed ok!"); |
| 102 |
} catch (Exception e) { |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
public void testEquals1() { |
| 107 |
GlobusURL url, url2, url3; |
| 108 |
|
| 109 |
url = url2 = url3 = null; |
| 110 |
try { |
| 111 |
url = new GlobusURL("http://host1:123/jarek"); |
| 112 |
url2 = new GlobusURL("http://host1:123/jarek"); |
| 113 |
url3 = new GlobusURL("ftp://host1:123/jarek"); |
| 114 |
} catch (Exception e) { |
| 115 |
} |
| 116 |
|
| 117 |
assertTrue("c1", url.equals("HTTP://host1:123/jarek")); |
| 118 |
assertTrue("c2", !url.equals("HTTP://host1:123/Jarek")); |
| 119 |
assertTrue("c3", url.equals(url)); |
| 120 |
assertTrue("c4", url.equals(url2)); |
| 121 |
assertTrue("c5", !url.equals(url3)); |
| 122 |
} |
| 123 |
|
| 124 |
public void testIPv6Address() { |
| 125 |
GlobusURL url = null; |
| 126 |
|
| 127 |
try { |
| 128 |
url = new GlobusURL("http://[1080:0:0:0:8:800:200C:417A]/index.html"); |
| 129 |
} catch (Exception e) { |
| 130 |
fail("Parse failed: " + e.getMessage()); |
| 131 |
} |
| 132 |
checkUrl(url, "http", "[1080:0:0:0:8:800:200C:417A]", 80, "index.html", null, null); |
| 133 |
|
| 134 |
try { |
| 135 |
url = new GlobusURL("hdl://[3ffe:2a00:100:7031::1]:123"); |
| 136 |
} catch (Exception e) { |
| 137 |
fail("Parse failed: " + e.getMessage()); |
| 138 |
} |
| 139 |
checkUrl(url, "hdl", "[3ffe:2a00:100:7031::1]", 123, null, null, null); |
| 140 |
|
| 141 |
try { |
| 142 |
url = new GlobusURL("p1://gawor:123@[3ffe:2a00:100:7031::1]:123/testFile"); |
| 143 |
} catch (Exception e) { |
| 144 |
fail("Parse failed: " + e.getMessage()); |
| 145 |
} |
| 146 |
checkUrl(url, "p1", "[3ffe:2a00:100:7031::1]", 123, "testFile", "gawor", "123"); |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
} |