### Question:
JsonExporter implements Exporter { @Override public StringBuilder exportOneMetric(MetricRegistry.Type scope, MetricID metricID) { MetricRegistry registry = MetricRegistries.get(scope); Map<MetricID, Metric> metricMap = registry.getMetrics(); Map<String, Metadata> metadataMap = registry.getMetadata(); Metric m = metricMap.get(metricID); Map<MetricID, Metric> outMap = new HashMap<>(1); outMap.put(metricID, m); JsonObjectBuilder root = JsonProviderHolder.get().createObjectBuilder(); exportMetricsForMap(outMap, metadataMap) .forEach(root::add); return stringify(root.build()); } JsonExporter(); @Override StringBuilder exportOneScope(MetricRegistry.Type scope); @Override StringBuilder exportAllScopes(); @Override StringBuilder exportOneMetric(MetricRegistry.Type scope, MetricID metricID); @Override StringBuilder exportMetricsByName(MetricRegistry.Type scope, String name); @Override String getContentType();  }### Answer:
@Test public void testExportOfDifferentMeterImplementations() { JsonExporter exporter = new JsonExporter(); MetricRegistry applicationRegistry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); Meter[] meters = { new MeterImpl(), new SomeMeter() }; int idx = 0; for (Meter m : meters) { String name = "meter_" + idx++; applicationRegistry.register(name, m); StringBuilder out = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID(name)); assertNotNull(out); List<String> lines = Arrays.asList(out.toString().split(LINE_SEPARATOR)); assertEquals(1, lines.stream().filter(line -> line.contains("\"" + name + "\"")).count()); assertEquals(1, lines.stream().filter(line -> line.contains("\"count\": 0")).count()); } }
@Test public void testExportOfDifferentHistogramImplementations() { JsonExporter exporter = new JsonExporter(); MetricRegistry applicationRegistry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); Histogram[] histograms = { new HistogramImpl(new ExponentiallyDecayingReservoir()), new SomeHistogram() }; int idx = 0; for (Histogram h : histograms) { String name = "histo_" + idx++; applicationRegistry.register(name, h); StringBuilder out = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID(name)); assertNotNull(out); System.out.println(out.toString()); List<String> lines = Arrays.asList(out.toString().split(LINE_SEPARATOR)); assertEquals(1, lines.stream().filter(line -> line.contains("\"" + name + "\"")).count()); assertEquals(1, lines.stream().filter(line -> line.contains("\"count\": 0")).count()); } }
@Test public void testExportOfDifferentTimerImplementations() { JsonExporter exporter = new JsonExporter(); MetricRegistry applicationRegistry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); Timer[] timers = { new TimerImpl(), new SomeTimer() }; int idx = 0; for (Timer t : timers) { String name = "json_timer_" + idx++; applicationRegistry.register(name, t); StringBuilder out = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID(name)); assertNotNull(out); List<String> lines = Arrays.asList(out.toString().split(LINE_SEPARATOR)); assertEquals(1, lines.stream().filter(line -> line.contains("\"" + name + "\"")).count()); assertEquals(1, lines.stream().filter(line -> line.contains("\"count\": 0")).count()); } }