diff options
author | Keith Wall <kwall@apache.org> | 2012-06-15 13:57:53 +0000 |
---|---|---|
committer | Keith Wall <kwall@apache.org> | 2012-06-15 13:57:53 +0000 |
commit | f5ffdd2417a8c8dd27f3c623e10cde961163c04a (patch) | |
tree | 0eb2748e5fc6d5d19dcb9803e1387ac75bee5977 | |
parent | 178f342319aee9f6f85545aa48e4a442b060d6b6 (diff) | |
download | qpid-python-f5ffdd2417a8c8dd27f3c623e10cde961163c04a.tar.gz |
QPID-3977: ChartingUtil now generates chart-summary.html file to facilitate chart png browsing from CI server.
Applied patch from Philip Harvey <phil@philharveyonline.com>
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1350625 13f79535-47bb-0310-9956-ffa450edef68
7 files changed, 259 insertions, 5 deletions
diff --git a/qpid/java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java b/qpid/java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java new file mode 100644 index 0000000000..056d11faaa --- /dev/null +++ b/qpid/java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java @@ -0,0 +1,63 @@ +/* + * + * 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.test.utils; + +import java.io.File; + +import org.apache.qpid.util.FileUtils; + +/** + * Utility methods intended to be used in unit tests that manipulate files + */ +public class TestFileUtils +{ + private static final String SYSTEM_TMP_DIR = System.getProperty("java.io.tmpdir"); + + /** + * Create and return a temporary directory that will be deleted on exit. + */ + public static File createTestDirectory() + { + String dirNameStem = TestFileUtils.class.getSimpleName() + "-testDir"; + return createTestDirectory(dirNameStem, true); + } + + /** + * Creates an empty directory with a name like /tmp/dirNameStem-12345678 + */ + public static File createTestDirectory(String dirNameStem, boolean deleteOnExit) + { + File testDir = new File(SYSTEM_TMP_DIR, dirNameStem + "-" + System.currentTimeMillis()); + if (testDir.exists()) + { + FileUtils.delete(testDir, true); + } + + testDir.mkdirs(); + + if (deleteOnExit) + { + testDir.deleteOnExit(); + } + + return testDir; + } +} 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 @@ --> <project name="visualisation-jfc" xmlns:ivy="antlib:org.apache.ivy.ant" default="build"> <property name="module.depends" value="common perftests" /> - <property name="module.test.depends" value="test" /> + <property name="module.test.depends" value="test common/test" /> + <property name="module.manifest" value="true" /> <import file="../../module.xml" /> 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<ChartingDefinition> 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<File> _chartFiles = new ArrayList<File>(); 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 = + "<html>\n" + + " <head>\n" + + " <title>Performance Charts</title>\n" + + " </head>\n" + + " <body>\n"; + + String htmlFooter = + " </body>\n" + + "</html>"; + + 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(" <img src='" + chartFile.getName() + "'/>\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 @@ +<html> + <head> + <title>Performance Charts</title> + </head> + <body> + <img src='chart1.png'/> + <img src='chart2.png'/> + </body> +</html>
\ No newline at end of file |