From 07d6f0cc788962d9faaec575da30cbdefdbe9637 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 13 Feb 2016 23:09:03 +0000 Subject: Extract common test code for testing image reading --- .../zwobble/mammoth/tests/docx/BodyXmlTests.java | 35 ++++++++++++---------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'src/test') diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java index 0683bff..9f7c6c5 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java @@ -19,6 +19,7 @@ import java.io.InputStreamReader; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.function.Function; import static com.natpryce.makeiteasy.MakeItEasy.*; import static java.util.Arrays.asList; @@ -387,26 +388,18 @@ public class BodyXmlTests { @Test public void canReadImagedataElementsWithIdAttribute() throws IOException { - XmlElement element = element("v:imagedata", map("r:id", "rId5", "o:title", "It's a hat")); - Relationships relationships = new Relationships(map( - "rId5", new Relationship("media/hat.png"))); - String imageBytes = "Not an image at all!"; - DocxFile file = new InMemoryDocxFile(map("word/media/hat.png", imageBytes)); - - Image image = (Image) readSuccess( - a(bodyReader, with(RELATIONSHIPS, relationships), with(DOCX_FILE, file)), - element); - assertThat(image, allOf( - hasProperty("altText", deepEquals(Optional.of("It's a hat"))), - hasProperty("contentType", deepEquals(Optional.of("image/png"))))); - assertThat( - CharStreams.toString(new InputStreamReader(image.open())), - equalTo("Not an image at all!")); + assertCanReadEmbeddedImage(image -> + element("v:imagedata", map("r:id", image.relationshipId, "o:title", image.altText))); } @Test public void canReadInlinePictures() throws IOException { - XmlElement element = inlineImageXml(embeddedBlipXml("rId5"), "It's a hat"); + assertCanReadEmbeddedImage(image -> + inlineImageXml(embeddedBlipXml(image.relationshipId), image.altText)); + } + + private void assertCanReadEmbeddedImage(Function generateXml) throws IOException { + XmlElement element = generateXml.apply(new EmbeddedImage("rId5", "It's a hat")); Relationships relationships = new Relationships(map( "rId5", new Relationship("media/hat.png"))); String imageBytes = "Not an image at all!"; @@ -423,6 +416,16 @@ public class BodyXmlTests { equalTo("Not an image at all!")); } + private class EmbeddedImage { + private final String relationshipId; + private final String altText; + + public EmbeddedImage(String relationshipId, String altText) { + this.relationshipId = relationshipId; + this.altText = altText; + } + } + private XmlElement inlineImageXml(XmlElement blip, String description) { return element("w:drawing", list( element("wp:inline", imageXml(blip, description)))); -- cgit v1.2.3