1 42 43 package org.jfree.chart.title.junit; 44 45 import java.awt.BasicStroke ; 46 import java.awt.Color ; 47 import java.awt.Stroke ; 48 import java.awt.geom.Line2D ; 49 import java.awt.geom.Rectangle2D ; 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.title.LegendGraphic; 62 import org.jfree.ui.RectangleAnchor; 63 64 67 public class LegendGraphicTests extends TestCase { 68 69 74 public static Test suite() { 75 return new TestSuite(LegendGraphicTests.class); 76 } 77 78 83 public LegendGraphicTests(String name) { 84 super(name); 85 } 86 87 90 public void testEquals() { 91 LegendGraphic g1 = new LegendGraphic(new Rectangle2D.Double (1.0, 2.0, 92 3.0, 4.0), Color.black); 93 LegendGraphic g2 = new LegendGraphic(new Rectangle2D.Double (1.0, 2.0, 94 3.0, 4.0), Color.black); 95 assertEquals(g1, g2); 96 assertEquals(g2, g1); 97 98 g1.setShapeVisible(!g1.isShapeVisible()); 101 assertFalse(g1.equals(g2)); 102 g2.setShapeVisible(!g2.isShapeVisible()); 103 assertTrue(g1.equals(g2)); 104 105 g1.setShape(new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0)); 107 assertFalse(g1.equals(g2)); 108 g2.setShape(new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0)); 109 assertTrue(g1.equals(g2)); 110 111 g1.setShapeFilled(!g1.isShapeFilled()); 113 assertFalse(g1.equals(g2)); 114 g2.setShapeFilled(!g2.isShapeFilled()); 115 assertTrue(g1.equals(g2)); 116 117 g1.setFillPaint(Color.green); 119 assertFalse(g1.equals(g2)); 120 g2.setFillPaint(Color.green); 121 assertTrue(g1.equals(g2)); 122 123 g1.setShapeOutlineVisible(!g1.isShapeOutlineVisible()); 125 assertFalse(g1.equals(g2)); 126 g2.setShapeOutlineVisible(!g2.isShapeOutlineVisible()); 127 assertTrue(g1.equals(g2)); 128 129 g1.setOutlinePaint(Color.green); 131 assertFalse(g1.equals(g2)); 132 g2.setOutlinePaint(Color.green); 133 assertTrue(g1.equals(g2)); 134 135 g1.setOutlineStroke(new BasicStroke (1.23f)); 137 assertFalse(g1.equals(g2)); 138 g2.setOutlineStroke(new BasicStroke (1.23f)); 139 assertTrue(g1.equals(g2)); 140 141 g1.setShapeAnchor(RectangleAnchor.BOTTOM_RIGHT); 143 assertFalse(g1.equals(g2)); 144 g2.setShapeAnchor(RectangleAnchor.BOTTOM_RIGHT); 145 assertTrue(g1.equals(g2)); 146 147 g1.setShapeLocation(RectangleAnchor.BOTTOM_RIGHT); 149 assertFalse(g1.equals(g2)); 150 g2.setShapeLocation(RectangleAnchor.BOTTOM_RIGHT); 151 assertTrue(g1.equals(g2)); 152 153 g1.setLineVisible(!g1.isLineVisible()); 155 assertFalse(g1.equals(g2)); 156 g2.setLineVisible(!g2.isLineVisible()); 157 assertTrue(g1.equals(g2)); 158 159 g1.setLine(new Line2D.Double (1.0, 2.0, 3.0, 4.0)); 161 assertFalse(g1.equals(g2)); 162 g2.setLine(new Line2D.Double (1.0, 2.0, 3.0, 4.0)); 163 assertTrue(g1.equals(g2)); 164 165 g1.setLinePaint(Color.green); 167 assertFalse(g1.equals(g2)); 168 g2.setLinePaint(Color.green); 169 assertTrue(g1.equals(g2)); 170 171 g1.setLineStroke(new BasicStroke (1.23f)); 173 assertFalse(g1.equals(g2)); 174 g2.setLineStroke(new BasicStroke (1.23f)); 175 assertTrue(g1.equals(g2)); 176 } 177 178 181 public void testHashcode() { 182 LegendGraphic g1 = new LegendGraphic(new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), Color.black); 183 LegendGraphic g2 = new LegendGraphic(new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), Color.black); 184 assertTrue(g1.equals(g2)); 185 int h1 = g1.hashCode(); 186 int h2 = g2.hashCode(); 187 assertEquals(h1, h2); 188 } 189 190 193 public void testCloning() { 194 LegendGraphic g1 = new LegendGraphic(new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), Color.black); 195 LegendGraphic g2 = null; 196 try { 197 g2 = (LegendGraphic) g1.clone(); 198 } 199 catch (CloneNotSupportedException e) { 200 System.err.println("Failed to clone."); 201 } 202 assertTrue(g1 != g2); 203 assertTrue(g1.getClass() == g2.getClass()); 204 assertTrue(g1.equals(g2)); 205 } 206 207 210 public void testSerialization() { 211 212 Stroke s = new BasicStroke (1.23f); 213 LegendGraphic g1 = new LegendGraphic(new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), Color.black); 214 g1.setOutlineStroke(s); 215 LegendGraphic g2 = null; 216 217 try { 218 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 219 ObjectOutput out = new ObjectOutputStream (buffer); 220 out.writeObject(g1); 221 out.close(); 222 223 ObjectInput in = new ObjectInputStream ( 224 new ByteArrayInputStream (buffer.toByteArray()) 225 ); 226 g2 = (LegendGraphic) in.readObject(); 227 in.close(); 228 } 229 catch (Exception e) { 230 System.out.println(e.toString()); 231 } 232 assertTrue(g1.equals(g2)); 233 234 } 235 236 } 237 | Popular Tags |