URITest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package org.apache.harmony.luni.tests.java.net; 18 19import dalvik.annotation.TestTargetClass; 20import dalvik.annotation.TestInfo; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTarget; 23 24import java.net.URI; 25import java.net.URISyntaxException; 26 27import junit.framework.TestCase; 28 29@TestTargetClass(URI.class) 30public class URITest extends TestCase { 31 /** 32 * @tests java.net.URI(java.lang.String) 33 */ 34@TestInfo( 35 level = TestLevel.PARTIAL, 36 purpose = "NullPointerException checking missed.", 37 targets = { 38 @TestTarget( 39 methodName = "URI", 40 methodArgs = {String.class} 41 ) 42 }) 43 public void test_ConstructorLjava_lang_String() throws URISyntaxException { 44 // Regression test for HARMONY-23 45 try { 46 new URI("%3"); 47 fail("Assert 0: URI constructor failed to throw exception on invalid input."); 48 } catch (URISyntaxException e) { 49 // Expected 50 assertEquals("Assert 1: Wrong index in URISyntaxException.", 0, e.getIndex()); 51 } 52 53 // Regression test for HARMONY-25 54 // if port value is negative, the authority should be considered registry-based. 55 URI uri = new URI("http://host:-8096/path/index.html"); 56 assertEquals("Assert 2: returned wrong port value,", -1, uri.getPort()); 57 assertNull("Assert 3: returned wrong host value,", uri.getHost()); 58 try { 59 uri.parseServerAuthority(); 60 fail("Assert 4: Expected URISyntaxException"); 61 } catch (URISyntaxException e){ 62 // Expected 63 } 64 65 uri = new URI("http","//myhost:-8096", null); 66 assertEquals("Assert 5: returned wrong port value,", -1, uri.getPort()); 67 assertNull("Assert 6: returned wrong host value,", uri.getHost()); 68 try { 69 uri.parseServerAuthority(); 70 fail("Assert 7: Expected URISyntaxException"); 71 } catch (URISyntaxException e){ 72 // Expected 73 } 74 } 75 76 /** 77 * @tests java.net.URI(java.lang.String, java.lang.String, java.lang.String) 78 */ 79@TestInfo( 80 level = TestLevel.PARTIAL, 81 purpose = "Exceptions checked only.", 82 targets = { 83 @TestTarget( 84 methodName = "URI", 85 methodArgs = {String.class, String.class, String.class} 86 ) 87 }) 88 public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_lang_String() { 89 // scheme can not be empty string 90 try { 91 new URI("","//authority/path", "fragment"); 92 fail ("Assert 0: Expected URISyntaxException with empty URI scheme"); 93 } catch(URISyntaxException e) { 94 // Expected 95 assertEquals("Assert 1: Wrong index in URISyntaxException.", 0, e.getIndex()); 96 } 97 } 98 99 /** 100 * @tests java.net.URI#relativize(java.net.URI) 101 */ 102@TestInfo( 103 level = TestLevel.PARTIAL, 104 purpose = "NullPointerException checking missed.", 105 targets = { 106 @TestTarget( 107 methodName = "relativize", 108 methodArgs = {URI.class} 109 ) 110 }) 111 public void test_relativizeLjava_net_URI() throws URISyntaxException{ 112 URI a = new URI("http://host/dir"); 113 URI b = new URI("http://host/dir/file?query"); 114 assertEquals("Assert 0: URI relativized incorrectly,", 115 new URI("file?query"), a.relativize(b)); 116 117 // One URI with empty host 118 a = new URI("file:///~/first"); 119 b = new URI("file://tools/~/first"); 120 assertEquals("Assert 1: URI relativized incorrectly,", 121 new URI("file://tools/~/first"), a.relativize(b)); 122 assertEquals("Assert 2: URI relativized incorrectly,", 123 new URI("file:///~/first"), b.relativize(a)); 124 125 // Both URIs with empty hosts 126 b = new URI("file:///~/second"); 127 assertEquals("Assert 3: URI relativized incorrectly,", 128 new URI("file:///~/second"), a.relativize(b)); 129 assertEquals("Assert 4: URI relativized incorrectly,", 130 new URI("file:///~/first"), b.relativize(a)); 131 } 132 133@TestInfo( 134 level = TestLevel.PARTIAL, 135 purpose = "NullPointerException checking missed.", 136 targets = { 137 @TestTarget( 138 methodName = "relativize", 139 methodArgs = {URI.class} 140 ) 141 }) 142 public void test_relativizeBasedOneEclipseCoreResources() throws URISyntaxException { 143 URI one = new URI("file:/C:/test/ws"); 144 URI two = new URI("file:/C:/test/ws"); 145 146 URI empty = new URI(""); 147 assertEquals(empty, one.relativize(two)); 148 149 one = new URI("file:/C:/test/ws"); 150 two = new URI("file:/C:/test/ws/p1"); 151 URI result = new URI("p1"); 152 assertEquals(result, one.relativize(two)); 153 154 one = new URI("file:/C:/test/ws/"); 155 assertEquals(result, one.relativize(two)); 156 } 157 158 /** 159 * @tests java.net.URI#compareTo(java.net.URI) 160 */ 161@TestInfo( 162 level = TestLevel.PARTIAL, 163 purpose = "ClassCastException checking missed.", 164 targets = { 165 @TestTarget( 166 methodName = "compareTo", 167 methodArgs = {URI.class} 168 ) 169 }) 170 public void test_compareToLjava_net_URI() throws URISyntaxException{ 171 URI uri1, uri2; 172 173 // URIs whose host names have different casing 174 uri1 = new URI("http://MixedCaseHost/path/resource"); 175 uri2 = new URI("http://mixedcasehost/path/resource"); 176 assertEquals("Assert 0: host name equality failure", 0, uri1.compareTo(uri2)); 177 assertEquals("Assert 1: host name equality failure", 0, uri1.compareTo(uri2)); 178 179 // URIs with one undefined component (port) 180 uri1 = new URI("http://anyhost:80/path/resource"); 181 uri2 = new URI("http://anyhost/path/resource"); 182 assertTrue("Assert 2: comparison failure", uri1.compareTo(uri2) > 0); 183 assertTrue("Assert 3: comparison failure", uri2.compareTo(uri1) < 0); 184 185 // URIs with one undefined component (user-info) 186 uri1 = new URI("http://user-info@anyhost/path/resource"); 187 uri2 = new URI("http://anyhost/path/resource"); 188 assertTrue("Assert 4: comparison failure", uri1.compareTo(uri2) > 0); 189 assertTrue("Assert 5: comparison failure", uri2.compareTo(uri1) < 0); 190 } 191} 192