1 45 46 package org.jfree.chart.plot.junit; 47 48 import java.awt.BasicStroke ; 49 import java.awt.Color ; 50 import java.awt.Font ; 51 import java.awt.GradientPaint ; 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 59 import junit.framework.Test; 60 import junit.framework.TestCase; 61 import junit.framework.TestSuite; 62 63 import org.jfree.chart.event.MarkerChangeEvent; 64 import org.jfree.chart.event.MarkerChangeListener; 65 import org.jfree.chart.plot.Marker; 66 import org.jfree.chart.plot.ValueMarker; 67 import org.jfree.ui.LengthAdjustmentType; 68 import org.jfree.ui.RectangleAnchor; 69 import org.jfree.ui.RectangleInsets; 70 import org.jfree.ui.TextAnchor; 71 72 75 public class ValueMarkerTests 76 extends TestCase 77 implements MarkerChangeListener { 78 79 80 MarkerChangeEvent lastEvent; 81 82 87 public static Test suite() { 88 return new TestSuite(ValueMarkerTests.class); 89 } 90 91 96 public ValueMarkerTests(String name) { 97 super(name); 98 } 99 100 103 public void testEquals() { 104 105 Marker m1 = new ValueMarker(45.0); 106 Marker m2 = new ValueMarker(45.0); 107 assertTrue(m1.equals(m2)); 108 assertTrue(m2.equals(m1)); 109 110 m1.setPaint(new GradientPaint (1.0f, 2.0f, Color.green, 111 3.0f, 4.0f, Color.red)); 112 assertFalse(m1.equals(m2)); 113 m2.setPaint(new GradientPaint (1.0f, 2.0f, Color.green, 114 3.0f, 4.0f, Color.red)); 115 assertTrue(m1.equals(m2)); 116 117 BasicStroke stroke = new BasicStroke (2.2f); 118 m1.setStroke(stroke); 119 assertFalse(m1.equals(m2)); 120 m2.setStroke(stroke); 121 assertTrue(m1.equals(m2)); 122 123 m1.setOutlinePaint(new GradientPaint (4.0f, 3.0f, Color.yellow, 124 2.0f, 1.0f, Color.white)); 125 assertFalse(m1.equals(m2)); 126 m2.setOutlinePaint(new GradientPaint (4.0f, 3.0f, Color.yellow, 127 2.0f, 1.0f, Color.white)); 128 assertTrue(m1.equals(m2)); 129 130 m1.setOutlineStroke(stroke); 131 assertFalse(m1.equals(m2)); 132 m2.setOutlineStroke(stroke); 133 assertTrue(m1.equals(m2)); 134 135 m1.setAlpha(0.1f); 136 assertFalse(m1.equals(m2)); 137 m2.setAlpha(0.1f); 138 assertTrue(m1.equals(m2)); 139 140 m1.setLabel("New Label"); 141 assertFalse(m1.equals(m2)); 142 m2.setLabel("New Label"); 143 assertTrue(m1.equals(m2)); 144 145 m1.setLabelFont(new Font ("SansSerif", Font.PLAIN, 10)); 146 assertFalse(m1.equals(m2)); 147 m2.setLabelFont(new Font ("SansSerif", Font.PLAIN, 10)); 148 assertTrue(m1.equals(m2)); 149 150 m1.setLabelPaint(new GradientPaint (1.0f, 2.0f, Color.blue, 151 3.0f, 4.0f, Color.yellow)); 152 assertFalse(m1.equals(m2)); 153 m2.setLabelPaint(new GradientPaint (1.0f, 2.0f, Color.blue, 154 3.0f, 4.0f, Color.yellow)); 155 assertTrue(m1.equals(m2)); 156 157 m1.setLabelAnchor(RectangleAnchor.TOP_RIGHT); 158 assertFalse(m1.equals(m2)); 159 m2.setLabelAnchor(RectangleAnchor.TOP_RIGHT); 160 assertTrue(m1.equals(m2)); 161 162 m1.setLabelTextAnchor(TextAnchor.BASELINE_RIGHT); 163 assertFalse(m1.equals(m2)); 164 m2.setLabelTextAnchor(TextAnchor.BASELINE_RIGHT); 165 assertTrue(m1.equals(m2)); 166 167 m1.setLabelOffset(new RectangleInsets(10.0, 10.0, 10.0, 10.0)); 168 assertFalse(m1.equals(m2)); 169 m2.setLabelOffset(new RectangleInsets(10.0, 10.0, 10.0, 10.0)); 170 assertTrue(m1.equals(m2)); 171 172 m1.setLabelOffsetType(LengthAdjustmentType.EXPAND); 173 assertFalse(m1.equals(m2)); 174 m2.setLabelOffsetType(LengthAdjustmentType.EXPAND); 175 assertTrue(m1.equals(m2)); 176 177 m1 = new ValueMarker(12.3); 178 m2 = new ValueMarker(45.6); 179 assertFalse(m1.equals(m2)); 180 m2 = new ValueMarker(12.3); 181 assertTrue(m1.equals(m2)); 182 183 } 184 185 188 public void testCloning() { 189 ValueMarker m1 = new ValueMarker(25.0); 190 ValueMarker m2 = null; 191 try { 192 m2 = (ValueMarker) m1.clone(); 193 } 194 catch (CloneNotSupportedException e) { 195 e.printStackTrace(); 196 } 197 assertTrue(m1 != m2); 198 assertTrue(m1.getClass() == m2.getClass()); 199 assertTrue(m1.equals(m2)); 200 } 201 202 205 public void testSerialization() { 206 207 ValueMarker m1 = new ValueMarker(25.0); 208 ValueMarker m2 = null; 209 210 try { 211 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 212 ObjectOutput out = new ObjectOutputStream (buffer); 213 out.writeObject(m1); 214 out.close(); 215 216 ObjectInput in = new ObjectInputStream ( 217 new ByteArrayInputStream (buffer.toByteArray())); 218 m2 = (ValueMarker) in.readObject(); 219 in.close(); 220 } 221 catch (Exception e) { 222 e.printStackTrace(); 223 } 224 boolean b = m1.equals(m2); 225 assertTrue(b); 226 227 } 228 229 private static final double EPSILON = 0.000000001; 230 231 234 public void testGetSetValue() { 235 ValueMarker m = new ValueMarker(1.1); 236 m.addChangeListener(this); 237 this.lastEvent = null; 238 assertEquals(1.1, m.getValue(), EPSILON); 239 m.setValue(33.3); 240 assertEquals(33.3, m.getValue(), EPSILON); 241 assertEquals(m, this.lastEvent.getMarker()); 242 } 243 244 public void markerChanged(MarkerChangeEvent event) { 245 this.lastEvent = event; 246 } 247 248 } 249 | Popular Tags |