KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > renderer > junit > AbstractRendererTests


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  * AbstractRendererTests.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: AbstractRendererTests.java,v 1.3 2005/03/16 16:18:43 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 23-Oct-2003 : Version 1 (DG);
39  * 01-Mar-2004 : Added serialization test (DG);
40  *
41  */

42
43 package org.jfree.chart.renderer.junit;
44
45 import java.awt.BasicStroke JavaDoc;
46 import java.awt.Color JavaDoc;
47 import java.awt.Font JavaDoc;
48 import java.awt.Shape JavaDoc;
49 import java.awt.Stroke JavaDoc;
50 import java.awt.geom.Rectangle2D JavaDoc;
51 import java.io.ByteArrayInputStream JavaDoc;
52 import java.io.ByteArrayOutputStream JavaDoc;
53 import java.io.ObjectInput JavaDoc;
54 import java.io.ObjectInputStream JavaDoc;
55 import java.io.ObjectOutput JavaDoc;
56 import java.io.ObjectOutputStream JavaDoc;
57
58 import junit.framework.Test;
59 import junit.framework.TestCase;
60 import junit.framework.TestSuite;
61
62 import org.jfree.chart.event.RendererChangeEvent;
63 import org.jfree.chart.labels.ItemLabelAnchor;
64 import org.jfree.chart.labels.ItemLabelPosition;
65 import org.jfree.chart.renderer.AbstractRenderer;
66 import org.jfree.chart.renderer.category.BarRenderer;
67 import org.jfree.ui.TextAnchor;
68
69 /**
70  * Tests for the {@link AbstractRenderer} class.
71  */

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

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

88     public AbstractRendererTests(String JavaDoc name) {
89         super(name);
90     }
91     
92     /**
93      * Test that setting the series visibility for ALL series does in fact work.
94      */

95     public void testSetSeriesVisible() {
96         BarRenderer r = new BarRenderer();
97         r.setSeriesVisible(Boolean.TRUE);
98         assertTrue(r.getItemVisible(0, 0));
99     }
100
101     /**
102      * Test that setting the paint for ALL series does in fact work.
103      */

104     public void testSetPaint() {
105         BarRenderer r = new BarRenderer();
106         r.setPaint(Color.orange);
107         assertEquals(Color.orange, r.getItemPaint(0, 0));
108     }
109
110     /**
111      * Test that setting the outline paint for ALL series does in fact work.
112      */

113     public void testSetOutlinePaint() {
114         BarRenderer r = new BarRenderer();
115         r.setOutlinePaint(Color.orange);
116         assertEquals(Color.orange, r.getItemOutlinePaint(0, 0));
117     }
118
119     /**
120      * Test that setting the stroke for ALL series does in fact work.
121      */

122     public void testSetStroke() {
123         BarRenderer r = new BarRenderer();
124         Stroke JavaDoc s = new BasicStroke JavaDoc(10.0f);
125         r.setStroke(s);
126         assertEquals(s, r.getItemStroke(0, 0));
127     }
128
129     /**
130      * Test that setting the outline stroke for ALL series does in fact work.
131      */

132     public void testSetOutlineStroke() {
133         BarRenderer r = new BarRenderer();
134         Stroke JavaDoc s = new BasicStroke JavaDoc(10.0f);
135         r.setOutlineStroke(s);
136         assertEquals(s, r.getItemOutlineStroke(0, 0));
137     }
138
139     /**
140      * Test that setting the shape for ALL series does in fact work.
141      */

142     public void testSetShape() {
143         BarRenderer r = new BarRenderer();
144         Shape JavaDoc s = new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0);
145         r.setShape(s);
146         assertEquals(s, r.getItemShape(0, 0));
147     }
148
149     /**
150      * Test that setting the item label visibility for ALL series does in fact
151      * work.
152      */

153     public void testSetItemLabelsVisible() {
154         BarRenderer r = new BarRenderer();
155         r.setItemLabelsVisible(true);
156         assertTrue(r.isItemLabelVisible(0, 0));
157     }
158
159     /**
160      * Test that setting the font for ALL series does in fact work (it was
161      * broken at one point).
162      */

