SLING-7062 - Commons metrics inventory closes zip

Instead of closing just flush the internal writer

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1805597 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/JSONReporter.java b/src/main/java/org/apache/sling/commons/metrics/internal/JSONReporter.java
index d501fb2..1b24b3e 100644
--- a/src/main/java/org/apache/sling/commons/metrics/internal/JSONReporter.java
+++ b/src/main/java/org/apache/sling/commons/metrics/internal/JSONReporter.java
@@ -155,7 +155,7 @@
 
     @Override
     public void close(){
-        pw.close();
+        pw.flush();
     }
 
     private void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters,
diff --git a/src/test/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePluginTest.java b/src/test/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePluginTest.java
index 6b73d91..8f0c3ce 100644
--- a/src/test/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePluginTest.java
+++ b/src/test/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePluginTest.java
@@ -21,6 +21,7 @@
 
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.io.Writer;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
@@ -37,6 +38,7 @@
 import com.gargoylesoftware.htmlunit.html.HtmlPage;
 import com.gargoylesoftware.htmlunit.html.HtmlTable;
 import org.apache.felix.inventory.Format;
+import org.apache.felix.utils.json.JSONParser;
 import org.apache.sling.testing.mock.osgi.MockOsgi;
 import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
 import org.junit.Rule;
@@ -49,6 +51,9 @@
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 public class MetricWebConsolePluginTest {
@@ -111,11 +116,30 @@
         activatePlugin();
 
         StringWriter sw = new StringWriter();
-        plugin.print(new PrintWriter(sw), Format.TEXT, false);
+        PrintWriter pw = spy(new PrintWriter(sw));
+        plugin.print(pw, Format.TEXT, false);
 
         String out = sw.toString();
         assertThat(out, containsString("foo:test1"));
         assertThat(out, containsString("Meters"));
+        verify(pw, never()).close();
+    }
+
+    @Test
+    public void inventory_json() throws Exception{
+        MetricRegistry reg1 = new MetricRegistry();
+        reg1.meter("test1").mark(5);
+        context.registerService(MetricRegistry.class, reg1, regProps("foo"));
+
+        activatePlugin();
+
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = spy(new PrintWriter(sw));
+        plugin.print(pw, Format.JSON, false);
+
+        Map<String, Object> json = new JSONParser(sw.toString()).getParsed();
+        assertTrue(json.containsKey("meters"));
+        verify(pw, never()).close();
     }
 
     @Test
@@ -160,4 +184,17 @@
     private void activatePlugin() {
         MockOsgi.activate(plugin, context.bundleContext(), Collections.<String, Object>emptyMap());
     }
+
+    private static class CloseRecordingWriter extends PrintWriter {
+
+
+        public CloseRecordingWriter(Writer out) {
+            super(out);
+        }
+
+        @Override
+        public void close() {
+            super.close();
+        }
+    }
 }