KickJava   Java API By Example, From Geeks To Geeks.

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


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  * StandardCategoryLabelGeneratorTests.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: StandardCategoryItemLabelGeneratorTests.java,v 1.1 2005/05/17 12:24:44 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 21-Mar-2003 : Version 1 (DG);
39  * 13-Aug-2003 : Added cloning tests (DG);
40  * 11-May-2004 : 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.StandardCategoryItemLabelGenerator;
61 import org.jfree.data.category.DefaultCategoryDataset;
62
63 /**
64  * Tests for the {@link StandardCategoryItemLabelGenerator} class.
65  */

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

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

82     public StandardCategoryItemLabelGeneratorTests(String JavaDoc name) {
83         super(name);
84     }
85     
86     public void testGenerateLabel() {
87         StandardCategoryItemLabelGenerator g
88             = new StandardCategoryItemLabelGenerator("{2}", new DecimalFormat JavaDoc("0.000"));
89         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
90         dataset.addValue(1.0, "R0", "C0");
91         dataset.addValue(2.0, "R0", "C1");
92         dataset.addValue(3.0, "R1", "C0");
93         dataset.addValue(null, "R1", "C1");
94         String JavaDoc s = g.generateLabel(dataset, 0, 0);
95         assertEquals("1.000", s);
96         
97         // try a null value
98
s = g.generateLabel(dataset, 1, 1);
99         assertEquals("-", s);
100     }
101     
102     /**
103      * Some checks for the equals() method.
104      */

105     public void testEquals() {
106         
107         StandardCategoryItemLabelGenerator g1
108             = new StandardCategoryItemLabelGenerator();
109         StandardCategoryItemLabelGenerator g2
110             = new StandardCategoryItemLabelGenerator();
111         assertTrue(g1.equals(g2));
112         assertTrue(g2.equals(g1));
113         
114         g1 = new StandardCategoryItemLabelGenerator(
115             "{0}", new DecimalFormat JavaDoc("0.000")
116         );
117         assertFalse(g1.equals(g2));
118         g2 = new StandardCategoryItemLabelGenerator(
119             "{0}", new DecimalFormat JavaDoc("0.000")
120         );
121         assertTrue(g1.equals(g2));
122
123         g1 = new StandardCategoryItemLabelGenerator(
124             "{1}", new DecimalFormat JavaDoc("0.000")
125         );
126         assertFalse(g1.equals(g2));
127         g2 = new StandardCategoryItemLabelGenerator(
128             "{1}", new DecimalFormat JavaDoc("0.000")
129         );
130         assertTrue(g1.equals(g2));
131
132         g1 = new StandardCategoryItemLabelGenerator(
133             "{2}", new SimpleDateFormat JavaDoc("d-MMM")
134         );
135         assertFalse(g1.equals(g2));
136         g2 = new StandardCategoryItemLabelGenerator(
137             "{2}", new SimpleDateFormat JavaDoc("d-MMM")
138         );
139         assertTrue(g1.equals(g2));
140         
141     }
142
143     /**
144      * Confirm that cloning works.
145      */

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

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