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 * LevelRendererTests.java
28 * -----------------------
29 * (C) Copyright 2005, by Object Refinery Limited and Contributors.
30 *
31 * Original Author: David Gilbert (for Object Refinery Limited);
32 * Contributor(s): -;
33 *
34 * $Id: LevelRendererTests.java,v 1.1 2005/03/29 12:57:13 mungady Exp $
35 *
36 * Changes
37 * -------
38 * 29-Mar-2005 : Version 1 (DG);
39 *
40 */
41
42 package org.jfree.chart.renderer.category.junit;
43
44 import java.io.ByteArrayInputStream;
45 import java.io.ByteArrayOutputStream;
46 import java.io.ObjectInput;
47 import java.io.ObjectInputStream;
48 import java.io.ObjectOutput;
49 import java.io.ObjectOutputStream;
50
51 import junit.framework.Test;
52 import junit.framework.TestCase;
53 import junit.framework.TestSuite;
54
55 import org.jfree.chart.renderer.category.LevelRenderer;
56
57 /**
58 * Tests for the {@link LevelRenderer} class.
59 */
60 public class LevelRendererTests extends TestCase {
61
62 /**
63 * Returns the tests as a test suite.
64 *
65 * @return The test suite.
66 */
67 public static Test suite() {
68 return new TestSuite(LevelRendererTests.class);
69 }
70
71 /**
72 * Constructs a new set of tests.
73 *
74 * @param name the name of the tests.
75 */
76 public LevelRendererTests(String name) {
77 super(name);
78 }
79
80 /**
81 * Test that the equals() method distinguishes all fields.
82 */
83 public void testEquals() {
84 LevelRenderer r1 = new LevelRenderer();
85 LevelRenderer r2 = new LevelRenderer();
86 assertTrue(r1.equals(r2));
87 assertTrue(r2.equals(r1));
88
89 r1.setItemMargin(0.123);
90 assertFalse(r1.equals(r2));
91 r2.setItemMargin(0.123);
92 assertTrue(r1.equals(r2));
93
94 r1.setMaxItemWidth(0.234);
95 assertFalse(r1.equals(r2));
96 r2.setMaxItemWidth(0.234);
97 assertTrue(r1.equals(r2));
98
99 }
100
101 /**
102 * Two objects that are equal are required to return the same hashCode.
103 */
104 public void testHashcode() {
105 LevelRenderer r1 = new LevelRenderer();
106 LevelRenderer r2 = new LevelRenderer();
107 assertTrue(r1.equals(r2));
108 int h1 = r1.hashCode();
109 int h2 = r2.hashCode();
110 assertEquals(h1, h2);
111 }
112
113 /**
114 * Confirm that cloning works.
115 */
116 public void testCloning() {
117 LevelRenderer r1 = new LevelRenderer();
118 r1.setItemMargin(0.123);
119 r1.setMaxItemWidth(0.234);
120 LevelRenderer r2 = null;
121 try {
122 r2 = (LevelRenderer) r1.clone();
123 }
124 catch (CloneNotSupportedException e) {
125 System.err.println("Failed to clone.");
126 }
127 assertTrue(r1 != r2);
128 assertTrue(r1.getClass() == r2.getClass());
129 assertTrue(r1.equals(r2));
130
131 assertTrue(checkIndependence(r1, r2));
132
133 }
134
135 /**
136 * Checks that the two renderers are equal but independent of one another.
137 *
138 * @param r1 renderer 1.
139 * @param r2 renderer 2.
140 *
141 * @return A boolean.
142 */
143 private boolean checkIndependence(LevelRenderer r1, LevelRenderer r2) {
144
145 // should be equal...
146 boolean b0 = r1.equals(r2);
147
148 // and independent...
149 r1.setItemMargin(0.0);
150 boolean b1 = !r1.equals(r2);
151 r2.setItemMargin(0.0);
152 boolean b2 = r1.equals(r2);
153
154 return b0 && b1 && b2;
155
156 }
157
158 /**
159 * Serialize an instance, restore it, and check for equality.
160 */
161 public void testSerialization() {
162
163 LevelRenderer r1 = new LevelRenderer();
164 LevelRenderer r2 = null;
165
166 try {
167 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
168 ObjectOutput out = new ObjectOutputStream(buffer);
169 out.writeObject(r1);
170 out.close();
171
172 ObjectInput in = new ObjectInputStream(
173 new ByteArrayInputStream(buffer.toByteArray())
174 );
175 r2 = (LevelRenderer) in.readObject();
176 in.close();
177 }
178 catch (Exception e) {
179 e.printStackTrace();
180 }
181 assertEquals(r1, r2);
182
183 }
184
185 }