1 41 42 package org.jfree.chart.plot.junit; 43 44 import java.io.ByteArrayInputStream ; 45 import java.io.ByteArrayOutputStream ; 46 import java.io.ObjectInput ; 47 import java.io.ObjectInputStream ; 48 import java.io.ObjectOutput ; 49 import java.io.ObjectOutputStream ; 50 import java.util.List ; 51 52 import junit.framework.Test; 53 import junit.framework.TestCase; 54 import junit.framework.TestSuite; 55 56 import org.jfree.chart.axis.NumberAxis; 57 import org.jfree.chart.labels.StandardCategoryToolTipGenerator; 58 import org.jfree.chart.plot.CategoryPlot; 59 import org.jfree.chart.plot.CombinedRangeCategoryPlot; 60 import org.jfree.chart.renderer.category.BarRenderer; 61 import org.jfree.chart.renderer.category.LineAndShapeRenderer; 62 import org.jfree.data.category.CategoryDataset; 63 import org.jfree.data.category.DefaultCategoryDataset; 64 65 68 public class CombinedRangeCategoryPlotTests extends TestCase { 69 70 75 public static Test suite() { 76 return new TestSuite(CombinedRangeCategoryPlotTests.class); 77 } 78 79 84 public CombinedRangeCategoryPlotTests(String name) { 85 super(name); 86 } 87 88 91 public void testEquals() { 92 CombinedRangeCategoryPlot plot1 = createPlot(); 93 CombinedRangeCategoryPlot plot2 = createPlot(); 94 assertTrue(plot1.equals(plot2)); 95 } 96 97 100 public void testCloning() { 101 CombinedRangeCategoryPlot plot1 = createPlot(); 102 CombinedRangeCategoryPlot plot2 = null; 103 try { 104 plot2 = (CombinedRangeCategoryPlot) plot1.clone(); 105 } 106 catch (CloneNotSupportedException e) { 107 System.err.println("Failed to clone."); 108 } 109 assertTrue(plot1 != plot2); 110 assertTrue(plot1.getClass() == plot2.getClass()); 111 assertTrue(plot1.equals(plot2)); 112 } 113 114 117 public void testSerialization() { 118 CombinedRangeCategoryPlot plot1 = createPlot(); 119 CombinedRangeCategoryPlot plot2 = null; 120 try { 121 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 122 ObjectOutput out = new ObjectOutputStream (buffer); 123 out.writeObject(plot1); 124 out.close(); 125 ObjectInput in = new ObjectInputStream ( 126 new ByteArrayInputStream (buffer.toByteArray()) 127 ); 128 plot2 = (CombinedRangeCategoryPlot) in.readObject(); 129 in.close(); 130 } 131 catch (Exception e) { 132 e.printStackTrace(); 133 } 134 assertEquals(plot1, plot2); 135 136 } 137 138 141 public void testRemoveSubplot() { 142 CombinedRangeCategoryPlot plot = new CombinedRangeCategoryPlot(); 143 CategoryPlot plot1 = new CategoryPlot(); 144 CategoryPlot plot2 = new CategoryPlot(); 145 CategoryPlot plot3 = new CategoryPlot(); 146 plot.add(plot1); 147 plot.add(plot2); 148 plot.add(plot3); 149 plot.remove(plot2); 150 List plots = plot.getSubplots(); 151 assertEquals(2, plots.size()); 152 } 153 154 159 public CategoryDataset createDataset1() { 160 161 DefaultCategoryDataset result = new DefaultCategoryDataset(); 162 163 String series1 = "First"; 165 String series2 = "Second"; 166 167 String type1 = "Type 1"; 169 String type2 = "Type 2"; 170 String type3 = "Type 3"; 171 String type4 = "Type 4"; 172 String type5 = "Type 5"; 173 String type6 = "Type 6"; 174 String type7 = "Type 7"; 175 String type8 = "Type 8"; 176 177 result.addValue(1.0, series1, type1); 178 result.addValue(4.0, series1, type2); 179 result.addValue(3.0, series1, type3); 180 result.addValue(5.0, series1, type4); 181 result.addValue(5.0, series1, type5); 182 result.addValue(7.0, series1, type6); 183 result.addValue(7.0, series1, type7); 184 result.addValue(8.0, series1, type8); 185 186 result.addValue(5.0, series2, type1); 187 result.addValue(7.0, series2, type2); 188 result.addValue(6.0, series2, type3); 189 result.addValue(8.0, series2, type4); 190 result.addValue(4.0, series2, type5); 191 result.addValue(4.0, series2, type6); 192 result.addValue(2.0, series2, type7); 193 result.addValue(1.0, series2, type8); 194 195 return result; 196 197 } 198 199 204 public CategoryDataset createDataset2() { 205 206 DefaultCategoryDataset result = new DefaultCategoryDataset(); 207 208 String series1 = "Third"; 210 String series2 = "Fourth"; 211 212 String type1 = "Type 1"; 214 String type2 = "Type 2"; 215 String type3 = "Type 3"; 216 String type4 = "Type 4"; 217 String type5 = "Type 5"; 218 String type6 = "Type 6"; 219 String type7 = "Type 7"; 220 String type8 = "Type 8"; 221 222 result.addValue(11.0, series1, type1); 223 result.addValue(14.0, series1, type2); 224 result.addValue(13.0, series1, type3); 225 result.addValue(15.0, series1, type4); 226 result.addValue(15.0, series1, type5); 227 result.addValue(17.0, series1, type6); 228 result.addValue(17.0, series1, type7); 229 result.addValue(18.0, series1, type8); 230 231 result.addValue(15.0, series2, type1); 232 result.addValue(17.0, series2, type2); 233 result.addValue(16.0, series2, type3); 234 result.addValue(18.0, series2, type4); 235 result.addValue(14.0, series2, type5); 236 result.addValue(14.0, series2, type6); 237 result.addValue(12.0, series2, type7); 238 result.addValue(11.0, series2, type8); 239 240 return result; 241 242 } 243 244 249 private CombinedRangeCategoryPlot createPlot() { 250 CategoryDataset dataset1 = createDataset1(); 251 NumberAxis rangeAxis1 = new NumberAxis("Value"); 252 rangeAxis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 253 LineAndShapeRenderer renderer1 = new LineAndShapeRenderer(); 254 renderer1.setBaseToolTipGenerator( 255 new StandardCategoryToolTipGenerator() 256 ); 257 CategoryPlot subplot1 = new CategoryPlot( 258 dataset1, null, rangeAxis1, renderer1 259 ); 260 subplot1.setDomainGridlinesVisible(true); 261 262 CategoryDataset dataset2 = createDataset2(); 263 NumberAxis rangeAxis2 = new NumberAxis("Value"); 264 rangeAxis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 265 BarRenderer renderer2 = new BarRenderer(); 266 renderer2.setBaseToolTipGenerator( 267 new StandardCategoryToolTipGenerator() 268 ); 269 CategoryPlot subplot2 = new CategoryPlot( 270 dataset2, null, rangeAxis2, renderer2 271 ); 272 subplot2.setDomainGridlinesVisible(true); 273 274 NumberAxis rangeAxis = new NumberAxis("Value"); 275 CombinedRangeCategoryPlot plot 276 = new CombinedRangeCategoryPlot(rangeAxis); 277 plot.add(subplot1, 2); 278 plot.add(subplot2, 1); 279 return plot; 280 } 281 } 282 | Popular Tags |