1 44 45 package org.jfree.chart.junit; 46 47 import java.io.ByteArrayInputStream ; 48 import java.io.ByteArrayOutputStream ; 49 import java.io.ObjectInput ; 50 import java.io.ObjectInputStream ; 51 import java.io.ObjectOutput ; 52 import java.io.ObjectOutputStream ; 53 54 import junit.framework.Test; 55 import junit.framework.TestCase; 56 import junit.framework.TestSuite; 57 58 import org.jfree.chart.ChartFactory; 59 import org.jfree.chart.JFreeChart; 60 import org.jfree.chart.plot.PlotOrientation; 61 import org.jfree.data.category.DefaultCategoryDataset; 62 import org.jfree.data.general.DefaultPieDataset; 63 import org.jfree.data.time.Day; 64 import org.jfree.data.time.RegularTimePeriod; 65 import org.jfree.data.time.TimeSeries; 66 import org.jfree.data.time.TimeSeriesCollection; 67 68 72 public class JFreeChartTests extends TestCase { 73 74 75 private JFreeChart pieChart; 76 77 82 public static Test suite() { 83 return new TestSuite(JFreeChartTests.class); 84 } 85 86 91 public JFreeChartTests(String name) { 92 super(name); 93 } 94 95 98 protected void setUp() { 99 100 DefaultPieDataset data = new DefaultPieDataset(); 102 data.setValue("Java", new Double (43.2)); 103 data.setValue("Visual Basic", new Double (0.0)); 104 data.setValue("C/C++", new Double (17.5)); 105 106 this.pieChart = ChartFactory.createPieChart( 108 "Pie Chart", data, true, true, 112 false 113 ); 114 115 } 116 117 120 public void testSubtitleCount() { 121 int count = this.pieChart.getSubtitleCount(); 122 assertEquals(1, count); 123 } 124 125 128 public void testSerialization1() { 129 130 DefaultPieDataset data = new DefaultPieDataset(); 131 data.setValue("Type 1", 54.5); 132 data.setValue("Type 2", 23.9); 133 data.setValue("Type 3", 45.8); 134 135 JFreeChart c1 = ChartFactory.createPieChart( 136 "Test", data, true, true, true 137 ); 138 JFreeChart c2 = null; 139 140 try { 141 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 142 ObjectOutput out = new ObjectOutputStream (buffer); 143 out.writeObject(c1); 144 out.close(); 145 146 ObjectInput in = new ObjectInputStream ( 147 new ByteArrayInputStream (buffer.toByteArray()) 148 ); 149 c2 = (JFreeChart) in.readObject(); 150 in.close(); 151 } 152 catch (Exception e) { 153 e.printStackTrace(); 154 } 155 assertEquals(c1, c2); 156 157 } 158 159 162 public void testSerialization2() { 163 164 DefaultPieDataset data = new DefaultPieDataset(); 165 data.setValue("Type 1", 54.5); 166 data.setValue("Type 2", 23.9); 167 data.setValue("Type 3", 45.8); 168 169 JFreeChart c1 = ChartFactory.createPieChart3D( 170 "Test", data, true, true, true 171 ); 172 JFreeChart c2 = null; 173 174 try { 175 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 176 ObjectOutput out = new ObjectOutputStream (buffer); 177 out.writeObject(c1); 178 out.close(); 179 180 ObjectInput in = new ObjectInputStream ( 181 new ByteArrayInputStream (buffer.toByteArray()) 182 ); 183 c2 = (JFreeChart) in.readObject(); 184 in.close(); 185 } 186 catch (Exception e) { 187 e.printStackTrace(); 188 } 189 assertEquals(c1, c2); 190 191 } 192 193 196 public void testSerialization3() { 197 198 String series1 = "First"; 200 String series2 = "Second"; 201 String series3 = "Third"; 202 203 String category1 = "Category 1"; 205 String category2 = "Category 2"; 206 String category3 = "Category 3"; 207 String category4 = "Category 4"; 208 String category5 = "Category 5"; 209 String category6 = "Category 6"; 210 String category7 = "Category 7"; 211 String category8 = "Category 8"; 212 213 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 215 216 dataset.addValue(1.0, series1, category1); 217 dataset.addValue(4.0, series1, category2); 218 dataset.addValue(3.0, series1, category3); 219 dataset.addValue(5.0, series1, category4); 220 dataset.addValue(5.0, series1, category5); 221 dataset.addValue(7.0, series1, category6); 222 dataset.addValue(7.0, series1, category7); 223 dataset.addValue(8.0, series1, category8); 224 225 dataset.addValue(5.0, series2, category1); 226 dataset.addValue(7.0, series2, category2); 227 dataset.addValue(6.0, series2, category3); 228 dataset.addValue(8.0, series2, category4); 229 dataset.addValue(4.0, series2, category5); 230 dataset.addValue(4.0, series2, category6); 231 dataset.addValue(2.0, series2, category7); 232 dataset.addValue(1.0, series2, category8); 233 234 dataset.addValue(4.0, series3, category1); 235 dataset.addValue(3.0, series3, category2); 236 dataset.addValue(2.0, series3, category3); 237 dataset.addValue(3.0, series3, category4); 238 dataset.addValue(6.0, series3, category5); 239 dataset.addValue(3.0, series3, category6); 240 dataset.addValue(4.0, series3, category7); 241 dataset.addValue(3.0, series3, category8); 242 243 JFreeChart c1 = ChartFactory.createBarChart( 245 "Vertical Bar Chart", "Category", "Value", dataset, PlotOrientation.VERTICAL, true, true, 252 false 253 ); 254 255 JFreeChart c2 = null; 256 257 try { 258 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 259 ObjectOutput out = new ObjectOutputStream (buffer); 260 out.writeObject(c1); 261 out.close(); 262 263 ObjectInput in = new ObjectInputStream ( 264 new ByteArrayInputStream (buffer.toByteArray()) 265 ); 266 c2 = (JFreeChart) in.readObject(); 267 in.close(); 268 } 269 catch (Exception e) { 270 e.printStackTrace(); 271 } 272 assertEquals(c1, c2); 273 274 } 275 276 279 public void testSerialization4() { 280 281 RegularTimePeriod t = new Day(); 282 TimeSeries series = new TimeSeries("Series 1"); 283 series.add(t, 36.4); 284 t = t.next(); 285 series.add(t, 63.5); 286 TimeSeriesCollection dataset = new TimeSeriesCollection(); 287 dataset.addSeries(series); 288 289 JFreeChart c1 = ChartFactory.createTimeSeriesChart( 290 "Test", "Date", "Value", dataset, true, true, true 291 ); 292 JFreeChart c2 = null; 293 294 try { 295 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 296 ObjectOutput out = new ObjectOutputStream (buffer); 297 out.writeObject(c1); 298 out.close(); 299 300 ObjectInput in = new ObjectInputStream ( 301 new ByteArrayInputStream (buffer.toByteArray()) 302 ); 303 c2 = (JFreeChart) in.readObject(); 304 in.close(); 305 } 306 catch (Exception e) { 307 e.printStackTrace(); 308 } 309 assertEquals(c1, c2); 310 311 } 312 313 314 } 315 | Popular Tags |