1 /* =========================================================== 2 * JFreeChart : a free chart library for the Java(tm) platform 3 * =========================================================== 4 * 5 * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors. 6 * 7 * Project Info: http://www.jfree.org/jfreechart/index.html 8 * 9 * This library is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU Lesser General Public License as published by 11 * the Free Software Foundation; either version 2.1 of the License, or 12 * (at your option) any later version. 13 * 14 * This library is distributed in the hope that it will be useful, but 15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17 * License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this library; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 22 * USA. 23 * 24 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 25 * Other names may be trademarks of their respective owners.] 26 * 27 * ------------------------ 28 * XYStepAreaChartTest.java 29 * ------------------------ 30 * (C) Copyright 2005-2013, by Object Refinery Limited. 31 * 32 * Original Author: David Gilbert (for Object Refinery Limited); 33 * Contributor(s): -; 34 * 35 * Changes: 36 * -------- 37 * 12-Apr-2005 : Version 1 (DG); 38 * 39 */ 40 41 package org.jfree.chart; 42 43 import static org.junit.Assert.assertTrue; 44 import static org.junit.Assert.fail; 45 import static org.junit.Assert.assertEquals; 46 47 import java.awt.Graphics2D; 48 import java.awt.geom.Rectangle2D; 49 import java.awt.image.BufferedImage; 50 51 import org.jfree.chart.axis.ValueAxis; 52 import org.jfree.chart.event.ChartChangeEvent; 53 import org.jfree.chart.event.ChartChangeListener; 54 import org.jfree.chart.labels.StandardXYToolTipGenerator; 55 import org.jfree.chart.labels.XYToolTipGenerator; 56 import org.jfree.chart.plot.XYPlot; 57 import org.jfree.chart.renderer.xy.XYItemRenderer; 58 import org.jfree.data.Range; 59 import org.jfree.data.xy.XYDataset; 60 import org.jfree.data.xy.XYSeries; 61 import org.jfree.data.xy.XYSeriesCollection; 62 import org.junit.Before; 63 import org.junit.Test; 64 65 /** 66 * Some tests for an XY step area chart. 67 */ 68 public class XYStepAreaChartTest { 69 70 /** A chart. */ 71 private JFreeChart chart; 72 73 /** 74 * Common test setup. 75 */ 76 @Before setUp()77 public void setUp() { 78 this.chart = createChart(); 79 } 80 81 /** 82 * Draws the chart with a null info object to make sure that no exceptions 83 * are thrown (a problem that was occurring at one point). 84 */ 85 @Test testDrawWithNullInfo()86 public void testDrawWithNullInfo() { 87 try { 88 BufferedImage image = new BufferedImage(200 , 100, 89 BufferedImage.TYPE_INT_RGB); 90 Graphics2D g2 = image.createGraphics(); 91 this.chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, 92 null); 93 g2.dispose(); 94 } 95 catch (Exception e) { 96 fail("No exception should be triggered."); 97 } 98 } 99 100 /** 101 * Replaces the dataset and checks that it has changed as expected. 102 */ 103 @Test testReplaceDataset()104 public void testReplaceDataset() { 105 106 // create a dataset... 107 XYSeries series1 = new XYSeries("Series 1"); 108 series1.add(10.0, 10.0); 109 series1.add(20.0, 20.0); 110 series1.add(30.0, 30.0); 111 XYDataset dataset = new XYSeriesCollection(series1); 112 113 LocalListener l = new LocalListener(); 114 this.chart.addChangeListener(l); 115 XYPlot plot = (XYPlot) this.chart.getPlot(); 116 plot.setDataset(dataset); 117 assertEquals(true, l.flag); 118 ValueAxis axis = plot.getRangeAxis(); 119 Range range = axis.getRange(); 120 assertTrue("Expecting the lower bound of the range to be around 10: " 121 + range.getLowerBound(), range.getLowerBound() <= 10); 122 assertTrue("Expecting the upper bound of the range to be around 30: " 123 + range.getUpperBound(), range.getUpperBound() >= 30); 124 125 } 126 127 /** 128 * Check that setting a tool tip generator for a series does override the 129 * default generator. 130 */ 131 @Test testSetSeriesToolTipGenerator()132 public void testSetSeriesToolTipGenerator() { 133 XYPlot plot = (XYPlot) this.chart.getPlot(); 134 XYItemRenderer renderer = plot.getRenderer(); 135 StandardXYToolTipGenerator tt = new StandardXYToolTipGenerator(); 136 renderer.setSeriesToolTipGenerator(0, tt); 137 XYToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0); 138 assertTrue(tt2 == tt); 139 } 140 141 /** 142 * Create an area chart. 143 * 144 * @return The chart. 145 */ createChart()146 private static JFreeChart createChart() { 147 XYSeries series1 = new XYSeries("Series 1"); 148 series1.add(1.0, 1.0); 149 series1.add(2.0, 2.0); 150 series1.add(3.0, 3.0); 151 XYDataset dataset = new XYSeriesCollection(series1); 152 return ChartFactory.createXYStepAreaChart("Step Chart", "Domain", 153 "Range", dataset); 154 } 155 156 /** 157 * A chart change listener. 158 * 159 */ 160 static class LocalListener implements ChartChangeListener { 161 162 /** A flag. */ 163 private boolean flag = false; 164 165 /** 166 * Event handler. 167 * 168 * @param event the event. 169 */ 170 @Override chartChanged(ChartChangeEvent event)171 public void chartChanged(ChartChangeEvent event) { 172 this.flag = true; 173 } 174 175 } 176 177 } 178