From 178f342319aee9f6f85545aa48e4a442b060d6b6 Mon Sep 17 00:00:00 2001 From: Keith Wall Date: Fri, 15 Jun 2012 13:57:25 +0000 Subject: QPID-3977: Add support for 3D bar/line charts; XY line charts, chart subtitles, and add Main-Class/Class-Path manifest entries to visualisation JAR to allow for convienient use from command line. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1350624 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/qpid/disttest/charting/ChartType.java | 3 +- .../qpid/disttest/charting/ChartingUtil.java | 71 ++++----- .../charting/chartbuilder/BarChart3DBuilder.java | 56 +++++++ .../charting/chartbuilder/BarChartBuilder.java | 15 +- .../charting/chartbuilder/BaseChartBuilder.java | 58 +++++++ .../CategoryDataSetBasedChartBuilder.java | 80 ++++++++++ .../charting/chartbuilder/ChartBuilder.java | 6 +- .../charting/chartbuilder/ChartBuilderFactory.java | 13 +- .../charting/chartbuilder/DataPointCallback.java | 27 ---- .../chartbuilder/DataSetBasedChartBuilder.java | 68 --------- .../charting/chartbuilder/LineChart3DBuilder.java | 52 +++++++ .../charting/chartbuilder/LineChartBuilder.java | 16 +- .../charting/chartbuilder/SeriesBuilder.java | 133 ----------------- .../chartbuilder/XYDataSetBasedChartBuilder.java | 102 +++++++++++++ .../charting/chartbuilder/XYLineChartBuilder.java | 51 +++++++ .../charting/definition/ChartingDefinition.java | 12 +- .../definition/ChartingDefinitionCreator.java | 9 +- .../seriesbuilder/JdbcCsvSeriesBuilder.java | 138 +++++++++++++++++ .../charting/seriesbuilder/SeriesBuilder.java | 32 ++++ .../seriesbuilder/SeriesBuilderCallback.java | 30 ++++ .../qpid/disttest/charting/writer/ChartWriter.java | 76 ++++++++++ .../chartbuilder/ChartBuilderFactoryTest.java | 26 +++- .../charting/chartbuilder/ChartProductionTest.java | 166 +++++++++++++++++++++ .../charting/chartbuilder/SeriesBuilderTest.java | 90 ----------- .../definition/ChartingDefinitionCreatorTest.java | 4 + .../seriesbuilder/JdbcCsvSeriesBuilderTest.java | 94 ++++++++++++ 26 files changed, 1042 insertions(+), 386 deletions(-) create mode 100644 qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/BarChart3DBuilder.java create mode 100644 qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/BaseChartBuilder.java create mode 100644 qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/CategoryDataSetBasedChartBuilder.java delete mode 100644 qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/DataPointCallback.java delete mode 100644 qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/DataSetBasedChartBuilder.java create mode 100644 qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/LineChart3DBuilder.java delete mode 100644 qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/SeriesBuilder.java create mode 100644 qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/XYDataSetBasedChartBuilder.java create mode 100644 qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/XYLineChartBuilder.java create mode 100644 qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/seriesbuilder/JdbcCsvSeriesBuilder.java create mode 100644 qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/seriesbuilder/SeriesBuilder.java create mode 100644 qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/seriesbuilder/SeriesBuilderCallback.java create mode 100644 qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/writer/ChartWriter.java create mode 100644 qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/chartbuilder/ChartProductionTest.java delete mode 100644 qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/chartbuilder/SeriesBuilderTest.java create mode 100644 qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/seriesbuilder/JdbcCsvSeriesBuilderTest.java (limited to 'qpid/java/perftests/visualisation-jfc/src') diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/ChartType.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/ChartType.java index 2803f2d767..8bddfd1379 100644 --- a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/ChartType.java +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/ChartType.java @@ -21,6 +21,5 @@ package org.apache.qpid.disttest.charting; public enum ChartType { - LINE, BAR - + LINE, LINE3D, BAR, BAR3D, XYLINE } 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 a149d1a097..f46bc45583 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 @@ -20,11 +20,7 @@ */ package org.apache.qpid.disttest.charting; -import java.io.BufferedOutputStream; import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -34,11 +30,24 @@ import org.apache.qpid.disttest.charting.chartbuilder.ChartBuilder; import org.apache.qpid.disttest.charting.chartbuilder.ChartBuilderFactory; import org.apache.qpid.disttest.charting.definition.ChartingDefinition; import org.apache.qpid.disttest.charting.definition.ChartingDefinitionCreator; -import org.jfree.chart.ChartUtilities; +import org.apache.qpid.disttest.charting.seriesbuilder.JdbcCsvSeriesBuilder; +import org.apache.qpid.disttest.charting.seriesbuilder.SeriesBuilder; +import org.apache.qpid.disttest.charting.writer.ChartWriter; import org.jfree.chart.JFreeChart; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * Draws charts for data drawn from CSV datasources using rules described in + * charting definitions (.chartdef) files. + *

+ * The following arguments are understood: + *

+ *
    + *
  1. chart-defs=directory contain chartdef file(s)
  2. + *
  3. output-dir=directory in which to produce the PNGs
  4. + *
