SLING-10581: properly implement Closeable#close() contract for SitemapImpl
diff --git a/src/main/java/org/apache/sling/sitemap/impl/builder/SitemapImpl.java b/src/main/java/org/apache/sling/sitemap/impl/builder/SitemapImpl.java
index d034ee0..ce590a3 100644
--- a/src/main/java/org/apache/sling/sitemap/impl/builder/SitemapImpl.java
+++ b/src/main/java/org/apache/sling/sitemap/impl/builder/SitemapImpl.java
@@ -75,8 +75,10 @@
@Override
public void close() throws IOException {
+ if (closed) {
+ return;
+ }
try {
- ensureNotClosed();
closed = true;
writePendingUrl();
out.write("</urlset>");
diff --git a/src/test/java/org/apache/sling/sitemap/impl/builder/SitemapImplTest.java b/src/test/java/org/apache/sling/sitemap/impl/builder/SitemapImplTest.java
index f0a8057..c7e320b 100644
--- a/src/test/java/org/apache/sling/sitemap/impl/builder/SitemapImplTest.java
+++ b/src/test/java/org/apache/sling/sitemap/impl/builder/SitemapImplTest.java
@@ -18,21 +18,21 @@
*/
package org.apache.sling.sitemap.impl.builder;
-import org.apache.sling.sitemap.SitemapException;
-import org.apache.sling.sitemap.impl.builder.extensions.ExtensionProviderManager;
-import org.apache.sling.testing.mock.sling.junit5.SlingContext;
-import org.apache.sling.testing.mock.sling.junit5.SlingContextExtension;
-import org.jetbrains.annotations.NotNull;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
+import org.apache.sling.sitemap.SitemapException;
+import org.apache.sling.sitemap.impl.builder.extensions.ExtensionProviderManager;
+import org.apache.sling.testing.mock.sling.junit5.SlingContext;
+import org.apache.sling.testing.mock.sling.junit5.SlingContextExtension;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -142,4 +142,19 @@
SitemapException ex = assertThrows(SitemapException.class, () -> sitemap.addUrl("http://localhost:4503"));
assertThat(ex.getCause(), instanceOf(IOException.class));
}
+
+
+ @Test
+ void testWritingToClosedSitemapThrows() throws IOException {
+ // given
+ StringWriter writer = new StringWriter();
+ SitemapImpl subject = new SitemapImpl(writer, extensionManager);
+
+ // when
+ subject.close();
+
+ assertThrows(IllegalStateException.class, () -> subject.addUrl("http://localhost:4502"));
+ assertThrows(IllegalStateException.class, () -> subject.flush());
+ assertDoesNotThrow(() -> subject.close());
+ }
}
diff --git a/src/test/java/org/apache/sling/sitemap/impl/builder/UrlImplTest.java b/src/test/java/org/apache/sling/sitemap/impl/builder/UrlImplTest.java
index d2496b5..43a7137 100644
--- a/src/test/java/org/apache/sling/sitemap/impl/builder/UrlImplTest.java
+++ b/src/test/java/org/apache/sling/sitemap/impl/builder/UrlImplTest.java
@@ -148,7 +148,7 @@
}
@Test
- void testWritingUrlOrExtensionTwiceFails() throws SitemapException, IOException {
+ void testWritingToWrittenUrlThrows() throws SitemapException, IOException {
// given
StringWriter writer = new StringWriter();
SitemapImpl subject = new SitemapImpl(writer, extensionManager);
@@ -164,8 +164,6 @@
assertThrows(IllegalStateException.class, () -> url.setChangeFrequency(Url.ChangeFrequency.ALWAYS));
assertThrows(IllegalStateException.class, () -> url.setLastModified(now));
assertThrows(IllegalStateException.class, () -> url.addExtension(TestExtension.class));
- assertThrows(IllegalStateException.class, () -> subject.addUrl("http://foo.bar"));
- assertThrows(IllegalStateException.class, subject::close);
}
interface TestExtension2 extends Extension {