KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > util > junit > ShapeUtilitiesTests


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  * ShapeUtilitiesTests.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: ShapeUtilitiesTests.java,v 1.6 2005/10/18 13:25:14 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 26-Oct-2004 : Version 1 (DG);
40  * 10-Nov-2004 : Extended test for equal shapes to include Ellipse2D (DG);
41  * 16-Mar-2005 : Extended test for equal shapes to include Polygon (DG);
42  *
43  */

44
45 package org.jfree.util.junit;
46
47 import java.awt.Polygon JavaDoc;
48 import java.awt.Shape JavaDoc;
49 import java.awt.geom.Arc2D JavaDoc;
50 import java.awt.geom.Ellipse2D JavaDoc;
51 import java.awt.geom.GeneralPath JavaDoc;
52 import java.awt.geom.Line2D JavaDoc;
53 import java.awt.geom.Rectangle2D JavaDoc;
54
55 import junit.framework.Test;
56 import junit.framework.TestCase;
57 import junit.framework.TestSuite;
58
59 import org.jfree.util.ShapeUtilities;
60
61 /**
62  * Tests for the {@link ShapeUtilities} class.
63  */

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

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

80     public ShapeUtilitiesTests(final String JavaDoc name) {
81         super(name);
82     }
83
84     /**
85      * Tests the equal() method.
86      */

87     public void testEqualLine2Ds() {
88         
89         // LINE2D
90
assertTrue(ShapeUtilities.equal((Line2D JavaDoc) null, (Line2D JavaDoc) null));
91         Line2D JavaDoc l1 = new Line2D.Float JavaDoc(1.0f, 2.0f, 3.0f, 4.0f);
92         Line2D JavaDoc l2 = new Line2D.Float JavaDoc(1.0f, 2.0f, 3.0f, 4.0f);
93         assertTrue(ShapeUtilities.equal(l1, l2));
94         
95         l1 = new Line2D.Float JavaDoc(4.0f, 3.0f, 2.0f, 1.0f);
96         assertFalse(ShapeUtilities.equal(l1, l2));
97         l2 = new Line2D.Float JavaDoc(4.0f, 3.0f, 2.0f, 1.0f);
98         assertTrue(ShapeUtilities.equal(l1, l2));
99         
100         l1 = new Line2D.Double JavaDoc(4.0f, 3.0f, 2.0f, 1.0f);
101         assertTrue(ShapeUtilities.equal(l1, l2));
102         
103     }
104
105     /**
106      * Some checks for the equal(Shape, Shape) method.
107      */

108     public void testEqualShapes() {
109         
110         // NULL
111
Shape JavaDoc s1 = null;
112         Shape JavaDoc s2 = null;
113         assertTrue(ShapeUtilities.equal(s1, s2));
114         
115         // LINE2D
116
s1 = new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0);
117         assertFalse(ShapeUtilities.equal(s1, s2));
118         s2 = new Line2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0);
119         assertTrue(ShapeUtilities.equal(s1, s2));
120         assertFalse(s1.equals(s2));
121         
122         // RECTANGLE2D
123
s1 = new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0);
124         assertFalse(ShapeUtilities.equal(s1, s2));
125         s2 = new Rectangle2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0);
126         assertTrue(ShapeUtilities.equal(s1, s2));
127         assertTrue(s1.equals(s2)); // Rectangle2D overrides equals()
128

129         // ELLIPSE2D
130
s1 = new Ellipse2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0);
131         assertFalse(ShapeUtilities.equal(s1, s2));
132         s2 = new Ellipse2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0);
133         assertTrue(ShapeUtilities.equal(s1, s2));
134         assertFalse(s1.equals(s2));
135         
136         // ARC2D
137
s1 = new Arc2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, Arc2D.PIE);
138         assertFalse(ShapeUtilities.equal(s1, s2));
139         s2 = new Arc2D.Double JavaDoc(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, Arc2D.PIE);
140         assertTrue(ShapeUtilities.equal(s1, s2));
141         assertFalse(s1.equals(s2));
142         
143         // POLYGON
144
Polygon JavaDoc p1 = new Polygon JavaDoc(new int[] {0, 1, 0}, new int[] {1, 0, 1}, 3);
145         Polygon JavaDoc p2 = new Polygon JavaDoc(new int[] {1, 1, 0}, new int[] {1, 0, 1}, 3);
146         s1 = p1;
147         s2 = p2;
148         assertFalse(ShapeUtilities.equal(s1, s2));
149         p2 = new Polygon JavaDoc(new int[] {0, 1, 0}, new int[] {1, 0, 1}, 3);
150         s2 = p2;
151         assertTrue(ShapeUtilities.equal(s1, s2));
152         
153         // GENERALPATH
154
GeneralPath JavaDoc g1 = new GeneralPath JavaDoc();
155         g1.moveTo(1.0f, 2.0f);
156         g1.lineTo(3.0f, 4.0f);
157         g1.curveTo(5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f);
158         g1.quadTo(1.0f, 2.0f, 3.0f, 4.0f);
159         g1.closePath();
160         s1 = g1;
161         assertFalse(ShapeUtilities.equal(s1, s2));
162         GeneralPath JavaDoc g2 = new GeneralPath JavaDoc();
163         g2.moveTo(1.0f, 2.0f);
164         g2.lineTo(3.0f, 4.0f);
165         g2.curveTo(5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f);
166         g2.quadTo(1.0f, 2.0f, 3.0f, 4.0f);
167         g2.closePath();
168         s2 = g2;
169         assertTrue(ShapeUtilities.equal(s1, s2));
170         assertFalse(s1.equals(s2));
171         
172         
173     }
174
175   /**
176    * Some checks for the intersects() method,
177    */

178   public void testIntersects ()
179   {
180     final Rectangle2D JavaDoc r1 = new Rectangle2D.Float JavaDoc(0, 0, 100, 100);
181     final Rectangle2D JavaDoc r2 = new Rectangle2D.Float JavaDoc(0, 0, 100, 100);
182     assertTrue(ShapeUtilities.intersects(r1, r2));
183
184     r1.setRect(100, 0, 100, 0);
185     assertTrue(ShapeUtilities.intersects(r1, r2));
186     assertTrue(ShapeUtilities.intersects(r2, r1));
187
188     r1.setRect(0, 0, 0, 0);
189     assertTrue(ShapeUtilities.intersects(r1, r2));
190     assertTrue(ShapeUtilities.intersects(r2, r1));
191
192     r1.setRect(50, 50, 10, 0);
193     assertTrue(ShapeUtilities.intersects(r1, r2));
194     assertTrue(ShapeUtilities.intersects(r2, r1));
195   }
196 }
197
Popular Tags