KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ------------------------------
23  * LineAndShapeRendererTests.java
24  * ------------------------------
25  * (C) Copyright 2003 by Object Refinery Limited and Contributors.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: LineAndShapeRendererTests.java,v 1.3 2003/10/28 16:46:05 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 22-Sep-2003 : Version 1 (DG);
35  *
36  */

37
38 package org.jfree.chart.renderer.junit;
39
40 import java.io.ByteArrayInputStream JavaDoc;
41 import java.io.ByteArrayOutputStream JavaDoc;
42 import java.io.ObjectInput JavaDoc;
43 import java.io.ObjectInputStream JavaDoc;
44 import java.io.ObjectOutput JavaDoc;
45 import java.io.ObjectOutputStream JavaDoc;
46
47 import junit.framework.Test;
48 import junit.framework.TestCase;
49 import junit.framework.TestSuite;
50
51 import org.jfree.chart.renderer.LineAndShapeRenderer;
52
53 /**
54  * Tests for the {@link LineAndShapeRenderer} class.
55  *
56  * @author David Gilbert
57  */

58 public class LineAndShapeRendererTests extends TestCase {
59
60     /**
61      * Returns the tests as a test suite.
62      *
63      * @return the test suite.
64      */

65     public static Test suite() {
66         return new TestSuite(LineAndShapeRendererTests.class);
67     }
68
69     /**
70      * Constructs a new set of tests.
71      *
72      * @param name the name of the tests.
73      */

74     public LineAndShapeRendererTests(String JavaDoc name) {
75         super(name);
76     }
77
78     /**
79      * Test that the equals(...) method distinguishes all fields.
80      */

81     public void testEquals() {
82         LineAndShapeRenderer r1 = new LineAndShapeRenderer();
83         LineAndShapeRenderer r2 = new LineAndShapeRenderer();
84         assertEquals(r1, r2);
85     }
86
87     /**
88      * Two objects that are equal are required to return the same hashCode.
89      */

90     public void testHashcode() {
91         LineAndShapeRenderer r1 = new LineAndShapeRenderer();
92         LineAndShapeRenderer r2 = new LineAndShapeRenderer();
93         assertTrue(r1.equals(r2));
94         int h1 = r1.hashCode();
95         int h2 = r2.hashCode();
96         assertEquals(h1, h2);
97     }
98     
99     /**
100      * Confirm that cloning works.
101      */

102     public void testCloning() {
103         LineAndShapeRenderer r1 = new LineAndShapeRenderer();
104         LineAndShapeRenderer r2 = null;
105         try {
106             r2 = (LineAndShapeRenderer) r1.clone();
107         }
108         catch (CloneNotSupportedException JavaDoc e) {
109             System.err.println("LineAndShapeRendererTests.testCloning: failed to clone.");
110         }
111         assertTrue(r1 != r2);
112         assertTrue(r1.getClass() == r2.getClass());
113         assertTrue(r1.equals(r2));
114         
115         assertTrue(checkIndependence(r1, r2));
116         
117     }
118
119     /**
120      * Checks that the two renderers are equal but independent of one another.
121      *
122      * @param r1 renderer 1.
123      * @param r2 renderer 2.
124      *
125      * @return A boolean.
126      */

127     private boolean checkIndependence(LineAndShapeRenderer r1, LineAndShapeRenderer r2) {
128
129         // should be equal...
130
boolean b0 = r1.equals(r2);
131         
132         // and independent...
133
r1.setDrawShapes(!r1.isDrawShapes());
134         boolean b1 = !r1.equals(r2);
135         r2.setDrawShapes(r1.isDrawShapes());
136         boolean b2 = r1.equals(r2);
137         
138         r1.setDrawLines(!r1.isDrawLines());
139         boolean b3 = !r1.equals(r2);
140         r2.setDrawLines(r1.isDrawLines());
141         boolean b4 = r1.equals(r2);
142                 
143         boolean flag = true;
144         Boolean JavaDoc existing = r1.getShapesFilled();
145         if (existing != null) {
146             flag = !existing.booleanValue();
147         }
148         r1.setShapesFilled(flag);
149         boolean b5 = !r1.equals(r2);
150         r2.setShapesFilled(flag);
151         boolean b6 = r1.equals(r2);
152
153         r1.setShapesFilled(false);
154         r2.setShapesFilled(false);
155         r1.setSeriesShapesFilled(0, false);
156         r2.setSeriesShapesFilled(0, true);
157         boolean b7 = !r1.equals(r2);
158         r2.setSeriesShapesFilled(0, false);
159         boolean b8 = (r1.equals(r2));
160         
161         r1.setDefaultShapesFilled(false);
162         r2.setDefaultShapesFilled(true);
163         boolean b9 = !r1.equals(r2);
164         r2.setDefaultShapesFilled(false);
165         boolean b10 = (r1.equals(r2));
166         
167         return b0 && b1 && b2 && b3 && b4 && b5 && b6 && b7 && b8 && b9 && b10;
168     
169     }
170     
171     /**
172      * Serialize an instance, restore it, and check for equality.
173      */

174     public void testSerialization() {
175
176         LineAndShapeRenderer r1 = new LineAndShapeRenderer();
177         LineAndShapeRenderer r2 = null;
178
179         try {
180             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
181             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
182             out.writeObject(r1);
183             out.close();
184
185             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
186             r2 = (LineAndShapeRenderer) in.readObject();
187             in.close();
188         }
189         catch (Exception JavaDoc e) {
190             System.out.println(e.toString());
191         }
192         assertEquals(r1, r2);
193
194     }
195
196 }
197
Popular Tags