1 43 44 package org.jfree.data.xy.junit; 45 46 import java.io.ByteArrayInputStream ; 47 import java.io.ByteArrayOutputStream ; 48 import java.io.ObjectInput ; 49 import java.io.ObjectInputStream ; 50 import java.io.ObjectOutput ; 51 import java.io.ObjectOutputStream ; 52 53 import junit.framework.Test; 54 import junit.framework.TestCase; 55 import junit.framework.TestSuite; 56 57 import org.jfree.data.xy.DefaultXYDataset; 58 59 62 public class DefaultXYDatasetTests extends TestCase { 63 64 69 public static Test suite() { 70 return new TestSuite(DefaultXYDatasetTests.class); 71 } 72 73 78 public DefaultXYDatasetTests(String name) { 79 super(name); 80 } 81 82 85 public void testEquals() { 86 87 DefaultXYDataset d1 = new DefaultXYDataset(); 88 DefaultXYDataset d2 = new DefaultXYDataset(); 89 assertTrue(d1.equals(d2)); 90 assertTrue(d2.equals(d1)); 91 92 double[] x1 = new double[] {1.0, 2.0, 3.0}; 93 double[] y1 = new double[] {4.0, 5.0, 6.0}; 94 double[][] data1 = new double[][] {x1, y1}; 95 double[] x2 = new double[] {1.0, 2.0, 3.0}; 96 double[] y2 = new double[] {4.0, 5.0, 6.0}; 97 double[][] data2 = new double[][] {x2, y2}; 98 d1.addSeries("S1", data1); 99 assertFalse(d1.equals(d2)); 100 d2.addSeries("S1", data2); 101 assertTrue(d1.equals(d2)); 102 } 103 104 107 public void testCloning() { 108 DefaultXYDataset d1 = new DefaultXYDataset(); 109 DefaultXYDataset d2 = null; 110 try { 111 d2 = (DefaultXYDataset) d1.clone(); 112 } 113 catch (CloneNotSupportedException e) { 114 e.printStackTrace(); 115 } 116 assertTrue(d1 != d2); 117 assertTrue(d1.getClass() == d2.getClass()); 118 assertTrue(d1.equals(d2)); 119 120 double[] x1 = new double[] {1.0, 2.0, 3.0}; 122 double[] y1 = new double[] {4.0, 5.0, 6.0}; 123 double[][] data1 = new double[][] {x1, y1}; 124 d1.addSeries("S1", data1); 125 try { 126 d2 = (DefaultXYDataset) d1.clone(); 127 } 128 catch (CloneNotSupportedException e) { 129 e.printStackTrace(); 130 } 131 assertTrue(d1 != d2); 132 assertTrue(d1.getClass() == d2.getClass()); 133 assertTrue(d1.equals(d2)); 134 135 x1[1] = 2.2; 137 assertFalse(d1.equals(d2)); 138 x1[1] = 2.0; 139 assertTrue(d1.equals(d2)); 140 } 141 142 145 public void testSerialization() { 146 147 DefaultXYDataset d1 = new DefaultXYDataset(); 148 DefaultXYDataset d2 = null; 149 150 try { 151 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 152 ObjectOutput out = new ObjectOutputStream (buffer); 153 out.writeObject(d1); 154 out.close(); 155 156 ObjectInput in = new ObjectInputStream ( 157 new ByteArrayInputStream (buffer.toByteArray()) 158 ); 159 d2 = (DefaultXYDataset) in.readObject(); 160 in.close(); 161 } 162 catch (Exception e) { 163 e.printStackTrace(); 164 } 165 assertEquals(d1, d2); 166 167 double[] x1 = new double[] {1.0, 2.0, 3.0}; 169 double[] y1 = new double[] {4.0, 5.0, 6.0}; 170 double[][] data1 = new double[][] {x1, y1}; 171 d1.addSeries("S1", data1); 172 try { 173 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 174 ObjectOutput out = new ObjectOutputStream (buffer); 175 out.writeObject(d1); 176 out.close(); 177 178 ObjectInput in = new ObjectInputStream ( 179 new ByteArrayInputStream (buffer.toByteArray()) 180 ); 181 d2 = (DefaultXYDataset) in.readObject(); 182 in.close(); 183 } 184 catch (Exception e) { 185 e.printStackTrace(); 186 } 187 assertEquals(d1, d2); 188 189 } 190 191 194 public void testGetSeriesKey() { 195 DefaultXYDataset d = createSampleDataset1(); 196 assertEquals("S1", d.getSeriesKey(0)); 197 assertEquals("S2", d.getSeriesKey(1)); 198 199 boolean pass = false; 201 try { 202 d.getSeriesKey(-1); 203 } 204 catch (IllegalArgumentException e) { 205 pass = true; 206 } 207 assertTrue(pass); 208 209 pass = false; 210 try { 211 d.getSeriesKey(2); 212 } 213 catch (IllegalArgumentException e) { 214 pass = true; 215 } 216 assertTrue(pass); 217 } 218 219 222 public void testIndexOf() { 223 DefaultXYDataset d = createSampleDataset1(); 224 assertEquals(0, d.indexOf("S1")); 225 assertEquals(1, d.indexOf("S2")); 226 assertEquals(-1, d.indexOf("Green Eggs and Ham")); 227 assertEquals(-1, d.indexOf(null)); 228 } 229 230 static final double EPSILON = 0.0000000001; 231 232 235 public void testAddSeries() { 236 DefaultXYDataset d = new DefaultXYDataset(); 237 d.addSeries("S1", new double[][] {{1.0}, {2.0}}); 238 assertEquals(1, d.getSeriesCount()); 239 assertEquals("S1", d.getSeriesKey(0)); 240 241 d.addSeries("S1", new double[][] {{11.0}, {12.0}}); 243 assertEquals(1, d.getSeriesCount()); 244 assertEquals(12.0, d.getYValue(0, 0), EPSILON); 245 246 boolean pass = false; 248 try 249 { 250 d.addSeries(null, new double[][] {{1.0}, {2.0}}); 251 } 252 catch (IllegalArgumentException e) 253 { 254 pass = true; 255 } 256 assertTrue(pass); 257 } 258 259 264 public DefaultXYDataset createSampleDataset1() { 265 DefaultXYDataset d = new DefaultXYDataset(); 266 double[] x1 = new double[] {1.0, 2.0, 3.0}; 267 double[] y1 = new double[] {4.0, 5.0, 6.0}; 268 double[][] data1 = new double[][] {x1, y1}; 269 d.addSeries("S1", data1); 270 271 double[] x2 = new double[] {1.0, 2.0, 3.0}; 272 double[] y2 = new double[] {4.0, 5.0, 6.0}; 273 double[][] data2 = new double[][] {x2, y2}; 274 d.addSeries("S2", data2); 275 return d; 276 } 277 278 } 279 | Popular Tags |