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: * ValueMarkerTests.java
029: * ---------------------
030: * (C) Copyright 2003-2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: ValueMarkerTests.java,v 1.1.2.1 2006/10/03 15:41:25 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 18-Aug-2003 : Version 1 (DG);
040: * 14-Jun-2004 : Renamed MarkerTests --> ValueMarkerTests (DG);
041: * 01-Jun-2005 : Strengthened equals() test (DG);
042: * 05-Sep-2006 : Added checks for MarkerChangeEvent generation (DG);
043: *
044: */
045:
046: package org.jfree.chart.plot.junit;
047:
048: import java.awt.BasicStroke;
049: import java.awt.Color;
050: import java.awt.Font;
051: import java.awt.GradientPaint;
052: import java.io.ByteArrayInputStream;
053: import java.io.ByteArrayOutputStream;
054: import java.io.ObjectInput;
055: import java.io.ObjectInputStream;
056: import java.io.ObjectOutput;
057: import java.io.ObjectOutputStream;
058:
059: import junit.framework.Test;
060: import junit.framework.TestCase;
061: import junit.framework.TestSuite;
062:
063: import org.jfree.chart.event.MarkerChangeEvent;
064: import org.jfree.chart.event.MarkerChangeListener;
065: import org.jfree.chart.plot.Marker;
066: import org.jfree.chart.plot.ValueMarker;
067: import org.jfree.ui.LengthAdjustmentType;
068: import org.jfree.ui.RectangleAnchor;
069: import org.jfree.ui.RectangleInsets;
070: import org.jfree.ui.TextAnchor;
071:
072: /**
073: * Tests for the {@link ValueMarker} class.
074: */
075: public class ValueMarkerTests extends TestCase implements
076: MarkerChangeListener {
077:
078: MarkerChangeEvent lastEvent;
079:
080: /**
081: * Returns the tests as a test suite.
082: *
083: * @return The test suite.
084: */
085: public static Test suite() {
086: return new TestSuite(ValueMarkerTests.class);
087: }
088:
089: /**
090: * Constructs a new set of tests.
091: *
092: * @param name the name of the tests.
093: */
094: public ValueMarkerTests(String name) {
095: super (name);
096: }
097:
098: /**
099: * Confirm that the equals method can distinguish all the required fields.
100: */
101: public void testEquals() {
102:
103: Marker m1 = new ValueMarker(45.0);
104: Marker m2 = new ValueMarker(45.0);
105: assertTrue(m1.equals(m2));
106: assertTrue(m2.equals(m1));
107:
108: m1.setPaint(new GradientPaint(1.0f, 2.0f, Color.green, 3.0f,
109: 4.0f, Color.red));
110: assertFalse(m1.equals(m2));
111: m2.setPaint(new GradientPaint(1.0f, 2.0f, Color.green, 3.0f,
112: 4.0f, Color.red));
113: assertTrue(m1.equals(m2));
114:
115: BasicStroke stroke = new BasicStroke(2.2f);
116: m1.setStroke(stroke);
117: assertFalse(m1.equals(m2));
118: m2.setStroke(stroke);
119: assertTrue(m1.equals(m2));
120:
121: m1.setOutlinePaint(new GradientPaint(4.0f, 3.0f, Color.yellow,
122: 2.0f, 1.0f, Color.white));
123: assertFalse(m1.equals(m2));
124: m2.setOutlinePaint(new GradientPaint(4.0f, 3.0f, Color.yellow,
125: 2.0f, 1.0f, Color.white));
126: assertTrue(m1.equals(m2));
127:
128: m1.setOutlineStroke(stroke);
129: assertFalse(m1.equals(m2));
130: m2.setOutlineStroke(stroke);
131: assertTrue(m1.equals(m2));
132:
133: m1.setAlpha(0.1f);
134: assertFalse(m1.equals(m2));
135: m2.setAlpha(0.1f);
136: assertTrue(m1.equals(m2));
137:
138: m1.setLabel("New Label");
139: assertFalse(m1.equals(m2));
140: m2.setLabel("New Label");
141: assertTrue(m1.equals(m2));
142:
143: m1.setLabelFont(new Font("SansSerif", Font.PLAIN, 10));
144: assertFalse(m1.equals(m2));
145: m2.setLabelFont(new Font("SansSerif", Font.PLAIN, 10));
146: assertTrue(m1.equals(m2));
147:
148: m1.setLabelPaint(new GradientPaint(1.0f, 2.0f, Color.blue,
149: 3.0f, 4.0f, Color.yellow));
150: assertFalse(m1.equals(m2));
151: m2.setLabelPaint(new GradientPaint(1.0f, 2.0f, Color.blue,
152: 3.0f, 4.0f, Color.yellow));
153: assertTrue(m1.equals(m2));
154:
155: m1.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
156: assertFalse(m1.equals(m2));
157: m2.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
158: assertTrue(m1.equals(m2));
159:
160: m1.setLabelTextAnchor(TextAnchor.BASELINE_RIGHT);
161: assertFalse(m1.equals(m2));
162: m2.setLabelTextAnchor(TextAnchor.BASELINE_RIGHT);
163: assertTrue(m1.equals(m2));
164:
165: m1.setLabelOffset(new RectangleInsets(10.0, 10.0, 10.0, 10.0));
166: assertFalse(m1.equals(m2));
167: m2.setLabelOffset(new RectangleInsets(10.0, 10.0, 10.0, 10.0));
168: assertTrue(m1.equals(m2));
169:
170: m1.setLabelOffsetType(LengthAdjustmentType.EXPAND);
171: assertFalse(m1.equals(m2));
172: m2.setLabelOffsetType(LengthAdjustmentType.EXPAND);
173: assertTrue(m1.equals(m2));
174:
175: m1 = new ValueMarker(12.3);
176: m2 = new ValueMarker(45.6);
177: assertFalse(m1.equals(m2));
178: m2 = new ValueMarker(12.3);
179: assertTrue(m1.equals(m2));
180:
181: }
182:
183: /**
184: * Confirm that cloning works.
185: */
186: public void testCloning() {
187: ValueMarker m1 = new ValueMarker(25.0);
188: ValueMarker m2 = null;
189: try {
190: m2 = (ValueMarker) m1.clone();
191: } catch (CloneNotSupportedException e) {
192: e.printStackTrace();
193: }
194: assertTrue(m1 != m2);
195: assertTrue(m1.getClass() == m2.getClass());
196: assertTrue(m1.equals(m2));
197: }
198:
199: /**
200: * Serialize an instance, restore it, and check for equality.
201: */
202: public void testSerialization() {
203:
204: ValueMarker m1 = new ValueMarker(25.0);
205: ValueMarker m2 = null;
206:
207: try {
208: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
209: ObjectOutput out = new ObjectOutputStream(buffer);
210: out.writeObject(m1);
211: out.close();
212:
213: ObjectInput in = new ObjectInputStream(
214: new ByteArrayInputStream(buffer.toByteArray()));
215: m2 = (ValueMarker) in.readObject();
216: in.close();
217: } catch (Exception e) {
218: e.printStackTrace();
219: }
220: boolean b = m1.equals(m2);
221: assertTrue(b);
222:
223: }
224:
225: private static final double EPSILON = 0.000000001;
226:
227: /**
228: * Some checks for the getValue() and setValue() methods.
229: */
230: public void testGetSetValue() {
231: ValueMarker m = new ValueMarker(1.1);
232: m.addChangeListener(this );
233: this .lastEvent = null;
234: assertEquals(1.1, m.getValue(), EPSILON);
235: m.setValue(33.3);
236: assertEquals(33.3, m.getValue(), EPSILON);
237: assertEquals(m, this .lastEvent.getMarker());
238: }
239:
240: public void markerChanged(MarkerChangeEvent event) {
241: this.lastEvent = event;
242: }
243:
244: }
|