Narayan Kamath | cb318c6 | 2013-11-12 16:01:55 +0000 | [diff] [blame^] | 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 | package tests.api.internal.net.www.protocol.file; |
| 18 | |
| 19 | import java.io.IOException; |
| 20 | import java.net.URL; |
| 21 | import java.net.URLConnection; |
| 22 | |
| 23 | import junit.framework.TestCase; |
| 24 | |
| 25 | import libcore.net.url.FileURLConnection; |
| 26 | |
| 27 | /** |
| 28 | * Tests for <code>FileURLConnection</code> class constructors and methods. |
| 29 | */ |
| 30 | public class FileURLConnectionTest extends TestCase { |
| 31 | |
| 32 | static String getContentType(String fileName) throws IOException { |
| 33 | String resourceName = "org/apache/harmony/luni/tests/" + fileName; |
| 34 | URL url = ClassLoader.getSystemClassLoader().getResource(resourceName); |
| 35 | assertNotNull("Cannot find test resource " + resourceName, url); |
| 36 | return new FileURLConnection(url).getContentType(); |
| 37 | } |
| 38 | |
| 39 | public void testGetContentType() throws IOException { |
| 40 | // Regression for HARMONY-4699 |
| 41 | assertEquals("application/rtf", getContentType("test.rtf")); |
| 42 | assertEquals("text/plain", getContentType("test.java")); |
| 43 | // RI would return "content/unknown" |
| 44 | assertEquals("application/msword", getContentType("test.doc")); |
| 45 | assertEquals("text/html", getContentType("test.htx")); |
| 46 | assertEquals("application/xml", getContentType("test.xml")); |
| 47 | assertEquals("text/plain", getContentType(".")); |
| 48 | } |
| 49 | |
| 50 | public void testGetInputStream() throws IOException { |
| 51 | // Regression for Harmony-5737 |
| 52 | String resourceName = "org/apache/harmony/luni/tests/" + "test.rtf"; |
| 53 | URL url = ClassLoader.getSystemClassLoader().getResource(resourceName); |
| 54 | URL anchorUrl = new URL(url, "#anchor"); |
| 55 | assertNotNull("Cannot find test resource " + resourceName, anchorUrl); |
| 56 | |
| 57 | FileURLConnection conn = new FileURLConnection(anchorUrl); |
| 58 | assertNotNull(conn.getInputStream()); |
| 59 | |
| 60 | // Regression for Harmony-5779 |
| 61 | String localURLString = "file://localhost/" + url.getFile(); |
| 62 | URL localURL = new URL(localURLString); |
| 63 | conn = new FileURLConnection(localURL); |
| 64 | assertNotNull(conn.getInputStream()); |
| 65 | assertEquals("file", conn.getURL().getProtocol()); |
| 66 | } |
| 67 | |
| 68 | public void testHeaderFunctions() throws IOException { |
| 69 | String resourceName = "org/apache/harmony/luni/tests/"; //folder name |
| 70 | URL url = ClassLoader.getSystemClassLoader().getResource(resourceName); |
| 71 | FileURLConnection conn = new FileURLConnection(url); |
| 72 | assertNotNull(conn.getInputStream()); |
| 73 | assertEquals(conn.getContentType(), conn.getHeaderField("content-type")); |
| 74 | |
| 75 | resourceName = "org/apache/harmony/luni/tests/" + "test.rtf"; |
| 76 | ; //folder name |
| 77 | url = ClassLoader.getSystemClassLoader().getResource(resourceName); |
| 78 | conn = new FileURLConnection(url); |
| 79 | assertNotNull(conn.getInputStream()); |
| 80 | assertEquals(conn.getContentType(), conn.getHeaderField("content-type")); |
| 81 | assertEquals(Integer.toString(conn.getContentLength()), conn.getHeaderField("content-length")); |
| 82 | assertEquals(conn.getHeaderField(0), conn.getHeaderField("content-type")); |
| 83 | assertEquals(conn.getHeaderField(1), conn.getHeaderField("content-length")); |
| 84 | assertEquals(conn.getHeaderField(2), conn.getHeaderField("last-modified")); |
| 85 | assertEquals("last-modified", conn.getHeaderFieldKey(2)); |
| 86 | assertEquals("content-length", conn.getHeaderFieldKey(1)); |
| 87 | assertEquals("content-type", conn.getHeaderFieldKey(0)); |
| 88 | } |
| 89 | |
| 90 | public void testHeader_BoundaryCheck() throws IOException { |
| 91 | String resourceName = "org/apache/harmony/luni/tests/"; |
| 92 | URL url = ClassLoader.getSystemClassLoader().getResource(resourceName); |
| 93 | URLConnection urlConnection = url.openConnection(); |
| 94 | assertNull(urlConnection.getHeaderField(Integer.MIN_VALUE)); |
| 95 | assertNull(urlConnection.getHeaderField(Integer.MAX_VALUE)); |
| 96 | assertNull(urlConnection.getHeaderFieldKey(Integer.MIN_VALUE)); |
| 97 | assertNull(urlConnection.getHeaderFieldKey(Integer.MAX_VALUE)); |
| 98 | assertNull(urlConnection.getHeaderField(null)); |
| 99 | } |
| 100 | } |