Skip to content

Commit

Permalink
SLING-4757 - fix pax exam memory setup and add test for non-markable …
Browse files Browse the repository at this point in the history
…stream

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1686179 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
bdelacretaz committed Jun 18, 2015
1 parent 054f5bf commit 777a003
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 38 deletions.
7 changes: 2 additions & 5 deletions bundles/commons/contentdetection/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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 -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand All @@ -29,7 +30,6 @@

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;
Expand All @@ -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(){
Expand All @@ -50,36 +94,26 @@ public void detectFromExtension(){

@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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down

0 comments on commit 777a003

Please sign in to comment.