1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, 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. * TaskTests.java
  26. * --------------
  27. * (C) Copyright 2004, 2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: TaskTests.java,v 1.2 2005/01/12 10:45:18 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.time.SimpleTimePeriod;
  52. /**
  53. * Tests for the {@link Task} class.
  54. */
  55. public class TaskTests 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(TaskTests.class);
  63. }
  64. /**
  65. * Constructs a new set of tests.
  66. *
  67. * @param name the name of the tests.
  68. */
  69. public TaskTests(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. Task t1 = new Task("T", new Date(1), new Date(2));
  77. Task t2 = new Task("T", new Date(1), new Date(2));
  78. assertTrue(t1.equals(t2));
  79. assertTrue(t2.equals(t1));
  80. t1.setDescription("X");
  81. assertFalse(t1.equals(t2));
  82. t2.setDescription("X");
  83. assertTrue(t1.equals(t2));
  84. t1.setDuration(new SimpleTimePeriod(new Date(2), new Date(3)));
  85. assertFalse(t1.equals(t2));
  86. t2.setDuration(new SimpleTimePeriod(new Date(2), new Date(3)));
  87. assertTrue(t1.equals(t2));
  88. t1.setPercentComplete(0.5);
  89. assertFalse(t1.equals(t2));
  90. t2.setPercentComplete(0.5);
  91. assertTrue(t1.equals(t2));
  92. t1.addSubtask(new Task("T", new Date(22), new Date(33)));
  93. assertFalse(t1.equals(t2));
  94. t2.addSubtask(new Task("T", new Date(22), new Date(33)));
  95. assertTrue(t1.equals(t2));
  96. }
  97. /**
  98. * Confirm that cloning works.
  99. */
  100. public void testCloning() {
  101. Task t1 = new Task("T", new Date(1), new Date(2));
  102. Task t2 = null;
  103. try {
  104. t2 = (Task) t1.clone();
  105. }
  106. catch (CloneNotSupportedException e) {
  107. System.err.println("Failed to clone.");
  108. }
  109. assertTrue(t1 != t2);
  110. assertTrue(t1.getClass() == t2.getClass());
  111. assertTrue(t1.equals(t2));
  112. }
  113. /**
  114. * Serialize an instance, restore it, and check for equality.
  115. */
  116. public void testSerialization() {
  117. Task t1 = new Task("T", new Date(1), new Date(2));
  118. Task t2 = null;
  119. try {
  120. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  121. ObjectOutput out = new ObjectOutputStream(buffer);
  122. out.writeObject(t1);
  123. out.close();
  124. ObjectInput in = new ObjectInputStream(
  125. new ByteArrayInputStream(buffer.toByteArray())
  126. );
  127. t2 = (Task) in.readObject();
  128. in.close();
  129. }
  130. catch (Exception e) {
  131. System.out.println(e.toString());
  132. }
  133. assertEquals(t1, t2);
  134. }
  135. /**
  136. * Check the getSubTaskCount() method.
  137. */
  138. public void testGetSubTaskCount() {
  139. Task t1 = new Task("T", new Date(100), new Date(200));
  140. assertEquals(0, t1.getSubtaskCount());
  141. t1.addSubtask(new Task("S1", new Date(100), new Date(110)));
  142. assertEquals(1, t1.getSubtaskCount());
  143. Task s2 = new Task("S2", new Date(111), new Date(120));
  144. t1.addSubtask(s2);
  145. assertEquals(2, t1.getSubtaskCount());
  146. t1.addSubtask(new Task("S3", new Date(121), new Date(130)));
  147. assertEquals(3, t1.getSubtaskCount());
  148. t1.removeSubtask(s2);
  149. assertEquals(2, t1.getSubtaskCount());
  150. }
  151. }