+ */ public class ChartingUtil { private static final Logger LOGGER = LoggerFactory.getLogger(ChartingUtil.class); @@ -59,7 +68,7 @@ public class ChartingUtil { try { - LOGGER.debug("Starting charting"); + LOGGER.info("Starting charting"); ChartingUtil chartingUtil = new ChartingUtil(); chartingUtil.parseArgumentsIntoConfig(args); @@ -67,55 +76,29 @@ public class ChartingUtil } finally { - LOGGER.debug("Charting complete"); + LOGGER.info("Charting complete"); } } private void produceAllCharts() { final String chartingDefsDir = _cliOptions.get(CHART_DEFINITIONS_PROP); + final File chartDirectory = new File(_cliOptions.get(OUTPUT_DIR_PROP)); + LOGGER.info("Chart chartdef directory/file: {} output directory : {}", chartingDefsDir, chartDirectory); + List definitions = loadChartDefinitions(chartingDefsDir); - LOGGER.debug("There are {} chart(s) to produce", definitions.size()); + LOGGER.info("There are {} chart(s) to produce", definitions.size()); + + final ChartWriter writer = new ChartWriter(); + writer.setOutputDirectory(chartDirectory); + final SeriesBuilder seriesBuilder = new JdbcCsvSeriesBuilder(); for (ChartingDefinition chartingDefinition : definitions) { - ChartBuilder chartBuilder = ChartBuilderFactory.createChartBuilder(chartingDefinition.getChartType()); + ChartBuilder chartBuilder = ChartBuilderFactory.createChartBuilder(chartingDefinition.getChartType(), seriesBuilder); JFreeChart chart = chartBuilder.buildChart(chartingDefinition); - writeChartToFileSystem(chart, chartingDefinition.getChartStemName()); - } - } - - private void writeChartToFileSystem(JFreeChart chart, String chartStemName) - { - OutputStream pngOutputStream = null; - try - { - - File pngFile = new File(chartStemName + ".png"); - pngOutputStream = new BufferedOutputStream(new FileOutputStream(pngFile)); - ChartUtilities.writeChartAsPNG(pngOutputStream, chart, 600, 400, true, 0); - pngOutputStream.close(); - - LOGGER.debug("Written {} chart", pngFile); - } - catch (IOException e) - { - throw new ChartingException("Failed to create chart", e); - } - finally - { - if (pngOutputStream != null) - { - try - { - pngOutputStream.close(); - } - catch (IOException e) - { - throw new ChartingException("Failed to create chart", e); - } - } + writer.writeChartToFileSystem(chart, chartingDefinition.getChartStemName()); } } @@ -130,6 +113,4 @@ public class ChartingUtil ArgumentParser argumentParser = new ArgumentParser(); argumentParser.parseArgumentsIntoConfig(_cliOptions, args); } - - } diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/BarChart3DBuilder.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/BarChart3DBuilder.java new file mode 100644 index 0000000000..491bb1c67d --- /dev/null +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/BarChart3DBuilder.java @@ -0,0 +1,56 @@ +/* + * 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.chartbuilder; + + +import org.apache.qpid.disttest.charting.seriesbuilder.SeriesBuilder; +import org.jfree.chart.ChartFactory; +import org.jfree.chart.JFreeChart; +import org.jfree.chart.plot.PlotOrientation; +import org.jfree.data.category.CategoryDataset; +import org.jfree.data.general.Dataset; + +public class BarChart3DBuilder extends CategoryDataSetBasedChartBuilder +{ + + public BarChart3DBuilder(SeriesBuilder seriesBuilder) + { + super(seriesBuilder); + } + + @Override + public JFreeChart createChartImpl(String title, String xAxisTitle, + String yAxisTitle, final Dataset dataset, PlotOrientation plotOrientation, + boolean showLegend, boolean showToolTips, boolean showUrls) + { + JFreeChart chart = ChartFactory.createBarChart3D(title, + xAxisTitle, + yAxisTitle, + (CategoryDataset)dataset, + plotOrientation, + showLegend, + showToolTips, + showUrls); + + return chart; + } + + +} diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/BarChartBuilder.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/BarChartBuilder.java index 302263a604..b5c6a38067 100644 --- a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/BarChartBuilder.java +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/BarChartBuilder.java @@ -19,23 +19,30 @@ */ package org.apache.qpid.disttest.charting.chartbuilder; +import org.apache.qpid.disttest.charting.seriesbuilder.SeriesBuilder; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; -import org.jfree.data.category.DefaultCategoryDataset; +import org.jfree.data.category.CategoryDataset; +import org.jfree.data.general.Dataset; -public class BarChartBuilder extends DataSetBasedChartBuilder +public class BarChartBuilder extends CategoryDataSetBasedChartBuilder { + public BarChartBuilder(SeriesBuilder seriesBuilder) + { + super(seriesBuilder); + } + @Override public JFreeChart createChartImpl(String title, String xAxisTitle, - String yAxisTitle, final DefaultCategoryDataset dataset, PlotOrientation plotOrientation, + String yAxisTitle, final Dataset dataset, PlotOrientation plotOrientation, boolean showLegend, boolean showToolTips, boolean showUrls) { JFreeChart chart = ChartFactory.createBarChart(title, xAxisTitle, yAxisTitle, - dataset, + (CategoryDataset) dataset, plotOrientation, showLegend, showToolTips, diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/BaseChartBuilder.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/BaseChartBuilder.java new file mode 100644 index 0000000000..def87f5840 --- /dev/null +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/BaseChartBuilder.java @@ -0,0 +1,58 @@ +/* + * 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.chartbuilder; + +import java.awt.Color; +import java.awt.GradientPaint; + +import org.apache.qpid.disttest.charting.definition.ChartingDefinition; +import org.jfree.chart.JFreeChart; +import org.jfree.chart.plot.PlotOrientation; +import org.jfree.chart.title.ShortTextTitle; +import org.jfree.data.general.Dataset; + +public abstract class BaseChartBuilder implements ChartBuilder +{ + private static final GradientPaint BLUE_GRADIENT = new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue); + + public void addCommonChartAttributes(JFreeChart chart, ChartingDefinition chartingDefinition) + { + addSubtitle(chart, chartingDefinition); + setBackgroundColour(chart); + } + + private void addSubtitle(JFreeChart chart, ChartingDefinition chartingDefinition) + { + if (chartingDefinition.getChartSubtitle() != null) + { + chart.addSubtitle(new ShortTextTitle(chartingDefinition.getChartSubtitle())); + } + } + + private void setBackgroundColour(JFreeChart chart) + { + chart.setBackgroundPaint(BLUE_GRADIENT); + } + + public abstract JFreeChart createChartImpl(String title, String xAxisTitle, + String yAxisTitle, final Dataset dataset, PlotOrientation plotOrientation, boolean showLegend, boolean showToolTips, + boolean showUrls); + +} diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/CategoryDataSetBasedChartBuilder.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/CategoryDataSetBasedChartBuilder.java new file mode 100644 index 0000000000..ad33da8311 --- /dev/null +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/CategoryDataSetBasedChartBuilder.java @@ -0,0 +1,80 @@ +/* + * 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.chartbuilder; + +import org.apache.qpid.disttest.charting.definition.ChartingDefinition; +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.jfree.chart.JFreeChart; +import org.jfree.data.category.DefaultCategoryDataset; + +public abstract class CategoryDataSetBasedChartBuilder extends BaseChartBuilder +{ + private final SeriesBuilder _seriesBuilder; + + public CategoryDataSetBasedChartBuilder(SeriesBuilder seriesBuilder) + { + _seriesBuilder = seriesBuilder; + } + + @Override + public JFreeChart buildChart(ChartingDefinition chartingDefinition) + { + String title = chartingDefinition.getChartTitle(); + String xAxisTitle = chartingDefinition.getXAxisTitle(); + String yAxisTitle = chartingDefinition.getYAxisTitle(); + + final DefaultCategoryDataset dataset = new DefaultCategoryDataset(); + + _seriesBuilder.setSeriesBuilderCallback(new SeriesBuilderCallback() + { + @Override + public void addDataPointToSeries(SeriesDefinition seriesDefinition, + Object xValue, Object yValue) + { + String x = String.valueOf(xValue); + double y = Double.parseDouble(yValue.toString()); + dataset.addValue( y, seriesDefinition.getSeriesLegend(), x); + } + + @Override + public void beginSeries(SeriesDefinition seriesDefinition) + { + // unused + } + + @Override + public void endSeries(SeriesDefinition seriesDefinition) + { + // unused + } + }); + + _seriesBuilder.build(chartingDefinition.getSeries()); + + JFreeChart chart = createChartImpl(title, xAxisTitle, yAxisTitle, + dataset, PLOT_ORIENTATION, SHOW_LEGEND, SHOW_TOOL_TIPS, SHOW_URLS); + + addCommonChartAttributes(chart, chartingDefinition); + + return chart; + } +} diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/ChartBuilder.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/ChartBuilder.java index c6f5ecc175..425b83596f 100644 --- a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/ChartBuilder.java +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/ChartBuilder.java @@ -21,10 +21,14 @@ package org.apache.qpid.disttest.charting.chartbuilder; import org.apache.qpid.disttest.charting.definition.ChartingDefinition; import org.jfree.chart.JFreeChart; +import org.jfree.chart.plot.PlotOrientation; public interface ChartBuilder { + public static final boolean SHOW_URLS = false; + public static final boolean SHOW_TOOL_TIPS = false; + public static final boolean SHOW_LEGEND = true; + public static final PlotOrientation PLOT_ORIENTATION = PlotOrientation.VERTICAL; public JFreeChart buildChart(ChartingDefinition chartingDefinition); - } diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/ChartBuilderFactory.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/ChartBuilderFactory.java index 4c5d4fa09f..f9d5a5a0df 100644 --- a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/ChartBuilderFactory.java +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/ChartBuilderFactory.java @@ -20,18 +20,25 @@ package org.apache.qpid.disttest.charting.chartbuilder; import org.apache.qpid.disttest.charting.ChartType; +import org.apache.qpid.disttest.charting.seriesbuilder.SeriesBuilder; public class ChartBuilderFactory { - public static ChartBuilder createChartBuilder(ChartType chartType) + public static ChartBuilder createChartBuilder(ChartType chartType, SeriesBuilder seriesBuilder) { switch (chartType) { case LINE: - return new LineChartBuilder(); + return new LineChartBuilder(seriesBuilder); + case LINE3D: + return new LineChart3DBuilder(seriesBuilder); case BAR: - return new BarChartBuilder(); + return new BarChartBuilder(seriesBuilder); + case BAR3D: + return new BarChart3DBuilder(seriesBuilder); + case XYLINE: + return new XYLineChartBuilder(seriesBuilder); default: throw new IllegalArgumentException("Unknown chart type " + chartType); } diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/DataPointCallback.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/DataPointCallback.java deleted file mode 100644 index a47df0e8af..0000000000 --- a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/DataPointCallback.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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.chartbuilder; - -import org.apache.qpid.disttest.charting.definition.SeriesDefinition; - -public interface DataPointCallback -{ - public void addDataPointToSeries(SeriesDefinition seriesDefinition, Object xValue, Object yValue); -} diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/DataSetBasedChartBuilder.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/DataSetBasedChartBuilder.java deleted file mode 100644 index 6e2491c883..0000000000 --- a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/DataSetBasedChartBuilder.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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.chartbuilder; - -import org.apache.qpid.disttest.charting.definition.ChartingDefinition; -import org.apache.qpid.disttest.charting.definition.SeriesDefinition; -import org.jfree.chart.JFreeChart; -import org.jfree.chart.axis.CategoryLabelPositions; -import org.jfree.chart.plot.PlotOrientation; -import org.jfree.data.category.DefaultCategoryDataset; - -public abstract class DataSetBasedChartBuilder implements ChartBuilder -{ - private static final CategoryLabelPositions LABEL_POSITION = CategoryLabelPositions.UP_45; - private static final PlotOrientation PLOT_ORIENTATION = PlotOrientation.VERTICAL; - - @Override - public JFreeChart buildChart(ChartingDefinition chartingDefinition) - { - String title = chartingDefinition.getChartTitle(); - String xAxisTitle = chartingDefinition.getXAxisTitle(); - String yAxisTitle = chartingDefinition.getYAxisTitle(); - - final DefaultCategoryDataset dataset = new DefaultCategoryDataset(); - - SeriesBuilder seriesBuilder = new SeriesBuilder(new DataPointCallback() - { - @Override - public void addDataPointToSeries(SeriesDefinition seriesDefinition, - Object xValue, Object yValue) - { - String x = (String) xValue; - double y = (Double) yValue; - dataset.addValue( y, seriesDefinition.getSeriesLegend(), x); - } - }); - - seriesBuilder.build(chartingDefinition.getSeries()); - - JFreeChart chart = createChartImpl(title, xAxisTitle, yAxisTitle, - dataset, PLOT_ORIENTATION, true, false, false); - - chart.getCategoryPlot().getDomainAxis().setCategoryLabelPositions(LABEL_POSITION); - - return chart; - } - - public abstract JFreeChart createChartImpl(String title, String xAxisTitle, - String yAxisTitle, final DefaultCategoryDataset dataset, PlotOrientation plotOrientation, - boolean showLegend, boolean showToolTips, boolean showUrls); -} diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/LineChart3DBuilder.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/LineChart3DBuilder.java new file mode 100644 index 0000000000..27fff12da0 --- /dev/null +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/LineChart3DBuilder.java @@ -0,0 +1,52 @@ +/* + * 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.chartbuilder; + +import org.apache.qpid.disttest.charting.seriesbuilder.SeriesBuilder; +import org.jfree.chart.ChartFactory; +import org.jfree.chart.JFreeChart; +import org.jfree.chart.plot.PlotOrientation; +import org.jfree.data.category.CategoryDataset; +import org.jfree.data.general.Dataset; + +public class LineChart3DBuilder extends CategoryDataSetBasedChartBuilder +{ + public LineChart3DBuilder(SeriesBuilder seriesBuilder) + { + super(seriesBuilder); + } + + @Override + public JFreeChart createChartImpl(String title, String xAxisTitle, + String yAxisTitle, final Dataset dataset, PlotOrientation plotOrientation, + boolean showLegend, boolean showToolTips, boolean showUrls) + { + JFreeChart chart = ChartFactory.createLineChart3D(title, + xAxisTitle, + yAxisTitle, + (CategoryDataset)dataset, + plotOrientation, + showLegend, + showToolTips, + showUrls); + return chart; + } + +} diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/LineChartBuilder.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/LineChartBuilder.java index 697dfdcf3e..40f3a09b6b 100644 --- a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/LineChartBuilder.java +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/LineChartBuilder.java @@ -19,22 +19,30 @@ */ package org.apache.qpid.disttest.charting.chartbuilder; +import org.apache.qpid.disttest.charting.seriesbuilder.SeriesBuilder; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; -import org.jfree.data.category.DefaultCategoryDataset; +import org.jfree.data.category.CategoryDataset; +import org.jfree.data.general.Dataset; -public class LineChartBuilder extends DataSetBasedChartBuilder +public class LineChartBuilder extends CategoryDataSetBasedChartBuilder { + + public LineChartBuilder(SeriesBuilder seriesBuilder) + { + super(seriesBuilder); + } + @Override public JFreeChart createChartImpl(String title, String xAxisTitle, - String yAxisTitle, final DefaultCategoryDataset dataset, PlotOrientation plotOrientation, + String yAxisTitle, final Dataset dataset, PlotOrientation plotOrientation, boolean showLegend, boolean showToolTips, boolean showUrls) { JFreeChart chart = ChartFactory.createLineChart(title, xAxisTitle, yAxisTitle, - dataset, + (CategoryDataset)dataset, plotOrientation, showLegend, showToolTips, diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/SeriesBuilder.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/SeriesBuilder.java deleted file mode 100644 index de717792db..0000000000 --- a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/SeriesBuilder.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * 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.chartbuilder; - -import java.io.File; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.Iterator; -import java.util.List; - -import org.apache.qpid.disttest.charting.ChartingException; -import org.apache.qpid.disttest.charting.definition.SeriesDefinition; - -public class SeriesBuilder -{ - static - { - registerCsvDriver(); - } - - private final DataPointCallback _dataPointCallback; - - public SeriesBuilder(DataPointCallback dataPointCallback) - { - _dataPointCallback = dataPointCallback; - } - - public void build(List seriesDefinitions) - { - for (Iterator iterator = seriesDefinitions.iterator(); iterator.hasNext();) - { - SeriesDefinition series = iterator.next(); - buildDataSetForSingleSeries(series); - } - } - - private void buildDataSetForSingleSeries(SeriesDefinition seriesDefinition) - { - Connection conn = null; - Statement stmt = null; - try - { - File seriesDir = getValidatedSeriesDirectory(seriesDefinition); - - conn = DriverManager.getConnection("jdbc:relique:csv:" + seriesDir.getAbsolutePath()); - - final String seriesStatement = seriesDefinition.getSeriesStatement(); - - stmt = conn.createStatement(); - ResultSet results = stmt.executeQuery(seriesStatement); - - while (results.next()) - { - Object xValue = results.getString(1); - Object yValue = results.getDouble(2); - - _dataPointCallback.addDataPointToSeries(seriesDefinition, xValue, yValue); - } - } - catch (SQLException e) - { - throw new ChartingException("Failed to create chart dataset", e); - } - finally - { - if (stmt != null) - { - try - { - stmt.close(); - } - catch (SQLException e) - { - throw new RuntimeException("Failed to close statement", e); - } - } - if (conn != null) - { - try - { - conn.close(); - } - catch (SQLException e) - { - throw new RuntimeException("Failed to close connection", e); - } - } - } - } - - private File getValidatedSeriesDirectory(SeriesDefinition series) - { - File seriesDir = new File(series.getSeriesDirectory()); - if (!seriesDir.isDirectory()) - { - throw new ChartingException("seriesDirectory must be a directory : " + seriesDir); - } - return seriesDir; - } - - private static void registerCsvDriver() throws ExceptionInInitializerError - { - try - { - Class.forName("org.relique.jdbc.csv.CsvDriver"); - } - catch (ClassNotFoundException e) - { - throw new RuntimeException("Could not load CSV/JDBC driver", e); - } - } - -} diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/XYDataSetBasedChartBuilder.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/XYDataSetBasedChartBuilder.java new file mode 100644 index 0000000000..6814272b8e --- /dev/null +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/XYDataSetBasedChartBuilder.java @@ -0,0 +1,102 @@ +/* + * 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.chartbuilder; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.qpid.disttest.charting.definition.ChartingDefinition; +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.jfree.chart.JFreeChart; +import org.jfree.data.xy.DefaultXYDataset; + + +public abstract class XYDataSetBasedChartBuilder extends BaseChartBuilder +{ + private final SeriesBuilder _seriesBuilder; + + public XYDataSetBasedChartBuilder(SeriesBuilder seriesBuilder) + { + this._seriesBuilder = seriesBuilder; + } + + @Override + public JFreeChart buildChart(ChartingDefinition chartingDefinition) + { + String title = chartingDefinition.getChartTitle(); + String xAxisTitle = chartingDefinition.getXAxisTitle(); + String yAxisTitle = chartingDefinition.getYAxisTitle(); + + final DefaultXYDataset dataset = new DefaultXYDataset(); + _seriesBuilder.setSeriesBuilderCallback(new SeriesBuilderCallback() + { + private List _xyPairs = null; + + @Override + public void beginSeries(SeriesDefinition seriesDefinition) + { + _xyPairs = new ArrayList(); + } + + @Override + public void addDataPointToSeries(SeriesDefinition seriesDefinition, + Object xValue, Object yValue) + { + double x = Double.parseDouble(xValue.toString()); + double y = Double.parseDouble(yValue.toString()); + _xyPairs.add(new Double[] {x, y}); + } + + + @Override + public void endSeries(SeriesDefinition seriesDefinition) + { + double[][] seriesData = listToSeriesDataArray(); + dataset.addSeries(seriesDefinition.getSeriesLegend(), seriesData); + } + + private double[][] listToSeriesDataArray() + { + double[][] seriesData = new double[2][_xyPairs.size()]; + int i = 0; + for (Iterator iterator = _xyPairs.iterator(); iterator.hasNext();) + { + Double[] xyPair = iterator.next(); + seriesData[0][i] = xyPair[0]; + seriesData[1][i] = xyPair[1]; + i++; + } + return seriesData; + } + }); + + _seriesBuilder.build(chartingDefinition.getSeries()); + + JFreeChart chart = createChartImpl(title, xAxisTitle, yAxisTitle, + dataset, PLOT_ORIENTATION, SHOW_LEGEND, SHOW_TOOL_TIPS, SHOW_URLS); + + addCommonChartAttributes(chart, chartingDefinition); + + return chart; + } +} diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/XYLineChartBuilder.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/XYLineChartBuilder.java new file mode 100644 index 0000000000..48b02a7c60 --- /dev/null +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/chartbuilder/XYLineChartBuilder.java @@ -0,0 +1,51 @@ +/* + * 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.chartbuilder; + +import org.apache.qpid.disttest.charting.seriesbuilder.SeriesBuilder; +import org.jfree.chart.ChartFactory; +import org.jfree.chart.JFreeChart; +import org.jfree.chart.plot.PlotOrientation; +import org.jfree.data.general.Dataset; +import org.jfree.data.xy.XYDataset; + +public class XYLineChartBuilder extends XYDataSetBasedChartBuilder +{ + public XYLineChartBuilder(SeriesBuilder seriesBuilder) + { + super(seriesBuilder); + } + + @Override + public JFreeChart createChartImpl(String title, String xAxisTitle, + String yAxisTitle, final Dataset dataset, PlotOrientation plotOrientation, + boolean showLegend, boolean showToolTips, boolean showUrls) + { + JFreeChart chart = ChartFactory.createXYLineChart(title, + xAxisTitle, + yAxisTitle, + (XYDataset)dataset, + plotOrientation, + showLegend, + showToolTips, + showUrls); + return chart; + } +} diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/definition/ChartingDefinition.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/definition/ChartingDefinition.java index 82b59b6d6e..04b3f7ed3b 100644 --- a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/definition/ChartingDefinition.java +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/definition/ChartingDefinition.java @@ -29,6 +29,7 @@ public class ChartingDefinition private final String _chartStemName; private final ChartType _chartType; private final String _chartTitle; + private final String _chartSubtitle; private final String _xaxisTitle; private final String _yaxisTitle; private final List _seriesDefinitions; @@ -37,12 +38,13 @@ public class ChartingDefinition public ChartingDefinition(final String chartStemName, final ChartType chartType, final String chartTitle, - final String xaxisTitle, - final String yaxisTitle, List seriesDefinitions) + final String chartSubtitle, + final String xaxisTitle, final String yaxisTitle, List seriesDefinitions) { _chartStemName = chartStemName; _chartType = chartType; _chartTitle = chartTitle; + _chartSubtitle = chartSubtitle; _xaxisTitle = xaxisTitle; _yaxisTitle = yaxisTitle; _seriesDefinitions = seriesDefinitions; @@ -58,6 +60,11 @@ public class ChartingDefinition return _chartTitle; } + public String getChartSubtitle() + { + return _chartSubtitle; + } + public String getXAxisTitle() { @@ -82,4 +89,5 @@ public class ChartingDefinition return Collections.unmodifiableList(_seriesDefinitions); } + } diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/definition/ChartingDefinitionCreator.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/definition/ChartingDefinitionCreator.java index 8a3d313519..4cbc9318a9 100644 --- a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/definition/ChartingDefinitionCreator.java +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/definition/ChartingDefinitionCreator.java @@ -38,6 +38,7 @@ public class ChartingDefinitionCreator public static final String CHART_TYPE_KEY = "chartType"; public static final String CHART_TITLE_KEY = "chartTitle"; + public static final String CHART_SUBTITLE_KEY = "chartSubtitle"; public static final String XAXIS_TITLE_KEY = "xAxisTitle"; public static final String YAXIS_TITLE_KEY = "yAxisTitle"; @@ -80,6 +81,7 @@ public class ChartingDefinitionCreator final ChartType chartType = ChartType.valueOf(props.getProperty(CHART_TYPE_KEY)); final String chartTitle = props.getProperty(CHART_TITLE_KEY); + final String chartSubtitle = props.getProperty(CHART_SUBTITLE_KEY); final String xAxisTitle = props.getProperty(XAXIS_TITLE_KEY); final String yAxisTitle = props.getProperty(YAXIS_TITLE_KEY); @@ -88,9 +90,9 @@ public class ChartingDefinitionCreator final ChartingDefinition chartDefinition = new ChartingDefinition(chartStemName, chartType, chartTitle, + chartSubtitle, xAxisTitle, - yAxisTitle, - seriesDefinitions); + yAxisTitle, seriesDefinitions); return chartDefinition; } catch (IOException e) @@ -134,7 +136,4 @@ public class ChartingDefinitionCreator return pathname.isFile() && pathname.getName().endsWith(CHARTDEF_FILE_EXTENSION); } } - - - } diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/seriesbuilder/JdbcCsvSeriesBuilder.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/seriesbuilder/JdbcCsvSeriesBuilder.java new file mode 100644 index 0000000000..8b4c2ab382 --- /dev/null +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/seriesbuilder/JdbcCsvSeriesBuilder.java @@ -0,0 +1,138 @@ +/* + * 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.seriesbuilder; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Iterator; +import java.util.List; + +import org.apache.qpid.disttest.charting.ChartingException; +import org.apache.qpid.disttest.charting.definition.SeriesDefinition; + +public class JdbcCsvSeriesBuilder implements SeriesBuilder +{ + + static + { + registerCsvDriver(); + } + + private SeriesBuilderCallback _callback; + + @Override + public void setSeriesBuilderCallback(SeriesBuilderCallback callback) + { + this._callback = callback; + } + + @Override + public void build(List seriesDefinitions) + { + for (Iterator iterator = seriesDefinitions.iterator(); iterator.hasNext();) + { + SeriesDefinition series = iterator.next(); + buildDataSetForSingleSeries(series); + } + } + + private void buildDataSetForSingleSeries(SeriesDefinition seriesDefinition) + { + Connection conn = null; + Statement stmt = null; + try + { + File seriesDir = getValidatedSeriesDirectory(seriesDefinition); + + conn = DriverManager.getConnection("jdbc:relique:csv:" + seriesDir.getAbsolutePath()); + + final String seriesStatement = seriesDefinition.getSeriesStatement(); + + stmt = conn.createStatement(); + ResultSet results = stmt.executeQuery(seriesStatement); + + _callback.beginSeries(seriesDefinition); + while (results.next()) + { + Object xValue = results.getObject(1); + Object yValue = results.getObject(2); + + _callback.addDataPointToSeries(seriesDefinition, xValue, yValue); + } + _callback.endSeries(seriesDefinition); + } + catch (SQLException e) + { + throw new ChartingException("Failed to create chart dataset", e); + } + finally + { + if (stmt != null) + { + try + { + stmt.close(); + } + catch (SQLException e) + { + throw new RuntimeException("Failed to close statement", e); + } + } + if (conn != null) + { + try + { + conn.close(); + } + catch (SQLException e) + { + throw new RuntimeException("Failed to close connection", e); + } + } + } + } + + private File getValidatedSeriesDirectory(SeriesDefinition series) + { + File seriesDir = new File(series.getSeriesDirectory()); + if (!seriesDir.isDirectory()) + { + throw new ChartingException("seriesDirectory must be a directory : " + seriesDir); + } + return seriesDir; + } + + private static void registerCsvDriver() throws ExceptionInInitializerError + { + try + { + Class.forName("org.relique.jdbc.csv.CsvDriver"); + } + catch (ClassNotFoundException e) + { + throw new RuntimeException("Could not load CSV/JDBC driver", e); + } + } + +} diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/seriesbuilder/SeriesBuilder.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/seriesbuilder/SeriesBuilder.java new file mode 100644 index 0000000000..86e471efaf --- /dev/null +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/seriesbuilder/SeriesBuilder.java @@ -0,0 +1,32 @@ +/* + * 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.seriesbuilder; + +import java.util.List; + +import org.apache.qpid.disttest.charting.definition.SeriesDefinition; + +public interface SeriesBuilder +{ + void build(List seriesDefinitions); + + void setSeriesBuilderCallback(SeriesBuilderCallback seriesBuilderCallback); + +} \ No newline at end of file diff --git a/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/seriesbuilder/SeriesBuilderCallback.java b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/seriesbuilder/SeriesBuilderCallback.java new file mode 100644 index 0000000000..22c99deca0 --- /dev/null +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/seriesbuilder/SeriesBuilderCallback.java @@ -0,0 +1,30 @@ +/* + * 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.seriesbuilder; + +import org.apache.qpid.disttest.charting.definition.SeriesDefinition; + +public interface SeriesBuilderCallback +{ + public void beginSeries(SeriesDefinition seriesDefinition); + public void addDataPointToSeries(SeriesDefinition seriesDefinition, Object xValue, Object yValue); + public void endSeries(SeriesDefinition seriesDefinition); + +} 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 new file mode 100644 index 0000000000..14aa5f3a37 --- /dev/null +++ b/qpid/java/perftests/visualisation-jfc/src/main/java/org/apache/qpid/disttest/charting/writer/ChartWriter.java @@ -0,0 +1,76 @@ +/* + * 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.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.qpid.disttest.charting.ChartingException; +import org.jfree.chart.ChartUtilities; +import org.jfree.chart.JFreeChart; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ChartWriter +{ + private static final Logger LOGGER = LoggerFactory.getLogger(ChartWriter.class); + private File _chartDirectory = new File("."); + + public void writeChartToFileSystem(JFreeChart chart, String chartStemName) + { + OutputStream pngOutputStream = null; + try + { + + File pngFile = new File(_chartDirectory, chartStemName + ".png"); + pngOutputStream = new BufferedOutputStream(new FileOutputStream(pngFile)); + ChartUtilities.writeChartAsPNG(pngOutputStream, chart, 600, 400, true, 0); + pngOutputStream.close(); + + LOGGER.info("Written {} chart", pngFile); + } + catch (IOException e) + { + throw new ChartingException("Failed to create chart", e); + } + finally + { + if (pngOutputStream != null) + { + try + { + pngOutputStream.close(); + } + catch (IOException e) + { + throw new ChartingException("Failed to create chart", 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/ChartBuilderFactoryTest.java b/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/chartbuilder/ChartBuilderFactoryTest.java index bdbbc4a585..e735fb58c6 100644 --- a/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/chartbuilder/ChartBuilderFactoryTest.java +++ b/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/chartbuilder/ChartBuilderFactoryTest.java @@ -19,22 +19,44 @@ */ package org.apache.qpid.disttest.charting.chartbuilder; +import static org.mockito.Mockito.*; + import org.apache.qpid.disttest.charting.ChartType; +import org.apache.qpid.disttest.charting.seriesbuilder.SeriesBuilder; import junit.framework.TestCase; public class ChartBuilderFactoryTest extends TestCase { + private SeriesBuilder _seriesBuilder = mock(SeriesBuilder.class); + public void testLineChart() { - ChartBuilder builder = ChartBuilderFactory.createChartBuilder(ChartType.LINE); + ChartBuilder builder = ChartBuilderFactory.createChartBuilder(ChartType.LINE, _seriesBuilder); assertTrue(builder instanceof LineChartBuilder); } + public void testLineChart3D() + { + ChartBuilder builder = ChartBuilderFactory.createChartBuilder(ChartType.LINE3D, _seriesBuilder); + assertTrue(builder instanceof LineChart3DBuilder); + } + public void testBarChart() { - ChartBuilder builder = ChartBuilderFactory.createChartBuilder(ChartType.BAR); + ChartBuilder builder = ChartBuilderFactory.createChartBuilder(ChartType.BAR, _seriesBuilder); assertTrue(builder instanceof BarChartBuilder); } + public void testBarChart3D() + { + ChartBuilder builder = ChartBuilderFactory.createChartBuilder(ChartType.BAR3D, _seriesBuilder); + assertTrue(builder instanceof BarChart3DBuilder); + } + + public void testXYLineChart() + { + ChartBuilder builder = ChartBuilderFactory.createChartBuilder(ChartType.XYLINE, _seriesBuilder); + assertTrue(builder instanceof XYLineChartBuilder); + } } 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 new file mode 100644 index 0000000000..2e97772f37 --- /dev/null +++ b/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/chartbuilder/ChartProductionTest.java @@ -0,0 +1,166 @@ +/* + * 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.chartbuilder; + +import static org.mockito.Mockito.*; + +import java.io.File; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import org.apache.qpid.disttest.charting.ChartType; +import org.apache.qpid.disttest.charting.definition.ChartingDefinition; +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.jfree.chart.JFreeChart; +import org.jfree.chart.plot.XYPlot; +import org.jfree.chart.title.ShortTextTitle; + +import junit.framework.TestCase; + +/** + * Tests the production of the different chart types. To manually + * verify the generated output, set the system property {@link #RETAIN_TEST_CHARTS} + * to prevent the automatic deletion of the test chart directory. + * + */ +public class ChartProductionTest extends TestCase +{ + private static final String TEST_CHARTTITLE = "TEST_CHARTTITLE"; + private static final String TEST_CHARTSUBTITLE = "TEST_CHARTSUBTITLE"; + private static final String TEST_XAXIS = "TEST_XAXIS"; + private static final String TEST_YAXIS = "TEST_YAXIS"; + + 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); + private ChartingDefinition _chartingDefinition = mock(ChartingDefinition.class); + private ChartWriter _writer = new ChartWriter(); + + @Override + protected void setUp() throws Exception + { + super.setUp(); + + when(_seriesDefinition.getSeriesLegend()).thenReturn(TEST_SERIESLEGEND); + + when(_chartingDefinition.getChartTitle()).thenReturn(TEST_CHARTTITLE); + when(_chartingDefinition.getChartSubtitle()).thenReturn(TEST_CHARTSUBTITLE); + when(_chartingDefinition.getXAxisTitle()).thenReturn(TEST_XAXIS); + when(_chartingDefinition.getYAxisTitle()).thenReturn(TEST_YAXIS); + when(_chartingDefinition.getSeries()).thenReturn(Collections.singletonList(_seriesDefinition)); + + File chartDir = new File(SYSTEM_TMP_DIR, CHART_DIRECTORY); + chartDir.mkdirs(); + if (!System.getProperties().containsKey(RETAIN_TEST_CHARTS)) + { + chartDir.deleteOnExit(); + } + else + { + System.out.println("Charting directory for manual observation " + chartDir); + } + + _writer.setOutputDirectory(chartDir); + } + + public void testBarChart() throws Exception + { + ChartBuilder builder = ChartBuilderFactory.createChartBuilder(ChartType.BAR, new SampleSeriesBuilder()); + assertChartTitlesAndWriteToFile(builder); + } + + public void testBar3DChart() throws Exception + { + ChartBuilder builder = ChartBuilderFactory.createChartBuilder(ChartType.BAR3D, new SampleSeriesBuilder()); + assertChartTitlesAndWriteToFile(builder); + } + + public void testLineChart() throws Exception + { + ChartBuilder builder = ChartBuilderFactory.createChartBuilder(ChartType.LINE, new SampleSeriesBuilder()); + assertChartTitlesAndWriteToFile(builder); + } + + public void testLine3DChart() throws Exception + { + ChartBuilder builder = ChartBuilderFactory.createChartBuilder(ChartType.LINE3D, new SampleSeriesBuilder()); + assertChartTitlesAndWriteToFile(builder); + } + public void testXYLineChart() throws Exception + { + ChartBuilder builder = ChartBuilderFactory.createChartBuilder(ChartType.XYLINE, new SampleSeriesBuilder()); + assertChartTitlesAndWriteToFile(builder); + } + + private void assertChartTitlesAndWriteToFile(ChartBuilder builder) + { + JFreeChart chart = builder.buildChart(_chartingDefinition); + assertEquals(TEST_CHARTTITLE, chart.getTitle().getText()); + assertEquals(TEST_CHARTSUBTITLE, ((ShortTextTitle)chart.getSubtitle(1)).getText()); + assertEquals(TEST_SERIESLEGEND, chart.getPlot().getLegendItems().get(0).getLabel()); + + if (chart.getPlot() instanceof XYPlot) + { + assertEquals(1, chart.getXYPlot().getDatasetCount()); + } + else + { + assertEquals(1, chart.getCategoryPlot().getDatasetCount()); + } + + _writer.writeChartToFileSystem(chart, getName()); + } + + private class SampleSeriesBuilder implements SeriesBuilder + { + private SeriesBuilderCallback _dataPointCallback; + + @Override + public void build(List seriesDefinitions) + { + for (Iterator iterator = seriesDefinitions.iterator(); iterator + .hasNext();) + { + SeriesDefinition seriesDefinition = iterator.next(); + _dataPointCallback.beginSeries(seriesDefinition); + _dataPointCallback.addDataPointToSeries(seriesDefinition, Double.valueOf(1), Double.valueOf(1)); + _dataPointCallback.addDataPointToSeries(seriesDefinition, Double.valueOf(2), Double.valueOf(2)); + _dataPointCallback.addDataPointToSeries(seriesDefinition, Double.valueOf(4), Double.valueOf(4)); + _dataPointCallback.addDataPointToSeries(seriesDefinition, Double.valueOf(5), Double.valueOf(5)); + _dataPointCallback.addDataPointToSeries(seriesDefinition, Double.valueOf(6), Double.valueOf(3)); + _dataPointCallback.endSeries(seriesDefinition); + } + } + + @Override + public void setSeriesBuilderCallback(SeriesBuilderCallback dataPointCallback) + { + _dataPointCallback = dataPointCallback; + } + } +} diff --git a/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/chartbuilder/SeriesBuilderTest.java b/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/chartbuilder/SeriesBuilderTest.java deleted file mode 100644 index 1b1c9969b3..0000000000 --- a/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/chartbuilder/SeriesBuilderTest.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * 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.chartbuilder; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.PrintWriter; -import java.util.Collections; - -import junit.framework.TestCase; - -import org.apache.qpid.disttest.charting.definition.SeriesDefinition; - -public class SeriesBuilderTest extends TestCase -{ - private static final String TEST_SERIES_1_SELECT_STATEMENT = "SELECT A, B FROM test"; - private static final String TEST_SERIES_1_LEGEND = "SERIES_1_LEGEND"; - - private DataPointCallback _dataPointCallback = mock(DataPointCallback.class); - private SeriesBuilder _seriesBuilder = new SeriesBuilder(_dataPointCallback); - - private File _testTempDir; - - @Override - protected void setUp() throws Exception - { - super.setUp(); - _testTempDir = createTestTemporaryDirectory(); - } - - public void testBuildOneSeries() throws Exception - { - createTestCsvIn(_testTempDir); - SeriesDefinition seriesDefinition = createTestSeriesDefinition(); - - _seriesBuilder.build(Collections.singletonList(seriesDefinition)); - - verify(_dataPointCallback).addDataPointToSeries(seriesDefinition, (Object)"elephant", (Object)2.0); - verify(_dataPointCallback).addDataPointToSeries(seriesDefinition, (Object)"lion", (Object)3.0); - verify(_dataPointCallback).addDataPointToSeries(seriesDefinition, (Object)"tiger", (Object)4.0); - } - - private void createTestCsvIn(File testDir) throws Exception - { - File csv = new File(_testTempDir, "test.csv"); - - PrintWriter csvWriter = new PrintWriter(new BufferedWriter(new FileWriter(csv))); - csvWriter.println("A,B"); - csvWriter.println("elephant,2"); - csvWriter.println("lion,3"); - csvWriter.println("tiger,4"); - csvWriter.close(); - } - - private SeriesDefinition createTestSeriesDefinition() - { - SeriesDefinition definition = new SeriesDefinition(TEST_SERIES_1_SELECT_STATEMENT, TEST_SERIES_1_LEGEND, _testTempDir.getAbsolutePath()); - return definition; - } - - private File createTestTemporaryDirectory() throws Exception - { - File tmpDir = new File(System.getProperty("java.io.tmpdir"), "testdef" + System.nanoTime()); - tmpDir.mkdirs(); - tmpDir.deleteOnExit(); - return tmpDir; - } - -} diff --git a/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/definition/ChartingDefinitionCreatorTest.java b/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/definition/ChartingDefinitionCreatorTest.java index 80d1cc3d21..5371f3df45 100644 --- a/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/definition/ChartingDefinitionCreatorTest.java +++ b/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/definition/ChartingDefinitionCreatorTest.java @@ -20,6 +20,7 @@ package org.apache.qpid.disttest.charting.definition; import static org.apache.qpid.disttest.charting.definition.ChartingDefinitionCreator.CHART_TITLE_KEY; +import static org.apache.qpid.disttest.charting.definition.ChartingDefinitionCreator.CHART_SUBTITLE_KEY; import static org.apache.qpid.disttest.charting.definition.ChartingDefinitionCreator.CHART_TYPE_KEY; import static org.apache.qpid.disttest.charting.definition.ChartingDefinitionCreator.XAXIS_TITLE_KEY; import static org.apache.qpid.disttest.charting.definition.ChartingDefinitionCreator.YAXIS_TITLE_KEY; @@ -38,6 +39,7 @@ import org.apache.qpid.disttest.charting.ChartingException; public class ChartingDefinitionCreatorTest extends TestCase { private static final String TEST_CHART_TITLE = "CHART_TITLE"; + private static final String TEST_CHART_SUBTITLE = "CHART_SUBTITLE"; private static final String TEST_XAXIS_TITLE = "XAXIS_TITLE"; private static final String TEST_YAXIS_TITLE = "YAXIS_TITLE"; private static final ChartType TEST_CHART_TYPE = ChartType.LINE; @@ -83,6 +85,7 @@ public class ChartingDefinitionCreatorTest extends TestCase ChartingDefinition definition1 = definitions.get(0); assertEquals(TEST_CHART_TITLE, definition1.getChartTitle()); + assertEquals(TEST_CHART_SUBTITLE, definition1.getChartSubtitle()); assertEquals(TEST_XAXIS_TITLE, definition1.getXAxisTitle()); assertEquals(TEST_YAXIS_TITLE, definition1.getYAxisTitle()); assertEquals(TEST_CHART_TYPE, definition1.getChartType()); @@ -121,6 +124,7 @@ public class ChartingDefinitionCreatorTest extends TestCase Properties props = new Properties(); props.setProperty(CHART_TYPE_KEY, TEST_CHART_TYPE.name()); props.setProperty(CHART_TITLE_KEY, TEST_CHART_TITLE); + props.setProperty(CHART_SUBTITLE_KEY, TEST_CHART_SUBTITLE); props.setProperty(XAXIS_TITLE_KEY, TEST_XAXIS_TITLE); props.setProperty(YAXIS_TITLE_KEY, TEST_YAXIS_TITLE); diff --git a/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/seriesbuilder/JdbcCsvSeriesBuilderTest.java b/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/seriesbuilder/JdbcCsvSeriesBuilderTest.java new file mode 100644 index 0000000000..ae241b453d --- /dev/null +++ b/qpid/java/perftests/visualisation-jfc/src/test/java/org/apache/qpid/disttest/charting/seriesbuilder/JdbcCsvSeriesBuilderTest.java @@ -0,0 +1,94 @@ +/* + * 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.seriesbuilder; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.PrintWriter; +import java.util.Collections; + +import junit.framework.TestCase; + +import org.apache.qpid.disttest.charting.definition.SeriesDefinition; +import org.apache.qpid.disttest.charting.seriesbuilder.JdbcCsvSeriesBuilder; + +public class JdbcCsvSeriesBuilderTest extends TestCase +{ + private static final String TEST_SERIES_1_SELECT_STATEMENT = "SELECT A, B FROM test"; + private static final String TEST_SERIES_1_LEGEND = "SERIES_1_LEGEND"; + + private SeriesBuilderCallback _seriesWalkerCallback = mock(SeriesBuilderCallback.class); + private JdbcCsvSeriesBuilder _seriesBuilder = new JdbcCsvSeriesBuilder(); + + private File _testTempDir; + + @Override + protected void setUp() throws Exception + { + super.setUp(); + _seriesBuilder.setSeriesBuilderCallback(_seriesWalkerCallback); + _testTempDir = createTestTemporaryDirectory(); + } + + public void testBuildOneSeries() throws Exception + { + createTestCsvIn(_testTempDir); + SeriesDefinition seriesDefinition = createTestSeriesDefinition(); + + _seriesBuilder.build(Collections.singletonList(seriesDefinition)); + + verify(_seriesWalkerCallback).beginSeries(seriesDefinition); + verify(_seriesWalkerCallback).addDataPointToSeries(seriesDefinition, (Object)"elephant", (Object)"2"); + verify(_seriesWalkerCallback).addDataPointToSeries(seriesDefinition, (Object)"lion", (Object)"3"); + verify(_seriesWalkerCallback).addDataPointToSeries(seriesDefinition, (Object)"tiger", (Object)"4"); + verify(_seriesWalkerCallback).endSeries(seriesDefinition); + } + + private void createTestCsvIn(File testDir) throws Exception + { + File csv = new File(_testTempDir, "test.csv"); + + PrintWriter csvWriter = new PrintWriter(new BufferedWriter(new FileWriter(csv))); + csvWriter.println("A,B"); + csvWriter.println("elephant,2"); + csvWriter.println("lion,3"); + csvWriter.println("tiger,4"); + csvWriter.close(); + } + + private SeriesDefinition createTestSeriesDefinition() + { + SeriesDefinition definition = new SeriesDefinition(TEST_SERIES_1_SELECT_STATEMENT, TEST_SERIES_1_LEGEND, _testTempDir.getAbsolutePath()); + return definition; + } + + private File createTestTemporaryDirectory() throws Exception + { + File tmpDir = new File(System.getProperty("java.io.tmpdir"), "testdef" + System.nanoTime()); + tmpDir.mkdirs(); + tmpDir.deleteOnExit(); + return tmpDir; + } + +} -- cgit v1.2.1