KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > plot > junit > CategoryMarkerTests


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  * CatetgoryMarkerTests.java
29  * -------------------------
30  * (C) Copyright 2005, 2006, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: CategoryMarkerTests.java,v 1.1.2.1 2006/10/03 15:41:26 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 22-Mar-2005 : Version 1 (DG);
40  *
41  */

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

66 public class CategoryMarkerTests
67     extends TestCase
68     implements MarkerChangeListener {
69
70     MarkerChangeEvent lastEvent;
71     
72     public void markerChanged(MarkerChangeEvent event) {
73         this.lastEvent = event;
74     }
75
76     /**
77      * Returns the tests as a test suite.
78      *
79      * @return The test suite.
80      */

81     public static Test suite() {
82         return new TestSuite(CategoryMarkerTests.class);
83     }
84
85     /**
86      * Constructs a new set of tests.
87      *
88      * @param name the name of the tests.
89      */

90     public CategoryMarkerTests(String JavaDoc name) {
91         super(name);
92     }
93
94     /**
95      * Confirm that the equals method can distinguish all the required fields.
96      */

97     public void testEquals() {
98         CategoryMarker m1 = new CategoryMarker("A");
99         CategoryMarker m2 = new CategoryMarker("A");
100         assertTrue(m1.equals(m2));
101         assertTrue(m2.equals(m1));
102         
103         //key
104
m1 = new CategoryMarker("B");
105         assertFalse(m1.equals(m2));
106         m2 = new CategoryMarker("B");
107         assertTrue(m1.equals(m2));
108         
109         //paint
110
m1 = new CategoryMarker("A", new GradientPaint JavaDoc(1.0f, 2.0f, Color.white,
111                 3.0f, 4.0f, Color.yellow), new BasicStroke JavaDoc(1.1f));
112         assertFalse(m1.equals(m2));
113         m2 = new CategoryMarker("A", new GradientPaint JavaDoc(1.0f, 2.0f, Color.white,
114                 3.0f, 4.0f, Color.yellow), new BasicStroke JavaDoc(1.1f));
115         assertTrue(m1.equals(m2));
116
117         //stroke
118
m1 = new CategoryMarker("A", new GradientPaint JavaDoc(1.0f, 2.0f, Color.white,
119                 3.0f, 4.0f, Color.yellow), new BasicStroke JavaDoc(2.2f));
120         assertFalse(m1.equals(m2));
121         m2 = new CategoryMarker("A", new GradientPaint JavaDoc(1.0f, 2.0f, Color.white,
122                 3.0f, 4.0f, Color.yellow), new BasicStroke JavaDoc(2.2f));
123         assertTrue(m1.equals(m2));
124
125         //outlinePaint
126
m1 = new CategoryMarker("A", new GradientPaint JavaDoc(1.0f, 2.0f, Color.white,
127                 3.0f, 4.0f, Color.yellow), new BasicStroke JavaDoc(2.2f), Color.red,
128                 new BasicStroke JavaDoc(1.0f), 1.0f);
129         assertFalse(m1.equals(m2));
130         m2 = new CategoryMarker("A", new GradientPaint JavaDoc(1.0f, 2.0f, Color.white,
131                 3.0f, 4.0f, Color.yellow), new BasicStroke JavaDoc(2.2f), Color.red,
132                 new BasicStroke JavaDoc(1.0f), 1.0f);
133         assertTrue(m1.equals(m2));
134
135         //outlineStroke
136
m1 = new CategoryMarker("A", new GradientPaint JavaDoc(1.0f, 2.0f, Color.white,
137                 3.0f, 4.0f, Color.yellow), new BasicStroke JavaDoc(2.2f), Color.red,
138                 new BasicStroke JavaDoc(3.3f), 1.0f);
139         assertFalse(m1.equals(m2));
140         m2 = new CategoryMarker("A", new GradientPaint JavaDoc(1.0f, 2.0f, Color.white,
141                 3.0f, 4.0f, Color.yellow), new BasicStroke JavaDoc(2.2f), Color.red,
142                 new BasicStroke JavaDoc(3.3f), 1.0f);
143         assertTrue(m1.equals(m2));
144
145         //alpha
146
m1 = new CategoryMarker("A", new GradientPaint JavaDoc(1.0f, 2.0f, Color.white,
147                 3.0f, 4.0f, Color.yellow), new BasicStroke JavaDoc(2.2f), Color.red,
148                 new BasicStroke JavaDoc(1.0f), 0.5f);
149         assertFalse(m1.equals(m2));
150         m2 = new CategoryMarker("A", new GradientPaint JavaDoc(1.0f, 2.0f, Color.white,
151                 3.0f, 4.0f, Color.yellow), new BasicStroke JavaDoc(2.2f), Color.red,
152                 new BasicStroke JavaDoc(1.0f), 0.5f);
153         assertTrue(m1.equals(m2));
154
155     }
156         
157     /**
158      * Check cloning.
159      */

160     public void testCloning() {
161         CategoryMarker m1 = new CategoryMarker("A", new GradientPaint JavaDoc(1.0f,
162                 2.0f, Color.white, 3.0f, 4.0f, Color.yellow),
163                 new BasicStroke JavaDoc(1.1f));
164         CategoryMarker m2 = null;
165         try {
166             m2 = (CategoryMarker) m1.clone();
167         }
168         catch (CloneNotSupportedException JavaDoc e) {
169             e.printStackTrace();
170         }
171         assertTrue(m1 != m2);
172         assertTrue(m1.getClass() == m2.getClass());
173         assertTrue(m1.equals(m2));
174     }
175
176    /**
177      * Serialize an instance, restore it, and check for equality.
178      */

179     public void testSerialization() {
180
181         CategoryMarker m1 = new CategoryMarker("A", new GradientPaint JavaDoc(1.0f,
182                 2.0f, Color.white, 3.0f, 4.0f, Color.yellow),
183                 new BasicStroke JavaDoc(1.1f));
184         CategoryMarker m2 = null;
185         try {
186             ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
187             ObjectOutput JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
188             out.writeObject(m1);
189             out.close();
190
191             ObjectInput JavaDoc in = new ObjectInputStream JavaDoc(
192                     new ByteArrayInputStream JavaDoc(buffer.toByteArray()));
193             m2 = (CategoryMarker) in.readObject();
194             in.close();
195         }
196         catch (Exception JavaDoc e) {
197             fail(e.toString());
198         }
199         boolean b = m1.equals(m2);
200         assertTrue(b);
201
202     }
203     
204     /**
205      * Some checks for the getKey() and setKey() methods.
206      */

207     public void testGetSetKey() {
208         CategoryMarker m = new CategoryMarker("X");
209         m.addChangeListener(this);
210         this.lastEvent = null;
211         assertEquals("X", m.getKey());
212         m.setKey("Y");
213         assertEquals("Y", m.getKey());
214         assertEquals(m, this.lastEvent.getMarker());
215         
216         // check null argument...
217
try {
218             m.setKey(null);
219             fail("Expected an IllegalArgumentException for null.");
220         }
221         catch (IllegalArgumentException JavaDoc e) {
222             assertTrue(true);
223         }
224     }
225
226     /**
227      * Some checks for the getDrawAsLine() and setDrawAsLine() methods.
228      */

229     public void testGetSetDrawAsLine() {
230         CategoryMarker m = new CategoryMarker("X");
231         m.addChangeListener(this);
232         this.lastEvent = null;
233         assertEquals(false, m.getDrawAsLine());
234         m.setDrawAsLine(true);
235         assertEquals(true, m.getDrawAsLine());
236         assertEquals(m, this.lastEvent.getMarker());
237     }
238 }
239
Popular Tags