KickJava   Java API By Example, From Geeks To Geeks.

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


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  * StandardXYItemLabelGeneratorTests.java
29  * --------------------------------------
30  * (C) Copyright 2003-2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: StandardXYItemLabelGeneratorTests.java,v 1.1.2.1 2006/10/03 15:41:37 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 23-Mar-2003 : Version 1 (DG);
40  * 26-Feb-2004 : Updates for new code (DG);
41  * 20-Jan-2006 : Renamed StandardXYItemLabelGeneratorTests.java (DG);
42  *
43  */

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

67 public class StandardXYItemLabelGeneratorTests extends TestCase {
68
69     /**
70      * Returns the tests as a test suite.
71      *
72      * @return The test suite.
73      */

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

83     public StandardXYItemLabelGeneratorTests(String JavaDoc name) {
84         super(name);
85     }
86
87     /**
88      * A series of tests for the equals() method.
89      */

90     public void testEquals() {
91         
92         // some setup...
93
String JavaDoc f1 = "{1}";
94         String JavaDoc f2 = "{2}";
95         NumberFormat JavaDoc xnf1 = new DecimalFormat JavaDoc("0.00");
96         NumberFormat JavaDoc xnf2 = new DecimalFormat JavaDoc("0.000");
97         NumberFormat JavaDoc ynf1 = new DecimalFormat JavaDoc("0.00");
98         NumberFormat JavaDoc ynf2 = new DecimalFormat JavaDoc("0.000");
99
100         StandardXYItemLabelGenerator g1 = null;
101         StandardXYItemLabelGenerator g2 = null;
102         
103         g1 = new StandardXYItemLabelGenerator(f1, xnf1, ynf1);
104         g2 = new StandardXYItemLabelGenerator(f1, xnf1, ynf1);
105         assertTrue(g1.equals(g2));
106         assertTrue(g2.equals(g1));
107         
108         g1 = new StandardXYItemLabelGenerator(f2, xnf1, ynf1);
109         assertFalse(g1.equals(g2));
110         g2 = new StandardXYItemLabelGenerator(f2, xnf1, ynf1);
111         assertTrue(g1.equals(g2));
112                 
113         g1 = new StandardXYItemLabelGenerator(f2, xnf2, ynf1);
114         assertFalse(g1.equals(g2));
115         g2 = new StandardXYItemLabelGenerator(f2, xnf2, ynf1);
116         assertTrue(g1.equals(g2));
117
118         g1 = new StandardXYItemLabelGenerator(f2, xnf2, ynf2);
119         assertFalse(g1.equals(g2));
120         g2 = new StandardXYItemLabelGenerator(f2, xnf2, ynf2);
121         assertTrue(g1.equals(g2));
122                 
123         DateFormat JavaDoc xdf1 = new SimpleDateFormat JavaDoc("d-MMM");
124         DateFormat JavaDoc xdf2 = new SimpleDateFormat JavaDoc("d-MMM-yyyy");
125         DateFormat JavaDoc ydf1 = new SimpleDateFormat JavaDoc("d-MMM");
126         DateFormat JavaDoc ydf2 = new SimpleDateFormat JavaDoc("d-MMM-yyyy");
127
128         g1 = new StandardXYItemLabelGenerator(f1, xdf1, ydf1);
129         g2 = new StandardXYItemLabelGenerator(f1, xdf1, ydf1);
130         assertTrue(g1.equals(g2));
131         assertTrue(g2.equals(g1));
132         
133         g1 = new StandardXYItemLabelGenerator(f1, xdf2, ydf1);
134         assertFalse(g1.equals(g2));
135         g2 = new StandardXYItemLabelGenerator(f1, xdf2, ydf1);
136         assertTrue(g1.equals(g2));
137                 
138         g1 = new StandardXYItemLabelGenerator(f1, xdf2, ydf2);
139         assertFalse(g1.equals(g2));
140         g2 = new StandardXYItemLabelGenerator(f1, xdf2, ydf2);
141         assertTrue(g1.equals(g2));
142
143     }
144     
145     /**
146      * Confirm that cloning works.
147      */

148     public void testCloning() {
149         StandardXYItemLabelGenerator g1 = new StandardXYItemLabelGenerator();
150         StandardXYItemLabelGenerator g2 = null;
151         try {
152             g2 = (StandardXYItemLabelGenerator) g1.clone();
153         }
154         catch (CloneNotSupportedException JavaDoc e) {
155             System.err.println("Clone failed.");
156         }
157         assertTrue(g1 != g2);
158         assertTrue(g1.getClass() == g2.getClass());
159         assertTrue(g1.equals(g2));
160     }
161
162     /**
163      * Serialize an instance, restore it, and check for equality.
164      */

165     public void testSerialization() {
166
167         StandardXYItemLabelGenerator g1 = new StandardXYItemLabelGenerator();
168         StandardXYItemLabelGenerator g2 = null;
169
170         try {
171             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
172             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
173             out.writeObject(g1);
174             out.close();
175
176             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
177                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
178             g2 = (StandardXYItemLabelGenerator) in.readObject();
179             in.close();
180         }
181         catch (Exception JavaDoc e) {
182             System.out.println(e.toString());
183         }
184         assertEquals(g1, g2);
185
186     }
187
188 }
189
Popular Tags