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.DefaultTableXYDataset; 58 import org.jfree.data.xy.XYSeries; 59 60 63 public class DefaultTableXYDatasetTests extends TestCase { 64 65 70 public static Test suite() { 71 return new TestSuite(DefaultTableXYDatasetTests.class); 72 } 73 74 79 public DefaultTableXYDatasetTests(String name) { 80 super(name); 81 } 82 83 86 public void testEquals() { 87 88 DefaultTableXYDataset d1 = new DefaultTableXYDataset(); 89 XYSeries s1 = new XYSeries("Series 1", true, false); 90 s1.add(1.0, 1.1); 91 s1.add(2.0, 2.2); 92 d1.addSeries(s1); 93 94 DefaultTableXYDataset d2 = new DefaultTableXYDataset(); 95 XYSeries s2 = new XYSeries("Series 1", true, false); 96 s2.add(1.0, 1.1); 97 s2.add(2.0, 2.2); 98 d2.addSeries(s2); 99 100 assertTrue(d1.equals(d2)); 101 assertTrue(d2.equals(d1)); 102 103 s1.add(3.0, 3.3); 104 assertFalse(d1.equals(d2)); 105 106 s2.add(3.0, 3.3); 107 assertTrue(d1.equals(d2)); 108 109 } 110 111 114 public void testCloning() { 115 DefaultTableXYDataset d1 = new DefaultTableXYDataset(); 116 XYSeries s1 = new XYSeries("Series 1", true, false); 117 s1.add(1.0, 1.1); 118 s1.add(2.0, 2.2); 119 d1.addSeries(s1); 120 121 DefaultTableXYDataset d2 = null; 122 try { 123 d2 = (DefaultTableXYDataset) d1.clone(); 124 } 125 catch (CloneNotSupportedException e) { 126 System.err.println("Failed to clone."); 127 } 128 assertTrue(d1 != d2); 129 assertTrue(d1.getClass() == d2.getClass()); 130 assertTrue(d1.equals(d2)); 131 } 132 133 136 public void testSerialization() { 137 138 DefaultTableXYDataset d1 = new DefaultTableXYDataset(); 139 XYSeries s1 = new XYSeries("Series 1", true, false); 140 s1.add(1.0, 1.1); 141 s1.add(2.0, 2.2); 142 d1.addSeries(s1); 143 144 DefaultTableXYDataset d2 = null; 145 146 try { 147 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 148 ObjectOutput out = new ObjectOutputStream (buffer); 149 out.writeObject(d1); 150 out.close(); 151 152 ObjectInput in = new ObjectInputStream ( 153 new ByteArrayInputStream (buffer.toByteArray()) 154 ); 155 d2 = (DefaultTableXYDataset) in.readObject(); 156 in.close(); 157 } 158 catch (Exception e) { 159 System.out.println(e.toString()); 160 } 161 assertEquals(d1, d2); 162 163 } 164 165 private static final double EPSILON = 0.0000000001; 166 167 172 public void testAddSeries() { 173 DefaultTableXYDataset d1 = new DefaultTableXYDataset(); 174 d1.setAutoWidth(true); 175 XYSeries s1 = new XYSeries("Series 1", true, false); 176 s1.add(3.0, 1.1); 177 s1.add(7.0, 2.2); 178 d1.addSeries(s1); 179 assertEquals(3.0, d1.getXValue(0, 0), EPSILON); 180 assertEquals(7.0, d1.getXValue(0, 1), EPSILON); 181 assertEquals(1.0, d1.getStartXValue(0, 0), EPSILON); 182 assertEquals(5.0, d1.getStartXValue(0, 1), EPSILON); 183 assertEquals(5.0, d1.getEndXValue(0, 0), EPSILON); 184 assertEquals(9.0, d1.getEndXValue(0, 1), EPSILON); 185 186 XYSeries s2 = new XYSeries("Series 2", true, false); 188 s2.add(7.5, 1.1); 189 s2.add(9.0, 2.2); 190 d1.addSeries(s2); 191 192 assertEquals(3.0, d1.getXValue(1, 0), EPSILON); 193 assertEquals(7.0, d1.getXValue(1, 1), EPSILON); 194 assertEquals(7.5, d1.getXValue(1, 2), EPSILON); 195 assertEquals(9.0, d1.getXValue(1, 3), EPSILON); 196 197 assertEquals(7.25, d1.getStartXValue(1, 2), EPSILON); 198 assertEquals(8.75, d1.getStartXValue(1, 3), EPSILON); 199 assertEquals(7.75, d1.getEndXValue(1, 2), EPSILON); 200 assertEquals(9.25, d1.getEndXValue(1, 3), EPSILON); 201 202 assertEquals(2.75, d1.getStartXValue(0, 0), EPSILON); 204 assertEquals(6.75, d1.getStartXValue(0, 1), EPSILON); 205 assertEquals(3.25, d1.getEndXValue(0, 0), EPSILON); 206 assertEquals(7.25, d1.getEndXValue(0, 1), EPSILON); 207 } 208 } 209 | Popular Tags |