CsvStatisticsWriter implements StatisticsOutputter { @Override public void finaliseOutput() throws IOException { if (algorithmNames == null) { throw new IllegalStateException(getClass().getSimpleName() + " not initialised."); } writeHeader(); for (String bandName : statisticsContainer.getBandNames()) { final BandStatistics bandStatistics = statisticsContainer.getDataForBandName(bandName); for (String regionName : bandStatistics.getRegionNames()) { csvOutput.append(regionName) .append("\t") .append(bandName); for (String algorithmName : algorithmNames) { csvOutput.append("\t"); final RegionStatistics dataForRegionName = bandStatistics.getDataForRegionName(regionName); if (dataForRegionName.containsAlgorithm(algorithmName)) { final Number numberValue = dataForRegionName.getDataForAlgorithmName(algorithmName); csvOutput.append(getValueAsString(numberValue)); } } csvOutput.append("\n"); } } } CsvStatisticsWriter(PrintStream csvOutput); @Override void initialiseOutput(StatisticsOutputContext statisticsOutputContext); @Override void addToOutput(String bandName, String regionId, Map<String, Number> statistics); @Override void finaliseOutput();  }
@Test public void testFinaliseOutput() throws Exception { csvStatisticsWriter.initialiseOutput(StatisticsOutputContext.create(null, new String[]{ "p90", "p95", "max", "min" })); addOutput(); csvStatisticsWriter.finaliseOutput(); csvStream.close(); String actualOutput = csvOutput.toString(); assertTrue(actualOutput.startsWith("# Region\tBand\tmax\tmin\tp90\tp95\n")); assertTrue(actualOutput.contains("werdohl\tnormalised_cow_density_index_(ncdi)\t\t\t2.0000\t3.0000\n")); assertTrue(actualOutput.contains("bielefeld\tnormalised_cow_density_index_(ncdi)\t\t\t1.0000\t3.0000\n")); assertTrue(actualOutput.contains("bielefeld\tnormalised_pig_density_index_(npdi)\t3.0000\t0.5000\t1.0000\t2.0000\n")); }