163     public void testSetItemLabelFont() {
164         BarRenderer r = new BarRenderer();
165         r.setItemLabelFont(new Font JavaDoc("SansSerif", Font.PLAIN, 33));
166         assertEquals(
167             new Font JavaDoc("SansSerif", Font.PLAIN, 33), r.getItemLabelFont(0, 0)
168         );
169     }
170
171     /**
172      * Test that setting the paint for ALL series does in fact work (it was
173      * broken at one point).
174      */

175     public void testSetItemLabelPaint() {
176         BarRenderer r = new BarRenderer();
177         r.setItemLabelPaint(Color.green);
178         assertEquals(Color.green, r.getItemLabelPaint(0, 0));
179     }
180
181     /**
182      * Test that setting the positive item label position for ALL series does
183      * in fact work.
184      */

185     public void testSetPositiveItemLabelPosition() {
186         BarRenderer r = new BarRenderer();
187         r.setPositiveItemLabelPosition(
188             new ItemLabelPosition(
189                 ItemLabelAnchor.INSIDE1, TextAnchor.BASELINE_LEFT
190             )
191         );
192         assertEquals(
193             new ItemLabelPosition(
194                 ItemLabelAnchor.INSIDE1, TextAnchor.BASELINE_LEFT
195             ),
196             r.getPositiveItemLabelPosition(0, 0)
197         );
198     }
199
200     /**
201      * Test that setting the negative item label position for ALL series does
202      * in fact work.
203      */

204     public void testSetNegativeItemLabelPosition() {
205         BarRenderer r = new BarRenderer();
206         r.setNegativeItemLabelPosition(
207             new ItemLabelPosition(
208                 ItemLabelAnchor.INSIDE1, TextAnchor.BASELINE_LEFT
209             )
210         );
211         assertEquals(
212             new ItemLabelPosition(
213                 ItemLabelAnchor.INSIDE1, TextAnchor.BASELINE_LEFT
214             ),
215             r.getNegativeItemLabelPosition(0, 0)
216         );
217     }
218
219     /**
220      * Tests each setter method to ensure that it sends an event notification.
221      */

