KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > statistics > junit > SimpleHistogramBinTests


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  * SimpleHistogramBinTests.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: SimpleHistogramBinTests.java,v 1.1.2.1 2006/10/03 15:41:42 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 10-Jan-2005 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.data.statistics.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.data.statistics.SimpleHistogramBin;
57
58 /**
59  * Tests for the {@link SimpleHistogramBin} class.
60  */

61 public class SimpleHistogramBinTests extends TestCase {
62
63     /**
64      * Returns the tests as a test suite.
65      *
66      * @return The test suite.
67      */

68     public static Test suite() {
69         return new TestSuite(SimpleHistogramBinTests.class);
70     }
71
72     /**
73      * Constructs a new set of tests.
74      *
75      * @param name the name of the tests.
76      */

77     public SimpleHistogramBinTests(String JavaDoc name) {
78         super(name);
79     }
80
81     /**
82      * Some checks for the accepts() method.
83      */

84     public void testAccepts() {
85         SimpleHistogramBin bin1 = new SimpleHistogramBin(1.0, 2.0);
86         assertFalse(bin1.accepts(0.0));
87         assertTrue(bin1.accepts(1.0));
88         assertTrue(bin1.accepts(1.5));
89         assertTrue(bin1.accepts(2.0));
90         assertFalse(bin1.accepts(2.1));
91         assertFalse(bin1.accepts(Double.NaN));
92
93         SimpleHistogramBin bin2
94             = new SimpleHistogramBin(1.0, 2.0, false, false);
95         assertFalse(bin2.accepts(0.0));
96         assertFalse(bin2.accepts(1.0));
97         assertTrue(bin2.accepts(1.5));
98         assertFalse(bin2.accepts(2.0));
99         assertFalse(bin2.accepts(2.1));
100         assertFalse(bin2.accepts(Double.NaN));
101     }
102     
103     /**
104      * Some checks for the overlapsWith() method.
105      */

106     public void testOverlapsWidth() {
107         SimpleHistogramBin b1 = new SimpleHistogramBin(1.0, 2.0);
108         SimpleHistogramBin b2 = new SimpleHistogramBin(2.0, 3.0);
109         SimpleHistogramBin b3 = new SimpleHistogramBin(3.0, 4.0);
110         SimpleHistogramBin b4 = new SimpleHistogramBin(0.0, 5.0);
111         SimpleHistogramBin b5 = new SimpleHistogramBin(2.0, 3.0, false, true);
112         SimpleHistogramBin b6 = new SimpleHistogramBin(2.0, 3.0, true, false);
113         assertTrue(b1.overlapsWith(b2));
114         assertTrue(b2.overlapsWith(b1));
115         assertFalse(b1.overlapsWith(b3));
116         assertFalse(b3.overlapsWith(b1));
117         assertTrue(b1.overlapsWith(b4));
118         assertTrue(b4.overlapsWith(b1));
119         assertFalse(b1.overlapsWith(b5));
120         assertFalse(b5.overlapsWith(b1));
121         assertTrue(b1.overlapsWith(b6));
122         assertTrue(b6.overlapsWith(b1));
123     }
124     
125     /**
126      * Ensure that the equals() method can distinguish all fields.
127      */

128     public void testEquals() {
129         SimpleHistogramBin b1 = new SimpleHistogramBin(1.0, 2.0);
130         SimpleHistogramBin b2 = new SimpleHistogramBin(1.0, 2.0);
131         assertTrue(b1.equals(b2));
132         assertTrue(b2.equals(b1));
133         
134         b1 = new SimpleHistogramBin(1.1, 2.0, true, true);
135         assertFalse(b1.equals(b2));
136         b2 = new SimpleHistogramBin(1.1, 2.0, true, true);
137         assertTrue(b1.equals(b2));
138         
139         b1 = new SimpleHistogramBin(1.1, 2.2, true, true);
140         assertFalse(b1.equals(b2));
141         b2 = new SimpleHistogramBin(1.1, 2.2, true, true);
142         assertTrue(b1.equals(b2));
143
144         b1 = new SimpleHistogramBin(1.1, 2.2, false, true);
145         assertFalse(b1.equals(b2));
146         b2 = new SimpleHistogramBin(1.1, 2.2, false, true);
147         assertTrue(b1.equals(b2));
148
149         b1 = new SimpleHistogramBin(1.1, 2.2, false, false);
150         assertFalse(b1.equals(b2));
151         b2 = new SimpleHistogramBin(1.1, 2.2, false, false);
152         assertTrue(b1.equals(b2));
153         
154         b1.setItemCount(99);
155         assertFalse(b1.equals(b2));
156         b2.setItemCount(99);
157         assertTrue(b1.equals(b2));
158     }
159     
160     /**
161      * Some checks for the clone() method.
162      */

163     public void testCloning() {
164         SimpleHistogramBin b1 = new SimpleHistogramBin(1.1, 2.2, false, true);
165         b1.setItemCount(99);
166         SimpleHistogramBin b2 = null;
167         try {
168             b2 = (SimpleHistogramBin) b1.clone();
169         }
170         catch (CloneNotSupportedException JavaDoc e) {
171             System.err.println("Failed to clone.");
172         }
173         assertTrue(b1 != b2);
174         assertTrue(b1.getClass() == b2.getClass());
175         assertTrue(b1.equals(b2));
176         
177         // check that clone is independent of the original
178
b2.setItemCount(111);
179         assertFalse(b1.equals(b2));
180     }
181     
182     /**
183      * Serialize an instance, restore it, and check for equality.
184      */

185     public void testSerialization() {
186
187         SimpleHistogramBin b1 = new SimpleHistogramBin(1.0, 2.0, false, true);
188         b1.setItemCount(123);
189         SimpleHistogramBin b2 = null;
190         try {
191             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
192             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
193             out.writeObject(b1);
194             out.close();
195             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
196                 new ByteArrayInputStream JavaDoc(buffer.toByteArray())
197             );
198             b2 = (SimpleHistogramBin) in.readObject();
199             in.close();
200         }
201         catch (Exception JavaDoc e) {
202             System.out.println(e.toString());
203         }
204         assertEquals(b1, b2);
205     }
206
207 }
208
Popular Tags