1 42 43 package org.jfree.data.category.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.UnknownKeyException; 57 import org.jfree.data.category.DefaultCategoryDataset; 58 59 62 public class DefaultCategoryDatasetTests extends TestCase { 63 64 69 public static Test suite() { 70 return new TestSuite(DefaultCategoryDatasetTests.class); 71 } 72 73 78 public DefaultCategoryDatasetTests(String name) { 79 super(name); 80 } 81 82 85 public void testGetValue() { 86 DefaultCategoryDataset d = new DefaultCategoryDataset(); 87 d.addValue(1.0, "R1", "C1"); 88 assertEquals(new Double (1.0), d.getValue("R1", "C1")); 89 boolean pass = false; 90 try { 91 d.getValue("XX", "C1"); 92 } 93 catch (UnknownKeyException e) { 94 pass = true; 95 } 96 assertTrue(pass); 97 98 pass = false; 99 try { 100 d.getValue("R1", "XX"); 101 } 102 catch (UnknownKeyException e) { 103 pass = true; 104 } 105 assertTrue(pass); 106 } 107 108 111 public void testIncrementValue() { 112 DefaultCategoryDataset d = new DefaultCategoryDataset(); 113 d.addValue(1.0, "R1", "C1"); 114 d.incrementValue(2.0, "R1", "C1"); 115 assertEquals(new Double (3.0), d.getValue("R1", "C1")); 116 117 d.addValue(null, "R2", "C1"); 119 d.incrementValue(2.0, "R2", "C1"); 120 assertEquals(new Double (2.0), d.getValue("R2", "C1")); 121 122 boolean pass = false; 124 try { 125 d.incrementValue(1.0, "XX", "C1"); 126 } 127 catch (UnknownKeyException e) { 128 pass = true; 129 } 130 assertTrue(pass); 131 132 pass = false; 134 try { 135 d.incrementValue(1.0, "R1", "XX"); 136 } 137 catch (UnknownKeyException e) { 138 pass = true; 139 } 140 assertTrue(pass); 141 } 142 143 146 public void testGetRowCount() { 147 DefaultCategoryDataset d = new DefaultCategoryDataset(); 148 assertTrue(d.getRowCount() == 0); 149 150 d.addValue(1.0, "R1", "C1"); 151 assertTrue(d.getRowCount() == 1); 152 153 d.addValue(1.0, "R2", "C1"); 154 assertTrue(d.getRowCount() == 2); 155 156 d.addValue(2.0, "R2", "C1"); 157 assertTrue(d.getRowCount() == 2); 158 159 d.setValue(null, "R2", "C1"); 161 assertTrue(d.getRowCount() == 2); 162 } 163 164 167 public void testGetColumnCount() { 168 DefaultCategoryDataset d = new DefaultCategoryDataset(); 169 assertTrue(d.getColumnCount() == 0); 170 171 d.addValue(1.0, "R1", "C1"); 172 assertTrue(d.getColumnCount() == 1); 173 174 d.addValue(1.0, "R1", "C2"); 175 assertTrue(d.getColumnCount() == 2); 176 177 d.addValue(2.0, "R1", "C2"); 178 assertTrue(d.getColumnCount() == 2); 179 180 d.setValue(null, "R1", "C2"); 182 assertTrue(d.getColumnCount() == 2); 183 } 184 185 188 public void testEquals() { 189 DefaultCategoryDataset d1 = new DefaultCategoryDataset(); 190 d1.setValue(23.4, "R1", "C1"); 191 DefaultCategoryDataset d2 = new DefaultCategoryDataset(); 192 d2.setValue(23.4, "R1", "C1"); 193 assertTrue(d1.equals(d2)); 194 assertTrue(d2.equals(d1)); 195 196 d1.setValue(36.5, "R1", "C2"); 197 assertFalse(d1.equals(d2)); 198 d2.setValue(36.5, "R1", "C2"); 199 assertTrue(d1.equals(d2)); 200 201 d1.setValue(null, "R1", "C1"); 202 assertFalse(d1.equals(d2)); 203 d2.setValue(null, "R1", "C1"); 204 assertTrue(d1.equals(d2)); 205 } 206 207 210 public void testSerialization() { 211 212 DefaultCategoryDataset d1 = new DefaultCategoryDataset(); 213 d1.setValue(23.4, "R1", "C1"); 214 DefaultCategoryDataset d2 = null; 215 216 try { 217 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 218 ObjectOutput out = new ObjectOutputStream (buffer); 219 out.writeObject(d1); 220 out.close(); 221 222 ObjectInput in = new ObjectInputStream ( 223 new ByteArrayInputStream (buffer.toByteArray()) 224 ); 225 d2 = (DefaultCategoryDataset) in.readObject(); 226 in.close(); 227 } 228 catch (Exception e) { 229 System.out.println(e.toString()); 230 } 231 assertEquals(d1, d2); 232 233 } 234 235 } 236 | Popular Tags |