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 org.apache.harmony.tests.internal.net.www.protocol.file; 18 19 import java.io.BufferedOutputStream; 20 import java.io.File; 21 import java.io.FileOutputStream; 22 import java.io.IOException; 23 import java.io.InputStream; 24 import java.io.OutputStream; 25 import java.net.URL; 26 import java.net.URLConnection; 27 import junit.framework.TestCase; 28 import libcore.io.Streams; 29 import libcore.net.url.FileURLConnection; 30 31 /** 32 * Tests for <code>FileURLConnection</code> class constructors and methods. 33 */ 34 public class FileURLConnectionTest extends TestCase { 35 36 private static final String RESOURCE_NAME = "resources/test.rtf"; 37 38 private final ClassLoader loader = FileURLConnectionTest.class.getClassLoader(); 39 40 private URL createTempFileWithContent(String resourceName) throws IOException { 41 InputStream is = null; 42 OutputStream os = null; 43 try { 44 final URL url = loader.getResource(resourceName); 45 assertNotNull("Cannot find test resource " + resourceName, url); 46 is = url.openStream(); 47 File file = File.createTempFile("FileURLConnectionTest", 48 resourceName.substring(resourceName.indexOf("."))); 49 os = new BufferedOutputStream(new FileOutputStream(file)); 50 Streams.copy(is, os); 51 52 return new URL("file://" + file.getAbsolutePath()); 53 } finally { 54 if (is != null) { 55 is.close(); 56 } 57 58 if (os != null) { 59 os.close(); 60 } 61 } 62 } 63 64 private String getContentType(String fileName) throws IOException { 65 String resourceName = "resources/" + fileName; 66 URL url = createTempFileWithContent(resourceName); 67 return new FileURLConnection(url).getContentType(); 68 } 69 70 public void testGetContentType() throws IOException { 71 // Regression for HARMONY-4699 72 assertEquals("text/rtf", getContentType("test.rtf")); 73 // RI would return "content/unknown" 74 assertEquals("application/msword", getContentType("test.doc")); 75 assertEquals("text/html", getContentType("test.htx")); 76 assertEquals("text/xml", getContentType("test.xml")); 77 assertEquals("text/html", 78 new FileURLConnection(new URL("file:///")).getContentType()); 79 } 80 81 public void testGetInputStream() throws IOException { 82 // Regression for Harmony-5737 83 URL url = createTempFileWithContent(RESOURCE_NAME); 84 assertNotNull(url); 85 URL anchorUrl = new URL(url, "#anchor"); 86 assertNotNull("Cannot find test resource " + RESOURCE_NAME, anchorUrl); 87 88 FileURLConnection conn = new FileURLConnection(anchorUrl); 89 assertNotNull(conn.getInputStream()); 90 } 91 92 public void testGetInputStream_localHost() throws IOException { 93 // Regression for Harmony-5779 94 URL url = createTempFileWithContent(RESOURCE_NAME); 95 String localURLString = "file://localhost/" + url.getFile(); 96 URL localURL = new URL(localURLString); 97 FileURLConnection conn = new FileURLConnection(localURL); 98 assertNotNull(conn.getInputStream()); 99 assertEquals("file", conn.getURL().getProtocol()); 100 } 101 102 public void testHeaderFunctions() throws IOException { 103 URL url = createTempFileWithContent(RESOURCE_NAME); 104 FileURLConnection conn = new FileURLConnection(url); 105 assertNotNull(conn.getInputStream()); 106 assertEquals(conn.getContentType(), conn.getHeaderField("content-type")); 107 108 url = createTempFileWithContent(RESOURCE_NAME); 109 conn = new FileURLConnection(url); 110 111 assertNotNull(conn.getInputStream()); 112 assertEquals(conn.getContentType(), conn.getHeaderField("content-type")); 113 assertEquals(Integer.toString(conn.getContentLength()), 114 conn.getHeaderField("content-length")); 115 assertEquals(conn.getHeaderField(0), conn.getHeaderField("content-type")); 116 assertEquals(conn.getHeaderField(1), conn.getHeaderField("content-length")); 117 assertEquals(conn.getHeaderField(2), conn.getHeaderField("last-modified")); 118 assertEquals("last-modified", conn.getHeaderFieldKey(2)); 119 assertEquals("content-length", conn.getHeaderFieldKey(1)); 120 assertEquals("content-type", conn.getHeaderFieldKey(0)); 121 } 122 123 public void testHeader_BoundaryCheck() throws IOException { 124 URL url = createTempFileWithContent(RESOURCE_NAME); 125 URLConnection urlConnection = url.openConnection(); 126 assertNull(urlConnection.getHeaderField(Integer.MIN_VALUE)); 127 assertNull(urlConnection.getHeaderField(Integer.MAX_VALUE)); 128 assertNull(urlConnection.getHeaderFieldKey(Integer.MIN_VALUE)); 129 assertNull(urlConnection.getHeaderFieldKey(Integer.MAX_VALUE)); 130 assertNull(urlConnection.getHeaderField(null)); 131 } 132 } 133