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.CategoryAxis; 57 import org.jfree.chart.axis.NumberAxis; 58 import org.jfree.chart.labels.StandardCategoryToolTipGenerator; 59 import org.jfree.chart.plot.CategoryPlot; 60 import org.jfree.chart.plot.CombinedDomainCategoryPlot; 61 import org.jfree.chart.renderer.category.BarRenderer; 62 import org.jfree.chart.renderer.category.LineAndShapeRenderer; 63 import org.jfree.data.category.CategoryDataset; 64 import org.jfree.data.category.DefaultCategoryDataset; 65 66 69 public class CombinedDomainCategoryPlotTests extends TestCase { 70 71 76 public static Test suite() { 77 return new TestSuite(CombinedDomainCategoryPlotTests.class); 78 } 79 80 85 public CombinedDomainCategoryPlotTests(String name) { 86 super(name); 87 } 88 89 92 public void testRemoveSubplot() { 93 CombinedDomainCategoryPlot plot = new CombinedDomainCategoryPlot(); 94 CategoryPlot plot1 = new CategoryPlot(); 95 CategoryPlot plot2 = new CategoryPlot(); 96 plot.add(plot1); 97 plot.add(plot2); 98 plot.remove(plot2); 100 List plots = plot.getSubplots(); 101 assertTrue(plots.get(0) == plot1); 102 assertEquals(1, plots.size()); 103 } 104 105 108 public void testEquals() { 109 CombinedDomainCategoryPlot plot1 = createPlot(); 110 CombinedDomainCategoryPlot plot2 = createPlot(); 111 assertTrue(plot1.equals(plot2)); 112 } 113 114 117 public void testCloning() { 118 CombinedDomainCategoryPlot plot1 = createPlot(); 119 CombinedDomainCategoryPlot plot2 = null; 120 try { 121 plot2 = (CombinedDomainCategoryPlot) plot1.clone(); 122 } 123 catch (CloneNotSupportedException e) { 124 System.err.println("Failed to clone."); 125 } 126 assertTrue(plot1 != plot2); 127 assertTrue(plot1.getClass() == plot2.getClass()); 128 assertTrue(plot1.equals(plot2)); 129 } 130 131 134 public void testSerialization() { 135 CombinedDomainCategoryPlot plot1 = createPlot(); 136 CombinedDomainCategoryPlot plot2 = null; 137 try { 138 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 139 ObjectOutput out = new ObjectOutputStream (buffer); 140 out.writeObject(plot1); 141 out.close(); 142 ObjectInput in = new ObjectInputStream ( 143 new ByteArrayInputStream (buffer.toByteArray()) 144 ); 145 plot2 = (CombinedDomainCategoryPlot) in.readObject(); 146 in.close(); 147 } 148 catch (Exception e) { 149 e.printStackTrace(); 150 } 151 assertEquals(plot1, plot2); 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 CombinedDomainCategoryPlot createPlot() { 250 251 CategoryDataset dataset1 = createDataset1(); 252 NumberAxis rangeAxis1 = new NumberAxis("Value"); 253 rangeAxis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 254 LineAndShapeRenderer renderer1 = new LineAndShapeRenderer(); 255 renderer1.setBaseToolTipGenerator( 256 new StandardCategoryToolTipGenerator() 257 ); 258 CategoryPlot subplot1 = new CategoryPlot( 259 dataset1, null, rangeAxis1, renderer1 260 ); 261 subplot1.setDomainGridlinesVisible(true); 262 263 CategoryDataset dataset2 = createDataset2(); 264 NumberAxis rangeAxis2 = new NumberAxis("Value"); 265 rangeAxis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 266 BarRenderer renderer2 = new BarRenderer(); 267 renderer2.setBaseToolTipGenerator( 268 new StandardCategoryToolTipGenerator() 269 ); 270 CategoryPlot subplot2 = new CategoryPlot( 271 dataset2, null, rangeAxis2, renderer2 272 ); 273 subplot2.setDomainGridlinesVisible(true); 274 275 CategoryAxis domainAxis = new CategoryAxis("Category"); 276 CombinedDomainCategoryPlot plot 277 = new CombinedDomainCategoryPlot(domainAxis); 278 plot.add(subplot1, 2); 279 plot.add(subplot2, 1); 280 return plot; 281 282 } 283 284 } 285 | Popular Tags |