1 44 45 package org.jfree.chart.renderer.category.junit; 46 47 import java.awt.Color ; 48 import java.awt.GradientPaint ; 49 import java.awt.Graphics2D ; 50 import java.awt.geom.Rectangle2D ; 51 import java.awt.image.BufferedImage ; 52 import java.io.ByteArrayInputStream ; 53 import java.io.ByteArrayOutputStream ; 54 import java.io.ObjectInput ; 55 import java.io.ObjectInputStream ; 56 import java.io.ObjectOutput ; 57 import java.io.ObjectOutputStream ; 58 import java.util.ArrayList ; 59 import java.util.List ; 60 61 import junit.framework.Test; 62 import junit.framework.TestCase; 63 import junit.framework.TestSuite; 64 import org.jfree.chart.ChartRenderingInfo; 65 66 import org.jfree.chart.JFreeChart; 67 import org.jfree.chart.axis.CategoryAxis; 68 import org.jfree.chart.axis.NumberAxis; 69 import org.jfree.chart.plot.CategoryPlot; 70 import org.jfree.chart.plot.PlotOrientation; 71 import org.jfree.chart.renderer.category.BoxAndWhiskerRenderer; 72 import org.jfree.data.statistics.BoxAndWhiskerItem; 73 import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset; 74 75 78 public class BoxAndWhiskerRendererTests extends TestCase { 79 80 85 public static Test suite() { 86 return new TestSuite(BoxAndWhiskerRendererTests.class); 87 } 88 89 94 public BoxAndWhiskerRendererTests(String name) { 95 super(name); 96 } 97 98 101 public void testEquals() { 102 BoxAndWhiskerRenderer r1 = new BoxAndWhiskerRenderer(); 103 BoxAndWhiskerRenderer r2 = new BoxAndWhiskerRenderer(); 104 assertEquals(r1, r2); 105 106 r1.setArtifactPaint(new GradientPaint (1.0f, 2.0f, Color.yellow, 107 3.0f, 4.0f, Color.blue)); 108 assertFalse(r1.equals(r2)); 109 r2.setArtifactPaint(new GradientPaint (1.0f, 2.0f, Color.yellow, 110 3.0f, 4.0f, Color.blue)); 111 assertEquals(r1, r2); 112 113 r1.setFillBox(!r1.getFillBox()); 114 assertFalse(r1.equals(r2)); 115 r2.setFillBox(!r2.getFillBox()); 116 assertEquals(r1, r2); 117 118 r1.setItemMargin(0.11); 119 assertFalse(r1.equals(r2)); 120 r2.setItemMargin(0.11); 121 assertEquals(r1, r2); 122 123 } 124 125 128 public void testHashcode() { 129 BoxAndWhiskerRenderer r1 = new BoxAndWhiskerRenderer(); 130 BoxAndWhiskerRenderer r2 = new BoxAndWhiskerRenderer(); 131 assertTrue(r1.equals(r2)); 132 int h1 = r1.hashCode(); 133 int h2 = r2.hashCode(); 134 assertEquals(h1, h2); 135 } 136 137 140 public void testCloning() { 141 BoxAndWhiskerRenderer r1 = new BoxAndWhiskerRenderer(); 142 BoxAndWhiskerRenderer r2 = null; 143 try { 144 r2 = (BoxAndWhiskerRenderer) r1.clone(); 145 } 146 catch (CloneNotSupportedException e) { 147 e.printStackTrace(); 148 } 149 assertTrue(r1 != r2); 150 assertTrue(r1.getClass() == r2.getClass()); 151 assertTrue(r1.equals(r2)); 152 } 153 154 157 public void testSerialization() { 158 159 BoxAndWhiskerRenderer r1 = new BoxAndWhiskerRenderer(); 160 BoxAndWhiskerRenderer r2 = null; 161 162 try { 163 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 164 ObjectOutput out = new ObjectOutputStream (buffer); 165 out.writeObject(r1); 166 out.close(); 167 168 ObjectInput in = new ObjectInputStream ( 169 new ByteArrayInputStream (buffer.toByteArray())); 170 r2 = (BoxAndWhiskerRenderer) in.readObject(); 171 in.close(); 172 } 173 catch (Exception e) { 174 e.printStackTrace(); 175 } 176 assertEquals(r1, r2); 177 178 } 179 180 184 public void testDrawWithNullInfo() { 185 boolean success = false; 186 try { 187 DefaultBoxAndWhiskerCategoryDataset dataset 188 = new DefaultBoxAndWhiskerCategoryDataset(); 189 dataset.add(new BoxAndWhiskerItem(new Double (1.0), new Double (2.0), 190 new Double (0.0), new Double (4.0), new Double (0.5), 191 new Double (4.5), new Double (-0.5), new Double (5.5), 192 null), "S1", "C1"); 193 CategoryPlot plot = new CategoryPlot(dataset, 194 new CategoryAxis("Category"), new NumberAxis("Value"), 195 new BoxAndWhiskerRenderer()); 196 JFreeChart chart = new JFreeChart(plot); 197 chart.createBufferedImage(300, 200, 198 null); 199 success = true; 200 } 201 catch (NullPointerException e) { 202 success = false; 203 } 204 assertTrue(success); 205 } 206 207 210 public void testBug1572478Vertical() { 211 DefaultBoxAndWhiskerCategoryDataset dataset 212 = new DefaultBoxAndWhiskerCategoryDataset() { 213 214 public Number getQ1Value(int row, int column) { 215 return null; 216 } 217 218 public Number getQ1Value(Comparable rowKey, Comparable columnKey) { 219 return null; 220 } 221 }; 222 List values = new ArrayList (); 223 values.add(new Double (1.0)); 224 values.add(new Double (10.0)); 225 values.add(new Double (100.0)); 226 dataset.add(values, "row", "column"); 227 CategoryPlot plot = new CategoryPlot(dataset, new CategoryAxis("x"), 228 new NumberAxis("y"), new BoxAndWhiskerRenderer()); 229 JFreeChart chart = new JFreeChart(plot); 230 boolean success = false; 231 232 try { 233 BufferedImage image = new BufferedImage (200 , 100, 234 BufferedImage.TYPE_INT_RGB); 235 Graphics2D g2 = image.createGraphics(); 236 chart.draw(g2, new Rectangle2D.Double (0, 0, 200, 100), null, 237 new ChartRenderingInfo()); 238 g2.dispose(); 239 success = true; 240 } 241 catch (Exception e) { 242 success = false; 243 } 244 245 assertTrue(success); 246 247 } 248 249 252 public void testBug1572478Horizontal() { 253 DefaultBoxAndWhiskerCategoryDataset dataset 254 = new DefaultBoxAndWhiskerCategoryDataset() { 255 256 public Number getQ1Value(int row, int column) { 257 return null; 258 } 259 260 public Number getQ1Value(Comparable rowKey, Comparable columnKey) { 261 return null; 262 } 263 }; 264 List values = new ArrayList (); 265 values.add(new Double (1.0)); 266 values.add(new Double (10.0)); 267 values.add(new Double (100.0)); 268 dataset.add(values, "row", "column"); 269 CategoryPlot plot = new CategoryPlot(dataset, new CategoryAxis("x"), 270 new NumberAxis("y"), new BoxAndWhiskerRenderer()); 271 plot.setOrientation(PlotOrientation.HORIZONTAL); 272 JFreeChart chart = new JFreeChart(plot); 273 boolean success = false; 274 275 try { 276 BufferedImage image = new BufferedImage (200 , 100, 277 BufferedImage.TYPE_INT_RGB); 278 Graphics2D g2 = image.createGraphics(); 279 chart.draw(g2, new Rectangle2D.Double (0, 0, 200, 100), null, 280 new ChartRenderingInfo()); 281 g2.dispose(); 282 success = true; 283 } 284 catch (Exception e) { 285 success = false; 286 } 287 288 assertTrue(success); 289 290 } 291 } 292 | Popular Tags |