1 45 46 package org.jfree.chart.axis.junit; 47 48 import java.awt.geom.Rectangle2D ; 49 import java.io.ByteArrayInputStream ; 50 import java.io.ByteArrayOutputStream ; 51 import java.io.ObjectInput ; 52 import java.io.ObjectInputStream ; 53 import java.io.ObjectOutput ; 54 import java.io.ObjectOutputStream ; 55 import java.text.DecimalFormat ; 56 57 import junit.framework.Test; 58 import junit.framework.TestCase; 59 import junit.framework.TestSuite; 60 61 import org.jfree.chart.ChartFactory; 62 import org.jfree.chart.JFreeChart; 63 import org.jfree.chart.axis.NumberAxis; 64 import org.jfree.chart.axis.NumberTickUnit; 65 import org.jfree.chart.plot.CategoryPlot; 66 import org.jfree.chart.plot.PlotOrientation; 67 import org.jfree.chart.plot.XYPlot; 68 import org.jfree.data.category.DefaultCategoryDataset; 69 import org.jfree.data.xy.XYSeries; 70 import org.jfree.data.xy.XYSeriesCollection; 71 import org.jfree.ui.RectangleEdge; 72 73 76 public class NumberAxisTests extends TestCase { 77 78 83 public static Test suite() { 84 return new TestSuite(NumberAxisTests.class); 85 } 86 87 92 public NumberAxisTests(String name) { 93 super(name); 94 } 95 96 99 public void testCloning() { 100 NumberAxis a1 = new NumberAxis("Test"); 101 NumberAxis a2 = null; 102 try { 103 a2 = (NumberAxis) a1.clone(); 104 } 105 catch (CloneNotSupportedException e) { 106 System.err.println("Failed to clone."); 107 } 108 assertTrue(a1 != a2); 109 assertTrue(a1.getClass() == a2.getClass()); 110 assertTrue(a1.equals(a2)); 111 } 112 113 116 public void testEquals() { 117 118 NumberAxis a1 = new NumberAxis("Test"); 119 NumberAxis a2 = new NumberAxis("Test"); 120 assertTrue(a1.equals(a2)); 121 122 a1.setAutoRangeIncludesZero(false); 124 assertFalse(a1.equals(a2)); 125 a2.setAutoRangeIncludesZero(false); 126 assertTrue(a1.equals(a2)); 127 128 a1.setAutoRangeStickyZero(false); 130 assertFalse(a1.equals(a2)); 131 a2.setAutoRangeStickyZero(false); 132 assertTrue(a1.equals(a2)); 133 134 a1.setTickUnit(new NumberTickUnit(25.0)); 136 assertFalse(a1.equals(a2)); 137 a2.setTickUnit(new NumberTickUnit(25.0)); 138 assertTrue(a1.equals(a2)); 139 140 a1.setNumberFormatOverride(new DecimalFormat ("0.00")); 142 assertFalse(a1.equals(a2)); 143 a2.setNumberFormatOverride(new DecimalFormat ("0.00")); 144 assertTrue(a1.equals(a2)); 145 146 } 147 148 151 public void testHashCode() { 152 NumberAxis a1 = new NumberAxis("Test"); 153 NumberAxis a2 = new NumberAxis("Test"); 154 assertTrue(a1.equals(a2)); 155 int h1 = a1.hashCode(); 156 int h2 = a2.hashCode(); 157 assertEquals(h1, h2); 158 } 159 160 private static final double EPSILON = 0.0000001; 161 162 165 public void testTranslateJava2DToValue() { 166 NumberAxis axis = new NumberAxis(); 167 axis.setRange(50.0, 100.0); 168 Rectangle2D dataArea = new Rectangle2D.Double (10.0, 50.0, 400.0, 300.0); 169 double y1 = axis.java2DToValue(75.0, dataArea, RectangleEdge.LEFT); 170 assertEquals(y1, 95.8333333, EPSILON); 171 double y2 = axis.java2DToValue(75.0, dataArea, RectangleEdge.RIGHT); 172 assertEquals(y2, 95.8333333, EPSILON); 173 double x1 = axis.java2DToValue(75.0, dataArea, RectangleEdge.TOP); 174 assertEquals(x1, 58.125, EPSILON); 175 double x2 = axis.java2DToValue(75.0, dataArea, RectangleEdge.BOTTOM); 176 assertEquals(x2, 58.125, EPSILON); 177 axis.setInverted(true); 178 double y3 = axis.java2DToValue(75.0, dataArea, RectangleEdge.LEFT); 179 assertEquals(y3, 54.1666667, EPSILON); 180 double y4 = axis.java2DToValue(75.0, dataArea, RectangleEdge.RIGHT); 181 assertEquals(y4, 54.1666667, EPSILON); 182 double x3 = axis.java2DToValue(75.0, dataArea, RectangleEdge.TOP); 183 assertEquals(x3, 91.875, EPSILON); 184 double x4 = axis.java2DToValue(75.0, dataArea, RectangleEdge.BOTTOM); 185 assertEquals(x4, 91.875, EPSILON); 186 187 } 188 189 192 public void testSerialization() { 193 194 NumberAxis a1 = new NumberAxis("Test Axis"); 195 NumberAxis a2 = null; 196 197 try { 198 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 199 ObjectOutput out = new ObjectOutputStream (buffer); 200 out.writeObject(a1); 201 out.close(); 202 203 ObjectInput in = new ObjectInputStream ( 204 new ByteArrayInputStream (buffer.toByteArray()) 205 ); 206 a2 = (NumberAxis) in.readObject(); 207 in.close(); 208 } 209 catch (Exception e) { 210 System.out.println(e.toString()); 211 } 212 assertEquals(a1, a2); 213 214 } 215 216 220 public void testAutoRange1() { 221 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 222 dataset.setValue(100.0, "Row 1", "Column 1"); 223 dataset.setValue(200.0, "Row 1", "Column 2"); 224 JFreeChart chart = ChartFactory.createBarChart( 225 "Test", 226 "Categories", 227 "Value", 228 dataset, 229 PlotOrientation.VERTICAL, 230 false, 231 false, 232 false 233 ); 234 CategoryPlot plot = (CategoryPlot) chart.getPlot(); 235 NumberAxis axis = (NumberAxis) plot.getRangeAxis(); 236 assertEquals(axis.getLowerBound(), 0.0, EPSILON); 237 assertEquals(axis.getUpperBound(), 210.0, EPSILON); 238 } 239 240 245 public void testAutoRange2() { 246 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 247 dataset.setValue(100.0, "Row 1", "Column 1"); 248 dataset.setValue(200.0, "Row 1", "Column 2"); 249 JFreeChart chart = ChartFactory.createBarChart( 250 "Test", 251 "Categories", 252 "Value", 253 dataset, 254 PlotOrientation.VERTICAL, 255 false, 256 false, 257 false 258 ); 259 CategoryPlot plot = (CategoryPlot) chart.getPlot(); 260 NumberAxis axis = (NumberAxis) plot.getRangeAxis(); 261 axis.setAutoRangeIncludesZero(false); 262 assertEquals(axis.getLowerBound(), 95.0, EPSILON); 263 assertEquals(axis.getUpperBound(), 205.0, EPSILON); 264 } 265 266 272 public void testAutoRange3() { 273 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 274 dataset.setValue(100.0, "Row 1", "Column 1"); 275 dataset.setValue(200.0, "Row 1", "Column 2"); 276 JFreeChart chart = ChartFactory.createBarChart( 277 "Test", 278 "Categories", 279 "Value", 280 dataset, 281 PlotOrientation.VERTICAL, 282 false, 283 false, 284 false 285 ); 286 CategoryPlot plot = (CategoryPlot) chart.getPlot(); 287 NumberAxis axis = (NumberAxis) plot.getRangeAxis(); 288 axis.setAutoRangeIncludesZero(false); 289 assertEquals(axis.getLowerBound(), 95.0, EPSILON); 290 assertEquals(axis.getUpperBound(), 205.0, EPSILON); 291 292 DefaultCategoryDataset dataset2 = new DefaultCategoryDataset(); 294 dataset2.setValue(900.0, "Row 1", "Column 1"); 295 dataset2.setValue(1000.0, "Row 1", "Column 2"); 296 plot.setDataset(dataset2); 297 assertEquals(axis.getLowerBound(), 895.0, EPSILON); 298 assertEquals(axis.getUpperBound(), 1005.0, EPSILON); 299 } 300 301 305 public void testXYAutoRange1() { 306 XYSeries series = new XYSeries("Series 1"); 307 series.add(1.0, 1.0); 308 series.add(2.0, 2.0); 309 series.add(3.0, 3.0); 310 XYSeriesCollection dataset = new XYSeriesCollection(); 311 dataset.addSeries(series); 312 JFreeChart chart = ChartFactory.createScatterPlot( 313 "Test", 314 "X", 315 "Y", 316 dataset, 317 PlotOrientation.VERTICAL, 318 false, 319 false, 320 false 321 ); 322 XYPlot plot = (XYPlot) chart.getPlot(); 323 NumberAxis axis = (NumberAxis) plot.getDomainAxis(); 324 axis.setAutoRangeIncludesZero(false); 325 assertEquals(0.9, axis.getLowerBound(), EPSILON); 326 assertEquals(3.1, axis.getUpperBound(), EPSILON); 327 } 328 329 333 public void testXYAutoRange2() { 334 XYSeries series = new XYSeries("Series 1"); 335 series.add(1.0, 1.0); 336 series.add(2.0, 2.0); 337 series.add(3.0, 3.0); 338 XYSeriesCollection dataset = new XYSeriesCollection(); 339 dataset.addSeries(series); 340 JFreeChart chart = ChartFactory.createScatterPlot( 341 "Test", 342 "X", 343 "Y", 344 dataset, 345 PlotOrientation.VERTICAL, 346 false, 347 false, 348 false 349 ); 350 XYPlot plot = (XYPlot) chart.getPlot(); 351 NumberAxis axis = (NumberAxis) plot.getRangeAxis(); 352 axis.setAutoRangeIncludesZero(false); 353 assertEquals(0.9, axis.getLowerBound(), EPSILON); 354 assertEquals(3.1, axis.getUpperBound(), EPSILON); 355 } 356 357 } 358 | Popular Tags |