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: * CombinedRangeCategoryPlotTests.java
029: * ------------------------------------
030: * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: CombinedRangeCategoryPlotTests.java,v 1.1.2.1 2006/10/03 15:41:27 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 21-Aug-2003 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.chart.plot.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: import java.util.List;
052:
053: import junit.framework.Test;
054: import junit.framework.TestCase;
055: import junit.framework.TestSuite;
056:
057: import org.jfree.chart.axis.NumberAxis;
058: import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
059: import org.jfree.chart.plot.CategoryPlot;
060: import org.jfree.chart.plot.CombinedRangeCategoryPlot;
061: import org.jfree.chart.renderer.category.BarRenderer;
062: import org.jfree.chart.renderer.category.LineAndShapeRenderer;
063: import org.jfree.data.category.CategoryDataset;
064: import org.jfree.data.category.DefaultCategoryDataset;
065:
066: /**
067: * Tests for the {@link CombinedRangeCategoryPlot} class.
068: */
069: public class CombinedRangeCategoryPlotTests extends TestCase {
070:
071: /**
072: * Returns the tests as a test suite.
073: *
074: * @return The test suite.
075: */
076: public static Test suite() {
077: return new TestSuite(CombinedRangeCategoryPlotTests.class);
078: }
079:
080: /**
081: * Constructs a new set of tests.
082: *
083: * @param name the name of the tests.
084: */
085: public CombinedRangeCategoryPlotTests(String name) {
086: super (name);
087: }
088:
089: /**
090: * Test the equals() method.
091: */
092: public void testEquals() {
093: CombinedRangeCategoryPlot plot1 = createPlot();
094: CombinedRangeCategoryPlot plot2 = createPlot();
095: assertTrue(plot1.equals(plot2));
096: }
097:
098: /**
099: * Confirm that cloning works.
100: */
101: public void testCloning() {
102: CombinedRangeCategoryPlot plot1 = createPlot();
103: CombinedRangeCategoryPlot plot2 = null;
104: try {
105: plot2 = (CombinedRangeCategoryPlot) plot1.clone();
106: } catch (CloneNotSupportedException e) {
107: System.err.println("Failed to clone.");
108: }
109: assertTrue(plot1 != plot2);
110: assertTrue(plot1.getClass() == plot2.getClass());
111: assertTrue(plot1.equals(plot2));
112: }
113:
114: /**
115: * Serialize an instance, restore it, and check for equality.
116: */
117: public void testSerialization() {
118: CombinedRangeCategoryPlot plot1 = createPlot();
119: CombinedRangeCategoryPlot plot2 = null;
120: try {
121: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
122: ObjectOutput out = new ObjectOutputStream(buffer);
123: out.writeObject(plot1);
124: out.close();
125: ObjectInput in = new ObjectInputStream(
126: new ByteArrayInputStream(buffer.toByteArray()));
127: plot2 = (CombinedRangeCategoryPlot) in.readObject();
128: in.close();
129: } catch (Exception e) {
130: e.printStackTrace();
131: }
132: assertEquals(plot1, plot2);
133:
134: }
135:
136: /**
137: * This is a test to replicate the bug report 1121172.
138: */
139: public void testRemoveSubplot() {
140: CombinedRangeCategoryPlot plot = new CombinedRangeCategoryPlot();
141: CategoryPlot plot1 = new CategoryPlot();
142: CategoryPlot plot2 = new CategoryPlot();
143: CategoryPlot plot3 = new CategoryPlot();
144: plot.add(plot1);
145: plot.add(plot2);
146: plot.add(plot3);
147: plot.remove(plot2);
148: List plots = plot.getSubplots();
149: assertEquals(2, plots.size());
150: }
151:
152: /**
153: * Creates a dataset.
154: *
155: * @return A dataset.
156: */
157: public CategoryDataset createDataset1() {
158:
159: DefaultCategoryDataset result = new DefaultCategoryDataset();
160:
161: // row keys...
162: String series1 = "First";
163: String series2 = "Second";
164:
165: // column keys...
166: String type1 = "Type 1";
167: String type2 = "Type 2";
168: String type3 = "Type 3";
169: String type4 = "Type 4";
170: String type5 = "Type 5";
171: String type6 = "Type 6";
172: String type7 = "Type 7";
173: String type8 = "Type 8";
174:
175: result.addValue(1.0, series1, type1);
176: result.addValue(4.0, series1, type2);
177: result.addValue(3.0, series1, type3);
178: result.addValue(5.0, series1, type4);
179: result.addValue(5.0, series1, type5);
180: result.addValue(7.0, series1, type6);
181: result.addValue(7.0, series1, type7);
182: result.addValue(8.0, series1, type8);
183:
184: result.addValue(5.0, series2, type1);
185: result.addValue(7.0, series2, type2);
186: result.addValue(6.0, series2, type3);
187: result.addValue(8.0, series2, type4);
188: result.addValue(4.0, series2, type5);
189: result.addValue(4.0, series2, type6);
190: result.addValue(2.0, series2, type7);
191: result.addValue(1.0, series2, type8);
192:
193: return result;
194:
195: }
196:
197: /**
198: * Creates a dataset.
199: *
200: * @return A dataset.
201: */
202: public CategoryDataset createDataset2() {
203:
204: DefaultCategoryDataset result = new DefaultCategoryDataset();
205:
206: // row keys...
207: String series1 = "Third";
208: String series2 = "Fourth";
209:
210: // column keys...
211: String type1 = "Type 1";
212: String type2 = "Type 2";
213: String type3 = "Type 3";
214: String type4 = "Type 4";
215: String type5 = "Type 5";
216: String type6 = "Type 6";
217: String type7 = "Type 7";
218: String type8 = "Type 8";
219:
220: result.addValue(11.0, series1, type1);
221: result.addValue(14.0, series1, type2);
222: result.addValue(13.0, series1, type3);
223: result.addValue(15.0, series1, type4);
224: result.addValue(15.0, series1, type5);
225: result.addValue(17.0, series1, type6);
226: result.addValue(17.0, series1, type7);
227: result.addValue(18.0, series1, type8);
228:
229: result.addValue(15.0, series2, type1);
230: result.addValue(17.0, series2, type2);
231: result.addValue(16.0, series2, type3);
232: result.addValue(18.0, series2, type4);
233: result.addValue(14.0, series2, type5);
234: result.addValue(14.0, series2, type6);
235: result.addValue(12.0, series2, type7);
236: result.addValue(11.0, series2, type8);
237:
238: return result;
239:
240: }
241:
242: /**
243: * Creates a sample plot.
244: *
245: * @return A plot.
246: */
247: private CombinedRangeCategoryPlot createPlot() {
248: CategoryDataset dataset1 = createDataset1();
249: NumberAxis rangeAxis1 = new NumberAxis("Value");
250: rangeAxis1.setStandardTickUnits(NumberAxis
251: .createIntegerTickUnits());
252: LineAndShapeRenderer renderer1 = new LineAndShapeRenderer();
253: renderer1
254: .setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
255: CategoryPlot subplot1 = new CategoryPlot(dataset1, null,
256: rangeAxis1, renderer1);
257: subplot1.setDomainGridlinesVisible(true);
258:
259: CategoryDataset dataset2 = createDataset2();
260: NumberAxis rangeAxis2 = new NumberAxis("Value");
261: rangeAxis2.setStandardTickUnits(NumberAxis
262: .createIntegerTickUnits());
263: BarRenderer renderer2 = new BarRenderer();
264: renderer2
265: .setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
266: CategoryPlot subplot2 = new CategoryPlot(dataset2, null,
267: rangeAxis2, renderer2);
268: subplot2.setDomainGridlinesVisible(true);
269:
270: NumberAxis rangeAxis = new NumberAxis("Value");
271: CombinedRangeCategoryPlot plot = new CombinedRangeCategoryPlot(
272: rangeAxis);
273: plot.add(subplot1, 2);
274: plot.add(subplot2, 1);
275: return plot;
276: }
277: }
|