001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * --------------------
028: * NumberAxisTests.java
029: * --------------------
030: * (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: NumberAxisTests.java,v 1.1.2.4 2007/02/02 14:16:48 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 26-Mar-2003 : Version 1 (DG);
040: * 14-Aug-2003 : Added tests for equals() method (DG);
041: * 05-Oct-2004 : Added tests to pick up a bug in the auto-range calculation for
042: * a domain axis on an XYPlot using an XYSeriesCollection (DG);
043: * 07-Jan-2005 : Added test for hashCode() (DG);
044: * 11-Jan-2006 : Fixed testAutoRange2() and testAutoRange3() following changes
045: * to BarRenderer (DG);
046: * 20-Feb-2006 : Added rangeType field to equals() test (DG);
047: *
048: */
049:
050: package org.jfree.chart.axis.junit;
051:
052: import java.awt.geom.Rectangle2D;
053: import java.io.ByteArrayInputStream;
054: import java.io.ByteArrayOutputStream;
055: import java.io.ObjectInput;
056: import java.io.ObjectInputStream;
057: import java.io.ObjectOutput;
058: import java.io.ObjectOutputStream;
059: import java.text.DecimalFormat;
060:
061: import junit.framework.Test;
062: import junit.framework.TestCase;
063: import junit.framework.TestSuite;
064:
065: import org.jfree.chart.ChartFactory;
066: import org.jfree.chart.JFreeChart;
067: import org.jfree.chart.axis.NumberAxis;
068: import org.jfree.chart.axis.NumberTickUnit;
069: import org.jfree.chart.plot.CategoryPlot;
070: import org.jfree.chart.plot.PlotOrientation;
071: import org.jfree.chart.plot.XYPlot;
072: import org.jfree.chart.renderer.category.BarRenderer;
073: import org.jfree.data.RangeType;
074: import org.jfree.data.category.DefaultCategoryDataset;
075: import org.jfree.data.xy.XYSeries;
076: import org.jfree.data.xy.XYSeriesCollection;
077: import org.jfree.ui.RectangleEdge;
078:
079: /**
080: * Tests for the {@link NumberAxis} class.
081: */
082: public class NumberAxisTests extends TestCase {
083:
084: /**
085: * Returns the tests as a test suite.
086: *
087: * @return The test suite.
088: */
089: public static Test suite() {
090: return new TestSuite(NumberAxisTests.class);
091: }
092:
093: /**
094: * Constructs a new set of tests.
095: *
096: * @param name the name of the tests.
097: */
098: public NumberAxisTests(String name) {
099: super (name);
100: }
101:
102: /**
103: * Confirm that cloning works.
104: */
105: public void testCloning() {
106: NumberAxis a1 = new NumberAxis("Test");
107: NumberAxis a2 = null;
108: try {
109: a2 = (NumberAxis) a1.clone();
110: } catch (CloneNotSupportedException e) {
111: e.printStackTrace();
112: }
113: assertTrue(a1 != a2);
114: assertTrue(a1.getClass() == a2.getClass());
115: assertTrue(a1.equals(a2));
116: }
117:
118: /**
119: * Confirm that the equals method can distinguish all the required fields.
120: */
121: public void testEquals() {
122:
123: NumberAxis a1 = new NumberAxis("Test");
124: NumberAxis a2 = new NumberAxis("Test");
125: assertTrue(a1.equals(a2));
126:
127: //private boolean autoRangeIncludesZero;
128: a1.setAutoRangeIncludesZero(false);
129: assertFalse(a1.equals(a2));
130: a2.setAutoRangeIncludesZero(false);
131: assertTrue(a1.equals(a2));
132:
133: //private boolean autoRangeStickyZero;
134: a1.setAutoRangeStickyZero(false);
135: assertFalse(a1.equals(a2));
136: a2.setAutoRangeStickyZero(false);
137: assertTrue(a1.equals(a2));
138:
139: //private NumberTickUnit tickUnit;
140: a1.setTickUnit(new NumberTickUnit(25.0));
141: assertFalse(a1.equals(a2));
142: a2.setTickUnit(new NumberTickUnit(25.0));
143: assertTrue(a1.equals(a2));
144:
145: //private NumberFormat numberFormatOverride;
146: a1.setNumberFormatOverride(new DecimalFormat("0.00"));
147: assertFalse(a1.equals(a2));
148: a2.setNumberFormatOverride(new DecimalFormat("0.00"));
149: assertTrue(a1.equals(a2));
150:
151: a1.setRangeType(RangeType.POSITIVE);
152: assertFalse(a1.equals(a2));
153: a2.setRangeType(RangeType.POSITIVE);
154: assertTrue(a1.equals(a2));
155:
156: }
157:
158: /**
159: * Two objects that are equal are required to return the same hashCode.
160: */
161: public void testHashCode() {
162: NumberAxis a1 = new NumberAxis("Test");
163: NumberAxis a2 = new NumberAxis("Test");
164: assertTrue(a1.equals(a2));
165: int h1 = a1.hashCode();
166: int h2 = a2.hashCode();
167: assertEquals(h1, h2);
168: }
169:
170: private static final double EPSILON = 0.0000001;
171:
172: /**
173: * Test the translation of Java2D values to data values.
174: */
175: public void testTranslateJava2DToValue() {
176: NumberAxis axis = new NumberAxis();
177: axis.setRange(50.0, 100.0);
178: Rectangle2D dataArea = new Rectangle2D.Double(10.0, 50.0,
179: 400.0, 300.0);
180: double y1 = axis.java2DToValue(75.0, dataArea,
181: RectangleEdge.LEFT);
182: assertEquals(y1, 95.8333333, EPSILON);
183: double y2 = axis.java2DToValue(75.0, dataArea,
184: RectangleEdge.RIGHT);
185: assertEquals(y2, 95.8333333, EPSILON);
186: double x1 = axis.java2DToValue(75.0, dataArea,
187: RectangleEdge.TOP);
188: assertEquals(x1, 58.125, EPSILON);
189: double x2 = axis.java2DToValue(75.0, dataArea,
190: RectangleEdge.BOTTOM);
191: assertEquals(x2, 58.125, EPSILON);
192: axis.setInverted(true);
193: double y3 = axis.java2DToValue(75.0, dataArea,
194: RectangleEdge.LEFT);
195: assertEquals(y3, 54.1666667, EPSILON);
196: double y4 = axis.java2DToValue(75.0, dataArea,
197: RectangleEdge.RIGHT);
198: assertEquals(y4, 54.1666667, EPSILON);
199: double x3 = axis.java2DToValue(75.0, dataArea,
200: RectangleEdge.TOP);
201: assertEquals(x3, 91.875, EPSILON);
202: double x4 = axis.java2DToValue(75.0, dataArea,
203: RectangleEdge.BOTTOM);
204: assertEquals(x4, 91.875, EPSILON);
205: }
206:
207: /**
208: * Serialize an instance, restore it, and check for equality.
209: */
210: public void testSerialization() {
211:
212: NumberAxis a1 = new NumberAxis("Test Axis");
213: NumberAxis a2 = null;
214:
215: try {
216: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
217: ObjectOutput out = new ObjectOutputStream(buffer);
218: out.writeObject(a1);
219: out.close();
220:
221: ObjectInput in = new ObjectInputStream(
222: new ByteArrayInputStream(buffer.toByteArray()));
223: a2 = (NumberAxis) in.readObject();
224: in.close();
225: } catch (Exception e) {
226: e.printStackTrace();
227: }
228: assertEquals(a1, a2);
229:
230: }
231:
232: /**
233: * A simple test for the auto-range calculation looking at a
234: * NumberAxis used as the range axis for a CategoryPlot.
235: */
236: public void testAutoRange1() {
237: DefaultCategoryDataset dataset = new DefaultCategoryDataset();
238: dataset.setValue(100.0, "Row 1", "Column 1");
239: dataset.setValue(200.0, "Row 1", "Column 2");
240: JFreeChart chart = ChartFactory.createBarChart("Test",
241: "Categories", "Value", dataset,
242: PlotOrientation.VERTICAL, false, false, false);
243: CategoryPlot plot = (CategoryPlot) chart.getPlot();
244: NumberAxis axis = (NumberAxis) plot.getRangeAxis();
245: assertEquals(axis.getLowerBound(), 0.0, EPSILON);
246: assertEquals(axis.getUpperBound(), 210.0, EPSILON);
247: }
248:
249: /**
250: * A simple test for the auto-range calculation looking at a
251: * NumberAxis used as the range axis for a CategoryPlot. In this
252: * case, the 'autoRangeIncludesZero' flag is set to false.
253: */
254: public void testAutoRange2() {
255: DefaultCategoryDataset dataset = new DefaultCategoryDataset();
256: dataset.setValue(100.0, "Row 1", "Column 1");
257: dataset.setValue(200.0, "Row 1", "Column 2");
258: JFreeChart chart = ChartFactory.createLineChart("Test",
259: "Categories", "Value", dataset,
260: PlotOrientation.VERTICAL, false, false, false);
261: CategoryPlot plot = (CategoryPlot) chart.getPlot();
262: NumberAxis axis = (NumberAxis) plot.getRangeAxis();
263: axis.setAutoRangeIncludesZero(false);
264: assertEquals(axis.getLowerBound(), 95.0, EPSILON);
265: assertEquals(axis.getUpperBound(), 205.0, EPSILON);
266: }
267:
268: /**
269: * A simple test for the auto-range calculation looking at a
270: * NumberAxis used as the range axis for a CategoryPlot. In this
271: * case, the 'autoRangeIncludesZero' flag is set to false AND the
272: * original dataset is replaced with a new dataset.
273: */
274: public void testAutoRange3() {
275: DefaultCategoryDataset dataset = new DefaultCategoryDataset();
276: dataset.setValue(100.0, "Row 1", "Column 1");
277: dataset.setValue(200.0, "Row 1", "Column 2");
278: JFreeChart chart = ChartFactory.createLineChart("Test",
279: "Categories", "Value", dataset,
280: PlotOrientation.VERTICAL, false, false, false);
281: CategoryPlot plot = (CategoryPlot) chart.getPlot();
282: NumberAxis axis = (NumberAxis) plot.getRangeAxis();
283: axis.setAutoRangeIncludesZero(false);
284: assertEquals(axis.getLowerBound(), 95.0, EPSILON);
285: assertEquals(axis.getUpperBound(), 205.0, EPSILON);
286:
287: // now replacing the dataset should update the axis range...
288: DefaultCategoryDataset dataset2 = new DefaultCategoryDataset();
289: dataset2.setValue(900.0, "Row 1", "Column 1");
290: dataset2.setValue(1000.0, "Row 1", "Column 2");
291: plot.setDataset(dataset2);
292: assertEquals(axis.getLowerBound(), 895.0, EPSILON);
293: assertEquals(axis.getUpperBound(), 1005.0, EPSILON);
294: }
295:
296: /**
297: * A check for the interaction between the 'autoRangeIncludesZero' flag
298: * and the base setting in the BarRenderer.
299: */
300: public void testAutoRange4() {
301: DefaultCategoryDataset dataset = new DefaultCategoryDataset();
302: dataset.setValue(100.0, "Row 1", "Column 1");
303: dataset.setValue(200.0, "Row 1", "Column 2");
304: JFreeChart chart = ChartFactory.createBarChart("Test",
305: "Categories", "Value", dataset,
306: PlotOrientation.VERTICAL, false, false, false);
307: CategoryPlot plot = (CategoryPlot) chart.getPlot();
308: NumberAxis axis = (NumberAxis) plot.getRangeAxis();
309: axis.setAutoRangeIncludesZero(false);
310: BarRenderer br = (BarRenderer) plot.getRenderer();
311: br.setIncludeBaseInRange(false);
312: assertEquals(95.0, axis.getLowerBound(), EPSILON);
313: assertEquals(205.0, axis.getUpperBound(), EPSILON);
314:
315: br.setIncludeBaseInRange(true);
316: assertEquals(0.0, axis.getLowerBound(), EPSILON);
317: assertEquals(210.0, axis.getUpperBound(), EPSILON);
318:
319: axis.setAutoRangeIncludesZero(true);
320: assertEquals(0.0, axis.getLowerBound(), EPSILON);
321: assertEquals(210.0, axis.getUpperBound(), EPSILON);
322:
323: br.setIncludeBaseInRange(true);
324: assertEquals(0.0, axis.getLowerBound(), EPSILON);
325: assertEquals(210.0, axis.getUpperBound(), EPSILON);
326:
327: // now replacing the dataset should update the axis range...
328: DefaultCategoryDataset dataset2 = new DefaultCategoryDataset();
329: dataset2.setValue(900.0, "Row 1", "Column 1");
330: dataset2.setValue(1000.0, "Row 1", "Column 2");
331: plot.setDataset(dataset2);
332: assertEquals(0.0, axis.getLowerBound(), EPSILON);
333: assertEquals(1050.0, axis.getUpperBound(), EPSILON);
334:
335: br.setIncludeBaseInRange(false);
336: assertEquals(0.0, axis.getLowerBound(), EPSILON);
337: assertEquals(1050.0, axis.getUpperBound(), EPSILON);
338:
339: axis.setAutoRangeIncludesZero(false);
340: assertEquals(895.0, axis.getLowerBound(), EPSILON);
341: assertEquals(1005.0, axis.getUpperBound(), EPSILON);
342: }
343:
344: /**
345: * Checks that the auto-range for the domain axis on an XYPlot is
346: * working as expected.
347: */
348: public void testXYAutoRange1() {
349: XYSeries series = new XYSeries("Series 1");
350: series.add(1.0, 1.0);
351: series.add(2.0, 2.0);
352: series.add(3.0, 3.0);
353: XYSeriesCollection dataset = new XYSeriesCollection();
354: dataset.addSeries(series);
355: JFreeChart chart = ChartFactory.createScatterPlot("Test", "X",
356: "Y", dataset, PlotOrientation.VERTICAL, false, false,
357: false);
358: XYPlot plot = (XYPlot) chart.getPlot();
359: NumberAxis axis = (NumberAxis) plot.getDomainAxis();
360: axis.setAutoRangeIncludesZero(false);
361: assertEquals(0.9, axis.getLowerBound(), EPSILON);
362: assertEquals(3.1, axis.getUpperBound(), EPSILON);
363: }
364:
365: /**
366: * Checks that the auto-range for the range axis on an XYPlot is
367: * working as expected.
368: */
369: public void testXYAutoRange2() {
370: XYSeries series = new XYSeries("Series 1");
371: series.add(1.0, 1.0);
372: series.add(2.0, 2.0);
373: series.add(3.0, 3.0);
374: XYSeriesCollection dataset = new XYSeriesCollection();
375: dataset.addSeries(series);
376: JFreeChart chart = ChartFactory.createScatterPlot("Test", "X",
377: "Y", dataset, PlotOrientation.VERTICAL, false, false,
378: false);
379: XYPlot plot = (XYPlot) chart.getPlot();
380: NumberAxis axis = (NumberAxis) plot.getRangeAxis();
381: axis.setAutoRangeIncludesZero(false);
382: assertEquals(0.9, axis.getLowerBound(), EPSILON);
383: assertEquals(3.1, axis.getUpperBound(), EPSILON);
384: }
385:
386: // /**
387: // * Some checks for the setRangeType() method.
388: // */
389: // public void testSetRangeType() {
390: //
391: // NumberAxis axis = new NumberAxis("X");
392: // axis.setRangeType(RangeType.POSITIVE);
393: // assertEquals(RangeType.POSITIVE, axis.getRangeType());
394: //
395: // // test a change to RangeType.POSITIVE
396: // axis.setRangeType(RangeType.FULL);
397: // axis.setRange(-5.0, 5.0);
398: // axis.setRangeType(RangeType.POSITIVE);
399: // assertEquals(new Range(0.0, 5.0), axis.getRange());
400: //
401: // axis.setRangeType(RangeType.FULL);
402: // axis.setRange(-10.0, -5.0);
403: // axis.setRangeType(RangeType.POSITIVE);
404: // assertEquals(new Range(0.0, axis.getAutoRangeMinimumSize()),
405: // axis.getRange());
406: //
407: // // test a change to RangeType.NEGATIVE
408: // axis.setRangeType(RangeType.FULL);
409: // axis.setRange(-5.0, 5.0);
410: // axis.setRangeType(RangeType.NEGATIVE);
411: // assertEquals(new Range(-5.0, 0.0), axis.getRange());
412: //
413: // axis.setRangeType(RangeType.FULL);
414: // axis.setRange(5.0, 10.0);
415: // axis.setRangeType(RangeType.NEGATIVE);
416: // assertEquals(new Range(-axis.getAutoRangeMinimumSize(), 0.0),
417: // axis.getRange());
418: //
419: // // try null
420: // boolean pass = false;
421: // try {
422: // axis.setRangeType(null);
423: // }
424: // catch (IllegalArgumentException e) {
425: // pass = true;
426: // }
427: // assertTrue(pass);
428: // }
429:
430: /**
431: * Some checks for the setLowerBound() method.
432: */
433: public void testSetLowerBound() {
434: NumberAxis axis = new NumberAxis("X");
435: axis.setRange(0.0, 10.0);
436: axis.setLowerBound(5.0);
437: assertEquals(5.0, axis.getLowerBound(), EPSILON);
438: axis.setLowerBound(10.0);
439: assertEquals(10.0, axis.getLowerBound(), EPSILON);
440: assertEquals(11.0, axis.getUpperBound(), EPSILON);
441:
442: //axis.setRangeType(RangeType.POSITIVE);
443: //axis.setLowerBound(-5.0);
444: //assertEquals(0.0, axis.getLowerBound(), EPSILON);
445: }
446:
447: }
|