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: * RectangleInsetsTests.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: RectangleInsetsTests.java,v 1.6 2005/10/18 13:22:36 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 14-Jun-2004 : Version 1 (DG);
040: * 03-May-2005 : Fixed tests for changes to RectangleInsets constructor (DG);
041: *
042: */
043:
044: package org.jfree.ui.junit;
045:
046: import java.awt.geom.Rectangle2D;
047: import java.io.ByteArrayInputStream;
048: import java.io.ByteArrayOutputStream;
049: import java.io.ObjectInput;
050: import java.io.ObjectInputStream;
051: import java.io.ObjectOutput;
052: import java.io.ObjectOutputStream;
053:
054: import junit.framework.Test;
055: import junit.framework.TestCase;
056: import junit.framework.TestSuite;
057:
058: import org.jfree.ui.RectangleInsets;
059: import org.jfree.ui.LengthAdjustmentType;
060: import org.jfree.util.UnitType;
061:
062: /**
063: * Tests for the {@link RectangleInsets} class.
064: */
065: public class RectangleInsetsTests extends TestCase {
066:
067: /**
068: * Returns the tests as a test suite.
069: *
070: * @return The test suite.
071: */
072: public static Test suite() {
073: return new TestSuite(RectangleInsetsTests.class);
074: }
075:
076: /**
077: * Constructs a new set of tests.
078: *
079: * @param name the name of the tests.
080: */
081: public RectangleInsetsTests(final String name) {
082: super (name);
083: }
084:
085: /**
086: * Some checks for the createAdjustedRectangle() method.
087: */
088: public void testCreateAdjustedRectangleAbsolute() {
089:
090: RectangleInsets i1 = new RectangleInsets(UnitType.ABSOLUTE,
091: 1.0, 2.0, 3.0, 4.0);
092: Rectangle2D base = new Rectangle2D.Double(10.0, 20.0, 30.0,
093: 40.0);
094:
095: // no adjustment
096: Rectangle2D adjusted = i1.createAdjustedRectangle(base,
097: LengthAdjustmentType.NO_CHANGE,
098: LengthAdjustmentType.NO_CHANGE);
099: assertEquals(new Rectangle2D.Double(10.0, 20.0, 30.0, 40.0),
100: adjusted);
101:
102: // expand height
103: adjusted = i1.createAdjustedRectangle(base,
104: LengthAdjustmentType.NO_CHANGE,
105: LengthAdjustmentType.EXPAND);
106: assertEquals(new Rectangle2D.Double(10.0, 19.0, 30.0, 44.0),
107: adjusted);
108:
109: // contract height
110: adjusted = i1.createAdjustedRectangle(base,
111: LengthAdjustmentType.NO_CHANGE,
112: LengthAdjustmentType.CONTRACT);
113: assertEquals(new Rectangle2D.Double(10.0, 21.0, 30.0, 36.0),
114: adjusted);
115:
116: // expand width
117: adjusted = i1.createAdjustedRectangle(base,
118: LengthAdjustmentType.EXPAND,
119: LengthAdjustmentType.NO_CHANGE);
120: assertEquals(new Rectangle2D.Double(8.0, 20.0, 36.0, 40.0),
121: adjusted);
122:
123: // contract width
124: adjusted = i1.createAdjustedRectangle(base,
125: LengthAdjustmentType.CONTRACT,
126: LengthAdjustmentType.NO_CHANGE);
127: assertEquals(new Rectangle2D.Double(12.0, 20.0, 24.0, 40.0),
128: adjusted);
129:
130: // expand both
131: adjusted = i1.createAdjustedRectangle(base,
132: LengthAdjustmentType.EXPAND,
133: LengthAdjustmentType.EXPAND);
134: assertEquals(new Rectangle2D.Double(8.0, 19.0, 36.0, 44.0),
135: adjusted);
136:
137: // expand width contract height
138: adjusted = i1.createAdjustedRectangle(base,
139: LengthAdjustmentType.EXPAND,
140: LengthAdjustmentType.CONTRACT);
141: assertEquals(new Rectangle2D.Double(8.0, 21.0, 36.0, 36.0),
142: adjusted);
143:
144: // contract both
145: adjusted = i1.createAdjustedRectangle(base,
146: LengthAdjustmentType.CONTRACT,
147: LengthAdjustmentType.CONTRACT);
148: assertEquals(new Rectangle2D.Double(12.0, 21.0, 24.0, 36.0),
149: adjusted);
150:
151: // contract width expand height
152: adjusted = i1.createAdjustedRectangle(base,
153: LengthAdjustmentType.CONTRACT,
154: LengthAdjustmentType.EXPAND);
155: assertEquals(new Rectangle2D.Double(12.0, 19.0, 24.0, 44.0),
156: adjusted);
157:
158: }
159:
160: private static final double EPSILON = 0.0000001;
161:
162: /**
163: * Some checks for the createAdjustedRectangle() method.
164: */
165: public void testCreateAdjustedRectangleRelative() {
166:
167: RectangleInsets i1 = new RectangleInsets(UnitType.RELATIVE,
168: 0.04, 0.03, 0.02, 0.01);
169: Rectangle2D base = new Rectangle2D.Double(10.0, 20.0, 30.0,
170: 40.0);
171:
172: // no adjustment
173: Rectangle2D adjusted = i1.createAdjustedRectangle(base,
174: LengthAdjustmentType.NO_CHANGE,
175: LengthAdjustmentType.NO_CHANGE);
176: assertEquals(new Rectangle2D.Double(10.0, 20.0, 30.0, 40.0),
177: adjusted);
178:
179: // expand height
180: adjusted = i1.createAdjustedRectangle(base,
181: LengthAdjustmentType.NO_CHANGE,
182: LengthAdjustmentType.EXPAND);
183: assertEquals(10.0, adjusted.getX(), EPSILON);
184: assertEquals(18.2978723404, adjusted.getY(), EPSILON);
185: assertEquals(30.0, adjusted.getWidth(), EPSILON);
186: assertEquals(42.553191489, adjusted.getHeight(), EPSILON);
187:
188: // contract height
189: adjusted = i1.createAdjustedRectangle(base,
190: LengthAdjustmentType.NO_CHANGE,
191: LengthAdjustmentType.CONTRACT);
192: assertEquals(10.0, adjusted.getX(), EPSILON);
193: assertEquals(21.6, adjusted.getY(), EPSILON);
194: assertEquals(30.0, adjusted.getWidth(), EPSILON);
195: assertEquals(37.6, adjusted.getHeight(), EPSILON);
196:
197: // expand width
198: adjusted = i1.createAdjustedRectangle(base,
199: LengthAdjustmentType.EXPAND,
200: LengthAdjustmentType.NO_CHANGE);
201: assertEquals(9.0625, adjusted.getX(), EPSILON);
202: assertEquals(20.0, adjusted.getY(), EPSILON);
203: assertEquals(31.25, adjusted.getWidth(), EPSILON);
204: assertEquals(40.0, adjusted.getHeight(), EPSILON);
205:
206: // contract width
207: adjusted = i1.createAdjustedRectangle(base,
208: LengthAdjustmentType.CONTRACT,
209: LengthAdjustmentType.NO_CHANGE);
210: assertEquals(10.9, adjusted.getX(), EPSILON);
211: assertEquals(20.0, adjusted.getY(), EPSILON);
212: assertEquals(28.8, adjusted.getWidth(), EPSILON);
213: assertEquals(40.0, adjusted.getHeight(), EPSILON);
214:
215: // expand both
216: adjusted = i1.createAdjustedRectangle(base,
217: LengthAdjustmentType.EXPAND,
218: LengthAdjustmentType.EXPAND);
219: assertEquals(9.0625, adjusted.getX(), EPSILON);
220: assertEquals(18.2978723404, adjusted.getY(), EPSILON);
221: assertEquals(31.25, adjusted.getWidth(), EPSILON);
222: assertEquals(42.553191489, adjusted.getHeight(), EPSILON);
223:
224: // expand width contract height
225: adjusted = i1.createAdjustedRectangle(base,
226: LengthAdjustmentType.EXPAND,
227: LengthAdjustmentType.CONTRACT);
228: assertEquals(9.0625, adjusted.getX(), EPSILON);
229: assertEquals(21.6, adjusted.getY(), EPSILON);
230: assertEquals(31.25, adjusted.getWidth(), EPSILON);
231: assertEquals(37.6, adjusted.getHeight(), EPSILON);
232:
233: // contract both
234: adjusted = i1.createAdjustedRectangle(base,
235: LengthAdjustmentType.CONTRACT,
236: LengthAdjustmentType.CONTRACT);
237: assertEquals(10.9, adjusted.getX(), EPSILON);
238: assertEquals(21.6, adjusted.getY(), EPSILON);
239: assertEquals(28.8, adjusted.getWidth(), EPSILON);
240: assertEquals(37.6, adjusted.getHeight(), EPSILON);
241:
242: // contract width expand height
243: adjusted = i1.createAdjustedRectangle(base,
244: LengthAdjustmentType.CONTRACT,
245: LengthAdjustmentType.EXPAND);
246: assertEquals(10.9, adjusted.getX(), EPSILON);
247: assertEquals(18.2978723404, adjusted.getY(), EPSILON);
248: assertEquals(28.8, adjusted.getWidth(), EPSILON);
249: assertEquals(42.553191489, adjusted.getHeight(), EPSILON);
250:
251: }
252:
253: /**
254: * Test the equals() method.
255: */
256: public void testEquals() {
257: RectangleInsets i1 = new RectangleInsets(UnitType.ABSOLUTE,
258: 1.0, 2.0, 3.0, 4.0);
259: RectangleInsets i2 = new RectangleInsets(UnitType.ABSOLUTE,
260: 1.0, 2.0, 3.0, 4.0);
261: assertTrue(i1.equals(i2));
262: assertTrue(i2.equals(i1));
263:
264: i1 = new RectangleInsets(UnitType.RELATIVE, 1.0, 2.0, 3.0, 4.0);
265: assertFalse(i1.equals(i2));
266: i2 = new RectangleInsets(UnitType.RELATIVE, 1.0, 2.0, 3.0, 4.0);
267: assertTrue(i1.equals(i2));
268:
269: i1 = new RectangleInsets(UnitType.RELATIVE, 0.0, 2.0, 3.0, 4.0);
270: assertFalse(i1.equals(i2));
271: i2 = new RectangleInsets(UnitType.RELATIVE, 0.0, 2.0, 3.0, 4.0);
272: assertTrue(i1.equals(i2));
273:
274: i1 = new RectangleInsets(UnitType.RELATIVE, 0.0, 0.0, 3.0, 4.0);
275: assertFalse(i1.equals(i2));
276: i2 = new RectangleInsets(UnitType.RELATIVE, 0.0, 0.0, 3.0, 4.0);
277: assertTrue(i1.equals(i2));
278:
279: i1 = new RectangleInsets(UnitType.RELATIVE, 0.0, 0.0, 0.0, 4.0);
280: assertFalse(i1.equals(i2));
281: i2 = new RectangleInsets(UnitType.RELATIVE, 0.0, 0.0, 0.0, 4.0);
282: assertTrue(i1.equals(i2));
283:
284: i1 = new RectangleInsets(UnitType.RELATIVE, 0.0, 0.0, 0.0, 0.0);
285: assertFalse(i1.equals(i2));
286: i2 = new RectangleInsets(UnitType.RELATIVE, 0.0, 0.0, 0.0, 0.0);
287: assertTrue(i1.equals(i2));
288:
289: }
290:
291: /**
292: * Serialize an instance, restore it, and check for identity.
293: */
294: public void testSerialization() {
295: final RectangleInsets i1 = new RectangleInsets(
296: UnitType.ABSOLUTE, 1.0, 2.0, 3.0, 4.0);
297: RectangleInsets i2 = null;
298: try {
299: final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
300: final ObjectOutput out = new ObjectOutputStream(buffer);
301: out.writeObject(i1);
302: out.close();
303:
304: final ObjectInput in = new ObjectInputStream(
305: new ByteArrayInputStream(buffer.toByteArray()));
306: i2 = (RectangleInsets) in.readObject();
307: in.close();
308: } catch (Exception e) {
309: System.out.println(e.toString());
310: }
311: assertTrue(i1.equals(i2));
312: }
313:
314: /**
315: * Runs the test suite using the JUnit text-based runner.
316: *
317: * @param args ignored.
318: */
319: public static void main(String[] args) {
320: junit.textui.TestRunner.run(suite());
321: }
322:
323: }
|