KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > plot > junit > IntervalMarkerTests


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, 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  * IntervalMarkerTests.java
29  * ------------------------
30  * (C) Copyright 2004, 2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: IntervalMarkerTests.java,v 1.1.2.1 2006/10/03 15:41:26 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 14-Jun-2004 : Version 1 (DG);
40  * 05-Sep-2006 : Added checks for MarkerChangeEvents (DG);
41  *
42  */

43
44 package org.jfree.chart.plot.junit;
45
46 import java.io.ByteArrayInputStream JavaDoc;
47 import java.io.ByteArrayOutputStream JavaDoc;
48 import java.io.ObjectInput JavaDoc;
49 import java.io.ObjectInputStream JavaDoc;
50 import java.io.ObjectOutput JavaDoc;
51 import java.io.ObjectOutputStream JavaDoc;
52
53 import junit.framework.Test;
54 import junit.framework.TestCase;
55 import junit.framework.TestSuite;
56
57 import org.jfree.chart.event.MarkerChangeEvent;
58 import org.jfree.chart.event.MarkerChangeListener;
59 import org.jfree.chart.plot.IntervalMarker;
60 import org.jfree.ui.GradientPaintTransformType;
61 import org.jfree.ui.GradientPaintTransformer;
62 import org.jfree.ui.StandardGradientPaintTransformer;
63
64 /**
65  * Tests for the {@link IntervalMarker} class.
66  */

67 public class IntervalMarkerTests
68     extends TestCase
69     implements MarkerChangeListener {
70
71     MarkerChangeEvent lastEvent;
72     
73     public void markerChanged(MarkerChangeEvent event) {
74         this.lastEvent = event;
75     }
76
77     /**
78      * Returns the tests as a test suite.
79      *
80      * @return The test suite.
81      */

82     public static Test suite() {
83         return new TestSuite(IntervalMarkerTests.class);
84     }
85
86     /**
87      * Constructs a new set of tests.
88      *
89      * @param name the name of the tests.
90      */

91     public IntervalMarkerTests(String JavaDoc name) {
92         super(name);
93     }
94
95     /**
96      * Confirm that the equals method can distinguish all the required fields.
97      */

98     public void testEquals() {
99         
100         IntervalMarker m1 = new IntervalMarker(45.0, 50.0);
101         IntervalMarker m2 = new IntervalMarker(45.0, 50.0);
102         assertTrue(m1.equals(m2));
103         assertTrue(m2.equals(m1));
104         
105         m1 = new IntervalMarker(44.0, 50.0);
106         assertFalse(m1.equals(m2));
107         m2 = new IntervalMarker(44.0, 50.0);
108         assertTrue(m1.equals(m2));
109         
110         m1 = new IntervalMarker(44.0, 55.0);
111         assertFalse(m1.equals(m2));
112         m2 = new IntervalMarker(44.0, 55.0);
113         assertTrue(m1.equals(m2));
114        
115         GradientPaintTransformer t = new StandardGradientPaintTransformer(
116                 GradientPaintTransformType.HORIZONTAL);
117         m1.setGradientPaintTransformer(t);
118         assertFalse(m1.equals(m2));
119         m2.setGradientPaintTransformer(t);
120         assertTrue(m1.equals(m2));
121         
122     }
123         
124     /**
125      * Confirm that cloning works.
126      */

127     public void testCloning() {
128         IntervalMarker m1 = new IntervalMarker(45.0, 50.0);
129         IntervalMarker m2 = null;
130         try {
131             m2 = (IntervalMarker) m1.clone();
132         }
133         catch (CloneNotSupportedException JavaDoc e) {
134             e.printStackTrace();
135         }
136         assertTrue(m1 != m2);
137         assertTrue(m1.getClass() == m2.getClass());
138         assertTrue(m1.equals(m2));
139     }
140
141    /**
142      * Serialize an instance, restore it, and check for equality.
143      */

144     public void testSerialization() {
145
146         IntervalMarker m1 = new IntervalMarker(45.0, 50.0);
147         IntervalMarker m2 = null;
148         try {
149             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
150             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
151             out.writeObject(m1);
152             out.close();
153
154             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
155                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
156             m2 = (IntervalMarker) in.readObject();
157             in.close();
158         }
159         catch (Exception JavaDoc e) {
160             e.printStackTrace();
161         }
162         boolean b = m1.equals(m2);
163         assertTrue(b);
164
165     }
166
167     private static final double EPSILON = 0.0000000001;
168    
169     /**
170      * Some checks for the getStartValue() and setStartValue() methods.
171      */

172     public void testGetSetStartValue() {
173         IntervalMarker m = new IntervalMarker(1.0, 2.0);
174         m.addChangeListener(this);
175         this.lastEvent = null;
176         assertEquals(1.0, m.getStartValue(), EPSILON);
177         m.setStartValue(0.5);
178         assertEquals(0.5, m.getStartValue(), EPSILON);
179         assertEquals(m, this.lastEvent.getMarker());
180     }
181
182     /**
183      * Some checks for the getEndValue() and setEndValue() methods.
184      */

185     public void testGetSetEndValue() {
186         IntervalMarker m = new IntervalMarker(1.0, 2.0);
187         m.addChangeListener(this);
188         this.lastEvent = null;
189         assertEquals(2.0, m.getEndValue(), EPSILON);
190         m.setEndValue(0.5);
191         assertEquals(0.5, m.getEndValue(), EPSILON);
192         assertEquals(m, this.lastEvent.getMarker());
193     }
194 }
195
Popular Tags