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 BarChart3DTests extends TestCase { 75 76 77 private JFreeChart chart; 78 79 84 public static Test suite() { 85 return new TestSuite(BarChart3DTests.class); 86 } 87 88 93 public BarChart3DTests(String name) { 94 super(name); 95 } 96 97 100 protected void setUp() { 101 102 this.chart = createBarChart3D(); 103 104 } 105 106 110 public void testDrawWithNullInfo() { 111 boolean success = false; 112 try { 113 BufferedImage image = new BufferedImage ( 114 200 , 100, BufferedImage.TYPE_INT_RGB 115 ); 116 Graphics2D g2 = image.createGraphics(); 117 this.chart.draw( 118 g2, new Rectangle2D.Double (0, 0, 200, 100), null, null 119 ); 120 g2.dispose(); 121 success = true; 122 } 123 catch (Exception e) { 124 success = false; 125 } 126 assertTrue(success); 127 } 128 129 132 public void testReplaceDataset() { 133 134 Number [][] data = new Integer [][] 136 {{new Integer (-30), new Integer (-20)}, 137 {new Integer (-10), new Integer (10)}, 138 {new Integer (20), new Integer (30)}}; 139 140 CategoryDataset newData = DatasetUtilities.createCategoryDataset( 141 "S", "C", data 142 ); 143 144 LocalListener l = new LocalListener(); 145 this.chart.addChangeListener(l); 146 this.chart.getCategoryPlot().setDataset(newData); 147 assertEquals(true, l.flag); 148 ValueAxis axis = this.chart.getCategoryPlot().getRangeAxis(); 149 Range range = axis.getRange(); 150 assertTrue( 151 "Expecting the lower bound of the range to be around -30: " 152 + range.getLowerBound(), range.getLowerBound() <= -30 153 ); 154 assertTrue( 155 "Expecting the upper bound of the range to be around 30: " 156 + range.getUpperBound(), range.getUpperBound() >= 30 157 ); 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 createBarChart3D() { 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 = DatasetUtilities.createCategoryDataset( 203 "S", "C", data 204 ); 205 206 return ChartFactory.createBarChart3D( 208 "Bar Chart 3D", 209 "Domain", 210 "Range", 211 dataset, 212 PlotOrientation.HORIZONTAL, 213 true, true, 215 true 216 ); 217 218 } 219 220 224 static class LocalListener implements ChartChangeListener { 225 226 227 private boolean flag = false; 228 229 234 public void chartChanged(ChartChangeEvent event) { 235 this.flag = true; 236 } 237 238 } 239 240 } 241 | Popular Tags |