1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2013, 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  * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
25  * Other names may be trademarks of their respective owners.]
26  *
27  * -----------------
28  * DialPlotTest.java
29  * -----------------
30  * (C) Copyright 2006-2013, by Object Refinery Limited and Contributors.
31  *
32  * Original Author:  David Gilbert (for Object Refinery Limited);
33  * Contributor(s):   -;
34  *
35  * Changes
36  * -------
37  * 03-Nov-2006 : Version 1 (DG);
38  *
39  */
40 
41 package org.jfree.chart.plot.dial;
42 
43 import static org.junit.Assert.assertEquals;
44 import static org.junit.Assert.assertFalse;
45 import static org.junit.Assert.assertTrue;
46 import static org.junit.Assert.assertNull;
47 import static org.junit.Assert.assertNotNull;
48 
49 import java.awt.Color;
50 import java.awt.GradientPaint;
51 
52 import org.jfree.chart.TestUtilities;
53 
54 import org.jfree.chart.event.PlotChangeEvent;
55 import org.jfree.chart.event.PlotChangeListener;
56 import org.junit.Test;
57 
58 /**
59  * Tests for the {@link DialPlot} class.
60  */
61 public class DialPlotTest implements PlotChangeListener {
62 
63     /** The last plot change event received. */
64     private PlotChangeEvent lastEvent;
65 
66     /**
67      * Records the last plot change event received.
68      *
69      * @param event  the event.
70      */
71     @Override
plotChanged(PlotChangeEvent event)72     public void plotChanged(PlotChangeEvent event) {
73         this.lastEvent = event;
74     }
75 
76     /**
77      * Confirm that the equals method can distinguish all the required fields.
78      */
79     @Test
testEquals()80     public void testEquals() {
81         DialPlot p1 = new DialPlot();
82         DialPlot p2 = new DialPlot();
83         assertTrue(p1.equals(p2));
84 
85         // background
86         p1.setBackground(new DialBackground(Color.green));
87         assertFalse(p1.equals(p2));
88         p2.setBackground(new DialBackground(Color.green));
89         assertTrue(p1.equals(p2));
90 
91         p1.setBackground(null);
92         assertFalse(p1.equals(p2));
93         p2.setBackground(null);
94         assertTrue(p1.equals(p2));
95 
96         // dial cap
97         DialCap cap1 = new DialCap();
98         cap1.setFillPaint(Color.red);
99         p1.setCap(cap1);
100         assertFalse(p1.equals(p2));
101         DialCap cap2 = new DialCap();
102         cap2.setFillPaint(Color.red);
103         p2.setCap(cap2);
104         assertTrue(p1.equals(p2));
105 
106         p1.setCap(null);
107         assertFalse(p1.equals(p2));
108         p2.setCap(null);
109         assertTrue(p1.equals(p2));
110 
111         // frame
112         StandardDialFrame f1 = new StandardDialFrame();
113         f1.setBackgroundPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
114                 4.0f, Color.white));
115         p1.setDialFrame(f1);
116         assertFalse(p1.equals(p2));
117         StandardDialFrame f2 = new StandardDialFrame();
118         f2.setBackgroundPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
119                 4.0f, Color.white));
120         p2.setDialFrame(f2);
121         assertTrue(p1.equals(p2));
122 
123         // view
124         p1.setView(0.2, 0.0, 0.8, 1.0);
125         assertFalse(p1.equals(p2));
126         p2.setView(0.2, 0.0, 0.8, 1.0);
127         assertTrue(p1.equals(p2));
128 
129         // layer
130         p1.addLayer(new StandardDialScale());
131         assertFalse(p1.equals(p2));
132         p2.addLayer(new StandardDialScale());
133         assertTrue(p1.equals(p2));
134     }
135 
136     /**
137      * Two objects that are equal are required to return the same hashCode.
138      */
139     @Test
testHashCode()140     public void testHashCode() {
141         DialPlot p1 = new DialPlot();
142         DialPlot p2 = new DialPlot();
143         assertTrue(p1.equals(p2));
144         int h1 = p1.hashCode();
145         int h2 = p2.hashCode();
146         assertEquals(h1, h2);
147     }
148 
149     /**
150      * Confirm that cloning works.
151      */
152     @Test
testCloning()153     public void testCloning() throws CloneNotSupportedException {
154         DialPlot p1 = new DialPlot();
155         DialPlot p2 = (DialPlot) p1.clone();
156         assertTrue(p1 != p2);
157         assertTrue(p1.getClass() == p2.getClass());
158         assertTrue(p1.equals(p2));
159     }
160 
161 
162     /**
163      * Serialize an instance, restore it, and check for equality.
164      */
165     @Test
testSerialization()166     public void testSerialization() {
167         DialPlot p1 = new DialPlot();
168         DialPlot p2 = (DialPlot) TestUtilities.serialised(p1);
169         assertEquals(p1, p2);
170     }
171 
172     /**
173      * Check the notification event mechanism for the dial background.
174      */
175     @Test
testBackgroundListener()176     public void testBackgroundListener() {
177         DialPlot p = new DialPlot();
178         DialBackground b1 = new DialBackground(Color.red);
179         p.setBackground(b1);
180         p.addChangeListener(this);
181         this.lastEvent = null;
182         b1.setPaint(Color.blue);
183         assertNotNull(this.lastEvent);
184 
185         DialBackground b2 = new DialBackground(Color.green);
186         p.setBackground(b2);
187         this.lastEvent = null;
188         b1.setPaint(Color.red);
189         assertNull(this.lastEvent);
190         b2.setPaint(Color.red);
191         assertNotNull(this.lastEvent);
192     }
193 
194     /**
195      * Check the notification event mechanism for the dial cap.
196      */
197     @Test
testCapListener()198     public void testCapListener() {
199         DialPlot p = new DialPlot();
200         DialCap c1 = new DialCap();
201         p.setCap(c1);
202         p.addChangeListener(this);
203         this.lastEvent = null;
204         c1.setFillPaint(Color.red);
205         assertNotNull(this.lastEvent);
206 
207         DialCap c2 = new DialCap();
208         p.setCap(c2);
209         this.lastEvent = null;
210         c1.setFillPaint(Color.blue);
211         assertNull(this.lastEvent);
212         c2.setFillPaint(Color.green);
213         assertNotNull(this.lastEvent);
214     }
215 
216     /**
217      * Check the notification event mechanism for the dial frame.
218      */
219     @Test
testFrameListener()220     public void testFrameListener() {
221         DialPlot p = new DialPlot();
222         ArcDialFrame f1 = new ArcDialFrame();
223         p.setDialFrame(f1);
224         p.addChangeListener(this);
225         this.lastEvent = null;
226         f1.setBackgroundPaint(Color.gray);
227         assertNotNull(this.lastEvent);
228 
229         ArcDialFrame f2 = new ArcDialFrame();
230         p.setDialFrame(f2);
231         this.lastEvent = null;
232         f1.setBackgroundPaint(Color.blue);
233         assertNull(this.lastEvent);
234         f2.setBackgroundPaint(Color.green);
235         assertNotNull(this.lastEvent);
236     }
237 
238     /**
239      * Check the notification event mechanism for the dial scales.
240      */
241     @Test
testScaleListener()242     public void testScaleListener() {
243         DialPlot p = new DialPlot();
244         StandardDialScale s1 = new StandardDialScale();
245         p.addScale(0, s1);
246         p.addChangeListener(this);
247         this.lastEvent = null;
248         s1.setStartAngle(22.0);
249         assertNotNull(this.lastEvent);
250 
251         StandardDialScale s2 = new StandardDialScale();
252         p.addScale(0, s2);
253         this.lastEvent = null;
254         s1.setStartAngle(33.0);
255         assertNull(this.lastEvent);
256         s2.setStartAngle(33.0);
257         assertNotNull(this.lastEvent);
258     }
259 
260     /**
261      * Check the notification event mechanism for a layer.
262      */
263     @Test
testLayerListener()264     public void testLayerListener() {
265         DialPlot p = new DialPlot();
266         DialBackground b1 = new DialBackground(Color.red);
267         p.addLayer(b1);
268         p.addChangeListener(this);
269         this.lastEvent = null;
270         b1.setPaint(Color.blue);
271         assertNotNull(this.lastEvent);
272 
273         DialBackground b2 = new DialBackground(Color.green);
274         p.addLayer(b2);
275         this.lastEvent = null;
276         b1.setPaint(Color.red);
277         assertNotNull(this.lastEvent);
278         b2.setPaint(Color.green);
279         assertNotNull(this.lastEvent);
280 
281         p.removeLayer(b2);
282         this.lastEvent = null;
283         b2.setPaint(Color.red);
284         assertNull(this.lastEvent);
285     }
286 
287 }
288