KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > junit > GanttChartTests


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * --------------------
28  * GanttChartTests.java
29  * --------------------
30  * (C) Copyright 2005, 2006, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: GanttChartTests.java,v 1.1.2.1 2006/10/03 15:41:30 mungady Exp $
36  *
37  * Changes:
38  * --------
39  * 12-Apr-2005 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.chart.junit;
44
45 import java.awt.Graphics2D JavaDoc;
46 import java.awt.geom.Rectangle2D JavaDoc;
47 import java.awt.image.BufferedImage JavaDoc;
48 import java.util.Calendar JavaDoc;
49 import java.util.Date JavaDoc;
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 /**
72  * Some tests for a Gantt chart.
73  */

74 public class GanttChartTests extends TestCase {
75
76     /** A chart. */
77     private JFreeChart chart;
78
79     /**
80      * Returns the tests as a test suite.
81      *
82      * @return The test suite.
83      */

84     public static Test suite() {
85         return new TestSuite(GanttChartTests.class);
86     }
87
88     /**
89      * Constructs a new set of tests.
90      *
91      * @param name the name of the tests.
92      */

93     public GanttChartTests(String JavaDoc name) {
94         super(name);
95     }
96
97     /**
98      * Common test setup.
99      */

100     protected void setUp() {
101         this.chart = createGanttChart();
102     }
103
104     /**
105      * Draws the chart with a <code>null</code> info object to make sure that
106      * no exceptions are thrown (a problem that was occurring at one point).
107      */

108     public void testDrawWithNullInfo() {
109         boolean success = false;
110         try {
111             BufferedImage JavaDoc image = new BufferedImage JavaDoc(200 , 100,
112                     BufferedImage.TYPE_INT_RGB);
113             Graphics2D JavaDoc g2 = image.createGraphics();
114             this.chart.draw(g2, new Rectangle2D.Double JavaDoc(0, 0, 200, 100), null,
115                     null);
116             g2.dispose();
117             success = true;
118         }
119         catch (Exception JavaDoc e) {
120             success = false;
121         }
122         assertTrue(success);
123     }
124
125     /**
126      * Draws the chart with a <code>null</code> info object to make sure that
127      * no exceptions are thrown (a problem that was occurring at one point).
128      */

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             /* BufferedImage img =*/ chart.createBufferedImage(300, 200, null);
136             success = true;
137         }
138         catch (NullPointerException JavaDoc e) {
139             success = false;
140         }
141         assertTrue(success);
142     }
143
144     /**
145      * Replaces the chart's dataset and then checks that the new dataset is OK.
146      */

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     /**
155      * Check that setting a tool tip generator for a series does override the
156      * default generator.
157      */

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     /**
169      * Check that setting a URL generator for a series does override the
170      * default generator.
171      */

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     /**
183      * Create a Gantt chart.
184      *
185      * @return The chart.
186      */

187     private static JFreeChart createGanttChart() {
188
189         // create the chart...
190
return ChartFactory.createGanttChart(
191             "Gantt Chart",
192             "Domain", "Range",
193             null,
194             true, // include legend
195
true,
196             true
197         );
198
199     }
200
201     /**
202      * Creates a sample dataset for a Gantt chart.
203      *
204      * @return The dataset.
205      */

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     /**
292      * Utility method for creating <code>Date</code> objects.
293      *
294      * @param day the date.
295      * @param month the month.
296      * @param year the year.
297      *
298      * @return a date.
299      */

300     private static Date JavaDoc date(int day, int month, int year) {
301
302         Calendar JavaDoc calendar = Calendar.getInstance();
303         calendar.set(year, month, day);
304         Date JavaDoc result = calendar.getTime();
305         return result;
306
307     } /**
308      * A chart change listener.
309      *
310      */

311     static class LocalListener implements ChartChangeListener {
312
313         /** A flag. */
314         private boolean flag = false;
315
316         /**
317          * Event handler.
318          *
319          * @param event the event.
320          */

321         public void chartChanged(ChartChangeEvent event) {
322             this.flag = true;
323         }
324
325     }
326
327 }
328
Popular Tags