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/10/18 23:18:50 UTC

[sling-org-apache-sling-commons-contentdetection] 16/39: SLING-4757 - fix pax exam memory setup and add test for non-markable stream

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

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

commit 55c622e48c60da4f131d0a368090f11ef25e3033
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Thu Jun 18 10:46:57 2015 +0000

    SLING-4757 - fix pax exam memory setup and add test for non-markable stream
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1686179 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  7 +-
 .../ContentAwareMimeTypeService.java               |  5 +-
 .../it/ContentAwareMimeTypeServiceImplIT.java      | 86 +++++++++++++++-------
 .../commons/contentdetection/internal/it/U.java    |  7 +-
 4 files changed, 67 insertions(+), 38 deletions(-)

diff --git a/pom.xml b/pom.xml
index 851c3a8..28a64f2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -37,12 +37,11 @@
     <properties>
         <exam.version>3.5.0</exam.version>
         <url.version>1.5.2</url.version>
-        <org.ops4j.pax.logging.DefaultServiceLog.level>INFO</org.ops4j.pax.logging.DefaultServiceLog.level>
         <bundle.file.name>${basedir}/target/${project.build.finalName}.jar</bundle.file.name>
         <sling.java.version>6</sling.java.version>
         <sling.launchpad.version>7</sling.launchpad.version>
         <powermock.version>1.6.2</powermock.version>
-        <pax.vm.options>-Xmx256M</pax.vm.options>
+        <pax.vm.options>-Xmx256M -XX:MaxPermSize=256m</pax.vm.options>
     </properties>
 
     <dependencies>
@@ -168,13 +167,11 @@
                     </execution>
                 </executions>
                 <configuration>
+                    <argLine>${pax.vm.options}</argLine>
                     <systemPropertyVariables>
-                        <org.ops4j.pax.logging.DefaultServiceLog.level>${org.ops4j.pax.logging.DefaultServiceLog.level}</org.ops4j.pax.logging.DefaultServiceLog.level>
-                        <pax.exam.log.level>${pax.exam.log.level}</pax.exam.log.level>
                         <java.protocol.handler.pkgs>org.ops4j.pax.url</java.protocol.handler.pkgs>
                         <bundle.file.name>${bundle.file.name}</bundle.file.name>
                         <sling.launchpad.version>${sling.launchpad.version}</sling.launchpad.version>
-                        <pax.vm.options>${pax.vm.options}</pax.vm.options>
                     </systemPropertyVariables>
                     <classpathDependencyExcludes>
                         <!-- The osgi.org dependencies cause trouble with pax exam -->
