001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, 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: * LookupPaintScaleTests.java
029: * --------------------------
030: * (C) Copyright 2006, 2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: LookupPaintScaleTests.java,v 1.1.2.3 2007/03/09 15:59:21 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 05-Jul-2006 : Version 1 (DG);
040: * 31-Jan-2007 : Additional serialization tests (DG);
041: * 07-Mar-2007 : Added new tests (DG);
042: * 09-Mar-2007 : Check independence in testCloning() (DG);
043: *
044: */
045:
046: package org.jfree.chart.renderer.junit;
047:
048: import java.awt.Color;
049: import java.awt.GradientPaint;
050: import java.io.ByteArrayInputStream;
051: import java.io.ByteArrayOutputStream;
052: import java.io.ObjectInput;
053: import java.io.ObjectInputStream;
054: import java.io.ObjectOutput;
055: import java.io.ObjectOutputStream;
056:
057: import junit.framework.Test;
058: import junit.framework.TestCase;
059: import junit.framework.TestSuite;
060:
061: import org.jfree.chart.renderer.LookupPaintScale;
062:
063: /**
064: * Tests for the {@link LookupPaintScale} class.
065: */
066: public class LookupPaintScaleTests extends TestCase {
067:
068: /**
069: * Returns the tests as a test suite.
070: *
071: * @return The test suite.
072: */
073: public static Test suite() {
074: return new TestSuite(LookupPaintScaleTests.class);
075: }
076:
077: /**
078: * Constructs a new set of tests.
079: *
080: * @param name the name of the tests.
081: */
082: public LookupPaintScaleTests(String name) {
083: super (name);
084: }
085:
086: /**
087: * A test for the equals() method.
088: */
089: public void testEquals() {
090: LookupPaintScale g1 = new LookupPaintScale();
091: LookupPaintScale g2 = new LookupPaintScale();
092: assertTrue(g1.equals(g2));
093: assertTrue(g2.equals(g1));
094:
095: g1 = new LookupPaintScale(1.0, 2.0, Color.red);
096: assertFalse(g1.equals(g2));
097: g2 = new LookupPaintScale(1.0, 2.0, Color.red);
098: assertTrue(g1.equals(g2));
099:
100: g1.add(new Double(1.5), new GradientPaint(1.0f, 2.0f,
101: Color.red, 3.0f, 4.0f, Color.blue));
102: assertFalse(g1.equals(g2));
103: g2.add(new Double(1.5), new GradientPaint(1.0f, 2.0f,
104: Color.red, 3.0f, 4.0f, Color.blue));
105: assertTrue(g1.equals(g2));
106: }
107:
108: /**
109: * Confirm that cloning works.
110: */
111: public void testCloning() {
112: LookupPaintScale g1 = new LookupPaintScale();
113: LookupPaintScale g2 = null;
114: try {
115: g2 = (LookupPaintScale) g1.clone();
116: } catch (CloneNotSupportedException e) {
117: e.printStackTrace();
118: }
119: assertTrue(g1 != g2);
120: assertTrue(g1.getClass() == g2.getClass());
121: assertTrue(g1.equals(g2));
122:
123: // check independence
124: g1.add(new Double(0.5), Color.red);
125: assertFalse(g1.equals(g2));
126: g2.add(new Double(0.5), Color.red);
127: assertTrue(g1.equals(g2));
128:
129: // try with gradient paint
130: g1 = new LookupPaintScale(1.0, 2.0, new GradientPaint(1.0f,
131: 2.0f, Color.red, 3.0f, 4.0f, Color.green));
132: g1.add(new Double(1.5), new GradientPaint(1.0f, 2.0f,
133: Color.red, 3.0f, 4.0f, Color.blue));
134: g2 = null;
135: try {
136: g2 = (LookupPaintScale) g1.clone();
137: } catch (CloneNotSupportedException e) {
138: e.printStackTrace();
139: }
140: assertTrue(g1 != g2);
141: assertTrue(g1.getClass() == g2.getClass());
142: assertTrue(g1.equals(g2));
143: }
144:
145: /**
146: * Serialize an instance, restore it, and check for equality.
147: */
148: public void testSerialization() {
149: LookupPaintScale g1 = new LookupPaintScale();
150: LookupPaintScale g2 = null;
151: try {
152: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
153: ObjectOutput out = new ObjectOutputStream(buffer);
154: out.writeObject(g1);
155: out.close();
156:
157: ObjectInput in = new ObjectInputStream(
158: new ByteArrayInputStream(buffer.toByteArray()));
159: g2 = (LookupPaintScale) in.readObject();
160: in.close();
161: } catch (Exception e) {
162: e.printStackTrace();
163: }
164: assertEquals(g1, g2);
165:
166: g1 = new LookupPaintScale(1.0, 2.0, new GradientPaint(1.0f,
167: 2.0f, Color.red, 3.0f, 4.0f, Color.yellow));
168: g1.add(new Double(1.5), new GradientPaint(1.1f, 2.2f,
169: Color.red, 3.3f, 4.4f, Color.blue));
170: g2 = null;
171: try {
172: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
173: ObjectOutput out = new ObjectOutputStream(buffer);
174: out.writeObject(g1);
175: out.close();
176:
177: ObjectInput in = new ObjectInputStream(
178: new ByteArrayInputStream(buffer.toByteArray()));
179: g2 = (LookupPaintScale) in.readObject();
180: in.close();
181: } catch (Exception e) {
182: e.printStackTrace();
183: }
184: assertEquals(g1, g2);
185: }
186:
187: private static final double EPSILON = 0.0000000001;
188:
189: /**
190: * Some checks for the default constructor.
191: */
192: public void testConstructor1() {
193: LookupPaintScale s = new LookupPaintScale();
194: assertEquals(0.0, s.getLowerBound(), EPSILON);
195: assertEquals(1.0, s.getUpperBound(), EPSILON);
196: }
197:
198: /**
199: * Some checks for the other constructor.
200: */
201: public void testConstructor2() {
202: LookupPaintScale s = new LookupPaintScale(1.0, 2.0, Color.red);
203: assertEquals(1.0, s.getLowerBound(), EPSILON);
204: assertEquals(2.0, s.getUpperBound(), EPSILON);
205: assertEquals(Color.red, s.getDefaultPaint());
206: }
207:
208: /**
209: * Some general checks for the lookup table.
210: */
211: public void testGeneral() {
212:
213: LookupPaintScale s = new LookupPaintScale(0.0, 100.0,
214: Color.black);
215: assertEquals(Color.black, s.getPaint(-1.0));
216: assertEquals(Color.black, s.getPaint(0.0));
217: assertEquals(Color.black, s.getPaint(50.0));
218: assertEquals(Color.black, s.getPaint(100.0));
219: assertEquals(Color.black, s.getPaint(101.0));
220:
221: s.add(new Double(50.0), Color.blue);
222: assertEquals(Color.black, s.getPaint(-1.0));
223: assertEquals(Color.black, s.getPaint(0.0));
224: assertEquals(Color.blue, s.getPaint(50.0));
225: assertEquals(Color.blue, s.getPaint(100.0));
226: assertEquals(Color.black, s.getPaint(101.0));
227:
228: s.add(new Double(50.0), Color.red);
229: assertEquals(Color.black, s.getPaint(-1.0));
230: assertEquals(Color.black, s.getPaint(0.0));
231: assertEquals(Color.red, s.getPaint(50.0));
232: assertEquals(Color.red, s.getPaint(100.0));
233: assertEquals(Color.black, s.getPaint(101.0));
234:
235: s.add(new Double(25.0), Color.green);
236: assertEquals(Color.black, s.getPaint(-1.0));
237: assertEquals(Color.black, s.getPaint(0.0));
238: assertEquals(Color.green, s.getPaint(25.0));
239: assertEquals(Color.red, s.getPaint(50.0));
240: assertEquals(Color.red, s.getPaint(100.0));
241: assertEquals(Color.black, s.getPaint(101.0));
242:
243: s.add(new Double(75.0), Color.yellow);
244: assertEquals(Color.black, s.getPaint(-1.0));
245: assertEquals(Color.black, s.getPaint(0.0));
246: assertEquals(Color.green, s.getPaint(25.0));
247: assertEquals(Color.red, s.getPaint(50.0));
248: assertEquals(Color.yellow, s.getPaint(75.0));
249: assertEquals(Color.yellow, s.getPaint(100.0));
250: assertEquals(Color.black, s.getPaint(101.0));
251: }
252:
253: }
|