KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > junit > StrokeMapTests


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -------------------
28  * StrokeMapTests.java
29  * -------------------
30  * (C) Copyright 2006, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: StrokeMapTests.java,v 1.1.2.1 2006/10/03 15:41:32 mungady Exp $
36  *
37  * Changes:
38  * --------
39  * 27-Sep-2006 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.chart.junit;
44
45 import java.awt.BasicStroke JavaDoc;
46 import java.io.ByteArrayInputStream JavaDoc;
47 import java.io.ByteArrayOutputStream JavaDoc;
48 import java.io.ObjectInput JavaDoc;
49 import java.io.ObjectInputStream JavaDoc;
50 import java.io.ObjectOutput JavaDoc;
51 import java.io.ObjectOutputStream JavaDoc;
52
53 import junit.framework.Test;
54 import junit.framework.TestCase;
55 import junit.framework.TestSuite;
56
57 import org.jfree.chart.StrokeMap;
58
59 /**
60  * Some tests for the {@link StrokeMap} class.
61  */

62 public class StrokeMapTests extends TestCase {
63
64     /**
65      * Returns the tests as a test suite.
66      *
67      * @return The test suite.
68      */

69     public static Test suite() {
70         return new TestSuite(StrokeMapTests.class);
71     }
72
73     /**
74      * Constructs a new set of tests.
75      *
76      * @param name the name of the tests.
77      */

78     public StrokeMapTests(String JavaDoc name) {
79         super(name);
80     }
81
82     /**
83      * Some checks for the getStroke() method.
84      */

85     public void testGetStroke() {
86         StrokeMap m1 = new StrokeMap();
87         assertEquals(null, m1.getStroke("A"));
88         m1.put("A", new BasicStroke JavaDoc(1.1f));
89         assertEquals(new BasicStroke JavaDoc(1.1f), m1.getStroke("A"));
90         m1.put("A", null);
91         assertEquals(null, m1.getStroke("A"));
92         
93         // a null key should throw an IllegalArgumentException
94
boolean pass = false;
95         try {
96             m1.getStroke(null);
97         }
98         catch (IllegalArgumentException JavaDoc e) {
99             pass = true;
100         }
101         assertTrue(pass);
102     }
103
104     /**
105      * Some checks for the put() method.
106      */

107     public void testPut() {
108         StrokeMap m1 = new StrokeMap();
109         m1.put("A", new BasicStroke JavaDoc(1.1f));
110         assertEquals(new BasicStroke JavaDoc(1.1f), m1.getStroke("A"));
111         
112         // a null key should throw an IllegalArgumentException
113
boolean pass = false;
114         try {
115             m1.put(null, new BasicStroke JavaDoc(1.1f));
116         }
117         catch (IllegalArgumentException JavaDoc e) {
118             pass = true;
119         }
120         assertTrue(pass);
121     }
122     
123     /**
124      * Some checks for the equals() method.
125      */

126     public void testEquals() {
127         StrokeMap m1 = new StrokeMap();
128         StrokeMap m2 = new StrokeMap();
129         assertTrue(m1.equals(m1));
130         assertTrue(m1.equals(m2));
131         assertFalse(m1.equals(null));
132         assertFalse(m1.equals("ABC"));
133         
134         m1.put("K1", new BasicStroke JavaDoc(1.1f));
135         assertFalse(m1.equals(m2));
136         m2.put("K1", new BasicStroke JavaDoc(1.1f));
137         assertTrue(m1.equals(m2));
138         
139         m1.put("K2", new BasicStroke JavaDoc(2.2f));
140         assertFalse(m1.equals(m2));
141         m2.put("K2", new BasicStroke JavaDoc(2.2f));
142         assertTrue(m1.equals(m2));
143         
144         m1.put("K2", null);
145         assertFalse(m1.equals(m2));
146         m2.put("K2", null);
147         assertTrue(m1.equals(m2));
148     }
149     
150     /**
151      * Some checks for cloning.
152      */

153     public void testCloning() {
154         StrokeMap m1 = new StrokeMap();
155         StrokeMap m2 = null;
156         try {
157             m2 = (StrokeMap) m1.clone();
158         }
159         catch (CloneNotSupportedException JavaDoc e) {
160             e.printStackTrace();
161         }
162         assertTrue(m1.equals(m2));
163         
164         m1.put("K1", new BasicStroke JavaDoc(1.1f));
165         m1.put("K2", new BasicStroke JavaDoc(2.2f));
166         try {
167             m2 = (StrokeMap) m1.clone();
168         }
169         catch (CloneNotSupportedException JavaDoc e) {
170             e.printStackTrace();
171         }
172         assertTrue(m1.equals(m2));
173     }
174     
175     /**
176      * A check for serialization.
177      */

178     public void testSerialization1() {
179         StrokeMap m1 = new StrokeMap();
180         StrokeMap m2 = null;
181         try {
182             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
183             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
184             out.writeObject(m1);
185             out.close();
186
187             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(
188                     buffer.toByteArray()));
189             m2 = (StrokeMap) in.readObject();
190             in.close();
191         }
192         catch (Exception JavaDoc e) {
193             e.printStackTrace();
194         }
195         assertEquals(m1, m2);
196     }
197
198     /**
199      * A check for serialization.
200      */

201     public void testSerialization2() {
202         StrokeMap m1 = new StrokeMap();
203         m1.put("K1", new BasicStroke JavaDoc(1.1f));
204         m1.put("K2", new BasicStroke JavaDoc(2.2f));
205         StrokeMap m2 = null;
206         try {
207             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
208             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
209             out.writeObject(m1);
210             out.close();
211
212             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(
213                     buffer.toByteArray()));
214             m2 = (StrokeMap) in.readObject();
215             in.close();
216         }
217         catch (Exception JavaDoc e) {
218             e.printStackTrace();
219         }
220         assertEquals(m1, m2);
221     }
222
223 }
224
225
Popular Tags