001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ------------------------
028: * ShapeUtilitiesTests.java
029: * ------------------------
030: * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: ShapeUtilitiesTests.java,v 1.6 2005/10/18 13:25:14 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 26-Oct-2004 : Version 1 (DG);
040: * 10-Nov-2004 : Extended test for equal shapes to include Ellipse2D (DG);
041: * 16-Mar-2005 : Extended test for equal shapes to include Polygon (DG);
042: *
043: */
044:
045: package org.jfree.util.junit;
046:
047: import java.awt.Polygon;
048: import java.awt.Shape;
049: import java.awt.geom.Arc2D;
050: import java.awt.geom.Ellipse2D;
051: import java.awt.geom.GeneralPath;
052: import java.awt.geom.Line2D;
053: import java.awt.geom.Rectangle2D;
054:
055: import junit.framework.Test;
056: import junit.framework.TestCase;
057: import junit.framework.TestSuite;
058:
059: import org.jfree.util.ShapeUtilities;
060:
061: /**
062: * Tests for the {@link ShapeUtilities} class.
063: */
064: public class ShapeUtilitiesTests extends TestCase {
065:
066: /**
067: * Returns the tests as a test suite.
068: *
069: * @return The test suite.
070: */
071: public static Test suite() {
072: return new TestSuite(ShapeUtilitiesTests.class);
073: }
074:
075: /**
076: * Constructs a new set of tests.
077: *
078: * @param name the name of the tests.
079: */
080: public ShapeUtilitiesTests(final String name) {
081: super (name);
082: }
083:
084: /**
085: * Tests the equal() method.
086: */
087: public void testEqualLine2Ds() {
088:
089: // LINE2D
090: assertTrue(ShapeUtilities.equal((Line2D) null, (Line2D) null));
091: Line2D l1 = new Line2D.Float(1.0f, 2.0f, 3.0f, 4.0f);
092: Line2D l2 = new Line2D.Float(1.0f, 2.0f, 3.0f, 4.0f);
093: assertTrue(ShapeUtilities.equal(l1, l2));
094:
095: l1 = new Line2D.Float(4.0f, 3.0f, 2.0f, 1.0f);
096: assertFalse(ShapeUtilities.equal(l1, l2));
097: l2 = new Line2D.Float(4.0f, 3.0f, 2.0f, 1.0f);
098: assertTrue(ShapeUtilities.equal(l1, l2));
099:
100: l1 = new Line2D.Double(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 s1 = null;
112: Shape s2 = null;
113: assertTrue(ShapeUtilities.equal(s1, s2));
114:
115: // LINE2D
116: s1 = new Line2D.Double(1.0, 2.0, 3.0, 4.0);
117: assertFalse(ShapeUtilities.equal(s1, s2));
118: s2 = new Line2D.Double(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(1.0, 2.0, 3.0, 4.0);
124: assertFalse(ShapeUtilities.equal(s1, s2));
125: s2 = new Rectangle2D.Double(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(1.0, 2.0, 3.0, 4.0);
131: assertFalse(ShapeUtilities.equal(s1, s2));
132: s2 = new Ellipse2D.Double(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(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(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 p1 = new Polygon(new int[] { 0, 1, 0 }, new int[] { 1,
145: 0, 1 }, 3);
146: Polygon p2 = new Polygon(new int[] { 1, 1, 0 }, new int[] { 1,
147: 0, 1 }, 3);
148: s1 = p1;
149: s2 = p2;
150: assertFalse(ShapeUtilities.equal(s1, s2));
151: p2 = new Polygon(new int[] { 0, 1, 0 }, new int[] { 1, 0, 1 },
152: 3);
153: s2 = p2;
154: assertTrue(ShapeUtilities.equal(s1, s2));
155:
156: // GENERALPATH
157: GeneralPath g1 = new GeneralPath();
158: g1.moveTo(1.0f, 2.0f);
159: g1.lineTo(3.0f, 4.0f);
160: g1.curveTo(5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f);
161: g1.quadTo(1.0f, 2.0f, 3.0f, 4.0f);
162: g1.closePath();
163: s1 = g1;
164: assertFalse(ShapeUtilities.equal(s1, s2));
165: GeneralPath g2 = new GeneralPath();
166: g2.moveTo(1.0f, 2.0f);
167: g2.lineTo(3.0f, 4.0f);
168: g2.curveTo(5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f);
169: g2.quadTo(1.0f, 2.0f, 3.0f, 4.0f);
170: g2.closePath();
171: s2 = g2;
172: assertTrue(ShapeUtilities.equal(s1, s2));
173: assertFalse(s1.equals(s2));
174:
175: }
176:
177: /**
178: * Some checks for the intersects() method,
179: */
180: public void testIntersects() {
181: final Rectangle2D r1 = new Rectangle2D.Float(0, 0, 100, 100);
182: final Rectangle2D r2 = new Rectangle2D.Float(0, 0, 100, 100);
183: assertTrue(ShapeUtilities.intersects(r1, r2));
184:
185: r1.setRect(100, 0, 100, 0);
186: assertTrue(ShapeUtilities.intersects(r1, r2));
187: assertTrue(ShapeUtilities.intersects(r2, r1));
188:
189: r1.setRect(0, 0, 0, 0);
190: assertTrue(ShapeUtilities.intersects(r1, r2));
191: assertTrue(ShapeUtilities.intersects(r2, r1));
192:
193: r1.setRect(50, 50, 10, 0);
194: assertTrue(ShapeUtilities.intersects(r1, r2));
195: assertTrue(ShapeUtilities.intersects(r2, r1));
196: }
197: }
|