KickJava   Java API By Example, From Geeks To Geeks.

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


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  * BlockContainerTests.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: BlockContainerTests.java,v 1.1.2.1 2006/10/03 15:41:44 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 04-Feb-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.BlockContainer;
57 import org.jfree.chart.block.ColumnArrangement;
58 import org.jfree.chart.block.EmptyBlock;
59 import org.jfree.chart.block.FlowArrangement;
60
61 /**
62  * Tests for the {@link BlockContainer} class.
63  */

64 public class BlockContainerTests extends TestCase {
65     
66     /**
67      * Returns the tests as a test suite.
68      *
69      * @return The test suite.
70      */

71     public static Test suite() {
72         return new TestSuite(BlockContainerTests.class);
73     }
74
75     /**
76      * Constructs a new set of tests.
77      *
78      * @param name the name of the tests.
79      */

80     public BlockContainerTests(String JavaDoc name) {
81         super(name);
82     }
83     
84     /**
85      * Confirm that the equals() method can distinguish all the required fields.
86      */

87     public void testEquals() {
88         BlockContainer c1 = new BlockContainer(new FlowArrangement());
89         BlockContainer c2 = new BlockContainer(new FlowArrangement());
90         assertTrue(c1.equals(c2));
91         assertTrue(c2.equals(c2));
92         
93         c1.setArrangement(new ColumnArrangement());
94         assertFalse(c1.equals(c2));
95         c2.setArrangement(new ColumnArrangement());
96         assertTrue(c1.equals(c2));
97         
98         c1.add(new EmptyBlock(1.2, 3.4));
99         assertFalse(c1.equals(c2));
100         c2.add(new EmptyBlock(1.2, 3.4));
101         assertTrue(c1.equals(c2));
102     }
103
104     /**
105      * Confirm that cloning works.
106      */

107     public void testCloning() {
108         BlockContainer c1 = new BlockContainer(new FlowArrangement());
109         c1.add(new EmptyBlock(1.2, 3.4));
110         
111         BlockContainer c2 = null;
112         
113         try {
114             c2 = (BlockContainer) c1.clone();
115         }
116         catch (CloneNotSupportedException JavaDoc e) {
117             System.err.println("Failed to clone.");
118         }
119         assertTrue(c1 != c2);
120         assertTrue(c1.getClass() == c2.getClass());
121         assertTrue(c1.equals(c2));
122     }
123
124     /**
125      * Serialize an instance, restore it, and check for equality.
126      */

127     public void testSerialization() {
128         BlockContainer c1 = new BlockContainer();
129         c1.add(new EmptyBlock(1.2, 3.4));
130         BlockContainer c2 = null;
131         try {
132             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
133             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
134             out.writeObject(c1);
135             out.close();
136
137             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
138                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
139             );
140             c2 = (BlockContainer) in.readObject();
141             in.close();
142         }
143         catch (Exception JavaDoc e) {
144             fail(e.toString());
145         }
146         assertEquals(c1, c2);
147     }
148    
149 }
150
Popular Tags