added several negative tests
This commit is contained in:
@@ -71,6 +71,8 @@ public class CommonmarkTransformer implements Transformer {
|
|||||||
throws IOException {
|
throws IOException {
|
||||||
if (this.logger.isTraceEnabled())
|
if (this.logger.isTraceEnabled())
|
||||||
this.logger.trace("transform('" + transformName + "', '" + sourceMimetype + "', '" + targetMimetype + "', " + transformOptions.keySet() + ", '" + sourceFile + "', '" + targetFile + "')");
|
this.logger.trace("transform('" + transformName + "', '" + sourceMimetype + "', '" + targetMimetype + "', " + transformOptions.keySet() + ", '" + sourceFile + "', '" + targetFile + "')");
|
||||||
|
if (!MediaType.TEXT_PLAIN_VALUE.equals(sourceMimetype))
|
||||||
|
throw new TransformException(HttpStatus.BAD_REQUEST.value(), "This transformer does not support source data of the '" + sourceMimetype + "' type");
|
||||||
|
|
||||||
List<Extension> extensions = this.gatherExtensions(transformOptions);
|
List<Extension> extensions = this.gatherExtensions(transformOptions);
|
||||||
|
|
||||||
@@ -140,10 +142,11 @@ public class CommonmarkTransformer implements Transformer {
|
|||||||
private Class<?> findClass(String className) {
|
private Class<?> findClass(String className) {
|
||||||
Matcher matcher = this.extClassNamePattern.matcher(className);
|
Matcher matcher = this.extClassNamePattern.matcher(className);
|
||||||
String extName = matcher.find() ? matcher.group(2) : null;
|
String extName = matcher.find() ? matcher.group(2) : null;
|
||||||
String extPackageName = this.camel2package(extName);
|
String extPackageName = extName != null ? this.camel2package(extName) : null;
|
||||||
|
|
||||||
for (String prefix : this.classSearchPrefixes) {
|
for (String classPrefix : this.classSearchPrefixes) {
|
||||||
String classPrefix = prefix.replaceAll("\\{name\\}", extPackageName);
|
if (extPackageName != null)
|
||||||
|
classPrefix = classPrefix.replaceAll("\\{name\\}", extPackageName);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Class<?> extClass = Class.forName(classPrefix + className);
|
Class<?> extClass = Class.forName(classPrefix + className);
|
||||||
|
@@ -73,6 +73,8 @@ public class FlexmarkTransformer implements Transformer {
|
|||||||
throws IOException {
|
throws IOException {
|
||||||
if (this.logger.isTraceEnabled())
|
if (this.logger.isTraceEnabled())
|
||||||
this.logger.trace("transform('" + transformName + "', '" + sourceMimetype + "', '" + targetMimetype + "', " + transformOptions.keySet() + ", '" + sourceFile + "', '" + targetFile + "')");
|
this.logger.trace("transform('" + transformName + "', '" + sourceMimetype + "', '" + targetMimetype + "', " + transformOptions.keySet() + ", '" + sourceFile + "', '" + targetFile + "')");
|
||||||
|
if (!MediaType.TEXT_PLAIN_VALUE.equals(sourceMimetype))
|
||||||
|
throw new TransformException(HttpStatus.BAD_REQUEST.value(), "This transformer does not support source data of the '" + sourceMimetype + "' type");
|
||||||
|
|
||||||
String profile = transformOptions.getOrDefault(RequestParamConstants.PROFILE, this.profile);
|
String profile = transformOptions.getOrDefault(RequestParamConstants.PROFILE, this.profile);
|
||||||
|
|
||||||
@@ -147,8 +149,9 @@ public class FlexmarkTransformer implements Transformer {
|
|||||||
Matcher matcher = this.extClassNamePattern.matcher(className);
|
Matcher matcher = this.extClassNamePattern.matcher(className);
|
||||||
String extName = matcher.find() ? matcher.group(2).toLowerCase() : null;
|
String extName = matcher.find() ? matcher.group(2).toLowerCase() : null;
|
||||||
|
|
||||||
for (String prefix : this.classSearchPrefixes) {
|
for (String classPrefix : this.classSearchPrefixes) {
|
||||||
String classPrefix = prefix.replaceAll("\\{name\\}", extName);
|
if (extName != null)
|
||||||
|
classPrefix = classPrefix.replaceAll("\\{name\\}", extName);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Class<?> extClass = Class.forName(classPrefix + className);
|
Class<?> extClass = Class.forName(classPrefix + className);
|
||||||
|
@@ -117,7 +117,6 @@ public class TransformerController extends AbstractTransformerController {
|
|||||||
logger.trace("getProbeTestTransform().executeTransformCommand('" + sourceFile + "', '" + targetFile + "')");
|
logger.trace("getProbeTestTransform().executeTransformCommand('" + sourceFile + "', '" + targetFile + "')");
|
||||||
Transformer transformer = transformers.get(new Random().nextInt(2));
|
Transformer transformer = transformers.get(new Random().nextInt(2));
|
||||||
transformer.transform(MediaType.TEXT_MARKDOWN_VALUE, MediaType.TEXT_HTML_VALUE, Collections.emptyMap(), sourceFile, targetFile);
|
transformer.transform(MediaType.TEXT_MARKDOWN_VALUE, MediaType.TEXT_HTML_VALUE, Collections.emptyMap(), sourceFile, targetFile);
|
||||||
System.out.println(targetFile.length());
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -151,9 +150,6 @@ public class TransformerController extends AbstractTransformerController {
|
|||||||
targetMimetype = matcher.find() ? this.ext2mime(matcher.group(1)) : this.defaultTarget.toString();
|
targetMimetype = matcher.find() ? this.ext2mime(matcher.group(1)) : this.defaultTarget.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!MediaType.TEXT_MARKDOWN_VALUE.equals(sourceMimetype))
|
|
||||||
throw new TransformException(HttpStatus.BAD_REQUEST.value(), "This transformer does not support source data of the '" + sourceMimetype + "' type");
|
|
||||||
|
|
||||||
String engine = transformOptions.getOrDefault(RequestParamConstants.ENGINE, FlexmarkTransformer.ID);
|
String engine = transformOptions.getOrDefault(RequestParamConstants.ENGINE, FlexmarkTransformer.ID);
|
||||||
if (!this.engineMap.containsKey(engine))
|
if (!this.engineMap.containsKey(engine))
|
||||||
throw new TransformException(HttpStatus.BAD_REQUEST.value(), "This transformer does not support the following engine: " + engine);
|
throw new TransformException(HttpStatus.BAD_REQUEST.value(), "This transformer does not support the following engine: " + engine);
|
||||||
|
@@ -0,0 +1,18 @@
|
|||||||
|
package com.inteligr8.alfresco.module.alfmarkdown;
|
||||||
|
|
||||||
|
import org.alfresco.transformer.executors.Transformer;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
|
||||||
|
public class CommonmarkTransformerTest extends TransformerTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier(FlexmarkTransformer.ID)
|
||||||
|
protected Transformer transformer;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Transformer getTransformer() {
|
||||||
|
return this.transformer;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -1,12 +1,17 @@
|
|||||||
package com.inteligr8.alfresco.module.alfmarkdown;
|
package com.inteligr8.alfresco.module.alfmarkdown;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.alfresco.transform.exceptions.TransformException;
|
import org.alfresco.transform.exceptions.TransformException;
|
||||||
import org.alfresco.transformer.executors.Transformer;
|
import org.alfresco.transformer.executors.Transformer;
|
||||||
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import com.inteligr8.junit4.AssertRegex;
|
import com.inteligr8.junit4.AssertRegex;
|
||||||
@@ -27,4 +32,24 @@ public abstract class TransformerTest {
|
|||||||
this.getTransformer().extractMetadata("does-not-matter", "does-not-matter", "does-not-matter", Collections.emptyMap(), null, null);
|
this.getTransformer().extractMetadata("does-not-matter", "does-not-matter", "does-not-matter", Collections.emptyMap(), null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void tryTransformUnsupportedMedia() throws Exception {
|
||||||
|
List<String[]> mediaTypePairs = Arrays.asList(
|
||||||
|
new String[] {MediaType.TEXT_PLAIN_VALUE, MediaType.TEXT_PLAIN_VALUE},
|
||||||
|
new String[] {MediaType.TEXT_PLAIN_VALUE, MediaType.TEXT_MARKDOWN_VALUE},
|
||||||
|
new String[] {MediaType.TEXT_MARKDOWN_VALUE, MediaType.TEXT_PLAIN_VALUE},
|
||||||
|
new String[] {MediaType.TEXT_HTML_VALUE, MediaType.TEXT_MARKDOWN_VALUE},
|
||||||
|
new String[] {MediaType.TEXT_MARKDOWN_VALUE, MediaType.TEXT_MARKDOWN_VALUE}
|
||||||
|
);
|
||||||
|
|
||||||
|
for (String[] mediaTypePair : mediaTypePairs) {
|
||||||
|
try {
|
||||||
|
this.getTransformer().transform(mediaTypePair[0], mediaTypePair[1], Collections.emptyMap(), null, null);
|
||||||
|
Assert.fail();
|
||||||
|
} catch (TransformException te) {
|
||||||
|
Assert.assertEquals(te.getMessage(), HttpStatus.BAD_REQUEST.value(), te.getStatusCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user