You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:20:46 UTC

[sling-org-apache-sling-commons-contentdetection] 06/30: SLING-4757 - test invalid encoding

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.commons.contentdetection-1.0.2
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-contentdetection.git

commit f1577bc43fa739551fbfc8164f72ccbc7e77eeb4
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Mon Jun 1 09:40:58 2015 +0000

    SLING-4757 - test invalid encoding
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/contentdetection@1682850 13f79535-47bb-0310-9956-ffa450edef68
---
 .../internal/FileNameExtractorImpl.java            | 15 ++++++++----
 .../internal/FileNameExtractorImplTest.java        | 27 +++++++++++++++++++++-
 2 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/apache/sling/commons/contentdetection/internal/FileNameExtractorImpl.java b/src/main/java/org/apache/sling/commons/contentdetection/internal/FileNameExtractorImpl.java
index 289f777..f4c3eda 100644
--- a/src/main/java/org/apache/sling/commons/contentdetection/internal/FileNameExtractorImpl.java
+++ b/src/main/java/org/apache/sling/commons/contentdetection/internal/FileNameExtractorImpl.java
@@ -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 implements FileNameExtractor {
         // 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 implements FileNameExtractor {
         name = name.trim();
         return name;
     }
+    
+    protected String getDefaultEncoding() {
+        return "UTF-8";
+    }
 }
diff --git a/src/test/java/org/apache/sling/commons/contentdetection/internal/FileNameExtractorImplTest.java b/src/test/java/org/apache/sling/commons/contentdetection/internal/FileNameExtractorImplTest.java
index f280dc7..695d7af 100644
--- a/src/test/java/org/apache/sling/commons/contentdetection/internal/FileNameExtractorImplTest.java
+++ b/src/test/java/org/apache/sling/commons/contentdetection/internal/FileNameExtractorImplTest.java
@@ -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

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.