PeriodAxisTest.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. * PeriodAxisTest.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. * 10-Jun-2003 : Version 1 (DG);
  38. * 07-Jan-2005 : Added test for hashCode() method (DG);
  39. * 08-Apr-2008 : Added test1932146() (DG);
  40. * 16-Jan-2009 : Added test2490803() (DG);
  41. * 02-Mar-2009 : Added testEqualsWithLocale (DG);
  42. *
  43. */
  44. package org.jfree.chart.axis;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertTrue;
  48. import java.awt.BasicStroke;
  49. import java.awt.Color;
  50. import java.awt.Stroke;
  51. import java.text.SimpleDateFormat;
  52. import java.util.GregorianCalendar;
  53. import java.util.Locale;
  54. import java.util.SimpleTimeZone;
  55. import java.util.TimeZone;
  56. import org.jfree.chart.TestUtilities;
  57. import org.jfree.chart.event.AxisChangeEvent;
  58. import org.jfree.chart.event.AxisChangeListener;
  59. import org.jfree.data.Range;
  60. import org.jfree.data.time.DateRange;
  61. import org.jfree.data.time.Day;
  62. import org.jfree.data.time.Minute;
  63. import org.jfree.data.time.Month;
  64. import org.jfree.data.time.Quarter;
  65. import org.jfree.data.time.Second;
  66. import org.jfree.data.time.Year;
  67. import org.junit.Test;
  68. /**
  69. * Tests for the {@link PeriodAxis} class.
  70. */
  71. public class PeriodAxisTest implements AxisChangeListener {
  72. /** The last event received. */
  73. private AxisChangeEvent lastEvent;
  74. /**
  75. * Receives and records an {@link AxisChangeEvent}.
  76. *
  77. * @param event the event.
  78. */
  79. @Override
  80. public void axisChanged(AxisChangeEvent event) {
  81. this.lastEvent = event;
  82. }
  83. /**
  84. * Confirm that the equals() method can distinguish all the required fields.
  85. */
  86. @Test
  87. public void testEquals() {
  88. PeriodAxis a1 = new PeriodAxis("Test");
  89. PeriodAxis a2 = new PeriodAxis("Test");
  90. assertTrue(a1.equals(a2));
  91. assertTrue(a2.equals(a1));
  92. a1.setFirst(new Year(2000));
  93. assertFalse(a1.equals(a2));
  94. a2.setFirst(new Year(2000));
  95. assertTrue(a1.equals(a2));
  96. a1.setLast(new Year(2004));
  97. assertFalse(a1.equals(a2));
  98. a2.setLast(new Year(2004));
  99. assertTrue(a1.equals(a2));
  100. a1.setTimeZone(TimeZone.getTimeZone("Pacific/Auckland"));
  101. assertFalse(a1.equals(a2));
  102. a2.setTimeZone(TimeZone.getTimeZone("Pacific/Auckland"));
  103. assertTrue(a1.equals(a2));
  104. a1.setAutoRangeTimePeriodClass(Quarter.class);
  105. assertFalse(a1.equals(a2));
  106. a2.setAutoRangeTimePeriodClass(Quarter.class);
  107. assertTrue(a1.equals(a2));
  108. PeriodAxisLabelInfo info[] = new PeriodAxisLabelInfo[1];
  109. info[0] = new PeriodAxisLabelInfo(Month.class,
  110. new SimpleDateFormat("MMM"));
  111. a1.setLabelInfo(info);
  112. assertFalse(a1.equals(a2));
  113. a2.setLabelInfo(info);
  114. assertTrue(a1.equals(a2));
  115. a1.setMajorTickTimePeriodClass(Minute.class);
  116. assertFalse(a1.equals(a2));
  117. a2.setMajorTickTimePeriodClass(Minute.class);
  118. assertTrue(a1.equals(a2));
  119. a1.setMinorTickMarksVisible(!a1.isMinorTickMarksVisible());
  120. assertFalse(a1.equals(a2));
  121. a2.setMinorTickMarksVisible(a1.isMinorTickMarksVisible());
  122. assertTrue(a1.equals(a2));
  123. a1.setMinorTickTimePeriodClass(Minute.class);
  124. assertFalse(a1.equals(a2));
  125. a2.setMinorTickTimePeriodClass(Minute.class);
  126. assertTrue(a1.equals(a2));
  127. Stroke s = new BasicStroke(1.23f);
  128. a1.setMinorTickMarkStroke(s);
  129. assertFalse(a1.equals(a2));
  130. a2.setMinorTickMarkStroke(s);
  131. assertTrue(a1.equals(a2));
  132. a1.setMinorTickMarkPaint(Color.blue);
  133. assertFalse(a1.equals(a2));
  134. a2.setMinorTickMarkPaint(Color.blue);
  135. assertTrue(a1.equals(a2));
  136. }
  137. /**
  138. * Confirm that the equals() method can distinguish the locale field (which
  139. * is new in version 1.0.13).
  140. */
  141. @Test
  142. public void testEqualsWithLocale() {
  143. PeriodAxis a1 = new PeriodAxis("Test", new Year(2000), new Year(2009),
  144. TimeZone.getDefault(), Locale.JAPAN);
  145. PeriodAxis a2 = new PeriodAxis("Test", new Year(2000), new Year(2009),
  146. TimeZone.getDefault(), Locale.JAPAN);
  147. assertTrue(a1.equals(a2));
  148. assertTrue(a2.equals(a1));
  149. a1 = new PeriodAxis("Test", new Year(2000), new Year(2009),
  150. TimeZone.getDefault(), Locale.UK);
  151. assertFalse(a1.equals(a2));
  152. a2 = new PeriodAxis("Test", new Year(2000), new Year(2009),
  153. TimeZone.getDefault(), Locale.UK);
  154. assertTrue(a1.equals(a2));
  155. }
  156. /**
  157. * Two objects that are equal are required to return the same hashCode.
  158. */
  159. @Test
  160. public void testHashCode() {
  161. PeriodAxis a1 = new PeriodAxis("Test");
  162. PeriodAxis a2 = new PeriodAxis("Test");
  163. assertTrue(a1.equals(a2));
  164. int h1 = a1.hashCode();
  165. int h2 = a2.hashCode();
  166. assertEquals(h1, h2);
  167. }
  168. /**
  169. * Confirm that cloning works.
  170. */
  171. @Test
  172. public void testCloning() throws CloneNotSupportedException {
  173. PeriodAxis a1 = new PeriodAxis("Test");
  174. PeriodAxis a2 = (PeriodAxis) a1.clone();
  175. assertTrue(a1 != a2);
  176. assertTrue(a1.getClass() == a2.getClass());
  177. assertTrue(a1.equals(a2));
  178. // some checks that the clone is independent of the original
  179. a1.setLabel("New Label");
  180. assertFalse(a1.equals(a2));
  181. a2.setLabel("New Label");
  182. assertTrue(a1.equals(a2));
  183. a1.setFirst(new Year(1920));
  184. assertFalse(a1.equals(a2));
  185. a2.setFirst(new Year(1920));
  186. assertTrue(a1.equals(a2));
  187. a1.setLast(new Year(2020));
  188. assertFalse(a1.equals(a2));
  189. a2.setLast(new Year(2020));
  190. assertTrue(a1.equals(a2));
  191. PeriodAxisLabelInfo[] info = new PeriodAxisLabelInfo[2];
  192. info[0] = new PeriodAxisLabelInfo(Day.class, new SimpleDateFormat("d"));
  193. info[1] = new PeriodAxisLabelInfo(Year.class,
  194. new SimpleDateFormat("yyyy"));
  195. a1.setLabelInfo(info);
  196. assertFalse(a1.equals(a2));
  197. a2.setLabelInfo(info);
  198. assertTrue(a1.equals(a2));
  199. a1.setAutoRangeTimePeriodClass(Second.class);
  200. assertFalse(a1.equals(a2));
  201. a2.setAutoRangeTimePeriodClass(Second.class);
  202. assertTrue(a1.equals(a2));
  203. a1.setTimeZone(new SimpleTimeZone(123, "Bogus"));
  204. assertFalse(a1.equals(a2));
  205. a2.setTimeZone(new SimpleTimeZone(123, "Bogus"));
  206. assertTrue(a1.equals(a2));
  207. }
  208. /**
  209. * Serialize an instance, restore it, and check for equality.
  210. */
  211. @Test
  212. public void testSerialization() {
  213. PeriodAxis a1 = new PeriodAxis("Test Axis");
  214. PeriodAxis a2 = (PeriodAxis) TestUtilities.serialised(a1);
  215. boolean b = a1.equals(a2);
  216. assertTrue(b);
  217. }
  218. /**
  219. * A test for bug 1932146.
  220. */
  221. @Test
  222. public void test1932146() {
  223. PeriodAxis axis = new PeriodAxis("TestAxis");
  224. axis.addChangeListener(this);
  225. this.lastEvent = null;
  226. axis.setRange(new DateRange(0L, 1000L));
  227. assertTrue(this.lastEvent != null);
  228. }
  229. private static final double EPSILON = 0.0000000001;
  230. /**
  231. * A test for the setRange() method (because the axis shows whole time
  232. * periods, the range set for the axis will most likely be wider than the
  233. * one specified).
  234. */
  235. @Test
  236. public void test2490803() {
  237. Locale savedLocale = Locale.getDefault();
  238. TimeZone savedTimeZone = TimeZone.getDefault();
  239. try {
  240. Locale.setDefault(Locale.FRANCE);
  241. TimeZone.setDefault(TimeZone.getTimeZone("Europe/Paris"));
  242. GregorianCalendar c0 = new GregorianCalendar();
  243. c0.clear();
  244. /* c0.set(2009, Calendar.JANUARY, 16, 12, 34, 56);
  245. System.out.println(c0.getTime().getTime());
  246. c0.clear();
  247. c0.set(2009, Calendar.JANUARY, 17, 12, 34, 56);
  248. System.out.println(c0.getTime().getTime()); */
  249. PeriodAxis axis = new PeriodAxis("TestAxis");
  250. axis.setRange(new Range(1232105696000L, 1232192096000L), false,
  251. false);
  252. Range r = axis.getRange();
  253. Day d0 = new Day(16, 1, 2009);
  254. Day d1 = new Day(17, 1, 2009);
  255. assertEquals(d0.getFirstMillisecond(), r.getLowerBound(), EPSILON);
  256. assertEquals(d1.getLastMillisecond() + 1.0, r.getUpperBound(),
  257. EPSILON);
  258. }
  259. finally {
  260. TimeZone.setDefault(savedTimeZone);
  261. Locale.setDefault(savedLocale);
  262. }
  263. }
  264. }