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: * CombinedDomainCategoryPlotTests.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: CombinedDomainCategoryPlotTests.java,v 1.1.2.1 2006/10/03 15:41:27 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 19-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.CategoryAxis;
058: import org.jfree.chart.axis.NumberAxis;
059: import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
060: import org.jfree.chart.plot.CategoryPlot;
061: import org.jfree.chart.plot.CombinedDomainCategoryPlot;
062: import org.jfree.chart.renderer.category.BarRenderer;
063: import org.jfree.chart.renderer.category.LineAndShapeRenderer;
064: import org.jfree.data.category.CategoryDataset;
065: import org.jfree.data.category.DefaultCategoryDataset;
066:
067: /**
068: * Tests for the {@link CombinedDomainCategoryPlot} class.
069: */
070: public class CombinedDomainCategoryPlotTests extends TestCase {
071:
072: /**
073: * Returns the tests as a test suite.
074: *
075: * @return The test suite.
076: */
077: public static Test suite() {
078: return new TestSuite(CombinedDomainCategoryPlotTests.class);
079: }
080:
081: /**
082: * Constructs a new set of tests.
083: *
084: * @param name the name of the tests.
085: */
086: public CombinedDomainCategoryPlotTests(String name) {
087: super (name);
088: }
089:
090: /**
091: * This is a test to replicate the bug report 987080.
092: */
093: public void testRemoveSubplot() {
094: CombinedDomainCategoryPlot plot = new CombinedDomainCategoryPlot();
095: CategoryPlot plot1 = new CategoryPlot();
096: CategoryPlot plot2 = new CategoryPlot();
097: plot.add(plot1);
098: plot.add(plot2);
099: // remove plot2, but plot1 is removed instead
100: plot.remove(plot2);
101: List plots = plot.getSubplots();
102: assertTrue(plots.get(0) == plot1);
103: assertEquals(1, plots.size());
104: }
105:
106: /**
107: * Some checks for the equals() method.
108: */
109: public void testEquals() {
110: CombinedDomainCategoryPlot plot1 = createPlot();
111: CombinedDomainCategoryPlot plot2 = createPlot();
112: assertTrue(plot1.equals(plot2));
113: }
114:
115: /**
116: * Some checks for cloning.
117: */
118: public void testCloning() {
119: CombinedDomainCategoryPlot plot1 = createPlot();
120: CombinedDomainCategoryPlot plot2 = null;
121: try {
122: plot2 = (CombinedDomainCategoryPlot) plot1.clone();
123: } catch (CloneNotSupportedException e) {
124: System.err.println("Failed to clone.");
125: }
126: assertTrue(plot1 != plot2);
127: assertTrue(plot1.getClass() == plot2.getClass());
128: assertTrue(plot1.equals(plot2));
129: }
130:
131: /**
132: * Serialize an instance, restore it, and check for equality.
133: */
134: public void testSerialization() {
135: CombinedDomainCategoryPlot plot1 = createPlot();
136: CombinedDomainCategoryPlot plot2 = null;
137: try {
138: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
139: ObjectOutput out = new ObjectOutputStream(buffer);
140: out.writeObject(plot1);
141: out.close();
142: ObjectInput in = new ObjectInputStream(
143: new ByteArrayInputStream(buffer.toByteArray()));
144: plot2 = (CombinedDomainCategoryPlot) in.readObject();
145: in.close();
146: } catch (Exception e) {
147: e.printStackTrace();
148: }
149: assertEquals(plot1, plot2);
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 sample plot.
246: */
247: private CombinedDomainCategoryPlot createPlot() {
248:
249: CategoryDataset dataset1 = createDataset1();
250: NumberAxis rangeAxis1 = new NumberAxis("Value");
251: rangeAxis1.setStandardTickUnits(NumberAxis
252: .createIntegerTickUnits());
253: LineAndShapeRenderer renderer1 = new LineAndShapeRenderer();
254: renderer1
255: .setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
256: CategoryPlot subplot1 = new CategoryPlot(dataset1, null,
257: rangeAxis1, renderer1);
258: subplot1.setDomainGridlinesVisible(true);
259:
260: CategoryDataset dataset2 = createDataset2();
261: NumberAxis rangeAxis2 = new NumberAxis("Value");
262: rangeAxis2.setStandardTickUnits(NumberAxis
263: .createIntegerTickUnits());
264: BarRenderer renderer2 = new BarRenderer();
265: renderer2
266: .setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
267: CategoryPlot subplot2 = new CategoryPlot(dataset2, null,
268: rangeAxis2, renderer2);
269: subplot2.setDomainGridlinesVisible(true);
270:
271: CategoryAxis domainAxis = new CategoryAxis("Category");
272: CombinedDomainCategoryPlot plot = new CombinedDomainCategoryPlot(
273: domainAxis);
274: plot.add(subplot1, 2);
275: plot.add(subplot2, 1);
276: return plot;
277:
278: }
279:
280: }
|