001: /* ===========================================================
002: * JFreeChart : a free chart 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/jfreechart/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: * GridArrangementTests.java
029: * -------------------------
030: * (C) Copyright 2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: GridArrangementTests.java,v 1.1.2.1 2006/10/03 15:41:44 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 08-Mar-2005 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.block.junit;
044:
045: import java.io.ByteArrayInputStream;
046: import java.io.ByteArrayOutputStream;
047: import java.io.ObjectInput;
048: import java.io.ObjectInputStream;
049: import java.io.ObjectOutput;
050: import java.io.ObjectOutputStream;
051:
052: import junit.framework.Test;
053: import junit.framework.TestCase;
054: import junit.framework.TestSuite;
055:
056: import org.jfree.chart.block.Block;
057: import org.jfree.chart.block.BlockContainer;
058: import org.jfree.chart.block.EmptyBlock;
059: import org.jfree.chart.block.GridArrangement;
060: import org.jfree.chart.block.LengthConstraintType;
061: import org.jfree.chart.block.RectangleConstraint;
062: import org.jfree.ui.Size2D;
063:
064: /**
065: * Tests for the {@link GridArrangement} class.
066: */
067: public class GridArrangementTests extends TestCase {
068:
069: /**
070: * Returns the tests as a test suite.
071: *
072: * @return The test suite.
073: */
074: public static Test suite() {
075: return new TestSuite(GridArrangementTests.class);
076: }
077:
078: /**
079: * Constructs a new set of tests.
080: *
081: * @param name the name of the tests.
082: */
083: public GridArrangementTests(String name) {
084: super (name);
085: }
086:
087: /**
088: * Confirm that the equals() method can distinguish all the required fields.
089: */
090: public void testEquals() {
091: GridArrangement f1 = new GridArrangement(11, 22);
092: GridArrangement f2 = new GridArrangement(11, 22);
093: assertTrue(f1.equals(f2));
094: assertTrue(f2.equals(f1));
095:
096: f1 = new GridArrangement(33, 22);
097: assertFalse(f1.equals(f2));
098: f2 = new GridArrangement(33, 22);
099: assertTrue(f1.equals(f2));
100:
101: f1 = new GridArrangement(33, 44);
102: assertFalse(f1.equals(f2));
103: f2 = new GridArrangement(33, 44);
104: assertTrue(f1.equals(f2));
105: }
106:
107: /**
108: * Immutable - cloning is not necessary.
109: */
110: public void testCloning() {
111: GridArrangement f1 = new GridArrangement(1, 2);
112: assertFalse(f1 instanceof Cloneable);
113: }
114:
115: /**
116: * Serialize an instance, restore it, and check for equality.
117: */
118: public void testSerialization() {
119: GridArrangement f1 = new GridArrangement(33, 44);
120: GridArrangement f2 = null;
121: try {
122: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
123: ObjectOutput out = new ObjectOutputStream(buffer);
124: out.writeObject(f1);
125: out.close();
126:
127: ObjectInput in = new ObjectInputStream(
128: new ByteArrayInputStream(buffer.toByteArray()));
129: f2 = (GridArrangement) in.readObject();
130: in.close();
131: } catch (Exception e) {
132: e.printStackTrace();
133: }
134: assertEquals(f1, f2);
135: }
136:
137: private static final double EPSILON = 0.000000001;
138:
139: /**
140: * Test arrangement with no constraints.
141: */
142: public void testNN() {
143: BlockContainer c = createTestContainer1();
144: Size2D s = c.arrange(null, RectangleConstraint.NONE);
145: assertEquals(90.0, s.width, EPSILON);
146: assertEquals(33.0, s.height, EPSILON);
147: }
148:
149: /**
150: * Test arrangement with no constraints.
151: */
152: public void testFN() {
153: BlockContainer c = createTestContainer1();
154: RectangleConstraint constraint = new RectangleConstraint(100.0,
155: null, LengthConstraintType.FIXED, 0.0, null,
156: LengthConstraintType.NONE);
157: Size2D s = c.arrange(null, constraint);
158: assertEquals(100.0, s.width, EPSILON);
159: assertEquals(33.0, s.height, EPSILON);
160: }
161:
162: private BlockContainer createTestContainer1() {
163: Block b1 = new EmptyBlock(10, 11);
164: Block b2 = new EmptyBlock(20, 22);
165: Block b3 = new EmptyBlock(30, 33);
166: BlockContainer result = new BlockContainer(new GridArrangement(
167: 1, 3));
168: result.add(b1);
169: result.add(b2);
170: result.add(b3);
171: return result;
172: }
173:
174: }
|