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.time.Day; 57 import org.jfree.data.time.RegularTimePeriod; 58 import org.jfree.data.xy.DefaultWindDataset; 59 60 63 public class DefaultWindDatasetTests extends TestCase { 64 65 70 public static Test suite() { 71 return new TestSuite(DefaultWindDatasetTests.class); 72 } 73 74 79 public DefaultWindDatasetTests(String name) { 80 super(name); 81 } 82 83 86 public void testEquals() { 87 DefaultWindDataset d1 = new DefaultWindDataset(); 88 DefaultWindDataset d2 = new DefaultWindDataset(); 89 assertTrue(d1.equals(d2)); 90 assertTrue(d2.equals(d1)); 91 92 d1 = createSampleDataset1(); 93 assertFalse(d1.equals(d2)); 94 d2 = createSampleDataset1(); 95 assertTrue(d1.equals(d2)); 96 } 97 98 101 public void testCloning() { 102 DefaultWindDataset d1 = new DefaultWindDataset(); 103 DefaultWindDataset d2 = null; 104 try { 105 d2 = (DefaultWindDataset) d1.clone(); 106 } 107 catch (CloneNotSupportedException e) { 108 e.printStackTrace(); 109 } 110 assertTrue(d1 != d2); 111 assertTrue(d1.getClass() == d2.getClass()); 112 assertTrue(d1.equals(d2)); 113 114 d1 = createSampleDataset1(); 116 d2 = null; 117 try { 118 d2 = (DefaultWindDataset) d1.clone(); 119 } 120 catch (CloneNotSupportedException e) { 121 e.printStackTrace(); 122 } 123 assertTrue(d1 != d2); 124 assertTrue(d1.getClass() == d2.getClass()); 125 assertTrue(d1.equals(d2)); 126 } 127 128 131 public void testSerialization() { 132 DefaultWindDataset d1 = new DefaultWindDataset(); 133 DefaultWindDataset d2 = null; 134 try { 135 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 136 ObjectOutput out = new ObjectOutputStream (buffer); 137 out.writeObject(d1); 138 out.close(); 139 140 ObjectInput in = new ObjectInputStream (new ByteArrayInputStream ( 141 buffer.toByteArray())); 142 d2 = (DefaultWindDataset) in.readObject(); 143 in.close(); 144 } 145 catch (Exception e) { 146 e.printStackTrace(); 147 } 148 assertEquals(d1, d2); 149 150 d1 = createSampleDataset1(); 152 try { 153 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 154 ObjectOutput out = new ObjectOutputStream (buffer); 155 out.writeObject(d1); 156 out.close(); 157 158 ObjectInput in = new ObjectInputStream (new ByteArrayInputStream ( 159 buffer.toByteArray())); 160 d2 = (DefaultWindDataset) in.readObject(); 161 in.close(); 162 } 163 catch (Exception e) { 164 e.printStackTrace(); 165 } 166 assertEquals(d1, d2); 167 168 } 169 170 173 public void testGetSeriesKey() { 174 DefaultWindDataset d = createSampleDataset1(); 175 assertEquals("Series 1", d.getSeriesKey(0)); 176 assertEquals("Series 2", d.getSeriesKey(1)); 177 178 boolean pass = false; 180 try { 181 d.getSeriesKey(-1); 182 } 183 catch (IllegalArgumentException e) { 184 pass = true; 185 } 186 assertTrue(pass); 187 188 pass = false; 189 try { 190 d.getSeriesKey(2); 191 } 192 catch (IllegalArgumentException e) { 193 pass = true; 194 } 195 assertTrue(pass); 196 } 197 198 201 public void testIndexOf() { 202 DefaultWindDataset d = createSampleDataset1(); 203 assertEquals(0, d.indexOf("Series 1")); 204 assertEquals(1, d.indexOf("Series 2")); 205 assertEquals(-1, d.indexOf("Green Eggs and Ham")); 206 assertEquals(-1, d.indexOf(null)); 207 } 208 209 214 public DefaultWindDataset createSampleDataset1() { 215 Day t = new Day(1, 4, 2006); 216 Object [] item1 = createItem(t, 3, 7); 217 Object [] item2 = createItem(t.next(), 4, 8); 218 Object [] item3 = createItem(t.next(), 5, 9); 219 Object [][] series1 = new Object [][] {item1, item2, item3}; 220 Object [] item1b = createItem(t, 6, 10); 221 Object [] item2b = createItem(t.next(), 7, 11); 222 Object [] item3b = createItem(t.next(), 8, 12); 223 Object [][] series2 = new Object [][] {item1b, item2b, item3b}; 224 Object [][][] data = new Object [][][] {series1, series2}; 225 return new DefaultWindDataset(data); 226 } 227 228 237 private Object [] createItem(RegularTimePeriod t, int dir, int force) { 238 return new Object [] { new Long (t.getMiddleMillisecond()), 239 new Integer (dir), new Integer (force) }; 240 } 241 } 242 | Popular Tags |