1 43 44 package org.jfree.chart.axis.junit; 45 46 import java.awt.BasicStroke ; 47 import java.awt.Color ; 48 import java.awt.Font ; 49 import java.awt.Paint ; 50 import java.awt.Stroke ; 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 import java.text.DateFormat ; 58 import java.text.SimpleDateFormat ; 59 60 import junit.framework.Test; 61 import junit.framework.TestCase; 62 import junit.framework.TestSuite; 63 64 import org.jfree.chart.axis.PeriodAxisLabelInfo; 65 import org.jfree.data.time.Day; 66 import org.jfree.data.time.Month; 67 import org.jfree.ui.RectangleInsets; 68 69 72 public class PeriodAxisLabelInfoTests extends TestCase { 73 74 79 public static Test suite() { 80 return new TestSuite(PeriodAxisLabelInfoTests.class); 81 } 82 83 88 public PeriodAxisLabelInfoTests(String name) { 89 super(name); 90 } 91 92 95 public void testEquals() { 96 PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo( 97 Day.class, new SimpleDateFormat ("d") 98 ); 99 PeriodAxisLabelInfo info2 = new PeriodAxisLabelInfo( 100 Day.class, new SimpleDateFormat ("d") 101 ); 102 assertTrue(info1.equals(info2)); 103 assertTrue(info2.equals(info1)); 104 105 Class c1 = Day.class; 106 Class c2 = Month.class; 107 DateFormat df1 = new SimpleDateFormat ("d"); 108 DateFormat df2 = new SimpleDateFormat ("MMM"); 109 RectangleInsets sp1 = new RectangleInsets(1, 1, 1, 1); 110 RectangleInsets sp2 = new RectangleInsets(2, 2, 2, 2); 111 Font lf1 = new Font ("SansSerif", Font.PLAIN, 10); 112 Font lf2 = new Font ("SansSerif", Font.BOLD, 9); 113 Paint lp1 = Color.black; 114 Paint lp2 = Color.blue; 115 boolean b1 = true; 116 boolean b2 = false; 117 Stroke s1 = new BasicStroke (0.5f); 118 Stroke s2 = new BasicStroke (0.25f); 119 Paint dp1 = Color.red; 120 Paint dp2 = Color.green; 121 122 info1 = new PeriodAxisLabelInfo(c2, df1, sp1, lf1, lp1, b1, s1, dp1); 123 info2 = new PeriodAxisLabelInfo(c1, df1, sp1, lf1, lp1, b1, s1, dp1); 124 assertFalse(info1.equals(info2)); 125 info2 = new PeriodAxisLabelInfo(c2, df1, sp1, lf1, lp1, b1, s1, dp1); 126 assertTrue(info1.equals(info2)); 127 128 info1 = new PeriodAxisLabelInfo(c2, df2, sp1, lf1, lp1, b1, s1, dp1); 129 assertFalse(info1.equals(info2)); 130 info2 = new PeriodAxisLabelInfo(c2, df2, sp1, lf1, lp1, b1, s1, dp1); 131 assertTrue(info1.equals(info2)); 132 133 info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf1, lp1, b1, s1, dp1); 134 assertFalse(info1.equals(info2)); 135 info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf1, lp1, b1, s1, dp1); 136 assertTrue(info1.equals(info2)); 137 138 info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp1, b1, s1, dp1); 139 assertFalse(info1.equals(info2)); 140 info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp1, b1, s1, dp1); 141 assertTrue(info1.equals(info2)); 142 143 info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b1, s1, dp1); 144 assertFalse(info1.equals(info2)); 145 info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b1, s1, dp1); 146 assertTrue(info1.equals(info2)); 147 148 info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s1, dp1); 149 assertFalse(info1.equals(info2)); 150 info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s1, dp1); 151 assertTrue(info1.equals(info2)); 152 153 info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp1); 154 assertFalse(info1.equals(info2)); 155 info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp1); 156 assertTrue(info1.equals(info2)); 157 158 info1 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp2); 159 assertFalse(info1.equals(info2)); 160 info2 = new PeriodAxisLabelInfo(c2, df2, sp2, lf2, lp2, b2, s2, dp2); 161 assertTrue(info1.equals(info2)); 162 163 } 164 165 168 public void testHashCode() { 169 PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo( 170 Day.class, new SimpleDateFormat ("d") 171 ); 172 PeriodAxisLabelInfo info2 = new PeriodAxisLabelInfo( 173 Day.class, new SimpleDateFormat ("d") 174 ); 175 assertTrue(info1.equals(info2)); 176 int h1 = info1.hashCode(); 177 int h2 = info2.hashCode(); 178 assertEquals(h1, h2); 179 } 180 181 184 public void testCloning() { 185 PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo( 186 Day.class, new SimpleDateFormat ("d") 187 ); 188 PeriodAxisLabelInfo info2 = null; 189 try { 190 info2 = (PeriodAxisLabelInfo) info1.clone(); 191 } 192 catch (CloneNotSupportedException e) { 193 System.err.println("Failed to clone."); 194 } 195 assertTrue(info1 != info2); 196 assertTrue(info1.getClass() == info2.getClass()); 197 assertTrue(info1.equals(info2)); 198 } 199 200 203 public void testSerialization() { 204 PeriodAxisLabelInfo info1 = new PeriodAxisLabelInfo( 205 Day.class, new SimpleDateFormat ("d") 206 ); 207 PeriodAxisLabelInfo info2 = null; 208 try { 209 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 210 ObjectOutput out = new ObjectOutputStream (buffer); 211 out.writeObject(info1); 212 out.close(); 213 214 ObjectInput in = new ObjectInputStream ( 215 new ByteArrayInputStream (buffer.toByteArray()) 216 ); 217 info2 = (PeriodAxisLabelInfo) in.readObject(); 218 in.close(); 219 } 220 catch (Exception e) { 221 System.out.println(e.toString()); 222 } 223 boolean b = info1.equals(info2); 224 assertTrue(b); 225 } 226 227 } 228 | Popular Tags |