001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, 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: * ComparableObjectItemTests.java
029: * ------------------------------
030: * (C) Copyright 2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: ComparableObjectItemTests.java,v 1.1.2.3 2007/04/13 14:38:31 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 20-Oct-2006 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.data.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:
052: import junit.framework.Test;
053: import junit.framework.TestCase;
054: import junit.framework.TestSuite;
055:
056: import org.jfree.data.ComparableObjectItem;
057:
058: /**
059: * Tests for the {@link ComparableObjectItem} class.
060: */
061: public class ComparableObjectItemTests extends TestCase {
062:
063: /**
064: * Returns the tests as a test suite.
065: *
066: * @return The test suite.
067: */
068: public static Test suite() {
069: return new TestSuite(ComparableObjectItemTests.class);
070: }
071:
072: /**
073: * Constructs a new set of tests.
074: *
075: * @param name the name of the tests.
076: */
077: public ComparableObjectItemTests(String name) {
078: super (name);
079: }
080:
081: /**
082: * Some checks for the constructor.
083: */
084: public void testConstructor() {
085: // check null argument 1
086: boolean pass = false;
087: try {
088: /* ComparableObjectItem item1 = */new ComparableObjectItem(
089: null, "XYZ");
090: } catch (IllegalArgumentException e) {
091: pass = true;
092: }
093: assertTrue(pass);
094: }
095:
096: /**
097: * Confirm that the equals method can distinguish all the required fields.
098: */
099: public void testEquals() {
100: ComparableObjectItem item1 = new ComparableObjectItem(
101: new Integer(1), "XYZ");
102: ComparableObjectItem item2 = new ComparableObjectItem(
103: new Integer(1), "XYZ");
104: assertTrue(item1.equals(item2));
105: assertTrue(item2.equals(item1));
106:
107: item1 = new ComparableObjectItem(new Integer(2), "XYZ");
108: assertFalse(item1.equals(item2));
109: item2 = new ComparableObjectItem(new Integer(2), "XYZ");
110: assertTrue(item1.equals(item2));
111:
112: item1 = new ComparableObjectItem(new Integer(2), null);
113: assertFalse(item1.equals(item2));
114: item2 = new ComparableObjectItem(new Integer(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(
123: new Integer(1), "XYZ");
124: ComparableObjectItem item2 = null;
125: try {
126: item2 = (ComparableObjectItem) item1.clone();
127: } catch (CloneNotSupportedException e) {
128: e.printStackTrace();
129: }
130: assertTrue(item1 != item2);
131: assertTrue(item1.getClass() == item2.getClass());
132: assertTrue(item1.equals(item2));
133: }
134:
135: /**
136: * Serialize an instance, restore it, and check for equality.
137: */
138: public void testSerialization() {
139: ComparableObjectItem item1 = new ComparableObjectItem(
140: new Integer(1), "XYZ");
141: ComparableObjectItem item2 = null;
142: try {
143: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
144: ObjectOutput out = new ObjectOutputStream(buffer);
145: out.writeObject(item1);
146: out.close();
147:
148: ObjectInput in = new ObjectInputStream(
149: new ByteArrayInputStream(buffer.toByteArray()));
150: item2 = (ComparableObjectItem) in.readObject();
151: in.close();
152: } catch (Exception e) {
153: e.printStackTrace();
154: }
155: assertEquals(item1, item2);
156: }
157:
158: /**
159: * Some checks for the compareTo() method.
160: */
161: public void testCompareTo() {
162: ComparableObjectItem item1 = new ComparableObjectItem(
163: new Integer(1), "XYZ");
164: ComparableObjectItem item2 = new ComparableObjectItem(
165: new Integer(2), "XYZ");
166: ComparableObjectItem item3 = new ComparableObjectItem(
167: new Integer(3), "XYZ");
168: ComparableObjectItem item4 = new ComparableObjectItem(
169: new Integer(1), "XYZ");
170: assertTrue(item2.compareTo(item1) > 0);
171: assertTrue(item3.compareTo(item1) > 0);
172: assertTrue(item4.compareTo(item1) == 0);
173: assertTrue(item1.compareTo(item2) < 0);
174: }
175:
176: }
|