1 42 43 package org.jfree.chart.plot.junit; 44 45 import java.awt.BasicStroke ; 46 import java.awt.Color ; 47 import java.awt.Font ; 48 import java.util.Arrays ; 49 import java.util.EventListener ; 50 51 import junit.framework.Test; 52 import junit.framework.TestCase; 53 import junit.framework.TestSuite; 54 55 import org.jfree.chart.event.MarkerChangeEvent; 56 import org.jfree.chart.event.MarkerChangeListener; 57 import org.jfree.chart.plot.CategoryMarker; 58 import org.jfree.chart.plot.CategoryPlot; 59 import org.jfree.chart.plot.Marker; 60 import org.jfree.chart.plot.ValueMarker; 61 import org.jfree.chart.plot.XYPlot; 62 import org.jfree.ui.LengthAdjustmentType; 63 import org.jfree.ui.RectangleAnchor; 64 import org.jfree.ui.RectangleInsets; 65 import org.jfree.ui.TextAnchor; 66 67 70 public class MarkerTests extends TestCase implements MarkerChangeListener { 71 72 MarkerChangeEvent lastEvent; 73 74 79 public static Test suite() { 80 return new TestSuite(MarkerTests.class); 81 } 82 83 88 public MarkerTests(String name) { 89 super(name); 90 } 91 92 95 public void testGetSetPaint() { 96 ValueMarker m = new ValueMarker(1.1); 99 m.addChangeListener(this); 100 this.lastEvent = null; 101 assertEquals(Color.gray, m.getPaint()); 102 m.setPaint(Color.blue); 103 assertEquals(Color.blue, m.getPaint()); 104 assertEquals(m, this.lastEvent.getMarker()); 105 106 try { 108 m.setPaint(null); 109 fail("Expected an IllegalArgumentException for null."); 110 } 111 catch (IllegalArgumentException e) { 112 assertTrue(true); 113 } 114 } 115 116 119 public void testGetSetStroke() { 120 ValueMarker m = new ValueMarker(1.1); 123 m.addChangeListener(this); 124 this.lastEvent = null; 125 assertEquals(new BasicStroke (0.5f), m.getStroke()); 126 m.setStroke(new BasicStroke (1.1f)); 127 assertEquals(new BasicStroke (1.1f), m.getStroke()); 128 assertEquals(m, this.lastEvent.getMarker()); 129 130 try { 132 m.setStroke(null); 133 fail("Expected an IllegalArgumentException for null."); 134 } 135 catch (IllegalArgumentException e) { 136 assertTrue(true); 137 } 138 } 139 140 143 public void testGetSetOutlinePaint() { 144 ValueMarker m = new ValueMarker(1.1); 147 m.addChangeListener(this); 148 this.lastEvent = null; 149 assertEquals(Color.gray, m.getOutlinePaint()); 150 m.setOutlinePaint(Color.yellow); 151 assertEquals(Color.yellow, m.getOutlinePaint()); 152 assertEquals(m, this.lastEvent.getMarker()); 153 154 m.setOutlinePaint(null); 156 assertEquals(null, m.getOutlinePaint()); 157 } 158 159 162 public void testGetSetOutlineStroke() { 163 ValueMarker m = new ValueMarker(1.1); 166 m.addChangeListener(this); 167 this.lastEvent = null; 168 assertEquals(new BasicStroke (0.5f), m.getOutlineStroke()); 169 m.setOutlineStroke(new BasicStroke (1.1f)); 170 assertEquals(new BasicStroke (1.1f), m.getOutlineStroke()); 171 assertEquals(m, this.lastEvent.getMarker()); 172 173 m.setOutlineStroke(null); 175 assertEquals(null, m.getOutlineStroke()); 176 } 177 178 private static final float EPSILON = 0.000000001f; 179 180 183 public void testGetSetAlpha() { 184 ValueMarker m = new ValueMarker(1.1); 187 m.addChangeListener(this); 188 this.lastEvent = null; 189 assertEquals(0.8f, m.getAlpha(), EPSILON); 190 m.setAlpha(0.5f); 191 assertEquals(0.5f, m.getAlpha(), EPSILON); 192 assertEquals(m, this.lastEvent.getMarker()); 193 } 194 195 198 public void testGetSetLabel() { 199 ValueMarker m = new ValueMarker(1.1); 202 m.addChangeListener(this); 203 this.lastEvent = null; 204 assertEquals(null, m.getLabel()); 205 m.setLabel("XYZ"); 206 assertEquals("XYZ", m.getLabel()); 207 assertEquals(m, this.lastEvent.getMarker()); 208 209 m.setLabel(null); 211 assertEquals(null, m.getLabel()); 212 } 213 214 217 public void testGetSetLabelFont() { 218 ValueMarker m = new ValueMarker(1.1); 221 m.addChangeListener(this); 222 this.lastEvent = null; 223 assertEquals(new Font ("SansSerif", Font.PLAIN, 9), m.getLabelFont()); 224 m.setLabelFont(new Font ("SansSerif", Font.BOLD, 10)); 225 assertEquals(new Font ("SansSerif", Font.BOLD, 10), m.getLabelFont()); 226 assertEquals(m, this.lastEvent.getMarker()); 227 228 try { 230 m.setLabelFont(null); 231 fail("Expected an IllegalArgumentException for null."); 232 } 233 catch (IllegalArgumentException e) { 234 assertTrue(true); 235 } 236 } 237 238 241 public void testGetSetLabelPaint() { 242 ValueMarker m = new ValueMarker(1.1); 245 m.addChangeListener(this); 246 this.lastEvent = null; 247 assertEquals(Color.black, m.getLabelPaint()); 248 m.setLabelPaint(Color.red); 249 assertEquals(Color.red, m.getLabelPaint()); 250 assertEquals(m, this.lastEvent.getMarker()); 251 252 try { 254 m.setLabelPaint(null); 255 fail("Expected an IllegalArgumentException for null."); 256 } 257 catch (IllegalArgumentException e) { 258 assertTrue(true); 259 } 260 } 261 262 265 public void testGetSetLabelAnchor() { 266 ValueMarker m = new ValueMarker(1.1); 269 m.addChangeListener(this); 270 this.lastEvent = null; 271 assertEquals(RectangleAnchor.TOP_LEFT, m.getLabelAnchor()); 272 m.setLabelAnchor(RectangleAnchor.TOP); 273 assertEquals(RectangleAnchor.TOP, m.getLabelAnchor()); 274 assertEquals(m, this.lastEvent.getMarker()); 275 276 try { 278 m.setLabelAnchor(null); 279 fail("Expected an IllegalArgumentException for null."); 280 } 281 catch (IllegalArgumentException e) { 282 assertTrue(true); 283 } 284 } 285 286 289 public void testGetSetLabelOffset() { 290 ValueMarker m = new ValueMarker(1.1); 293 m.addChangeListener(this); 294 this.lastEvent = null; 295 assertEquals(new RectangleInsets(3, 3, 3, 3), m.getLabelOffset()); 296 m.setLabelOffset(new RectangleInsets(1, 2, 3, 4)); 297 assertEquals(new RectangleInsets(1, 2, 3, 4), m.getLabelOffset()); 298 assertEquals(m, this.lastEvent.getMarker()); 299 300 try { 302 m.setLabelOffset(null); 303 fail("Expected an IllegalArgumentException for null."); 304 } 305 catch (IllegalArgumentException e) { 306 assertTrue(true); 307 } 308 } 309 310 314 public void testGetSetLabelOffsetType() { 315 ValueMarker m = new ValueMarker(1.1); 318 m.addChangeListener(this); 319 this.lastEvent = null; 320 assertEquals(LengthAdjustmentType.CONTRACT, m.getLabelOffsetType()); 321 m.setLabelOffsetType(LengthAdjustmentType.EXPAND); 322 assertEquals(LengthAdjustmentType.EXPAND, m.getLabelOffsetType()); 323 assertEquals(m, this.lastEvent.getMarker()); 324 325 try { 327 m.setLabelOffsetType(null); 328 fail("Expected an IllegalArgumentException for null."); 329 } 330 catch (IllegalArgumentException e) { 331 assertTrue(true); 332 } 333 } 334 335 339 public void testGetSetLabelTextAnchor() { 340 ValueMarker m = new ValueMarker(1.1); 343 m.addChangeListener(this); 344 this.lastEvent = null; 345 assertEquals(TextAnchor.CENTER, m.getLabelTextAnchor()); 346 m.setLabelTextAnchor(TextAnchor.BASELINE_LEFT); 347 assertEquals(TextAnchor.BASELINE_LEFT, m.getLabelTextAnchor()); 348 assertEquals(m, this.lastEvent.getMarker()); 349 350 try { 352 m.setLabelTextAnchor(null); 353 fail("Expected an IllegalArgumentException for null."); 354 } 355 catch (IllegalArgumentException e) { 356 assertTrue(true); 357 } 358 } 359 360 363 public void testListenersWithCategoryPlot() { 364 CategoryPlot plot = new CategoryPlot(); 365 CategoryMarker marker1 = new CategoryMarker("X"); 366 ValueMarker marker2 = new ValueMarker(1.0); 367 plot.addDomainMarker(marker1); 368 plot.addRangeMarker(marker2); 369 EventListener [] listeners1 = marker1.getListeners( 370 MarkerChangeListener.class); 371 assertTrue(Arrays.asList(listeners1).contains(plot)); 372 EventListener [] listeners2 = marker1.getListeners( 373 MarkerChangeListener.class); 374 assertTrue(Arrays.asList(listeners2).contains(plot)); 375 plot.clearDomainMarkers(); 376 plot.clearRangeMarkers(); 377 listeners1 = marker1.getListeners(MarkerChangeListener.class); 378 assertFalse(Arrays.asList(listeners1).contains(plot)); 379 listeners2 = marker1.getListeners(MarkerChangeListener.class); 380 assertFalse(Arrays.asList(listeners2).contains(plot)); 381 } 382 383 386 public void testListenersWithXYPlot() { 387 XYPlot plot = new XYPlot(); 388 ValueMarker marker1 = new ValueMarker(1.0); 389 ValueMarker marker2 = new ValueMarker(2.0); 390 plot.addDomainMarker(marker1); 391 plot.addRangeMarker(marker2); 392 EventListener [] listeners1 = marker1.getListeners( 393 MarkerChangeListener.class); 394 assertTrue(Arrays.asList(listeners1).contains(plot)); 395 EventListener [] listeners2 = marker1.getListeners( 396 MarkerChangeListener.class); 397 assertTrue(Arrays.asList(listeners2).contains(plot)); 398 plot.clearDomainMarkers(); 399 plot.clearRangeMarkers(); 400 listeners1 = marker1.getListeners(MarkerChangeListener.class); 401 assertFalse(Arrays.asList(listeners1).contains(plot)); 402 listeners2 = marker1.getListeners(MarkerChangeListener.class); 403 assertFalse(Arrays.asList(listeners2).contains(plot)); 404 } 405 406 public void markerChanged(MarkerChangeEvent event) { 407 this.lastEvent = event; 408 } 409 410 } 411 | Popular Tags |