1 46 47 package org.jfree.chart.renderer.category.junit; 48 49 import java.awt.Color ; 50 import java.io.ByteArrayInputStream ; 51 import java.io.ByteArrayOutputStream ; 52 import java.io.ObjectInput ; 53 import java.io.ObjectInputStream ; 54 import java.io.ObjectOutput ; 55 import java.io.ObjectOutputStream ; 56 57 import junit.framework.Test; 58 import junit.framework.TestCase; 59 import junit.framework.TestSuite; 60 61 import org.jfree.chart.labels.ItemLabelAnchor; 62 import org.jfree.chart.labels.ItemLabelPosition; 63 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; 64 import org.jfree.chart.renderer.category.BarRenderer; 65 import org.jfree.chart.renderer.junit.RendererChangeDetector; 66 import org.jfree.ui.GradientPaintTransformType; 67 import org.jfree.ui.StandardGradientPaintTransformer; 68 import org.jfree.ui.TextAnchor; 69 70 73 public class BarRendererTests extends TestCase { 74 75 80 public static Test suite() { 81 return new TestSuite(BarRendererTests.class); 82 } 83 84 89 public BarRendererTests(String name) { 90 super(name); 91 } 92 93 96 public void testEquals() { 97 BarRenderer r1 = new BarRenderer(); 98 BarRenderer r2 = new BarRenderer(); 99 assertTrue(r1.equals(r2)); 100 assertTrue(r2.equals(r1)); 101 102 r1.setBase(0.123); 104 assertFalse(r1.equals(r2)); 105 r2.setBase(0.123); 106 assertTrue(r1.equals(r2)); 107 108 r1.setItemMargin(0.22); 110 assertFalse(r1.equals(r2)); 111 r2.setItemMargin(0.22); 112 assertTrue(r1.equals(r2)); 113 114 r1.setDrawBarOutline(!r1.isDrawBarOutline()); 116 assertFalse(r1.equals(r2)); 117 r2.setDrawBarOutline(!r2.isDrawBarOutline()); 118 assertTrue(r1.equals(r2)); 119 120 r1.setMaximumBarWidth(0.11); 122 assertFalse(r1.equals(r2)); 123 r2.setMaximumBarWidth(0.11); 124 assertTrue(r1.equals(r2)); 125 126 r1.setMinimumBarLength(0.04); 128 assertFalse(r1.equals(r2)); 129 r2.setMinimumBarLength(0.04); 130 assertTrue(r1.equals(r2)); 131 132 r1.setGradientPaintTransformer( 134 new StandardGradientPaintTransformer( 135 GradientPaintTransformType.CENTER_VERTICAL 136 ) 137 ); 138 assertFalse(r1.equals(r2)); 139 r2.setGradientPaintTransformer( 140 new StandardGradientPaintTransformer( 141 GradientPaintTransformType.CENTER_VERTICAL 142 ) 143 ); 144 assertTrue(r1.equals(r2)); 145 146 r1.setPositiveItemLabelPositionFallback( 148 new ItemLabelPosition(ItemLabelAnchor.INSIDE1, TextAnchor.CENTER) 149 ); 150 assertFalse(r1.equals(r2)); 151 r2.setPositiveItemLabelPositionFallback( 152 new ItemLabelPosition(ItemLabelAnchor.INSIDE1, TextAnchor.CENTER) 153 ); 154 assertTrue(r1.equals(r2)); 155 156 r1.setNegativeItemLabelPositionFallback( 158 new ItemLabelPosition(ItemLabelAnchor.INSIDE1, TextAnchor.CENTER) 159 ); 160 assertFalse(r1.equals(r2)); 161 r2.setNegativeItemLabelPositionFallback( 162 new ItemLabelPosition(ItemLabelAnchor.INSIDE1, TextAnchor.CENTER) 163 ); 164 assertTrue(r1.equals(r2)); 165 166 } 167 168 171 public void testHashcode() { 172 BarRenderer r1 = new BarRenderer(); 173 BarRenderer r2 = new BarRenderer(); 174 assertTrue(r1.equals(r2)); 175 int h1 = r1.hashCode(); 176 int h2 = r2.hashCode(); 177 assertEquals(h1, h2); 178 } 179 180 183 public void testCloning() { 184 BarRenderer r1 = new BarRenderer(); 185 r1.setItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 186 BarRenderer r2 = null; 187 try { 188 r2 = (BarRenderer) r1.clone(); 189 } 190 catch (CloneNotSupportedException e) { 191 System.err.println("Failed to clone."); 192 } 193 assertTrue(r1 != r2); 194 assertTrue(r1.getClass() == r2.getClass()); 195 assertTrue(r1.equals(r2)); 196 } 197 198 201 public void testSerialization() { 202 203 BarRenderer r1 = new BarRenderer(); 204 BarRenderer r2 = null; 205 206 try { 207 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 208 ObjectOutput out = new ObjectOutputStream (buffer); 209 out.writeObject(r1); 210 out.close(); 211 212 ObjectInput in = new ObjectInputStream ( 213 new ByteArrayInputStream (buffer.toByteArray()) 214 ); 215 r2 = (BarRenderer) in.readObject(); 216 in.close(); 217 } 218 catch (Exception e) { 219 System.out.println(e.toString()); 220 } 221 assertEquals(r1, r2); 222 223 } 224 225 228 public void testEventNotification() { 229 230 RendererChangeDetector detector = new RendererChangeDetector(); 231 BarRenderer r1 = new BarRenderer(); 232 r1.addChangeListener(detector); 233 234 detector.setNotified(false); 235 r1.setPaint(Color.red); 236 assertTrue(detector.getNotified()); 237 238 } 239 240 } 241 | Popular Tags |