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.XYDataset; 65 import org.jfree.data.xy.XYSeries; 66 import org.jfree.data.xy.XYSeriesCollection; 67 68 71 public class XYAreaChartTests extends TestCase { 72 73 74 private JFreeChart chart; 75 76 81 public static Test suite() { 82 return new TestSuite(XYAreaChartTests.class); 83 } 84 85 90 public XYAreaChartTests(String name) { 91 super(name); 92 } 93 94 97 protected void setUp() { 98 this.chart = createChart(); 99 } 100 101 105 public void testDrawWithNullInfo() { 106 107 boolean success = false; 108 try { 109 BufferedImage image = new BufferedImage ( 110 200 , 100, BufferedImage.TYPE_INT_RGB 111 ); 112 Graphics2D g2 = image.createGraphics(); 113 this.chart.draw( 114 g2, new Rectangle2D.Double (0, 0, 200, 100), null, null 115 ); 116 g2.dispose(); 117 success = true; 118 } 119 catch (Exception e) { 120 success = false; 121 e.printStackTrace(); 122 } 123 assertTrue(success); 124 125 } 126 127 130 public void testReplaceDataset() { 131 132 XYSeries series1 = new XYSeries("Series 1"); 134 series1.add(10.0, 10.0); 135 series1.add(20.0, 20.0); 136 series1.add(30.0, 30.0); 137 XYDataset dataset = new XYSeriesCollection(series1); 138 139 LocalListener l = new LocalListener(); 140 this.chart.addChangeListener(l); 141 this.chart.getXYPlot().setDataset(dataset); 142 assertEquals(true, l.flag); 143 ValueAxis axis = this.chart.getXYPlot().getRangeAxis(); 144 Range range = axis.getRange(); 145 assertTrue("Expecting the lower bound of the range to be around 10: " 146 + range.getLowerBound(), range.getLowerBound() <= 10); 147 assertTrue("Expecting the upper bound of the range to be around 30: " 148 + range.getUpperBound(), range.getUpperBound() >= 30); 149 150 } 151 152 156 public void testSetSeriesToolTipGenerator() { 157 XYPlot plot = (XYPlot) this.chart.getPlot(); 158 XYItemRenderer renderer = plot.getRenderer(); 159 StandardXYToolTipGenerator tt = new StandardXYToolTipGenerator(); 160 renderer.setSeriesToolTipGenerator(0, tt); 161 XYToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0); 162 assertTrue(tt2 == tt); 163 } 164 165 170 private static JFreeChart createChart() { 171 172 XYSeries series1 = new XYSeries("Series 1"); 174 series1.add(1.0, 1.0); 175 series1.add(2.0, 2.0); 176 series1.add(3.0, 3.0); 177 XYDataset dataset = new XYSeriesCollection(series1); 178 179 return ChartFactory.createXYAreaChart( 181 "Area Chart", "Domain", 183 "Range", 184 dataset, PlotOrientation.VERTICAL, 186 true, true, true ); 190 191 } 192 193 197 static class LocalListener implements ChartChangeListener { 198 199 200 private boolean flag = false; 201 202 207 public void chartChanged(ChartChangeEvent event) { 208 this.flag = true; 209 } 210 211 } 212 213 } 214 | Popular Tags |