LegendItemTest.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. * LegendItemTest.java
  29. * -------------------
  30. * (C) Copyright 2004-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. * 04-Jun-2004 : Version 1 (DG);
  38. * 10-Dec-2005 : Addded new test to cover bug report 1374328 (DG);
  39. * 13-Dec-2006 : Extended testEquals() for new fillPaintTransformer
  40. * attribute (DG);
  41. * 23-Apr-2008 : Implemented Cloneable (DG);
  42. * 17-Jun-2008 : Included new fields in existing tests (DG);
  43. *
  44. */
  45. package org.jfree.chart;
  46. import java.awt.BasicStroke;
  47. import java.awt.Color;
  48. import java.awt.Font;
  49. import java.awt.GradientPaint;
  50. import java.awt.font.TextAttribute;
  51. import java.awt.geom.Line2D;
  52. import java.awt.geom.Rectangle2D;
  53. import java.text.AttributedString;
  54. import org.jfree.ui.GradientPaintTransformType;
  55. import org.jfree.ui.StandardGradientPaintTransformer;
  56. import org.junit.Test;
  57. import static org.junit.Assert.assertEquals;
  58. import static org.junit.Assert.assertFalse;
  59. import static org.junit.Assert.assertSame;
  60. import static org.junit.Assert.assertNotSame;
  61. /**
  62. * Tests for the {@link LegendItem} class.
  63. */
  64. public class LegendItemTest {
  65. /**
  66. * Confirm that the equals method can distinguish all the required fields.
  67. */
  68. @Test
  69. public void testEquals() {
  70. LegendItem item1 = new LegendItem("Label", "Description",
  71. "ToolTip", "URL", true,
  72. new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0), true, Color.red,
  73. true, Color.blue, new BasicStroke(1.2f), true,
  74. new Line2D.Double(1.0, 2.0, 3.0, 4.0),
  75. new BasicStroke(2.1f), Color.green);
  76. LegendItem item2 = new LegendItem("Label", "Description",
  77. "ToolTip", "URL", true,
  78. new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
  79. true, Color.red, true, Color.blue, new BasicStroke(1.2f), true,
  80. new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(2.1f),
  81. Color.green);
  82. assertEquals(item1, item2);
  83. assertEquals(item2, item1);
  84. item1 = new LegendItem("Label2", "Description", "ToolTip", "URL",
  85. true, new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0), true,
  86. Color.red, true, Color.blue, new BasicStroke(1.2f), true,
  87. new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(2.1f),
  88. Color.green);
  89. assertFalse(item1.equals(item2));
  90. item2 = new LegendItem("Label2", "Description", "ToolTip", "URL",
  91. true, new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
  92. true, Color.red, true, Color.blue, new BasicStroke(1.2f), true,
  93. new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(2.1f),
  94. Color.green);
  95. assertEquals(item1, item2);
  96. item1 = new LegendItem("Label2", "Description2", "ToolTip",
  97. "URL", true, new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
  98. true, Color.red, true, Color.blue, new BasicStroke(1.2f), true,
  99. new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(2.1f),
  100. Color.green);
  101. assertFalse(item1.equals(item2));
  102. item2 = new LegendItem("Label2", "Description2", "ToolTip",
  103. "URL", true, new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
  104. true, Color.red, true, Color.blue, new BasicStroke(1.2f), true,
  105. new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(2.1f),
  106. Color.green);
  107. assertEquals(item1, item2);
  108. item1 = new LegendItem("Label2", "Description2", "ToolTip",
  109. "URL", false, new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
  110. true, Color.red, true, Color.blue, new BasicStroke(1.2f), true,
  111. new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(2.1f),
  112. Color.green);
  113. assertFalse(item1.equals(item2));
  114. item2 = new LegendItem("Label2", "Description2", "ToolTip",
  115. "URL", false, new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0),
  116. true, Color.red, true, Color.blue, new BasicStroke(1.2f), true,
  117. new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(2.1f),
  118. Color.green);
  119. assertEquals(item1, item2);
  120. item1 = new LegendItem("Label2", "Description2", "ToolTip",
  121. "URL", false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  122. true, Color.red, true, Color.blue, new BasicStroke(1.2f), true,
  123. new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(2.1f),
  124. Color.green);
  125. assertFalse(item1.equals(item2));
  126. item2 = new LegendItem("Label2", "Description2", "ToolTip",
  127. "URL", false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  128. true, Color.red, true, Color.blue, new BasicStroke(1.2f), true,
  129. new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(2.1f),
  130. Color.green);
  131. assertEquals(item1, item2);
  132. item1 = new LegendItem("Label2", "Description2", "ToolTip",
  133. "URL", false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  134. false, Color.red, true, Color.blue, new BasicStroke(1.2f), true,
  135. new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(2.1f),
  136. Color.green);
  137. assertFalse(item1.equals(item2));
  138. item2 = new LegendItem("Label2", "Description2", "ToolTip",
  139. "URL", false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  140. false, Color.red, true, Color.blue, new BasicStroke(1.2f), true,
  141. new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(2.1f),
  142. Color.green);
  143. assertEquals(item1, item2);
  144. item1 = new LegendItem("Label2", "Description2", "ToolTip", "URL",
  145. false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
  146. Color.black, true, Color.blue, new BasicStroke(1.2f), true,
  147. new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(2.1f),
  148. Color.green);
  149. assertFalse(item1.equals(item2));
  150. item2 = new LegendItem("Label2", "Description2", "ToolTip", "URL",
  151. false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
  152. Color.black, true, Color.blue, new BasicStroke(1.2f), true,
  153. new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(2.1f),
  154. Color.green);
  155. assertEquals(item1, item2);
  156. item1 = new LegendItem("Label2", "Description2", "ToolTip",
  157. "URL", false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  158. false, Color.black, false, Color.blue, new BasicStroke(1.2f),
  159. true, new Line2D.Double(1.0, 2.0, 3.0, 4.0),
  160. new BasicStroke(2.1f), Color.green);
  161. assertFalse(item1.equals(item2));
  162. item2 = new LegendItem("Label2", "Description2", "ToolTip", "URL",
  163. false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
  164. Color.black, false, Color.blue, new BasicStroke(1.2f), true,
  165. new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(2.1f),
  166. Color.green);
  167. assertEquals(item1, item2);
  168. item1 = new LegendItem("Label2", "Description2", "ToolTip", "URL",
  169. false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
  170. Color.black, false, Color.yellow, new BasicStroke(1.2f), true,
  171. new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(2.1f),
  172. Color.green);
  173. assertFalse(item1.equals(item2));
  174. item2 = new LegendItem("Label2", "Description2", "ToolTip", "URL",
  175. false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
  176. Color.black, false, Color.yellow, new BasicStroke(1.2f), true,
  177. new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(2.1f),
  178. Color.green);
  179. assertEquals(item1, item2);
  180. item1 = new LegendItem("Label2", "Description2", "ToolTip", "URL",
  181. false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
  182. Color.black, false, Color.yellow, new BasicStroke(2.1f), true,
  183. new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(2.1f),
  184. Color.green);
  185. assertFalse(item1.equals(item2));
  186. item2 = new LegendItem("Label2", "Description2", "ToolTip", "URL",
  187. false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
  188. Color.black, false, Color.yellow, new BasicStroke(2.1f), true,
  189. new Line2D.Double(1.0, 2.0, 3.0, 4.0), new BasicStroke(2.1f),
  190. Color.green);
  191. assertEquals(item1, item2);
  192. item1 = new LegendItem("Label2", "Description2", "ToolTip",
  193. "URL", false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  194. false, Color.black, false, Color.yellow, new BasicStroke(2.1f),
  195. false, new Line2D.Double(1.0, 2.0, 3.0, 4.0),
  196. new BasicStroke(2.1f), Color.green);
  197. assertFalse(item1.equals(item2));
  198. item2 = new LegendItem("Label2", "Description2", "ToolTip",
  199. "URL", false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  200. false, Color.black, false, Color.yellow, new BasicStroke(2.1f),
  201. false, new Line2D.Double(1.0, 2.0, 3.0, 4.0),
  202. new BasicStroke(2.1f), Color.green);
  203. assertEquals(item1, item2);
  204. item1 = new LegendItem("Label2", "Description2", "ToolTip",
  205. "URL", false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  206. false, Color.black, false, Color.yellow, new BasicStroke(2.1f),
  207. false, new Line2D.Double(4.0, 3.0, 2.0, 1.0),
  208. new BasicStroke(2.1f), Color.green);
  209. assertFalse(item1.equals(item2));
  210. item2 = new LegendItem("Label2", "Description2", "ToolTip",
  211. "URL", false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  212. false, Color.black, false, Color.yellow, new BasicStroke(2.1f),
  213. false, new Line2D.Double(4.0, 3.0, 2.0, 1.0),
  214. new BasicStroke(2.1f), Color.green);
  215. assertEquals(item1, item2);
  216. item1 = new LegendItem("Label2", "Description2", "ToolTip",
  217. "URL", false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  218. false, Color.black, false, Color.yellow, new BasicStroke(2.1f),
  219. false, new Line2D.Double(4.0, 3.0, 2.0, 1.0),
  220. new BasicStroke(3.3f), Color.green);
  221. assertFalse(item1.equals(item2));
  222. item2 = new LegendItem("Label2", "Description2", "ToolTip",
  223. "URL", false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  224. false, Color.black, false, Color.yellow, new BasicStroke(2.1f),
  225. false, new Line2D.Double(4.0, 3.0, 2.0, 1.0),
  226. new BasicStroke(3.3f), Color.green);
  227. assertEquals(item1, item2);
  228. item1 = new LegendItem("Label2", "Description2", "ToolTip", "URL",
  229. false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0), false,
  230. Color.black, false, Color.yellow, new BasicStroke(2.1f), false,
  231. new Line2D.Double(4.0, 3.0, 2.0, 1.0), new BasicStroke(3.3f),
  232. Color.white
  233. );
  234. assertFalse(item1.equals(item2));
  235. item2 = new LegendItem("Label2", "Description2", "ToolTip",
  236. "URL", false, new Rectangle2D.Double(4.0, 3.0, 2.0, 1.0),
  237. false, Color.black, false, Color.yellow, new BasicStroke(2.1f),
  238. false, new Line2D.Double(4.0, 3.0, 2.0, 1.0),
  239. new BasicStroke(3.3f),
  240. Color.white);
  241. assertEquals(item1, item2);
  242. // fillPaintTransformer
  243. item1.setFillPaintTransformer(new StandardGradientPaintTransformer(
  244. GradientPaintTransformType.CENTER_VERTICAL));
  245. assertFalse(item1.equals(item2));
  246. item2.setFillPaintTransformer(new StandardGradientPaintTransformer(
  247. GradientPaintTransformType.CENTER_VERTICAL));
  248. assertEquals(item1, item2);
  249. // labelFont
  250. item1.setLabelFont(new Font("Dialog", Font.PLAIN, 13));
  251. assertFalse(item1.equals(item2));
  252. item2.setLabelFont(new Font("Dialog", Font.PLAIN, 13));
  253. assertEquals(item1, item2);
  254. // labelPaint
  255. item1.setLabelPaint(Color.red);
  256. assertFalse(item1.equals(item2));
  257. item2.setLabelPaint(Color.red);
  258. assertEquals(item1, item2);
  259. // fillPaint
  260. item1.setFillPaint(new GradientPaint(1.0f, 2.0f, Color.green, 3.0f,
  261. 4.0f, Color.blue));
  262. assertFalse(item1.equals(item2));
  263. item2.setFillPaint(new GradientPaint(1.0f, 2.0f, Color.green, 3.0f,
  264. 4.0f, Color.blue));
  265. assertEquals(item1, item2);
  266. // outlinePaint
  267. item1.setOutlinePaint(new GradientPaint(1.1f, 2.2f, Color.green, 3.3f,
  268. 4.4f, Color.blue));
  269. assertFalse(item1.equals(item2));
  270. item2.setOutlinePaint(new GradientPaint(1.1f, 2.2f, Color.green, 3.3f,
  271. 4.4f, Color.blue));
  272. assertEquals(item1, item2);
  273. // linePaint
  274. item1.setLinePaint(new GradientPaint(0.1f, 0.2f, Color.green, 0.3f,
  275. 0.4f, Color.blue));
  276. assertFalse(item1.equals(item2));
  277. item2.setLinePaint(new GradientPaint(0.1f, 0.2f, Color.green, 0.3f,
  278. 0.4f, Color.blue));
  279. assertEquals(item1, item2);
  280. }
  281. /**
  282. * Serialize an instance, restore it, and check for equality.
  283. */
  284. @Test
  285. public void testSerialization() {
  286. LegendItem item1 = new LegendItem("Item", "Description",
  287. "ToolTip", "URL",
  288. new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0), new GradientPaint(
  289. 5.0f, 6.0f, Color.blue, 7.0f, 8.0f, Color.gray));
  290. item1.setLabelPaint(new GradientPaint(1.0f, 2.0f, Color.red, 3.0f,
  291. 4.0f, Color.yellow));
  292. item1.setOutlinePaint(new GradientPaint(4.0f, 3.0f, Color.green, 2.0f,
  293. 1.0f, Color.red));
  294. item1.setLinePaint(new GradientPaint(1.0f, 2.0f, Color.white, 3.0f,
  295. 4.0f, Color.red));
  296. LegendItem item2;
  297. item2 = (LegendItem) TestUtilities.serialised(item1);
  298. assertEquals(item1, item2);
  299. }
  300. /**
  301. * Serialize an instance, restore it, and check for equality.
  302. */
  303. @Test
  304. public void testSerialization2() {
  305. AttributedString as = new AttributedString("Test String");
  306. as.addAttribute(TextAttribute.FONT, new Font("Dialog", Font.PLAIN, 12));
  307. LegendItem item1 = new LegendItem(as, "Description", "ToolTip", "URL",
  308. new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0), Color.red);
  309. LegendItem item2 = (LegendItem) TestUtilities.serialised(item1);
  310. assertEquals(item1, item2);
  311. }
  312. /**
  313. * Basic checks for cloning.
  314. */
  315. @Test
  316. public void testCloning() throws CloneNotSupportedException {
  317. LegendItem item1 = new LegendItem("Item");
  318. LegendItem item2 = (LegendItem) item1.clone();
  319. assertNotSame(item1, item2);
  320. assertSame(item1.getClass(), item2.getClass());
  321. assertEquals(item1, item2);
  322. // the clone references the same dataset as the original
  323. assertSame(item1.getDataset(), item2.getDataset());
  324. }
  325. }