From f5ffdd2417a8c8dd27f3c623e10cde961163c04a Mon Sep 17 00:00:00 2001 From: Keith Wall Date: Fri, 15 Jun 2012 13:57:53 +0000 Subject: QPID-3977: ChartingUtil now generates chart-summary.html file to facilitate chart png browsing from CI server. Applied patch from Philip Harvey git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1350625 13f79535-47bb-0310-9956-ffa450edef68 --- qpid/java/perftests/visualisation-jfc/build.xml | 3 +- .../qpid/disttest/charting/ChartingUtil.java | 2 + .../qpid/disttest/charting/writer/ChartWriter.java | 64 +++++++++++ .../charting/chartbuilder/ChartProductionTest.java | 6 +- .../disttest/charting/writer/ChartWriterTest.java | 117 +++++++++++++++++++++ .../charting/writer/expected-chart-summary.html | 9 ++ 6 files changed, 196 insertions(+), 5 deletions(-) create mode 100644 qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/ChartWriterTest.java create mode 100644 qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/expected-chart-summary.html (limited to 'qpid/java/perftests/visualisation-jfc') diff --git a/qpid/java/perftests/visualisation-jfc/build.xml b/qpid/java/perftests/visualisation-jfc/build.xml index a9fd6c4102..02c9f5dcbd 100644 --- a/qpid/java/perftests/visualisation-jfc/build.xml +++ b/qpid/java/perftests/visualisation-jfc/build.xml @@ -18,7 +18,8 @@ --> - + + diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/ChartingUtil.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/ChartingUtil.java index f46bc45583..e00859855e 100644 --- a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/ChartingUtil.java +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/ChartingUtil.java @@ -100,6 +100,8 @@ public class ChartingUtil JFreeChart chart = chartBuilder.buildChart(chartingDefinition); writer.writeChartToFileSystem(chart, chartingDefinition.getChartStemName()); } + + writer.writeHtmlSummaryToFileSystem(); } private List loadChartDefinitions(String chartingDefsDir) diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/writer/ChartWriter.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/writer/ChartWriter.java index 14aa5f3a37..134933ef50 100644 --- a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/writer/ChartWriter.java +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/writer/ChartWriter.java @@ -20,10 +20,14 @@ package org.apache.qpid.disttest.charting.writer; import java.io.BufferedOutputStream; +import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; import org.apache.qpid.disttest.charting.ChartingException; import org.jfree.chart.ChartUtilities; @@ -34,7 +38,11 @@ import org.slf4j.LoggerFactory; public class ChartWriter { private static final Logger LOGGER = LoggerFactory.getLogger(ChartWriter.class); + + static final String SUMMARY_FILE_NAME = "chart-summary.html"; + private File _chartDirectory = new File("."); + private List _chartFiles = new ArrayList(); public void writeChartToFileSystem(JFreeChart chart, String chartStemName) { @@ -47,6 +55,8 @@ public class ChartWriter ChartUtilities.writeChartAsPNG(pngOutputStream, chart, 600, 400, true, 0); pngOutputStream.close(); + _chartFiles.add(pngFile); + LOGGER.info("Written {} chart", pngFile); } catch (IOException e) @@ -69,6 +79,60 @@ public class ChartWriter } } + public void writeHtmlSummaryToFileSystem() + { + if(_chartFiles.size() < 2) + { + LOGGER.info("Only " + _chartFiles.size() + " chart image(s) have been written so no HTML summary file will be produced"); + return; + } + + String htmlHeader = + "\n" + + " \n" + + " Performance Charts\n" + + " \n" + + " \n"; + + String htmlFooter = + " \n" + + ""; + + BufferedWriter writer = null; + try + { + File summaryFile = new File(_chartDirectory, SUMMARY_FILE_NAME); + LOGGER.debug("About to produce HTML summary file " + summaryFile.getAbsolutePath() + " from charts " + _chartFiles); + + writer = new BufferedWriter(new FileWriter(summaryFile)); + writer.write(htmlHeader); + for (File chartFile : _chartFiles) + { + writer.write(" \n"); + } + writer.write(htmlFooter); + writer.close(); + } + catch (Exception e) + { + throw new ChartingException("Failed to create HTML summary file", e); + } + finally + { + if(writer != null) + { + try + { + writer.close(); + } + catch(IOException e) + { + throw new ChartingException("Failed to create HTML summary file", e); + } + } + } + } + public void setOutputDirectory(final File chartDirectory) { _chartDirectory = chartDirectory; diff --git a/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/chartbuilder/ChartProductionTest.java b/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/chartbuilder/ChartProductionTest.java index 2e97772f37..51bde1327b 100644 --- a/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/chartbuilder/ChartProductionTest.java +++ b/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/chartbuilder/ChartProductionTest.java @@ -32,6 +32,7 @@ import org.apache.qpid.disttest.charting.definition.SeriesDefinition; import org.apache.qpid.disttest.charting.seriesbuilder.SeriesBuilderCallback; import org.apache.qpid.disttest.charting.seriesbuilder.SeriesBuilder; import org.apache.qpid.disttest.charting.writer.ChartWriter; +import org.apache.qpid.test.utils.TestFileUtils; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.title.ShortTextTitle; @@ -53,8 +54,6 @@ public class ChartProductionTest extends TestCase private static final String TEST_SERIESLEGEND = "TEST_SERIESLEGEND"; - private static final String SYSTEM_TMP_DIR = System.getProperty("java.io.tmpdir"); - private static final String CHART_DIRECTORY = "charts." + System.currentTimeMillis(); private static final String RETAIN_TEST_CHARTS = "retainTestCharts"; private SeriesDefinition _seriesDefinition = mock(SeriesDefinition.class); @@ -74,8 +73,7 @@ public class ChartProductionTest extends TestCase when(_chartingDefinition.getYAxisTitle()).thenReturn(TEST_YAXIS); when(_chartingDefinition.getSeries()).thenReturn(Collections.singletonList(_seriesDefinition)); - File chartDir = new File(SYSTEM_TMP_DIR, CHART_DIRECTORY); - chartDir.mkdirs(); + File chartDir = TestFileUtils.createTestDirectory("charts", false); if (!System.getProperties().containsKey(RETAIN_TEST_CHARTS)) { chartDir.deleteOnExit(); diff --git a/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/ChartWriterTest.java b/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/ChartWriterTest.java new file mode 100644 index 0000000000..472edd69a1 --- /dev/null +++ b/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/ChartWriterTest.java @@ -0,0 +1,117 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.qpid.disttest.charting.writer; + +import java.io.File; +import java.io.FileWriter; +import java.io.InputStream; +import java.util.Scanner; + +import junit.framework.TestCase; + +import org.apache.qpid.test.utils.TestFileUtils; +import org.apache.qpid.util.FileUtils; +import org.jfree.chart.ChartFactory; +import org.jfree.chart.JFreeChart; +import org.jfree.data.general.DefaultPieDataset; + +public class ChartWriterTest extends TestCase +{ + private JFreeChart _chart1; + private JFreeChart _chart2; + + private File _chartDir; + private ChartWriter _writer; + + @Override + public void setUp() + { + DefaultPieDataset dataset = new DefaultPieDataset(); + dataset.setValue("a", 1); + dataset.setValue("b", 2); + + _chart1 = ChartFactory.createPieChart("chart1", dataset, true, true, false); + _chart2 = ChartFactory.createPieChart("chart2", dataset, true, true, false); + + _chartDir = TestFileUtils.createTestDirectory(); + + _writer = new ChartWriter(); + _writer.setOutputDirectory(_chartDir); + } + + public void testWriteChartToFileSystem() + { + File chart1File = new File(_chartDir, "chart1.png"); + assertFalse("chart1 png should not exist yet", chart1File.exists()); + + _writer.writeChartToFileSystem(_chart1, "chart1"); + + assertTrue("chart1 png does not exist", chart1File.exists()); + } + + public void testWriteHtmlSummaryToFileSystemOverwritingExistingFile() throws Exception + { + File summaryFile = new File(_chartDir, ChartWriter.SUMMARY_FILE_NAME); + + writeDummyContentToSummaryFileToEnsureItGetsOverwritten(summaryFile); + + _writer.writeChartToFileSystem(_chart1, "chart1"); + _writer.writeChartToFileSystem(_chart2, "chart2"); + + _writer.writeHtmlSummaryToFileSystem(); + + InputStream expectedSummaryFileInputStream = getClass().getResourceAsStream("expected-chart-summary.html"); + String expectedSummaryContent = new Scanner(expectedSummaryFileInputStream).useDelimiter("\\A").next(); + String actualSummaryContent = FileUtils.readFileAsString(summaryFile); + + assertEquals("HTML summary file has unexpected content", expectedSummaryContent, actualSummaryContent); + } + + public void testWriteHtmlSummaryToFileSystemDoesNothingIfLessThanTwoCharts() + { + File summaryFile = new File(_chartDir, ChartWriter.SUMMARY_FILE_NAME); + + _writer.writeChartToFileSystem(_chart1, "chart1"); + + _writer.writeHtmlSummaryToFileSystem(); + + assertFalse("Only one chart generated so no summary file should have been written", + summaryFile.exists()); + } + + private void writeDummyContentToSummaryFileToEnsureItGetsOverwritten(File summaryFile) throws Exception + { + FileWriter writer = null; + try + { + writer = new FileWriter(summaryFile); + writer.write("dummy content"); + writer.close(); + } + finally + { + if (writer != null) + { + writer.close(); + } + } + } +} diff --git a/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/expected-chart-summary.html b/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/expected-chart-summary.html new file mode 100644 index 0000000000..d1f039f44a --- /dev/null +++ b/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/expected-chart-summary.html @@ -0,0 +1,9 @@ + + + Performance Charts + + + + + + \ No newline at end of file -- cgit v1.2.1