1 46 47 package org.jfree.chart.renderer.xy.junit; 48 49 import java.awt.BasicStroke ; 50 import java.awt.Color ; 51 import java.awt.GradientPaint ; 52 import java.awt.Stroke ; 53 import java.io.ByteArrayInputStream ; 54 import java.io.ByteArrayOutputStream ; 55 import java.io.ObjectInput ; 56 import java.io.ObjectInputStream ; 57 import java.io.ObjectOutput ; 58 import java.io.ObjectOutputStream ; 59 60 import junit.framework.Test; 61 import junit.framework.TestCase; 62 import junit.framework.TestSuite; 63 64 import org.jfree.chart.ChartFactory; 65 import org.jfree.chart.JFreeChart; 66 import org.jfree.chart.axis.NumberAxis; 67 import org.jfree.chart.plot.PlotOrientation; 68 import org.jfree.chart.plot.XYPlot; 69 import org.jfree.chart.renderer.xy.StackedXYAreaRenderer; 70 import org.jfree.chart.renderer.xy.XYAreaRenderer; 71 import org.jfree.data.Range; 72 import org.jfree.data.xy.DefaultTableXYDataset; 73 import org.jfree.data.xy.TableXYDataset; 74 import org.jfree.data.xy.XYSeries; 75 76 79 public class StackedXYAreaRendererTests extends TestCase { 80 81 86 public static Test suite() { 87 return new TestSuite(StackedXYAreaRendererTests.class); 88 } 89 90 95 public StackedXYAreaRendererTests(String name) { 96 super(name); 97 } 98 99 102 public void testEquals() { 103 StackedXYAreaRenderer r1 = new StackedXYAreaRenderer(); 104 StackedXYAreaRenderer r2 = new StackedXYAreaRenderer(); 105 assertEquals(r1, r2); 106 assertEquals(r2, r1); 107 108 r1.setShapePaint(new GradientPaint (1.0f, 2.0f, Color.yellow, 109 3.0f, 4.0f, Color.green)); 110 assertFalse(r1.equals(r2)); 111 r2.setShapePaint(new GradientPaint (1.0f, 2.0f, Color.yellow, 112 3.0f, 4.0f, Color.green)); 113 assertTrue(r1.equals(r2)); 114 115 Stroke s = new BasicStroke (1.23f); 116 r1.setShapeStroke(s); 117 assertFalse(r1.equals(r2)); 118 r2.setShapeStroke(s); 119 assertTrue(r1.equals(r2)); 120 } 121 122 125 public void testHashcode() { 126 StackedXYAreaRenderer r1 = new StackedXYAreaRenderer(); 127 StackedXYAreaRenderer r2 = new StackedXYAreaRenderer(); 128 assertTrue(r1.equals(r2)); 129 int h1 = r1.hashCode(); 130 int h2 = r2.hashCode(); 131 assertEquals(h1, h2); 132 } 133 134 137 public void testCloning() { 138 StackedXYAreaRenderer r1 = new StackedXYAreaRenderer(); 139 StackedXYAreaRenderer r2 = null; 140 try { 141 r2 = (StackedXYAreaRenderer) r1.clone(); 142 } 143 catch (CloneNotSupportedException e) { 144 System.err.println("Failed to clone."); 145 } 146 assertTrue(r1 != r2); 147 assertTrue(r1.getClass() == r2.getClass()); 148 assertTrue(r1.equals(r2)); 149 } 150 151 154 public void testSerialization() { 155 StackedXYAreaRenderer r1 = new StackedXYAreaRenderer(); 156 r1.setShapePaint(Color.red); 157 r1.setShapeStroke(new BasicStroke (1.23f)); 158 StackedXYAreaRenderer r2 = null; 159 try { 160 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 161 ObjectOutput out = new ObjectOutputStream (buffer); 162 out.writeObject(r1); 163 out.close(); 164 165 ObjectInput in = new ObjectInputStream ( 166 new ByteArrayInputStream (buffer.toByteArray()) 167 ); 168 r2 = (StackedXYAreaRenderer) in.readObject(); 169 in.close(); 170 } 171 catch (Exception e) { 172 System.out.println(e.toString()); 173 } 174 assertEquals(r1, r2); 175 } 176 177 180 public void testFindRangeBounds() { 181 TableXYDataset dataset 182 = RendererXYPackageTests.createTestTableXYDataset(); 183 JFreeChart chart = ChartFactory.createStackedXYAreaChart( 184 "Test Chart", "X", "Y", dataset, 185 PlotOrientation.VERTICAL, false, false, false 186 ); 187 XYPlot plot = (XYPlot) chart.getPlot(); 188 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 189 Range bounds = rangeAxis.getRange(); 190 assertTrue(bounds.contains(6.0)); 191 assertTrue(bounds.contains(8.0)); 192 } 193 194 198 public void testDrawWithNullInfo() { 199 boolean success = false; 200 try { 201 DefaultTableXYDataset dataset = new DefaultTableXYDataset(); 202 203 XYSeries s1 = new XYSeries("Series 1", true, false); 204 s1.add(5.0, 5.0); 205 s1.add(10.0, 15.5); 206 s1.add(15.0, 9.5); 207 s1.add(20.0, 7.5); 208 dataset.addSeries(s1); 209 210 XYSeries s2 = new XYSeries("Series 2", true, false); 211 s2.add(5.0, 5.0); 212 s2.add(10.0, 15.5); 213 s2.add(15.0, 9.5); 214 s2.add(20.0, 3.5); 215 dataset.addSeries(s2); 216 XYPlot plot = new XYPlot(dataset, 217 new NumberAxis("X"), new NumberAxis("Y"), 218 new StackedXYAreaRenderer()); 219 JFreeChart chart = new JFreeChart(plot); 220 chart.createBufferedImage(300, 200, 221 null); 222 success = true; 223 } 224 catch (NullPointerException e) { 225 e.printStackTrace(); 226 success = false; 227 } 228 assertTrue(success); 229 } 230 231 234 public void testBug1593156() { 235 boolean success = false; 236 try { 237 DefaultTableXYDataset dataset = new DefaultTableXYDataset(); 238 239 XYSeries s1 = new XYSeries("Series 1", true, false); 240 s1.add(5.0, 5.0); 241 s1.add(10.0, 15.5); 242 s1.add(15.0, 9.5); 243 s1.add(20.0, 7.5); 244 dataset.addSeries(s1); 245 246 XYSeries s2 = new XYSeries("Series 2", true, false); 247 s2.add(5.0, 5.0); 248 s2.add(10.0, 15.5); 249 s2.add(15.0, 9.5); 250 s2.add(20.0, 3.5); 251 dataset.addSeries(s2); 252 StackedXYAreaRenderer renderer = new StackedXYAreaRenderer( 253 XYAreaRenderer.LINES); 254 XYPlot plot = new XYPlot(dataset, 255 new NumberAxis("X"), new NumberAxis("Y"), 256 renderer); 257 JFreeChart chart = new JFreeChart(plot); 258 chart.createBufferedImage(300, 200, 259 null); 260 success = true; 261 } 262 catch (NullPointerException e) { 263 e.printStackTrace(); 264 success = false; 265 } 266 assertTrue(success); 267 } 268 269 } 270 | Popular Tags |