diff --git a/src/main/java/org/apache/sling/commons/contentdetection/ContentAwareMimeTypeService.java b/src/main/java/org/apache/sling/commons/contentdetection/ContentAwareMimeTypeService.java
index 37be925..5999dc1 100644
--- a/src/main/java/org/apache/sling/commons/contentdetection/ContentAwareMimeTypeService.java
+++ b/src/main/java/org/apache/sling/commons/contentdetection/ContentAwareMimeTypeService.java
@@ -33,9 +33,10 @@ import java.io.InputStream;
 
 public interface ContentAwareMimeTypeService extends MimeTypeService {
     /**
-     * @param filename used if <code>content</code> is <code>null</code> or if
+     * @param filename Used if <code>content</code> is <code>null</code> or if
      *                 this service does not support content-based detection
-     * @param content  optional stream that points to the content to analyze
+     * @param content  Optional stream that points to the content to analyze,
+     *                 must support mark/reset.
      * @return the mime type
      */
     String getMimeType(String filename, InputStream content) throws IOException;
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 62d8450..f488e9d 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,6 +20,7 @@ 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.IOException;
@@ -29,7 +30,6 @@ import javax.inject.Inject;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.sling.commons.contentdetection.ContentAwareMimeTypeService;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.ops4j.pax.exam.Option;
@@ -40,6 +40,50 @@ public class ContentAwareMimeTypeServiceImplIT {
 
     @Inject
     private ContentAwareMimeTypeService contentAwaremimeTypeService;
+    
+    class NonMarkableStream extends BufferedInputStream {
+        NonMarkableStream(InputStream is) {
+            super(is);
+        }
+
+        @Override
+        public synchronized void mark(int readlimit) {
+        }
+
+        @Override
+        public synchronized void reset() throws IOException {
+        }
+
+        @Override
+        public boolean markSupported() {
+            return false;
+        }
+    };
+    
+    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(){
@@ -50,36 +94,26 @@ public class ContentAwareMimeTypeServiceImplIT {
 
     @Test
     public void detectFromContent() throws IOException{
-        final String filename = "this-is-actually-a-wav-file.mp3";
-        final InputStream s = getClass().getResourceAsStream("/" + filename);
-        assertNotNull("Expecting stream to be found:" + filename, s);
-        try {
-            assertEquals("audio/x-wav", contentAwaremimeTypeService.getMimeType(filename, s));
-        } finally {
-            if(s != null) {
-                s.close();
+        new AssertDetect() {
+            @Override
+            InputStream wrapStream(InputStream toWrap) {
+                return new BufferedInputStream(toWrap);
             }
-        }
+        }.assertDetection("audio/x-wav", true);
     }
-
+    
     @Test
-    @Ignore("OutOfMemoryError: PermGen space??")
-    public void testNoContentTampering() throws IOException{
-        final String filename = "this-is-actually-a-wav-file.mp3";
-        final InputStream s = new BufferedInputStream(getClass().getResourceAsStream("/" + filename));
-        assertNotNull("Expecting stream to be found:" + filename, s);
-        try {
-            contentAwaremimeTypeService.getMimeType(filename, s);
-            assertTrue(IOUtils.contentEquals(s,
-                    new BufferedInputStream(getClass().getResourceAsStream(
-                            "/" + filename))));
-        } finally {
-            if(s != null) {
-                s.close();
+    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);
     }
-
+    
     @org.ops4j.pax.exam.Configuration
     public Option[] config() {
         return U.paxConfig();
diff --git a/src/test/java/org/apache/sling/commons/contentdetection/internal/it/U.java b/src/test/java/org/apache/sling/commons/contentdetection/internal/it/U.java
index 901a3eb..d92cb46 100644
--- a/src/test/java/org/apache/sling/commons/contentdetection/internal/it/U.java
+++ b/src/test/java/org/apache/sling/commons/contentdetection/internal/it/U.java
@@ -18,7 +18,6 @@
 package org.apache.sling.commons.contentdetection.internal.it;
 
 import java.io.File;
-import java.util.Arrays;
 
 import org.apache.sling.paxexam.util.SlingPaxOptions;
 import org.ops4j.pax.exam.CoreOptions;
@@ -33,12 +32,10 @@ public class U {
     public static Option[] paxConfig() {
         final File thisProjectsBundle = new File(System.getProperty( "bundle.file.name", "BUNDLE_FILE_NOT_SET" ));
         final String launchpadVersion = System.getProperty("sling.launchpad.version", "LAUNCHPAD_VERSION_NOT_SET");
-        final String [] paxVmOptions = System.getProperty("pax.vm.options", "PAX_VM_OPTIONS_NOT_SET").split(",");
-        log.info("Sling launchpad version: {}, VM options: {}", launchpadVersion, Arrays.asList(paxVmOptions));
+        log.info("Sling launchpad version: {}", launchpadVersion);
         return new DefaultCompositeOption(
                 SlingPaxOptions.defaultLaunchpadOptions(launchpadVersion),
-                CoreOptions.provision(CoreOptions.bundle(thisProjectsBundle.toURI().toString())),
-                CoreOptions.vmOptions(paxVmOptions)
+                CoreOptions.provision(CoreOptions.bundle(thisProjectsBundle.toURI().toString()))
                 
         ).getOptions();
     }

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