You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2015/06/01 11:40:58 UTC

svn commit: r1682850 - in /sling/trunk/bundles/commons/contentdetection/src: main/java/org/apache/sling/commons/contentdetection/internal/ test/java/org/apache/sling/commons/contentdetection/internal/

Author: bdelacretaz
Date: Mon Jun  1 09:40:58 2015
New Revision: 1682850

URL: http://svn.apache.org/r1682850
Log:
SLING-4757 - test invalid encoding

Modified:
    sling/trunk/bundles/commons/contentdetection/src/main/java/org/apache/sling/commons/contentdetection/internal/FileNameExtractorImpl.java
    sling/trunk/bundles/commons/contentdetection/src/test/java/org/apache/sling/commons/contentdetection/internal/FileNameExtractorImplTest.java

Modified: sling/trunk/bundles/commons/contentdetection/src/main/java/org/apache/sling/commons/contentdetection/internal/FileNameExtractorImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/contentdetection/src/main/java/org/apache/sling/commons/contentdetection/internal/FileNameExtractorImpl.java?rev=1682850&r1=1682849&r2=1682850&view=diff
==============================================================================
--- sling/trunk/bundles/commons/contentdetection/src/main/java/org/apache/sling/commons/contentdetection/internal/FileNameExtractorImpl.java (original)
+++ sling/trunk/bundles/commons/contentdetection/src/main/java/org/apache/sling/commons/contentdetection/internal/FileNameExtractorImpl.java Mon Jun  1 09:40:58 2015
@@ -16,15 +16,15 @@
  */
 package org.apache.sling.commons.contentdetection.internal;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.commons.contentdetection.FileNameExtractor;
 import org.osgi.framework.Constants;
 
-import java.io.UnsupportedEncodingException;
-import java.net.URLDecoder;
-
 @Component(metatype = true, label = "%filenameextractor.service.name", description = "%filenameextractor.service.description")
 @Service(FileNameExtractor.class)
 @Property(name = Constants.SERVICE_DESCRIPTION, value = "Apache Sling Filename Extractor Service")
@@ -53,10 +53,11 @@ public class FileNameExtractorImpl imple
         // Decode any potential URL encoding
         int percent = name.indexOf('%');
         if (percent != -1) {
+            final String encoding = getDefaultEncoding();
             try {
-                name = URLDecoder.decode(name, "UTF-8");
+                name = URLDecoder.decode(name, encoding);
             } catch (UnsupportedEncodingException e) {
-                throw new AssertionError("UTF-8 not supported");
+                throw new RuntimeException(encoding + " not supported??", e);
             }
         }
 
@@ -64,4 +65,8 @@ public class FileNameExtractorImpl imple
         name = name.trim();
         return name;
     }
+    
+    protected String getDefaultEncoding() {
+        return "UTF-8";
+    }
 }

Modified: sling/trunk/bundles/commons/contentdetection/src/test/java/org/apache/sling/commons/contentdetection/internal/FileNameExtractorImplTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/contentdetection/src/test/java/org/apache/sling/commons/contentdetection/internal/FileNameExtractorImplTest.java?rev=1682850&r1=1682849&r2=1682850&view=diff
==============================================================================
--- sling/trunk/bundles/commons/contentdetection/src/test/java/org/apache/sling/commons/contentdetection/internal/FileNameExtractorImplTest.java (original)
+++ sling/trunk/bundles/commons/contentdetection/src/test/java/org/apache/sling/commons/contentdetection/internal/FileNameExtractorImplTest.java Mon Jun  1 09:40:58 2015
@@ -23,7 +23,13 @@ import org.junit.Test;
 
 public class FileNameExtractorImplTest {
 
-    FileNameExtractor fileNameExtractor = new FileNameExtractorImpl();
+    private String defaultEncoding = new FileNameExtractorImpl().getDefaultEncoding();
+    
+    FileNameExtractor fileNameExtractor = new FileNameExtractorImpl() {
+        protected String getDefaultEncoding() {
+            return defaultEncoding;
+        }
+    };
 
     @Test
     public void testExtract() throws Exception {
@@ -45,4 +51,23 @@ public class FileNameExtractorImplTest {
         String expectedFileName = "demo test.jpg";
         Assert.assertEquals(expectedFileName, fileNameExtractor.extract(rawPath));
     }
+    
+    @Test
+    public void testInvalidEncoding() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
+        final String rawPath = "http://example.com/demo%20test.jpg?test=true";
+        final String oldEncoding = defaultEncoding;
+        final String badEncoding = "INVALID_ENCODING";
+        try {
+            defaultEncoding = badEncoding;
+            try {
+                fileNameExtractor.extract(rawPath);
+                Assert.fail("Expected an exception with encoding " + defaultEncoding);
+            } catch(RuntimeException re) {
+                final String msg = re.getMessage();
+                Assert.assertTrue("Expected exception message to contain " + badEncoding + " (" + msg + ")", msg.contains(badEncoding));
+            }
+        } finally {
+            defaultEncoding = oldEncoding;
+        }
+    }
 }
\ No newline at end of file