1 /* ===========================================================
2 * JFreeChart : a free chart 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/jfreechart/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 License
20 * along with this library; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 *
23 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24 * in the United States and other countries.]
25 *
26 * -------------------------
27 * GridArrangementTests.java
28 * -------------------------
29 * (C) Copyright 2005, by Object Refinery Limited and Contributors.
30 *
31 * Original Author: David Gilbert (for Object Refinery Limited);
32 * Contributor(s): -;
33 *
34 * $Id: GridArrangementTests.java,v 1.1 2005/03/08 11:40:50 mungady Exp $
35 *
36 * Changes
37 * -------
38 * 08-Mar-2005 : Version 1 (DG);
39 *
40 */
41
42 package org.jfree.chart.block.junit;
43
44 import java.io.ByteArrayInputStream;
45 import java.io.ByteArrayOutputStream;
46 import java.io.ObjectInput;
47 import java.io.ObjectInputStream;
48 import java.io.ObjectOutput;
49 import java.io.ObjectOutputStream;
50
51 import junit.framework.Test;
52 import junit.framework.TestCase;
53 import junit.framework.TestSuite;
54
55 import org.jfree.chart.block.Block;
56 import org.jfree.chart.block.BlockContainer;
57 import org.jfree.chart.block.EmptyBlock;
58 import org.jfree.chart.block.GridArrangement;
59 import org.jfree.chart.block.LengthConstraintType;
60 import org.jfree.chart.block.RectangleConstraint;
61 import org.jfree.ui.Size2D;
62
63 /**
64 * Tests for the {@link GridArrangement} class.
65 */
66 public class GridArrangementTests extends TestCase {
67
68 /**
69 * Returns the tests as a test suite.
70 *
71 * @return The test suite.
72 */
73 public static Test suite() {
74 return new TestSuite(GridArrangementTests.class);
75 }
76
77 /**
78 * Constructs a new set of tests.
79 *
80 * @param name the name of the tests.
81 */
82 public GridArrangementTests(String name) {
83 super(name);
84 }
85
86 /**
87 * Confirm that the equals() method can distinguish all the required fields.
88 */
89 public void testEquals() {
90 GridArrangement f1 = new GridArrangement(11, 22);
91 GridArrangement f2 = new GridArrangement(11, 22);
92 assertTrue(f1.equals(f2));
93 assertTrue(f2.equals(f1));
94
95 f1 = new GridArrangement(33, 22);
96 assertFalse(f1.equals(f2));
97 f2 = new GridArrangement(33, 22);
98 assertTrue(f1.equals(f2));
99
100 f1 = new GridArrangement(33, 44);
101 assertFalse(f1.equals(f2));
102 f2 = new GridArrangement(33, 44);
103 assertTrue(f1.equals(f2));
104 }
105
106 /**
107 * Immutable - cloning is not necessary.
108 */
109 public void testCloning() {
110 GridArrangement f1 = new GridArrangement(1, 2);
111 assertFalse(f1 instanceof Cloneable);
112 }
113
114 /**
115 * Serialize an instance, restore it, and check for equality.
116 */
117 public void testSerialization() {
118 GridArrangement f1 = new GridArrangement(33, 44);
119 GridArrangement f2 = null;
120 try {
121 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
122 ObjectOutput out = new ObjectOutputStream(buffer);
123 out.writeObject(f1);
124 out.close();
125
126 ObjectInput in = new ObjectInputStream(
127 new ByteArrayInputStream(buffer.toByteArray())
128 );
129 f2 = (GridArrangement) in.readObject();
130 in.close();
131 }
132 catch (Exception e) {
133 e.printStackTrace();
134 }
135 assertEquals(f1, f2);
136 }
137
138 private static final double EPSILON = 0.000000001;
139
140 /**
141 * Test arrangement with no constraints.
142 */
143 public void testNN() {
144 BlockContainer c = createTestContainer1();
145 Size2D s = c.arrange(null, RectangleConstraint.NONE);
146 assertEquals(90.0, s.width, EPSILON);
147 assertEquals(33.0, s.height, EPSILON);
148 }
149
150 /**
151 * Test arrangement with no constraints.
152 */
153 public void testFN() {
154 BlockContainer c = createTestContainer1();
155 RectangleConstraint constraint = new RectangleConstraint(
156 100.0, null, LengthConstraintType.FIXED,
157 0.0, null, LengthConstraintType.NONE
158 );
159 Size2D s = c.arrange(null, constraint);
160 assertEquals(100.0, s.width, EPSILON);
161 assertEquals(33.0, s.height, EPSILON);
162 }
163
164 private BlockContainer createTestContainer1() {
165 Block b1 = new EmptyBlock(10, 11);
166 Block b2 = new EmptyBlock(20, 22);
167 Block b3 = new EmptyBlock(30, 33);
168 BlockContainer result = new BlockContainer(new GridArrangement(1, 3));
169 result.add(b1);
170 result.add(b2);
171 result.add(b3);
172 return result;
173 }
174
175 }