J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4160195 |
| 27 | * @summary Check for correct detection of XML content type |
| 28 | */ |
| 29 | |
| 30 | import java.io.*; |
| 31 | import java.net.*; |
| 32 | |
| 33 | |
| 34 | public class GetXmlContentType { |
| 35 | |
| 36 | static final String XML_MIME_TYPE = "application/xml"; |
| 37 | |
| 38 | // guess type from content and filename |
| 39 | static final String goodFiles [] = { |
| 40 | "xml1", // US-ASCII and supersets |
| 41 | "xml2.xml", // typed inferred from filename |
| 42 | "xml3", // UTF-16 little endian (partial) |
| 43 | "xml4" // UTF-16 big endian (partial) |
| 44 | }; |
| 45 | |
| 46 | // some common non-XML examples |
| 47 | static final String badFiles [] = { |
| 48 | "not-xml1", |
| 49 | "not-xml2" |
| 50 | }; |
| 51 | |
| 52 | public static void main(String[] args) throws Exception { |
| 53 | boolean sawError = false; |
| 54 | |
| 55 | // |
| 56 | // POSITIVE tests: good data --> good result |
| 57 | // |
| 58 | for (int i = 0; i < goodFiles.length; i++) { |
| 59 | String result = getUrlContentType (goodFiles [i]); |
| 60 | |
| 61 | if (!XML_MIME_TYPE.equals (result)) { |
| 62 | System.out.println ("Wrong MIME type: " |
| 63 | + goodFiles [i] |
| 64 | + " --> " + result |
| 65 | ); |
| 66 | sawError = true; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // |
| 71 | // NEGATIVE tests: bad data --> correct diagnostic |
| 72 | // |
| 73 | for (int i = 0; i < badFiles.length; i++) { |
| 74 | String result = getUrlContentType (badFiles [i]); |
| 75 | |
| 76 | if (XML_MIME_TYPE.equals (result)) { |
| 77 | System.out.println ("Wrong MIME type: " |
| 78 | + badFiles [i] |
| 79 | + " --> " + result |
| 80 | ); |
| 81 | sawError = true; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (sawError) |
| 86 | throw new Exception ( |
| 87 | "GetXmlContentType Test failed; see diagnostics."); |
| 88 | } |
| 89 | |
| 90 | static String getUrlContentType (String name) throws IOException { |
| 91 | File file = new File(System.getProperty("test.src", "."), "xml"); |
| 92 | URL u = new URL ("file:" |
| 93 | + file.getCanonicalPath() |
| 94 | + file.separator |
| 95 | + name); |
| 96 | URLConnection conn = u.openConnection (); |
| 97 | |
| 98 | return conn.getContentType (); |
| 99 | } |
| 100 | |
| 101 | } |