001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2005, 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: * TaskTests.java
029: * --------------
030: * (C) Copyright 2004, 2005, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: TaskTests.java,v 1.1.2.1 2006/10/03 15:41:39 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 30-Jul-2004 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.data.gantt.junit;
044:
045: import java.io.ByteArrayInputStream;
046: import java.io.ByteArrayOutputStream;
047: import java.io.ObjectInput;
048: import java.io.ObjectInputStream;
049: import java.io.ObjectOutput;
050: import java.io.ObjectOutputStream;
051: import java.util.Date;
052:
053: import junit.framework.Test;
054: import junit.framework.TestCase;
055: import junit.framework.TestSuite;
056:
057: import org.jfree.data.gantt.Task;
058: import org.jfree.data.time.SimpleTimePeriod;
059:
060: /**
061: * Tests for the {@link Task} class.
062: */
063: public class TaskTests extends TestCase {
064:
065: /**
066: * Returns the tests as a test suite.
067: *
068: * @return The test suite.
069: */
070: public static Test suite() {
071: return new TestSuite(TaskTests.class);
072: }
073:
074: /**
075: * Constructs a new set of tests.
076: *
077: * @param name the name of the tests.
078: */
079: public TaskTests(String name) {
080: super (name);
081: }
082:
083: /**
084: * Confirm that the equals method can distinguish all the required fields.
085: */
086: public void testEquals() {
087:
088: Task t1 = new Task("T", new Date(1), new Date(2));
089: Task t2 = new Task("T", new Date(1), new Date(2));
090: assertTrue(t1.equals(t2));
091: assertTrue(t2.equals(t1));
092:
093: t1.setDescription("X");
094: assertFalse(t1.equals(t2));
095: t2.setDescription("X");
096: assertTrue(t1.equals(t2));
097:
098: t1.setDuration(new SimpleTimePeriod(new Date(2), new Date(3)));
099: assertFalse(t1.equals(t2));
100: t2.setDuration(new SimpleTimePeriod(new Date(2), new Date(3)));
101: assertTrue(t1.equals(t2));
102:
103: t1.setPercentComplete(0.5);
104: assertFalse(t1.equals(t2));
105: t2.setPercentComplete(0.5);
106: assertTrue(t1.equals(t2));
107:
108: t1.addSubtask(new Task("T", new Date(22), new Date(33)));
109: assertFalse(t1.equals(t2));
110: t2.addSubtask(new Task("T", new Date(22), new Date(33)));
111: assertTrue(t1.equals(t2));
112:
113: }
114:
115: /**
116: * Confirm that cloning works.
117: */
118: public void testCloning() {
119: Task t1 = new Task("T", new Date(1), new Date(2));
120: Task t2 = null;
121: try {
122: t2 = (Task) t1.clone();
123: } catch (CloneNotSupportedException e) {
124: System.err.println("Failed to clone.");
125: }
126: assertTrue(t1 != t2);
127: assertTrue(t1.getClass() == t2.getClass());
128: assertTrue(t1.equals(t2));
129: }
130:
131: /**
132: * Serialize an instance, restore it, and check for equality.
133: */
134: public void testSerialization() {
135:
136: Task t1 = new Task("T", new Date(1), new Date(2));
137: Task t2 = null;
138:
139: try {
140: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
141: ObjectOutput out = new ObjectOutputStream(buffer);
142: out.writeObject(t1);
143: out.close();
144:
145: ObjectInput in = new ObjectInputStream(
146: new ByteArrayInputStream(buffer.toByteArray()));
147: t2 = (Task) in.readObject();
148: in.close();
149: } catch (Exception e) {
150: System.out.println(e.toString());
151: }
152: assertEquals(t1, t2);
153:
154: }
155:
156: /**
157: * Check the getSubTaskCount() method.
158: */
159: public void testGetSubTaskCount() {
160: Task t1 = new Task("T", new Date(100), new Date(200));
161: assertEquals(0, t1.getSubtaskCount());
162: t1.addSubtask(new Task("S1", new Date(100), new Date(110)));
163: assertEquals(1, t1.getSubtaskCount());
164: Task s2 = new Task("S2", new Date(111), new Date(120));
165: t1.addSubtask(s2);
166: assertEquals(2, t1.getSubtaskCount());
167: t1.addSubtask(new Task("S3", new Date(121), new Date(130)));
168: assertEquals(3, t1.getSubtaskCount());
169: t1.removeSubtask(s2);
170: assertEquals(2, t1.getSubtaskCount());
171: }
172:
173: }
|