KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > util > test > MIMEUtilsTestCase


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.util.test;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.io.StringReader JavaDoc;
21 import junit.framework.TestCase;
22
23 import org.apache.cocoon.util.MIMEUtils;
24 import org.apache.commons.lang.SystemUtils;
25
26 /**
27  * Test Cases for the MIMEUtils class.
28  * @see org.apache.cocoon.util.MIMEUtils
29  * Specifically, code for testing the parsing of mime.types files.
30  *
31  * @author <a HREF="mailto:jefft@apache.org">Jeff Turner</a>
32  * @version CVS $Id: MIMEUtilsTestCase.java 165516 2005-05-01 15:36:38Z antonio $
33  */

34 public class MIMEUtilsTestCase extends TestCase
35 {
36     final String JavaDoc NL = SystemUtils.LINE_SEPARATOR;
37     Map JavaDoc mimeMap;
38     Map JavaDoc extMap;
39     static final String JavaDoc M2E = "MIME to extension mappings";
40     static final String JavaDoc E2M = "Extension to MIME mappings";
41
42     public MIMEUtilsTestCase(String JavaDoc name) {
43         super(name);
44     }
45
46     public static void main(String JavaDoc args[]) {
47         junit.textui.TestRunner.run(MIMEUtilsTestCase.class);
48     }
49
50     public void setUp() throws Exception JavaDoc {
51         super.setUp();
52         mimeMap = new HashMap JavaDoc();
53         extMap = new HashMap JavaDoc();
54     }
55
56     public void tearDown() throws Exception JavaDoc {
57         super.tearDown();
58         mimeMap = null;
59         extMap = null;
60     }
61
62     public void tstGetMimeType() {
63         assertEquals(E2M, "text/plain", MIMEUtils.getMIMEType(".txt"));
64         assertEquals(E2M, "image/jpeg", MIMEUtils.getMIMEType(".jpg"));
65         assertEquals(E2M, "video/mpeg", MIMEUtils.getMIMEType(".mpg"));
66         assertEquals(E2M, null, MIMEUtils.getMIMEType(".undefined"));
67         assertEquals(M2E, ".txt", MIMEUtils.getDefaultExtension("text/plain"));
68         assertEquals(M2E, ".jpeg", MIMEUtils.getDefaultExtension("image/jpeg"));
69         assertEquals(M2E, ".mpeg", MIMEUtils.getDefaultExtension("video/mpeg"));
70         assertEquals(M2E, null, MIMEUtils.getDefaultExtension("application/octet-stream"));
71
72     }
73
74     public void testTypicalUsage() throws Exception JavaDoc {
75         String JavaDoc mime_types="# MIME type mappings"+ NL +
76             "text/plain txt text "+ NL +
77             "text/html html htm"+ NL +
78             " "+ NL +
79             "text/xml xml"+ NL +
80             "text/css css"+ NL +
81             "text/javascript js "+ NL +
82             "application/x-javascript js";
83
84         MIMEUtils.loadMimeTypes(new StringReader JavaDoc(mime_types), extMap, mimeMap);
85         assertEquals(".txt", extMap.get("text/plain"));
86         assertEquals(".html", extMap.get("text/html"));
87         assertEquals(".xml", extMap.get("text/xml"));
88         assertEquals(".css", extMap.get("text/css"));
89         assertEquals(".js", extMap.get("text/javascript"));
90         assertEquals(".js", extMap.get("application/x-javascript"));
91
92         assertEquals("text/plain", mimeMap.get(".text"));
93         assertEquals("text/plain", mimeMap.get(".txt"));
94         assertEquals("text/html", mimeMap.get(".html"));
95         assertEquals("text/html", mimeMap.get(".htm"));
96         assertEquals("text/xml", mimeMap.get(".xml"));
97         assertEquals("text/css", mimeMap.get(".css"));
98         assertEquals("text/javascript", mimeMap.get(".js"));
99
100         assertEquals(M2E, 6, extMap.size());
101         assertEquals(E2M, 7, mimeMap.size());
102     }
103
104     public void tstEmpty() throws Exception JavaDoc {
105         String JavaDoc mime_types="";
106         MIMEUtils.loadMimeTypes(new StringReader JavaDoc(mime_types), extMap, mimeMap);
107         assertEquals(M2E, 0, extMap.size());
108         assertEquals(E2M, 0, mimeMap.size());
109     }
110
111     public void tstCommentsAndWhitespace() throws Exception JavaDoc {
112         String JavaDoc mime_types="## A commented line"+NL+
113             " "+ NL +
114             "# Another comment";
115         MIMEUtils.loadMimeTypes(new StringReader JavaDoc(mime_types), extMap, mimeMap);
116         assertEquals(M2E, 0, extMap.size());
117         assertEquals(E2M, 0, mimeMap.size());
118     }
119
120     public void tstMimeTypeWithoutExtension() throws Exception JavaDoc {
121         String JavaDoc mime_types=
122             "text/plain txt text"+ NL +
123             "application/octet-stream"+ NL + NL;
124         MIMEUtils.loadMimeTypes(new StringReader JavaDoc(mime_types), extMap, mimeMap);
125         assertEquals(".txt", extMap.get("text/plain"));
126         assertEquals("text/plain", mimeMap.get(".txt"));
127         assertEquals("text/plain", mimeMap.get(".text"));
128         assertEquals(M2E, 1, extMap.size());
129         assertEquals(E2M, 2, mimeMap.size());
130     }
131 }
132
Popular Tags