1 43 44 package org.jfree.chart.renderer.xy.junit; 45 46 import java.awt.geom.Line2D ; 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.axis.NumberAxis; 61 import org.jfree.chart.plot.PlotOrientation; 62 import org.jfree.chart.plot.XYPlot; 63 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; 64 import org.jfree.data.Range; 65 import org.jfree.data.xy.TableXYDataset; 66 import org.jfree.data.xy.XYSeriesCollection; 67 68 71 public class XYLineAndShapeRendererTests extends TestCase { 72 73 78 public static Test suite() { 79 return new TestSuite(XYLineAndShapeRendererTests.class); 80 } 81 82 87 public XYLineAndShapeRendererTests(String name) { 88 super(name); 89 } 90 91 94 public void testEquals() { 95 96 XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer(); 97 XYLineAndShapeRenderer r2 = new XYLineAndShapeRenderer(); 98 assertEquals(r1, r2); 99 assertEquals(r2, r1); 100 101 r1.setLinesVisible(true); 102 assertFalse(r1.equals(r2)); 103 r2.setLinesVisible(true); 104 assertTrue(r1.equals(r2)); 105 106 r1.setSeriesLinesVisible(3, true); 107 assertFalse(r1.equals(r2)); 108 r2.setSeriesLinesVisible(3, true); 109 assertTrue(r1.equals(r2)); 110 111 r1.setBaseLinesVisible(false); 112 assertFalse(r1.equals(r2)); 113 r2.setBaseLinesVisible(false); 114 assertTrue(r1.equals(r2)); 115 116 r1.setLegendLine(new Line2D.Double (1.0, 2.0, 3.0, 4.0)); 117 assertFalse(r1.equals(r2)); 118 r2.setLegendLine(new Line2D.Double (1.0, 2.0, 3.0, 4.0)); 119 assertTrue(r1.equals(r2)); 120 121 r1.setShapesVisible(true); 122 assertFalse(r1.equals(r2)); 123 r2.setShapesVisible(true); 124 assertTrue(r1.equals(r2)); 125 126 r1.setSeriesShapesVisible(3, true); 127 assertFalse(r1.equals(r2)); 128 r2.setSeriesShapesVisible(3, true); 129 assertTrue(r1.equals(r2)); 130 131 r1.setBaseShapesVisible(false); 132 assertFalse(r1.equals(r2)); 133 r2.setBaseShapesVisible(false); 134 assertTrue(r1.equals(r2)); 135 136 r1.setShapesFilled(true); 137 assertFalse(r1.equals(r2)); 138 r2.setShapesFilled(true); 139 assertTrue(r1.equals(r2)); 140 141 r1.setSeriesShapesFilled(3, true); 142 assertFalse(r1.equals(r2)); 143 r2.setSeriesShapesFilled(3, true); 144 assertTrue(r1.equals(r2)); 145 146 r1.setBaseShapesFilled(false); 147 assertFalse(r1.equals(r2)); 148 r2.setBaseShapesFilled(false); 149 assertTrue(r1.equals(r2)); 150 151 r1.setDrawOutlines(!r1.getDrawOutlines()); 152 assertFalse(r1.equals(r2)); 153 r2.setDrawOutlines(r1.getDrawOutlines()); 154 assertTrue(r1.equals(r2)); 155 156 r1.setUseOutlinePaint(true); 157 assertFalse(r1.equals(r2)); 158 r2.setUseOutlinePaint(true); 159 assertTrue(r1.equals(r2)); 160 } 161 162 165 public void testHashcode() { 166 XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer(); 167 XYLineAndShapeRenderer r2 = new XYLineAndShapeRenderer(); 168 assertTrue(r1.equals(r2)); 169 int h1 = r1.hashCode(); 170 int h2 = r2.hashCode(); 171 assertEquals(h1, h2); 172 } 173 174 177 public void testCloning() { 178 XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer(); 179 XYLineAndShapeRenderer r2 = null; 180 try { 181 r2 = (XYLineAndShapeRenderer) r1.clone(); 182 } 183 catch (CloneNotSupportedException e) { 184 System.err.println("Failed to clone."); 185 } 186 assertTrue(r1 != r2); 187 assertTrue(r1.getClass() == r2.getClass()); 188 assertTrue(r1.equals(r2)); 189 } 190 191 194 public void testSerialization() { 195 196 XYLineAndShapeRenderer r1 = new XYLineAndShapeRenderer(); 197 XYLineAndShapeRenderer r2 = null; 198 199 try { 200 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 201 ObjectOutput out = new ObjectOutputStream (buffer); 202 out.writeObject(r1); 203 out.close(); 204 205 ObjectInput in = new ObjectInputStream ( 206 new ByteArrayInputStream (buffer.toByteArray()) 207 ); 208 r2 = (XYLineAndShapeRenderer) in.readObject(); 209 in.close(); 210 } 211 catch (Exception e) { 212 System.out.println(e.toString()); 213 } 214 assertEquals(r1, r2); 215 216 } 217 218 221 public void testFindDomainBounds() { 222 XYSeriesCollection dataset 223 = RendererXYPackageTests.createTestXYSeriesCollection(); 224 JFreeChart chart = ChartFactory.createXYLineChart( 225 "Test Chart", "X", "Y", dataset, PlotOrientation.VERTICAL, 226 false, false, false 227 ); 228 XYPlot plot = (XYPlot) chart.getPlot(); 229 NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis(); 230 domainAxis.setAutoRangeIncludesZero(false); 231 Range bounds = domainAxis.getRange(); 232 assertFalse(bounds.contains(0.9)); 233 assertTrue(bounds.contains(1.0)); 234 assertTrue(bounds.contains(2.0)); 235 assertFalse(bounds.contains(2.10)); 236 } 237 238 241 public void testFindRangeBounds() { 242 TableXYDataset dataset 243 = RendererXYPackageTests.createTestTableXYDataset(); 244 JFreeChart chart = ChartFactory.createXYLineChart( 245 "Test Chart", "X", "Y", dataset, PlotOrientation.VERTICAL, 246 false, false, false 247 ); 248 XYPlot plot = (XYPlot) chart.getPlot(); 249 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 250 rangeAxis.setAutoRangeIncludesZero(false); 251 Range bounds = rangeAxis.getRange(); 252 assertFalse(bounds.contains(1.0)); 253 assertTrue(bounds.contains(2.0)); 254 assertTrue(bounds.contains(5.0)); 255 assertFalse(bounds.contains(6.0)); 256 } 257 258 } 259 | Popular Tags |