From 275dafcd9f4911c3d71d3dc7b86d68e62f06f86a Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 15 Jan 2015 11:10:10 +0100 Subject: SONAR-6056 API: closeable components must be closed when stopping picocontainer --- .../sonar/api/platform/ComponentContainerTest.java | 54 ++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'sonar-plugin-api/src/test') diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/platform/ComponentContainerTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/platform/ComponentContainerTest.java index 4135639a877..44660bf61c7 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/platform/ComponentContainerTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/platform/ComponentContainerTest.java @@ -288,6 +288,37 @@ public class ComponentContainerTest { assertThat(component.stopped).isTrue(); } + /** + * Method close() must be called even if the methods start() or stop() + * are not defined. + */ + @Test + public void should_close_components_without_lifecycle() throws Exception { + ComponentContainer container = new ComponentContainer(); + CloseableComponent component = new CloseableComponent(); + container.add(component); + + container.execute(); + + assertThat(component.isClosed).isTrue(); + } + + /** + * Method close() must be executed after stop() + */ + @Test + public void should_close_components_with_lifecycle() throws Exception { + ComponentContainer container = new ComponentContainer(); + StartableCloseableComponent component = new StartableCloseableComponent(); + container.add(component); + + container.execute(); + + assertThat(component.isStopped).isTrue(); + assertThat(component.isClosed).isTrue(); + assertThat(component.isClosedAfterStop).isTrue(); + } + public static class StartableComponent { public boolean started = false, stopped = false; @@ -333,4 +364,27 @@ public class ComponentContainerTest { return new SimpleComponent(); } } + + public static class CloseableComponent implements AutoCloseable { + public boolean isClosed = false; + + @Override + public void close() throws Exception { + isClosed = true; + } + } + + public static class StartableCloseableComponent implements AutoCloseable { + public boolean isClosed = false, isStopped = false, isClosedAfterStop = false; + + public void stop() { + isStopped = true; + } + + @Override + public void close() throws Exception { + isClosed = true; + isClosedAfterStop = isStopped; + } + } } -- cgit v1.2.3