1 42 43 package org.jfree.chart.renderer.junit; 44 45 import java.awt.BasicStroke ; 46 import java.awt.Color ; 47 import java.awt.Font ; 48 import java.awt.Shape ; 49 import java.awt.Stroke ; 50 import java.awt.geom.Rectangle2D ; 51 import java.io.ByteArrayInputStream ; 52 import java.io.ByteArrayOutputStream ; 53 import java.io.ObjectInput ; 54 import java.io.ObjectInputStream ; 55 import java.io.ObjectOutput ; 56 import java.io.ObjectOutputStream ; 57 58 import junit.framework.Test; 59 import junit.framework.TestCase; 60 import junit.framework.TestSuite; 61 62 import org.jfree.chart.event.RendererChangeEvent; 63 import org.jfree.chart.labels.ItemLabelAnchor; 64 import org.jfree.chart.labels.ItemLabelPosition; 65 import org.jfree.chart.renderer.AbstractRenderer; 66 import org.jfree.chart.renderer.category.BarRenderer; 67 import org.jfree.ui.TextAnchor; 68 69 72 public class AbstractRendererTests extends TestCase { 73 74 79 public static Test suite() { 80 return new TestSuite(AbstractRendererTests.class); 81 } 82 83 88 public AbstractRendererTests(String name) { 89 super(name); 90 } 91 92 95 public void testSetSeriesVisible() { 96 BarRenderer r = new BarRenderer(); 97 r.setSeriesVisible(Boolean.TRUE); 98 assertTrue(r.getItemVisible(0, 0)); 99 } 100 101 104 public void testSetPaint() { 105 BarRenderer r = new BarRenderer(); 106 r.setPaint(Color.orange); 107 assertEquals(Color.orange, r.getItemPaint(0, 0)); 108 } 109 110 113 public void testSetOutlinePaint() { 114 BarRenderer r = new BarRenderer(); 115 r.setOutlinePaint(Color.orange); 116 assertEquals(Color.orange, r.getItemOutlinePaint(0, 0)); 117 } 118 119 122 public void testSetStroke() { 123 BarRenderer r = new BarRenderer(); 124 Stroke s = new BasicStroke (10.0f); 125 r.setStroke(s); 126 assertEquals(s, r.getItemStroke(0, 0)); 127 } 128 129 132 public void testSetOutlineStroke() { 133 BarRenderer r = new BarRenderer(); 134 Stroke s = new BasicStroke (10.0f); 135 r.setOutlineStroke(s); 136 assertEquals(s, r.getItemOutlineStroke(0, 0)); 137 } 138 139 142 public void testSetShape() { 143 BarRenderer r = new BarRenderer(); 144 Shape s = new Rectangle2D.Double (1.0, 2.0, 3.0, 4.0); 145 r.setShape(s); 146 assertEquals(s, r.getItemShape(0, 0)); 147 } 148 149 153 public void testSetItemLabelsVisible() { 154 BarRenderer r = new BarRenderer(); 155 r.setItemLabelsVisible(true); 156 assertTrue(r.isItemLabelVisible(0, 0)); 157 } 158 159 163 public void testSetItemLabelFont() { 164 BarRenderer r = new BarRenderer(); 165 r.setItemLabelFont(new Font ("SansSerif", Font.PLAIN, 33)); 166 assertEquals( 167 new Font ("SansSerif", Font.PLAIN, 33), r.getItemLabelFont(0, 0) 168 ); 169 } 170 171 175 public void testSetItemLabelPaint() { 176 BarRenderer r = new BarRenderer(); 177 r.setItemLabelPaint(Color.green); 178 assertEquals(Color.green, r.getItemLabelPaint(0, 0)); 179 } 180 181 185 public void testSetPositiveItemLabelPosition() { 186 BarRenderer r = new BarRenderer(); 187 r.setPositiveItemLabelPosition( 188 new ItemLabelPosition( 189 ItemLabelAnchor.INSIDE1, TextAnchor.BASELINE_LEFT 190 ) 191 ); 192 assertEquals( 193 new ItemLabelPosition( 194 ItemLabelAnchor.INSIDE1, TextAnchor.BASELINE_LEFT 195 ), 196 r.getPositiveItemLabelPosition(0, 0) 197 ); 198 } 199 200 204 public void testSetNegativeItemLabelPosition() { 205 BarRenderer r = new BarRenderer(); 206 r.setNegativeItemLabelPosition( 207 new ItemLabelPosition( 208 ItemLabelAnchor.INSIDE1, TextAnchor.BASELINE_LEFT 209 ) 210 ); 211 assertEquals( 212 new ItemLabelPosition( 213 ItemLabelAnchor.INSIDE1, TextAnchor.BASELINE_LEFT 214 ), 215 r.getNegativeItemLabelPosition(0, 0) 216 ); 217 } 218 219 222 public void testEventNotification() { 223 224 RendererChangeDetector detector = new RendererChangeDetector(); 225 BarRenderer r1 = new BarRenderer(); r1.addChangeListener(detector); 228 229 detector.setNotified(false); 231 r1.setPaint(Color.red); 232 assertTrue(detector.getNotified()); 233 234 detector.setNotified(false); 235 r1.setSeriesPaint(0, Color.red); 236 assertTrue(detector.getNotified()); 237 238 detector.setNotified(false); 239 r1.setBasePaint(Color.red); 240 assertTrue(detector.getNotified()); 241 242 detector.setNotified(false); 244 r1.setOutlinePaint(Color.red); 245 assertTrue(detector.getNotified()); 246 247 detector.setNotified(false); 248 r1.setSeriesOutlinePaint(0, Color.red); 249 assertTrue(detector.getNotified()); 250 251 detector.setNotified(false); 252 r1.setBaseOutlinePaint(Color.red); 253 assertTrue(detector.getNotified()); 254 255 detector.setNotified(false); 257 r1.setStroke(new BasicStroke (1.0f)); 258 assertTrue(detector.getNotified()); 259 260 detector.setNotified(false); 261 r1.setSeriesStroke(0, new BasicStroke (1.0f)); 262 assertTrue(detector.getNotified()); 263 264 detector.setNotified(false); 265 r1.setBaseStroke(new BasicStroke (1.0f)); 266 assertTrue(detector.getNotified()); 267 268 detector.setNotified(false); 270 r1.setOutlineStroke(new BasicStroke (1.0f)); 271 assertTrue(detector.getNotified()); 272 273 detector.setNotified(false); 274 r1.setSeriesOutlineStroke(0, new BasicStroke (1.0f)); 275 assertTrue(detector.getNotified()); 276 277 detector.setNotified(false); 278 r1.setBaseOutlineStroke(new BasicStroke (1.0f)); 279 assertTrue(detector.getNotified()); 280 281 detector.setNotified(false); 283 r1.setShape(new Rectangle2D.Float ()); 284 assertTrue(detector.getNotified()); 285 286 detector.setNotified(false); 287 r1.setSeriesShape(0, new Rectangle2D.Float ()); 288 assertTrue(detector.getNotified()); 289 290 detector.setNotified(false); 291 r1.setBaseShape(new Rectangle2D.Float ()); 292 assertTrue(detector.getNotified()); 293 294 detector.setNotified(false); 296 r1.setItemLabelsVisible(Boolean.TRUE); 297 assertTrue(detector.getNotified()); 298 299 detector.setNotified(false); 300 r1.setSeriesItemLabelsVisible(0, Boolean.TRUE); 301 assertTrue(detector.getNotified()); 302 303 detector.setNotified(false); 304 r1.setBaseItemLabelsVisible(Boolean.TRUE); 305 assertTrue(detector.getNotified()); 306 307 detector.setNotified(false); 309 r1.setItemLabelFont(new Font ("Serif", Font.PLAIN, 12)); 310 assertTrue(detector.getNotified()); 311 312 detector.setNotified(false); 313 r1.setSeriesItemLabelFont(0, new Font ("Serif", Font.PLAIN, 12)); 314 assertTrue(detector.getNotified()); 315 316 detector.setNotified(false); 317 r1.setBaseItemLabelFont(new Font ("Serif", Font.PLAIN, 12)); 318 assertTrue(detector.getNotified()); 319 320 detector.setNotified(false); 322 r1.setItemLabelPaint(Color.blue); 323 assertTrue(detector.getNotified()); 324 325 detector.setNotified(false); 326 r1.setSeriesItemLabelPaint(0, Color.blue); 327 assertTrue(detector.getNotified()); 328 329 detector.setNotified(false); 330 r1.setBaseItemLabelPaint(Color.blue); 331 assertTrue(detector.getNotified()); 332 333 detector.setNotified(false); 335 r1.setPositiveItemLabelPosition( 336 new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER) 337 ); 338 assertTrue(detector.getNotified()); 339 340 detector.setNotified(false); 341 r1.setSeriesPositiveItemLabelPosition( 342 0, new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER) 343 ); 344 assertTrue(detector.getNotified()); 345 346 detector.setNotified(false); 347 r1.setBasePositiveItemLabelPosition( 348 new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER) 349 ); 350 assertTrue(detector.getNotified()); 351 352 detector.setNotified(false); 354 r1.setNegativeItemLabelPosition( 355 new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER) 356 ); 357 assertTrue(detector.getNotified()); 358 359 detector.setNotified(false); 360 r1.setSeriesNegativeItemLabelPosition( 361 0, new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER) 362 ); 363 assertTrue(detector.getNotified()); 364 365 detector.setNotified(false); 366 r1.setBaseNegativeItemLabelPosition( 367 new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER) 368 ); 369 assertTrue(detector.getNotified()); 370 371 } 372 373 378 public void testSerialization() { 379 380 BarRenderer r1 = new BarRenderer(); 381 BarRenderer r2 = null; 382 383 try { 384 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 385 ObjectOutput out = new ObjectOutputStream (buffer); 386 out.writeObject(r1); 387 out.close(); 388 389 ObjectInput in = new ObjectInputStream ( 390 new ByteArrayInputStream (buffer.toByteArray()) 391 ); 392 r2 = (BarRenderer) in.readObject(); 393 in.close(); 394 } 395 catch (Exception e) { 396 System.out.println(e.toString()); 397 } 398 assertEquals(r1, r2); 399 try { 400 r2.notifyListeners(new RendererChangeEvent(r2)); 401 } 402 catch (NullPointerException e) { 403 assertTrue(false); } 405 406 } 407 408 } 409 | Popular Tags |