KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > renderer > category > junit > BarRendererTests


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  * BarRendererTests.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: BarRendererTests.java,v 1.1.2.1 2006/10/03 15:41:34 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 25-Mar-2003 : Version 1 (DG);
40  * 19-Aug-2003 : Renamed HorizontalBarRendererTests --> BarRendererTests (DG);
41  * 22-Oct-2003 : Added hashCode test (DG);
42  * 18-May-2005 : Added field to equals() test (DG);
43  * 22-Sep-2005 : Renamed getMaxBarWidth() --> getMaximumBarWidth() (DG);
44  *
45  */

46
47 package org.jfree.chart.renderer.category.junit;
48
49 import java.awt.Color JavaDoc;
50 import java.io.ByteArrayInputStream JavaDoc;
51 import java.io.ByteArrayOutputStream JavaDoc;
52 import java.io.ObjectInput JavaDoc;
53 import java.io.ObjectInputStream JavaDoc;
54 import java.io.ObjectOutput JavaDoc;
55 import java.io.ObjectOutputStream JavaDoc;
56
57 import junit.framework.Test;
58 import junit.framework.TestCase;
59 import junit.framework.TestSuite;
60
61 import org.jfree.chart.labels.ItemLabelAnchor;
62 import org.jfree.chart.labels.ItemLabelPosition;
63 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
64 import org.jfree.chart.renderer.category.BarRenderer;
65 import org.jfree.chart.renderer.junit.RendererChangeDetector;
66 import org.jfree.ui.GradientPaintTransformType;
67 import org.jfree.ui.StandardGradientPaintTransformer;
68 import org.jfree.ui.TextAnchor;
69
70 /**
71  * Tests for the {@link BarRenderer} class.
72  */

73 public class BarRendererTests extends TestCase {
74
75     /**
76      * Returns the tests as a test suite.
77      *
78      * @return The test suite.
79      */

80     public static Test suite() {
81         return new TestSuite(BarRendererTests.class);
82     }
83
84     /**
85      * Constructs a new set of tests.
86      *
87      * @param name the name of the tests.
88      */

89     public BarRendererTests(String JavaDoc name) {
90         super(name);
91     }
92
93     /**
94      * Test that the equals() method distinguishes all fields.
95      */

96     public void testEquals() {
97         BarRenderer r1 = new BarRenderer();
98         BarRenderer r2 = new BarRenderer();
99         assertTrue(r1.equals(r2));
100         assertTrue(r2.equals(r1));
101         
102         // base value
103
r1.setBase(0.123);
104         assertFalse(r1.equals(r2));
105         r2.setBase(0.123);
106         assertTrue(r1.equals(r2));
107         
108         // itemMargin
109
r1.setItemMargin(0.22);
110         assertFalse(r1.equals(r2));
111         r2.setItemMargin(0.22);
112         assertTrue(r1.equals(r2));
113         
114         // drawBarOutline
115
r1.setDrawBarOutline(!r1.isDrawBarOutline());
116         assertFalse(r1.equals(r2));
117         r2.setDrawBarOutline(!r2.isDrawBarOutline());
118         assertTrue(r1.equals(r2));
119         
120         // maximumBarWidth
121
r1.setMaximumBarWidth(0.11);
122         assertFalse(r1.equals(r2));
123         r2.setMaximumBarWidth(0.11);
124         assertTrue(r1.equals(r2));
125         
126         // minimumBarLength
127
r1.setMinimumBarLength(0.04);
128         assertFalse(r1.equals(r2));
129         r2.setMinimumBarLength(0.04);
130         assertTrue(r1.equals(r2));
131         
132         // gradientPaintTransformer
133
r1.setGradientPaintTransformer(
134             new StandardGradientPaintTransformer(
135                 GradientPaintTransformType.CENTER_VERTICAL
136             )
137         );
138         assertFalse(r1.equals(r2));
139         r2.setGradientPaintTransformer(
140             new StandardGradientPaintTransformer(
141                 GradientPaintTransformType.CENTER_VERTICAL
142             )
143         );
144         assertTrue(r1.equals(r2));
145         
146         // positiveItemLabelPositionFallback
147
r1.setPositiveItemLabelPositionFallback(
148             new ItemLabelPosition(ItemLabelAnchor.INSIDE1, TextAnchor.CENTER)
149         );
150         assertFalse(r1.equals(r2));
151         r2.setPositiveItemLabelPositionFallback(
152             new ItemLabelPosition(ItemLabelAnchor.INSIDE1, TextAnchor.CENTER)
153         );
154         assertTrue(r1.equals(r2));
155
156         // negativeItemLabelPositionFallback
157
r1.setNegativeItemLabelPositionFallback(
158             new ItemLabelPosition(ItemLabelAnchor.INSIDE1, TextAnchor.CENTER)
159         );
160         assertFalse(r1.equals(r2));
161         r2.setNegativeItemLabelPositionFallback(
162             new ItemLabelPosition(ItemLabelAnchor.INSIDE1, TextAnchor.CENTER)
163         );
164         assertTrue(r1.equals(r2));
165
166     }
167
168     /**
169      * Two objects that are equal are required to return the same hashCode.
170      */

171     public void testHashcode() {
172         BarRenderer r1 = new BarRenderer();
173         BarRenderer r2 = new BarRenderer();
174         assertTrue(r1.equals(r2));
175         int h1 = r1.hashCode();
176         int h2 = r2.hashCode();
177         assertEquals(h1, h2);
178     }
179     
180     /**
181      * Confirm that cloning works.
182      */

183     public void testCloning() {
184         BarRenderer r1 = new BarRenderer();
185         r1.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
186         BarRenderer r2 = null;
187         try {
188             r2 = (BarRenderer) r1.clone();
189         }
190         catch (CloneNotSupportedException JavaDoc e) {
191             System.err.println("Failed to clone.");
192         }
193         assertTrue(r1 != r2);
194         assertTrue(r1.getClass() == r2.getClass());
195         assertTrue(r1.equals(r2));
196     }
197
198     /**
199      * Serialize an instance, restore it, and check for equality.
200      */

201     public void testSerialization() {
202
203         BarRenderer r1 = new BarRenderer();
204         BarRenderer r2 = null;
205
206         try {
207             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
208             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
209             out.writeObject(r1);
210             out.close();
211
212             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
213                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
214             );
215             r2 = (BarRenderer) in.readObject();
216             in.close();
217         }
218         catch (Exception JavaDoc e) {
219             System.out.println(e.toString());
220         }
221         assertEquals(r1, r2);
222
223     }
224     
225     /**
226      * Tests each setter method to ensure that it sends an event notification.
227      */

228     public void testEventNotification() {
229         
230         RendererChangeDetector detector = new RendererChangeDetector();
231         BarRenderer r1 = new BarRenderer();
232         r1.addChangeListener(detector);
233         
234         detector.setNotified(false);
235         r1.setPaint(Color.red);
236         assertTrue(detector.getNotified());
237
238     }
239
240 }
241
Popular Tags