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: * IntervalMarkerTests.java
029: * ------------------------
030: * (C) Copyright 2004, 2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: IntervalMarkerTests.java,v 1.1.2.1 2006/10/03 15:41:26 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 14-Jun-2004 : Version 1 (DG);
040: * 05-Sep-2006 : Added checks for MarkerChangeEvents (DG);
041: *
042: */
043:
044: package org.jfree.chart.plot.junit;
045:
046: import java.io.ByteArrayInputStream;
047: import java.io.ByteArrayOutputStream;
048: import java.io.ObjectInput;
049: import java.io.ObjectInputStream;
050: import java.io.ObjectOutput;
051: import java.io.ObjectOutputStream;
052:
053: import junit.framework.Test;
054: import junit.framework.TestCase;
055: import junit.framework.TestSuite;
056:
057: import org.jfree.chart.event.MarkerChangeEvent;
058: import org.jfree.chart.event.MarkerChangeListener;
059: import org.jfree.chart.plot.IntervalMarker;
060: import org.jfree.ui.GradientPaintTransformType;
061: import org.jfree.ui.GradientPaintTransformer;
062: import org.jfree.ui.StandardGradientPaintTransformer;
063:
064: /**
065: * Tests for the {@link IntervalMarker} class.
066: */
067: public class IntervalMarkerTests extends TestCase implements
068: MarkerChangeListener {
069:
070: MarkerChangeEvent lastEvent;
071:
072: public void markerChanged(MarkerChangeEvent event) {
073: this .lastEvent = event;
074: }
075:
076: /**
077: * Returns the tests as a test suite.
078: *
079: * @return The test suite.
080: */
081: public static Test suite() {
082: return new TestSuite(IntervalMarkerTests.class);
083: }
084:
085: /**
086: * Constructs a new set of tests.
087: *
088: * @param name the name of the tests.
089: */
090: public IntervalMarkerTests(String name) {
091: super (name);
092: }
093:
094: /**
095: * Confirm that the equals method can distinguish all the required fields.
096: */
097: public void testEquals() {
098:
099: IntervalMarker m1 = new IntervalMarker(45.0, 50.0);
100: IntervalMarker m2 = new IntervalMarker(45.0, 50.0);
101: assertTrue(m1.equals(m2));
102: assertTrue(m2.equals(m1));
103:
104: m1 = new IntervalMarker(44.0, 50.0);
105: assertFalse(m1.equals(m2));
106: m2 = new IntervalMarker(44.0, 50.0);
107: assertTrue(m1.equals(m2));
108:
109: m1 = new IntervalMarker(44.0, 55.0);
110: assertFalse(m1.equals(m2));
111: m2 = new IntervalMarker(44.0, 55.0);
112: assertTrue(m1.equals(m2));
113:
114: GradientPaintTransformer t = new StandardGradientPaintTransformer(
115: GradientPaintTransformType.HORIZONTAL);
116: m1.setGradientPaintTransformer(t);
117: assertFalse(m1.equals(m2));
118: m2.setGradientPaintTransformer(t);
119: assertTrue(m1.equals(m2));
120:
121: }
122:
123: /**
124: * Confirm that cloning works.
125: */
126: public void testCloning() {
127: IntervalMarker m1 = new IntervalMarker(45.0, 50.0);
128: IntervalMarker m2 = null;
129: try {
130: m2 = (IntervalMarker) m1.clone();
131: } catch (CloneNotSupportedException e) {
132: e.printStackTrace();
133: }
134: assertTrue(m1 != m2);
135: assertTrue(m1.getClass() == m2.getClass());
136: assertTrue(m1.equals(m2));
137: }
138:
139: /**
140: * Serialize an instance, restore it, and check for equality.
141: */
142: public void testSerialization() {
143:
144: IntervalMarker m1 = new IntervalMarker(45.0, 50.0);
145: IntervalMarker m2 = null;
146: try {
147: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
148: ObjectOutput out = new ObjectOutputStream(buffer);
149: out.writeObject(m1);
150: out.close();
151:
152: ObjectInput in = new ObjectInputStream(
153: new ByteArrayInputStream(buffer.toByteArray()));
154: m2 = (IntervalMarker) in.readObject();
155: in.close();
156: } catch (Exception e) {
157: e.printStackTrace();
158: }
159: boolean b = m1.equals(m2);
160: assertTrue(b);
161:
162: }
163:
164: private static final double EPSILON = 0.0000000001;
165:
166: /**
167: * Some checks for the getStartValue() and setStartValue() methods.
168: */
169: public void testGetSetStartValue() {
170: IntervalMarker m = new IntervalMarker(1.0, 2.0);
171: m.addChangeListener(this );
172: this .lastEvent = null;
173: assertEquals(1.0, m.getStartValue(), EPSILON);
174: m.setStartValue(0.5);
175: assertEquals(0.5, m.getStartValue(), EPSILON);
176: assertEquals(m, this .lastEvent.getMarker());
177: }
178:
179: /**
180: * Some checks for the getEndValue() and setEndValue() methods.
181: */
182: public void testGetSetEndValue() {
183: IntervalMarker m = new IntervalMarker(1.0, 2.0);
184: m.addChangeListener(this );
185: this .lastEvent = null;
186: assertEquals(2.0, m.getEndValue(), EPSILON);
187: m.setEndValue(0.5);
188: assertEquals(0.5, m.getEndValue(), EPSILON);
189: assertEquals(m, this.lastEvent.getMarker());
190: }
191: }
|