KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > labels > junit > IntervalCategoryLabelGeneratorTests


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  * IntervalCategoryLabelGeneratorTests.java
29  * ----------------------------------------
30  * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: IntervalCategoryLabelGeneratorTests.java,v 1.1.2.1 2006/10/03 15:41:35 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 21-Mar-2003 : Version 1 (DG);
40  * 13-Aug-2003 : Added cloning tests, and renamed class (DG);
41  *
42  */

43
44 package org.jfree.chart.labels.junit;
45
46 import java.io.ByteArrayInputStream JavaDoc;
47 import java.io.ByteArrayOutputStream JavaDoc;
48 import java.io.ObjectInput JavaDoc;
49 import java.io.ObjectInputStream JavaDoc;
50 import java.io.ObjectOutput JavaDoc;
51 import java.io.ObjectOutputStream JavaDoc;
52 import java.text.DateFormat JavaDoc;
53 import java.text.DecimalFormat JavaDoc;
54 import java.text.SimpleDateFormat JavaDoc;
55
56 import junit.framework.Test;
57 import junit.framework.TestCase;
58 import junit.framework.TestSuite;
59
60 import org.jfree.chart.labels.IntervalCategoryItemLabelGenerator;
61
62 /**
63  * Tests for the {@link IntervalCategoryItemLabelGenerator} class.
64  */

65 public class IntervalCategoryLabelGeneratorTests extends TestCase {
66
67     /**
68      * Returns the tests as a test suite.
69      *
70      * @return The test suite.
71      */

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

81     public IntervalCategoryLabelGeneratorTests(String JavaDoc name) {
82         super(name);
83     }
84
85     /**
86      * Tests the equals() method.
87      */

88     public void testEquals() {
89         
90         IntervalCategoryItemLabelGenerator g1
91             = new IntervalCategoryItemLabelGenerator();
92         IntervalCategoryItemLabelGenerator g2
93             = new IntervalCategoryItemLabelGenerator();
94         assertTrue(g1.equals(g2));
95         assertTrue(g2.equals(g1));
96         
97         g1 = new IntervalCategoryItemLabelGenerator(
98             "{3} - {4}", new DecimalFormat JavaDoc("0.000")
99         );
100         assertFalse(g1.equals(g2));
101         g2 = new IntervalCategoryItemLabelGenerator(
102             "{3} - {4}", new DecimalFormat JavaDoc("0.000")
103         );
104         assertTrue(g1.equals(g2));
105         
106         g1 = new IntervalCategoryItemLabelGenerator(
107             "{3} - {4}", new SimpleDateFormat JavaDoc("d-MMM")
108         );
109         assertFalse(g1.equals(g2));
110         g2 = new IntervalCategoryItemLabelGenerator(
111             "{3} - {4}", new SimpleDateFormat JavaDoc("d-MMM")
112         );
113         assertTrue(g1.equals(g2));
114         
115     }
116
117     /**
118      * Confirm that cloning works.
119      */

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

138     public void testSerialization() {
139
140         IntervalCategoryItemLabelGenerator g1
141             = new IntervalCategoryItemLabelGenerator(
142                 "{3} - {4}", DateFormat.getInstance()
143             );
144         IntervalCategoryItemLabelGenerator g2 = null;
145
146         try {
147             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
148             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
149             out.writeObject(g1);
150             out.close();
151
152             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
153                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
154             );
155             g2 = (IntervalCategoryItemLabelGenerator) in.readObject();
156             in.close();
157         }
158         catch (Exception JavaDoc e) {
159             System.out.println(e.toString());
160         }
161         assertEquals(g1, g2);
162
163     }
164
165 }
166
Popular Tags