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.DefaultXYZDataset; 58 59 62 public class DefaultXYZDatasetTests extends TestCase { 63 64 69 public static Test suite() { 70 return new TestSuite(DefaultXYZDatasetTests.class); 71 } 72 73 78 public DefaultXYZDatasetTests(String name) { 79 super(name); 80 } 81 82 85 public void testEquals() { 86 87 DefaultXYZDataset d1 = new DefaultXYZDataset(); 88 DefaultXYZDataset d2 = new DefaultXYZDataset(); 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[] z1 = new double[] {7.0, 8.0, 9.0}; 95 double[][] data1 = new double[][] {x1, y1, z1}; 96 double[] x2 = new double[] {1.0, 2.0, 3.0}; 97 double[] y2 = new double[] {4.0, 5.0, 6.0}; 98 double[] z2 = new double[] {7.0, 8.0, 9.0}; 99 double[][] data2 = new double[][] {x2, y2, z2}; 100 d1.addSeries("S1", data1); 101 assertFalse(d1.equals(d2)); 102 d2.addSeries("S1", data2); 103 assertTrue(d1.equals(d2)); 104 } 105 106 109 public void testCloning() { 110 DefaultXYZDataset d1 = new DefaultXYZDataset(); 111 DefaultXYZDataset d2 = null; 112 try { 113 d2 = (DefaultXYZDataset) d1.clone(); 114 } 115 catch (CloneNotSupportedException e) { 116 e.printStackTrace(); 117 } 118 assertTrue(d1 != d2); 119 assertTrue(d1.getClass() == d2.getClass()); 120 assertTrue(d1.equals(d2)); 121 122 double[] x1 = new double[] {1.0, 2.0, 3.0}; 124 double[] y1 = new double[] {4.0, 5.0, 6.0}; 125 double[] z1 = new double[] {7.0, 8.0, 9.0}; 126 double[][] data1 = new double[][] {x1, y1, z1}; 127 d1.addSeries("S1", data1); 128 try { 129 d2 = (DefaultXYZDataset) d1.clone(); 130 } 131 catch (CloneNotSupportedException e) { 132 e.printStackTrace(); 133 } 134 assertTrue(d1 != d2); 135 assertTrue(d1.getClass() == d2.getClass()); 136 assertTrue(d1.equals(d2)); 137 138 x1[1] = 2.2; 140 assertFalse(d1.equals(d2)); 141 x1[1] = 2.0; 142 assertTrue(d1.equals(d2)); 143 } 144 145 148 public void testSerialization() { 149 150 DefaultXYZDataset d1 = new DefaultXYZDataset(); 151 DefaultXYZDataset d2 = null; 152 153 try { 154 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 155 ObjectOutput out = new ObjectOutputStream (buffer); 156 out.writeObject(d1); 157 out.close(); 158 159 ObjectInput in = new ObjectInputStream ( 160 new ByteArrayInputStream (buffer.toByteArray()) 161 ); 162 d2 = (DefaultXYZDataset) in.readObject(); 163 in.close(); 164 } 165 catch (Exception e) { 166 e.printStackTrace(); 167 } 168 assertEquals(d1, d2); 169 170 double[] x1 = new double[] {1.0, 2.0, 3.0}; 172 double[] y1 = new double[] {4.0, 5.0, 6.0}; 173 double[] z1 = new double[] {7.0, 8.0, 9.0}; 174 double[][] data1 = new double[][] {x1, y1, z1}; 175 d1.addSeries("S1", data1); 176 try { 177 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 178 ObjectOutput out = new ObjectOutputStream (buffer); 179 out.writeObject(d1); 180 out.close(); 181 182 ObjectInput in = new ObjectInputStream (new ByteArrayInputStream ( 183 buffer.toByteArray())); 184 d2 = (DefaultXYZDataset) in.readObject(); 185 in.close(); 186 } 187 catch (Exception e) { 188 e.printStackTrace(); 189 } 190 assertEquals(d1, d2); 191 192 } 193 194 197 public void testGetSeriesKey() { 198 DefaultXYZDataset d = createSampleDataset1(); 199 assertEquals("S1", d.getSeriesKey(0)); 200 assertEquals("S2", d.getSeriesKey(1)); 201 202 boolean pass = false; 204 try { 205 d.getSeriesKey(-1); 206 } 207 catch (IllegalArgumentException e) { 208 pass = true; 209 } 210 assertTrue(pass); 211 212 pass = false; 213 try { 214 d.getSeriesKey(2); 215 } 216 catch (IllegalArgumentException e) { 217 pass = true; 218 } 219 assertTrue(pass); 220 } 221 222 225 public void testIndexOf() { 226 DefaultXYZDataset d = createSampleDataset1(); 227 assertEquals(0, d.indexOf("S1")); 228 assertEquals(1, d.indexOf("S2")); 229 assertEquals(-1, d.indexOf("Green Eggs and Ham")); 230 assertEquals(-1, d.indexOf(null)); 231 } 232 233 static final double EPSILON = 0.0000000001; 234 235 238 public void testAddSeries() { 239 DefaultXYZDataset d = new DefaultXYZDataset(); 240 d.addSeries("S1", new double[][] {{1.0}, {2.0}, {3.0}}); 241 assertEquals(1, d.getSeriesCount()); 242 assertEquals("S1", d.getSeriesKey(0)); 243 244 d.addSeries("S1", new double[][] {{11.0}, {12.0}, {13.0}}); 246 assertEquals(1, d.getSeriesCount()); 247 assertEquals(12.0, d.getYValue(0, 0), EPSILON); 248 249 boolean pass = false; 251 try 252 { 253 d.addSeries(null, new double[][] {{1.0}, {2.0}, {3.0}}); 254 } 255 catch (IllegalArgumentException e) 256 { 257 pass = true; 258 } 259 assertTrue(pass); 260 } 261 262 267 public DefaultXYZDataset createSampleDataset1() { 268 DefaultXYZDataset d = new DefaultXYZDataset(); 269 double[] x1 = new double[] {1.0, 2.0, 3.0}; 270 double[] y1 = new double[] {4.0, 5.0, 6.0}; 271 double[] z1 = new double[] {7.0, 8.0, 9.0}; 272 double[][] data1 = new double[][] {x1, y1, z1}; 273 d.addSeries("S1", data1); 274 275 double[] x2 = new double[] {1.0, 2.0, 3.0}; 276 double[] y2 = new double[] {4.0, 5.0, 6.0}; 277 double[] z2 = new double[] {7.0, 8.0, 9.0}; 278 double[][] data2 = new double[][] {x2, y2, z2}; 279 d.addSeries("S2", data2); 280 return d; 281 } 282 283 } 284 | Popular Tags |