1 42 43 package org.jfree.data.xy.junit; 44 45 import java.io.ByteArrayInputStream ; 46 import java.io.ByteArrayOutputStream ; 47 import java.io.ObjectInput ; 48 import java.io.ObjectInputStream ; 49 import java.io.ObjectOutput ; 50 import java.io.ObjectOutputStream ; 51 52 import junit.framework.Test; 53 import junit.framework.TestCase; 54 import junit.framework.TestSuite; 55 56 import org.jfree.data.general.SeriesException; 57 import org.jfree.data.xy.XYDataItem; 58 import org.jfree.data.xy.XYSeries; 59 60 63 public class XYSeriesTests extends TestCase { 64 65 70 public static Test suite() { 71 return new TestSuite(XYSeriesTests.class); 72 } 73 74 79 public XYSeriesTests(String name) { 80 super(name); 81 } 82 83 86 public void testEquals() { 87 88 XYSeries s1 = new XYSeries("Series"); 89 s1.add(1.0, 1.1); 90 XYSeries s2 = new XYSeries("Series"); 91 s2.add(1.0, 1.1); 92 assertTrue(s1.equals(s2)); 93 assertTrue(s2.equals(s1)); 94 95 s1.setKey("Series X"); 96 assertFalse(s1.equals(s2)); 97 98 s2.setKey("Series X"); 99 assertTrue(s1.equals(s2)); 100 101 } 102 103 106 public void testCloning() { 107 XYSeries s1 = new XYSeries("Series"); 108 s1.add(1.0, 1.1); 109 XYSeries s2 = null; 110 try { 111 s2 = (XYSeries) s1.clone(); 112 } 113 catch (CloneNotSupportedException e) { 114 System.err.println("XYSeriesTests.testCloning: failed to clone."); 115 } 116 assertTrue(s1 != s2); 117 assertTrue(s1.getClass() == s2.getClass()); 118 assertTrue(s1.equals(s2)); 119 } 120 121 124 public void testSerialization() { 125 126 XYSeries s1 = new XYSeries("Series"); 127 s1.add(1.0, 1.1); 128 XYSeries s2 = null; 129 130 try { 131 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 132 ObjectOutput out = new ObjectOutputStream (buffer); 133 out.writeObject(s1); 134 out.close(); 135 136 ObjectInput in = new ObjectInputStream ( 137 new ByteArrayInputStream (buffer.toByteArray()) 138 ); 139 s2 = (XYSeries) in.readObject(); 140 in.close(); 141 } 142 catch (Exception e) { 143 System.out.println(e.toString()); 144 } 145 assertEquals(s1, s2); 146 147 } 148 149 152 public void testIndexOf() { 153 XYSeries s1 = new XYSeries("Series 1"); 154 s1.add(1.0, 1.0); 155 s1.add(2.0, 2.0); 156 s1.add(3.0, 3.0); 157 assertEquals(0, s1.indexOf(new Double (1.0))); 158 } 159 160 163 public void testIndexOf2() { 164 XYSeries s1 = new XYSeries("Series 1", false, true); 165 s1.add(1.0, 1.0); 166 s1.add(3.0, 3.0); 167 s1.add(2.0, 2.0); 168 assertEquals(0, s1.indexOf(new Double (1.0))); 169 assertEquals(1, s1.indexOf(new Double (3.0))); 170 assertEquals(2, s1.indexOf(new Double (2.0))); 171 } 172 173 176 public void testRemove() { 177 178 XYSeries s1 = new XYSeries("Series 1"); 179 s1.add(1.0, 1.0); 180 s1.add(2.0, 2.0); 181 s1.add(3.0, 3.0); 182 183 assertEquals(3, s1.getItemCount()); 184 s1.remove(new Double (2.0)); 185 assertEquals(new Double (3.0), s1.getX(1)); 186 187 s1.remove(0); 188 assertEquals(new Double (3.0), s1.getX(0)); 189 190 } 191 192 private static final double EPSILON = 0.0000000001; 193 194 198 public void testAdditionOfDuplicateXValues() { 199 XYSeries s1 = new XYSeries("Series 1"); 200 s1.add(1.0, 1.0); 201 s1.add(2.0, 2.0); 202 s1.add(2.0, 3.0); 203 s1.add(2.0, 4.0); 204 s1.add(3.0, 5.0); 205 assertEquals(1.0, s1.getY(0).doubleValue(), EPSILON); 206 assertEquals(2.0, s1.getY(1).doubleValue(), EPSILON); 207 assertEquals(3.0, s1.getY(2).doubleValue(), EPSILON); 208 assertEquals(4.0, s1.getY(3).doubleValue(), EPSILON); 209 assertEquals(5.0, s1.getY(4).doubleValue(), EPSILON); 210 } 211 212 215 public void testUpdate() { 216 XYSeries series = new XYSeries("S1"); 217 series.add(new Integer (1), new Integer (2)); 218 assertEquals(new Integer (2), series.getY(0)); 219 series.update(new Integer (1), new Integer (3)); 220 assertEquals(new Integer (3), series.getY(0)); 221 try { 222 series.update(new Integer (2), new Integer (99)); 223 assertTrue(false); 224 } 225 catch (SeriesException e) { 226 } 228 } 229 230 233 public void testUpdate2() { 234 XYSeries series = new XYSeries("Series", false, true); 235 series.add(5.0, 55.0); 236 series.add(4.0, 44.0); 237 series.add(6.0, 66.0); 238 series.update(new Double (4.0), new Double (99.0)); 239 assertEquals(new Double (99.0), series.getY(1)); 240 } 241 242 245 public void testAddOrUpdate() { 246 XYSeries series = new XYSeries("S1"); 247 XYDataItem old = series.addOrUpdate(new Long (1), new Long (2)); 248 assertTrue(old == null); 249 assertEquals(1, series.getItemCount()); 250 assertEquals(new Long (2), series.getY(0)); 251 252 old = series.addOrUpdate(new Long (2), new Long (3)); 253 assertTrue(old == null); 254 assertEquals(2, series.getItemCount()); 255 assertEquals(new Long (3), series.getY(1)); 256 257 old = series.addOrUpdate(new Long (1), new Long (99)); 258 assertEquals(new XYDataItem(new Long (1), new Long (2)), old); 259 assertEquals(2, series.getItemCount()); 260 assertEquals(new Long (99), series.getY(0)); 261 assertEquals(new Long (3), series.getY(1)); 262 } 263 264 267 public void testAddOrUpdate2() { 268 XYSeries series = new XYSeries("Series", false, true); 269 series.add(5.0, 5.5); 270 series.add(6.0, 6.6); 271 series.add(3.0, 3.3); 272 series.add(4.0, 4.4); 273 series.add(2.0, 2.2); 274 series.add(1.0, 1.1); 275 series.addOrUpdate(new Double (3.0), new Double (33.3)); 276 series.addOrUpdate(new Double (2.0), new Double (22.2)); 277 assertEquals(33.3, series.getY(2).doubleValue(), EPSILON); 278 assertEquals(22.2, series.getY(4).doubleValue(), EPSILON); 279 } 280 281 284 public void testAdd() { 285 XYSeries series = new XYSeries("Series", false, true); 286 series.add(5.0, 5.50); 287 series.add(5.1, 5.51); 288 series.add(6.0, 6.6); 289 series.add(3.0, 3.3); 290 series.add(4.0, 4.4); 291 series.add(2.0, 2.2); 292 series.add(1.0, 1.1); 293 assertEquals(5.5, series.getY(0).doubleValue(), EPSILON); 294 assertEquals(5.51, series.getY(1).doubleValue(), EPSILON); 295 assertEquals(6.6, series.getY(2).doubleValue(), EPSILON); 296 assertEquals(3.3, series.getY(3).doubleValue(), EPSILON); 297 assertEquals(4.4, series.getY(4).doubleValue(), EPSILON); 298 assertEquals(2.2, series.getY(5).doubleValue(), EPSILON); 299 assertEquals(1.1, series.getY(6).doubleValue(), EPSILON); 300 } 301 302 305 public void testSetMaximumItemCount() { 306 XYSeries s1 = new XYSeries("S1"); 307 assertEquals(Integer.MAX_VALUE, s1.getMaximumItemCount()); 308 s1.setMaximumItemCount(2); 309 assertEquals(2, s1.getMaximumItemCount()); 310 s1.add(1.0, 1.1); 311 s1.add(2.0, 2.2); 312 s1.add(3.0, 3.3); 313 assertEquals(2.0, s1.getX(0).doubleValue(), EPSILON); 314 assertEquals(3.0, s1.getX(1).doubleValue(), EPSILON); 315 } 316 317 320 public void testSetMaximumItemCount2() { 321 XYSeries s1 = new XYSeries("S1"); 322 s1.add(1.0, 1.1); 323 s1.add(2.0, 2.2); 324 s1.add(3.0, 3.3); 325 s1.setMaximumItemCount(2); 326 assertEquals(2.0, s1.getX(0).doubleValue(), EPSILON); 327 assertEquals(3.0, s1.getX(1).doubleValue(), EPSILON); 328 } 329 330 } 331 | Popular Tags |