1 43 44 package org.jfree.data.statistics.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.Range; 58 import org.jfree.data.statistics.DefaultStatisticalCategoryDataset; 59 60 63 public class DefaultStatisticalCategoryDatasetTests extends TestCase { 64 65 70 public static Test suite() { 71 return new TestSuite(DefaultStatisticalCategoryDatasetTests.class); 72 } 73 74 79 public DefaultStatisticalCategoryDatasetTests(String name) { 80 super(name); 81 } 82 83 86 public void testGetRangeBounds() { 87 DefaultStatisticalCategoryDataset d 88 = new DefaultStatisticalCategoryDataset(); 89 90 assertNull(d.getRangeBounds(true)); 92 93 d.add(4.5, 1.0, "R1", "C1"); 95 assertEquals(new Range(4.5, 4.5), d.getRangeBounds(false)); 96 assertEquals(new Range(3.5, 5.5), d.getRangeBounds(true)); 97 98 d.add(0.5, 2.0, "R1", "C2"); 100 assertEquals(new Range(0.5, 4.5), d.getRangeBounds(false)); 101 assertEquals(new Range(-1.5, 5.5), d.getRangeBounds(true)); 102 103 d.add(Double.NaN, 0.0, "R1", "C3"); 105 assertEquals(new Range(0.5, 4.5), d.getRangeBounds(false)); 106 assertEquals(new Range(-1.5, 5.5), d.getRangeBounds(true)); 107 108 d.add(Double.NEGATIVE_INFINITY, 0.0, "R1", "C3"); 110 assertEquals(new Range(Double.NEGATIVE_INFINITY, 4.5), 111 d.getRangeBounds(false)); 112 assertEquals(new Range(Double.NEGATIVE_INFINITY, 5.5), 113 d.getRangeBounds(true)); 114 115 d.add(Double.POSITIVE_INFINITY, 0.0, "R1", "C3"); 117 assertEquals(new Range(Double.NEGATIVE_INFINITY, 118 Double.POSITIVE_INFINITY), d.getRangeBounds(false)); 119 assertEquals(new Range(Double.NEGATIVE_INFINITY, 120 Double.POSITIVE_INFINITY), d.getRangeBounds(true)); 121 } 122 123 126 public void testEquals() { 127 DefaultStatisticalCategoryDataset d1 128 = new DefaultStatisticalCategoryDataset(); 129 DefaultStatisticalCategoryDataset d2 130 = new DefaultStatisticalCategoryDataset(); 131 assertTrue(d1.equals(d2)); 132 assertTrue(d2.equals(d1)); 133 134 } 135 136 139 public void testCloning() { 140 DefaultStatisticalCategoryDataset d1 141 = new DefaultStatisticalCategoryDataset(); 142 d1.add(1.1, 2.2, "R1", "C1"); 143 d1.add(3.3, 4.4, "R1", "C2"); 144 d1.add(null, new Double (5.5), "R1", "C3"); 145 d1.add(new Double (6.6), null, "R2", "C3"); 146 DefaultStatisticalCategoryDataset d2 = null; 147 try { 148 d2 = (DefaultStatisticalCategoryDataset) d1.clone(); 149 } 150 catch (CloneNotSupportedException e) { 151 fail(e.toString()); 152 } 153 assertTrue(d1 != d2); 154 assertTrue(d1.getClass() == d2.getClass()); 155 assertTrue(d1.equals(d2)); 156 } 157 158 161 public void testSerialization1() { 162 DefaultStatisticalCategoryDataset d1 163 = new DefaultStatisticalCategoryDataset(); 164 d1.add(1.1, 2.2, "R1", "C1"); 165 d1.add(3.3, 4.4, "R1", "C2"); 166 d1.add(null, new Double (5.5), "R1", "C3"); 167 d1.add(new Double (6.6), null, "R2", "C3"); 168 DefaultStatisticalCategoryDataset d2 = null; 169 try { 170 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 171 ObjectOutput out = new ObjectOutputStream (buffer); 172 out.writeObject(d1); 173 out.close(); 174 175 ObjectInput in = new ObjectInputStream ( 176 new ByteArrayInputStream (buffer.toByteArray())); 177 d2 = (DefaultStatisticalCategoryDataset) in.readObject(); 178 in.close(); 179 } 180 catch (Exception e) { 181 fail(e.toString()); 182 } 183 assertEquals(d1, d2); 184 } 185 186 189 public void testSerialization2() { 190 DefaultStatisticalCategoryDataset d1 191 = new DefaultStatisticalCategoryDataset(); 192 d1.add(1.2, 3.4, "Row 1", "Column 1"); 193 DefaultStatisticalCategoryDataset d2 = null; 194 try { 195 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 196 ObjectOutput out = new ObjectOutputStream (buffer); 197 out.writeObject(d1); 198 out.close(); 199 200 ObjectInput in = new ObjectInputStream ( 201 new ByteArrayInputStream (buffer.toByteArray())); 202 d2 = (DefaultStatisticalCategoryDataset) in.readObject(); 203 in.close(); 204 } 205 catch (Exception e) { 206 fail(e.toString()); 207 } 208 assertEquals(d1, d2); 209 } 210 211 } 212 | Popular Tags |