KickJava   Java API By Example, From Geeks To Geeks.

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


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  * StandardCategorySeriesLabelGeneratorTests.java
29  * ----------------------------------------------
30  * (C) Copyright 2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: StandardCategorySeriesLabelGeneratorTests.java,v 1.1.2.1 2006/10/03 15:41:36 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 03-May-2006 : Version 1 (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
52 import junit.framework.Test;
53 import junit.framework.TestCase;
54 import junit.framework.TestSuite;
55
56 import org.jfree.chart.labels.StandardCategorySeriesLabelGenerator;
57 import org.jfree.data.category.DefaultCategoryDataset;
58
59 /**
60  * Tests for the {@link StandardCategorySeriesLabelGenerator} class.
61  */

62 public class StandardCategorySeriesLabelGeneratorTests extends TestCase {
63
64     /**
65      * Returns the tests as a test suite.
66      *
67      * @return The test suite.
68      */

69     public static Test suite() {
70         return new TestSuite(StandardCategorySeriesLabelGeneratorTests.class);
71     }
72
73     /**
74      * Constructs a new set of tests.
75      *
76      * @param name the name of the tests.
77      */

78     public StandardCategorySeriesLabelGeneratorTests(String JavaDoc name) {
79         super(name);
80     }
81     
82     /**
83      * Some checks for the generalLabel() method.
84      */

85     public void testGenerateLabel() {
86         StandardCategorySeriesLabelGenerator g
87             = new StandardCategorySeriesLabelGenerator("{0}");
88         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
89         dataset.addValue(1.0, "R0", "C0");
90         dataset.addValue(2.0, "R0", "C1");
91         dataset.addValue(3.0, "R1", "C0");
92         dataset.addValue(null, "R1", "C1");
93         String JavaDoc s = g.generateLabel(dataset, 0);
94         assertEquals("R0", s);
95     }
96     
97     /**
98      * Some checks for the equals() method.
99      */

100     public void testEquals() {
101         StandardCategorySeriesLabelGenerator g1
102             = new StandardCategorySeriesLabelGenerator();
103         StandardCategorySeriesLabelGenerator g2
104             = new StandardCategorySeriesLabelGenerator();
105         assertTrue(g1.equals(g2));
106         assertTrue(g2.equals(g1));
107         
108         g1 = new StandardCategorySeriesLabelGenerator("{1}");
109         assertFalse(g1.equals(g2));
110         g2 = new StandardCategorySeriesLabelGenerator("{1}");
111         assertTrue(g1.equals(g2));
112     }
113
114     /**
115      * Confirm that cloning works.
116      */

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

135     public void testSerialization() {
136
137         StandardCategorySeriesLabelGenerator g1
138             = new StandardCategorySeriesLabelGenerator("{2}");
139         StandardCategorySeriesLabelGenerator g2 = null;
140
141         try {
142             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
143             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
144             out.writeObject(g1);
145             out.close();
146
147             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
148                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
149             g2 = (StandardCategorySeriesLabelGenerator) in.readObject();
150             in.close();
151         }
152         catch (Exception JavaDoc e) {
153             System.out.println(e.toString());
154         }
155         assertEquals(g1, g2);
156
157     }
158
159 }
160
Popular Tags