Author: kwall
Date: Fri Jun 15 13:57:53 2012
New Revision: 1350625

URL: http://svn.apache.org/viewvc?rev=1350625&view=rev
Log:
QPID-3977: ChartingUtil now generates chart-summary.html file to facilitate 
chart png browsing from CI server.

Applied patch from Philip Harvey <[email protected]>

Added:
    
qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java
    
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/
    
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/ChartWriterTest.java
    
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/expected-chart-summary.html
Modified:
    qpid/trunk/qpid/java/perftests/visualisation-jfc/build.xml
    
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/ChartingUtil.java
    
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/writer/ChartWriter.java
    
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/chartbuilder/ChartProductionTest.java

Added: 
qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java?rev=1350625&view=auto
==============================================================================
--- 
qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java
 (added)
+++ 
qpid/trunk/qpid/java/common/src/test/java/org/apache/qpid/test/utils/TestFileUtils.java
 Fri Jun 15 13:57:53 2012
@@ -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;
+    }
+}

Modified: qpid/trunk/qpid/java/perftests/visualisation-jfc/build.xml
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/perftests/visualisation-jfc/build.xml?rev=1350625&r1=1350624&r2=1350625&view=diff
==============================================================================
--- qpid/trunk/qpid/java/perftests/visualisation-jfc/build.xml (original)
+++ qpid/trunk/qpid/java/perftests/visualisation-jfc/build.xml Fri Jun 15 
13:57:53 2012
@@ -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" />

Modified: 
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/ChartingUtil.java
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/ChartingUtil.java?rev=1350625&r1=1350624&r2=1350625&view=diff
==============================================================================
--- 
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/ChartingUtil.java
 (original)
+++ 
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/ChartingUtil.java
 Fri Jun 15 13:57:53 2012
@@ -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)

Modified: 
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/writer/ChartWriter.java
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/writer/ChartWriter.java?rev=1350625&r1=1350624&r2=1350625&view=diff
==============================================================================
--- 
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/writer/ChartWriter.java
 (original)
+++ 
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/writer/ChartWriter.java
 Fri Jun 15 13:57:53 2012
@@ -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;

Modified: 
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/chartbuilder/ChartProductionTest.java
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/chartbuilder/ChartProductionTest.java?rev=1350625&r1=1350624&r2=1350625&view=diff
==============================================================================
--- 
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/chartbuilder/ChartProductionTest.java
 (original)
+++ 
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/chartbuilder/ChartProductionTest.java
 Fri Jun 15 13:57:53 2012
@@ -32,6 +32,7 @@ import org.apache.qpid.disttest.charting
 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
 
     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
         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();

Added: 
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/ChartWriterTest.java
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/ChartWriterTest.java?rev=1350625&view=auto
==============================================================================
--- 
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/ChartWriterTest.java
 (added)
+++ 
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/ChartWriterTest.java
 Fri Jun 15 13:57:53 2012
@@ -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();
+            }
+        }
+    }
+}

Added: 
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/expected-chart-summary.html
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/expected-chart-summary.html?rev=1350625&view=auto
==============================================================================
--- 
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/expected-chart-summary.html
 (added)
+++ 
qpid/trunk/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/writer/expected-chart-summary.html
 Fri Jun 15 13:57:53 2012
@@ -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



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to