AxisTest.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. * AxisTest.java
  29. * -------------
  30. * (C) Copyright 2003-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. * 13-Aug-2003 : Version 1 (DG);
  38. * 06-Jan-2004 : Added tests for axis line attributes (DG);
  39. * 07-Jan-2005 : Added hashCode() test (DG);
  40. * 25-Sep-2008 : Extended equals() to cover new fields (DG);
  41. * 05-Aug-2013 : Extended equals for new fields (DG);
  42. *
  43. */
  44. package org.jfree.chart.axis;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertNotEquals;
  47. import static org.junit.Assert.assertNotSame;
  48. import static org.junit.Assert.assertSame;
  49. import java.awt.BasicStroke;
  50. import java.awt.Color;
  51. import java.awt.Font;
  52. import java.awt.GradientPaint;
  53. import java.awt.font.TextAttribute;
  54. import java.text.AttributedString;
  55. import org.jfree.chart.TestUtilities;
  56. import org.jfree.ui.RectangleInsets;
  57. import org.junit.Test;
  58. /**
  59. * Tests for the {@link Axis} class.
  60. */
  61. public class AxisTest {
  62. /**
  63. * Confirm that cloning works.
  64. */
  65. @Test
  66. public void testCloning() throws CloneNotSupportedException {
  67. CategoryAxis a1 = new CategoryAxis("Test");
  68. a1.setAxisLinePaint(Color.red);
  69. CategoryAxis a2 = (CategoryAxis) a1.clone();
  70. assertNotSame(a1, a2);
  71. assertSame(a1.getClass(), a2.getClass());
  72. assertEquals(a1, a2);
  73. }
  74. /**
  75. * Confirm that the equals method can distinguish all the required fields.
  76. */
  77. @Test
  78. public void testEquals() {
  79. Axis a1 = new CategoryAxis("Test");
  80. Axis a2 = new CategoryAxis("Test");
  81. assertEquals(a1, a2);
  82. // visible flag...
  83. a1.setVisible(false);
  84. assertNotEquals(a1, a2);
  85. a2.setVisible(false);
  86. assertEquals(a1, a2);
  87. // label...
  88. a1.setLabel("New Label");
  89. assertNotEquals(a1, a2);
  90. a2.setLabel("New Label");
  91. assertEquals(a1, a2);
  92. // label font...
  93. a1.setLabelFont(new Font("Dialog", Font.PLAIN, 8));
  94. assertNotEquals(a1, a2);
  95. a2.setLabelFont(new Font("Dialog", Font.PLAIN, 8));
  96. assertEquals(a1, a2);
  97. // label paint...
  98. a1.setLabelPaint(new GradientPaint(1.0f, 2.0f, Color.white,
  99. 3.0f, 4.0f, Color.black));
  100. assertNotEquals(a1, a2);
  101. a2.setLabelPaint(new GradientPaint(1.0f, 2.0f, Color.white,
  102. 3.0f, 4.0f, Color.black));
  103. assertEquals(a1, a2);
  104. // attributed label...
  105. a1.setAttributedLabel(new AttributedString("Hello World!"));
  106. assertNotEquals(a1, a2);
  107. a2.setAttributedLabel(new AttributedString("Hello World!"));
  108. assertEquals(a1, a2);
  109. AttributedString l1 = a1.getAttributedLabel();
  110. l1.addAttribute(TextAttribute.SUPERSCRIPT,
  111. TextAttribute.SUPERSCRIPT_SUB, 1, 2);
  112. a1.setAttributedLabel(l1);
  113. assertNotEquals(a1, a2);
  114. AttributedString l2 = a2.getAttributedLabel();
  115. l2.addAttribute(TextAttribute.SUPERSCRIPT,
  116. TextAttribute.SUPERSCRIPT_SUB, 1, 2);
  117. a2.setAttributedLabel(l2);
  118. assertEquals(a1, a2);
  119. // label insets...
  120. a1.setLabelInsets(new RectangleInsets(10.0, 10.0, 10.0, 10.0));
  121. assertNotEquals(a1, a2);
  122. a2.setLabelInsets(new RectangleInsets(10.0, 10.0, 10.0, 10.0));
  123. assertEquals(a1, a2);
  124. // label angle...
  125. a1.setLabelAngle(1.23);
  126. assertNotEquals(a1, a2);
  127. a2.setLabelAngle(1.23);
  128. assertEquals(a1, a2);
  129. // label location...
  130. a1.setLabelLocation(AxisLabelLocation.HIGH_END);
  131. assertNotEquals(a1, a2);
  132. a2.setLabelLocation(AxisLabelLocation.HIGH_END);
  133. assertEquals(a1, a2);
  134. // axis line visible...
  135. a1.setAxisLineVisible(false);
  136. assertNotEquals(a1, a2);
  137. a2.setAxisLineVisible(false);
  138. assertEquals(a1, a2);
  139. // axis line stroke...
  140. BasicStroke s = new BasicStroke(1.1f);
  141. a1.setAxisLineStroke(s);
  142. assertNotEquals(a1, a2);
  143. a2.setAxisLineStroke(s);
  144. assertEquals(a1, a2);
  145. // axis line paint...
  146. a1.setAxisLinePaint(new GradientPaint(1.0f, 2.0f, Color.red,
  147. 3.0f, 4.0f, Color.black));
  148. assertNotEquals(a1, a2);
  149. a2.setAxisLinePaint(new GradientPaint(1.0f, 2.0f, Color.red,
  150. 3.0f, 4.0f, Color.black));
  151. assertEquals(a1, a2);
  152. // tick labels visible flag...
  153. a1.setTickLabelsVisible(false);
  154. assertNotEquals(a1, a2);
  155. a2.setTickLabelsVisible(false);
  156. assertEquals(a1, a2);
  157. // tick label font...
  158. a1.setTickLabelFont(new Font("Dialog", Font.PLAIN, 12));
  159. assertNotEquals(a1, a2);
  160. a2.setTickLabelFont(new Font("Dialog", Font.PLAIN, 12));
  161. assertEquals(a1, a2);
  162. // tick label paint...
  163. a1.setTickLabelPaint(new GradientPaint(1.0f, 2.0f, Color.yellow,
  164. 3.0f, 4.0f, Color.black));
  165. assertNotEquals(a1, a2);
  166. a2.setTickLabelPaint(new GradientPaint(1.0f, 2.0f, Color.yellow,
  167. 3.0f, 4.0f, Color.black));
  168. assertEquals(a1, a2);
  169. // tick label insets...
  170. a1.setTickLabelInsets(new RectangleInsets(10.0, 10.0, 10.0, 10.0));
  171. assertNotEquals(a1, a2);
  172. a2.setTickLabelInsets(new RectangleInsets(10.0, 10.0, 10.0, 10.0));
  173. assertEquals(a1, a2);
  174. // tick marks visible flag...
  175. a1.setTickMarksVisible(false);
  176. assertNotEquals(a1, a2);
  177. a2.setTickMarksVisible(false);
  178. assertEquals(a1, a2);
  179. // tick mark inside length...
  180. a1.setTickMarkInsideLength(1.23f);
  181. assertNotEquals(a1, a2);
  182. a2.setTickMarkInsideLength(1.23f);
  183. assertEquals(a1, a2);
  184. // tick mark outside length...
  185. a1.setTickMarkOutsideLength(1.23f);
  186. assertNotEquals(a1, a2);
  187. a2.setTickMarkOutsideLength(1.23f);
  188. assertEquals(a1, a2);
  189. // tick mark stroke...
  190. a1.setTickMarkStroke(new BasicStroke(2.0f));
  191. assertNotEquals(a1, a2);
  192. a2.setTickMarkStroke(new BasicStroke(2.0f));
  193. assertEquals(a1, a2);
  194. // tick mark paint...
  195. a1.setTickMarkPaint(new GradientPaint(1.0f, 2.0f, Color.cyan,
  196. 3.0f, 4.0f, Color.black));
  197. assertNotEquals(a1, a2);
  198. a2.setTickMarkPaint(new GradientPaint(1.0f, 2.0f, Color.cyan,
  199. 3.0f, 4.0f, Color.black));
  200. assertEquals(a1, a2);
  201. // fixed dimension...
  202. a1.setFixedDimension(3.21f);
  203. assertNotEquals(a1, a2);
  204. a2.setFixedDimension(3.21f);
  205. assertEquals(a1, a2);
  206. a1.setMinorTickMarksVisible(true);
  207. assertNotEquals(a1, a2);
  208. a2.setMinorTickMarksVisible(true);
  209. assertEquals(a1, a2);
  210. a1.setMinorTickMarkInsideLength(1.23f);
  211. assertNotEquals(a1, a2);
  212. a2.setMinorTickMarkInsideLength(1.23f);
  213. assertEquals(a1, a2);
  214. a1.setMinorTickMarkOutsideLength(3.21f);
  215. assertNotEquals(a1, a2);
  216. a2.setMinorTickMarkOutsideLength(3.21f);
  217. assertEquals(a1, a2);
  218. }
  219. /**
  220. * Two objects that are equal are required to return the same hashCode.
  221. */
  222. @Test
  223. public void testHashCode() {
  224. Axis a1 = new CategoryAxis("Test");
  225. Axis a2 = new CategoryAxis("Test");
  226. assertEquals(a1, a2);
  227. int h1 = a1.hashCode();
  228. int h2 = a2.hashCode();
  229. assertEquals(h1, h2);
  230. }
  231. /**
  232. * Checks that serialization works, particularly with the attributed label.
  233. */
  234. @Test
  235. public void testSerialization() {
  236. Axis a1 = new CategoryAxis("Test");
  237. AttributedString label = new AttributedString("Axis Label");
  238. label.addAttribute(TextAttribute.SUPERSCRIPT,
  239. TextAttribute.SUPERSCRIPT_SUB, 1, 4);
  240. a1.setAttributedLabel(label);
  241. Axis a2 = (Axis) TestUtilities.serialised(a1);
  242. assertEquals(a1, a2);
  243. }
  244. }