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