KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > block > junit > GridArrangementTests


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
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  * GridArrangementTests.java
29  * -------------------------
30  * (C) Copyright 2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: GridArrangementTests.java,v 1.1.2.1 2006/10/03 15:41:44 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 08-Mar-2005 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.chart.block.junit;
44
45 import java.io.ByteArrayInputStream JavaDoc;
46 import java.io.ByteArrayOutputStream JavaDoc;
47 import java.io.ObjectInput JavaDoc;
48 import java.io.ObjectInputStream JavaDoc;
49 import java.io.ObjectOutput JavaDoc;
50 import java.io.ObjectOutputStream JavaDoc;
51
52 import junit.framework.Test;
53 import junit.framework.TestCase;
54 import junit.framework.TestSuite;
55
56 import org.jfree.chart.block.Block;
57 import org.jfree.chart.block.BlockContainer;
58 import org.jfree.chart.block.EmptyBlock;
59 import org.jfree.chart.block.GridArrangement;
60 import org.jfree.chart.block.LengthConstraintType;
61 import org.jfree.chart.block.RectangleConstraint;
62 import org.jfree.ui.Size2D;
63
64 /**
65  * Tests for the {@link GridArrangement} class.
66  */

67 public class GridArrangementTests extends TestCase {
68     
69     /**
70      * Returns the tests as a test suite.
71      *
72      * @return The test suite.
73      */

74     public static Test suite() {
75         return new TestSuite(GridArrangementTests.class);
76     }
77
78     /**
79      * Constructs a new set of tests.
80      *
81      * @param name the name of the tests.
82      */

83     public GridArrangementTests(String JavaDoc name) {
84         super(name);
85     }
86     
87     /**
88      * Confirm that the equals() method can distinguish all the required fields.
89      */

90     public void testEquals() {
91         GridArrangement f1 = new GridArrangement(11, 22);
92         GridArrangement f2 = new GridArrangement(11, 22);
93         assertTrue(f1.equals(f2));
94         assertTrue(f2.equals(f1));
95
96         f1 = new GridArrangement(33, 22);
97         assertFalse(f1.equals(f2));
98         f2 = new GridArrangement(33, 22);
99         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 JavaDoc);
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 JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
123             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
124             out.writeObject(f1);
125             out.close();
126
127             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
128                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
129             );
130             f2 = (GridArrangement) in.readObject();
131             in.close();
132         }
133         catch (Exception JavaDoc e) {
134             e.printStackTrace();
135         }
136         assertEquals(f1, f2);
137     }
138     
139     private static final double EPSILON = 0.000000001;
140     
141     /**
142      * Test arrangement with no constraints.
143      */

144     public void testNN() {
145         BlockContainer c = createTestContainer1();
146         Size2D s = c.arrange(null, RectangleConstraint.NONE);
147         assertEquals(90.0, s.width, EPSILON);
148         assertEquals(33.0, s.height, EPSILON);
149     }
150    
151     /**
152      * Test arrangement with no constraints.
153      */

154     public void testFN() {
155         BlockContainer c = createTestContainer1();
156         RectangleConstraint constraint = new RectangleConstraint(
157             100.0, null, LengthConstraintType.FIXED,
158             0.0, null, LengthConstraintType.NONE
159         );
160         Size2D s = c.arrange(null, constraint);
161         assertEquals(100.0, s.width, EPSILON);
162         assertEquals(33.0, s.height, EPSILON);
163     }
164
165     private BlockContainer createTestContainer1() {
166         Block b1 = new EmptyBlock(10, 11);
167         Block b2 = new EmptyBlock(20, 22);
168         Block b3 = new EmptyBlock(30, 33);
169         BlockContainer result = new BlockContainer(new GridArrangement(1, 3));
170         result.add(b1);
171         result.add(b2);
172         result.add(b3);
173         return result;
174     }
175     
176 }
177
Popular Tags