1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.]
*
* ----------------------
* SpiderWebPlotTest.java
* ----------------------
* (C) Copyright 2005-2013, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* Changes
* -------
* 10-Jun-2005 : Version 1 (DG);
* 01-Jun-2006 : Added testDrawWithNullInfo() method (DG);
* 05-Feb-2007 : Added more checks to testCloning (DG);
* 01-Jun-2009 : Added test for getLegendItems() bug, series key is not
* set (DG);
*
*/
package org.jfree.chart.plot;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.text.DecimalFormat;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.TestUtilities;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.util.Rotation;
import org.jfree.util.TableOrder;
import org.junit.Test;
/**
* Tests for the {@link SpiderWebPlot} class.
*/
public class SpiderWebPlotTest {
/**
* Some checks for the equals() method.
*/
@Test
public void testEquals() {
SpiderWebPlot p1 = new SpiderWebPlot(new DefaultCategoryDataset());
SpiderWebPlot p2 = new SpiderWebPlot(new DefaultCategoryDataset());
assertTrue(p1.equals(p2));
assertTrue(p2.equals(p1));
// dataExtractOrder
p1.setDataExtractOrder(TableOrder.BY_COLUMN);
assertFalse(p1.equals(p2));
p2.setDataExtractOrder(TableOrder.BY_COLUMN);
assertTrue(p1.equals(p2));
// headPercent
p1.setHeadPercent(0.321);
assertFalse(p1.equals(p2));
p2.setHeadPercent(0.321);
assertTrue(p1.equals(p2));
// interiorGap
p1.setInteriorGap(0.123);
assertFalse(p1.equals(p2));
p2.setInteriorGap(0.123);
assertTrue(p1.equals(p2));
// startAngle
p1.setStartAngle(0.456);
assertFalse(p1.equals(p2));
p2.setStartAngle(0.456);
assertTrue(p1.equals(p2));
// direction
p1.setDirection(Rotation.ANTICLOCKWISE);
assertFalse(p1.equals(p2));
p2.setDirection(Rotation.ANTICLOCKWISE);
assertTrue(p1.equals(p2));
// maxValue
p1.setMaxValue(123.4);
assertFalse(p1.equals(p2));
p2.setMaxValue(123.4);
assertTrue(p1.equals(p2));
// legendItemShape
p1.setLegendItemShape(new Rectangle(1, 2, 3, 4));
assertFalse(p1.equals(p2));
p2.setLegendItemShape(new Rectangle(1, 2, 3, 4));
assertTrue(p1.equals(p2));
// seriesPaint
p1.setSeriesPaint(new GradientPaint(1.0f, 2.0f, Color.red,
3.0f, 4.0f, Color.white));
assertFalse(p1.equals(p2));
p2.setSeriesPaint(new GradientPaint(1.0f, 2.0f, Color.red,
3.0f, 4.0f, Color.white));
assertTrue(p1.equals(p2));
// seriesPaintList
p1.setSeriesPaint(1, new GradientPaint(1.0f, 2.0f, Color.yellow,
3.0f, 4.0f, Color.white));
assertFalse(p1.equals(p2));
p2.setSeriesPaint(1, new GradientPaint(1.0f, 2.0f, Color.yellow,
3.0f, 4.0f, Color.white));
assertTrue(p1.equals(p2));
// baseSeriesPaint
p1.setBaseSeriesPaint(new GradientPaint(1.0f, 2.0f, Color.red,
3.0f, 4.0f, Color.black));
assertFalse(p1.equals(p2));
p2.setBaseSeriesPaint(new GradientPaint(1.0f, 2.0f, Color.red,
3.0f, 4.0f, Color.black));
assertTrue(p1.equals(p2));
// seriesOutlinePaint
p1.setSeriesOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.blue,
3.0f, 4.0f, Color.black));
assertFalse(p1.equals(p2));
p2.setSeriesOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.blue,
3.0f, 4.0f, Color.black));
assertTrue(p1.equals(p2));
// seriesOutlinePaintList
p1.setSeriesOutlinePaint(1, new GradientPaint(1.0f, 2.0f, Color.blue,
3.0f, 4.0f, Color.green));
assertFalse(p1.equals(p2));
p2.setSeriesOutlinePaint(1, new GradientPaint(1.0f, 2.0f, Color.blue,
3.0f, 4.0f, Color.green));
assertTrue(p1.equals(p2));
// baseSeriesOutlinePaint
p1.setBaseSeriesOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.cyan,
3.0f, 4.0f, Color.green));
assertFalse(p1.equals(p2));
p2.setBaseSeriesOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.cyan,
3.0f, 4.0f, Color.green));
assertTrue(p1.equals(p2));
// seriesOutlineStroke
BasicStroke s = new BasicStroke(1.23f);
p1.setSeriesOutlineStroke(s);
assertFalse(p1.equals(p2));
p2.setSeriesOutlineStroke(s);
assertTrue(p1.equals(p2));
// seriesOutlineStrokeList
p1.setSeriesOutlineStroke(1, s);
assertFalse(p1.equals(p2));
p2.setSeriesOutlineStroke(1, s);
assertTrue(p1.equals(p2));
// baseSeriesOutlineStroke
p1.setBaseSeriesOutlineStroke(s);
assertFalse(p1.equals(p2));
p2.setBaseSeriesOutlineStroke(s);
assertTrue(p1.equals(p2));
// webFilled
p1.setWebFilled(false);
assertFalse(p1.equals(p2));
p2.setWebFilled(false);
assertTrue(p1.equals(p2));
// axisLabelGap
p1.setAxisLabelGap(0.11);
assertFalse(p1.equals(p2));
p2.setAxisLabelGap(0.11);
assertTrue(p1.equals(p2));
// labelFont
p1.setLabelFont(new Font("Serif", Font.PLAIN, 9));
assertFalse(p1.equals(p2));
p2.setLabelFont(new Font("Serif", Font.PLAIN, 9));
assertTrue(p1.equals(p2));
// labelPaint
p1.setLabelPaint(new GradientPaint(1.0f, 2.0f, Color.red,
3.0f, 4.0f, Color.blue));
assertFalse(p1.equals(p2));
p2.setLabelPaint(new GradientPaint(1.0f, 2.0f, Color.red,
3.0f, 4.0f, Color.blue));
assertTrue(p1.equals(p2));
// labelGenerator
p1.setLabelGenerator(new StandardCategoryItemLabelGenerator("XYZ: {0}",
new DecimalFormat("0.000")));
assertFalse(p1.equals(p2));
p2.setLabelGenerator(new StandardCategoryItemLabelGenerator("XYZ: {0}",
new DecimalFormat("0.000")));
assertTrue(p1.equals(p2));
// toolTipGenerator
p1.setToolTipGenerator(new StandardCategoryToolTipGenerator());
assertFalse(p1.equals(p2));
p2.setToolTipGenerator(new StandardCategoryToolTipGenerator());
assertTrue(p1.equals(p2));
// urlGenerator
p1.setURLGenerator(new StandardCategoryURLGenerator());
assertFalse(p1.equals(p2));
p2.setURLGenerator(new StandardCategoryURLGenerator());
assertTrue(p1.equals(p2));
// axisLinePaint
p1.setAxisLinePaint(Color.red);
assertFalse(p1.equals(p2));
p2.setAxisLinePaint(Color.red);
assertTrue(p1.equals(p2));
// axisLineStroke
p1.setAxisLineStroke(new BasicStroke(1.1f));
assertFalse(p1.equals(p2));
p2.setAxisLineStroke(new BasicStroke(1.1f));
assertTrue(p1.equals(p2));
}
/**
* Confirm that cloning works.
*/
@Test
public void testCloning() throws CloneNotSupportedException {
SpiderWebPlot p1 = new SpiderWebPlot(new DefaultCategoryDataset());
Rectangle2D legendShape = new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0);
p1.setLegendItemShape(legendShape);
SpiderWebPlot p2 = (SpiderWebPlot) p1.clone();
assertTrue(p1 != p2);
assertTrue(p1.getClass() == p2.getClass());
assertTrue(p1.equals(p2));
// change the legendItemShape
legendShape.setRect(4.0, 3.0, 2.0, 1.0);
assertFalse(p1.equals(p2));
p2.setLegendItemShape(legendShape);
assertTrue(p1.equals(p2));
// change a series paint
p1.setSeriesPaint(1, Color.black);
assertFalse(p1.equals(p2));
p2.setSeriesPaint(1, Color.black);
assertTrue(p1.equals(p2));
// change a series outline paint
p1.setSeriesOutlinePaint(0, Color.red);
assertFalse(p1.equals(p2));
p2.setSeriesOutlinePaint(0, Color.red);
assertTrue(p1.equals(p2));
// change a series outline stroke
p1.setSeriesOutlineStroke(0, new BasicStroke(1.1f));
assertFalse(p1.equals(p2));
p2.setSeriesOutlineStroke(0, new BasicStroke(1.1f));
assertTrue(p1.equals(p2));
}
/**
* Serialize an instance, restore it, and check for equality.
*/
@Test
public void testSerialization() {
SpiderWebPlot p1 = new SpiderWebPlot(new DefaultCategoryDataset());
SpiderWebPlot p2 = (SpiderWebPlot) TestUtilities.serialised(p1);
assertEquals(p1, p2);
}
/**
* Draws the chart with a null info object to make sure that no exceptions
* are thrown.
*/
@Test
public void testDrawWithNullInfo() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(35.0, "S1", "C1");
dataset.addValue(45.0, "S1", "C2");
dataset.addValue(55.0, "S1", "C3");
dataset.addValue(15.0, "S1", "C4");
dataset.addValue(25.0, "S1", "C5");
SpiderWebPlot plot = new SpiderWebPlot(dataset);
JFreeChart chart = new JFreeChart(plot);
try {
BufferedImage image = new BufferedImage(200 , 100,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
g2.dispose();
}
catch (Exception e) {
fail("There should be no exception.");
}
}
/**
* Fetches the legend items and checks the values.
*/
@Test
public void testGetLegendItems() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(35.0, "S1", "C1");
dataset.addValue(45.0, "S1", "C2");
dataset.addValue(55.0, "S2", "C1");
dataset.addValue(15.0, "S2", "C2");
SpiderWebPlot plot = new SpiderWebPlot(dataset);
JFreeChart chart = new JFreeChart(plot);
LegendItemCollection legendItems = plot.getLegendItems();
assertEquals(2, legendItems.getItemCount());
LegendItem item1 = legendItems.get(0);
assertEquals("S1", item1.getLabel());
assertEquals("S1", item1.getSeriesKey());
assertEquals(0, item1.getSeriesIndex());
assertEquals(dataset, item1.getDataset());
assertEquals(0, item1.getDatasetIndex());
LegendItem item2 = legendItems.get(1);
assertEquals("S2", item2.getLabel());
assertEquals("S2", item2.getSeriesKey());
assertEquals(1, item2.getSeriesIndex());
assertEquals(dataset, item2.getDataset());
assertEquals(0, item2.getDatasetIndex());
}
}
|