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