222     public void testEventNotification() {
223         
224         RendererChangeDetector detector = new RendererChangeDetector();
225         BarRenderer r1 = new BarRenderer(); // have to use a subclass of
226
// AbstractRenderer
227
r1.addChangeListener(detector);
228         
229         // PAINT
230
detector.setNotified(false);
231         r1.setPaint(Color.red);
232         assertTrue(detector.getNotified());
233
234         detector.setNotified(false);
235         r1.setSeriesPaint(0, Color.red);
236         assertTrue(detector.getNotified());
237
238         detector.setNotified(false);
239         r1.setBasePaint(Color.red);
240         assertTrue(detector.getNotified());
241
242         // OUTLINE PAINT
243
detector.setNotified(false);
244         r1.setOutlinePaint(Color.red);
245         assertTrue(detector.getNotified());
246
247         detector.setNotified(false);
248         r1.setSeriesOutlinePaint(0, Color.red);
249         assertTrue(detector.getNotified());
250
251         detector.setNotified(false);
252         r1.setBaseOutlinePaint(Color.red);
253         assertTrue(detector.getNotified());
254         
255         // STROKE
256
detector.setNotified(false);
257         r1.setStroke(new BasicStroke JavaDoc(1.0f));
258         assertTrue(detector.getNotified());
259
260         detector.setNotified(false);
261         r1.setSeriesStroke(0, new BasicStroke JavaDoc(1.0f));
262         assertTrue(detector.getNotified());
263
264         detector.setNotified(false);
265         r1.setBaseStroke(new BasicStroke JavaDoc(1.0f));
266         assertTrue(detector.getNotified());
267
268         // OUTLINE STROKE
269
detector.setNotified(false);
270         r1.setOutlineStroke(new BasicStroke JavaDoc(1.0f));
271         assertTrue(detector.getNotified());
272
273         detector.setNotified(false);
274         r1.setSeriesOutlineStroke(0, new BasicStroke JavaDoc(1.0f));
275         assertTrue(detector.getNotified());
276
277         detector.setNotified(false);
278         r1.setBaseOutlineStroke(new BasicStroke JavaDoc(1.0f));
279         assertTrue(detector.getNotified());
280
281         // SHAPE
282
detector.setNotified(false);
283         r1.setShape(new Rectangle2D.Float JavaDoc());
284         assertTrue(detector.getNotified());
285
286         detector.setNotified(false);
287         r1.setSeriesShape(0, new Rectangle2D.Float JavaDoc());
288         assertTrue(detector.getNotified());
289
290         detector.setNotified(false);
291         r1.setBaseShape(new Rectangle2D.Float JavaDoc());
292         assertTrue(detector.getNotified());
293
294         // ITEM_LABELS_VISIBLE
295
detector.setNotified(false);
296         r1.setItemLabelsVisible(Boolean.TRUE);
297         assertTrue(detector.getNotified());
298
299         detector.setNotified(false);
300         r1.setSeriesItemLabelsVisible(0, Boolean.TRUE);
301         assertTrue(detector.getNotified());
302
303         detector.setNotified(false);
304         r1.setBaseItemLabelsVisible(Boolean.TRUE);
305         assertTrue(detector.getNotified());
306         
307         // ITEM_LABEL_FONT
308
detector.setNotified(false);
309         r1.setItemLabelFont(new Font JavaDoc("Serif", Font.PLAIN, 12));
310         assertTrue(detector.getNotified());
311
312         detector.setNotified(false);
313         r1.setSeriesItemLabelFont(0, new Font JavaDoc("Serif", Font.PLAIN, 12));
314         assertTrue(detector.getNotified());
315
316         detector.setNotified(false);
317         r1.setBaseItemLabelFont(new Font JavaDoc("Serif", Font.PLAIN, 12));
318         assertTrue(detector.getNotified());
319         
320         // ITEM_LABEL_PAINT
321
detector.setNotified(false);
322         r1.setItemLabelPaint(Color.blue);
323         assertTrue(detector.getNotified());
324
325         detector.setNotified(false);
326         r1.setSeriesItemLabelPaint(0, Color.blue);
327         assertTrue(detector.getNotified());
328
329         detector.setNotified(false);
330         r1.setBaseItemLabelPaint(Color.blue);
331         assertTrue(detector.getNotified());
332         
333         // POSITIVE ITEM LABEL POSITION
334
detector.setNotified(false);
335         r1.setPositiveItemLabelPosition(
336             new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER)
337         );
338         assertTrue(detector.getNotified());
339
340         detector.setNotified(false);
341         r1.setSeriesPositiveItemLabelPosition(
342             0, new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER)
343         );
344         assertTrue(detector.getNotified());
345
346         detector.setNotified(false);
347         r1.setBasePositiveItemLabelPosition(
348             new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER)
349         );
350         assertTrue(detector.getNotified());
351
352         // NEGATIVE ITEM LABEL ANCHOR
353
detector.setNotified(false);
354         r1.setNegativeItemLabelPosition(
355             new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER)
356         );
357         assertTrue(detector.getNotified());
358
359         detector.setNotified(false);
360         r1.setSeriesNegativeItemLabelPosition(
361             0, new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER)
362         );
363         assertTrue(detector.getNotified());
364
365         detector.setNotified(false);
366         r1.setBaseNegativeItemLabelPosition(
367             new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER)
368         );
369         assertTrue(detector.getNotified());
370
371     }
372
373     /**
374      * Serialize an instance, restore it, and check for equality. In addition,
375      * test for a bug that was reported where the listener list is 'null' after
376      * deserialization.
377      */

378     public void testSerialization() {
379
380         BarRenderer r1 = new BarRenderer();
381         BarRenderer r2 = null;
382
383         try {
384             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
385             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
386             out.writeObject(r1);
387             out.close();
388
389             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
390                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
391             );
392             r2 = (BarRenderer) in.readObject();
393             in.close();
394         }
395         catch (Exception JavaDoc e) {
396             System.out.println(e.toString());
397         }
398         assertEquals(r1, r2);
399         try {
400             r2.notifyListeners(new RendererChangeEvent(r2));
401         }
402         catch (NullPointerException JavaDoc e) {
403             assertTrue(false); // failed
404
}
405
406     }
407
408 }
409
Popular Tags