1 43 44 package org.jfree.data.category.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.category.CategoryToPieDataset; 58 import org.jfree.data.category.DefaultCategoryDataset; 59 import org.jfree.data.general.DefaultPieDataset; 60 import org.jfree.util.TableOrder; 61 62 65 public class CategoryToPieDatasetTests extends TestCase { 66 67 72 public static Test suite() { 73 return new TestSuite(CategoryToPieDatasetTests.class); 74 } 75 76 81 public CategoryToPieDatasetTests(String name) { 82 super(name); 83 } 84 85 88 public void testConstructor() { 89 CategoryToPieDataset p1 = new CategoryToPieDataset(null, 91 TableOrder.BY_COLUMN, 0); 92 assertNull(p1.getUnderlyingDataset()); 93 assertEquals(p1.getItemCount(), 0); 94 assertTrue(p1.getKeys().isEmpty()); 95 assertNull(p1.getValue("R1")); 96 } 97 98 101 public void testGetValue() { 102 DefaultCategoryDataset underlying = new DefaultCategoryDataset(); 103 underlying.addValue(1.1, "R1", "C1"); 104 underlying.addValue(2.2, "R1", "C2"); 105 CategoryToPieDataset d1 = new CategoryToPieDataset(underlying, 106 TableOrder.BY_ROW, 0); 107 assertEquals(d1.getValue("C1"), new Double (1.1)); 108 assertEquals(d1.getValue("C2"), new Double (2.2)); 109 110 try { 112 d1.getValue(-1); 113 fail("Expected IndexOutOfBoundsException."); 114 } 115 catch (IndexOutOfBoundsException e) { 116 } 118 119 try { 121 d1.getValue(d1.getItemCount()); 122 fail("Expected IndexOutOfBoundsException."); 123 } 124 catch (IndexOutOfBoundsException e) { 125 } 127 128 CategoryToPieDataset p1 = new CategoryToPieDataset(null, 130 TableOrder.BY_COLUMN, 0); 131 try { 132 p1.getValue(0); 133 fail("Expected IndexOutOfBoundsException."); 134 } 135 catch (IndexOutOfBoundsException e) { 136 } 138 } 139 140 143 public void testGetKey() { 144 DefaultCategoryDataset underlying = new DefaultCategoryDataset(); 145 underlying.addValue(1.1, "R1", "C1"); 146 underlying.addValue(2.2, "R1", "C2"); 147 CategoryToPieDataset d1 = new CategoryToPieDataset(underlying, 148 TableOrder.BY_ROW, 0); 149 assertEquals(d1.getKey(0), "C1"); 150 assertEquals(d1.getKey(1), "C2"); 151 152 try { 154 d1.getKey(-1); 155 fail("Expected IndexOutOfBoundsException."); 156 } 157 catch (IndexOutOfBoundsException e) { 158 } 160 161 try { 163 d1.getKey(d1.getItemCount()); 164 fail("Expected IndexOutOfBoundsException."); 165 } 166 catch (IndexOutOfBoundsException e) { 167 } 169 170 CategoryToPieDataset p1 = new CategoryToPieDataset(null, 172 TableOrder.BY_COLUMN, 0); 173 try { 174 p1.getKey(0); 175 fail("Expected IndexOutOfBoundsException."); 176 } 177 catch (IndexOutOfBoundsException e) { 178 } 180 } 181 182 185 public void testGetIndex() { 186 DefaultCategoryDataset underlying = new DefaultCategoryDataset(); 187 underlying.addValue(1.1, "R1", "C1"); 188 underlying.addValue(2.2, "R1", "C2"); 189 CategoryToPieDataset d1 = new CategoryToPieDataset(underlying, 190 TableOrder.BY_ROW, 0); 191 assertEquals(0, d1.getIndex("C1")); 192 assertEquals(1, d1.getIndex("C2")); 193 assertEquals(-1, d1.getIndex("XX")); 194 195 boolean pass = false; 197 try { 198 d1.getIndex(null); 199 } 200 catch (IllegalArgumentException e) { 201 pass = true; 202 } 203 assertTrue(pass); 204 } 205 206 209 public void testEquals() { 210 DefaultCategoryDataset underlying = new DefaultCategoryDataset(); 211 underlying.addValue(1.1, "R1", "C1"); 212 underlying.addValue(2.2, "R1", "C2"); 213 CategoryToPieDataset d1 = new CategoryToPieDataset(underlying, 214 TableOrder.BY_COLUMN, 1); 215 DefaultPieDataset d2 = new DefaultPieDataset(); 216 d2.setValue("R1", 2.2); 217 assertTrue(d1.equals(d2)); 218 } 219 220 223 public void testSerialization() { 224 DefaultCategoryDataset underlying = new DefaultCategoryDataset(); 225 underlying.addValue(1.1, "R1", "C1"); 226 underlying.addValue(2.2, "R1", "C2"); 227 CategoryToPieDataset d1 = new CategoryToPieDataset(underlying, 228 TableOrder.BY_COLUMN, 1); 229 CategoryToPieDataset d2 = null; 230 231 try { 232 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 233 ObjectOutput out = new ObjectOutputStream (buffer); 234 out.writeObject(d1); 235 out.close(); 236 237 ObjectInput in = new ObjectInputStream ( 238 new ByteArrayInputStream (buffer.toByteArray()) 239 ); 240 d2 = (CategoryToPieDataset) in.readObject(); 241 in.close(); 242 } 243 catch (Exception e) { 244 e.printStackTrace(); 245 } 246 assertEquals(d1, d2); 247 248 assertEquals(d1.getUnderlyingDataset(), d2.getUnderlyingDataset()); 251 assertEquals(d1.getExtractType(), d2.getExtractType()); 252 assertEquals(d1.getExtractIndex(), d2.getExtractIndex()); 253 } 254 255 } 256 | Popular Tags |