LogAxisTest.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. * LogAxisTest.java
  29. * ----------------
  30. * (C) Copyright 2007-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. * 11-Jul-2007 : Version 1 (DG);
  38. * 08-Apr-2008 : Fixed incorrect testEquals() method (DG);
  39. * 28-Oct-2011 : Cdded test for endless loop, # 3429707 (MH);
  40. */
  41. package org.jfree.chart.axis;
  42. import static org.junit.Assert.assertEquals;
  43. import static org.junit.Assert.assertFalse;
  44. import static org.junit.Assert.assertTrue;
  45. import java.awt.Graphics2D;
  46. import java.awt.geom.Rectangle2D;
  47. import java.awt.image.BufferedImage;
  48. import org.jfree.chart.ChartFactory;
  49. import org.jfree.chart.JFreeChart;
  50. import org.jfree.chart.TestUtilities;
  51. import org.jfree.chart.plot.CategoryPlot;
  52. import org.jfree.chart.plot.PlotOrientation;
  53. import org.jfree.chart.plot.XYPlot;
  54. import org.jfree.data.category.DefaultCategoryDataset;
  55. import org.jfree.data.xy.XYSeries;
  56. import org.jfree.data.xy.XYSeriesCollection;
  57. import org.jfree.ui.RectangleEdge;
  58. import org.junit.Test;
  59. /**
  60. * Tests for the {@link LogAxis} class.
  61. */
  62. public class LogAxisTest {
  63. /**
  64. * Confirm that cloning works.
  65. */
  66. @Test
  67. public void testCloning() throws CloneNotSupportedException {
  68. LogAxis a1 = new LogAxis("Test");
  69. LogAxis a2 = (LogAxis) a1.clone();
  70. assertTrue(a1 != a2);
  71. assertTrue(a1.getClass() == a2.getClass());
  72. assertTrue(a1.equals(a2));
  73. }
  74. /**
  75. * Confirm that the equals method can distinguish all the required fields.
  76. */
  77. @Test
  78. public void testEquals() {
  79. LogAxis a1 = new LogAxis("Test");
  80. LogAxis a2 = new LogAxis("Test");
  81. assertTrue(a1.equals(a2));
  82. a1.setBase(2.0);
  83. assertFalse(a1.equals(a2));
  84. a2.setBase(2.0);
  85. assertTrue(a1.equals(a2));
  86. a1.setSmallestValue(0.1);
  87. assertFalse(a1.equals(a2));
  88. a2.setSmallestValue(0.1);
  89. assertTrue(a1.equals(a2));
  90. a1.setMinorTickCount(8);
  91. assertFalse(a1.equals(a2));
  92. a2.setMinorTickCount(8);
  93. assertTrue(a1.equals(a2));
  94. }
  95. /**
  96. * Two objects that are equal are required to return the same hashCode.
  97. */
  98. @Test
  99. public void testHashCode() {
  100. LogAxis a1 = new LogAxis("Test");
  101. LogAxis a2 = new LogAxis("Test");
  102. assertTrue(a1.equals(a2));
  103. int h1 = a1.hashCode();
  104. int h2 = a2.hashCode();
  105. assertEquals(h1, h2);
  106. }
  107. private static final double EPSILON = 0.0000001;
  108. /**
  109. * Test the translation of Java2D values to data values.
  110. */
  111. @Test
  112. public void testTranslateJava2DToValue() {
  113. LogAxis axis = new LogAxis();
  114. axis.setRange(50.0, 100.0);
  115. Rectangle2D dataArea = new Rectangle2D.Double(10.0, 50.0, 400.0, 300.0);
  116. double y1 = axis.java2DToValue(75.0, dataArea, RectangleEdge.LEFT);
  117. assertEquals(94.3874312681693, y1, EPSILON);
  118. double y2 = axis.java2DToValue(75.0, dataArea, RectangleEdge.RIGHT);
  119. assertEquals(94.3874312681693, y2, EPSILON);
  120. double x1 = axis.java2DToValue(75.0, dataArea, RectangleEdge.TOP);
  121. assertEquals(55.961246381405, x1, EPSILON);
  122. double x2 = axis.java2DToValue(75.0, dataArea, RectangleEdge.BOTTOM);
  123. assertEquals(55.961246381405, x2, EPSILON);
  124. axis.setInverted(true);
  125. double y3 = axis.java2DToValue(75.0, dataArea, RectangleEdge.LEFT);
  126. assertEquals(52.9731547179647, y3, EPSILON);
  127. double y4 = axis.java2DToValue(75.0, dataArea, RectangleEdge.RIGHT);
  128. assertEquals(52.9731547179647, y4, EPSILON);
  129. double x3 = axis.java2DToValue(75.0, dataArea, RectangleEdge.TOP);
  130. assertEquals(89.3475453695651, x3, EPSILON);
  131. double x4 = axis.java2DToValue(75.0, dataArea, RectangleEdge.BOTTOM);
  132. assertEquals(89.3475453695651, x4, EPSILON);
  133. }
  134. /**
  135. * Serialize an instance, restore it, and check for equality.
  136. */
  137. @Test
  138. public void testSerialization() {
  139. LogAxis a1 = new LogAxis("Test Axis");
  140. LogAxis a2 = (LogAxis) TestUtilities.serialised(a1);
  141. assertEquals(a1, a2);
  142. }
  143. /**
  144. * A simple test for the auto-range calculation looking at a
  145. * LogAxis used as the range axis for a CategoryPlot.
  146. */
  147. @Test
  148. public void testAutoRange1() {
  149. DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  150. dataset.setValue(100.0, "Row 1", "Column 1");
  151. dataset.setValue(200.0, "Row 1", "Column 2");
  152. JFreeChart chart = ChartFactory.createBarChart("Test", "Categories",
  153. "Value", dataset);
  154. CategoryPlot plot = (CategoryPlot) chart.getPlot();
  155. LogAxis axis = new LogAxis("Log(Y)");
  156. plot.setRangeAxis(axis);
  157. assertEquals(0.0, axis.getLowerBound(), EPSILON);
  158. assertEquals(2.6066426411261268E7, axis.getUpperBound(), EPSILON);
  159. }
  160. /**
  161. * A simple test for the auto-range calculation looking at a
  162. * NumberAxis used as the range axis for a CategoryPlot. In this
  163. * case, the original dataset is replaced with a new dataset.
  164. */
  165. @Test
  166. public void testAutoRange3() {
  167. DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  168. dataset.setValue(100.0, "Row 1", "Column 1");
  169. dataset.setValue(200.0, "Row 1", "Column 2");
  170. JFreeChart chart = ChartFactory.createLineChart("Test", "Categories",
  171. "Value", dataset, PlotOrientation.VERTICAL, false, false,
  172. false);
  173. CategoryPlot plot = (CategoryPlot) chart.getPlot();
  174. LogAxis axis = new LogAxis("Log(Y)");
  175. plot.setRangeAxis(axis);
  176. assertEquals(96.59363289248458, axis.getLowerBound(), EPSILON);
  177. assertEquals(207.0529847682752, axis.getUpperBound(), EPSILON);
  178. // now replacing the dataset should update the axis range...
  179. DefaultCategoryDataset dataset2 = new DefaultCategoryDataset();
  180. dataset2.setValue(900.0, "Row 1", "Column 1");
  181. dataset2.setValue(1000.0, "Row 1", "Column 2");
  182. plot.setDataset(dataset2);
  183. assertEquals(895.2712433374774, axis.getLowerBound(), EPSILON);
  184. assertEquals(1005.2819262292991, axis.getUpperBound(), EPSILON);
  185. }
  186. /**
  187. * Checks that the auto-range for the domain axis on an XYPlot is
  188. * working as expected.
  189. */
  190. @Test
  191. public void testXYAutoRange1() {
  192. XYSeries series = new XYSeries("Series 1");
  193. series.add(1.0, 1.0);
  194. series.add(2.0, 2.0);
  195. series.add(3.0, 3.0);
  196. XYSeriesCollection dataset = new XYSeriesCollection();
  197. dataset.addSeries(series);
  198. JFreeChart chart = ChartFactory.createScatterPlot("Test", "X", "Y",
  199. dataset);
  200. XYPlot plot = (XYPlot) chart.getPlot();
  201. LogAxis axis = new LogAxis("Log(Y)");
  202. plot.setRangeAxis(axis);
  203. assertEquals(0.9465508226401592, axis.getLowerBound(), EPSILON);
  204. assertEquals(3.1694019256486126, axis.getUpperBound(), EPSILON);
  205. }
  206. /**
  207. * Checks that the auto-range for the range axis on an XYPlot is
  208. * working as expected.
  209. */
  210. @Test
  211. public void testXYAutoRange2() {
  212. XYSeries series = new XYSeries("Series 1");
  213. series.add(1.0, 1.0);
  214. series.add(2.0, 2.0);
  215. series.add(3.0, 3.0);
  216. XYSeriesCollection dataset = new XYSeriesCollection();
  217. dataset.addSeries(series);
  218. JFreeChart chart = ChartFactory.createScatterPlot("Test", "X", "Y",
  219. dataset);
  220. XYPlot plot = (XYPlot) chart.getPlot();
  221. LogAxis axis = new LogAxis("Log(Y)");
  222. plot.setRangeAxis(axis);
  223. assertEquals(0.9465508226401592, axis.getLowerBound(), EPSILON);
  224. assertEquals(3.1694019256486126, axis.getUpperBound(), EPSILON);
  225. }
  226. /**
  227. * Some checks for the setLowerBound() method.
  228. */
  229. @Test
  230. public void testSetLowerBound() {
  231. LogAxis axis = new LogAxis("X");
  232. axis.setRange(0.0, 10.0);
  233. axis.setLowerBound(5.0);
  234. assertEquals(5.0, axis.getLowerBound(), EPSILON);
  235. axis.setLowerBound(10.0);
  236. assertEquals(10.0, axis.getLowerBound(), EPSILON);
  237. assertEquals(11.0, axis.getUpperBound(), EPSILON);
  238. }
  239. /**
  240. * Checks the default value for the tickMarksVisible flag.
  241. */
  242. @Test
  243. public void testTickMarksVisibleDefault() {
  244. LogAxis axis = new LogAxis("Log Axis");
  245. assertTrue(axis.isTickMarksVisible());
  246. }
  247. /**
  248. * Checks that a TickUnit with a size of 0 doesn't crash.
  249. */
  250. @Test
  251. public void testRefreshTicksWithZeroTickUnit() {
  252. LogAxis axis = new LogAxis();
  253. AxisState state = new AxisState();
  254. BufferedImage image = new BufferedImage(200, 100,
  255. BufferedImage.TYPE_INT_ARGB);
  256. Graphics2D g2 = image.createGraphics();
  257. Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, 200, 100);
  258. axis.refreshTicks(g2, state, area, RectangleEdge.TOP);
  259. }
  260. }