URLConnectionTest.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.io.IOException; 25import java.net.MalformedURLException; 26import java.net.URL; 27import java.net.URLConnection; 28 29import junit.framework.TestCase; 30 31@TestTargetClass(URLConnection.class) 32public class URLConnectionTest extends TestCase { 33 34 /** 35 * @tests java.net.URLConnection#addRequestProperty(String, String) 36 */ 37@TestInfo( 38 level = TestLevel.PARTIAL, 39 purpose = "Exceptions checked only.", 40 targets = { 41 @TestTarget( 42 methodName = "addRequestProperty", 43 methodArgs = {String.class, String.class} 44 ) 45 }) 46 public void test_addRequestProperty() throws MalformedURLException, 47 IOException { 48 49 MockURLConnection u = new MockURLConnection(new URL( 50 "http://www.apache.org")); 51 try { 52 // Regression for HARMONY-604 53 u.addRequestProperty(null, "someValue"); 54 fail("Expected NullPointerException"); 55 } catch (NullPointerException e) { 56 // expected 57 } 58 59 u.connect(); 60 try { 61 // state of connection is checked first 62 // so no NPE in case of null 'field' param 63 u.addRequestProperty(null, "someValue"); 64 fail("Expected IllegalStateException"); 65 } catch (IllegalStateException e) { 66 // expected 67 } 68 } 69 70 /** 71 * @tests java.net.URLConnection#setRequestProperty(String, String) 72 */ 73@TestInfo( 74 level = TestLevel.PARTIAL, 75 purpose = "Exceptions checked only.", 76 targets = { 77 @TestTarget( 78 methodName = "setRequestProperty", 79 methodArgs = {String.class, String.class} 80 ) 81 }) 82 public void test_setRequestProperty() throws MalformedURLException, 83 IOException { 84 85 MockURLConnection u = new MockURLConnection(new URL( 86 "http://www.apache.org")); 87 try { 88 u.setRequestProperty(null, "someValue"); 89 fail("Expected NullPointerException"); 90 } catch (NullPointerException e) { 91 // expected 92 } 93 94 u.connect(); 95 try { 96 // state of connection is checked first 97 // so no NPE in case of null 'field' param 98 u.setRequestProperty(null, "someValue"); 99 fail("Expected IllegalStateException"); 100 } catch (IllegalStateException e) { 101 // expected 102 } 103 } 104 105 /** 106 * @tests java.net.URLConnection#setUseCaches(boolean) 107 */ 108@TestInfo( 109 level = TestLevel.PARTIAL, 110 purpose = "Exceptions checked only.", 111 targets = { 112 @TestTarget( 113 methodName = "setUseCaches", 114 methodArgs = {boolean.class} 115 ) 116 }) 117 public void test_setUseCachesZ() throws MalformedURLException, IOException { 118 119 // Regression for HARMONY-71 120 MockURLConnection u = new MockURLConnection(new URL( 121 "http://www.apache.org")); 122 u.connect(); 123 try { 124 u.setUseCaches(true); 125 fail("Assert 0: expected an IllegalStateException"); 126 } catch (IllegalStateException e) { 127 // expected 128 } 129 } 130 131 /** 132 * @tests java.net.URLConnection#setAllowUserInteraction(boolean) 133 */ 134@TestInfo( 135 level = TestLevel.PARTIAL, 136 purpose = "Exceptions checked only.", 137 targets = { 138 @TestTarget( 139 methodName = "setAllowUserInteraction", 140 methodArgs = {boolean.class} 141 ) 142 }) 143 public void test_setAllowUserInteractionZ() throws MalformedURLException, 144 IOException { 145 146 // Regression for HARMONY-72 147 MockURLConnection u = new MockURLConnection(new URL( 148 "http://www.apache.org")); 149 u.connect(); 150 try { 151 u.setAllowUserInteraction(false); 152 fail("Assert 0: expected an IllegalStateException"); 153 } catch (IllegalStateException e) { 154 // expected 155 } 156 } 157 158 static class MockURLConnection extends URLConnection { 159 160 public MockURLConnection(URL url) { 161 super(url); 162 } 163 164 @Override 165 public void connect() { 166 connected = true; 167 } 168 } 169}