1 43 44 package org.jfree.chart.renderer.xy.junit; 45 46 import java.awt.geom.Rectangle2D ; 47 import java.io.ByteArrayInputStream ; 48 import java.io.ByteArrayOutputStream ; 49 import java.io.ObjectInput ; 50 import java.io.ObjectInputStream ; 51 import java.io.ObjectOutput ; 52 import java.io.ObjectOutputStream ; 53 54 import junit.framework.Test; 55 import junit.framework.TestCase; 56 import junit.framework.TestSuite; 57 58 import org.jfree.chart.ChartFactory; 59 import org.jfree.chart.JFreeChart; 60 import org.jfree.chart.axis.NumberAxis; 61 import org.jfree.chart.labels.ItemLabelPosition; 62 import org.jfree.chart.plot.PlotOrientation; 63 import org.jfree.chart.plot.XYPlot; 64 import org.jfree.chart.renderer.xy.XYBarRenderer; 65 import org.jfree.data.Range; 66 import org.jfree.data.xy.XYSeriesCollection; 67 import org.jfree.ui.GradientPaintTransformType; 68 import org.jfree.ui.StandardGradientPaintTransformer; 69 70 73 public class XYBarRendererTests extends TestCase { 74 75 80 public static Test suite() { 81 return new TestSuite(XYBarRendererTests.class); 82 } 83 84 89 public XYBarRendererTests(String name) { 90 super(name); 91 } 92 93 96 public void testEquals() { 97 98 XYBarRenderer r1 = new XYBarRenderer(); 100 XYBarRenderer r2 = new XYBarRenderer(); 101 assertTrue(r1.equals(r2)); 102 assertTrue(r2.equals(r1)); 103 104 r1.setBase(1.0); 106 assertFalse(r1.equals(r2)); 107 r2.setBase(1.0); 108 assertTrue(r1.equals(r2)); 109 110 r1.setUseYInterval(!r1.getUseYInterval()); 112 assertFalse(r1.equals(r2)); 113 r2.setUseYInterval(!r2.getUseYInterval()); 114 assertTrue(r1.equals(r2)); 115 116 r1.setMargin(0.10); 118 assertFalse(r1.equals(r2)); 119 r2.setMargin(0.10); 120 assertTrue(r1.equals(r2)); 121 122 r1.setDrawBarOutline(!r1.isDrawBarOutline()); 124 assertFalse(r1.equals(r2)); 125 r2.setDrawBarOutline(!r2.isDrawBarOutline()); 126 assertTrue(r1.equals(r2)); 127 128 r1.setGradientPaintTransformer(new StandardGradientPaintTransformer( 130 GradientPaintTransformType.CENTER_HORIZONTAL)); 131 assertFalse(r1.equals(r2)); 132 r2.setGradientPaintTransformer(new StandardGradientPaintTransformer( 133 GradientPaintTransformType.CENTER_HORIZONTAL)); 134 assertTrue(r1.equals(r2)); 135 136 r1.setLegendBar(new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0)); 138 assertFalse(r1.equals(r2)); 139 r2.setLegendBar(new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0)); 140 assertTrue(r1.equals(r2)); 141 142 r1.setPositiveItemLabelPositionFallback(new ItemLabelPosition()); 144 assertFalse(r1.equals(r2)); 145 r2.setPositiveItemLabelPositionFallback(new ItemLabelPosition()); 146 assertTrue(r1.equals(r2)); 147 148 r1.setNegativeItemLabelPositionFallback(new ItemLabelPosition()); 150 assertFalse(r1.equals(r2)); 151 r2.setNegativeItemLabelPositionFallback(new ItemLabelPosition()); 152 assertTrue(r1.equals(r2)); 153 } 154 155 158 public void testHashcode() { 159 XYBarRenderer r1 = new XYBarRenderer(); 160 XYBarRenderer r2 = new XYBarRenderer(); 161 assertTrue(r1.equals(r2)); 162 int h1 = r1.hashCode(); 163 int h2 = r2.hashCode(); 164 assertEquals(h1, h2); 165 } 166 167 170 public void testCloning() { 171 XYBarRenderer r1 = new XYBarRenderer(); 172 XYBarRenderer r2 = null; 173 try { 174 r2 = (XYBarRenderer) r1.clone(); 175 } 176 catch (CloneNotSupportedException e) { 177 System.err.println("Failed to clone."); 178 } 179 assertTrue(r1 != r2); 180 assertTrue(r1.getClass() == r2.getClass()); 181 assertTrue(r1.equals(r2)); 182 } 183 184 187 public void testSerialization() { 188 189 XYBarRenderer r1 = new XYBarRenderer(); 190 XYBarRenderer r2 = null; 191 192 try { 193 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 194 ObjectOutput out = new ObjectOutputStream (buffer); 195 out.writeObject(r1); 196 out.close(); 197 198 ObjectInput in = new ObjectInputStream ( 199 new ByteArrayInputStream (buffer.toByteArray()) 200 ); 201 r2 = (XYBarRenderer) in.readObject(); 202 in.close(); 203 } 204 catch (Exception e) { 205 System.out.println(e.toString()); 206 } 207 assertEquals(r1, r2); 208 209 } 210 211 214 public void testSerialization2() { 215 216 XYBarRenderer r1 = new XYBarRenderer(); 217 r1.setPositiveItemLabelPositionFallback(new ItemLabelPosition()); 218 XYBarRenderer r2 = null; 219 220 try { 221 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 222 ObjectOutput out = new ObjectOutputStream (buffer); 223 out.writeObject(r1); 224 out.close(); 225 226 ObjectInput in = new ObjectInputStream ( 227 new ByteArrayInputStream (buffer.toByteArray()) 228 ); 229 r2 = (XYBarRenderer) in.readObject(); 230 in.close(); 231 } 232 catch (Exception e) { 233 System.out.println(e.toString()); 234 } 235 assertEquals(r1, r2); 236 237 } 238 239 242 public void testFindDomainBounds() { 243 XYSeriesCollection dataset 244 = RendererXYPackageTests.createTestXYSeriesCollection(); 245 JFreeChart chart = ChartFactory.createXYBarChart( 246 "Test Chart", "X", false, "Y", dataset, 247 PlotOrientation.VERTICAL, false, false, false 248 ); 249 XYPlot plot = (XYPlot) chart.getPlot(); 250 NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis(); 251 domainAxis.setAutoRangeIncludesZero(false); 252 Range bounds = domainAxis.getRange(); 253 assertFalse(bounds.contains(0.3)); 254 assertTrue(bounds.contains(0.5)); 255 assertTrue(bounds.contains(2.5)); 256 assertFalse(bounds.contains(2.8)); 257 } 258 259 } 260 | Popular Tags |