1 43 44 package org.jfree.chart.title.junit; 45 46 import java.awt.Color ; 47 import java.awt.Font ; 48 import java.awt.GradientPaint ; 49 import java.io.ByteArrayInputStream ; 50 import java.io.ByteArrayOutputStream ; 51 import java.io.ObjectInput ; 52 import java.io.ObjectInputStream ; 53 import java.io.ObjectOutput ; 54 import java.io.ObjectOutputStream ; 55 56 import junit.framework.Test; 57 import junit.framework.TestCase; 58 import junit.framework.TestSuite; 59 60 import org.jfree.chart.plot.XYPlot; 61 import org.jfree.chart.title.LegendTitle; 62 import org.jfree.ui.RectangleAnchor; 63 import org.jfree.ui.RectangleEdge; 64 65 68 public class LegendTitleTests extends TestCase { 69 70 75 public static Test suite() { 76 return new TestSuite(LegendTitleTests.class); 77 } 78 79 84 public LegendTitleTests(String name) { 85 super(name); 86 } 87 88 91 public void testEquals() { 92 XYPlot plot1 = new XYPlot(); 93 LegendTitle t1 = new LegendTitle(plot1); 94 LegendTitle t2 = new LegendTitle(plot1); 95 assertEquals(t1, t2); 96 97 t1.setBackgroundPaint( 98 new GradientPaint (1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.yellow) 99 ); 100 assertFalse(t1.equals(t2)); 101 t2.setBackgroundPaint( 102 new GradientPaint (1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.yellow) 103 ); 104 assertTrue(t1.equals(t2)); 105 106 t1.setLegendItemGraphicEdge(RectangleEdge.BOTTOM); 107 assertFalse(t1.equals(t2)); 108 t2.setLegendItemGraphicEdge(RectangleEdge.BOTTOM); 109 assertTrue(t1.equals(t2)); 110 111 t1.setLegendItemGraphicAnchor(RectangleAnchor.BOTTOM_LEFT); 112 assertFalse(t1.equals(t2)); 113 t2.setLegendItemGraphicAnchor(RectangleAnchor.BOTTOM_LEFT); 114 assertTrue(t1.equals(t2)); 115 116 t1.setLegendItemGraphicLocation(RectangleAnchor.TOP_LEFT); 117 assertFalse(t1.equals(t2)); 118 t2.setLegendItemGraphicLocation(RectangleAnchor.TOP_LEFT); 119 assertTrue(t1.equals(t2)); 120 121 t1.setItemFont(new Font ("Dialog", Font.PLAIN, 19)); 122 assertFalse(t1.equals(t2)); 123 t2.setItemFont(new Font ("Dialog", Font.PLAIN, 19)); 124 assertTrue(t1.equals(t2)); 125 } 126 127 130 public void testHashcode() { 131 XYPlot plot1 = new XYPlot(); 132 LegendTitle t1 = new LegendTitle(plot1); 133 LegendTitle t2 = new LegendTitle(plot1); 134 assertTrue(t1.equals(t2)); 135 int h1 = t1.hashCode(); 136 int h2 = t2.hashCode(); 137 assertEquals(h1, h2); 138 } 139 140 143 public void testCloning() { 144 XYPlot plot = new XYPlot(); 145 LegendTitle t1 = new LegendTitle(plot); 146 t1.setBackgroundPaint( 147 new GradientPaint (1.0f, 2.0f, Color.red, 3.0f, 4.0f, Color.yellow) 148 ); 149 LegendTitle t2 = null; 150 try { 151 t2 = (LegendTitle) t1.clone(); 152 } 153 catch (CloneNotSupportedException e) { 154 System.err.println("Failed to clone."); 155 } 156 assertTrue(t1 != t2); 157 assertTrue(t1.getClass() == t2.getClass()); 158 assertTrue(t1.equals(t2)); 159 } 160 161 164 public void testSerialization() { 165 166 XYPlot plot = new XYPlot(); 167 LegendTitle t1 = new LegendTitle(plot); 168 LegendTitle t2 = null; 169 170 try { 171 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 172 ObjectOutput out = new ObjectOutputStream (buffer); 173 out.writeObject(t1); 174 out.close(); 175 176 ObjectInput in = new ObjectInputStream ( 177 new ByteArrayInputStream (buffer.toByteArray()) 178 ); 179 t2 = (LegendTitle) in.readObject(); 180 in.close(); 181 } 182 catch (Exception e) { 183 fail(e.toString()); 184 } 185 assertTrue(t1.equals(t2)); 186 assertTrue(t2.getSources()[0].equals(plot)); 187 } 188 189 } 190 | Popular Tags |