001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2005, 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: * LogarithmicAxisTests.java
029: * -------------------------
030: * (C) Copyright 2003-2005, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: LogarithmicAxisTests.java,v 1.1.2.2 2007/03/02 15:35:58 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 26-Mar-2003 : Version 1 (DG);
040: * 02-Mar-2007 : Added tests from bug report 880597 (DG);
041: *
042: */
043:
044: package org.jfree.chart.axis.junit;
045:
046: import java.awt.geom.Rectangle2D;
047: import java.io.ByteArrayInputStream;
048: import java.io.ByteArrayOutputStream;
049: import java.io.ObjectInput;
050: import java.io.ObjectInputStream;
051: import java.io.ObjectOutput;
052: import java.io.ObjectOutputStream;
053:
054: import junit.framework.Test;
055: import junit.framework.TestCase;
056: import junit.framework.TestSuite;
057:
058: import org.jfree.chart.axis.LogarithmicAxis;
059: import org.jfree.ui.RectangleEdge;
060:
061: /**
062: * Tests for the {@link LogarithmicAxis} class.
063: */
064: public class LogarithmicAxisTests extends TestCase {
065:
066: static class MyLogarithmicAxis extends LogarithmicAxis {
067:
068: public MyLogarithmicAxis(String label) {
069: super (label);
070: }
071:
072: /* (non-Javadoc)
073: * @see org.jfree.chart.axis.LogarithmicAxis#switchedLog10(double)
074: */
075: protected double switchedLog10(double val) {
076: return super .switchedLog10(val);
077: }
078:
079: }
080:
081: /** Tolerance for floating point comparisons */
082: public static double EPSILON = 0.000001;
083:
084: MyLogarithmicAxis axis = null;
085:
086: /**
087: * Returns the tests as a test suite.
088: *
089: * @return The test suite.
090: */
091: public static Test suite() {
092: return new TestSuite(LogarithmicAxisTests.class);
093: }
094:
095: /**
096: * Constructs a new set of tests.
097: *
098: * @param name the name of the tests.
099: */
100: public LogarithmicAxisTests(String name) {
101: super (name);
102: }
103:
104: /**
105: * Sets up a new axis.
106: *
107: * @throws Exception
108: */
109: protected void setUp() throws Exception {
110: this .axis = new MyLogarithmicAxis("Value (log)");
111: this .axis.setAllowNegativesFlag(false);
112: this .axis.setLog10TickLabelsFlag(false);
113: this .axis.setLowerMargin(0.0);
114: this .axis.setUpperMargin(0.0);
115:
116: this .axis.setLowerBound(0.2);
117: this .axis.setUpperBound(100.0);
118: }
119:
120: /**
121: * Serialize an instance, restore it, and check for equality.
122: */
123: public void testSerialization() {
124:
125: LogarithmicAxis a1 = new LogarithmicAxis("Test Axis");
126: LogarithmicAxis a2 = null;
127:
128: try {
129: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
130: ObjectOutput out = new ObjectOutputStream(buffer);
131: out.writeObject(a1);
132: out.close();
133:
134: ObjectInput in = new ObjectInputStream(
135: new ByteArrayInputStream(buffer.toByteArray()));
136: a2 = (LogarithmicAxis) in.readObject();
137: in.close();
138: } catch (Exception e) {
139: e.printStackTrace();
140: }
141: assertEquals(a1, a2);
142:
143: }
144:
145: /**
146: * Test if adjustedLog10 and adjustedPow10 are inverses of each other
147: */
148: public void testAdjustedLog10() {
149: checkLogPowRoundTrip(20);
150: checkLogPowRoundTrip(10);
151: checkLogPowRoundTrip(5);
152: checkLogPowRoundTrip(2);
153: checkLogPowRoundTrip(1);
154: checkLogPowRoundTrip(0.5);
155: checkLogPowRoundTrip(0.2);
156: checkLogPowRoundTrip(0.0001);
157: }
158:
159: private void checkLogPowRoundTrip(double value) {
160: assertEquals("log(pow(x)) = x", value, this .axis
161: .adjustedLog10(this .axis.adjustedPow10(value)), EPSILON);
162: assertEquals("pow(log(x)) = x", value, this .axis
163: .adjustedPow10(this .axis.adjustedLog10(value)), EPSILON);
164: }
165:
166: /**
167: * Test if switchedLog10 and switchedPow10 are inverses of each other
168: */
169: public void testSwitchedLog10() {
170: assertFalse("Axis should not allow negative values", this .axis
171: .getAllowNegativesFlag());
172:
173: assertEquals(Math.log(0.5) / LogarithmicAxis.LOG10_VALUE,
174: this .axis.switchedLog10(0.5), EPSILON);
175:
176: checkSwitchedLogPowRoundTrip(20);
177: checkSwitchedLogPowRoundTrip(10);
178: checkSwitchedLogPowRoundTrip(5);
179: checkSwitchedLogPowRoundTrip(2);
180: checkSwitchedLogPowRoundTrip(1);
181: checkSwitchedLogPowRoundTrip(0.5);
182: checkSwitchedLogPowRoundTrip(0.2);
183: checkSwitchedLogPowRoundTrip(0.0001);
184: }
185:
186: private void checkSwitchedLogPowRoundTrip(double value) {
187: assertEquals("log(pow(x)) = x", value, this .axis
188: .switchedLog10(this .axis.switchedPow10(value)), EPSILON);
189: assertEquals("pow(log(x)) = x", value, this .axis
190: .switchedPow10(this .axis.switchedLog10(value)), EPSILON);
191: }
192:
193: /**
194: * Test of java2DToValue method.
195: */
196: public void testJava2DToValue() {
197: Rectangle2D plotArea = new Rectangle2D.Double(22, 33, 500, 500);
198: RectangleEdge edge = RectangleEdge.BOTTOM;
199:
200: // set axis bounds to be both greater than 1
201: this .axis.setRange(10, 20);
202: checkPointsToValue(edge, plotArea);
203:
204: // check for bounds interval that includes 1
205: this .axis.setRange(0.5, 10);
206: checkPointsToValue(edge, plotArea);
207:
208: // check for bounds interval that includes 1
209: this .axis.setRange(0.2, 20);
210: checkPointsToValue(edge, plotArea);
211:
212: // check for both bounds smaller than 1
213: this .axis.setRange(0.2, 0.7);
214: checkPointsToValue(edge, plotArea);
215: }
216:
217: /**
218: * Test of valueToJava2D method.
219: */
220: public void testValueToJava2D() {
221: Rectangle2D plotArea = new Rectangle2D.Double(22, 33, 500, 500);
222: RectangleEdge edge = RectangleEdge.BOTTOM;
223:
224: // set axis bounds to be both greater than 1
225: this .axis.setRange(10, 20);
226: checkPointsToJava2D(edge, plotArea);
227:
228: // check for bounds interval that includes 1
229: this .axis.setRange(0.5, 10);
230: checkPointsToJava2D(edge, plotArea);
231:
232: // check for bounds interval that includes 1
233: this .axis.setRange(0.2, 20);
234: checkPointsToJava2D(edge, plotArea);
235:
236: // check for both bounds smaller than 1
237: this .axis.setRange(0.2, 0.7);
238: checkPointsToJava2D(edge, plotArea);
239: }
240:
241: private void checkPointsToJava2D(RectangleEdge edge,
242: Rectangle2D plotArea) {
243: assertEquals(
244: "Left most point on the axis should be beginning of "
245: + "range.", plotArea.getX(), this .axis
246: .valueToJava2D(this .axis.getLowerBound(),
247: plotArea, edge), EPSILON);
248: assertEquals(
249: "Right most point on the axis should be end of range.",
250: plotArea.getX() + plotArea.getWidth(), this .axis
251: .valueToJava2D(this .axis.getUpperBound(),
252: plotArea, edge), EPSILON);
253: assertEquals(
254: "Center point on the axis should geometric mean of the bounds.",
255: plotArea.getX() + (plotArea.getWidth() / 2), this .axis
256: .valueToJava2D(Math.sqrt(this .axis
257: .getLowerBound()
258: * this .axis.getUpperBound()), plotArea,
259: edge), EPSILON);
260: }
261:
262: /**
263: * Check the translation java2D to value for left, right, and center point.
264: */
265: private void checkPointsToValue(RectangleEdge edge,
266: Rectangle2D plotArea) {
267: assertEquals(
268: "Right most point on the axis should be end of range.",
269: this .axis.getUpperBound(), this .axis.java2DToValue(
270: plotArea.getX() + plotArea.getWidth(),
271: plotArea, edge), EPSILON);
272:
273: assertEquals(
274: "Left most point on the axis should be beginning of "
275: + "range.",
276: this .axis.getLowerBound(),
277: this .axis
278: .java2DToValue(plotArea.getX(), plotArea, edge),
279: EPSILON);
280:
281: assertEquals(
282: "Center point on the axis should geometric mean of the "
283: + "bounds.", Math.sqrt(this .axis
284: .getUpperBound()
285: * this .axis.getLowerBound()), this .axis
286: .java2DToValue(plotArea.getX()
287: + (plotArea.getWidth() / 2), plotArea,
288: edge), EPSILON);
289: }
290:
291: /**
292: * Runs all tests in this class.
293: *
294: * @param args ignored.
295: */
296: public static void main(String[] args) {
297: junit.textui.TestRunner.run(LogarithmicAxisTests.class);
298: }
299:
300: }
|