1 42 43 package org.jfree.chart.labels.junit; 44 45 import java.io.ByteArrayInputStream ; 46 import java.io.ByteArrayOutputStream ; 47 import java.io.ObjectInput ; 48 import java.io.ObjectInputStream ; 49 import java.io.ObjectOutput ; 50 import java.io.ObjectOutputStream ; 51 import java.text.DateFormat ; 52 import java.text.DecimalFormat ; 53 import java.text.NumberFormat ; 54 import java.text.SimpleDateFormat ; 55 56 import junit.framework.Test; 57 import junit.framework.TestCase; 58 import junit.framework.TestSuite; 59 60 import org.jfree.chart.labels.StandardXYItemLabelGenerator; 61 62 65 public class StandardXYLabelGeneratorTests extends TestCase { 66 67 72 public static Test suite() { 73 return new TestSuite(StandardXYLabelGeneratorTests.class); 74 } 75 76 81 public StandardXYLabelGeneratorTests(String name) { 82 super(name); 83 } 84 85 88 public void testEquals() { 89 90 String f1 = "{1}"; 92 String f2 = "{2}"; 93 NumberFormat xnf1 = new DecimalFormat ("0.00"); 94 NumberFormat xnf2 = new DecimalFormat ("0.000"); 95 NumberFormat ynf1 = new DecimalFormat ("0.00"); 96 NumberFormat ynf2 = new DecimalFormat ("0.000"); 97 98 StandardXYItemLabelGenerator g1 = null; 99 StandardXYItemLabelGenerator g2 = null; 100 101 g1 = new StandardXYItemLabelGenerator(f1, xnf1, ynf1); 102 g2 = new StandardXYItemLabelGenerator(f1, xnf1, ynf1); 103 assertTrue(g1.equals(g2)); 104 assertTrue(g2.equals(g1)); 105 106 g1 = new StandardXYItemLabelGenerator(f2, xnf1, ynf1); 107 assertFalse(g1.equals(g2)); 108 g2 = new StandardXYItemLabelGenerator(f2, xnf1, ynf1); 109 assertTrue(g1.equals(g2)); 110 111 g1 = new StandardXYItemLabelGenerator(f2, xnf2, ynf1); 112 assertFalse(g1.equals(g2)); 113 g2 = new StandardXYItemLabelGenerator(f2, xnf2, ynf1); 114 assertTrue(g1.equals(g2)); 115 116 g1 = new StandardXYItemLabelGenerator(f2, xnf2, ynf2); 117 assertFalse(g1.equals(g2)); 118 g2 = new StandardXYItemLabelGenerator(f2, xnf2, ynf2); 119 assertTrue(g1.equals(g2)); 120 121 DateFormat xdf1 = new SimpleDateFormat ("d-MMM"); 122 DateFormat xdf2 = new SimpleDateFormat ("d-MMM-yyyy"); 123 DateFormat ydf1 = new SimpleDateFormat ("d-MMM"); 124 DateFormat ydf2 = new SimpleDateFormat ("d-MMM-yyyy"); 125 126 g1 = new StandardXYItemLabelGenerator(f1, xdf1, ydf1); 127 g2 = new StandardXYItemLabelGenerator(f1, xdf1, ydf1); 128 assertTrue(g1.equals(g2)); 129 assertTrue(g2.equals(g1)); 130 131 g1 = new StandardXYItemLabelGenerator(f1, xdf2, ydf1); 132 assertFalse(g1.equals(g2)); 133 g2 = new StandardXYItemLabelGenerator(f1, xdf2, ydf1); 134 assertTrue(g1.equals(g2)); 135 136 g1 = new StandardXYItemLabelGenerator(f1, xdf2, ydf2); 137 assertFalse(g1.equals(g2)); 138 g2 = new StandardXYItemLabelGenerator(f1, xdf2, ydf2); 139 assertTrue(g1.equals(g2)); 140 141 } 142 143 146 public void testCloning() { 147 StandardXYItemLabelGenerator g1 = new StandardXYItemLabelGenerator(); 148 StandardXYItemLabelGenerator g2 = null; 149 try { 150 g2 = (StandardXYItemLabelGenerator) g1.clone(); 151 } 152 catch (CloneNotSupportedException e) { 153 System.err.println("Clone failed."); 154 } 155 assertTrue(g1 != g2); 156 assertTrue(g1.getClass() == g2.getClass()); 157 assertTrue(g1.equals(g2)); 158 } 159 160 163 public void testSerialization() { 164 165 StandardXYItemLabelGenerator g1 = new StandardXYItemLabelGenerator(); 166 StandardXYItemLabelGenerator g2 = null; 167 168 try { 169 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 170 ObjectOutput out = new ObjectOutputStream (buffer); 171 out.writeObject(g1); 172 out.close(); 173 174 ObjectInput in = new ObjectInputStream ( 175 new ByteArrayInputStream (buffer.toByteArray()) 176 ); 177 g2 = (StandardXYItemLabelGenerator) in.readObject(); 178 in.close(); 179 } 180 catch (Exception e) { 181 System.out.println(e.toString()); 182 } 183 assertEquals(g1, g2); 184 185 } 186 187 } 188 | Popular Tags |