KickJava   Java API By Example, From Geeks To Geeks.

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


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

42
43 package org.jfree.chart.plot.junit;
44
45 import java.awt.BasicStroke JavaDoc;
46 import java.awt.Color JavaDoc;
47 import java.awt.Font JavaDoc;
48 import java.util.Arrays JavaDoc;
49 import java.util.EventListener JavaDoc;
50
51 import junit.framework.Test;
52 import junit.framework.TestCase;
53 import junit.framework.TestSuite;
54
55 import org.jfree.chart.event.MarkerChangeEvent;
56 import org.jfree.chart.event.MarkerChangeListener;
57 import org.jfree.chart.plot.CategoryMarker;
58 import org.jfree.chart.plot.CategoryPlot;
59 import org.jfree.chart.plot.Marker;
60 import org.jfree.chart.plot.ValueMarker;
61 import org.jfree.chart.plot.XYPlot;
62 import org.jfree.ui.LengthAdjustmentType;
63 import org.jfree.ui.RectangleAnchor;
64 import org.jfree.ui.RectangleInsets;
65 import org.jfree.ui.TextAnchor;
66
67 /**
68  * Tests for the {@link Marker} class.
69  */

70 public class MarkerTests extends TestCase implements MarkerChangeListener {
71
72     MarkerChangeEvent lastEvent;
73     
74     /**
75      * Returns the tests as a test suite.
76      *
77      * @return The test suite.
78      */

79     public static Test suite() {
80         return new TestSuite(MarkerTests.class);
81     }
82
83     /**
84      * Constructs a new set of tests.
85      *
86      * @param name the name of the tests.
87      */

88     public MarkerTests(String JavaDoc name) {
89         super(name);
90     }
91
92     /**
93      * Some checks for the getPaint() and setPaint() methods.
94      */

95     public void testGetSetPaint() {
96         // we use ValueMarker for the tests, because we need a concrete
97
// subclass...
98
ValueMarker m = new ValueMarker(1.1);
99         m.addChangeListener(this);
100         this.lastEvent = null;
101         assertEquals(Color.gray, m.getPaint());
102         m.setPaint(Color.blue);
103         assertEquals(Color.blue, m.getPaint());
104         assertEquals(m, this.lastEvent.getMarker());
105         
106         // check null argument...
107
try {
108             m.setPaint(null);
109             fail("Expected an IllegalArgumentException for null.");
110         }
111         catch (IllegalArgumentException JavaDoc e) {
112             assertTrue(true);
113         }
114     }
115     
116     /**
117      * Some checks for the getStroke() and setStroke() methods.
118      */

119     public void testGetSetStroke() {
120         // we use ValueMarker for the tests, because we need a concrete
121
// subclass...
122
ValueMarker m = new ValueMarker(1.1);
123         m.addChangeListener(this);
124         this.lastEvent = null;
125         assertEquals(new BasicStroke JavaDoc(0.5f), m.getStroke());
126         m.setStroke(new BasicStroke JavaDoc(1.1f));
127         assertEquals(new BasicStroke JavaDoc(1.1f), m.getStroke());
128         assertEquals(m, this.lastEvent.getMarker());
129         
130         // check null argument...
131
try {
132             m.setStroke(null);
133             fail("Expected an IllegalArgumentException for null.");
134         }
135         catch (IllegalArgumentException JavaDoc e) {
136             assertTrue(true);
137         }
138     }
139     
140     /**
141      * Some checks for the getOutlinePaint() and setOutlinePaint() methods.
142      */

143     public void testGetSetOutlinePaint() {
144         // we use ValueMarker for the tests, because we need a concrete
145
// subclass...
146
ValueMarker m = new ValueMarker(1.1);
147         m.addChangeListener(this);
148         this.lastEvent = null;
149         assertEquals(Color.gray, m.getOutlinePaint());
150         m.setOutlinePaint(Color.yellow);
151         assertEquals(Color.yellow, m.getOutlinePaint());
152         assertEquals(m, this.lastEvent.getMarker());
153         
154         // check null argument...
155
m.setOutlinePaint(null);
156         assertEquals(null, m.getOutlinePaint());
157     }
158
159     /**
160      * Some checks for the getOutlineStroke() and setOutlineStroke() methods.
161      */

162     public void testGetSetOutlineStroke() {
163         // we use ValueMarker for the tests, because we need a concrete
164
// subclass...
165
ValueMarker m = new ValueMarker(1.1);
166         m.addChangeListener(this);
167         this.lastEvent = null;
168         assertEquals(new BasicStroke JavaDoc(0.5f), m.getOutlineStroke());
169         m.setOutlineStroke(new BasicStroke JavaDoc(1.1f));
170         assertEquals(new BasicStroke JavaDoc(1.1f), m.getOutlineStroke());
171         assertEquals(m, this.lastEvent.getMarker());
172         
173         // check null argument...
174
m.setOutlineStroke(null);
175         assertEquals(null, m.getOutlineStroke());
176     }
177
178     private static final float EPSILON = 0.000000001f;
179     
180     /**
181      * Some checks for the getAlpha() and setAlpha() methods.
182      */

183     public void testGetSetAlpha() {
184         // we use ValueMarker for the tests, because we need a concrete
185
// subclass...
186
ValueMarker m = new ValueMarker(1.1);
187         m.addChangeListener(this);
188         this.lastEvent = null;
189         assertEquals(0.8f, m.getAlpha(), EPSILON);
190         m.setAlpha(0.5f);
191         assertEquals(0.5f, m.getAlpha(), EPSILON);
192         assertEquals(m, this.lastEvent.getMarker());
193     }
194
195     /**
196      * Some checks for the getLabel() and setLabel() methods.
197      */

198     public void testGetSetLabel() {
199         // we use ValueMarker for the tests, because we need a concrete
200
// subclass...
201
ValueMarker m = new ValueMarker(1.1);
202         m.addChangeListener(this);
203         this.lastEvent = null;
204         assertEquals(null, m.getLabel());
205         m.setLabel("XYZ");
206         assertEquals("XYZ", m.getLabel());
207         assertEquals(m, this.lastEvent.getMarker());
208         
209         // check null argument...
210
m.setLabel(null);
211         assertEquals(null, m.getLabel());
212     }
213
214     /**
215      * Some checks for the getLabelFont() and setLabelFont() methods.
216      */

217     public void testGetSetLabelFont() {
218         // we use ValueMarker for the tests, because we need a concrete
219
// subclass...
220
ValueMarker m = new ValueMarker(1.1);
221         m.addChangeListener(this);
222         this.lastEvent = null;
223         assertEquals(new Font JavaDoc("SansSerif", Font.PLAIN, 9), m.getLabelFont());
224         m.setLabelFont(new Font JavaDoc("SansSerif", Font.BOLD, 10));
225         assertEquals(new Font JavaDoc("SansSerif", Font.BOLD, 10), m.getLabelFont());
226         assertEquals(m, this.lastEvent.getMarker());
227         
228         // check null argument...
229
try {
230             m.setLabelFont(null);
231             fail("Expected an IllegalArgumentException for null.");
232         }
233         catch (IllegalArgumentException JavaDoc e) {
234             assertTrue(true);
235         }
236     }
237
238     /**
239      * Some checks for the getLabelPaint() and setLabelPaint() methods.
240      */

241     public void testGetSetLabelPaint() {
242         // we use ValueMarker for the tests, because we need a concrete
243
// subclass...
244
ValueMarker m = new ValueMarker(1.1);
245         m.addChangeListener(this);
246         this.lastEvent = null;
247         assertEquals(Color.black, m.getLabelPaint());
248         m.setLabelPaint(Color.red);
249         assertEquals(Color.red, m.getLabelPaint());
250         assertEquals(m, this.lastEvent.getMarker());
251         
252         // check null argument...
253
try {
254             m.setLabelPaint(null);
255             fail("Expected an IllegalArgumentException for null.");
256         }
257         catch (IllegalArgumentException JavaDoc e) {
258             assertTrue(true);
259         }
260     }
261
262     /**
263      * Some checks for the getLabelAnchor() and setLabelAnchor() methods.
264      */

265     public void testGetSetLabelAnchor() {
266         // we use ValueMarker for the tests, because we need a concrete
267
// subclass...
268
ValueMarker m = new ValueMarker(1.1);
269         m.addChangeListener(this);
270         this.lastEvent = null;
271         assertEquals(RectangleAnchor.TOP_LEFT, m.getLabelAnchor());
272         m.setLabelAnchor(RectangleAnchor.TOP);
273         assertEquals(RectangleAnchor.TOP, m.getLabelAnchor());
274         assertEquals(m, this.lastEvent.getMarker());
275         
276         // check null argument...
277
try {
278             m.setLabelAnchor(null);
279             fail("Expected an IllegalArgumentException for null.");
280         }
281         catch (IllegalArgumentException JavaDoc e) {
282             assertTrue(true);
283         }
284     }
285
286     /**
287      * Some checks for the getLabelOffset() and setLabelOffset() methods.
288      */

289     public void testGetSetLabelOffset() {
290         // we use ValueMarker for the tests, because we need a concrete
291
// subclass...
292
ValueMarker m = new ValueMarker(1.1);
293         m.addChangeListener(this);
294         this.lastEvent = null;
295         assertEquals(new RectangleInsets(3, 3, 3, 3), m.getLabelOffset());
296         m.setLabelOffset(new RectangleInsets(1, 2, 3, 4));
297         assertEquals(new RectangleInsets(1, 2, 3, 4), m.getLabelOffset());
298         assertEquals(m, this.lastEvent.getMarker());
299         
300         // check null argument...
301
try {
302             m.setLabelOffset(null);
303             fail("Expected an IllegalArgumentException for null.");
304         }
305         catch (IllegalArgumentException JavaDoc e) {
306             assertTrue(true);
307         }
308     }
309
310     /**
311      * Some checks for the getLabelOffsetType() and setLabelOffsetType()
312      * methods.
313      */

314     public void testGetSetLabelOffsetType() {
315         // we use ValueMarker for the tests, because we need a concrete
316
// subclass...
317
ValueMarker m = new ValueMarker(1.1);
318         m.addChangeListener(this);
319         this.lastEvent = null;
320         assertEquals(LengthAdjustmentType.CONTRACT, m.getLabelOffsetType());
321         m.setLabelOffsetType(LengthAdjustmentType.EXPAND);
322         assertEquals(LengthAdjustmentType.EXPAND, m.getLabelOffsetType());
323         assertEquals(m, this.lastEvent.getMarker());
324         
325         // check null argument...
326
try {
327             m.setLabelOffsetType(null);
328             fail("Expected an IllegalArgumentException for null.");
329         }
330         catch (IllegalArgumentException JavaDoc e) {
331             assertTrue(true);
332         }
333     }
334
335     /**
336      * Some checks for the getLabelTextAnchor() and setLabelTextAnchor()
337      * methods.
338      */

339     public void testGetSetLabelTextAnchor() {
340         // we use ValueMarker for the tests, because we need a concrete
341
// subclass...
342
ValueMarker m = new ValueMarker(1.1);
343         m.addChangeListener(this);
344         this.lastEvent = null;
345         assertEquals(TextAnchor.CENTER, m.getLabelTextAnchor());
346         m.setLabelTextAnchor(TextAnchor.BASELINE_LEFT);
347         assertEquals(TextAnchor.BASELINE_LEFT, m.getLabelTextAnchor());
348         assertEquals(m, this.lastEvent.getMarker());
349         
350         // check null argument...
351
try {
352             m.setLabelTextAnchor(null);
353             fail("Expected an IllegalArgumentException for null.");
354         }
355         catch (IllegalArgumentException JavaDoc e) {
356             assertTrue(true);
357         }
358     }
359     
360     /**
361      * Checks that a CategoryPlot deregisters listeners when clearing markers.
362      */

363     public void testListenersWithCategoryPlot() {
364         CategoryPlot plot = new CategoryPlot();
365         CategoryMarker marker1 = new CategoryMarker("X");
366         ValueMarker marker2 = new ValueMarker(1.0);
367         plot.addDomainMarker(marker1);
368         plot.addRangeMarker(marker2);
369         EventListener JavaDoc[] listeners1 = marker1.getListeners(
370                 MarkerChangeListener.class);
371         assertTrue(Arrays.asList(listeners1).contains(plot));
372         EventListener JavaDoc[] listeners2 = marker1.getListeners(
373                 MarkerChangeListener.class);
374         assertTrue(Arrays.asList(listeners2).contains(plot));
375         plot.clearDomainMarkers();
376         plot.clearRangeMarkers();
377         listeners1 = marker1.getListeners(MarkerChangeListener.class);
378         assertFalse(Arrays.asList(listeners1).contains(plot));
379         listeners2 = marker1.getListeners(MarkerChangeListener.class);
380         assertFalse(Arrays.asList(listeners2).contains(plot));
381     }
382
383     /**
384      * Checks that an XYPlot deregisters listeners when clearing markers.
385      */

386     public void testListenersWithXYPlot() {
387         XYPlot plot = new XYPlot();
388         ValueMarker marker1 = new ValueMarker(1.0);
389         ValueMarker marker2 = new ValueMarker(2.0);
390         plot.addDomainMarker(marker1);
391         plot.addRangeMarker(marker2);
392         EventListener JavaDoc[] listeners1 = marker1.getListeners(
393                 MarkerChangeListener.class);
394         assertTrue(Arrays.asList(listeners1).contains(plot));
395         EventListener JavaDoc[] listeners2 = marker1.getListeners(
396                 MarkerChangeListener.class);
397         assertTrue(Arrays.asList(listeners2).contains(plot));
398         plot.clearDomainMarkers();
399         plot.clearRangeMarkers();
400         listeners1 = marker1.getListeners(MarkerChangeListener.class);
401         assertFalse(Arrays.asList(listeners1).contains(plot));
402         listeners2 = marker1.getListeners(MarkerChangeListener.class);
403         assertFalse(Arrays.asList(listeners2).contains(plot));
404     }
405
406     public void markerChanged(MarkerChangeEvent event) {
407         this.lastEvent = event;
408     }
409
410 }
411
Popular Tags