Author: elvisisking
Date: 2009-09-25 17:18:45 -0400 (Fri, 25 Sep 2009)
New Revision: 1271
Added:
trunk/dna-common/src/test/resources/org/jboss/dna/common/util/additionalmime.types
Modified:
trunk/dna-common/src/main/java/org/jboss/dna/common/util/MimeTypeUtil.java
trunk/dna-common/src/test/java/org/jboss/dna/common/util/MimeTypeUtilTest.java
Log:
DNA-528 Loading Additional Mimetypes Via Stream Does Not Work in MimeTypeUtil - Fixed a
regex in constructor that was nulling out all extensions loaded using the load method.
Also fixed a problem where hidden files (files with just an extension) were not getting a
mimetype. Added some tests.
Modified: trunk/dna-common/src/main/java/org/jboss/dna/common/util/MimeTypeUtil.java
===================================================================
--- trunk/dna-common/src/main/java/org/jboss/dna/common/util/MimeTypeUtil.java 2009-09-25
14:57:23 UTC (rev 1270)
+++ trunk/dna-common/src/main/java/org/jboss/dna/common/util/MimeTypeUtil.java 2009-09-25
21:18:45 UTC (rev 1271)
@@ -99,8 +99,8 @@
for (Map.Entry<String, String> entry :
extensionsToMimeTypes.entrySet()) {
String extensionString = entry.getKey();
if (extensionString == null) continue;
- // Lowercase, trime, and remove all leading '.' characters ...
- extensionString =
extensionString.toLowerCase().trim().replaceAll("^.+", "");
+ // Lowercase, trim, and remove all leading '.' characters ...
+ extensionString =
extensionString.toLowerCase().trim().replaceAll("^[.]+", "");
if (extensionString.length() == 0) continue;
String mimeType = entry.getValue();
if (mimeType == null) continue;
@@ -217,7 +217,7 @@
// Find the extension ...
int indexOfDelimiter = trimmedName.lastIndexOf('.');
- if (indexOfDelimiter < 1) return null;
+ if (indexOfDelimiter < 0) return null;
String extension = trimmedName.substring(indexOfDelimiter).toLowerCase();
// Look for a match ...
Modified: trunk/dna-common/src/test/java/org/jboss/dna/common/util/MimeTypeUtilTest.java
===================================================================
---
trunk/dna-common/src/test/java/org/jboss/dna/common/util/MimeTypeUtilTest.java 2009-09-25
14:57:23 UTC (rev 1270)
+++
trunk/dna-common/src/test/java/org/jboss/dna/common/util/MimeTypeUtilTest.java 2009-09-25
21:18:45 UTC (rev 1271)
@@ -25,8 +25,11 @@
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.nullValue;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import java.io.File;
+import java.io.InputStream;
+import java.util.Map;
import org.junit.Before;
import org.junit.Test;
@@ -99,4 +102,28 @@
assertThat(detector.mimeTypeOf("something.txt \t"),
is("text/plain"));
}
+ @Test
+ public void shouldLoadAdditionalMimeTypeMappings() {
+ String iniFile = ".ini";
+ assertNull(this.detector.mimeTypeOf(iniFile));
+
+ // load custom map
+ InputStream stream =
Thread.currentThread().getContextClassLoader().getResourceAsStream("org/jboss/dna/common/util/additionalmime.types");
+ Map<String, String> customMap = MimeTypeUtil.load(stream, null);
+ assertThat(customMap.size(), is(3));
+
+ // construct with custom map
+ this.detector = new MimeTypeUtil(customMap, false);
+ assertThat(this.detector.mimeTypeOf(iniFile), is("text/plain"));
+
+ // make sure all other extensions have been loaded correctly
+ assertThat(this.detector.mimeTypeOf(".properties"),
is("text/plain"));
+ assertThat(this.detector.mimeTypeOf(".xsd"),
is("application/xml"));
+ }
+
+ @Test
+ public void shouldFindMimeTypeOfHiddenFiles() {
+ assertThat(this.detector.mimeTypeOf(".txt"),
is("text/plain"));
+ }
+
}
Added: trunk/dna-common/src/test/resources/org/jboss/dna/common/util/additionalmime.types
===================================================================
--- trunk/dna-common/src/test/resources/org/jboss/dna/common/util/additionalmime.types
(rev 0)
+++
trunk/dna-common/src/test/resources/org/jboss/dna/common/util/additionalmime.types 2009-09-25
21:18:45 UTC (rev 1271)
@@ -0,0 +1,9 @@
+# This file defines the Internet media types and extensions.
+# For more information about Internet media types,
+# please read RFC 2045, 2046, 2047, 2048, and 2077. The Internet media type
+# registry is at <
http://www.iana.org/assignments/media-types/>.
+
+# MIME type Extensions
+
+text/plain ini properties
+application/xml xsd