sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From romb...@apache.org
Subject [sling-org-apache-sling-commons-contentdetection] 17/30: SLING-4757 - throw IllegalArgumentException if InputStream does not support mark/reset
Date Tue, 07 Nov 2017 09:20:57 GMT
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 2f62e4793f7f3a69dfd4617da948b36d1f4acf1a
Author: Bertrand Delacretaz <bdelacretaz@apache.org>
AuthorDate: Thu Jun 18 11:52:23 2015 +0000

    SLING-4757 - throw IllegalArgumentException if InputStream does not support mark/reset
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/contentdetection@1686195
13f79535-47bb-0310-9956-ffa450edef68
---
 .../internal/ContentAwareMimeTypeServiceImpl.java  |  3 +
 .../it/ContentAwareMimeTypeServiceImplIT.java      | 72 +++++++++-------------
 2 files changed, 31 insertions(+), 44 deletions(-)

diff --git a/src/main/java/org/apache/sling/commons/contentdetection/internal/ContentAwareMimeTypeServiceImpl.java
b/src/main/java/org/apache/sling/commons/contentdetection/internal/ContentAwareMimeTypeServiceImpl.java
index 32e29e7..29e8508 100644
--- a/src/main/java/org/apache/sling/commons/contentdetection/internal/ContentAwareMimeTypeServiceImpl.java
+++ b/src/main/java/org/apache/sling/commons/contentdetection/internal/ContentAwareMimeTypeServiceImpl.java
@@ -50,6 +50,9 @@ public class ContentAwareMimeTypeServiceImpl implements  ContentAwareMimeTypeSer
         if(content == null) {
             return mimeTypeService.getMimeType(filename);
         }
+        if(content != null && !content.markSupported()) {
+            throw new IllegalArgumentException("Supplied InputStream does not support mark/reset");
+        }
         TikaInputStream stream = TikaInputStream.get(content);
         Metadata metadata = new Metadata();
         metadata.set(Metadata.RESOURCE_NAME_KEY, filename);
diff --git a/src/test/java/org/apache/sling/commons/contentdetection/internal/it/ContentAwareMimeTypeServiceImplIT.java
b/src/test/java/org/apache/sling/commons/contentdetection/internal/it/ContentAwareMimeTypeServiceImplIT.java
index f488e9d..64269f5 100644
--- a/src/test/java/org/apache/sling/commons/contentdetection/internal/it/ContentAwareMimeTypeServiceImplIT.java
+++ b/src/test/java/org/apache/sling/commons/contentdetection/internal/it/ContentAwareMimeTypeServiceImplIT.java
@@ -20,9 +20,9 @@ package org.apache.sling.commons.contentdetection.internal.it;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.assertFalse;
 
 import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 
@@ -60,58 +60,42 @@ public class ContentAwareMimeTypeServiceImplIT {
         }
     };
     
-    abstract class AssertDetect {
-        void assertDetection(String expectedType, boolean expectSameContent) throws IOException
{
-            final String filename = "this-is-actually-a-wav-file.mp3";
-            final String path = "/" + filename;
-            final InputStream s = wrapStream(getClass().getResourceAsStream(path));
-            assertNotNull("Expecting stream to be found:" + filename, s);
-            InputStream originalStream = null;
-            try {
-                assertEquals(expectedType, contentAwaremimeTypeService.getMimeType(filename,
s));
-                originalStream = getClass().getResourceAsStream(path);
-                assertNotNull("Expecting stream to be found:" + filename, originalStream);
-                if(expectSameContent) {
-                    assertTrue("Expecting content to be unchanged", IOUtils.contentEquals(s,
originalStream));
-                } else {
-                    assertFalse("Expecting content to have changed", IOUtils.contentEquals(s,
originalStream));
-                }
-            } finally {
-                IOUtils.closeQuietly(s);
-                IOUtils.closeQuietly(originalStream);
-            }
-        }
-        
-        abstract InputStream wrapStream(InputStream toWrap);
-    }
-
     @Test
-    public void detectFromExtension(){
+    public void detectFromExtension() throws IOException {
         String mimeTypeName = "test.mp3";
         String mimeType = "audio/mpeg";
-        assertEquals(mimeType, contentAwaremimeTypeService.getMimeType(mimeTypeName));
+        assertEquals("Expecting mp3 type without InputStream parameter",
+                mimeType, contentAwaremimeTypeService.getMimeType(mimeTypeName));
+        assertEquals("Expecting mp3 type with null InputStream parameter",
+                mimeType, contentAwaremimeTypeService.getMimeType(mimeTypeName, null));
     }
 
     @Test
     public void detectFromContent() throws IOException{
-        new AssertDetect() {
-            @Override
-            InputStream wrapStream(InputStream toWrap) {
-                return new BufferedInputStream(toWrap);
-            }
-        }.assertDetection("audio/x-wav", true);
+        final String filename = "this-is-actually-a-wav-file.mp3";
+        final String path = "/" + filename;
+        final InputStream s = new BufferedInputStream(getClass().getResourceAsStream(path));
+        assertNotNull("Expecting stream to be found:" + filename, s);
+        InputStream originalStream = null;
+        try {
+            assertEquals("audio/x-wav", contentAwaremimeTypeService.getMimeType(filename,
s));
+            originalStream = getClass().getResourceAsStream(path);
+            assertNotNull("Expecting stream to be found:" + filename, originalStream);
+            assertTrue("Expecting content to be unchanged", IOUtils.contentEquals(s, originalStream));
+        } finally {
+            IOUtils.closeQuietly(s);
+            IOUtils.closeQuietly(originalStream);
+        }
     }
     
-    @Test
-    public void detectFromContentWithNonMarkableStream() throws IOException{
-        // Interestingly, with a non-markable stream  the detector falls back to
-        // filename detection but still touches the content stream
-        new AssertDetect() {
-            @Override
-            InputStream wrapStream(InputStream toWrap) {
-                return new NonMarkableStream(toWrap);
-            }
-        }.assertDetection("audio/mpeg", false);
+    @Test(expected=IllegalArgumentException.class)
+    public void nonMarkableStreamDetectionShouldFail() throws IOException{
+        final InputStream nms = new NonMarkableStream(new ByteArrayInputStream("1234567890".getBytes()));
+        try {
+            contentAwaremimeTypeService.getMimeType("foo.txt", nms);
+        } finally {
+            IOUtils.closeQuietly(nms);
+        }
     }
     
     @org.ops4j.pax.exam.Configuration

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

Mime
View raw message