KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > ui > junit > RectangleInsetsTests


1 /* ========================================================================
2  * JCommon : a free general purpose class 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/jcommon/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  * RectangleInsetsTests.java
29  * -------------------------
30  * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: RectangleInsetsTests.java,v 1.6 2005/10/18 13:22:36 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 14-Jun-2004 : Version 1 (DG);
40  * 03-May-2005 : Fixed tests for changes to RectangleInsets constructor (DG);
41  *
42  */

43
44 package org.jfree.ui.junit;
45
46 import java.awt.geom.Rectangle2D JavaDoc;
47 import java.io.ByteArrayInputStream JavaDoc;
48 import java.io.ByteArrayOutputStream JavaDoc;
49 import java.io.ObjectInput JavaDoc;
50 import java.io.ObjectInputStream JavaDoc;
51 import java.io.ObjectOutput JavaDoc;
52 import java.io.ObjectOutputStream JavaDoc;
53
54 import junit.framework.Test;
55 import junit.framework.TestCase;
56 import junit.framework.TestSuite;
57
58 import org.jfree.ui.RectangleInsets;
59 import org.jfree.ui.LengthAdjustmentType;
60 import org.jfree.util.UnitType;
61
62 /**
63  * Tests for the {@link RectangleInsets} class.
64  */

