1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2004, 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 under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * --------------------
  25. * TaskSeriesTests.java
  26. * --------------------
  27. * (C) Copyright 2004 by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: TaskSeriesTests.java,v 1.2 2005/01/12 10:52:17 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 30-Jul-2004 : Version 1 (DG);
  37. *
  38. */
  39. package org.jfree.data.gantt.junit;
  40. import java.io.ByteArrayInputStream;
  41. import java.io.ByteArrayOutputStream;
  42. import java.io.ObjectInput;
  43. import java.io.ObjectInputStream;
  44. import java.io.ObjectOutput;
  45. import java.io.ObjectOutputStream;
  46. import java.util.Date;
  47. import junit.framework.Test;
  48. import junit.framework.TestCase;
  49. import junit.framework.TestSuite;
  50. import org.jfree.data.gantt.Task;
  51. import org.jfree.data.gantt.TaskSeries;
  52. /**
  53. * Tests for the {@link TaskSeries} class.
  54. */
  55. public class TaskSeriesTests extends TestCase {
  56. /**
  57. * Returns the tests as a test suite.
  58. *
  59. * @return The test suite.
  60. */
  61. public static Test suite() {
  62. return new TestSuite(TaskSeriesTests.class);
  63. }
  64. /**
  65. * Constructs a new set of tests.
  66. *
  67. * @param name the name of the tests.
  68. */
  69. public TaskSeriesTests(String name) {
  70. super(name);
  71. }
  72. /**
  73. * Confirm that the equals method can distinguish all the required fields.
  74. */
  75. public void testEquals() {
  76. TaskSeries s1 = new TaskSeries("S");
  77. s1.add(new Task("T1", new Date(1), new Date(2)));
  78. s1.add(new Task("T2", new Date(11), new Date(22)));
  79. TaskSeries s2 = new TaskSeries("S");
  80. s2.add(new Task("T1", new Date(1), new Date(2)));
  81. s2.add(new Task("T2", new Date(11), new Date(22)));
  82. assertTrue(s1.equals(s2));
  83. assertTrue(s2.equals(s1));
  84. s1.add(new Task("T3", new Date(22), new Date(33)));
  85. assertFalse(s1.equals(s2));
  86. s2.add(new Task("T3", new Date(22), new Date(33)));
  87. assertTrue(s1.equals(s2));
  88. }
  89. /**
  90. * Confirm that cloning works.
  91. */
  92. public void testCloning() {
  93. TaskSeries s1 = new TaskSeries("S");
  94. s1.add(new Task("T1", new Date(1), new Date(2)));
  95. s1.add(new Task("T2", new Date(11), new Date(22)));
  96. TaskSeries s2 = null;
  97. try {
  98. s2 = (TaskSeries) s1.clone();
  99. }
  100. catch (CloneNotSupportedException e) {
  101. System.err.println("Failed to clone.");
  102. }
  103. assertTrue(s1 != s2);
  104. assertTrue(s1.getClass() == s2.getClass());
  105. assertTrue(s1.equals(s2));
  106. }
  107. /**
  108. * Serialize an instance, restore it, and check for equality.
  109. */
  110. public void testSerialization() {
  111. TaskSeries s1 = new TaskSeries("S");
  112. s1.add(new Task("T1", new Date(1), new Date(2)));
  113. s1.add(new Task("T2", new Date(11), new Date(22)));
  114. TaskSeries s2 = null;
  115. try {
  116. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  117. ObjectOutput out = new ObjectOutputStream(buffer);
  118. out.writeObject(s1);
  119. out.close();
  120. ObjectInput in = new ObjectInputStream(
  121. new ByteArrayInputStream(buffer.toByteArray())
  122. );
  123. s2 = (TaskSeries) in.readObject();
  124. in.close();
  125. }
  126. catch (Exception e) {
  127. System.out.println(e.toString());
  128. }
  129. assertEquals(s1, s2);
  130. }
  131. /**
  132. * Some checks for the getTask() method.
  133. */
  134. public void testGetTask() {
  135. TaskSeries s1 = new TaskSeries("S");
  136. s1.add(new Task("T1", new Date(1), new Date(2)));
  137. s1.add(new Task("T2", new Date(11), new Date(22)));
  138. Task t1 = s1.get("T1");
  139. assertTrue(t1.equals(new Task("T1", new Date(1), new Date(2))));
  140. Task t2 = s1.get("T2");
  141. assertTrue(t2.equals(new Task("T2", new Date(11), new Date(22))));
  142. Task t3 = s1.get("T3");
  143. assertTrue(t3 == null);
  144. }
  145. }