1 16 package org.apache.cocoon.util.test; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 import java.io.StringReader ; 21 import junit.framework.TestCase; 22 23 import org.apache.cocoon.util.MIMEUtils; 24 import org.apache.commons.lang.SystemUtils; 25 26 34 public class MIMEUtilsTestCase extends TestCase 35 { 36 final String NL = SystemUtils.LINE_SEPARATOR; 37 Map mimeMap; 38 Map extMap; 39 static final String M2E = "MIME to extension mappings"; 40 static final String E2M = "Extension to MIME mappings"; 41 42 public MIMEUtilsTestCase(String name) { 43 super(name); 44 } 45 46 public static void main(String args[]) { 47 junit.textui.TestRunner.run(MIMEUtilsTestCase.class); 48 } 49 50 public void setUp() throws Exception { 51 super.setUp(); 52 mimeMap = new HashMap (); 53 extMap = new HashMap (); 54 } 55 56 public void tearDown() throws Exception { 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 { 75 String 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 (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 { 105 String mime_types=""; 106 MIMEUtils.loadMimeTypes(new StringReader (mime_types), extMap, mimeMap); 107 assertEquals(M2E, 0, extMap.size()); 108 assertEquals(E2M, 0, mimeMap.size()); 109 } 110 111 public void tstCommentsAndWhitespace() throws Exception { 112 String mime_types="## A commented line"+NL+ 113 " "+ NL + 114 "# Another comment"; 115 MIMEUtils.loadMimeTypes(new StringReader (mime_types), extMap, mimeMap); 116 assertEquals(M2E, 0, extMap.size()); 117 assertEquals(E2M, 0, mimeMap.size()); 118 } 119 120 public void tstMimeTypeWithoutExtension() throws Exception { 121 String mime_types= 122 "text/plain txt text"+ NL + 123 "application/octet-stream"+ NL + NL; 124 MIMEUtils.loadMimeTypes(new StringReader (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 |