summaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-02-13 23:32:26 +0000
committerMichael Williamson <mike@zwobble.org>2016-02-13 23:32:26 +0000
commitc46ec78de9524a7b7df0b475b1447a83b4f857fe (patch)
treec98c58f6d4aa8cde8824bbfe3b73131143f1d3cc /src/test/java
parent6dd319ca5eebe1f97b9558b5b45bbccf402eb319 (diff)
Read linked blips
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlReaderMakers.java7
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java25
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/docx/InMemoryFileReader.java24
3 files changed, 55 insertions, 1 deletions
diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlReaderMakers.java b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlReaderMakers.java
index 41cefd5..f19f505 100644
--- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlReaderMakers.java
+++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlReaderMakers.java
@@ -15,6 +15,7 @@ public class BodyXmlReaderMakers {
public static final Property<BodyXmlReader, Relationships> RELATIONSHIPS = newProperty();
public static final Property<BodyXmlReader, ContentTypes> CONTENT_TYPES = newProperty();
public static final Property<BodyXmlReader, DocxFile> DOCX_FILE = newProperty();
+ public static final Property<BodyXmlReader, FileReader> FILE_READER = newProperty();
public static final Instantiator<BodyXmlReader> bodyReader =
propertyLookup -> new BodyXmlReader(
@@ -31,5 +32,11 @@ public class BodyXmlReaderMakers {
@Override
public void close() throws IOException {
}
+ }),
+ propertyLookup.valueOf(FILE_READER, new FileReader() {
+ @Override
+ public InputStream getInputStream(String uri) throws IOException {
+ throw new UnsupportedOperationException();
+ }
}));
}
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 49542ec..5c4c40d 100644
--- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java
+++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java
@@ -419,7 +419,7 @@ public class BodyXmlTests {
hasProperty("contentType", deepEquals(Optional.of("image/png")))));
assertThat(
CharStreams.toString(new InputStreamReader(image.open())),
- equalTo("Not an image at all!"));
+ equalTo(imageBytes));
}
private class EmbeddedImage {
@@ -446,11 +446,30 @@ public class BodyXmlTests {
with(DOCX_FILE, file),
with(CONTENT_TYPES, contentTypes)),
element);
+
assertThat(
result,
hasProperty("warnings", deepEquals(list(warning("Image of type image/x-emf is unlikely to display in web browsers")))));
}
+ @Test
+ public void canReadLinkedPictures() throws IOException {
+ XmlElement element = inlineImageXml(linkedBlipXml("rId5"), "");
+ Relationships relationships = new Relationships(map(
+ "rId5", new Relationship("file:///media/hat.png")));
+ String imageBytes = "Not an image at all!";
+
+ Image image = (Image) readSuccess(
+ a(bodyReader,
+ with(RELATIONSHIPS, relationships),
+ with(FILE_READER, new InMemoryFileReader(map("file:///media/hat.png", imageBytes)))),
+ element);
+
+ assertThat(
+ CharStreams.toString(new InputStreamReader(image.open())),
+ equalTo(imageBytes));
+ }
+
private XmlElement inlineImageXml(XmlElement blip, String description) {
return element("w:drawing", list(
element("wp:inline", imageXml(blip, description))));
@@ -474,6 +493,10 @@ public class BodyXmlTests {
return blipXml(map("r:embed", relationshipId));
}
+ private XmlElement linkedBlipXml(String relationshipId) {
+ return blipXml(map("r:link", relationshipId));
+ }
+
private XmlElement blipXml(Map<String, String> attributes) {
return element("a:blip", attributes);
}
diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/InMemoryFileReader.java b/src/test/java/org/zwobble/mammoth/tests/docx/InMemoryFileReader.java
new file mode 100644
index 0000000..7b716cc
--- /dev/null
+++ b/src/test/java/org/zwobble/mammoth/tests/docx/InMemoryFileReader.java
@@ -0,0 +1,24 @@
+package org.zwobble.mammoth.tests.docx;
+
+import org.zwobble.mammoth.docx.FileReader;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Map;
+import java.util.Optional;
+
+public class InMemoryFileReader implements FileReader {
+ private final Map<String, String> entries;
+
+ public InMemoryFileReader(Map<String, String> entries) {
+ this.entries = entries;
+ }
+
+ @Override
+ public InputStream getInputStream(String name) throws IOException {
+ String value = Optional.ofNullable(entries.get(name)).get();
+ return new ByteArrayInputStream(value.getBytes(StandardCharsets.UTF_8));
+ }
+}