diff options
Diffstat (limited to 'src')
4 files changed, 42 insertions, 1 deletions
diff --git a/src/main/java/org/zwobble/mammoth/internal/docx/Relationships.java b/src/main/java/org/zwobble/mammoth/internal/docx/Relationships.java index b1f2279..7e8c14c 100644 --- a/src/main/java/org/zwobble/mammoth/internal/docx/Relationships.java +++ b/src/main/java/org/zwobble/mammoth/internal/docx/Relationships.java @@ -15,6 +15,7 @@ public class Relationships { } public Relationship findRelationshipById(String id) { - return lookup(relationships, id).get(); + return lookup(relationships, id) + .orElseThrow(() -> new RuntimeException("Could not find relationship '" + id + "'")); } } diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/RelationshipsTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/RelationshipsTests.java new file mode 100644 index 0000000..f9a7672 --- /dev/null +++ b/src/test/java/org/zwobble/mammoth/tests/docx/RelationshipsTests.java @@ -0,0 +1,19 @@ +package org.zwobble.mammoth.tests.docx; + +import org.junit.Test; +import org.zwobble.mammoth.internal.docx.Relationships; + +import static org.junit.Assert.assertEquals; +import static org.zwobble.mammoth.internal.util.MammothMaps.map; +import static org.zwobble.mammoth.tests.util.MammothAsserts.assertThrows; + +public class RelationshipsTests { + @Test + public void exceptionIsThrownIfRelationshipCannotBeFound() { + Relationships relationships = new Relationships(map()); + RuntimeException exception = assertThrows( + RuntimeException.class, + () -> relationships.findRelationshipById("rId5")); + assertEquals("Could not find relationship 'rId5'", exception.getMessage()); + } +} diff --git a/src/test/java/org/zwobble/mammoth/tests/util/Action.java b/src/test/java/org/zwobble/mammoth/tests/util/Action.java new file mode 100644 index 0000000..c99a770 --- /dev/null +++ b/src/test/java/org/zwobble/mammoth/tests/util/Action.java @@ -0,0 +1,6 @@ +package org.zwobble.mammoth.tests.util; + +@FunctionalInterface +public interface Action { + void run(); +} diff --git a/src/test/java/org/zwobble/mammoth/tests/util/MammothAsserts.java b/src/test/java/org/zwobble/mammoth/tests/util/MammothAsserts.java new file mode 100644 index 0000000..2439ee2 --- /dev/null +++ b/src/test/java/org/zwobble/mammoth/tests/util/MammothAsserts.java @@ -0,0 +1,15 @@ +package org.zwobble.mammoth.tests.util; + +import org.zwobble.mammoth.internal.util.Casts; + +public class MammothAsserts { + public static <T extends Throwable> T assertThrows(Class<T> exceptionType, Action action) throws T { + try { + action.run(); + throw new AssertionError("Expected exception"); + } catch (RuntimeException exception) { + return Casts.tryCast(exceptionType, exception) + .orElseThrow(() -> exception); + } + } +} |
