001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * --------------------
028: * GanttChartTests.java
029: * --------------------
030: * (C) Copyright 2005-2007, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: GanttChartTests.java,v 1.1.2.2 2007/03/02 11:16:20 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 12-Apr-2005 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.junit;
044:
045: import java.awt.Graphics2D;
046: import java.awt.geom.Rectangle2D;
047: import java.awt.image.BufferedImage;
048: import java.util.Calendar;
049: import java.util.Date;
050:
051: import junit.framework.Test;
052: import junit.framework.TestCase;
053: import junit.framework.TestSuite;
054:
055: import org.jfree.chart.ChartFactory;
056: import org.jfree.chart.JFreeChart;
057: import org.jfree.chart.event.ChartChangeEvent;
058: import org.jfree.chart.event.ChartChangeListener;
059: import org.jfree.chart.labels.CategoryToolTipGenerator;
060: import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
061: import org.jfree.chart.plot.CategoryPlot;
062: import org.jfree.chart.renderer.category.CategoryItemRenderer;
063: import org.jfree.chart.urls.CategoryURLGenerator;
064: import org.jfree.chart.urls.StandardCategoryURLGenerator;
065: import org.jfree.data.category.IntervalCategoryDataset;
066: import org.jfree.data.gantt.Task;
067: import org.jfree.data.gantt.TaskSeries;
068: import org.jfree.data.gantt.TaskSeriesCollection;
069: import org.jfree.data.time.SimpleTimePeriod;
070:
071: /**
072: * Some tests for a Gantt chart.
073: */
074: public class GanttChartTests extends TestCase {
075:
076: /** A chart. */
077: private JFreeChart chart;
078:
079: /**
080: * Returns the tests as a test suite.
081: *
082: * @return The test suite.
083: */
084: public static Test suite() {
085: return new TestSuite(GanttChartTests.class);
086: }
087:
088: /**
089: * Constructs a new set of tests.
090: *
091: * @param name the name of the tests.
092: */
093: public GanttChartTests(String name) {
094: super (name);
095: }
096:
097: /**
098: * Common test setup.
099: */
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 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),
115: null, null);
116: g2.dispose();
117: success = true;
118: } catch (Exception e) {
119: success = false;
120: }
121: assertTrue(success);
122: }
123:
124: /**
125: * Draws the chart with a <code>null</code> info object to make sure that
126: * no exceptions are thrown (a problem that was occurring at one point).
127: */
128: public void testDrawWithNullInfo2() {
129: boolean success = false;
130: try {
131: JFreeChart chart = createGanttChart();
132: CategoryPlot plot = (CategoryPlot) chart.getPlot();
133: plot.setDataset(createDataset());
134: /* BufferedImage img =*/chart.createBufferedImage(300,
135: 200, null);
136: success = true;
137: } catch (NullPointerException e) {
138: success = false;
139: }
140: assertTrue(success);
141: }
142:
143: /**
144: * Replaces the chart's dataset and then checks that the new dataset is OK.
145: */
146: public void testReplaceDataset() {
147: LocalListener l = new LocalListener();
148: this .chart.addChangeListener(l);
149: CategoryPlot plot = (CategoryPlot) this .chart.getPlot();
150: plot.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 = new StandardCategoryToolTipGenerator();
162: renderer.setSeriesToolTipGenerator(0, tt);
163: CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0,
164: 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 = new StandardCategoryURLGenerator();
176: renderer.setSeriesItemURLGenerator(0, url1);
177: CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0);
178: assertTrue(url2 == url1);
179: }
180:
181: /**
182: * Create a Gantt chart.
183: *
184: * @return The chart.
185: */
186: private static JFreeChart createGanttChart() {
187:
188: // create the chart...
189: return ChartFactory.createGanttChart("Gantt Chart", "Domain",
190: "Range", null, true, // include legend
191: true, true);
192:
193: }
194:
195: /**
196: * Creates a sample dataset for a Gantt chart.
197: *
198: * @return The dataset.
199: */
200: public static IntervalCategoryDataset createDataset() {
201:
202: TaskSeries s1 = new TaskSeries("Scheduled");
203: s1.add(new Task("Write Proposal", new SimpleTimePeriod(date(1,
204: Calendar.APRIL, 2001), date(5, Calendar.APRIL, 2001))));
205: s1.add(new Task("Obtain Approval", new SimpleTimePeriod(date(9,
206: Calendar.APRIL, 2001), date(9, Calendar.APRIL, 2001))));
207: s1.add(new Task("Requirements Analysis", new SimpleTimePeriod(
208: date(10, Calendar.APRIL, 2001), date(5, Calendar.MAY,
209: 2001))));
210: s1.add(new Task("Design Phase", new SimpleTimePeriod(date(6,
211: Calendar.MAY, 2001), date(30, Calendar.MAY, 2001))));
212: s1.add(new Task("Design Signoff", new SimpleTimePeriod(date(2,
213: Calendar.JUNE, 2001), date(2, Calendar.JUNE, 2001))));
214: s1.add(new Task("Alpha Implementation", new SimpleTimePeriod(
215: date(3, Calendar.JUNE, 2001), date(31, Calendar.JULY,
216: 2001))));
217: s1
218: .add(new Task("Design Review", new SimpleTimePeriod(
219: date(1, Calendar.AUGUST, 2001), date(8,
220: Calendar.AUGUST, 2001))));
221: s1.add(new Task("Revised Design Signoff", new SimpleTimePeriod(
222: date(10, Calendar.AUGUST, 2001), date(10,
223: Calendar.AUGUST, 2001))));
224: s1.add(new Task("Beta Implementation", new SimpleTimePeriod(
225: date(12, Calendar.AUGUST, 2001), date(12,
226: Calendar.SEPTEMBER, 2001))));
227: s1.add(new Task("Testing", new SimpleTimePeriod(date(13,
228: Calendar.SEPTEMBER, 2001), date(31, Calendar.OCTOBER,
229: 2001))));
230: s1.add(new Task("Final Implementation", new SimpleTimePeriod(
231: date(1, Calendar.NOVEMBER, 2001), date(15,
232: Calendar.NOVEMBER, 2001))));
233: s1.add(new Task("Signoff", new SimpleTimePeriod(date(28,
234: Calendar.NOVEMBER, 2001), date(30, Calendar.NOVEMBER,
235: 2001))));
236:
237: TaskSeries s2 = new TaskSeries("Actual");
238: s2.add(new Task("Write Proposal", new SimpleTimePeriod(date(1,
239: Calendar.APRIL, 2001), date(5, Calendar.APRIL, 2001))));
240: s2.add(new Task("Obtain Approval", new SimpleTimePeriod(date(9,
241: Calendar.APRIL, 2001), date(9, Calendar.APRIL, 2001))));
242: s2.add(new Task("Requirements Analysis", new SimpleTimePeriod(
243: date(10, Calendar.APRIL, 2001), date(15, Calendar.MAY,
244: 2001))));
245: s2.add(new Task("Design Phase", new SimpleTimePeriod(date(15,
246: Calendar.MAY, 2001), date(17, Calendar.JUNE, 2001))));
247: s2.add(new Task("Design Signoff", new SimpleTimePeriod(date(30,
248: Calendar.JUNE, 2001), date(30, Calendar.JUNE, 2001))));
249: s2.add(new Task("Alpha Implementation", new SimpleTimePeriod(
250: date(1, Calendar.JULY, 2001), date(12,
251: Calendar.SEPTEMBER, 2001))));
252: s2.add(new Task("Design Review", new SimpleTimePeriod(date(12,
253: Calendar.SEPTEMBER, 2001), date(22, Calendar.SEPTEMBER,
254: 2001))));
255: s2.add(new Task("Revised Design Signoff", new SimpleTimePeriod(
256: date(25, Calendar.SEPTEMBER, 2001), date(27,
257: Calendar.SEPTEMBER, 2001))));
258: s2.add(new Task("Beta Implementation", new SimpleTimePeriod(
259: date(27, Calendar.SEPTEMBER, 2001), date(30,
260: Calendar.OCTOBER, 2001))));
261: s2.add(new Task("Testing", new SimpleTimePeriod(date(31,
262: Calendar.OCTOBER, 2001), date(17, Calendar.NOVEMBER,
263: 2001))));
264: s2.add(new Task("Final Implementation", new SimpleTimePeriod(
265: date(18, Calendar.NOVEMBER, 2001), date(5,
266: Calendar.DECEMBER, 2001))));
267: s2.add(new Task("Signoff", new SimpleTimePeriod(date(10,
268: Calendar.DECEMBER, 2001), date(11, Calendar.DECEMBER,
269: 2001))));
270:
271: TaskSeriesCollection collection = new TaskSeriesCollection();
272: collection.add(s1);
273: collection.add(s2);
274:
275: return collection;
276: }
277:
278: /**
279: * Utility method for creating <code>Date</code> objects.
280: *
281: * @param day the date.
282: * @param month the month.
283: * @param year the year.
284: *
285: * @return a date.
286: */
287: private static Date date(int day, int month, int year) {
288:
289: Calendar calendar = Calendar.getInstance();
290: calendar.set(year, month, day);
291: Date result = calendar.getTime();
292: return result;
293:
294: }
295:
296: /**
297: * A chart change listener.
298: *
299: */
300: static class LocalListener implements ChartChangeListener {
301:
302: /** A flag. */
303: private boolean flag = false;
304:
305: /**
306: * Event handler.
307: *
308: * @param event the event.
309: */
310: public void chartChanged(ChartChangeEvent event) {
311: this .flag = true;
312: }
313:
314: }
315:
316: }
|