KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ValueMarkerTests.java
29  * ---------------------
30  * (C) Copyright 2003-2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: ValueMarkerTests.java,v 1.1.2.1 2006/10/03 15:41:25 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 18-Aug-2003 : Version 1 (DG);
40  * 14-Jun-2004 : Renamed MarkerTests --> ValueMarkerTests (DG);
41  * 01-Jun-2005 : Strengthened equals() test (DG);
42  * 05-Sep-2006 : Added checks for MarkerChangeEvent generation (DG);
43  *
44  */

45
46 package org.jfree.chart.plot.junit;
47
48 import java.awt.BasicStroke JavaDoc;
49 import java.awt.Color JavaDoc;
50 import java.awt.Font JavaDoc;
51 import java.awt.GradientPaint JavaDoc;
52 import java.io.ByteArrayInputStream JavaDoc;
53 import java.io.ByteArrayOutputStream JavaDoc;
54 import java.io.ObjectInput JavaDoc;
55 import java.io.ObjectInputStream JavaDoc;
56 import java.io.ObjectOutput JavaDoc;
57 import java.io.ObjectOutputStream JavaDoc;
58
59 import junit.framework.Test;
60 import junit.framework.TestCase;
61 import junit.framework.TestSuite;
62
63 import org.jfree.chart.event.MarkerChangeEvent;
64 import org.jfree.chart.event.MarkerChangeListener;
65 import org.jfree.chart.plot.Marker;
66 import org.jfree.chart.plot.ValueMarker;
67 import org.jfree.ui.LengthAdjustmentType;
68 import org.jfree.ui.RectangleAnchor;
69 import org.jfree.ui.RectangleInsets;
70 import org.jfree.ui.TextAnchor;
71
72 /**
73  * Tests for the {@link ValueMarker} class.
74  */

