1 43 44 package org.jfree.chart.labels.junit; 45 46 import java.io.ByteArrayInputStream ; 47 import java.io.ByteArrayOutputStream ; 48 import java.io.ObjectInput ; 49 import java.io.ObjectInputStream ; 50 import java.io.ObjectOutput ; 51 import java.io.ObjectOutputStream ; 52 import java.text.DateFormat ; 53 import java.text.DecimalFormat ; 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.StandardCategoryItemLabelGenerator; 61 import org.jfree.data.category.DefaultCategoryDataset; 62 63 66 public class StandardCategoryItemLabelGeneratorTests extends TestCase { 67 68 73 public static Test suite() { 74 return new TestSuite(StandardCategoryItemLabelGeneratorTests.class); 75 } 76 77 82 public StandardCategoryItemLabelGeneratorTests(String name) { 83 super(name); 84 } 85 86 public void testGenerateLabel() { 87 StandardCategoryItemLabelGenerator g 88 = new StandardCategoryItemLabelGenerator("{2}", new DecimalFormat ("0.000")); 89 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 90 dataset.addValue(1.0, "R0", "C0"); 91 dataset.addValue(2.0, "R0", "C1"); 92 dataset.addValue(3.0, "R1", "C0"); 93 dataset.addValue(null, "R1", "C1"); 94 String s = g.generateLabel(dataset, 0, 0); 95 assertEquals("1.000", s); 96 97 s = g.generateLabel(dataset, 1, 1); 99 assertEquals("-", s); 100 } 101 102 105 public void testEquals() { 106 107 StandardCategoryItemLabelGenerator g1 108 = new StandardCategoryItemLabelGenerator(); 109 StandardCategoryItemLabelGenerator g2 110 = new StandardCategoryItemLabelGenerator(); 111 assertTrue(g1.equals(g2)); 112 assertTrue(g2.equals(g1)); 113 114 g1 = new StandardCategoryItemLabelGenerator( 115 "{0}", new DecimalFormat ("0.000") 116 ); 117 assertFalse(g1.equals(g2)); 118 g2 = new StandardCategoryItemLabelGenerator( 119 "{0}", new DecimalFormat ("0.000") 120 ); 121 assertTrue(g1.equals(g2)); 122 123 g1 = new StandardCategoryItemLabelGenerator( 124 "{1}", new DecimalFormat ("0.000") 125 ); 126 assertFalse(g1.equals(g2)); 127 g2 = new StandardCategoryItemLabelGenerator( 128 "{1}", new DecimalFormat ("0.000") 129 ); 130 assertTrue(g1.equals(g2)); 131 132 g1 = new StandardCategoryItemLabelGenerator( 133 "{2}", new SimpleDateFormat ("d-MMM") 134 ); 135 assertFalse(g1.equals(g2)); 136 g2 = new StandardCategoryItemLabelGenerator( 137 "{2}", new SimpleDateFormat ("d-MMM") 138 ); 139 assertTrue(g1.equals(g2)); 140 141 } 142 143 146 public void testCloning() { 147 StandardCategoryItemLabelGenerator g1 148 = new StandardCategoryItemLabelGenerator(); 149 StandardCategoryItemLabelGenerator g2 = null; 150 try { 151 g2 = (StandardCategoryItemLabelGenerator) g1.clone(); 152 } 153 catch (CloneNotSupportedException e) { 154 System.err.println("Failed to clone."); 155 } 156 assertTrue(g1 != g2); 157 assertTrue(g1.getClass() == g2.getClass()); 158 assertTrue(g1.equals(g2)); 159 } 160 161 164 public void testSerialization() { 165 166 StandardCategoryItemLabelGenerator g1 167 = new StandardCategoryItemLabelGenerator( 168 "{2}", DateFormat.getInstance() 169 ); 170 StandardCategoryItemLabelGenerator g2 = null; 171 172 try { 173 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 174 ObjectOutput out = new ObjectOutputStream (buffer); 175 out.writeObject(g1); 176 out.close(); 177 178 ObjectInput in = new ObjectInputStream ( 179 new ByteArrayInputStream (buffer.toByteArray()) 180 ); 181 g2 = (StandardCategoryItemLabelGenerator) in.readObject(); 182 in.close(); 183 } 184 catch (Exception e) { 185 System.out.println(e.toString()); 186 } 187 assertEquals(g1, g2); 188 189 } 190 191 } 192 | Popular Tags |