package org.zwobble.mammoth.tests; import org.junit.Test; import org.zwobble.mammoth.Mammoth; import org.zwobble.mammoth.results.Result; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat; import static org.zwobble.mammoth.results.Result.success; import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; public class MammothTests { @Test public void docxContainingOneParagraphIsConvertedToSingleParagraphElement() throws IOException { assertThat( convertToHtml("single-paragraph.docx"), deepEquals(success("

Walking on imported air

"))); } @Test public void canReadFilesWithUtf8Bom() throws IOException { assertThat( convertToHtml("utf8-bom.docx"), deepEquals(success("

This XML has a byte order mark.

"))); } @Test public void emptyParagraphsAreIgnoredByDefault() throws IOException { assertThat( convertToHtml("empty.docx"), deepEquals(success(""))); } @Test public void emptyParagraphsArePreservedIfIgnoreEmptyParagraphsIsFalse() throws IOException { assertThat( convertToHtml("empty.docx", Mammoth.Options.DEFAULT.preserveEmptyParagraphs()), deepEquals(success("

"))); } @Test public void simpleListIsConvertedToListElements() throws IOException { assertThat( convertToHtml("simple-list.docx"), deepEquals(success(""))); } @Test public void wordTablesAreConvertedToHtmlTables() throws IOException { assertThat( convertToHtml("tables.docx"), deepEquals(success( "

Above

" + "" + "" + "" + "

Top left

Top right

Bottom left

Bottom right

" + "

Below

"))); } @Test public void inlineImagesAreIncludedInOutput() throws IOException { assertThat( convertToHtml("tiny-picture.docx"), deepEquals(success("

"))); } @Test public void imagesStoredOutsideOfDocumentAreIncludedInOutput() throws IOException { Path tempDirectory = Files.createTempDirectory("mammoth-"); try { Path documentPath = tempDirectory.resolve("external-picture.docx"); Files.copy(TestData.file("external-picture.docx").toPath(), documentPath); Files.copy(TestData.file("tiny-picture.png").toPath(), tempDirectory.resolve("tiny-picture.png")); assertThat( Mammoth.convertToHtml(documentPath.toFile()), deepEquals(success("

"))); } finally { tempDirectory.toFile().delete(); } } @Test public void warnIfDocumentHasImagesStoredOutsideOfDocumentWhenPathOfDocumentIsUnknown() throws IOException { Path tempDirectory = Files.createTempDirectory("mammoth-"); try { Path documentPath = tempDirectory.resolve("external-picture.docx"); Files.copy(TestData.file("external-picture.docx").toPath(), documentPath); assertThat( Mammoth.convertToHtml(documentPath.toUri().toURL().openStream()), allOf( hasProperty("value", equalTo("")), hasProperty("warnings", contains( hasProperty("message", startsWith("could not open external image 'tiny-picture.png': path of document is unknown, but is required for relative URI")))))); } finally { tempDirectory.toFile().delete(); } } @Test public void warnIfImagesStoredOutsideOfDocumentAreNotFound() throws IOException { Path tempDirectory = Files.createTempDirectory("mammoth-"); try { Path documentPath = tempDirectory.resolve("external-picture.docx"); Files.copy(TestData.file("external-picture.docx").toPath(), documentPath); assertThat( Mammoth.convertToHtml(documentPath.toFile()), allOf( hasProperty("value", equalTo("")), hasProperty("warnings", contains( hasProperty("message", startsWith("could not open external image 'tiny-picture.png'")))))); } finally { tempDirectory.toFile().delete(); } } @Test public void contentTypesAreRead() throws IOException { assertThat( convertToHtml("tiny-picture-custom-content-type.docx"), deepEquals(success("

"))); } @Test public void footnotesAreAppendedToText() throws IOException { assertThat( convertToHtml("footnotes.docx", Mammoth.Options.DEFAULT.idPrefix("doc-42-")), deepEquals(success( "

Ouch" + "[1]." + "[2]

" + "
  1. A tachyon walks into a bar. ↑

  2. " + "
  3. Fin. ↑

"))); } @Test public void endNotesAreAppendedToText() throws IOException { assertThat( convertToHtml("endnotes.docx", Mammoth.Options.DEFAULT.idPrefix("doc-42-")), deepEquals(success( "

Ouch" + "[1]." + "[2]

" + "
  1. A tachyon walks into a bar. ↑

  2. " + "
  3. Fin. ↑

"))); } @Test public void relationshipsAreReadForEachFileContainingBodyXml() throws IOException { assertThat( convertToHtml("footnote-hyperlink.docx", Mammoth.Options.DEFAULT.idPrefix("doc-42-")), deepEquals(success( "

[1]

" + "
  1. Example ↑

"))); } @Test public void textBoxesAreRead() throws IOException { assertThat( convertToHtml("text-box.docx"), deepEquals(success("

Datum plane

"))); } @Test public void canExtractRawText() throws IOException { assertThat( Mammoth.extractRawText(TestData.file("simple-list.docx")), deepEquals(success("Apple\n\nBanana\n\n"))); } private Result convertToHtml(String name) throws IOException { File file = TestData.file(name); return Mammoth.convertToHtml(file); } private Result convertToHtml(String name, Mammoth.Options options) throws IOException { File file = TestData.file(name); return Mammoth.convertToHtml(file, options); } }