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: * StrokeMapTests.java
029: * -------------------
030: * (C) Copyright 2006, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: StrokeMapTests.java,v 1.1.2.1 2006/10/03 15:41:32 mungady Exp $
036: *
037: * Changes:
038: * --------
039: * 27-Sep-2006 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.junit;
044:
045: import java.awt.BasicStroke;
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.StrokeMap;
058:
059: /**
060: * Some tests for the {@link StrokeMap} class.
061: */
062: public class StrokeMapTests extends TestCase {
063:
064: /**
065: * Returns the tests as a test suite.
066: *
067: * @return The test suite.
068: */
069: public static Test suite() {
070: return new TestSuite(StrokeMapTests.class);
071: }
072:
073: /**
074: * Constructs a new set of tests.
075: *
076: * @param name the name of the tests.
077: */
078: public StrokeMapTests(String name) {
079: super (name);
080: }
081:
082: /**
083: * Some checks for the getStroke() method.
084: */
085: public void testGetStroke() {
086: StrokeMap m1 = new StrokeMap();
087: assertEquals(null, m1.getStroke("A"));
088: m1.put("A", new BasicStroke(1.1f));
089: assertEquals(new BasicStroke(1.1f), m1.getStroke("A"));
090: m1.put("A", null);
091: assertEquals(null, m1.getStroke("A"));
092:
093: // a null key should throw an IllegalArgumentException
094: boolean pass = false;
095: try {
096: m1.getStroke(null);
097: } catch (IllegalArgumentException e) {
098: pass = true;
099: }
100: assertTrue(pass);
101: }
102:
103: /**
104: * Some checks for the put() method.
105: */
106: public void testPut() {
107: StrokeMap m1 = new StrokeMap();
108: m1.put("A", new BasicStroke(1.1f));
109: assertEquals(new BasicStroke(1.1f), m1.getStroke("A"));
110:
111: // a null key should throw an IllegalArgumentException
112: boolean pass = false;
113: try {
114: m1.put(null, new BasicStroke(1.1f));
115: } catch (IllegalArgumentException e) {
116: pass = true;
117: }
118: assertTrue(pass);
119: }
120:
121: /**
122: * Some checks for the equals() method.
123: */
124: public void testEquals() {
125: StrokeMap m1 = new StrokeMap();
126: StrokeMap m2 = new StrokeMap();
127: assertTrue(m1.equals(m1));
128: assertTrue(m1.equals(m2));
129: assertFalse(m1.equals(null));
130: assertFalse(m1.equals("ABC"));
131:
132: m1.put("K1", new BasicStroke(1.1f));
133: assertFalse(m1.equals(m2));
134: m2.put("K1", new BasicStroke(1.1f));
135: assertTrue(m1.equals(m2));
136:
137: m1.put("K2", new BasicStroke(2.2f));
138: assertFalse(m1.equals(m2));
139: m2.put("K2", new BasicStroke(2.2f));
140: assertTrue(m1.equals(m2));
141:
142: m1.put("K2", null);
143: assertFalse(m1.equals(m2));
144: m2.put("K2", null);
145: assertTrue(m1.equals(m2));
146: }
147:
148: /**
149: * Some checks for cloning.
150: */
151: public void testCloning() {
152: StrokeMap m1 = new StrokeMap();
153: StrokeMap m2 = null;
154: try {
155: m2 = (StrokeMap) m1.clone();
156: } catch (CloneNotSupportedException e) {
157: e.printStackTrace();
158: }
159: assertTrue(m1.equals(m2));
160:
161: m1.put("K1", new BasicStroke(1.1f));
162: m1.put("K2", new BasicStroke(2.2f));
163: try {
164: m2 = (StrokeMap) m1.clone();
165: } catch (CloneNotSupportedException e) {
166: e.printStackTrace();
167: }
168: assertTrue(m1.equals(m2));
169: }
170:
171: /**
172: * A check for serialization.
173: */
174: public void testSerialization1() {
175: StrokeMap m1 = new StrokeMap();
176: StrokeMap m2 = null;
177: try {
178: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
179: ObjectOutput out = new ObjectOutputStream(buffer);
180: out.writeObject(m1);
181: out.close();
182:
183: ObjectInput in = new ObjectInputStream(
184: new ByteArrayInputStream(buffer.toByteArray()));
185: m2 = (StrokeMap) in.readObject();
186: in.close();
187: } catch (Exception e) {
188: e.printStackTrace();
189: }
190: assertEquals(m1, m2);
191: }
192:
193: /**
194: * A check for serialization.
195: */
196: public void testSerialization2() {
197: StrokeMap m1 = new StrokeMap();
198: m1.put("K1", new BasicStroke(1.1f));
199: m1.put("K2", new BasicStroke(2.2f));
200: StrokeMap m2 = null;
201: try {
202: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
203: ObjectOutput out = new ObjectOutputStream(buffer);
204: out.writeObject(m1);
205: out.close();
206:
207: ObjectInput in = new ObjectInputStream(
208: new ByteArrayInputStream(buffer.toByteArray()));
209: m2 = (StrokeMap) in.readObject();
210: in.close();
211: } catch (Exception e) {
212: e.printStackTrace();
213: }
214: assertEquals(m1, m2);
215: }
216:
217: }
|