65 public class RectangleInsetsTests extends TestCase {
66
67     /**
68      * Returns the tests as a test suite.
69      *
70      * @return The test suite.
71      */

72     public static Test suite() {
73         return new TestSuite(RectangleInsetsTests.class);
74     }
75
76     /**
77      * Constructs a new set of tests.
78      *
79      * @param name the name of the tests.
80      */

81     public RectangleInsetsTests(final String JavaDoc name) {
82         super(name);
83     }
84     
85     /**
86      * Some checks for the createAdjustedRectangle() method.
87      */

88     public void testCreateAdjustedRectangleAbsolute() {
89         
90         RectangleInsets i1 = new RectangleInsets(
91             UnitType.ABSOLUTE, 1.0, 2.0, 3.0, 4.0
92         );
93         Rectangle2D JavaDoc base = new Rectangle2D.Double JavaDoc(10.0, 20.0, 30.0, 40.0);
94         
95         // no adjustment
96
Rectangle2D JavaDoc adjusted = i1.createAdjustedRectangle(
97             base, LengthAdjustmentType.NO_CHANGE, LengthAdjustmentType.NO_CHANGE
98         );
99         assertEquals(new Rectangle2D.Double JavaDoc(10.0, 20.0, 30.0, 40.0), adjusted);
100         
101         // expand height
102
adjusted = i1.createAdjustedRectangle(
103             base, LengthAdjustmentType.NO_CHANGE, LengthAdjustmentType.EXPAND
104         );
105         assertEquals(new Rectangle2D.Double JavaDoc(10.0, 19.0, 30.0, 44.0), adjusted);
106         
107         // contract height
108
adjusted = i1.createAdjustedRectangle(
109             base, LengthAdjustmentType.NO_CHANGE, LengthAdjustmentType.CONTRACT
110         );
111         assertEquals(new Rectangle2D.Double JavaDoc(10.0, 21.0, 30.0, 36.0), adjusted);
112             
113         // expand width
114
adjusted = i1.createAdjustedRectangle(
115             base, LengthAdjustmentType.EXPAND, LengthAdjustmentType.NO_CHANGE
116         );
117         assertEquals(new Rectangle2D.Double JavaDoc(8.0, 20.0, 36.0, 40.0), adjusted);
118             
119         // contract width
120
adjusted = i1.createAdjustedRectangle(
121             base, LengthAdjustmentType.CONTRACT, LengthAdjustmentType.NO_CHANGE
122         );
123         assertEquals(new Rectangle2D.Double JavaDoc(12.0, 20.0, 24.0, 40.0), adjusted);
124         
125         // expand both
126
adjusted = i1.createAdjustedRectangle(
127             base, LengthAdjustmentType.EXPAND, LengthAdjustmentType.EXPAND
128         );
129         assertEquals(new Rectangle2D.Double JavaDoc(8.0, 19.0, 36.0, 44.0), adjusted);
130             
131         // expand width contract height
132
adjusted = i1.createAdjustedRectangle(
133             base, LengthAdjustmentType.EXPAND, LengthAdjustmentType.CONTRACT
134         );
135         assertEquals(new Rectangle2D.Double JavaDoc(8.0, 21.0, 36.0, 36.0), adjusted);
136         
137         // contract both
138
adjusted = i1.createAdjustedRectangle(
139             base, LengthAdjustmentType.CONTRACT, LengthAdjustmentType.CONTRACT
140         );
141         assertEquals(new Rectangle2D.Double JavaDoc(12.0, 21.0, 24.0, 36.0), adjusted);
142         
143         // contract width expand height
144
adjusted = i1.createAdjustedRectangle(
145             base, LengthAdjustmentType.CONTRACT, LengthAdjustmentType.EXPAND
146         );
147         assertEquals(new Rectangle2D.Double JavaDoc(12.0, 19.0, 24.0, 44.0), adjusted);
148         
149     }
150     
151     private static final double EPSILON = 0.0000001;
152     
153     /**
154      * Some checks for the createAdjustedRectangle() method.
155      */

156     public void testCreateAdjustedRectangleRelative() {
157         
158         RectangleInsets i1 = new RectangleInsets(
159             UnitType.RELATIVE, 0.04, 0.03, 0.02, 0.01
160         );
161         Rectangle2D JavaDoc base = new Rectangle2D.Double JavaDoc(10.0, 20.0, 30.0, 40.0);
162         
163         // no adjustment
164
Rectangle2D JavaDoc adjusted = i1.createAdjustedRectangle(
165             base, LengthAdjustmentType.NO_CHANGE, LengthAdjustmentType.NO_CHANGE
166         );
167         assertEquals(new Rectangle2D.Double JavaDoc(10.0, 20.0, 30.0, 40.0), adjusted);
168         
169         // expand height
170
adjusted = i1.createAdjustedRectangle(
171             base, LengthAdjustmentType.NO_CHANGE, LengthAdjustmentType.EXPAND
172         );
173         assertEquals(10.0, adjusted.getX(), EPSILON);
174         assertEquals(18.2978723404, adjusted.getY(), EPSILON);
175         assertEquals(30.0, adjusted.getWidth(), EPSILON);
176         assertEquals(42.553191489, adjusted.getHeight(), EPSILON);
177         
178         // contract height
179
adjusted = i1.createAdjustedRectangle(
180             base, LengthAdjustmentType.NO_CHANGE, LengthAdjustmentType.CONTRACT
181         );
182         assertEquals(10.0, adjusted.getX(), EPSILON);
183         assertEquals(21.6, adjusted.getY(), EPSILON);
184         assertEquals(30.0, adjusted.getWidth(), EPSILON);
185         assertEquals(37.6, adjusted.getHeight(), EPSILON);
186             
187         // expand width
188
adjusted = i1.createAdjustedRectangle(
189             base, LengthAdjustmentType.EXPAND, LengthAdjustmentType.NO_CHANGE
190         );
191         assertEquals(9.0625, adjusted.getX(), EPSILON);
192         assertEquals(20.0, adjusted.getY(), EPSILON);
193         assertEquals(31.25, adjusted.getWidth(), EPSILON);
194         assertEquals(40.0, adjusted.getHeight(), EPSILON);
195             
196         // contract width
197
adjusted = i1.createAdjustedRectangle(
198             base, LengthAdjustmentType.CONTRACT, LengthAdjustmentType.NO_CHANGE
199         );
200         assertEquals(10.9, adjusted.getX(), EPSILON);
201         assertEquals(20.0, adjusted.getY(), EPSILON);
202         assertEquals(28.8, adjusted.getWidth(), EPSILON);
203         assertEquals(40.0, adjusted.getHeight(), EPSILON);
204         
205         // expand both
206
adjusted = i1.createAdjustedRectangle(
207             base, LengthAdjustmentType.EXPAND, LengthAdjustmentType.EXPAND
208         );
209         assertEquals(9.0625, adjusted.getX(), EPSILON);
210         assertEquals(18.2978723404, adjusted.getY(), EPSILON);
211         assertEquals(31.25, adjusted.getWidth(), EPSILON);
212         assertEquals(42.553191489, adjusted.getHeight(), EPSILON);
213             
214         // expand width contract height
215
adjusted = i1.createAdjustedRectangle(
216             base, LengthAdjustmentType.EXPAND, LengthAdjustmentType.CONTRACT
217         );
218         assertEquals(9.0625, adjusted.getX(), EPSILON);
219         assertEquals(21.6, adjusted.getY(), EPSILON);
220         assertEquals(31.25, adjusted.getWidth(), EPSILON);
221         assertEquals(37.6, adjusted.getHeight(), EPSILON);
222         
223         // contract both
224
adjusted = i1.createAdjustedRectangle(
225             base, LengthAdjustmentType.CONTRACT, LengthAdjustmentType.CONTRACT
226         );
227         assertEquals(10.9, adjusted.getX(), EPSILON);
228         assertEquals(21.6, adjusted.getY(), EPSILON);
229         assertEquals(28.8, adjusted.getWidth(), EPSILON);
230         assertEquals(37.6, adjusted.getHeight(), EPSILON);
231         
232         // contract width expand height
233
adjusted = i1.createAdjustedRectangle(
234             base, LengthAdjustmentType.CONTRACT, LengthAdjustmentType.EXPAND
235         );
236         assertEquals(10.9, adjusted.getX(), EPSILON);
237         assertEquals(18.2978723404, adjusted.getY(), EPSILON);
238         assertEquals(28.8, adjusted.getWidth(), EPSILON);
239         assertEquals(42.553191489, adjusted.getHeight(), EPSILON);
240         
241     }
242
243     /**
244      * Test the equals() method.
245      */

246     public void testEquals() {
247         RectangleInsets i1 = new RectangleInsets(
248             UnitType.ABSOLUTE, 1.0, 2.0, 3.0, 4.0
249         );
250         RectangleInsets i2 = new RectangleInsets(
251             UnitType.ABSOLUTE, 1.0, 2.0, 3.0, 4.0
252         );
253         assertTrue(i1.equals(i2));
254         assertTrue(i2.equals(i1));
255         
256         i1 = new RectangleInsets(UnitType.RELATIVE, 1.0, 2.0, 3.0, 4.0);
257         assertFalse(i1.equals(i2));
258         i2 = new RectangleInsets(UnitType.RELATIVE, 1.0, 2.0, 3.0, 4.0);
259         assertTrue(i1.equals(i2));
260
261         i1 = new RectangleInsets(UnitType.RELATIVE, 0.0, 2.0, 3.0, 4.0);
262         assertFalse(i1.equals(i2));
263         i2 = new RectangleInsets(UnitType.RELATIVE, 0.0, 2.0, 3.0, 4.0);
264         assertTrue(i1.equals(i2));
265         
266         i1 = new RectangleInsets(UnitType.RELATIVE, 0.0, 0.0, 3.0, 4.0);
267         assertFalse(i1.equals(i2));
268         i2 = new RectangleInsets(UnitType.RELATIVE, 0.0, 0.0, 3.0, 4.0);
269         assertTrue(i1.equals(i2));
270         
271         i1 = new RectangleInsets(UnitType.RELATIVE, 0.0, 0.0, 0.0, 4.0);
272         assertFalse(i1.equals(i2));
273         i2 = new RectangleInsets(UnitType.RELATIVE, 0.0, 0.0, 0.0, 4.0);
274         assertTrue(i1.equals(i2));
275         
276         i1 = new RectangleInsets(UnitType.RELATIVE, 0.0, 0.0, 0.0, 0.0);
277         assertFalse(i1.equals(i2));
278         i2 = new RectangleInsets(UnitType.RELATIVE, 0.0, 0.0, 0.0, 0.0);
279         assertTrue(i1.equals(i2));
280         
281     }
282     
283     /**
284      * Serialize an instance, restore it, and check for identity.
285      */

286     public void testSerialization() {
287         final RectangleInsets i1 = new RectangleInsets(
288             UnitType.ABSOLUTE, 1.0, 2.0, 3.0, 4.0
289         );
290         RectangleInsets i2 = null;
291         try {
292             final ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
293             final ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
294             out.writeObject(i1);
295             out.close();
296
297             final ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
298                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
299             );
300             i2 = (RectangleInsets) in.readObject();
301             in.close();
302         }
303         catch (Exception JavaDoc e) {
304             System.out.println(e.toString());
305         }
306         assertTrue(i1.equals(i2));
307     }
308     
309     /**
310      * Runs the test suite using the JUnit text-based runner.
311      *
312      * @param args ignored.
313      */

314     public static void main(String JavaDoc[] args) {
315         junit.textui.TestRunner.run(suite());
316     }
317
318 }
319
Popular Tags