GanttChartTest.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2013, 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. * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  25. * Other names may be trademarks of their respective owners.]
  26. *
  27. * -------------------
  28. * GanttChartTest.java
  29. * -------------------
  30. * (C) Copyright 2005-2013, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes:
  36. * --------
  37. * 12-Apr-2005 : Version 1 (DG);
  38. *
  39. */
  40. package org.jfree.chart;
  41. import java.awt.Graphics2D;
  42. import java.awt.geom.Rectangle2D;
  43. import java.awt.image.BufferedImage;
  44. import java.util.Calendar;
  45. import java.util.Date;
  46. import org.jfree.chart.event.ChartChangeEvent;
  47. import org.jfree.chart.event.ChartChangeListener;
  48. import org.jfree.chart.labels.CategoryToolTipGenerator;
  49. import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
  50. import org.jfree.chart.plot.CategoryPlot;
  51. import org.jfree.chart.renderer.category.CategoryItemRenderer;
  52. import org.jfree.chart.urls.CategoryURLGenerator;
  53. import org.jfree.chart.urls.StandardCategoryURLGenerator;
  54. import org.jfree.data.category.IntervalCategoryDataset;
  55. import org.jfree.data.gantt.Task;
  56. import org.jfree.data.gantt.TaskSeries;
  57. import org.jfree.data.gantt.TaskSeriesCollection;
  58. import org.jfree.data.time.SimpleTimePeriod;
  59. import org.junit.Before;
  60. import org.junit.Test;
  61. import static org.junit.Assert.fail;
  62. import static org.junit.Assert.assertEquals;
  63. import static org.junit.Assert.assertSame;
  64. /**
  65. * Some tests for a Gantt chart.
  66. */
  67. public class GanttChartTest {
  68. /** A chart. */
  69. private JFreeChart chart;
  70. /**
  71. * Common test setup.
  72. */
  73. @Before
  74. public void setUp() {
  75. this.chart = createGanttChart();
  76. }
  77. /**
  78. * Draws the chart with a <code>null</code> info object to make sure that
  79. * no exceptions are thrown (a problem that was occurring at one point).
  80. */
  81. @Test
  82. public void testDrawWithNullInfo() {
  83. try {
  84. BufferedImage image = new BufferedImage(200 , 100,
  85. BufferedImage.TYPE_INT_RGB);
  86. Graphics2D g2 = image.createGraphics();
  87. this.chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null,
  88. null);
  89. g2.dispose();
  90. }
  91. catch (Exception e) {
  92. fail("There should be no exception.");
  93. }
  94. }
  95. /**
  96. * Draws the chart with a <code>null</code> info object to make sure that
  97. * no exceptions are thrown (a problem that was occurring at one point).
  98. */
  99. @Test
  100. public void testDrawWithNullInfo2() {
  101. JFreeChart chart = createGanttChart();
  102. CategoryPlot plot = (CategoryPlot) chart.getPlot();
  103. plot.setDataset(createDataset());
  104. /* BufferedImage img =*/ chart.createBufferedImage(300, 200, null);
  105. //FIXME we should really assert a value
  106. }
  107. /**
  108. * Replaces the chart's dataset and then checks that the new dataset is OK.
  109. */
  110. @Test
  111. public void testReplaceDataset() {
  112. LocalListener l = new LocalListener();
  113. this.chart.addChangeListener(l);
  114. CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
  115. plot.setDataset(null);
  116. assertEquals(true, l.flag);
  117. }
  118. /**
  119. * Check that setting a tool tip generator for a series does override the
  120. * default generator.
  121. */
  122. @Test
  123. public void testSetSeriesToolTipGenerator() {
  124. CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
  125. CategoryItemRenderer renderer = plot.getRenderer();
  126. StandardCategoryToolTipGenerator tt
  127. = new StandardCategoryToolTipGenerator();
  128. renderer.setSeriesToolTipGenerator(0, tt);
  129. CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0);
  130. assertSame(tt2, tt);
  131. }
  132. /**
  133. * Check that setting a URL generator for a series does override the
  134. * default generator.
  135. */
  136. @Test
  137. public void testSetSeriesURLGenerator() {
  138. CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
  139. CategoryItemRenderer renderer = plot.getRenderer();
  140. StandardCategoryURLGenerator url1
  141. = new StandardCategoryURLGenerator();
  142. renderer.setSeriesItemURLGenerator(0, url1);
  143. CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0);
  144. assertSame(url2, url1);
  145. }
  146. /**
  147. * Create a Gantt chart.
  148. *
  149. * @return The chart.
  150. */
  151. private static JFreeChart createGanttChart() {
  152. return ChartFactory.createGanttChart(
  153. "Gantt Chart",
  154. "Domain", "Range",
  155. null,
  156. true, // include legend
  157. true,
  158. true
  159. );
  160. }
  161. /**
  162. * Creates a sample dataset for a Gantt chart.
  163. *
  164. * @return The dataset.
  165. */
  166. public static IntervalCategoryDataset createDataset() {
  167. TaskSeries s1 = new TaskSeries("Scheduled");
  168. s1.add(new Task("Write Proposal",
  169. new SimpleTimePeriod(date(1, Calendar.APRIL, 2001),
  170. date(5, Calendar.APRIL, 2001))));
  171. s1.add(new Task("Obtain Approval",
  172. new SimpleTimePeriod(date(9, Calendar.APRIL, 2001),
  173. date(9, Calendar.APRIL, 2001))));
  174. s1.add(new Task("Requirements Analysis",
  175. new SimpleTimePeriod(date(10, Calendar.APRIL, 2001),
  176. date(5, Calendar.MAY, 2001))));
  177. s1.add(new Task("Design Phase",
  178. new SimpleTimePeriod(date(6, Calendar.MAY, 2001),
  179. date(30, Calendar.MAY, 2001))));
  180. s1.add(new Task("Design Signoff",
  181. new SimpleTimePeriod(date(2, Calendar.JUNE, 2001),
  182. date(2, Calendar.JUNE, 2001))));
  183. s1.add(new Task("Alpha Implementation",
  184. new SimpleTimePeriod(date(3, Calendar.JUNE, 2001),
  185. date(31, Calendar.JULY, 2001))));
  186. s1.add(new Task("Design Review",
  187. new SimpleTimePeriod(date(1, Calendar.AUGUST, 2001),
  188. date(8, Calendar.AUGUST, 2001))));
  189. s1.add(new Task("Revised Design Signoff",
  190. new SimpleTimePeriod(date(10, Calendar.AUGUST, 2001),
  191. date(10, Calendar.AUGUST, 2001))));
  192. s1.add(new Task("Beta Implementation",
  193. new SimpleTimePeriod(date(12, Calendar.AUGUST, 2001),
  194. date(12, Calendar.SEPTEMBER, 2001))));
  195. s1.add(new Task("Testing",
  196. new SimpleTimePeriod(date(13, Calendar.SEPTEMBER, 2001),
  197. date(31, Calendar.OCTOBER, 2001))));
  198. s1.add(new Task("Final Implementation",
  199. new SimpleTimePeriod(date(1, Calendar.NOVEMBER, 2001),
  200. date(15, Calendar.NOVEMBER, 2001))));
  201. s1.add(new Task("Signoff",
  202. new SimpleTimePeriod(date(28, Calendar.NOVEMBER, 2001),
  203. date(30, Calendar.NOVEMBER, 2001))));
  204. TaskSeries s2 = new TaskSeries("Actual");
  205. s2.add(new Task("Write Proposal",
  206. new SimpleTimePeriod(date(1, Calendar.APRIL, 2001),
  207. date(5, Calendar.APRIL, 2001))));
  208. s2.add(new Task("Obtain Approval",
  209. new SimpleTimePeriod(date(9, Calendar.APRIL, 2001),
  210. date(9, Calendar.APRIL, 2001))));
  211. s2.add(new Task("Requirements Analysis",
  212. new SimpleTimePeriod(date(10, Calendar.APRIL, 2001),
  213. date(15, Calendar.MAY, 2001))));
  214. s2.add(new Task("Design Phase",
  215. new SimpleTimePeriod(date(15, Calendar.MAY, 2001),
  216. date(17, Calendar.JUNE, 2001))));
  217. s2.add(new Task("Design Signoff",
  218. new SimpleTimePeriod(date(30, Calendar.JUNE, 2001),
  219. date(30, Calendar.JUNE, 2001))));
  220. s2.add(new Task("Alpha Implementation",
  221. new SimpleTimePeriod(date(1, Calendar.JULY, 2001),
  222. date(12, Calendar.SEPTEMBER, 2001))));
  223. s2.add(new Task("Design Review",
  224. new SimpleTimePeriod(date(12, Calendar.SEPTEMBER, 2001),
  225. date(22, Calendar.SEPTEMBER, 2001))));
  226. s2.add(new Task("Revised Design Signoff",
  227. new SimpleTimePeriod(date(25, Calendar.SEPTEMBER, 2001),
  228. date(27, Calendar.SEPTEMBER, 2001))));
  229. s2.add(new Task("Beta Implementation",
  230. new SimpleTimePeriod(date(27, Calendar.SEPTEMBER, 2001),
  231. date(30, Calendar.OCTOBER, 2001))));
  232. s2.add(new Task("Testing",
  233. new SimpleTimePeriod(date(31, Calendar.OCTOBER, 2001),
  234. date(17, Calendar.NOVEMBER, 2001))));
  235. s2.add(new Task("Final Implementation",
  236. new SimpleTimePeriod(date(18, Calendar.NOVEMBER, 2001),
  237. date(5, Calendar.DECEMBER, 2001))));
  238. s2.add(new Task("Signoff",
  239. new SimpleTimePeriod(date(10, Calendar.DECEMBER, 2001),
  240. date(11, Calendar.DECEMBER, 2001))));
  241. TaskSeriesCollection collection = new TaskSeriesCollection();
  242. collection.add(s1);
  243. collection.add(s2);
  244. return collection;
  245. }
  246. /**
  247. * Utility method for creating <code>Date</code> objects.
  248. *
  249. * @param day the date.
  250. * @param month the month.
  251. * @param year the year.
  252. *
  253. * @return a date.
  254. */
  255. private static Date date(int day, int month, int year) {
  256. Calendar calendar = Calendar.getInstance();
  257. calendar.set(year, month, day);
  258. Date result = calendar.getTime();
  259. return result;
  260. } /**
  261. * A chart change listener.
  262. *
  263. */
  264. static class LocalListener implements ChartChangeListener {
  265. /** A flag. */
  266. private boolean flag;
  267. /**
  268. * Event handler.
  269. *
  270. * @param event the event.
  271. */
  272. @Override
  273. public void chartChanged(ChartChangeEvent event) {
  274. this.flag = true;
  275. }
  276. }
  277. }