KickJava   Java API By Example, From Geeks To Geeks.

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


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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * ----------------------------------
27  * StandardXYLabelGeneratorTests.java
28  * ----------------------------------
29  * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: StandardXYLabelGeneratorTests.java,v 1.4 2005/04/20 22:19:12 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 23-Mar-2003 : Version 1 (DG);
39  * 26-Feb-2004 : Updates for new code (DG);
40  *
41  */

42
43 package org.jfree.chart.labels.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.text.DateFormat JavaDoc;
52 import java.text.DecimalFormat JavaDoc;
53 import java.text.NumberFormat 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.StandardXYItemLabelGenerator;
61
62 /**
63  * Tests for the {@link StandardXYItemLabelGenerator} class.
64  */

65 public class StandardXYLabelGeneratorTests 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(StandardXYLabelGeneratorTests.class);
74     }
75
76     /**
77      * Constructs a new set of tests.
78      *
79      * @param name the name of the tests.
80      */

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

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

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

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