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: * DefaultIntervalCategoryDatasetTests.java
029: * ----------------------------------------
030: * (C) Copyright 2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: DefaultIntervalCategoryDatasetTests.java,v 1.1.2.1 2007/03/08 13:32:20 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 08-Mar-2007 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.data.category.junit;
044:
045: import java.io.ByteArrayInputStream;
046: import java.io.ByteArrayOutputStream;
047: import java.io.ObjectInput;
048: import java.io.ObjectInputStream;
049: import java.io.ObjectOutput;
050: import java.io.ObjectOutputStream;
051:
052: import junit.framework.Test;
053: import junit.framework.TestCase;
054: import junit.framework.TestSuite;
055:
056: import org.jfree.data.UnknownKeyException;
057: import org.jfree.data.category.DefaultIntervalCategoryDataset;
058:
059: /**
060: * Tests for the {@link DefaultIntervalCategoryDataset} class.
061: */
062: public class DefaultIntervalCategoryDatasetTests extends TestCase {
063:
064: /**
065: * Returns the tests as a test suite.
066: *
067: * @return The test suite.
068: */
069: public static Test suite() {
070: return new TestSuite(DefaultIntervalCategoryDatasetTests.class);
071: }
072:
073: /**
074: * Constructs a new set of tests.
075: *
076: * @param name the name of the tests.
077: */
078: public DefaultIntervalCategoryDatasetTests(String name) {
079: super (name);
080: }
081:
082: /**
083: * Some checks for the getValue() method.
084: */
085: public void testGetValue() {
086: double[] starts_S1 = new double[] { 0.1, 0.2, 0.3 };
087: double[] starts_S2 = new double[] { 0.3, 0.4, 0.5 };
088: double[] ends_S1 = new double[] { 0.5, 0.6, 0.7 };
089: double[] ends_S2 = new double[] { 0.7, 0.8, 0.9 };
090: double[][] starts = new double[][] { starts_S1, starts_S2 };
091: double[][] ends = new double[][] { ends_S1, ends_S2 };
092: DefaultIntervalCategoryDataset d = new DefaultIntervalCategoryDataset(
093: starts, ends);
094:
095: assertEquals(new Double(0.1), d.getStartValue("Series 1",
096: "Category 1"));
097: assertEquals(new Double(0.2), d.getStartValue("Series 1",
098: "Category 2"));
099: assertEquals(new Double(0.3), d.getStartValue("Series 1",
100: "Category 3"));
101: assertEquals(new Double(0.3), d.getStartValue("Series 2",
102: "Category 1"));
103: assertEquals(new Double(0.4), d.getStartValue("Series 2",
104: "Category 2"));
105: assertEquals(new Double(0.5), d.getStartValue("Series 2",
106: "Category 3"));
107:
108: assertEquals(new Double(0.5), d.getEndValue("Series 1",
109: "Category 1"));
110: assertEquals(new Double(0.6), d.getEndValue("Series 1",
111: "Category 2"));
112: assertEquals(new Double(0.7), d.getEndValue("Series 1",
113: "Category 3"));
114: assertEquals(new Double(0.7), d.getEndValue("Series 2",
115: "Category 1"));
116: assertEquals(new Double(0.8), d.getEndValue("Series 2",
117: "Category 2"));
118: assertEquals(new Double(0.9), d.getEndValue("Series 2",
119: "Category 3"));
120:
121: boolean pass = false;
122: try {
123: d.getValue("XX", "Category 1");
124: } catch (UnknownKeyException e) {
125: pass = true;
126: }
127: assertTrue(pass);
128:
129: pass = false;
130: try {
131: d.getValue("Series 1", "XX");
132: } catch (UnknownKeyException e) {
133: pass = true;
134: }
135: assertTrue(pass);
136: }
137:
138: /**
139: * Some tests for the getRowCount() method.
140: */
141: public void testGetRowAndColumnCount() {
142: double[] starts_S1 = new double[] { 0.1, 0.2, 0.3 };
143: double[] starts_S2 = new double[] { 0.3, 0.4, 0.5 };
144: double[] ends_S1 = new double[] { 0.5, 0.6, 0.7 };
145: double[] ends_S2 = new double[] { 0.7, 0.8, 0.9 };
146: double[][] starts = new double[][] { starts_S1, starts_S2 };
147: double[][] ends = new double[][] { ends_S1, ends_S2 };
148: DefaultIntervalCategoryDataset d = new DefaultIntervalCategoryDataset(
149: starts, ends);
150:
151: assertEquals(2, d.getRowCount());
152: assertEquals(3, d.getColumnCount());
153: }
154:
155: /**
156: * Confirm that the equals method can distinguish all the required fields.
157: */
158: public void testEquals() {
159: double[] starts_S1A = new double[] { 0.1, 0.2, 0.3 };
160: double[] starts_S2A = new double[] { 0.3, 0.4, 0.5 };
161: double[] ends_S1A = new double[] { 0.5, 0.6, 0.7 };
162: double[] ends_S2A = new double[] { 0.7, 0.8, 0.9 };
163: double[][] startsA = new double[][] { starts_S1A, starts_S2A };
164: double[][] endsA = new double[][] { ends_S1A, ends_S2A };
165: DefaultIntervalCategoryDataset dA = new DefaultIntervalCategoryDataset(
166: startsA, endsA);
167:
168: double[] starts_S1B = new double[] { 0.1, 0.2, 0.3 };
169: double[] starts_S2B = new double[] { 0.3, 0.4, 0.5 };
170: double[] ends_S1B = new double[] { 0.5, 0.6, 0.7 };
171: double[] ends_S2B = new double[] { 0.7, 0.8, 0.9 };
172: double[][] startsB = new double[][] { starts_S1B, starts_S2B };
173: double[][] endsB = new double[][] { ends_S1B, ends_S2B };
174: DefaultIntervalCategoryDataset dB = new DefaultIntervalCategoryDataset(
175: startsB, endsB);
176:
177: assertTrue(dA.equals(dB));
178: assertTrue(dB.equals(dA));
179: }
180:
181: /**
182: * Serialize an instance, restore it, and check for equality.
183: */
184: public void testSerialization() {
185:
186: double[] starts_S1 = new double[] { 0.1, 0.2, 0.3 };
187: double[] starts_S2 = new double[] { 0.3, 0.4, 0.5 };
188: double[] ends_S1 = new double[] { 0.5, 0.6, 0.7 };
189: double[] ends_S2 = new double[] { 0.7, 0.8, 0.9 };
190: double[][] starts = new double[][] { starts_S1, starts_S2 };
191: double[][] ends = new double[][] { ends_S1, ends_S2 };
192: DefaultIntervalCategoryDataset d1 = new DefaultIntervalCategoryDataset(
193: starts, ends);
194: DefaultIntervalCategoryDataset d2 = null;
195:
196: try {
197: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
198: ObjectOutput out = new ObjectOutputStream(buffer);
199: out.writeObject(d1);
200: out.close();
201:
202: ObjectInput in = new ObjectInputStream(
203: new ByteArrayInputStream(buffer.toByteArray()));
204: d2 = (DefaultIntervalCategoryDataset) in.readObject();
205: in.close();
206: } catch (Exception e) {
207: e.printStackTrace();
208: }
209: assertEquals(d1, d2);
210:
211: }
212:
213: /**
214: * Confirm that cloning works.
215: */
216: public void testCloning() {
217: double[] starts_S1 = new double[] { 0.1, 0.2, 0.3 };
218: double[] starts_S2 = new double[] { 0.3, 0.4, 0.5 };
219: double[] ends_S1 = new double[] { 0.5, 0.6, 0.7 };
220: double[] ends_S2 = new double[] { 0.7, 0.8, 0.9 };
221: double[][] starts = new double[][] { starts_S1, starts_S2 };
222: double[][] ends = new double[][] { ends_S1, ends_S2 };
223: DefaultIntervalCategoryDataset d1 = new DefaultIntervalCategoryDataset(
224: starts, ends);
225: DefaultIntervalCategoryDataset d2 = null;
226: try {
227: d2 = (DefaultIntervalCategoryDataset) d1.clone();
228: } catch (CloneNotSupportedException e) {
229: e.printStackTrace();
230: }
231: assertTrue(d1 != d2);
232: assertTrue(d1.getClass() == d2.getClass());
233: assertTrue(d1.equals(d2));
234:
235: // check that the clone doesn't share the same underlying arrays.
236: d1.setStartValue(0, "Category 1", new Double(0.99));
237: assertFalse(d1.equals(d2));
238: d2.setStartValue(0, "Category 1", new Double(0.99));
239: assertTrue(d1.equals(d2));
240: }
241:
242: /**
243: * Some basic checks for the setStartValue() method.
244: */
245: public void testSetStartValue() {
246: double[] starts_S1 = new double[] { 0.1, 0.2, 0.3 };
247: double[] starts_S2 = new double[] { 0.3, 0.4, 0.5 };
248: double[] ends_S1 = new double[] { 0.5, 0.6, 0.7 };
249: double[] ends_S2 = new double[] { 0.7, 0.8, 0.9 };
250: double[][] starts = new double[][] { starts_S1, starts_S2 };
251: double[][] ends = new double[][] { ends_S1, ends_S2 };
252: DefaultIntervalCategoryDataset d1 = new DefaultIntervalCategoryDataset(
253: starts, ends);
254: d1.setStartValue(0, "Category 2", new Double(99.9));
255: assertEquals(new Double(99.9), d1.getStartValue("Series 1",
256: "Category 2"));
257:
258: boolean pass = false;
259: try {
260: d1.setStartValue(-1, "Category 2", new Double(99.9));
261: } catch (IllegalArgumentException e) {
262: pass = true;
263: }
264: assertTrue(pass);
265:
266: pass = false;
267: try {
268: d1.setStartValue(2, "Category 2", new Double(99.9));
269: } catch (IllegalArgumentException e) {
270: pass = true;
271: }
272: assertTrue(pass);
273: }
274:
275: /**
276: * Some basic checks for the setEndValue() method.
277: */
278: public void testSetEndValue() {
279: double[] starts_S1 = new double[] { 0.1, 0.2, 0.3 };
280: double[] starts_S2 = new double[] { 0.3, 0.4, 0.5 };
281: double[] ends_S1 = new double[] { 0.5, 0.6, 0.7 };
282: double[] ends_S2 = new double[] { 0.7, 0.8, 0.9 };
283: double[][] starts = new double[][] { starts_S1, starts_S2 };
284: double[][] ends = new double[][] { ends_S1, ends_S2 };
285: DefaultIntervalCategoryDataset d1 = new DefaultIntervalCategoryDataset(
286: starts, ends);
287: d1.setEndValue(0, "Category 2", new Double(99.9));
288: assertEquals(new Double(99.9), d1.getEndValue("Series 1",
289: "Category 2"));
290:
291: boolean pass = false;
292: try {
293: d1.setEndValue(-1, "Category 2", new Double(99.9));
294: } catch (IllegalArgumentException e) {
295: pass = true;
296: }
297: assertTrue(pass);
298:
299: pass = false;
300: try {
301: d1.setEndValue(2, "Category 2", new Double(99.9));
302: } catch (IllegalArgumentException e) {
303: pass = true;
304: }
305: assertTrue(pass);
306: }
307: }
|