package org.zwobble.mammoth.tests; import org.junit.Test; import org.zwobble.mammoth.DocumentConverter; import org.zwobble.mammoth.Result; import org.zwobble.mammoth.internal.styles.parsing.ParseException; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.function.Function; import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat; import static org.zwobble.mammoth.internal.util.Lists.list; import static org.zwobble.mammoth.tests.ResultMatchers.isResult; import static org.zwobble.mammoth.tests.ResultMatchers.isSuccess; import static org.zwobble.mammoth.tests.util.MammothAsserts.assertThrows; public class MammothTests { @Test public void docxContainingOneParagraphIsConvertedToSingleParagraphElement() throws IOException { assertThat( convertToHtml("single-paragraph.docx"), isSuccess("
Walking on imported air
")); } @Test public void canReadFilesWithUtf8Bom() throws IOException { assertThat( convertToHtml("utf8-bom.docx"), isSuccess("This XML has a byte order mark.
")); } @Test public void emptyParagraphsAreIgnoredByDefault() throws IOException { assertThat( convertToHtml("empty.docx"), isSuccess("")); } @Test public void emptyParagraphsArePreservedIfIgnoreEmptyParagraphsIsFalse() throws IOException { assertThat( convertToHtml("empty.docx", mammoth -> mammoth.preserveEmptyParagraphs()), isSuccess("")); } @Test public void simpleListIsConvertedToListElements() throws IOException { assertThat( convertToHtml("simple-list.docx"), isSuccess("Above
" + "Top left | Top right |
Bottom left | Bottom right |
Below
")); } @Test public void inlineImagesAreIncludedInOutput() throws IOException { assertThat( convertToHtml("tiny-picture.docx"), isSuccess("Datum plane
")); } @Test public void canUseCustomStyleMap() throws IOException { assertThat( convertToHtml("underline.docx", mammoth -> mammoth.addStyleMap("u => em")), isSuccess("The Sunset Tree
")); } @Test public void mostRecentlyAddedStyleMapTakesPrecedence() throws IOException { assertThat( convertToHtml("underline.docx", mammoth -> mammoth.addStyleMap("u => em").addStyleMap("u => strong")), isSuccess("The Sunset Tree
")); } @Test public void rulesFromPreviouslyAddedStyleMapsStillTakeEffectIfNotOverriden() throws IOException { assertThat( convertToHtml("underline.docx", mammoth -> mammoth.addStyleMap("u => em").addStyleMap("strike => del")), isSuccess("The Sunset Tree
")); } @Test public void errorIsRaisedIfStyleMapCannotBeParsed() throws IOException { RuntimeException exception = assertThrows( ParseException.class, () -> new DocumentConverter().addStyleMap("p =>\np[style-name=] =>")); assertThat( exception.getMessage(), equalTo( "error reading style map at line 2, character 14: expected token of type STRING but was of type CLOSE_SQUARE_BRACKET\n\n" + "p[style-name=] =>\n" + " ^")); } @Test public void canDisableDefaultStyleMap() throws IOException { assertThat( convertToHtml("simple-list.docx", mammoth -> mammoth.disableDefaultStyleMap()), isResult( equalTo("Apple
Banana
"), list("Unrecognised paragraph style: List Paragraph (Style ID: ListParagraph)"))); } @Test public void canExtractRawTextFromFile() throws IOException { assertThat( new DocumentConverter().extractRawText(TestData.file("simple-list.docx")), isSuccess("Apple\n\nBanana\n\n")); } @Test public void canExtractRawTextFromStream() throws IOException { assertThat( new DocumentConverter().extractRawText(TestData.file("simple-list.docx").toURI().toURL().openStream()), isSuccess("Apple\n\nBanana\n\n")); } private Result