1 42 43 package org.jfree.chart.junit; 44 45 import java.awt.Graphics2D ; 46 import java.awt.geom.Rectangle2D ; 47 import java.awt.image.BufferedImage ; 48 49 import junit.framework.Test; 50 import junit.framework.TestCase; 51 import junit.framework.TestSuite; 52 53 import org.jfree.chart.ChartFactory; 54 import org.jfree.chart.JFreeChart; 55 import org.jfree.chart.axis.ValueAxis; 56 import org.jfree.chart.event.ChartChangeEvent; 57 import org.jfree.chart.event.ChartChangeListener; 58 import org.jfree.chart.labels.StandardXYToolTipGenerator; 59 import org.jfree.chart.labels.XYToolTipGenerator; 60 import org.jfree.chart.plot.PlotOrientation; 61 import org.jfree.chart.plot.XYPlot; 62 import org.jfree.chart.renderer.xy.XYItemRenderer; 63 import org.jfree.data.Range; 64 import org.jfree.data.xy.IntervalXYDataset; 65 import org.jfree.data.xy.XYBarDataset; 66 import org.jfree.data.xy.XYDataset; 67 import org.jfree.data.xy.XYSeries; 68 import org.jfree.data.xy.XYSeriesCollection; 69 70 73 public class XYBarChartTests extends TestCase { 74 75 76 private JFreeChart chart; 77 78 83 public static Test suite() { 84 return new TestSuite(XYBarChartTests.class); 85 } 86 87 92 public XYBarChartTests(String name) { 93 super(name); 94 } 95 96 99 protected void setUp() { 100 this.chart = createChart(); 101 } 102 103 107 public void testDrawWithNullInfo() { 108 109 boolean success = false; 110 try { 111 BufferedImage image = new BufferedImage ( 112 200 , 100, BufferedImage.TYPE_INT_RGB 113 ); 114 Graphics2D g2 = image.createGraphics(); 115 this.chart.draw( 116 g2, new Rectangle2D.Double (0, 0, 200, 100), null, null 117 ); 118 g2.dispose(); 119 success = true; 120 } 121 catch (Exception e) { 122 success = false; 123 e.printStackTrace(); 124 } 125 assertTrue(success); 126 127 } 128 129 132 public void testReplaceDataset() { 133 134 XYSeries series1 = new XYSeries("Series 1"); 136 series1.add(10.0, 10.0); 137 series1.add(20.0, 20.0); 138 series1.add(30.0, 30.0); 139 XYDataset dataset = new XYSeriesCollection(series1); 140 141 LocalListener l = new LocalListener(); 142 this.chart.addChangeListener(l); 143 this.chart.getXYPlot().setDataset(dataset); 144 assertEquals(true, l.flag); 145 ValueAxis axis = this.chart.getXYPlot().getRangeAxis(); 146 Range range = axis.getRange(); 147 assertTrue("Expecting the lower bound of the range to be around 10: " 148 + range.getLowerBound(), range.getLowerBound() <= 10); 149 assertTrue("Expecting the upper bound of the range to be around 30: " 150 + range.getUpperBound(), range.getUpperBound() >= 30); 151 152 } 153 154 158 public void testSetSeriesToolTipGenerator() { 159 XYPlot plot = (XYPlot) this.chart.getPlot(); 160 XYItemRenderer renderer = plot.getRenderer(); 161 StandardXYToolTipGenerator tt = new StandardXYToolTipGenerator(); 162 renderer.setSeriesToolTipGenerator(0, tt); 163 XYToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0); 164 assertTrue(tt2 == tt); 165 } 166 167 172 private static JFreeChart createChart() { 173 174 XYSeries series1 = new XYSeries("Series 1"); 176 series1.add(1.0, 1.0); 177 series1.add(2.0, 2.0); 178 series1.add(3.0, 3.0); 179 IntervalXYDataset dataset = new XYBarDataset( 180 new XYSeriesCollection(series1), 1.0 181 ); 182 183 return ChartFactory.createXYBarChart( 185 "XY Bar Chart", "Domain", false, 187 "Range", 188 dataset, PlotOrientation.VERTICAL, 190 true, true, true ); 194 195 } 196 197 201 static class LocalListener implements ChartChangeListener { 202 203 204 private boolean flag = false; 205 206 211 public void chartChanged(ChartChangeEvent event) { 212 this.flag = true; 213 } 214 215 } 216 217 } 218 | Popular Tags |