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.xy.CategoryTableXYDataset; 57 58 61 public class CategoryTableXYDatasetTests extends TestCase { 62 63 68 public static Test suite() { 69 return new TestSuite(CategoryTableXYDatasetTests.class); 70 } 71 72 77 public CategoryTableXYDatasetTests(String name) { 78 super(name); 79 } 80 81 84 public void testEquals() { 85 86 CategoryTableXYDataset d1 = new CategoryTableXYDataset(); 87 d1.add(1.0, 1.1, "Series 1"); 88 d1.add(2.0, 2.2, "Series 1"); 89 90 CategoryTableXYDataset d2 = new CategoryTableXYDataset(); 91 d2.add(1.0, 1.1, "Series 1"); 92 d2.add(2.0, 2.2, "Series 1"); 93 94 assertTrue(d1.equals(d2)); 95 assertTrue(d2.equals(d1)); 96 97 d1.add(3.0, 3.3, "Series 1"); 98 assertFalse(d1.equals(d2)); 99 100 d2.add(3.0, 3.3, "Series 1"); 101 assertTrue(d1.equals(d2)); 102 103 } 104 105 108 public void testCloning() { 109 110 CategoryTableXYDataset d1 = new CategoryTableXYDataset(); 111 d1.add(1.0, 1.1, "Series 1"); 112 d1.add(2.0, 2.2, "Series 1"); 113 114 CategoryTableXYDataset d2 = null; 115 try { 116 d2 = (CategoryTableXYDataset) d1.clone(); 117 } 118 catch (CloneNotSupportedException e) { 119 e.printStackTrace(); 120 } 121 assertTrue(d1 != d2); 122 assertTrue(d1.getClass() == d2.getClass()); 123 assertTrue(d1.equals(d2)); 124 } 125 126 129 public void testSerialization() { 130 131 CategoryTableXYDataset d1 = new CategoryTableXYDataset(); 132 d1.add(1.0, 1.1, "Series 1"); 133 d1.add(2.0, 2.2, "Series 1"); 134 135 CategoryTableXYDataset d2 = null; 136 137 try { 138 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 139 ObjectOutput out = new ObjectOutputStream (buffer); 140 out.writeObject(d1); 141 out.close(); 142 143 ObjectInput in = new ObjectInputStream ( 144 new ByteArrayInputStream (buffer.toByteArray()) 145 ); 146 d2 = (CategoryTableXYDataset) in.readObject(); 147 in.close(); 148 } 149 catch (Exception e) { 150 e.printStackTrace(); 151 } 152 assertEquals(d1, d2); 153 154 } 155 156 private static final double EPSILON = 0.0000000001; 157 158 163 public void testAddSeries() { 164 CategoryTableXYDataset d1 = new CategoryTableXYDataset(); 165 d1.setAutoWidth(true); 166 d1.add(3.0, 1.1, "Series 1"); 167 d1.add(7.0, 2.2, "Series 1"); 168 assertEquals(3.0, d1.getXValue(0, 0), EPSILON); 169 assertEquals(7.0, d1.getXValue(0, 1), EPSILON); 170 assertEquals(1.0, d1.getStartXValue(0, 0), EPSILON); 171 assertEquals(5.0, d1.getStartXValue(0, 1), EPSILON); 172 assertEquals(5.0, d1.getEndXValue(0, 0), EPSILON); 173 assertEquals(9.0, d1.getEndXValue(0, 1), EPSILON); 174 175 d1.add(7.5, 1.1, "Series 2"); 177 d1.add(9.0, 2.2, "Series 2"); 178 179 assertEquals(3.0, d1.getXValue(1, 0), EPSILON); 180 assertEquals(7.0, d1.getXValue(1, 1), EPSILON); 181 assertEquals(7.5, d1.getXValue(1, 2), EPSILON); 182 assertEquals(9.0, d1.getXValue(1, 3), EPSILON); 183 184 assertEquals(7.25, d1.getStartXValue(1, 2), EPSILON); 185 assertEquals(8.75, d1.getStartXValue(1, 3), EPSILON); 186 assertEquals(7.75, d1.getEndXValue(1, 2), EPSILON); 187 assertEquals(9.25, d1.getEndXValue(1, 3), EPSILON); 188 189 assertEquals(2.75, d1.getStartXValue(0, 0), EPSILON); 191 assertEquals(6.75, d1.getStartXValue(0, 1), EPSILON); 192 assertEquals(3.25, d1.getEndXValue(0, 0), EPSILON); 193 assertEquals(7.25, d1.getEndXValue(0, 1), EPSILON); 194 } 195 } 196 | Popular Tags |