1 44 45 package org.jfree.chart.junit; 46 47 import java.awt.Graphics2D ; 48 import java.awt.geom.Rectangle2D ; 49 import java.awt.image.BufferedImage ; 50 51 import junit.framework.Test; 52 import junit.framework.TestCase; 53 import junit.framework.TestSuite; 54 55 import org.jfree.chart.ChartFactory; 56 import org.jfree.chart.JFreeChart; 57 import org.jfree.chart.axis.ValueAxis; 58 import org.jfree.chart.event.ChartChangeEvent; 59 import org.jfree.chart.event.ChartChangeListener; 60 import org.jfree.chart.labels.CategoryToolTipGenerator; 61 import org.jfree.chart.labels.StandardCategoryToolTipGenerator; 62 import org.jfree.chart.plot.CategoryPlot; 63 import org.jfree.chart.plot.PlotOrientation; 64 import org.jfree.chart.renderer.category.CategoryItemRenderer; 65 import org.jfree.chart.urls.CategoryURLGenerator; 66 import org.jfree.chart.urls.StandardCategoryURLGenerator; 67 import org.jfree.data.Range; 68 import org.jfree.data.category.CategoryDataset; 69 import org.jfree.data.general.DatasetUtilities; 70 71 74 public class BarChartTests extends TestCase { 75 76 77 private JFreeChart chart; 78 79 84 public static Test suite() { 85 return new TestSuite(BarChartTests.class); 86 } 87 88 93 public BarChartTests(String name) { 94 super(name); 95 } 96 97 100 protected void setUp() { 101 102 this.chart = createBarChart(); 103 104 } 105 106 110 public void testDrawWithNullInfo() { 111 112 boolean success = false; 113 114 try { 115 BufferedImage image = new BufferedImage ( 116 200 , 100, BufferedImage.TYPE_INT_RGB 117 ); 118 Graphics2D g2 = image.createGraphics(); 119 this.chart.draw( 120 g2, new Rectangle2D.Double (0, 0, 200, 100), null, null 121 ); 122 g2.dispose(); 123 success = true; 124 } 125 catch (Exception e) { 126 success = false; 127 } 128 129 assertTrue(success); 130 131 } 132 133 136 public void testReplaceDataset() { 137 138 Number [][] data = new Integer [][] 140 {{new Integer (-30), new Integer (-20)}, 141 {new Integer (-10), new Integer (10)}, 142 {new Integer (20), new Integer (30)}}; 143 144 CategoryDataset newData = DatasetUtilities.createCategoryDataset( 145 "S", "C", data 146 ); 147 148 LocalListener l = new LocalListener(); 149 this.chart.addChangeListener(l); 150 this.chart.getCategoryPlot().setDataset(newData); 151 assertEquals(true, l.flag); 152 ValueAxis axis = this.chart.getCategoryPlot().getRangeAxis(); 153 Range range = axis.getRange(); 154 assertTrue("Expecting the lower bound of the range to be around -30: " 155 + range.getLowerBound(), range.getLowerBound() <= -30); 156 assertTrue("Expecting the upper bound of the range to be around 30: " 157 + range.getUpperBound(), range.getUpperBound() >= 30); 158 159 } 160 161 165 public void testSetSeriesToolTipGenerator() { 166 CategoryPlot plot = (CategoryPlot) this.chart.getPlot(); 167 CategoryItemRenderer renderer = plot.getRenderer(); 168 StandardCategoryToolTipGenerator tt 169 = new StandardCategoryToolTipGenerator(); 170 renderer.setSeriesToolTipGenerator(0, tt); 171 CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0); 172 assertTrue(tt2 == tt); 173 } 174 175 179 public void testSetSeriesURLGenerator() { 180 CategoryPlot plot = (CategoryPlot) this.chart.getPlot(); 181 CategoryItemRenderer renderer = plot.getRenderer(); 182 StandardCategoryURLGenerator url1 183 = new StandardCategoryURLGenerator(); 184 renderer.setSeriesItemURLGenerator(0, url1); 185 CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0); 186 assertTrue(url2 == url1); 187 } 188 189 194 private static JFreeChart createBarChart() { 195 196 Number [][] data = new Integer [][] 198 {{new Integer (-3), new Integer (-2)}, 199 {new Integer (-1), new Integer (1)}, 200 {new Integer (2), new Integer (3)}}; 201 202 CategoryDataset dataset 203 = DatasetUtilities.createCategoryDataset("S", "C", data); 204 205 return ChartFactory.createBarChart( 207 "Bar Chart", 208 "Domain", "Range", 209 dataset, 210 PlotOrientation.HORIZONTAL, 211 true, true, 213 true 214 ); 215 216 } 217 218 222 static class LocalListener implements ChartChangeListener { 223 224 225 private boolean flag = false; 226 227 232 public void chartChanged(ChartChangeEvent event) { 233 this.flag = true; 234 } 235 236 } 237 238 } 239 | Popular Tags |