• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  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 org.apache.harmony.luni.tests.java.net;
19 
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.File;
23 import java.io.FilePermission;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.net.FileNameMap;
27 import java.net.HttpURLConnection;
28 import java.net.JarURLConnection;
29 import java.net.MalformedURLException;
30 import java.net.SocketPermission;
31 import java.net.URL;
32 import java.net.URLConnection;
33 import java.net.URLStreamHandler;
34 import java.security.Permission;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Calendar;
38 import java.util.GregorianCalendar;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.TimeZone;
42 import tests.support.Support_Configuration;
43 import tests.support.resource.Support_Resources;
44 
45 public class URLConnectionTest extends junit.framework.TestCase {
46 
47     static class MockURLConnection extends URLConnection {
48 
MockURLConnection(URL url)49         public MockURLConnection(URL url) {
50             super(url);
51         }
52 
53         @Override
connect()54         public void connect() {
55             connected = true;
56         }
57     }
58 
59     static class NewHandler extends URLStreamHandler {
openConnection(URL u)60         protected URLConnection openConnection(URL u) throws IOException {
61             return new HttpURLConnection(u) {
62                 @Override
63                 public void connect() throws IOException {
64                     connected = true;
65                 }
66 
67                 @Override
68                 public void disconnect() {
69                     // do nothing
70                 }
71 
72                 @Override
73                 public boolean usingProxy() {
74                     return false;
75                 }
76             };
77         }
78     }
79 
80     static String getContentType(String fileName) throws IOException {
81         String resourceName = "org/apache/harmony/luni/tests/" + fileName;
82         URL url = ClassLoader.getSystemClassLoader().getResource(resourceName);
83         assertNotNull("Cannot find test resource " + resourceName, url);
84         return url.openConnection().getContentType();
85     }
86 
87     URL url;
88     URLConnection uc;
89 
90     protected void setUp() throws Exception {
91         url = new URL("http://localhost/");
92         uc = url.openConnection();
93     }
94 
95     protected void tearDown() {
96         ((HttpURLConnection) uc).disconnect();
97     }
98 
99     /**
100      * @tests java.net.URLConnection#addRequestProperty(String, String)
101      */
102     public void test_addRequestProperty() throws MalformedURLException,
103             IOException {
104 
105         MockURLConnection u = new MockURLConnection(new URL(
106                 "http://www.apache.org"));
107         try {
108             // Regression for HARMONY-604
109             u.addRequestProperty(null, "someValue");
110             fail("Expected NullPointerException");
111         } catch (NullPointerException e) {
112             // expected
113         }
114 
115         u.addRequestProperty("key", "value");
116         u.addRequestProperty("key", "value2");
117         assertEquals("value2", u.getRequestProperty("key"));
118         ArrayList list = new ArrayList();
119         list.add("value2");
120         list.add("value");
121 
122         Map<String, List<String>> propertyMap = u.getRequestProperties();
123         // Check this map is unmodifiable
124         try {
125             propertyMap.put("test", null);
126             fail("Map returned by URLConnection.getRequestProperties() should be unmodifiable");
127         } catch (UnsupportedOperationException e) {
128             // Expected
129         }
130 
131         List<String> valuesList = propertyMap.get("key");
132         // Check this list is also unmodifiable
133         try {
134             valuesList.add("test");
135             fail("List entries in the map returned by URLConnection.getRequestProperties() should be unmodifiable");
136         } catch (UnsupportedOperationException e) {
137             // Expected
138         }
139         assertEquals(list, valuesList);
140 
141         u.connect();
142         try {
143             // state of connection is checked first
144             // so no NPE in case of null 'field' param
145             u.addRequestProperty(null, "someValue");
146             fail("Expected IllegalStateException");
147         } catch (IllegalStateException e) {
148             // expected
149         }
150     }
151 
152     /**
153      * @tests java.net.URLConnection#addRequestProperty(java.lang.String, java.lang.String)
154      */
155     public void test_addRequestPropertyLjava_lang_StringLjava_lang_String()
156             throws IOException {
157         uc.setRequestProperty("prop", "yo");
158         uc.setRequestProperty("prop", "yo2");
159         assertEquals("yo2", uc.getRequestProperty("prop"));
160         Map<String, List<String>> map = uc.getRequestProperties();
161         List<String> props = uc.getRequestProperties().get("prop");
162         assertEquals(1, props.size());
163 
164         try {
165             // the map should be unmodifiable
166             map.put("hi", Arrays.asList(new String[] { "bye" }));
167             fail("could modify map");
168         } catch (UnsupportedOperationException e) {
169             // Expected
170         }
171         try {
172             // the list should be unmodifiable
173             props.add("hi");
174             fail("could modify list");
175         } catch (UnsupportedOperationException e) {
176             // Expected
177         }
178 
179         File resources = Support_Resources.createTempFolder();
180         Support_Resources.copyFile(resources, null, "hyts_att.jar");
181         URL fUrl1 = new URL("jar:file:" + resources.getPath()
182                 + "/hyts_att.jar!/");
183         JarURLConnection con1 = (JarURLConnection) fUrl1.openConnection();
184         map = con1.getRequestProperties();
185         assertNotNull(map);
186         assertEquals(0, map.size());
187         try {
188             // the map should be unmodifiable
189             map.put("hi", Arrays.asList(new String[] { "bye" }));
190             fail();
191         } catch (UnsupportedOperationException e) {
192             // Expected
193         }
194     }
195 
196     /**
197      * @tests java.net.URLConnection#getAllowUserInteraction()
198      */
199     public void test_getAllowUserInteraction() {
200         uc.setAllowUserInteraction(false);
201         assertFalse("getAllowUserInteraction should have returned false", uc
202                 .getAllowUserInteraction());
203 
204         uc.setAllowUserInteraction(true);
205         assertTrue("getAllowUserInteraction should have returned true", uc
206                 .getAllowUserInteraction());
207     }
208 
209     /**
210      * @tests java.net.URLConnection#getContentEncoding()
211      */
212     public void test_getContentEncoding() {
213         // should not be known for a file
214         assertNull("getContentEncoding failed: " + uc.getContentEncoding(), uc
215                 .getContentEncoding());
216     }
217 
218     /**
219      * @tests java.net.URLConnection#getContentType()
220      */
221     public void test_getContentType_regression() throws IOException {
222         // Regression for HARMONY-4699
223         assertEquals("application/rtf", getContentType("test.rtf"));
224         assertEquals("text/plain", getContentType("test.java"));
225         // RI would return "content/unknown"
226         assertEquals("application/msword", getContentType("test.doc"));
227         assertEquals("text/html", getContentType("test.htx"));
228         assertEquals("application/xml", getContentType("test.xml"));
229         assertEquals("text/plain", getContentType("."));
230     }
231 
232     /**
233      * @tests java.net.URLConnection#getDate()
234      */
235     public void test_getDate() {
236         // should be greater than 930000000000L which represents the past
237         if (uc.getDate() == 0) {
238             System.out
239                     .println("WARNING: server does not support 'Date', in test_getDate");
240         } else {
241             assertTrue("getDate gave wrong date: " + uc.getDate(),
242                     uc.getDate() > 930000000000L);
243         }
244     }
245 
246     /**
247      * @tests java.net.URLConnection#getDefaultAllowUserInteraction()
248      */
249     public void test_getDefaultAllowUserInteraction() {
250         boolean oldSetting = URLConnection.getDefaultAllowUserInteraction();
251 
252         URLConnection.setDefaultAllowUserInteraction(false);
253         assertFalse(
254                 "getDefaultAllowUserInteraction should have returned false",
255                 URLConnection.getDefaultAllowUserInteraction());
256 
257         URLConnection.setDefaultAllowUserInteraction(true);
258         assertTrue("getDefaultAllowUserInteraction should have returned true",
259                 URLConnection.getDefaultAllowUserInteraction());
260 
261         URLConnection.setDefaultAllowUserInteraction(oldSetting);
262     }
263 
264     /**
265      * @tests java.net.URLConnection#getDefaultRequestProperty(java.lang.String)
266      */
267     @SuppressWarnings("deprecation")
268     public void test_getDefaultRequestPropertyLjava_lang_String() {
269         URLConnection.setDefaultRequestProperty("Shmoo", "Blah");
270         assertNull("setDefaultRequestProperty should have returned: null",
271                 URLConnection.getDefaultRequestProperty("Shmoo"));
272 
273         URLConnection.setDefaultRequestProperty("Shmoo", "Boom");
274         assertNull("setDefaultRequestProperty should have returned: null",
275                 URLConnection.getDefaultRequestProperty("Shmoo"));
276 
277         assertNull("setDefaultRequestProperty should have returned: null",
278                 URLConnection.getDefaultRequestProperty("Kapow"));
279 
280         URLConnection.setDefaultRequestProperty("Shmoo", null);
281     }
282 
283     /**
284      * @tests java.net.URLConnection#getDefaultUseCaches()
285      */
286     public void test_getDefaultUseCaches() {
287         boolean oldSetting = uc.getDefaultUseCaches();
288 
289         uc.setDefaultUseCaches(false);
290         assertFalse("getDefaultUseCaches should have returned false", uc
291                 .getDefaultUseCaches());
292 
293         uc.setDefaultUseCaches(true);
294         assertTrue("getDefaultUseCaches should have returned true", uc
295                 .getDefaultUseCaches());
296 
297         uc.setDefaultUseCaches(oldSetting);
298     }
299 
300     /**
301      * @tests java.net.URLConnection#getDoInput()
302      */
303     public void test_getDoInput() {
304         assertTrue("Should be set to true by default", uc.getDoInput());
305 
306         uc.setDoInput(true);
307         assertTrue("Should have been set to true", uc.getDoInput());
308 
309         uc.setDoInput(false);
310         assertFalse("Should have been set to false", uc.getDoInput());
311     }
312 
313     /**
314      * @tests java.net.URLConnection#getDoOutput()
315      */
316     public void test_getDoOutput() {
317         assertFalse("Should be set to false by default", uc.getDoOutput());
318 
319         uc.setDoOutput(true);
320         assertTrue("Should have been set to true", uc.getDoOutput());
321 
322         uc.setDoOutput(false);
323         assertFalse("Should have been set to false", uc.getDoOutput());
324     }
325 
326     /**
327      * @tests java.net.URLConnection#getExpiration()
328      */
329     public void test_getExpiration() {
330         // should be unknown
331         assertEquals("getExpiration returned wrong expiration", 0, uc
332                 .getExpiration());
333     }
334 
335     /**
336      * @tests java.net.URLConnection#getFileNameMap()
337      */
338     public void test_getFileNameMap() {
339         // Tests for the standard MIME types -- users may override these
340         // in their JRE
341         FileNameMap map = URLConnection.getFileNameMap();
342 
343         // These types are defaulted
344         assertEquals("text/html", map.getContentTypeFor(".htm"));
345         assertEquals("text/html", map.getContentTypeFor(".html"));
346         assertEquals("text/plain", map.getContentTypeFor(".text"));
347         assertEquals("text/plain", map.getContentTypeFor(".txt"));
348 
349         // These types come from the properties file
350         assertEquals("application/pdf", map.getContentTypeFor(".pdf"));
351         assertEquals("application/zip", map.getContentTypeFor(".zip"));
352 
353         URLConnection.setFileNameMap(new FileNameMap() {
354             public String getContentTypeFor(String fileName) {
355                 return "Spam!";
356             }
357         });
358         try {
359             assertEquals("Incorrect FileNameMap returned", "Spam!",
360                     URLConnection.getFileNameMap().getContentTypeFor(null));
361         } finally {
362             // unset the map so other tests don't fail
363             URLConnection.setFileNameMap(null);
364         }
365         // RI fails since it does not support fileName that does not begin with
366         // '.'
367         assertEquals("image/gif", map.getContentTypeFor("gif"));
368     }
369 
370     /**
371      * @tests java.net.URLConnection#getHeaderFieldDate(java.lang.String, long)
372      */
373     public void test_getHeaderFieldDateLjava_lang_StringJ() {
374 
375         if (uc.getHeaderFieldDate("Date", 22L) == 22L) {
376             System.out
377                     .println("WARNING: Server does not support 'Date', test_getHeaderFieldDateLjava_lang_StringJ not run");
378             return;
379         }
380         assertTrue("Wrong value returned: "
381                 + uc.getHeaderFieldDate("Date", 22L), uc.getHeaderFieldDate(
382                 "Date", 22L) > 930000000000L);
383 
384         long time = uc.getHeaderFieldDate("Last-Modified", 0);
385         assertEquals("Wrong date: ", time,
386                 Support_Configuration.URLConnectionLastModified);
387     }
388 
389     /**
390      * @tests java.net.URLConnection#getHeaderField(java.lang.String)
391      */
392     public void test_getHeaderFieldLjava_lang_String() {
393         String hf;
394         hf = uc.getHeaderField("Content-Encoding");
395         if (hf != null) {
396             assertNull(
397                     "Wrong value returned for header field 'Content-Encoding': "
398                             + hf, hf);
399         }
400         hf = uc.getHeaderField("Content-Length");
401         if (hf != null) {
402             assertEquals(
403                     "Wrong value returned for header field 'Content-Length': ",
404                     "25", hf);
405         }
406         hf = uc.getHeaderField("Content-Type");
407         if (hf != null) {
408             assertTrue("Wrong value returned for header field 'Content-Type': "
409                     + hf, hf.contains("text/html"));
410         }
411         hf = uc.getHeaderField("content-type");
412         if (hf != null) {
413             assertTrue("Wrong value returned for header field 'content-type': "
414                     + hf, hf.contains("text/html"));
415         }
416         hf = uc.getHeaderField("Date");
417         if (hf != null) {
418             assertTrue("Wrong value returned for header field 'Date': " + hf,
419                     Integer.parseInt(hf.substring(hf.length() - 17,
420                             hf.length() - 13)) >= 1999);
421         }
422         hf = uc.getHeaderField("Expires");
423         if (hf != null) {
424             assertNull(
425                     "Wrong value returned for header field 'Expires': " + hf,
426                     hf);
427         }
428         hf = uc.getHeaderField("SERVER");
429         if (hf != null) {
430             assertTrue("Wrong value returned for header field 'SERVER': " + hf
431                     + " (expected " + Support_Configuration.HomeAddressSoftware
432                     + ")", hf.equals(Support_Configuration.HomeAddressSoftware));
433         }
434         hf = uc.getHeaderField("Last-Modified");
435         if (hf != null) {
436             assertTrue(
437                     "Wrong value returned for header field 'Last-Modified': "
438                             + hf,
439                     hf
440                             .equals(Support_Configuration.URLConnectionLastModifiedString));
441         }
442         hf = uc.getHeaderField("accept-ranges");
443         if (hf != null) {
444             assertTrue(
445                     "Wrong value returned for header field 'accept-ranges': "
446                             + hf, hf.equals("bytes"));
447         }
448         hf = uc.getHeaderField("DoesNotExist");
449         if (hf != null) {
450             assertNull("Wrong value returned for header field 'DoesNotExist': "
451                     + hf, hf);
452         }
453     }
454 
455     /**
456      * @tests java.net.URLConnection#getIfModifiedSince()
457      */
458     public void test_getIfModifiedSince() {
459         uc.setIfModifiedSince(200);
460         assertEquals("Returned wrong ifModifiedSince value", 200, uc
461                 .getIfModifiedSince());
462     }
463 
464     /**
465      * @tests java.net.URLConnection#getLastModified()
466      */
467     public void test_getLastModified() {
468         if (uc.getLastModified() == 0) {
469             System.out
470                     .println("WARNING: Server does not support 'Last-Modified', test_getLastModified() not run");
471             return;
472         }
473         assertTrue(
474                 "Returned wrong getLastModified value.  Wanted: "
475                         + Support_Configuration.URLConnectionLastModified
476                         + " got: " + uc.getLastModified(),
477                 uc.getLastModified() == Support_Configuration.URLConnectionLastModified);
478     }
479 
480     /**
481      * @tests java.net.URLConnection#getRequestProperties()
482      */
483     public void test_getRequestProperties() {
484         uc.setRequestProperty("whatever", "you like");
485         Map headers = uc.getRequestProperties();
486 
487         // content-length should always appear
488         List header = (List) headers.get("whatever");
489         assertNotNull(header);
490 
491         assertEquals("you like", header.get(0));
492 
493         assertTrue(headers.size() >= 1);
494 
495         try {
496             // the map should be unmodifiable
497             headers.put("hi", "bye");
498             fail();
499         } catch (UnsupportedOperationException e) {
500         }
501         try {
502             // the list should be unmodifiable
503             header.add("hi");
504             fail();
505         } catch (UnsupportedOperationException e) {
506         }
507 
508     }
509 
510     /**
511      * @tests java.net.URLConnection#getRequestProperties()
512      */
513     public void test_getRequestProperties_Exception() throws IOException {
514         URL url = new URL("http", "test", 80, "index.html", new NewHandler());
515         URLConnection urlCon = url.openConnection();
516         urlCon.connect();
517 
518         try {
519             urlCon.getRequestProperties();
520             fail("should throw IllegalStateException");
521         } catch (IllegalStateException e) {
522             // expected
523         }
524     }
525 
526     /**
527      * @tests java.net.URLConnection#getRequestProperty(java.lang.String)
528      */
529     public void test_getRequestProperty_LString_Exception() throws IOException {
530         URL url = new URL("http", "test", 80, "index.html", new NewHandler());
531         URLConnection urlCon = url.openConnection();
532         urlCon.setRequestProperty("test", "testProperty");
533         assertEquals("testProperty", urlCon.getRequestProperty("test"));
534 
535         urlCon.connect();
536         try {
537             urlCon.getRequestProperty("test");
538             fail("should throw IllegalStateException");
539         } catch (IllegalStateException e) {
540             // expected
541         }
542     }
543 
544     /**
545      * @tests java.net.URLConnection#getRequestProperty(java.lang.String)
546      */
547     public void test_getRequestPropertyLjava_lang_String() {
548         uc.setRequestProperty("Yo", "yo");
549         assertTrue("Wrong property returned: " + uc.getRequestProperty("Yo"),
550                 uc.getRequestProperty("Yo").equals("yo"));
551         assertNull("Wrong property returned: " + uc.getRequestProperty("No"),
552                 uc.getRequestProperty("No"));
553     }
554 
555     /**
556      * @tests java.net.URLConnection#getURL()
557      */
558     public void test_getURL() {
559         assertTrue("Incorrect URL returned", uc.getURL().equals(url));
560     }
561 
562     /**
563      * @tests java.net.URLConnection#getUseCaches()
564      */
565     public void test_getUseCaches() {
566         uc.setUseCaches(false);
567         assertTrue("getUseCaches should have returned false", !uc
568                 .getUseCaches());
569         uc.setUseCaches(true);
570         assertTrue("getUseCaches should have returned true", uc.getUseCaches());
571     }
572 
573     /**
574      * @tests java.net.URLConnection#guessContentTypeFromStream(java.io.InputStream)
575      */
576     public void test_guessContentTypeFromStreamLjava_io_InputStream()
577             throws IOException {
578         String[] headers = new String[] { "<html>", "<head>", " <head ",
579                 "<body", "<BODY ", "<!DOCTYPE html", "<?xml " };
580         String[] expected = new String[] { "text/html", "text/html",
581                 "text/html", "text/html", "text/html", "text/html",
582                 "application/xml" };
583 
584         String[] encodings = new String[] { "ASCII", "UTF-8", "UTF-16BE",
585                 "UTF-16LE", "UTF-32BE", "UTF-32LE" };
586         for (int i = 0; i < headers.length; i++) {
587             for (String enc : encodings) {
588                 InputStream is = new ByteArrayInputStream(toBOMBytes(
589                         headers[i], enc));
590                 String mime = URLConnection.guessContentTypeFromStream(is);
591                 assertEquals("checking " + headers[i] + " with " + enc,
592                         expected[i], mime);
593             }
594         }
595 
596         // Try simple case
597         try {
598             URLConnection.guessContentTypeFromStream(null);
599             fail("should throw NullPointerException");
600         } catch (NullPointerException e) {
601             // expected
602         }
603 
604         // Test magic bytes
605         byte[][] bytes = new byte[][] { { 'P', 'K' }, { 'G', 'I' } };
606         expected = new String[] { "application/zip", "image/gif" };
607 
608         for (int i = 0; i < bytes.length; i++) {
609             InputStream is = new ByteArrayInputStream(bytes[i]);
610             assertEquals(expected[i], URLConnection
611                     .guessContentTypeFromStream(is));
612         }
613 
614         ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
615         assertNull(URLConnection.guessContentTypeFromStream(bais));
616         bais.close();
617     }
618 
619     /**
620      * @tests java.net.URLConnection#setAllowUserInteraction(boolean)
621      */
622     public void test_setAllowUserInteractionZ() throws MalformedURLException {
623         // Regression for HARMONY-72
624         MockURLConnection u = new MockURLConnection(new URL(
625                 "http://www.apache.org"));
626         u.connect();
627         try {
628             u.setAllowUserInteraction(false);
629             fail("Assert 0: expected an IllegalStateException");
630         } catch (IllegalStateException e) {
631             // expected
632         }
633 
634     }
635 
636     /**
637      * @tests java.net.URLConnection#setConnectTimeout(int)
638      */
639     public void test_setConnectTimeoutI() throws Exception {
640         URLConnection uc = new URL("http://localhost").openConnection();
641         assertEquals(0, uc.getConnectTimeout());
642         uc.setConnectTimeout(0);
643         assertEquals(0, uc.getConnectTimeout());
644         try {
645             uc.setConnectTimeout(-100);
646             fail("should throw IllegalArgumentException");
647         } catch (IllegalArgumentException e) {
648             // correct
649         }
650         assertEquals(0, uc.getConnectTimeout());
651         uc.setConnectTimeout(100);
652         assertEquals(100, uc.getConnectTimeout());
653         try {
654             uc.setConnectTimeout(-1);
655             fail("should throw IllegalArgumentException");
656         } catch (IllegalArgumentException e) {
657             // correct
658         }
659         assertEquals(100, uc.getConnectTimeout());
660     }
661 
662     /**
663      * @tests java.net.URLConnection#setDefaultAllowUserInteraction(boolean)
664      */
665     public void test_setDefaultAllowUserInteractionZ() {
666         assertTrue("Used to test", true);
667     }
668 
669     /**
670      * @tests java.net.URLConnection#setDefaultRequestProperty(java.lang.String,
671      *java.lang.String)
672      */
673     public void test_setDefaultRequestPropertyLjava_lang_StringLjava_lang_String() {
674         assertTrue("Used to test", true);
675     }
676 
677     /**
678      * @tests java.net.URLConnection#setDefaultUseCaches(boolean)
679      */
680     public void test_setDefaultUseCachesZ() {
681         assertTrue("Used to test", true);
682     }
683 
684     /**
685      * @throws IOException
686      * @tests java.net.URLConnection#setFileNameMap(java.net.FileNameMap)
687      */
688     public void test_setFileNameMapLjava_net_FileNameMap() throws IOException {
689         // nothing happens if set null
690         URLConnection.setFileNameMap(null);
691         // take no effect
692         assertNotNull(URLConnection.getFileNameMap());
693     }
694 
695     /**
696      * @tests java.net.URLConnection#setIfModifiedSince(long)
697      */
698     public void test_setIfModifiedSinceJ() throws IOException {
699         URL url = new URL("http://localhost:8080/");
700         URLConnection connection = url.openConnection();
701         Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
702         cal.clear();
703         cal.set(2000, Calendar.MARCH, 5);
704 
705         long sinceTime = cal.getTime().getTime();
706         connection.setIfModifiedSince(sinceTime);
707         assertEquals("Wrong date set", sinceTime, connection
708                 .getIfModifiedSince());
709 
710     }
711 
712     /**
713      * @tests java.net.URLConnection#setReadTimeout(int)
714      */
715     public void test_setReadTimeoutI() throws Exception {
716         URLConnection uc = new URL("http://localhost").openConnection();
717         assertEquals(0, uc.getReadTimeout());
718         uc.setReadTimeout(0);
719         assertEquals(0, uc.getReadTimeout());
720         try {
721             uc.setReadTimeout(-100);
722             fail("should throw IllegalArgumentException");
723         } catch (IllegalArgumentException e) {
724             // correct
725         }
726         assertEquals(0, uc.getReadTimeout());
727         uc.setReadTimeout(100);
728         assertEquals(100, uc.getReadTimeout());
729         try {
730             uc.setReadTimeout(-1);
731             fail("should throw IllegalArgumentException");
732         } catch (IllegalArgumentException e) {
733             // correct
734         }
735         assertEquals(100, uc.getReadTimeout());
736     }
737 
738     /**
739      * @tests java.net.URLConnection#setRequestProperty(String, String)
740      */
741     public void test_setRequestProperty() throws MalformedURLException,
742             IOException {
743 
744         MockURLConnection u = new MockURLConnection(new URL(
745                 "http://www.apache.org"));
746         try {
747             u.setRequestProperty(null, "someValue");
748             fail("Expected NullPointerException");
749         } catch (NullPointerException e) {
750             // expected
751         }
752 
753         u.connect();
754         try {
755             // state of connection is checked first
756             // so no NPE in case of null 'field' param
757             u.setRequestProperty(null, "someValue");
758             fail("Expected IllegalStateException");
759         } catch (IllegalStateException e) {
760             // expected
761         }
762     }
763 
764     /**
765      * @tests java.net.URLConnection#setRequestProperty(java.lang.String,
766      *java.lang.String)
767      */
768     public void test_setRequestPropertyLjava_lang_StringLjava_lang_String()
769             throws MalformedURLException {
770         MockURLConnection u = new MockURLConnection(new URL(
771                 "http://www.apache.org"));
772 
773         u.setRequestProperty("", "");
774         assertEquals("", u.getRequestProperty(""));
775 
776         u.setRequestProperty("key", "value");
777         assertEquals("value", u.getRequestProperty("key"));
778     }
779 
780     /**
781      * @tests java.net.URLConnection#setUseCaches(boolean)
782      */
783     public void test_setUseCachesZ() throws MalformedURLException {
784         // Regression for HARMONY-71
785         MockURLConnection u = new MockURLConnection(new URL(
786                 "http://www.apache.org"));
787         u.connect();
788         try {
789             u.setUseCaches(true);
790             fail("Assert 0: expected an IllegalStateException");
791         } catch (IllegalStateException e) {
792             // expected
793         }
794     }
795 
796     /**
797      * @tests java.net.URLConnection#toString()
798      */
799     public void test_toString() {
800         assertTrue("Wrong toString: " + uc.toString(), uc.toString().indexOf(
801                 "URLConnection") > 0);
802     }
803 
804     private byte[] toBOMBytes(String text, String enc) throws IOException {
805         ByteArrayOutputStream bos = new ByteArrayOutputStream();
806 
807         if (enc.equals("UTF-8")) {
808             bos.write(new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF });
809         }
810         if (enc.equals("UTF-16BE")) {
811             bos.write(new byte[] { (byte) 0xFE, (byte) 0xFF });
812         }
813         if (enc.equals("UTF-16LE")) {
814             bos.write(new byte[] { (byte) 0xFF, (byte) 0xFE });
815         }
816         if (enc.equals("UTF-32BE")) {
817             bos.write(new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0xFE,
818                     (byte) 0xFF });
819         }
820         if (enc.equals("UTF-32LE")) {
821             bos.write(new byte[] { (byte) 0xFF, (byte) 0xFE, (byte) 0x00,
822                     (byte) 0x00 });
823         }
824 
825         bos.write(text.getBytes(enc));
826         return bos.toByteArray();
827     }
828 }
829