KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > gantt > junit > TaskTests


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
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  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * --------------
28  * TaskTests.java
29  * --------------
30  * (C) Copyright 2004, 2005, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: TaskTests.java,v 1.1.2.1 2006/10/03 15:41:39 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 30-Jul-2004 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.data.gantt.junit;
44
45 import java.io.ByteArrayInputStream JavaDoc;
46 import java.io.ByteArrayOutputStream JavaDoc;
47 import java.io.ObjectInput JavaDoc;
48 import java.io.ObjectInputStream JavaDoc;
49 import java.io.ObjectOutput JavaDoc;
50 import java.io.ObjectOutputStream JavaDoc;
51 import java.util.Date JavaDoc;
52
53 import junit.framework.Test;
54 import junit.framework.TestCase;
55 import junit.framework.TestSuite;
56
57 import org.jfree.data.gantt.Task;
58 import org.jfree.data.time.SimpleTimePeriod;
59
60 /**
61  * Tests for the {@link Task} class.
62  */

63 public class TaskTests extends TestCase {
64
65     /**
66      * Returns the tests as a test suite.
67      *
68      * @return The test suite.
69      */

70     public static Test suite() {
71         return new TestSuite(TaskTests.class);
72     }
73
74     /**
75      * Constructs a new set of tests.
76      *
77      * @param name the name of the tests.
78      */

79     public TaskTests(String JavaDoc name) {
80         super(name);
81     }
82
83     /**
84      * Confirm that the equals method can distinguish all the required fields.
85      */

86     public void testEquals() {
87         
88         Task t1 = new Task("T", new Date JavaDoc(1), new Date JavaDoc(2));
89         Task t2 = new Task("T", new Date JavaDoc(1), new Date JavaDoc(2));
90         assertTrue(t1.equals(t2));
91         assertTrue(t2.equals(t1));
92
93         t1.setDescription("X");
94         assertFalse(t1.equals(t2));
95         t2.setDescription("X");
96         assertTrue(t1.equals(t2));
97         
98         t1.setDuration(new SimpleTimePeriod(new Date JavaDoc(2), new Date JavaDoc(3)));
99         assertFalse(t1.equals(t2));
100         t2.setDuration(new SimpleTimePeriod(new Date JavaDoc(2), new Date JavaDoc(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 JavaDoc(22), new Date JavaDoc(33)));
109         assertFalse(t1.equals(t2));
110         t2.addSubtask(new Task("T", new Date JavaDoc(22), new Date JavaDoc(33)));
111         assertTrue(t1.equals(t2));
112         
113
114     }
115
116     /**
117      * Confirm that cloning works.
118      */

119     public void testCloning() {
120         Task t1 = new Task("T", new Date JavaDoc(1), new Date JavaDoc(2));
121         Task t2 = null;
122         try {
123             t2 = (Task) t1.clone();
124         }
125         catch (CloneNotSupportedException JavaDoc e) {
126             System.err.println("Failed to clone.");
127         }
128         assertTrue(t1 != t2);
129         assertTrue(t1.getClass() == t2.getClass());
130         assertTrue(t1.equals(t2));
131     }
132
133     /**
134      * Serialize an instance, restore it, and check for equality.
135      */

136     public void testSerialization() {
137
138         Task t1 = new Task("T", new Date JavaDoc(1), new Date JavaDoc(2));
139         Task t2 = null;
140
141         try {
142             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
143             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
144             out.writeObject(t1);
145             out.close();
146
147             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
148                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
149             );
150             t2 = (Task) in.readObject();
151             in.close();
152         }
153         catch (Exception JavaDoc e) {
154             System.out.println(e.toString());
155         }
156         assertEquals(t1, t2);
157
158     }
159     
160     /**
161      * Check the getSubTaskCount() method.
162      */

163     public void testGetSubTaskCount() {
164         Task t1 = new Task("T", new Date JavaDoc(100), new Date JavaDoc(200));
165         assertEquals(0, t1.getSubtaskCount());
166         t1.addSubtask(new Task("S1", new Date JavaDoc(100), new Date JavaDoc(110)));
167         assertEquals(1, t1.getSubtaskCount());
168         Task s2 = new Task("S2", new Date JavaDoc(111), new Date JavaDoc(120));
169         t1.addSubtask(s2);
170         assertEquals(2, t1.getSubtaskCount());
171         t1.addSubtask(new Task("S3", new Date JavaDoc(121), new Date JavaDoc(130)));
172         assertEquals(3, t1.getSubtaskCount());
173         t1.removeSubtask(s2);
174         assertEquals(2, t1.getSubtaskCount());
175     }
176
177 }
178
Popular Tags