1 43 44 package org.jfree.chart.renderer.xy.junit; 45 46 import java.awt.Graphics2D ; 47 import java.awt.geom.Rectangle2D ; 48 import java.awt.image.BufferedImage ; 49 import java.io.ByteArrayInputStream ; 50 import java.io.ByteArrayOutputStream ; 51 import java.io.ObjectInput ; 52 import java.io.ObjectInputStream ; 53 import java.io.ObjectOutput ; 54 import java.io.ObjectOutputStream ; 55 56 import junit.framework.Test; 57 import junit.framework.TestCase; 58 import junit.framework.TestSuite; 59 60 import org.jfree.chart.ChartFactory; 61 import org.jfree.chart.JFreeChart; 62 import org.jfree.chart.axis.NumberAxis; 63 import org.jfree.chart.plot.PlotOrientation; 64 import org.jfree.chart.plot.XYPlot; 65 import org.jfree.chart.renderer.xy.StackedXYAreaRenderer2; 66 import org.jfree.data.Range; 67 import org.jfree.data.xy.DefaultTableXYDataset; 68 import org.jfree.data.xy.TableXYDataset; 69 70 73 public class StackedXYAreaRenderer2Tests extends TestCase { 74 75 80 public static Test suite() { 81 return new TestSuite(StackedXYAreaRenderer2Tests.class); 82 } 83 84 89 public StackedXYAreaRenderer2Tests(String name) { 90 super(name); 91 } 92 93 97 public void testDrawWithEmptyDataset() { 98 boolean success = false; 99 JFreeChart chart = ChartFactory.createStackedXYAreaChart("title", "x", 100 "y", new DefaultTableXYDataset(), PlotOrientation.VERTICAL, 101 true, false, false); 102 XYPlot plot = (XYPlot) chart.getPlot(); 103 plot.setRenderer(new StackedXYAreaRenderer2()); 104 try { 105 BufferedImage image = new BufferedImage (200 , 100, 106 BufferedImage.TYPE_INT_RGB); 107 Graphics2D g2 = image.createGraphics(); 108 chart.draw(g2, new Rectangle2D.Double (0, 0, 200, 100), null, null); 109 g2.dispose(); 110 success = true; 111 } 112 catch (Exception e) { 113 success = false; 114 } 115 assertTrue(success); 116 } 117 118 121 public void testEquals() { 122 StackedXYAreaRenderer2 r1 = new StackedXYAreaRenderer2(); 123 StackedXYAreaRenderer2 r2 = new StackedXYAreaRenderer2(); 124 assertEquals(r1, r2); 125 assertEquals(r2, r1); 126 } 127 128 131 public void testHashcode() { 132 StackedXYAreaRenderer2 r1 = new StackedXYAreaRenderer2(); 133 StackedXYAreaRenderer2 r2 = new StackedXYAreaRenderer2(); 134 assertTrue(r1.equals(r2)); 135 int h1 = r1.hashCode(); 136 int h2 = r2.hashCode(); 137 assertEquals(h1, h2); 138 } 139 140 143 public void testCloning() { 144 StackedXYAreaRenderer2 r1 = new StackedXYAreaRenderer2(); 145 StackedXYAreaRenderer2 r2 = null; 146 try { 147 r2 = (StackedXYAreaRenderer2) r1.clone(); 148 } 149 catch (CloneNotSupportedException e) { 150 e.printStackTrace(); 151 } 152 assertTrue(r1 != r2); 153 assertTrue(r1.getClass() == r2.getClass()); 154 assertTrue(r1.equals(r2)); 155 } 156 157 160 public void testSerialization() { 161 StackedXYAreaRenderer2 r1 = new StackedXYAreaRenderer2(); 162 StackedXYAreaRenderer2 r2 = null; 163 try { 164 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 165 ObjectOutput out = new ObjectOutputStream (buffer); 166 out.writeObject(r1); 167 out.close(); 168 169 ObjectInput in = new ObjectInputStream ( 170 new ByteArrayInputStream (buffer.toByteArray())); 171 r2 = (StackedXYAreaRenderer2) in.readObject(); 172 in.close(); 173 } 174 catch (Exception e) { 175 e.printStackTrace(); 176 } 177 assertEquals(r1, r2); 178 } 179 180 183 public void testFindRangeBounds() { 184 TableXYDataset dataset 185 = RendererXYPackageTests.createTestTableXYDataset(); 186 JFreeChart chart = ChartFactory.createStackedXYAreaChart( 187 "Test Chart", "X", "Y", dataset, PlotOrientation.VERTICAL, 188 false, false, false); 189 XYPlot plot = (XYPlot) chart.getPlot(); 190 StackedXYAreaRenderer2 renderer = new StackedXYAreaRenderer2(); 191 plot.setRenderer(renderer); 192 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 193 Range bounds = rangeAxis.getRange(); 194 assertTrue(bounds.contains(6.0)); 195 assertTrue(bounds.contains(8.0)); 196 197 assertNull(renderer.findRangeBounds(null)); 199 200 assertNull(renderer.findRangeBounds(new DefaultTableXYDataset())); 202 } 203 204 } 205 | Popular Tags |