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.CategoryToolTipGenerator; 59 import org.jfree.chart.labels.StandardCategoryToolTipGenerator; 60 import org.jfree.chart.plot.CategoryPlot; 61 import org.jfree.chart.plot.PlotOrientation; 62 import org.jfree.chart.renderer.category.CategoryItemRenderer; 63 import org.jfree.chart.urls.CategoryURLGenerator; 64 import org.jfree.chart.urls.StandardCategoryURLGenerator; 65 import org.jfree.data.Range; 66 import org.jfree.data.category.CategoryDataset; 67 import org.jfree.data.general.DatasetUtilities; 68 69 72 public class AreaChartTests extends TestCase { 73 74 75 private JFreeChart chart; 76 77 82 public static Test suite() { 83 return new TestSuite(AreaChartTests.class); 84 } 85 86 91 public AreaChartTests(String name) { 92 super(name); 93 } 94 95 98 protected void setUp() { 99 this.chart = createAreaChart(); 100 } 101 102 106 public void testSetSeriesToolTipGenerator() { 107 CategoryPlot plot = (CategoryPlot) this.chart.getPlot(); 108 CategoryItemRenderer renderer = plot.getRenderer(); 109 StandardCategoryToolTipGenerator tt 110 = new StandardCategoryToolTipGenerator(); 111 renderer.setSeriesToolTipGenerator(0, tt); 112 CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0); 113 assertTrue(tt2 == tt); 114 } 115 116 120 public void testSetSeriesURLGenerator() { 121 CategoryPlot plot = (CategoryPlot) this.chart.getPlot(); 122 CategoryItemRenderer renderer = plot.getRenderer(); 123 StandardCategoryURLGenerator url1 124 = new StandardCategoryURLGenerator(); 125 renderer.setSeriesItemURLGenerator(0, url1); 126 CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0); 127 assertTrue(url2 == url1); 128 } 129 130 134 public void testDrawWithNullInfo() { 135 boolean success = false; 136 try { 137 BufferedImage image = new BufferedImage ( 138 200 , 100, BufferedImage.TYPE_INT_RGB 139 ); 140 Graphics2D g2 = image.createGraphics(); 141 this.chart.draw( 142 g2, new Rectangle2D.Double (0, 0, 200, 100), null, null 143 ); 144 g2.dispose(); 145 success = true; 146 } 147 catch (Exception e) { 148 success = false; 149 } 150 assertTrue(success); 151 } 152 153 156 public void testReplaceDataset() { 157 Number [][] data = new Integer [][] 158 {{new Integer (-30), new Integer (-20)}, 159 {new Integer (-10), new Integer (10)}, 160 {new Integer (20), new Integer (30)}}; 161 162 CategoryDataset newData = DatasetUtilities.createCategoryDataset( 163 "S", "C", data 164 ); 165 LocalListener l = new LocalListener(); 166 this.chart.addChangeListener(l); 167 this.chart.getCategoryPlot().setDataset(newData); 168 assertEquals(true, l.flag); 169 ValueAxis axis = this.chart.getCategoryPlot().getRangeAxis(); 170 Range range = axis.getRange(); 171 assertTrue("Expecting the lower bound of the range to be around -30: " 172 + range.getLowerBound(), range.getLowerBound() <= -30); 173 assertTrue("Expecting the upper bound of the range to be around 30: " 174 + range.getUpperBound(), range.getUpperBound() >= 30); 175 176 } 177 178 183 private static JFreeChart createAreaChart() { 184 Number [][] data = new Integer [][] 185 {{new Integer (-3), new Integer (-2)}, 186 {new Integer (-1), new Integer (1)}, 187 {new Integer (2), new Integer (3)}}; 188 CategoryDataset dataset 189 = DatasetUtilities.createCategoryDataset("S", "C", data); 190 return ChartFactory.createAreaChart( 191 "Area Chart", 192 "Domain", "Range", 193 dataset, 194 PlotOrientation.HORIZONTAL, 195 true, true, 197 true 198 ); 199 200 } 201 202 205 static class LocalListener implements ChartChangeListener { 206 207 208 private boolean flag = false; 209 210 215 public void chartChanged(ChartChangeEvent event) { 216 this.flag = true; 217 } 218 219 } 220 221 } 222 | Popular Tags |