Source Code Cross Referenced for CategoryPlotTests.java in  » Chart » jfreechart » org » jfree » chart » plot » junit » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Chart » jfreechart » org.jfree.chart.plot.junit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:         * CategoryPlotTests.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: CategoryPlotTests.java,v 1.1.2.4 2007/03/23 14:48:12 mungady Exp $
036:         *
037:         * Changes
038:         * -------
039:         * 26-Mar-2003 : Version 1 (DG);
040:         * 15-Sep-2003 : Added a unit test to reproduce a bug in serialization (now 
041:         *               fixed) (DG);
042:         * 05-Feb-2007 : Added testAddDomainMarker() and testAddRangeMarker() (DG);
043:         * 07-Feb-2007 : Added test1654215() (DG);
044:         *
045:         */
046:
047:        package org.jfree.chart.plot.junit;
048:
049:        import java.awt.BasicStroke;
050:        import java.awt.Color;
051:        import java.awt.GradientPaint;
052:        import java.awt.Graphics2D;
053:        import java.awt.Stroke;
054:        import java.awt.geom.Rectangle2D;
055:        import java.awt.image.BufferedImage;
056:        import java.io.ByteArrayInputStream;
057:        import java.io.ByteArrayOutputStream;
058:        import java.io.ObjectInput;
059:        import java.io.ObjectInputStream;
060:        import java.io.ObjectOutput;
061:        import java.io.ObjectOutputStream;
062:        import java.util.Arrays;
063:        import java.util.List;
064:
065:        import junit.framework.Test;
066:        import junit.framework.TestCase;
067:        import junit.framework.TestSuite;
068:
069:        import org.jfree.chart.ChartFactory;
070:        import org.jfree.chart.JFreeChart;
071:        import org.jfree.chart.annotations.CategoryTextAnnotation;
072:        import org.jfree.chart.axis.AxisLocation;
073:        import org.jfree.chart.axis.AxisSpace;
074:        import org.jfree.chart.axis.CategoryAnchor;
075:        import org.jfree.chart.axis.CategoryAxis;
076:        import org.jfree.chart.axis.NumberAxis;
077:        import org.jfree.chart.event.MarkerChangeListener;
078:        import org.jfree.chart.plot.CategoryMarker;
079:        import org.jfree.chart.plot.CategoryPlot;
080:        import org.jfree.chart.plot.DatasetRenderingOrder;
081:        import org.jfree.chart.plot.IntervalMarker;
082:        import org.jfree.chart.plot.Marker;
083:        import org.jfree.chart.plot.PlotOrientation;
084:        import org.jfree.chart.plot.ValueMarker;
085:        import org.jfree.chart.renderer.category.AreaRenderer;
086:        import org.jfree.chart.renderer.category.BarRenderer;
087:        import org.jfree.chart.renderer.category.CategoryItemRenderer;
088:        import org.jfree.chart.renderer.category.LineAndShapeRenderer;
089:        import org.jfree.data.Range;
090:        import org.jfree.data.category.DefaultCategoryDataset;
091:        import org.jfree.ui.Layer;
092:        import org.jfree.ui.RectangleInsets;
093:        import org.jfree.util.SortOrder;
094:
095:        /**
096:         * Tests for the {@link CategoryPlot} class.
097:         */
098:        public class CategoryPlotTests extends TestCase {
099:
100:            /**
101:             * Returns the tests as a test suite.
102:             *
103:             * @return The test suite.
104:             */
105:            public static Test suite() {
106:                return new TestSuite(CategoryPlotTests.class);
107:            }
108:
109:            /**
110:             * Constructs a new set of tests.
111:             *
112:             * @param name  the name of the tests.
113:             */
114:            public CategoryPlotTests(String name) {
115:                super (name);
116:            }
117:
118:            /**
119:             * A test for a bug reported in the forum.
120:             */
121:            public void testAxisRange() {
122:                DefaultCategoryDataset datasetA = new DefaultCategoryDataset();
123:                DefaultCategoryDataset datasetB = new DefaultCategoryDataset();
124:                datasetB.addValue(50.0, "R1", "C1");
125:                datasetB.addValue(80.0, "R1", "C1");
126:                CategoryPlot plot = new CategoryPlot(datasetA,
127:                        new CategoryAxis(null), new NumberAxis(null),
128:                        new LineAndShapeRenderer());
129:                plot.setDataset(1, datasetB);
130:                plot.setRenderer(1, new LineAndShapeRenderer());
131:                Range r = plot.getRangeAxis().getRange();
132:                assertEquals(84.0, r.getUpperBound(), 0.00001);
133:            }
134:
135:            /**
136:             * Test that the equals() method differentiates all the required fields.
137:             */
138:            public void testEquals() {
139:
140:                CategoryPlot plot1 = new CategoryPlot();
141:                CategoryPlot plot2 = new CategoryPlot();
142:                assertTrue(plot1.equals(plot2));
143:                assertTrue(plot2.equals(plot1));
144:
145:                // orientation...
146:                plot1.setOrientation(PlotOrientation.HORIZONTAL);
147:                assertFalse(plot1.equals(plot2));
148:                plot2.setOrientation(PlotOrientation.HORIZONTAL);
149:                assertTrue(plot1.equals(plot2));
150:
151:                // axisOffset...
152:                plot1
153:                        .setAxisOffset(new RectangleInsets(0.05, 0.05, 0.05,
154:                                0.05));
155:                assertFalse(plot1.equals(plot2));
156:                plot2
157:                        .setAxisOffset(new RectangleInsets(0.05, 0.05, 0.05,
158:                                0.05));
159:                assertTrue(plot1.equals(plot2));
160:
161:                // domainAxis - no longer a separate field but test anyway...
162:                plot1.setDomainAxis(new CategoryAxis("Category Axis"));
163:                assertFalse(plot1.equals(plot2));
164:                plot2.setDomainAxis(new CategoryAxis("Category Axis"));
165:                assertTrue(plot1.equals(plot2));
166:
167:                // domainAxes...
168:                plot1.setDomainAxis(11, new CategoryAxis("Secondary Axis"));
169:                assertFalse(plot1.equals(plot2));
170:                plot2.setDomainAxis(11, new CategoryAxis("Secondary Axis"));
171:                assertTrue(plot1.equals(plot2));
172:
173:                // domainAxisLocation - no longer a separate field but test anyway...
174:                plot1.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
175:                assertFalse(plot1.equals(plot2));
176:                plot2.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
177:                assertTrue(plot1.equals(plot2));
178:
179:                // domainAxisLocations...
180:                plot1.setDomainAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
181:                assertFalse(plot1.equals(plot2));
182:                plot2.setDomainAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
183:                assertTrue(plot1.equals(plot2));
184:
185:                // draw shared domain axis...
186:                plot1.setDrawSharedDomainAxis(!plot1.getDrawSharedDomainAxis());
187:                assertFalse(plot1.equals(plot2));
188:                plot2.setDrawSharedDomainAxis(!plot2.getDrawSharedDomainAxis());
189:                assertTrue(plot1.equals(plot2));
190:
191:                // rangeAxis - no longer a separate field but test anyway...
192:                plot1.setRangeAxis(new NumberAxis("Range Axis"));
193:                assertFalse(plot1.equals(plot2));
194:                plot2.setRangeAxis(new NumberAxis("Range Axis"));
195:                assertTrue(plot1.equals(plot2));
196:
197:                // rangeAxes...
198:                plot1.setRangeAxis(11, new NumberAxis("Secondary Range Axis"));
199:                assertFalse(plot1.equals(plot2));
200:                plot2.setRangeAxis(11, new NumberAxis("Secondary Range Axis"));
201:                assertTrue(plot1.equals(plot2));
202:
203:                // rangeAxisLocation - no longer a separate field but test anyway...
204:                plot1.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);
205:                assertFalse(plot1.equals(plot2));
206:                plot2.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT);
207:                assertTrue(plot1.equals(plot2));
208:
209:                // rangeAxisLocations...
210:                plot1.setRangeAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
211:                assertFalse(plot1.equals(plot2));
212:                plot2.setRangeAxisLocation(11, AxisLocation.TOP_OR_RIGHT);
213:                assertTrue(plot1.equals(plot2));
214:
215:                // datasetToDomainAxisMap...
216:                plot1.mapDatasetToDomainAxis(11, 11);
217:                assertFalse(plot1.equals(plot2));
218:                plot2.mapDatasetToDomainAxis(11, 11);
219:                assertTrue(plot1.equals(plot2));
220:
221:                // datasetToRangeAxisMap...
222:                plot1.mapDatasetToRangeAxis(11, 11);
223:                assertFalse(plot1.equals(plot2));
224:                plot2.mapDatasetToRangeAxis(11, 11);
225:                assertTrue(plot1.equals(plot2));
226:
227:                // renderer - no longer a separate field but test anyway...
228:                plot1.setRenderer(new AreaRenderer());
229:                assertFalse(plot1.equals(plot2));
230:                plot2.setRenderer(new AreaRenderer());
231:                assertTrue(plot1.equals(plot2));
232:
233:                // renderers...
234:                plot1.setRenderer(11, new AreaRenderer());
235:                assertFalse(plot1.equals(plot2));
236:                plot2.setRenderer(11, new AreaRenderer());
237:                assertTrue(plot1.equals(plot2));
238:
239:                // rendering order...
240:                plot1.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
241:                assertFalse(plot1.equals(plot2));
242:                plot2.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
243:                assertTrue(plot1.equals(plot2));
244:
245:                // columnRenderingOrder...
246:                plot1.setColumnRenderingOrder(SortOrder.DESCENDING);
247:                assertFalse(plot1.equals(plot2));
248:                plot2.setColumnRenderingOrder(SortOrder.DESCENDING);
249:                assertTrue(plot1.equals(plot2));
250:
251:                // rowRenderingOrder...
252:                plot1.setRowRenderingOrder(SortOrder.DESCENDING);
253:                assertFalse(plot1.equals(plot2));
254:                plot2.setRowRenderingOrder(SortOrder.DESCENDING);
255:                assertTrue(plot1.equals(plot2));
256:
257:                // domainGridlinesVisible
258:                plot1.setDomainGridlinesVisible(true);
259:                assertFalse(plot1.equals(plot2));
260:                plot2.setDomainGridlinesVisible(true);
261:                assertTrue(plot1.equals(plot2));
262:
263:                // domainGridlinePosition
264:                plot1.setDomainGridlinePosition(CategoryAnchor.END);
265:                assertFalse(plot1.equals(plot2));
266:                plot2.setDomainGridlinePosition(CategoryAnchor.END);
267:                assertTrue(plot1.equals(plot2));
268:
269:                // domainGridlineStroke
270:                Stroke stroke = new BasicStroke(2.0f);
271:                plot1.setDomainGridlineStroke(stroke);
272:                assertFalse(plot1.equals(plot2));
273:                plot2.setDomainGridlineStroke(stroke);
274:                assertTrue(plot1.equals(plot2));
275:
276:                // domainGridlinePaint
277:                plot1.setDomainGridlinePaint(new GradientPaint(1.0f, 2.0f,
278:                        Color.blue, 3.0f, 4.0f, Color.yellow));
279:                assertFalse(plot1.equals(plot2));
280:                plot2.setDomainGridlinePaint(new GradientPaint(1.0f, 2.0f,
281:                        Color.blue, 3.0f, 4.0f, Color.yellow));
282:                assertTrue(plot1.equals(plot2));
283:
284:                // rangeGridlinesVisible
285:                plot1.setRangeGridlinesVisible(false);
286:                assertFalse(plot1.equals(plot2));
287:                plot2.setRangeGridlinesVisible(false);
288:                assertTrue(plot1.equals(plot2));
289:
290:                // rangeGridlineStroke
291:                plot1.setRangeGridlineStroke(stroke);
292:                assertFalse(plot1.equals(plot2));
293:                plot2.setRangeGridlineStroke(stroke);
294:                assertTrue(plot1.equals(plot2));
295:
296:                // rangeGridlinePaint
297:                plot1.setRangeGridlinePaint(new GradientPaint(1.0f, 2.0f,
298:                        Color.green, 3.0f, 4.0f, Color.yellow));
299:                assertFalse(plot1.equals(plot2));
300:                plot2.setRangeGridlinePaint(new GradientPaint(1.0f, 2.0f,
301:                        Color.green, 3.0f, 4.0f, Color.yellow));
302:                assertTrue(plot1.equals(plot2));
303:
304:                // anchorValue
305:                plot1.setAnchorValue(100.0);
306:                assertFalse(plot1.equals(plot2));
307:                plot2.setAnchorValue(100.0);
308:                assertTrue(plot1.equals(plot2));
309:
310:                // rangeCrosshairVisible
311:                plot1.setRangeCrosshairVisible(true);
312:                assertFalse(plot1.equals(plot2));
313:                plot2.setRangeCrosshairVisible(true);
314:                assertTrue(plot1.equals(plot2));
315:
316:                // rangeCrosshairValue
317:                plot1.setRangeCrosshairValue(100.0);
318:                assertFalse(plot1.equals(plot2));
319:                plot2.setRangeCrosshairValue(100.0);
320:                assertTrue(plot1.equals(plot2));
321:
322:                // rangeCrosshairStroke
323:                plot1.setRangeCrosshairStroke(stroke);
324:                assertFalse(plot1.equals(plot2));
325:                plot2.setRangeCrosshairStroke(stroke);
326:                assertTrue(plot1.equals(plot2));
327:
328:                // rangeCrosshairPaint
329:                plot1.setRangeCrosshairPaint(new GradientPaint(1.0f, 2.0f,
330:                        Color.white, 3.0f, 4.0f, Color.yellow));
331:                assertFalse(plot1.equals(plot2));
332:                plot2.setRangeCrosshairPaint(new GradientPaint(1.0f, 2.0f,
333:                        Color.white, 3.0f, 4.0f, Color.yellow));
334:                assertTrue(plot1.equals(plot2));
335:
336:                // rangeCrosshairLockedOnData
337:                plot1.setRangeCrosshairLockedOnData(false);
338:                assertFalse(plot1.equals(plot2));
339:                plot2.setRangeCrosshairLockedOnData(false);
340:                assertTrue(plot1.equals(plot2));
341:
342:                // range markers - no longer separate fields but test anyway...
343:                plot1.addRangeMarker(new ValueMarker(4.0), Layer.FOREGROUND);
344:                assertFalse(plot1.equals(plot2));
345:                plot2.addRangeMarker(new ValueMarker(4.0), Layer.FOREGROUND);
346:                assertTrue(plot1.equals(plot2));
347:
348:                plot1.addRangeMarker(new ValueMarker(5.0), Layer.BACKGROUND);
349:                assertFalse(plot1.equals(plot2));
350:                plot2.addRangeMarker(new ValueMarker(5.0), Layer.BACKGROUND);
351:                assertTrue(plot1.equals(plot2));
352:
353:                // foreground range markers...
354:                plot1.addRangeMarker(1, new ValueMarker(4.0), Layer.FOREGROUND);
355:                assertFalse(plot1.equals(plot2));
356:                plot2.addRangeMarker(1, new ValueMarker(4.0), Layer.FOREGROUND);
357:                assertTrue(plot1.equals(plot2));
358:
359:                // background range markers...
360:                plot1.addRangeMarker(1, new ValueMarker(5.0), Layer.BACKGROUND);
361:                assertFalse(plot1.equals(plot2));
362:                plot2.addRangeMarker(1, new ValueMarker(5.0), Layer.BACKGROUND);
363:                assertTrue(plot1.equals(plot2));
364:
365:                // annotations
366:                plot1.addAnnotation(new CategoryTextAnnotation("Text",
367:                        "Category", 43.0));
368:                assertFalse(plot1.equals(plot2));
369:                plot2.addAnnotation(new CategoryTextAnnotation("Text",
370:                        "Category", 43.0));
371:                assertTrue(plot1.equals(plot2));
372:
373:                // weight
374:                plot1.setWeight(3);
375:                assertFalse(plot1.equals(plot2));
376:                plot2.setWeight(3);
377:                assertTrue(plot1.equals(plot2));
378:
379:                // fixed domain axis space...
380:                plot1.setFixedDomainAxisSpace(new AxisSpace());
381:                assertFalse(plot1.equals(plot2));
382:                plot2.setFixedDomainAxisSpace(new AxisSpace());
383:                assertTrue(plot1.equals(plot2));
384:
385:                // fixed range axis space...
386:                plot1.setFixedRangeAxisSpace(new AxisSpace());
387:                assertFalse(plot1.equals(plot2));
388:                plot2.setFixedRangeAxisSpace(new AxisSpace());
389:                assertTrue(plot1.equals(plot2));
390:
391:            }
392:
393:            /**
394:             * Confirm that cloning works.
395:             */
396:            public void testCloning() {
397:                CategoryPlot p1 = new CategoryPlot();
398:                p1.setRangeCrosshairPaint(new GradientPaint(1.0f, 2.0f,
399:                        Color.white, 3.0f, 4.0f, Color.yellow));
400:                CategoryPlot p2 = null;
401:                try {
402:                    p2 = (CategoryPlot) p1.clone();
403:                } catch (CloneNotSupportedException e) {
404:                    e.printStackTrace();
405:                    System.err.println("Failed to clone.");
406:                }
407:                assertTrue(p1 != p2);
408:                assertTrue(p1.getClass() == p2.getClass());
409:                assertTrue(p1.equals(p2));
410:            }
411:
412:            /**
413:             * Serialize an instance, restore it, and check for equality.
414:             */
415:            public void testSerialization() {
416:
417:                DefaultCategoryDataset dataset = new DefaultCategoryDataset();
418:                CategoryAxis domainAxis = new CategoryAxis("Domain");
419:                NumberAxis rangeAxis = new NumberAxis("Range");
420:                BarRenderer renderer = new BarRenderer();
421:                CategoryPlot p1 = new CategoryPlot(dataset, domainAxis,
422:                        rangeAxis, renderer);
423:                p1.setOrientation(PlotOrientation.HORIZONTAL);
424:                CategoryPlot p2 = null;
425:                try {
426:                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
427:                    ObjectOutput out = new ObjectOutputStream(buffer);
428:                    out.writeObject(p1);
429:                    out.close();
430:
431:                    ObjectInput in = new ObjectInputStream(
432:                            new ByteArrayInputStream(buffer.toByteArray()));
433:                    p2 = (CategoryPlot) in.readObject();
434:                    in.close();
435:                } catch (Exception e) {
436:                    e.printStackTrace();
437:                }
438:                assertTrue(p1.equals(p2));
439:            }
440:
441:            /**
442:             * Serialize an instance, restore it, and check for equality.
443:             */
444:            public void testSerialization2() {
445:
446:                DefaultCategoryDataset data = new DefaultCategoryDataset();
447:                CategoryAxis domainAxis = new CategoryAxis("Domain");
448:                NumberAxis rangeAxis = new NumberAxis("Range");
449:                BarRenderer renderer = new BarRenderer();
450:                CategoryPlot p1 = new CategoryPlot(data, domainAxis, rangeAxis,
451:                        renderer);
452:                p1.setOrientation(PlotOrientation.VERTICAL);
453:                CategoryPlot p2 = null;
454:
455:                try {
456:                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
457:                    ObjectOutput out = new ObjectOutputStream(buffer);
458:                    out.writeObject(p1);
459:                    out.close();
460:
461:                    ObjectInput in = new ObjectInputStream(
462:                            new ByteArrayInputStream(buffer.toByteArray()));
463:                    p2 = (CategoryPlot) in.readObject();
464:                    in.close();
465:                } catch (Exception e) {
466:                    fail(e.toString());
467:                }
468:                assertEquals(p1, p2);
469:
470:            }
471:
472:            /**
473:             * Serialize an instance, restore it, and check for equality.
474:             */
475:            public void testSerialization3() {
476:                DefaultCategoryDataset dataset = new DefaultCategoryDataset();
477:                JFreeChart chart = ChartFactory.createBarChart("Test Chart",
478:                        "Category Axis", "Value Axis", dataset,
479:                        PlotOrientation.VERTICAL, true, true, false);
480:                JFreeChart chart2 = null;
481:
482:                // serialize and deserialize the chart....
483:                try {
484:                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
485:                    ObjectOutput out = new ObjectOutputStream(buffer);
486:                    out.writeObject(chart);
487:                    out.close();
488:
489:                    ObjectInput in = new ObjectInputStream(
490:                            new ByteArrayInputStream(buffer.toByteArray()));
491:                    chart2 = (JFreeChart) in.readObject();
492:                    in.close();
493:                } catch (Exception e) {
494:                    fail(e.toString());
495:                }
496:
497:                // now check that the chart is usable...
498:                boolean passed = true;
499:                try {
500:                    chart2.createBufferedImage(300, 200);
501:                } catch (Exception e) {
502:                    passed = false;
503:                    e.printStackTrace();
504:                }
505:                assertTrue(passed);
506:            }
507:
508:            /**
509:             * This test ensures that a plot with markers is serialized correctly.
510:             */
511:            public void testSerialization4() {
512:                DefaultCategoryDataset dataset = new DefaultCategoryDataset();
513:                JFreeChart chart = ChartFactory.createBarChart("Test Chart",
514:                        "Category Axis", "Value Axis", dataset,
515:                        PlotOrientation.VERTICAL, true, true, false);
516:                CategoryPlot plot = (CategoryPlot) chart.getPlot();
517:                plot.addRangeMarker(new ValueMarker(1.1), Layer.FOREGROUND);
518:                plot.addRangeMarker(new IntervalMarker(2.2, 3.3),
519:                        Layer.BACKGROUND);
520:                JFreeChart chart2 = null;
521:
522:                // serialize and deserialize the chart....
523:                try {
524:                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
525:                    ObjectOutput out = new ObjectOutputStream(buffer);
526:                    out.writeObject(chart);
527:                    out.close();
528:
529:                    ObjectInput in = new ObjectInputStream(
530:                            new ByteArrayInputStream(buffer.toByteArray()));
531:                    chart2 = (JFreeChart) in.readObject();
532:                    in.close();
533:                } catch (Exception e) {
534:                    fail(e.toString());
535:                }
536:                assertEquals(chart, chart2);
537:
538:                // now check that the chart is usable...
539:                boolean passed = true;
540:                try {
541:                    chart2.createBufferedImage(300, 200);
542:                } catch (Exception e) {
543:                    passed = false;
544:                    e.printStackTrace();
545:                }
546:                assertTrue(passed);
547:            }
548:
549:            /**
550:             * Tests a bug where the plot is no longer registered as a listener
551:             * with the dataset(s) and axes after deserialization.  See patch 1209475
552:             * at SourceForge.
553:             */
554:            public void testSerialization5() {
555:                DefaultCategoryDataset dataset1 = new DefaultCategoryDataset();
556:                CategoryAxis domainAxis1 = new CategoryAxis("Domain 1");
557:                NumberAxis rangeAxis1 = new NumberAxis("Range 1");
558:                BarRenderer renderer1 = new BarRenderer();
559:                CategoryPlot p1 = new CategoryPlot(dataset1, domainAxis1,
560:                        rangeAxis1, renderer1);
561:                CategoryAxis domainAxis2 = new CategoryAxis("Domain 2");
562:                NumberAxis rangeAxis2 = new NumberAxis("Range 2");
563:                BarRenderer renderer2 = new BarRenderer();
564:                DefaultCategoryDataset dataset2 = new DefaultCategoryDataset();
565:                p1.setDataset(1, dataset2);
566:                p1.setDomainAxis(1, domainAxis2);
567:                p1.setRangeAxis(1, rangeAxis2);
568:                p1.setRenderer(1, renderer2);
569:                CategoryPlot p2 = null;
570:                try {
571:                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
572:                    ObjectOutput out = new ObjectOutputStream(buffer);
573:                    out.writeObject(p1);
574:                    out.close();
575:                    ObjectInput in = new ObjectInputStream(
576:                            new ByteArrayInputStream(buffer.toByteArray()));
577:                    p2 = (CategoryPlot) in.readObject();
578:                    in.close();
579:                } catch (Exception e) {
580:                    fail(e.toString());
581:                }
582:                assertEquals(p1, p2);
583:
584:                // now check that all datasets, renderers and axes are being listened 
585:                // too...
586:                CategoryAxis domainAxisA = p2.getDomainAxis(0);
587:                NumberAxis rangeAxisA = (NumberAxis) p2.getRangeAxis(0);
588:                DefaultCategoryDataset datasetA = (DefaultCategoryDataset) p2
589:                        .getDataset(0);
590:                BarRenderer rendererA = (BarRenderer) p2.getRenderer(0);
591:                CategoryAxis domainAxisB = p2.getDomainAxis(1);
592:                NumberAxis rangeAxisB = (NumberAxis) p2.getRangeAxis(1);
593:                DefaultCategoryDataset datasetB = (DefaultCategoryDataset) p2
594:                        .getDataset(1);
595:                BarRenderer rendererB = (BarRenderer) p2.getRenderer(1);
596:                assertTrue(datasetA.hasListener(p2));
597:                assertTrue(domainAxisA.hasListener(p2));
598:                assertTrue(rangeAxisA.hasListener(p2));
599:                assertTrue(rendererA.hasListener(p2));
600:                assertTrue(datasetB.hasListener(p2));
601:                assertTrue(domainAxisB.hasListener(p2));
602:                assertTrue(rangeAxisB.hasListener(p2));
603:                assertTrue(rendererB.hasListener(p2));
604:            }
605:
606:            /**
607:             * A test for a bug where setting the renderer doesn't register the plot
608:             * as a RendererChangeListener.
609:             */
610:            public void testSetRenderer() {
611:                CategoryPlot plot = new CategoryPlot();
612:                CategoryItemRenderer renderer = new LineAndShapeRenderer();
613:                plot.setRenderer(renderer);
614:                // now make a change to the renderer and see if it triggers a plot
615:                // change event...
616:                MyPlotChangeListener listener = new MyPlotChangeListener();
617:                plot.addChangeListener(listener);
618:                renderer.setSeriesPaint(0, Color.black);
619:                assertTrue(listener.getEvent() != null);
620:            }
621:
622:            /**
623:             * A test for bug report 1169972.
624:             */
625:            public void test1169972() {
626:                CategoryPlot plot = new CategoryPlot(null, null, null, null);
627:                plot.setDomainAxis(new CategoryAxis("C"));
628:                plot.setRangeAxis(new NumberAxis("Y"));
629:                plot.setRenderer(new BarRenderer());
630:                plot.setDataset(new DefaultCategoryDataset());
631:                assertTrue(plot != null);
632:            }
633:
634:            /**
635:             * Some tests for the addDomainMarker() method(s).
636:             */
637:            public void testAddDomainMarker() {
638:                CategoryPlot plot = new CategoryPlot();
639:                CategoryMarker m = new CategoryMarker("C1");
640:                plot.addDomainMarker(m);
641:                List listeners = Arrays.asList(m
642:                        .getListeners(MarkerChangeListener.class));
643:                assertTrue(listeners.contains(plot));
644:                plot.clearDomainMarkers();
645:                listeners = Arrays.asList(m
646:                        .getListeners(MarkerChangeListener.class));
647:                assertFalse(listeners.contains(plot));
648:            }
649:
650:            /**
651:             * Some tests for the addRangeMarker() method(s).
652:             */
653:            public void testAddRangeMarker() {
654:                CategoryPlot plot = new CategoryPlot();
655:                Marker m = new ValueMarker(1.0);
656:                plot.addRangeMarker(m);
657:                List listeners = Arrays.asList(m
658:                        .getListeners(MarkerChangeListener.class));
659:                assertTrue(listeners.contains(plot));
660:                plot.clearRangeMarkers();
661:                listeners = Arrays.asList(m
662:                        .getListeners(MarkerChangeListener.class));
663:                assertFalse(listeners.contains(plot));
664:            }
665:
666:            /**
667:             * A test for bug 1654215 (where a renderer is added to the plot without
668:             * a corresponding dataset and it throws an exception at drawing time).
669:             */
670:            public void test1654215() {
671:                DefaultCategoryDataset dataset = new DefaultCategoryDataset();
672:                JFreeChart chart = ChartFactory.createLineChart("Title", "X",
673:                        "Y", dataset, PlotOrientation.VERTICAL, true, false,
674:                        false);
675:                CategoryPlot plot = (CategoryPlot) chart.getPlot();
676:                plot.setRenderer(1, new LineAndShapeRenderer());
677:                boolean success = false;
678:                try {
679:                    BufferedImage image = new BufferedImage(200, 100,
680:                            BufferedImage.TYPE_INT_RGB);
681:                    Graphics2D g2 = image.createGraphics();
682:                    chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100),
683:                            null, null);
684:                    g2.dispose();
685:                    success = true;
686:                } catch (Exception e) {
687:                    e.printStackTrace();
688:                    success = false;
689:                }
690:                assertTrue(success);
691:            }
692:
693:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.