1 43 44 package org.jfree.chart.junit; 45 46 import java.awt.BasicStroke ; 47 import java.awt.Color ; 48 import java.awt.Font ; 49 import java.awt.font.TextAttribute ; 50 import java.awt.geom.Line2D ; 51 import java.awt.geom.Rectangle2D ; 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.text.AttributedString ; 59 60 import junit.framework.Test; 61 import junit.framework.TestCase; 62 import junit.framework.TestSuite; 63 64 import org.jfree.chart.LegendItem; 65 66 69 public class LegendItemTests extends TestCase { 70 71 76 public static Test suite() { 77 return new TestSuite(LegendItemTests.class); 78 } 79 80 85 public LegendItemTests(String name) { 86 super(name); 87 } 88 89 92 public void testEquals() { 93 94 LegendItem item1 = new LegendItem("Label", "Description", 95 "ToolTip", "URL", true, 96 new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), true, Color.red, 97 true, Color.blue, new BasicStroke (1.2f), true, 98 new Line2D.Double (1.0, 2.0, 3.0, 4.0), 99 new BasicStroke (2.1f), Color.green); 100 LegendItem item2 = new LegendItem("Label", "Description", 101 "ToolTip", "URL", true, 102 new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), 103 true, Color.red, true, Color.blue, new BasicStroke (1.2f), true, 104 new Line2D.Double (1.0, 2.0, 3.0, 4.0), new BasicStroke (2.1f), 105 Color.green); 106 assertTrue(item1.equals(item2)); 107 assertTrue(item2.equals(item1)); 108 109 item1 = new LegendItem("Label2", "Description", "ToolTip", "URL", 110 true, new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), true, 111 Color.red, true, Color.blue, new BasicStroke (1.2f), true, 112 new Line2D.Double (1.0, 2.0, 3.0, 4.0), new BasicStroke (2.1f), 113 Color.green); 114 assertFalse(item1.equals(item2)); 115 item2 = new LegendItem("Label2", "Description", "ToolTip", "URL", 116 true, new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), 117 true, Color.red, true, Color.blue, new BasicStroke (1.2f), true, 118 new Line2D.Double (1.0, 2.0, 3.0, 4.0), new BasicStroke (2.1f), 119 Color.green); 120 assertTrue(item1.equals(item2)); 121 122 item1 = new LegendItem("Label2", "Description2", "ToolTip", 123 "URL", true, new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), 124 true, Color.red, true, Color.blue, new BasicStroke (1.2f), true, 125 new Line2D.Double (1.0, 2.0, 3.0, 4.0), new BasicStroke (2.1f), 126 Color.green); 127 assertFalse(item1.equals(item2)); 128 item2 = new LegendItem("Label2", "Description2", "ToolTip", 129 "URL", true, new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), 130 true, Color.red, true, Color.blue, new BasicStroke (1.2f), true, 131 new Line2D.Double (1.0, 2.0, 3.0, 4.0), new BasicStroke (2.1f), 132 Color.green); 133 assertTrue(item1.equals(item2)); 134 135 item1 = new LegendItem("Label2", "Description2", "ToolTip", 136 "URL", false, new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), 137 true, Color.red, true, Color.blue, new BasicStroke (1.2f), true, 138 new Line2D.Double (1.0, 2.0, 3.0, 4.0), new BasicStroke (2.1f), 139 Color.green); 140 assertFalse(item1.equals(item2)); 141 item2 = new LegendItem("Label2", "Description2", "ToolTip", 142 "URL", false, new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), 143 true, Color.red, true, Color.blue, new BasicStroke (1.2f), true, 144 new Line2D.Double (1.0, 2.0, 3.0, 4.0), new BasicStroke (2.1f), 145 Color.green); 146 assertTrue(item1.equals(item2)); 147 148 item1 = new LegendItem("Label2", "Description2", "ToolTip", 149 "URL", false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 150 true, Color.red, true, Color.blue, new BasicStroke (1.2f), true, 151 new Line2D.Double (1.0, 2.0, 3.0, 4.0), new BasicStroke (2.1f), 152 Color.green); 153 assertFalse(item1.equals(item2)); 154 item2 = new LegendItem("Label2", "Description2", "ToolTip", 155 "URL", false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 156 true, Color.red, true, Color.blue, new BasicStroke (1.2f), true, 157 new Line2D.Double (1.0, 2.0, 3.0, 4.0), new BasicStroke (2.1f), 158 Color.green); 159 assertTrue(item1.equals(item2)); 160 161 item1 = new LegendItem("Label2", "Description2", "ToolTip", 162 "URL", false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 163 false, Color.red, true, Color.blue, new BasicStroke (1.2f), true, 164 new Line2D.Double (1.0, 2.0, 3.0, 4.0), new BasicStroke (2.1f), 165 Color.green); 166 assertFalse(item1.equals(item2)); 167 item2 = new LegendItem("Label2", "Description2", "ToolTip", 168 "URL", false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 169 false, Color.red, true, Color.blue, new BasicStroke (1.2f), true, 170 new Line2D.Double (1.0, 2.0, 3.0, 4.0), new BasicStroke (2.1f), 171 Color.green); 172 assertTrue(item1.equals(item2)); 173 174 item1 = new LegendItem("Label2", "Description2", "ToolTip", "URL", 175 false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), false, 176 Color.black, true, Color.blue, new BasicStroke (1.2f), true, 177 new Line2D.Double (1.0, 2.0, 3.0, 4.0), new BasicStroke (2.1f), 178 Color.green); 179 assertFalse(item1.equals(item2)); 180 item2 = new LegendItem("Label2", "Description2", "ToolTip", "URL", 181 false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), false, 182 Color.black, true, Color.blue, new BasicStroke (1.2f), true, 183 new Line2D.Double (1.0, 2.0, 3.0, 4.0), new BasicStroke (2.1f), 184 Color.green); 185 assertTrue(item1.equals(item2)); 186 187 item1 = new LegendItem("Label2", "Description2", "ToolTip", 188 "URL", false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 189 false, Color.black, false, Color.blue, new BasicStroke (1.2f), 190 true, new Line2D.Double (1.0, 2.0, 3.0, 4.0), 191 new BasicStroke (2.1f), Color.green); 192 assertFalse(item1.equals(item2)); 193 item2 = new LegendItem("Label2", "Description2", "ToolTip", "URL", 194 false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), false, 195 Color.black, false, Color.blue, new BasicStroke (1.2f), true, 196 new Line2D.Double (1.0, 2.0, 3.0, 4.0), new BasicStroke (2.1f), 197 Color.green); 198 assertTrue(item1.equals(item2)); 199 200 item1 = new LegendItem("Label2", "Description2", "ToolTip", "URL", 201 false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), false, 202 Color.black, false, Color.yellow, new BasicStroke (1.2f), true, 203 new Line2D.Double (1.0, 2.0, 3.0, 4.0), new BasicStroke (2.1f), 204 Color.green); 205 assertFalse(item1.equals(item2)); 206 item2 = new LegendItem("Label2", "Description2", "ToolTip", "URL", 207 false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), false, 208 Color.black, false, Color.yellow, new BasicStroke (1.2f), true, 209 new Line2D.Double (1.0, 2.0, 3.0, 4.0), new BasicStroke (2.1f), 210 Color.green); 211 assertTrue(item1.equals(item2)); 212 213 item1 = new LegendItem("Label2", "Description2", "ToolTip", "URL", 214 false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), false, 215 Color.black, false, Color.yellow, new BasicStroke (2.1f), true, 216 new Line2D.Double (1.0, 2.0, 3.0, 4.0), new BasicStroke (2.1f), 217 Color.green); 218 assertFalse(item1.equals(item2)); 219 item2 = new LegendItem("Label2", "Description2", "ToolTip", "URL", 220 false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), false, 221 Color.black, false, Color.yellow, new BasicStroke (2.1f), true, 222 new Line2D.Double (1.0, 2.0, 3.0, 4.0), new BasicStroke (2.1f), 223 Color.green); 224 assertTrue(item1.equals(item2)); 225 226 item1 = new LegendItem("Label2", "Description2", "ToolTip", 227 "URL", false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 228 false, Color.black, false, Color.yellow, new BasicStroke (2.1f), 229 false, new Line2D.Double (1.0, 2.0, 3.0, 4.0), 230 new BasicStroke (2.1f), Color.green); 231 assertFalse(item1.equals(item2)); 232 item2 = new LegendItem("Label2", "Description2", "ToolTip", 233 "URL", false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 234 false, Color.black, false, Color.yellow, new BasicStroke (2.1f), 235 false, new Line2D.Double (1.0, 2.0, 3.0, 4.0), 236 new BasicStroke (2.1f), Color.green); 237 assertTrue(item1.equals(item2)); 238 239 item1 = new LegendItem("Label2", "Description2", "ToolTip", 240 "URL", false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 241 false, Color.black, false, Color.yellow, new BasicStroke (2.1f), 242 false, new Line2D.Double (4.0, 3.0, 2.0, 1.0), 243 new BasicStroke (2.1f), Color.green); 244 assertFalse(item1.equals(item2)); 245 item2 = new LegendItem("Label2", "Description2", "ToolTip", 246 "URL", false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 247 false, Color.black, false, Color.yellow, new BasicStroke (2.1f), 248 false, new Line2D.Double (4.0, 3.0, 2.0, 1.0), 249 new BasicStroke (2.1f), Color.green); 250 assertTrue(item1.equals(item2)); 251 252 item1 = new LegendItem("Label2", "Description2", "ToolTip", 253 "URL", false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 254 false, Color.black, false, Color.yellow, new BasicStroke (2.1f), 255 false, new Line2D.Double (4.0, 3.0, 2.0, 1.0), 256 new BasicStroke (3.3f), Color.green); 257 assertFalse(item1.equals(item2)); 258 item2 = new LegendItem("Label2", "Description2", "ToolTip", 259 "URL", false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 260 false, Color.black, false, Color.yellow, new BasicStroke (2.1f), 261 false, new Line2D.Double (4.0, 3.0, 2.0, 1.0), 262 new BasicStroke (3.3f), Color.green); 263 assertTrue(item1.equals(item2)); 264 265 item1 = new LegendItem("Label2", "Description2", "ToolTip", "URL", 266 false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), false, 267 Color.black, false, Color.yellow, new BasicStroke (2.1f), false, 268 new Line2D.Double (4.0, 3.0, 2.0, 1.0), new BasicStroke (3.3f), 269 Color.white 270 ); 271 assertFalse(item1.equals(item2)); 272 item2 = new LegendItem("Label2", "Description2", "ToolTip", 273 "URL", false, new Rectangle2D.Double (4.0, 3.0, 2.0, 1.0), 274 false, Color.black, false, Color.yellow, new BasicStroke (2.1f), 275 false, new Line2D.Double (4.0, 3.0, 2.0, 1.0), 276 new BasicStroke (3.3f), 277 Color.white); 278 assertTrue(item1.equals(item2)); 279 280 } 281 282 285 public void testSerialization() { 286 LegendItem item1 = new LegendItem("Item", "Description", 287 "ToolTip", "URL", 288 new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), Color.red); 289 LegendItem item2 = null; 290 try { 291 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 292 ObjectOutput out = new ObjectOutputStream (buffer); 293 out.writeObject(item1); 294 out.close(); 295 296 ObjectInput in = new ObjectInputStream ( 297 new ByteArrayInputStream (buffer.toByteArray()) 298 ); 299 item2 = (LegendItem) in.readObject(); 300 in.close(); 301 } 302 catch (Exception e) { 303 System.out.println(e.toString()); 304 } 305 assertEquals(item1, item2); 306 } 307 308 311 public void testSerialization2() { 312 AttributedString as = new AttributedString ("Test String"); 313 as.addAttribute(TextAttribute.FONT, new Font ("Dialog", Font.PLAIN, 12)); 314 LegendItem item1 = new LegendItem(as, "Description", "ToolTip", "URL", 315 new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), Color.red); 316 LegendItem item2 = null; 317 try { 318 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 319 ObjectOutput out = new ObjectOutputStream (buffer); 320 out.writeObject(item1); 321 out.close(); 322 323 ObjectInput in = new ObjectInputStream ( 324 new ByteArrayInputStream (buffer.toByteArray()) 325 ); 326 item2 = (LegendItem) in.readObject(); 327 in.close(); 328 } 329 catch (Exception e) { 330 System.out.println(e.toString()); 331 } 332 assertEquals(item1, item2); 333 } 334 335 339 public void testCloning() { 340 LegendItem item = new LegendItem("Item", "Description", 341 "ToolTip", "URL", 342 new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0), Color.red); 343 assertFalse(item instanceof Cloneable ); 344 } 345 346 } 347 | Popular Tags |