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 import java.util.Date ; 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.general.DatasetUtilities; 59 import org.jfree.data.xy.DefaultOHLCDataset; 60 import org.jfree.data.xy.OHLCDataItem; 61 62 65 public class DefaultOHLCDatasetTests extends TestCase { 66 67 72 public static Test suite() { 73 return new TestSuite(DefaultOHLCDatasetTests.class); 74 } 75 76 81 public DefaultOHLCDatasetTests(String name) { 82 super(name); 83 } 84 85 private static final double EPSILON = 0.0000000001; 86 87 90 public void testDataRange() { 91 OHLCDataItem[] data = new OHLCDataItem[3]; 92 data[0] = new OHLCDataItem(new Date (11L), 2.0, 4.0, 1.0, 3.0, 100.0); 93 data[1] = new OHLCDataItem(new Date (22L), 4.0, 9.0, 2.0, 5.0, 120.0); 94 data[2] = new OHLCDataItem(new Date (33L), 3.0, 7.0, 3.0, 6.0, 140.0); 95 DefaultOHLCDataset d = new DefaultOHLCDataset("S1", data); 96 Range r = DatasetUtilities.findRangeBounds(d, false); 97 assertEquals(1.0, r.getLowerBound(), EPSILON); 98 assertEquals(9.0, r.getUpperBound(), EPSILON); 99 } 100 101 104 public void testEquals() { 105 DefaultOHLCDataset d1 = new DefaultOHLCDataset("Series 1", 106 new OHLCDataItem[0]); 107 DefaultOHLCDataset d2 = new DefaultOHLCDataset("Series 1", 108 new OHLCDataItem[0]); 109 assertTrue(d1.equals(d2)); 110 assertTrue(d2.equals(d1)); 111 } 112 113 116 public void testCloning() { 117 DefaultOHLCDataset d1 = new DefaultOHLCDataset("Series 1", 118 new OHLCDataItem[0]); 119 DefaultOHLCDataset d2 = null; 120 try { 121 d2 = (DefaultOHLCDataset) d1.clone(); 122 } 123 catch (CloneNotSupportedException e) { 124 e.printStackTrace(); 125 } 126 assertTrue(d1 != d2); 127 assertTrue(d1.getClass() == d2.getClass()); 128 assertTrue(d1.equals(d2)); 129 } 130 131 134 public void testSerialization() { 135 DefaultOHLCDataset d1 = new DefaultOHLCDataset("Series 1", 136 new OHLCDataItem[0]); 137 DefaultOHLCDataset d2 = null; 138 139 try { 140 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 141 ObjectOutput out = new ObjectOutputStream (buffer); 142 out.writeObject(d1); 143 out.close(); 144 145 ObjectInput in = new ObjectInputStream ( 146 new ByteArrayInputStream (buffer.toByteArray()) 147 ); 148 d2 = (DefaultOHLCDataset) in.readObject(); 149 in.close(); 150 } 151 catch (Exception e) { 152 System.out.println(e.toString()); 153 } 154 assertEquals(d1, d2); 155 } 156 157 } 158 | Popular Tags |