1 42 43 package org.jfree.chart.plot.junit; 44 45 import java.awt.BasicStroke ; 46 import java.awt.Color ; 47 import java.awt.GradientPaint ; 48 import java.io.ByteArrayInputStream ; 49 import java.io.ByteArrayOutputStream ; 50 import java.io.ObjectInput ; 51 import java.io.ObjectInputStream ; 52 import java.io.ObjectOutput ; 53 import java.io.ObjectOutputStream ; 54 55 import junit.framework.Test; 56 import junit.framework.TestCase; 57 import junit.framework.TestSuite; 58 59 import org.jfree.chart.event.MarkerChangeEvent; 60 import org.jfree.chart.event.MarkerChangeListener; 61 import org.jfree.chart.plot.CategoryMarker; 62 63 66 public class CategoryMarkerTests 67 extends TestCase 68 implements MarkerChangeListener { 69 70 MarkerChangeEvent lastEvent; 71 72 public void markerChanged(MarkerChangeEvent event) { 73 this.lastEvent = event; 74 } 75 76 81 public static Test suite() { 82 return new TestSuite(CategoryMarkerTests.class); 83 } 84 85 90 public CategoryMarkerTests(String name) { 91 super(name); 92 } 93 94 97 public void testEquals() { 98 CategoryMarker m1 = new CategoryMarker("A"); 99 CategoryMarker m2 = new CategoryMarker("A"); 100 assertTrue(m1.equals(m2)); 101 assertTrue(m2.equals(m1)); 102 103 m1 = new CategoryMarker("B"); 105 assertFalse(m1.equals(m2)); 106 m2 = new CategoryMarker("B"); 107 assertTrue(m1.equals(m2)); 108 109 m1 = new CategoryMarker("A", new GradientPaint (1.0f, 2.0f, Color.white, 111 3.0f, 4.0f, Color.yellow), new BasicStroke (1.1f)); 112 assertFalse(m1.equals(m2)); 113 m2 = new CategoryMarker("A", new GradientPaint (1.0f, 2.0f, Color.white, 114 3.0f, 4.0f, Color.yellow), new BasicStroke (1.1f)); 115 assertTrue(m1.equals(m2)); 116 117 m1 = new CategoryMarker("A", new GradientPaint (1.0f, 2.0f, Color.white, 119 3.0f, 4.0f, Color.yellow), new BasicStroke (2.2f)); 120 assertFalse(m1.equals(m2)); 121 m2 = new CategoryMarker("A", new GradientPaint (1.0f, 2.0f, Color.white, 122 3.0f, 4.0f, Color.yellow), new BasicStroke (2.2f)); 123 assertTrue(m1.equals(m2)); 124 125 m1 = new CategoryMarker("A", new GradientPaint (1.0f, 2.0f, Color.white, 127 3.0f, 4.0f, Color.yellow), new BasicStroke (2.2f), Color.red, 128 new BasicStroke (1.0f), 1.0f); 129 assertFalse(m1.equals(m2)); 130 m2 = new CategoryMarker("A", new GradientPaint (1.0f, 2.0f, Color.white, 131 3.0f, 4.0f, Color.yellow), new BasicStroke (2.2f), Color.red, 132 new BasicStroke (1.0f), 1.0f); 133 assertTrue(m1.equals(m2)); 134 135 m1 = new CategoryMarker("A", new GradientPaint (1.0f, 2.0f, Color.white, 137 3.0f, 4.0f, Color.yellow), new BasicStroke (2.2f), Color.red, 138 new BasicStroke (3.3f), 1.0f); 139 assertFalse(m1.equals(m2)); 140 m2 = new CategoryMarker("A", new GradientPaint (1.0f, 2.0f, Color.white, 141 3.0f, 4.0f, Color.yellow), new BasicStroke (2.2f), Color.red, 142 new BasicStroke (3.3f), 1.0f); 143 assertTrue(m1.equals(m2)); 144 145 m1 = new CategoryMarker("A", new GradientPaint (1.0f, 2.0f, Color.white, 147 3.0f, 4.0f, Color.yellow), new BasicStroke (2.2f), Color.red, 148 new BasicStroke (1.0f), 0.5f); 149 assertFalse(m1.equals(m2)); 150 m2 = new CategoryMarker("A", new GradientPaint (1.0f, 2.0f, Color.white, 151 3.0f, 4.0f, Color.yellow), new BasicStroke (2.2f), Color.red, 152 new BasicStroke (1.0f), 0.5f); 153 assertTrue(m1.equals(m2)); 154 155 } 156 157 160 public void testCloning() { 161 CategoryMarker m1 = new CategoryMarker("A", new GradientPaint (1.0f, 162 2.0f, Color.white, 3.0f, 4.0f, Color.yellow), 163 new BasicStroke (1.1f)); 164 CategoryMarker m2 = null; 165 try { 166 m2 = (CategoryMarker) m1.clone(); 167 } 168 catch (CloneNotSupportedException e) { 169 e.printStackTrace(); 170 } 171 assertTrue(m1 != m2); 172 assertTrue(m1.getClass() == m2.getClass()); 173 assertTrue(m1.equals(m2)); 174 } 175 176 179 public void testSerialization() { 180 181 CategoryMarker m1 = new CategoryMarker("A", new GradientPaint (1.0f, 182 2.0f, Color.white, 3.0f, 4.0f, Color.yellow), 183 new BasicStroke (1.1f)); 184 CategoryMarker m2 = null; 185 try { 186 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 187 ObjectOutput out = new ObjectOutputStream (buffer); 188 out.writeObject(m1); 189 out.close(); 190 191 ObjectInput in = new ObjectInputStream ( 192 new ByteArrayInputStream (buffer.toByteArray())); 193 m2 = (CategoryMarker) in.readObject(); 194 in.close(); 195 } 196 catch (Exception e) { 197 fail(e.toString()); 198 } 199 boolean b = m1.equals(m2); 200 assertTrue(b); 201 202 } 203 204 207 public void testGetSetKey() { 208 CategoryMarker m = new CategoryMarker("X"); 209 m.addChangeListener(this); 210 this.lastEvent = null; 211 assertEquals("X", m.getKey()); 212 m.setKey("Y"); 213 assertEquals("Y", m.getKey()); 214 assertEquals(m, this.lastEvent.getMarker()); 215 216 try { 218 m.setKey(null); 219 fail("Expected an IllegalArgumentException for null."); 220 } 221 catch (IllegalArgumentException e) { 222 assertTrue(true); 223 } 224 } 225 226 229 public void testGetSetDrawAsLine() { 230 CategoryMarker m = new CategoryMarker("X"); 231 m.addChangeListener(this); 232 this.lastEvent = null; 233 assertEquals(false, m.getDrawAsLine()); 234 m.setDrawAsLine(true); 235 assertEquals(true, m.getDrawAsLine()); 236 assertEquals(m, this.lastEvent.getMarker()); 237 } 238 } 239 | Popular Tags |