From 991e70f83b2a6b6b8b5b1191e52ada93f733c600 Mon Sep 17 00:00:00 2001 From: "Kevin S. Clarke" Date: Tue, 3 Sep 2024 23:34:10 -0400 Subject: [PATCH] Add more ContextList tests --- .../iiif/presentation/v3/ContextList.java | 7 ++- .../iiif/presentation/v3/ContextListTest.java | 46 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/main/java/info/freelibrary/iiif/presentation/v3/ContextList.java b/src/main/java/info/freelibrary/iiif/presentation/v3/ContextList.java index a57fd188..1d916bba 100644 --- a/src/main/java/info/freelibrary/iiif/presentation/v3/ContextList.java +++ b/src/main/java/info/freelibrary/iiif/presentation/v3/ContextList.java @@ -312,8 +312,11 @@ public URI set(final int aIndex, final URI aURI) { if (PRESENTATION_CONTEXT_URI.equals(aURI)) { if (aIndex != 0) { throw new IndexOutOfBoundsException(LOGGER.getMessage(MessageCodes.JPA_149)); - } // else, just ignore -- this should be the current index of the default context - } else if (aIndex != 0) { + } + + return aURI; + } + if (aIndex != 0) { return super.set(aIndex, aURI); } diff --git a/src/test/java/info/freelibrary/iiif/presentation/v3/ContextListTest.java b/src/test/java/info/freelibrary/iiif/presentation/v3/ContextListTest.java index ab858e7a..86770cbb 100644 --- a/src/test/java/info/freelibrary/iiif/presentation/v3/ContextListTest.java +++ b/src/test/java/info/freelibrary/iiif/presentation/v3/ContextListTest.java @@ -1,6 +1,7 @@ package info.freelibrary.iiif.presentation.v3; +import static info.freelibrary.iiif.presentation.v3.ContextList.PRESENTATION_CONTEXT_URI; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; @@ -15,6 +16,7 @@ import java.util.Collection; import java.util.Comparator; import java.util.List; +import java.util.NoSuchElementException; import java.util.function.Predicate; import java.util.function.UnaryOperator; @@ -337,6 +339,18 @@ public final void testRemoveIfPredicateOfQsuperURI() { assertTrue(contexts.contains(kept)); } + /** + * Test method for {@link ContextList#removeIf(Predicate)}. + */ + @Test + public final void testRemoveIfPredicateOfQsuperUriNoDefault() { + final ContextList contexts = new ContextList(myContexts); + + contexts.removeIf(uri -> !uri.equals(PRESENTATION_CONTEXT_URI)); + assertEquals(1, contexts.size()); + assertTrue(contexts.contains(PRESENTATION_CONTEXT_URI)); + } + /** * Test method for {@link ContextList#remove(int)}. */ @@ -365,6 +379,14 @@ public final void testRemoveLast() { assertFalse(contexts.contains(uri)); } + /** + * Test method for {@link ContextList#removeLast()}. + */ + @Test(expected = NoSuchElementException.class) + public final void testRemoveLastWhenEmpty() { + new ContextList().removeLast(); + } + /** * Test method for {@link ContextList#remove(Object)}. */ @@ -427,6 +449,30 @@ public final void testSetIntURI() { assertEquals(uri, contexts.get(1)); } + /** + * Test method for {@link ContextList#set(int, URI)}. + */ + @Test(expected = IndexOutOfBoundsException.class) + public final void testSetIntURIException() { + new ContextList(myContexts).set(1, PRESENTATION_CONTEXT_URI); + } + + /** + * Test method for {@link ContextList#set(int, URI)}. + */ + @Test(expected = IndexOutOfBoundsException.class) + public final void testSetIntURIException2() { + new ContextList(myContexts).set(0, URI.create(getURL())); + } + + /** + * Test method for {@link ContextList#set(int, URI)}. + */ + @Test + public final void testSetIntUriStatusQuo() { + new ContextList(myContexts).set(0, PRESENTATION_CONTEXT_URI); + } + /** * Test method for {@link ContextList#sort(Comparator)}. */