75 public class ValueMarkerTests
76     extends TestCase
77     implements MarkerChangeListener {
78
79     
80     MarkerChangeEvent lastEvent;
81     
82     /**
83      * Returns the tests as a test suite.
84      *
85      * @return The test suite.
86      */

87     public static Test suite() {
88         return new TestSuite(ValueMarkerTests.class);
89     }
90
91     /**
92      * Constructs a new set of tests.
93      *
94      * @param name the name of the tests.
95      */

96     public ValueMarkerTests(String JavaDoc name) {
97         super(name);
98     }
99
100     /**
101      * Confirm that the equals method can distinguish all the required fields.
102      */

103     public void testEquals() {
104         
105         Marker m1 = new ValueMarker(45.0);
106         Marker m2 = new ValueMarker(45.0);
107         assertTrue(m1.equals(m2));
108         assertTrue(m2.equals(m1));
109         
110         m1.setPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.green,
111                 3.0f, 4.0f, Color.red));
112         assertFalse(m1.equals(m2));
113         m2.setPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.green,
114                 3.0f, 4.0f, Color.red));
115         assertTrue(m1.equals(m2));
116         
117         BasicStroke JavaDoc stroke = new BasicStroke JavaDoc(2.2f);
118         m1.setStroke(stroke);
119         assertFalse(m1.equals(m2));
120         m2.setStroke(stroke);
121         assertTrue(m1.equals(m2));
122         
123         m1.setOutlinePaint(new GradientPaint JavaDoc(4.0f, 3.0f, Color.yellow,
124                 2.0f, 1.0f, Color.white));
125         assertFalse(m1.equals(m2));
126         m2.setOutlinePaint(new GradientPaint JavaDoc(4.0f, 3.0f, Color.yellow,
127                 2.0f, 1.0f, Color.white));
128         assertTrue(m1.equals(m2));
129         
130         m1.setOutlineStroke(stroke);
131         assertFalse(m1.equals(m2));
132         m2.setOutlineStroke(stroke);
133         assertTrue(m1.equals(m2));
134
135         m1.setAlpha(0.1f);
136         assertFalse(m1.equals(m2));
137         m2.setAlpha(0.1f);
138         assertTrue(m1.equals(m2));
139         
140         m1.setLabel("New Label");
141         assertFalse(m1.equals(m2));
142         m2.setLabel("New Label");
143         assertTrue(m1.equals(m2));
144         
145         m1.setLabelFont(new Font JavaDoc("SansSerif", Font.PLAIN, 10));
146         assertFalse(m1.equals(m2));
147         m2.setLabelFont(new Font JavaDoc("SansSerif", Font.PLAIN, 10));
148         assertTrue(m1.equals(m2));
149
150         m1.setLabelPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.blue,
151                 3.0f, 4.0f, Color.yellow));
152         assertFalse(m1.equals(m2));
153         m2.setLabelPaint(new GradientPaint JavaDoc(1.0f, 2.0f, Color.blue,
154                 3.0f, 4.0f, Color.yellow));
155         assertTrue(m1.equals(m2));
156
157         m1.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
158         assertFalse(m1.equals(m2));
159         m2.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
160         assertTrue(m1.equals(m2));
161         
162         m1.setLabelTextAnchor(TextAnchor.BASELINE_RIGHT);
163         assertFalse(m1.equals(m2));
164         m2.setLabelTextAnchor(TextAnchor.BASELINE_RIGHT);
165         assertTrue(m1.equals(m2));
166         
167         m1.setLabelOffset(new RectangleInsets(10.0, 10.0, 10.0, 10.0));
168         assertFalse(m1.equals(m2));
169         m2.setLabelOffset(new RectangleInsets(10.0, 10.0, 10.0, 10.0));
170         assertTrue(m1.equals(m2));
171         
172         m1.setLabelOffsetType(LengthAdjustmentType.EXPAND);
173         assertFalse(m1.equals(m2));
174         m2.setLabelOffsetType(LengthAdjustmentType.EXPAND);
175         assertTrue(m1.equals(m2));
176         
177         m1 = new ValueMarker(12.3);
178         m2 = new ValueMarker(45.6);
179         assertFalse(m1.equals(m2));
180         m2 = new ValueMarker(12.3);
181         assertTrue(m1.equals(m2));
182         
183     }
184         
185     /**
186      * Confirm that cloning works.
187      */

188     public void testCloning() {
189         ValueMarker m1 = new ValueMarker(25.0);
190         ValueMarker m2 = null;
191         try {
192             m2 = (ValueMarker) m1.clone();
193         }
194         catch (CloneNotSupportedException JavaDoc e) {
195             e.printStackTrace();
196         }
197         assertTrue(m1 != m2);
198         assertTrue(m1.getClass() == m2.getClass());
199         assertTrue(m1.equals(m2));
200     }
201
202    /**
203      * Serialize an instance, restore it, and check for equality.
204      */

205     public void testSerialization() {
206
207         ValueMarker m1 = new ValueMarker(25.0);
208         ValueMarker m2 = null;
209
210         try {
211             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
212             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
213             out.writeObject(m1);
214             out.close();
215
216             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
217                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
218             m2 = (ValueMarker) in.readObject();
219             in.close();
220         }
221         catch (Exception JavaDoc e) {
222             e.printStackTrace();
223         }
224         boolean b = m1.equals(m2);
225         assertTrue(b);
226
227     }
228
229     private static final double EPSILON = 0.000000001;
230     
231     /**
232      * Some checks for the getValue() and setValue() methods.
233      */

234     public void testGetSetValue() {
235         ValueMarker m = new ValueMarker(1.1);
236         m.addChangeListener(this);
237         this.lastEvent = null;
238         assertEquals(1.1, m.getValue(), EPSILON);
239         m.setValue(33.3);
240         assertEquals(33.3, m.getValue(), EPSILON);
241         assertEquals(m, this.lastEvent.getMarker());
242     }
243
244     public void markerChanged(MarkerChangeEvent event) {
245         this.lastEvent = event;
246     }
247
248 }
249
Popular Tags