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 import java.util.Calendar ; 49 import java.util.Date ; 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.event.ChartChangeEvent; 58 import org.jfree.chart.event.ChartChangeListener; 59 import org.jfree.chart.labels.CategoryToolTipGenerator; 60 import org.jfree.chart.labels.StandardCategoryToolTipGenerator; 61 import org.jfree.chart.plot.CategoryPlot; 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.category.IntervalCategoryDataset; 66 import org.jfree.data.gantt.Task; 67 import org.jfree.data.gantt.TaskSeries; 68 import org.jfree.data.gantt.TaskSeriesCollection; 69 import org.jfree.data.time.SimpleTimePeriod; 70 71 74 public class GanttChartTests extends TestCase { 75 76 77 private JFreeChart chart; 78 79 84 public static Test suite() { 85 return new TestSuite(GanttChartTests.class); 86 } 87 88 93 public GanttChartTests(String name) { 94 super(name); 95 } 96 97 100 protected void setUp() { 101 this.chart = createGanttChart(); 102 } 103 104 108 public void testDrawWithNullInfo() { 109 boolean success = false; 110 try { 111 BufferedImage image = new BufferedImage (200 , 100, 112 BufferedImage.TYPE_INT_RGB); 113 Graphics2D g2 = image.createGraphics(); 114 this.chart.draw(g2, new Rectangle2D.Double (0, 0, 200, 100), null, 115 null); 116 g2.dispose(); 117 success = true; 118 } 119 catch (Exception e) { 120 success = false; 121 } 122 assertTrue(success); 123 } 124 125 129 public void testDrawWithNullInfo2() { 130 boolean success = false; 131 try { 132 JFreeChart chart = createGanttChart(); 133 CategoryPlot plot = (CategoryPlot) chart.getPlot(); 134 plot.setDataset(createDataset()); 135 chart.createBufferedImage(300, 200, null); 136 success = true; 137 } 138 catch (NullPointerException e) { 139 success = false; 140 } 141 assertTrue(success); 142 } 143 144 147 public void testReplaceDataset() { 148 LocalListener l = new LocalListener(); 149 this.chart.addChangeListener(l); 150 this.chart.getCategoryPlot().setDataset(null); 151 assertEquals(true, l.flag); 152 } 153 154 158 public void testSetSeriesToolTipGenerator() { 159 CategoryPlot plot = (CategoryPlot) this.chart.getPlot(); 160 CategoryItemRenderer renderer = plot.getRenderer(); 161 StandardCategoryToolTipGenerator tt 162 = new StandardCategoryToolTipGenerator(); 163 renderer.setSeriesToolTipGenerator(0, tt); 164 CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0); 165 assertTrue(tt2 == tt); 166 } 167 168 172 public void testSetSeriesURLGenerator() { 173 CategoryPlot plot = (CategoryPlot) this.chart.getPlot(); 174 CategoryItemRenderer renderer = plot.getRenderer(); 175 StandardCategoryURLGenerator url1 176 = new StandardCategoryURLGenerator(); 177 renderer.setSeriesItemURLGenerator(0, url1); 178 CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0); 179 assertTrue(url2 == url1); 180 } 181 182 187 private static JFreeChart createGanttChart() { 188 189 return ChartFactory.createGanttChart( 191 "Gantt Chart", 192 "Domain", "Range", 193 null, 194 true, true, 196 true 197 ); 198 199 } 200 201 206 public static IntervalCategoryDataset createDataset() { 207 208 TaskSeries s1 = new TaskSeries("Scheduled"); 209 s1.add(new Task("Write Proposal", 210 new SimpleTimePeriod(date(1, Calendar.APRIL, 2001), 211 date(5, Calendar.APRIL, 2001)))); 212 s1.add(new Task("Obtain Approval", 213 new SimpleTimePeriod(date(9, Calendar.APRIL, 2001), 214 date(9, Calendar.APRIL, 2001)))); 215 s1.add(new Task("Requirements Analysis", 216 new SimpleTimePeriod(date(10, Calendar.APRIL, 2001), 217 date(5, Calendar.MAY, 2001)))); 218 s1.add(new Task("Design Phase", 219 new SimpleTimePeriod(date(6, Calendar.MAY, 2001), 220 date(30, Calendar.MAY, 2001)))); 221 s1.add(new Task("Design Signoff", 222 new SimpleTimePeriod(date(2, Calendar.JUNE, 2001), 223 date(2, Calendar.JUNE, 2001)))); 224 s1.add(new Task("Alpha Implementation", 225 new SimpleTimePeriod(date(3, Calendar.JUNE, 2001), 226 date(31, Calendar.JULY, 2001)))); 227 s1.add(new Task("Design Review", 228 new SimpleTimePeriod(date(1, Calendar.AUGUST, 2001), 229 date(8, Calendar.AUGUST, 2001)))); 230 s1.add(new Task("Revised Design Signoff", 231 new SimpleTimePeriod(date(10, Calendar.AUGUST, 2001), 232 date(10, Calendar.AUGUST, 2001)))); 233 s1.add(new Task("Beta Implementation", 234 new SimpleTimePeriod(date(12, Calendar.AUGUST, 2001), 235 date(12, Calendar.SEPTEMBER, 2001)))); 236 s1.add(new Task("Testing", 237 new SimpleTimePeriod(date(13, Calendar.SEPTEMBER, 2001), 238 date(31, Calendar.OCTOBER, 2001)))); 239 s1.add(new Task("Final Implementation", 240 new SimpleTimePeriod(date(1, Calendar.NOVEMBER, 2001), 241 date(15, Calendar.NOVEMBER, 2001)))); 242 s1.add(new Task("Signoff", 243 new SimpleTimePeriod(date(28, Calendar.NOVEMBER, 2001), 244 date(30, Calendar.NOVEMBER, 2001)))); 245 246 TaskSeries s2 = new TaskSeries("Actual"); 247 s2.add(new Task("Write Proposal", 248 new SimpleTimePeriod(date(1, Calendar.APRIL, 2001), 249 date(5, Calendar.APRIL, 2001)))); 250 s2.add(new Task("Obtain Approval", 251 new SimpleTimePeriod(date(9, Calendar.APRIL, 2001), 252 date(9, Calendar.APRIL, 2001)))); 253 s2.add(new Task("Requirements Analysis", 254 new SimpleTimePeriod(date(10, Calendar.APRIL, 2001), 255 date(15, Calendar.MAY, 2001)))); 256 s2.add(new Task("Design Phase", 257 new SimpleTimePeriod(date(15, Calendar.MAY, 2001), 258 date(17, Calendar.JUNE, 2001)))); 259 s2.add(new Task("Design Signoff", 260 new SimpleTimePeriod(date(30, Calendar.JUNE, 2001), 261 date(30, Calendar.JUNE, 2001)))); 262 s2.add(new Task("Alpha Implementation", 263 new SimpleTimePeriod(date(1, Calendar.JULY, 2001), 264 date(12, Calendar.SEPTEMBER, 2001)))); 265 s2.add(new Task("Design Review", 266 new SimpleTimePeriod(date(12, Calendar.SEPTEMBER, 2001), 267 date(22, Calendar.SEPTEMBER, 2001)))); 268 s2.add(new Task("Revised Design Signoff", 269 new SimpleTimePeriod(date(25, Calendar.SEPTEMBER, 2001), 270 date(27, Calendar.SEPTEMBER, 2001)))); 271 s2.add(new Task("Beta Implementation", 272 new SimpleTimePeriod(date(27, Calendar.SEPTEMBER, 2001), 273 date(30, Calendar.OCTOBER, 2001)))); 274 s2.add(new Task("Testing", 275 new SimpleTimePeriod(date(31, Calendar.OCTOBER, 2001), 276 date(17, Calendar.NOVEMBER, 2001)))); 277 s2.add(new Task("Final Implementation", 278 new SimpleTimePeriod(date(18, Calendar.NOVEMBER, 2001), 279 date(5, Calendar.DECEMBER, 2001)))); 280 s2.add(new Task("Signoff", 281 new SimpleTimePeriod(date(10, Calendar.DECEMBER, 2001), 282 date(11, Calendar.DECEMBER, 2001)))); 283 284 TaskSeriesCollection collection = new TaskSeriesCollection(); 285 collection.add(s1); 286 collection.add(s2); 287 288 return collection; 289 } 290 291 300 private static Date date(int day, int month, int year) { 301 302 Calendar calendar = Calendar.getInstance(); 303 calendar.set(year, month, day); 304 Date result = calendar.getTime(); 305 return result; 306 307 } 311 static class LocalListener implements ChartChangeListener { 312 313 314 private boolean flag = false; 315 316 321 public void chartChanged(ChartChangeEvent event) { 322 this.flag = true; 323 } 324 325 } 326 327 } 328 | Popular Tags |