Add test for asciidoc format

Related to #29.
This commit is contained in:
David Pilato 2015-02-23 11:54:38 +01:00
parent 4f65664916
commit cfd83443f1
2 changed files with 42 additions and 6 deletions

View File

@ -27,6 +27,7 @@ import org.elasticsearch.index.mapper.DocumentMapperParser;
import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.ParseContext;
import org.elasticsearch.index.mapper.attachment.AttachmentMapper; import org.elasticsearch.index.mapper.attachment.AttachmentMapper;
import org.elasticsearch.index.mapper.attachment.test.MapperTestUtils; import org.elasticsearch.index.mapper.attachment.test.MapperTestUtils;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
@ -35,6 +36,7 @@ import java.io.InputStream;
import static org.elasticsearch.common.io.Streams.copyToBytesFromClasspath; import static org.elasticsearch.common.io.Streams.copyToBytesFromClasspath;
import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath; import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.mapper.attachment.AttachmentMapper.FieldNames.*;
import static org.elasticsearch.plugin.mapper.attachments.tika.TikaInstance.tika; import static org.elasticsearch.plugin.mapper.attachments.tika.TikaInstance.tika;
import static org.hamcrest.Matchers.isEmptyOrNullString; import static org.hamcrest.Matchers.isEmptyOrNullString;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
@ -44,6 +46,17 @@ import static org.hamcrest.Matchers.not;
*/ */
public class VariousDocTest extends AttachmentUnitTestCase { public class VariousDocTest extends AttachmentUnitTestCase {
protected static DocumentMapper docMapper;
@BeforeClass
public static void createMapper() throws IOException {
DocumentMapperParser mapperParser = MapperTestUtils.newMapperParser(ImmutableSettings.builder().build());
mapperParser.putTypeParser(AttachmentMapper.CONTENT_TYPE, new AttachmentMapper.TypeParser());
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/attachment/test/unit/various-doc/test-mapping.json");
docMapper = mapperParser.parse(mapping);
}
/** /**
* Test for https://github.com/elasticsearch/elasticsearch-mapper-attachments/issues/104 * Test for https://github.com/elasticsearch/elasticsearch-mapper-attachments/issues/104
*/ */
@ -91,6 +104,16 @@ public class VariousDocTest extends AttachmentUnitTestCase {
testMapper("text-in-english.txt", false); testMapper("text-in-english.txt", false);
} }
/**
* Test for ASCIIDOC
* Not yet supported by Tika: https://github.com/elasticsearch/elasticsearch-mapper-attachments/issues/29
*/
@Test
public void testAsciidocDocument() throws Exception {
testTika("asciidoc.asciidoc", false);
testMapper("asciidoc.asciidoc", false);
}
protected void testTika(String filename, boolean errorExpected) { protected void testTika(String filename, boolean errorExpected) {
Tika tika = tika(); Tika tika = tika();
assumeTrue("Tika has been disabled. Ignoring test...", tika != null); assumeTrue("Tika has been disabled. Ignoring test...", tika != null);
@ -107,11 +130,6 @@ public class VariousDocTest extends AttachmentUnitTestCase {
} }
protected void testMapper(String filename, boolean errorExpected) throws IOException { protected void testMapper(String filename, boolean errorExpected) throws IOException {
DocumentMapperParser mapperParser = MapperTestUtils.newMapperParser(ImmutableSettings.builder().build());
mapperParser.putTypeParser(AttachmentMapper.CONTENT_TYPE, new AttachmentMapper.TypeParser());
String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/attachment/test/unit/various-doc/test-mapping.json");
DocumentMapper docMapper = mapperParser.parse(mapping);
byte[] html = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/attachment/test/sample-files/" + filename); byte[] html = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/attachment/test/sample-files/" + filename);
BytesReference json = jsonBuilder() BytesReference json = jsonBuilder()
@ -126,7 +144,20 @@ public class VariousDocTest extends AttachmentUnitTestCase {
ParseContext.Document doc = docMapper.parse(json).rootDoc(); ParseContext.Document doc = docMapper.parse(json).rootDoc();
if (!errorExpected) { if (!errorExpected) {
assertThat(doc.get(docMapper.mappers().smartName("file").mapper().names().indexName()), not(isEmptyOrNullString())); assertThat(doc.get(docMapper.mappers().smartName("file").mapper().names().indexName()), not(isEmptyOrNullString()));
logger.debug("extracted content: {}", doc.get(docMapper.mappers().smartName("file").mapper().names().indexName())); logger.debug("-> extracted content: {}", doc.get(docMapper.mappers().smartName("file").mapper().names().indexName()));
logger.debug("-> extracted metadata:");
printMetadataContent(doc, AUTHOR);
printMetadataContent(doc, CONTENT_LENGTH);
printMetadataContent(doc, CONTENT_TYPE);
printMetadataContent(doc, DATE);
printMetadataContent(doc, KEYWORDS);
printMetadataContent(doc, LANGUAGE);
printMetadataContent(doc, NAME);
printMetadataContent(doc, TITLE);
} }
} }
private void printMetadataContent(ParseContext.Document doc, String field) {
logger.debug("- [{}]: [{}]", field, doc.get(docMapper.mappers().smartName("file." + field).mapper().names().indexName()));
}
} }

View File

@ -0,0 +1,5 @@
[[tika-asciidoc]]
= AsciiDoc test
Here is a test of the asciidoc format.