001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.util.test;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021: import java.io.StringReader;
022: import junit.framework.TestCase;
023:
024: import org.apache.cocoon.util.MIMEUtils;
025: import org.apache.commons.lang.SystemUtils;
026:
027: /**
028: * Test Cases for the MIMEUtils class.
029: * @see org.apache.cocoon.util.MIMEUtils
030: * Specifically, code for testing the parsing of mime.types files.
031: *
032: * @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
033: * @version CVS $Id: MIMEUtilsTestCase.java 433543 2006-08-22 06:22:54Z crossley $
034: */
035: public class MIMEUtilsTestCase extends TestCase {
036: final String NL = SystemUtils.LINE_SEPARATOR;
037: Map mimeMap;
038: Map extMap;
039: static final String M2E = "MIME to extension mappings";
040: static final String E2M = "Extension to MIME mappings";
041:
042: public MIMEUtilsTestCase(String name) {
043: super (name);
044: }
045:
046: public static void main(String args[]) {
047: junit.textui.TestRunner.run(MIMEUtilsTestCase.class);
048: }
049:
050: public void setUp() throws Exception {
051: super .setUp();
052: mimeMap = new HashMap();
053: extMap = new HashMap();
054: }
055:
056: public void tearDown() throws Exception {
057: super .tearDown();
058: mimeMap = null;
059: extMap = null;
060: }
061:
062: public void tstGetMimeType() {
063: assertEquals(E2M, "text/plain", MIMEUtils.getMIMEType(".txt"));
064: assertEquals(E2M, "image/jpeg", MIMEUtils.getMIMEType(".jpg"));
065: assertEquals(E2M, "video/mpeg", MIMEUtils.getMIMEType(".mpg"));
066: assertEquals(E2M, null, MIMEUtils.getMIMEType(".undefined"));
067: assertEquals(M2E, ".txt", MIMEUtils
068: .getDefaultExtension("text/plain"));
069: assertEquals(M2E, ".jpeg", MIMEUtils
070: .getDefaultExtension("image/jpeg"));
071: assertEquals(M2E, ".mpeg", MIMEUtils
072: .getDefaultExtension("video/mpeg"));
073: assertEquals(M2E, null, MIMEUtils
074: .getDefaultExtension("application/octet-stream"));
075:
076: }
077:
078: public void testTypicalUsage() throws Exception {
079: String mime_types = "# MIME type mappings" + NL
080: + "text/plain txt text " + NL + "text/html html htm"
081: + NL + " " + NL + "text/xml xml" + NL
082: + "text/css css" + NL + "text/javascript js " + NL
083: + "application/x-javascript js";
084:
085: MIMEUtils.loadMimeTypes(new StringReader(mime_types), extMap,
086: mimeMap);
087: assertEquals(".txt", extMap.get("text/plain"));
088: assertEquals(".html", extMap.get("text/html"));
089: assertEquals(".xml", extMap.get("text/xml"));
090: assertEquals(".css", extMap.get("text/css"));
091: assertEquals(".js", extMap.get("text/javascript"));
092: assertEquals(".js", extMap.get("application/x-javascript"));
093:
094: assertEquals("text/plain", mimeMap.get(".text"));
095: assertEquals("text/plain", mimeMap.get(".txt"));
096: assertEquals("text/html", mimeMap.get(".html"));
097: assertEquals("text/html", mimeMap.get(".htm"));
098: assertEquals("text/xml", mimeMap.get(".xml"));
099: assertEquals("text/css", mimeMap.get(".css"));
100: assertEquals("text/javascript", mimeMap.get(".js"));
101:
102: assertEquals(M2E, 6, extMap.size());
103: assertEquals(E2M, 7, mimeMap.size());
104: }
105:
106: public void tstEmpty() throws Exception {
107: String mime_types = "";
108: MIMEUtils.loadMimeTypes(new StringReader(mime_types), extMap,
109: mimeMap);
110: assertEquals(M2E, 0, extMap.size());
111: assertEquals(E2M, 0, mimeMap.size());
112: }
113:
114: public void tstCommentsAndWhitespace() throws Exception {
115: String mime_types = "## A commented line" + NL + " " + NL
116: + "# Another comment";
117: MIMEUtils.loadMimeTypes(new StringReader(mime_types), extMap,
118: mimeMap);
119: assertEquals(M2E, 0, extMap.size());
120: assertEquals(E2M, 0, mimeMap.size());
121: }
122:
123: public void tstMimeTypeWithoutExtension() throws Exception {
124: String mime_types = "text/plain txt text" + NL
125: + "application/octet-stream" + NL + NL;
126: MIMEUtils.loadMimeTypes(new StringReader(mime_types), extMap,
127: mimeMap);
128: assertEquals(".txt", extMap.get("text/plain"));
129: assertEquals("text/plain", mimeMap.get(".txt"));
130: assertEquals("text/plain", mimeMap.get(".text"));
131: assertEquals(M2E, 1, extMap.size());
132: assertEquals(E2M, 2, mimeMap.size());
133: }
134: }
|