001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ----------------
028: * MarkerTests.java
029: * ----------------
030: * (C) Copyright 2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: MarkerTests.java,v 1.1.2.1 2006/10/03 15:41:26 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 05-Sep-2006 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.plot.junit;
044:
045: import java.awt.BasicStroke;
046: import java.awt.Color;
047: import java.awt.Font;
048: import java.util.Arrays;
049: import java.util.EventListener;
050:
051: import junit.framework.Test;
052: import junit.framework.TestCase;
053: import junit.framework.TestSuite;
054:
055: import org.jfree.chart.event.MarkerChangeEvent;
056: import org.jfree.chart.event.MarkerChangeListener;
057: import org.jfree.chart.plot.CategoryMarker;
058: import org.jfree.chart.plot.CategoryPlot;
059: import org.jfree.chart.plot.Marker;
060: import org.jfree.chart.plot.ValueMarker;
061: import org.jfree.chart.plot.XYPlot;
062: import org.jfree.ui.LengthAdjustmentType;
063: import org.jfree.ui.RectangleAnchor;
064: import org.jfree.ui.RectangleInsets;
065: import org.jfree.ui.TextAnchor;
066:
067: /**
068: * Tests for the {@link Marker} class.
069: */
070: public class MarkerTests extends TestCase implements
071: MarkerChangeListener {
072:
073: MarkerChangeEvent lastEvent;
074:
075: /**
076: * Returns the tests as a test suite.
077: *
078: * @return The test suite.
079: */
080: public static Test suite() {
081: return new TestSuite(MarkerTests.class);
082: }
083:
084: /**
085: * Constructs a new set of tests.
086: *
087: * @param name the name of the tests.
088: */
089: public MarkerTests(String name) {
090: super (name);
091: }
092:
093: /**
094: * Some checks for the getPaint() and setPaint() methods.
095: */
096: public void testGetSetPaint() {
097: // we use ValueMarker for the tests, because we need a concrete
098: // subclass...
099: ValueMarker m = new ValueMarker(1.1);
100: m.addChangeListener(this );
101: this .lastEvent = null;
102: assertEquals(Color.gray, m.getPaint());
103: m.setPaint(Color.blue);
104: assertEquals(Color.blue, m.getPaint());
105: assertEquals(m, this .lastEvent.getMarker());
106:
107: // check null argument...
108: try {
109: m.setPaint(null);
110: fail("Expected an IllegalArgumentException for null.");
111: } catch (IllegalArgumentException 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(0.5f), m.getStroke());
126: m.setStroke(new BasicStroke(1.1f));
127: assertEquals(new BasicStroke(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: } catch (IllegalArgumentException e) {
135: assertTrue(true);
136: }
137: }
138:
139: /**
140: * Some checks for the getOutlinePaint() and setOutlinePaint() methods.
141: */
142: public void testGetSetOutlinePaint() {
143: // we use ValueMarker for the tests, because we need a concrete
144: // subclass...
145: ValueMarker m = new ValueMarker(1.1);
146: m.addChangeListener(this );
147: this .lastEvent = null;
148: assertEquals(Color.gray, m.getOutlinePaint());
149: m.setOutlinePaint(Color.yellow);
150: assertEquals(Color.yellow, m.getOutlinePaint());
151: assertEquals(m, this .lastEvent.getMarker());
152:
153: // check null argument...
154: m.setOutlinePaint(null);
155: assertEquals(null, m.getOutlinePaint());
156: }
157:
158: /**
159: * Some checks for the getOutlineStroke() and setOutlineStroke() methods.
160: */
161: public void testGetSetOutlineStroke() {
162: // we use ValueMarker for the tests, because we need a concrete
163: // subclass...
164: ValueMarker m = new ValueMarker(1.1);
165: m.addChangeListener(this );
166: this .lastEvent = null;
167: assertEquals(new BasicStroke(0.5f), m.getOutlineStroke());
168: m.setOutlineStroke(new BasicStroke(1.1f));
169: assertEquals(new BasicStroke(1.1f), m.getOutlineStroke());
170: assertEquals(m, this .lastEvent.getMarker());
171:
172: // check null argument...
173: m.setOutlineStroke(null);
174: assertEquals(null, m.getOutlineStroke());
175: }
176:
177: private static final float EPSILON = 0.000000001f;
178:
179: /**
180: * Some checks for the getAlpha() and setAlpha() methods.
181: */
182: public void testGetSetAlpha() {
183: // we use ValueMarker for the tests, because we need a concrete
184: // subclass...
185: ValueMarker m = new ValueMarker(1.1);
186: m.addChangeListener(this );
187: this .lastEvent = null;
188: assertEquals(0.8f, m.getAlpha(), EPSILON);
189: m.setAlpha(0.5f);
190: assertEquals(0.5f, m.getAlpha(), EPSILON);
191: assertEquals(m, this .lastEvent.getMarker());
192: }
193:
194: /**
195: * Some checks for the getLabel() and setLabel() methods.
196: */
197: public void testGetSetLabel() {
198: // we use ValueMarker for the tests, because we need a concrete
199: // subclass...
200: ValueMarker m = new ValueMarker(1.1);
201: m.addChangeListener(this );
202: this .lastEvent = null;
203: assertEquals(null, m.getLabel());
204: m.setLabel("XYZ");
205: assertEquals("XYZ", m.getLabel());
206: assertEquals(m, this .lastEvent.getMarker());
207:
208: // check null argument...
209: m.setLabel(null);
210: assertEquals(null, m.getLabel());
211: }
212:
213: /**
214: * Some checks for the getLabelFont() and setLabelFont() methods.
215: */
216: public void testGetSetLabelFont() {
217: // we use ValueMarker for the tests, because we need a concrete
218: // subclass...
219: ValueMarker m = new ValueMarker(1.1);
220: m.addChangeListener(this );
221: this .lastEvent = null;
222: assertEquals(new Font("SansSerif", Font.PLAIN, 9), m
223: .getLabelFont());
224: m.setLabelFont(new Font("SansSerif", Font.BOLD, 10));
225: assertEquals(new Font("SansSerif", Font.BOLD, 10), m
226: .getLabelFont());
227: assertEquals(m, this .lastEvent.getMarker());
228:
229: // check null argument...
230: try {
231: m.setLabelFont(null);
232: fail("Expected an IllegalArgumentException for null.");
233: } catch (IllegalArgumentException 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: } catch (IllegalArgumentException e) {
257: assertTrue(true);
258: }
259: }
260:
261: /**
262: * Some checks for the getLabelAnchor() and setLabelAnchor() methods.
263: */
264: public void testGetSetLabelAnchor() {
265: // we use ValueMarker for the tests, because we need a concrete
266: // subclass...
267: ValueMarker m = new ValueMarker(1.1);
268: m.addChangeListener(this );
269: this .lastEvent = null;
270: assertEquals(RectangleAnchor.TOP_LEFT, m.getLabelAnchor());
271: m.setLabelAnchor(RectangleAnchor.TOP);
272: assertEquals(RectangleAnchor.TOP, m.getLabelAnchor());
273: assertEquals(m, this .lastEvent.getMarker());
274:
275: // check null argument...
276: try {
277: m.setLabelAnchor(null);
278: fail("Expected an IllegalArgumentException for null.");
279: } catch (IllegalArgumentException e) {
280: assertTrue(true);
281: }
282: }
283:
284: /**
285: * Some checks for the getLabelOffset() and setLabelOffset() methods.
286: */
287: public void testGetSetLabelOffset() {
288: // we use ValueMarker for the tests, because we need a concrete
289: // subclass...
290: ValueMarker m = new ValueMarker(1.1);
291: m.addChangeListener(this );
292: this .lastEvent = null;
293: assertEquals(new RectangleInsets(3, 3, 3, 3), m
294: .getLabelOffset());
295: m.setLabelOffset(new RectangleInsets(1, 2, 3, 4));
296: assertEquals(new RectangleInsets(1, 2, 3, 4), m
297: .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: } catch (IllegalArgumentException e) {
305: assertTrue(true);
306: }
307: }
308:
309: /**
310: * Some checks for the getLabelOffsetType() and setLabelOffsetType()
311: * methods.
312: */
313: public void testGetSetLabelOffsetType() {
314: // we use ValueMarker for the tests, because we need a concrete
315: // subclass...
316: ValueMarker m = new ValueMarker(1.1);
317: m.addChangeListener(this );
318: this .lastEvent = null;
319: assertEquals(LengthAdjustmentType.CONTRACT, m
320: .getLabelOffsetType());
321: m.setLabelOffsetType(LengthAdjustmentType.EXPAND);
322: assertEquals(LengthAdjustmentType.EXPAND, m
323: .getLabelOffsetType());
324: assertEquals(m, this .lastEvent.getMarker());
325:
326: // check null argument...
327: try {
328: m.setLabelOffsetType(null);
329: fail("Expected an IllegalArgumentException for null.");
330: } catch (IllegalArgumentException 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: } catch (IllegalArgumentException e) {
355: assertTrue(true);
356: }
357: }
358:
359: /**
360: * Checks that a CategoryPlot deregisters listeners when clearing markers.
361: */
362: public void testListenersWithCategoryPlot() {
363: CategoryPlot plot = new CategoryPlot();
364: CategoryMarker marker1 = new CategoryMarker("X");
365: ValueMarker marker2 = new ValueMarker(1.0);
366: plot.addDomainMarker(marker1);
367: plot.addRangeMarker(marker2);
368: EventListener[] listeners1 = marker1
369: .getListeners(MarkerChangeListener.class);
370: assertTrue(Arrays.asList(listeners1).contains(plot));
371: EventListener[] listeners2 = marker1
372: .getListeners(MarkerChangeListener.class);
373: assertTrue(Arrays.asList(listeners2).contains(plot));
374: plot.clearDomainMarkers();
375: plot.clearRangeMarkers();
376: listeners1 = marker1.getListeners(MarkerChangeListener.class);
377: assertFalse(Arrays.asList(listeners1).contains(plot));
378: listeners2 = marker1.getListeners(MarkerChangeListener.class);
379: assertFalse(Arrays.asList(listeners2).contains(plot));
380: }
381:
382: /**
383: * Checks that an XYPlot deregisters listeners when clearing markers.
384: */
385: public void testListenersWithXYPlot() {
386: XYPlot plot = new XYPlot();
387: ValueMarker marker1 = new ValueMarker(1.0);
388: ValueMarker marker2 = new ValueMarker(2.0);
389: plot.addDomainMarker(marker1);
390: plot.addRangeMarker(marker2);
391: EventListener[] listeners1 = marker1
392: .getListeners(MarkerChangeListener.class);
393: assertTrue(Arrays.asList(listeners1).contains(plot));
394: EventListener[] listeners2 = marker1
395: .getListeners(MarkerChangeListener.class);
396: assertTrue(Arrays.asList(listeners2).contains(plot));
397: plot.clearDomainMarkers();
398: plot.clearRangeMarkers();
399: listeners1 = marker1.getListeners(MarkerChangeListener.class);
400: assertFalse(Arrays.asList(listeners1).contains(plot));
401: listeners2 = marker1.getListeners(MarkerChangeListener.class);
402: assertFalse(Arrays.asList(listeners2).contains(plot));
403: }
404:
405: public void markerChanged(MarkerChangeEvent event) {
406: this.lastEvent = event;
407: }
408:
409: }
|