KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > junit > ComparableObjectItemTests


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, 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  * ComparableObjectItemTests.java
29  * ------------------------------
30  * (C) Copyright 2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: ComparableObjectItemTests.java,v 1.1.2.2 2006/10/21 05:11:23 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 20-Oct-2006 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.data.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
52 import junit.framework.Test;
53 import junit.framework.TestCase;
54 import junit.framework.TestSuite;
55 import org.jfree.data.ComparableObjectItem;
56
57 /**
58  * Tests for the {@link ComparableObjectItem} class.
59  */

60 public class ComparableObjectItemTests extends TestCase {
61
62     /**
63      * Returns the tests as a test suite.
64      *
65      * @return The test suite.
66      */

67     public static Test suite() {
68         return new TestSuite(ComparableObjectItemTests.class);
69     }
70
71     /**
72      * Constructs a new set of tests.
73      *
74      * @param name the name of the tests.
75      */

76     public ComparableObjectItemTests(String JavaDoc name) {
77         super(name);
78     }
79
80     /**
81      * Some checks for the constructor.
82      */

83     public void testConstructor() {
84         // check null argument 1
85
boolean pass = false;
86         try {
87             /* ComparableObjectItem item1 = */ new ComparableObjectItem(null,
88                     "XYZ");
89         }
90         catch (IllegalArgumentException JavaDoc e) {
91             pass = true;
92         }
93         assertTrue(pass);
94     }
95     
96     /**
97      * Confirm that the equals method can distinguish all the required fields.
98      */

99     public void testEquals() {
100         ComparableObjectItem item1 = new ComparableObjectItem(new Integer JavaDoc(1),
101                 "XYZ");
102         ComparableObjectItem item2 = new ComparableObjectItem(new Integer JavaDoc(1),
103                 "XYZ");
104         assertTrue(item1.equals(item2));
105         assertTrue(item2.equals(item1));
106
107         item1 = new ComparableObjectItem(new Integer JavaDoc(2), "XYZ");
108         assertFalse(item1.equals(item2));
109         item2 = new ComparableObjectItem(new Integer JavaDoc(2), "XYZ");
110         assertTrue(item1.equals(item2));
111         
112         item1 = new ComparableObjectItem(new Integer JavaDoc(2), null);
113         assertFalse(item1.equals(item2));
114         item2 = new ComparableObjectItem(new Integer JavaDoc(2), null);
115         assertTrue(item1.equals(item2));
116     }
117
118     /**
119      * Some checks for the clone() method.
120      */

121     public void testCloning() {
122         ComparableObjectItem item1 = new ComparableObjectItem(new Integer JavaDoc(1),
123                 "XYZ");
124         ComparableObjectItem item2 = null;
125         try {
126             item2 = (ComparableObjectItem) item1.clone();
127         }
128         catch (CloneNotSupportedException JavaDoc e) {
129             e.printStackTrace();
130         }
131         assertTrue(item1 != item2);
132         assertTrue(item1.getClass() == item2.getClass());
133         assertTrue(item1.equals(item2));
134     }
135
136     /**
137      * Serialize an instance, restore it, and check for equality.
138      */

139     public void testSerialization() {
140         ComparableObjectItem item1 = new ComparableObjectItem(new Integer JavaDoc(1),
141                 "XYZ");
142         ComparableObjectItem item2 = null;
143         try {
144             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
145             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
146             out.writeObject(item1);
147             out.close();
148
149             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
150                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
151             item2 = (ComparableObjectItem) in.readObject();
152             in.close();
153         }
154         catch (Exception JavaDoc e) {
155             e.printStackTrace();
156         }
157         assertEquals(item1, item2);
158     }
159     
160     /**
161      * Some checks for the compareTo() method.
162      */

163     public void testCompareTo() {
164         ComparableObjectItem item1 = new ComparableObjectItem(new Integer JavaDoc(1),
165                 "XYZ");
166         ComparableObjectItem item2 = new ComparableObjectItem(new Integer JavaDoc(2),
167                 "XYZ");
168         ComparableObjectItem item3 = new ComparableObjectItem(new Integer JavaDoc(3),
169                 "XYZ");
170         ComparableObjectItem item4 = new ComparableObjectItem(new Integer JavaDoc(1),
171                 "XYZ");
172         assertTrue(item2.compareTo(item1) > 0);
173         assertTrue(item3.compareTo(item1) > 0);
174         assertTrue(item4.compareTo(item1) == 0);
175         assertTrue(item1.compareTo(item2) < 0);
176     }
177
178 }
179
Popular Tags