1 45 46 package org.jfree.chart.renderer.xy.junit; 47 48 import java.awt.Color ; 49 import java.awt.GradientPaint ; 50 import java.io.ByteArrayInputStream ; 51 import java.io.ByteArrayOutputStream ; 52 import java.io.ObjectInput ; 53 import java.io.ObjectInputStream ; 54 import java.io.ObjectOutput ; 55 import java.io.ObjectOutputStream ; 56 import java.util.Date ; 57 58 import junit.framework.Test; 59 import junit.framework.TestCase; 60 import junit.framework.TestSuite; 61 62 import org.jfree.chart.renderer.xy.CandlestickRenderer; 63 import org.jfree.data.Range; 64 import org.jfree.data.xy.DefaultOHLCDataset; 65 import org.jfree.data.xy.OHLCDataItem; 66 import org.jfree.data.xy.OHLCDataset; 67 68 71 public class CandlestickRendererTests extends TestCase { 72 73 78 public static Test suite() { 79 return new TestSuite(CandlestickRendererTests.class); 80 } 81 82 87 public CandlestickRendererTests(String name) { 88 super(name); 89 } 90 91 94 public void testEquals() { 95 CandlestickRenderer r1 = new CandlestickRenderer(); 96 CandlestickRenderer r2 = new CandlestickRenderer(); 97 assertEquals(r1, r2); 98 99 r1.setUpPaint(new GradientPaint (1.0f, 2.0f, Color.red, 3.0f, 4.0f, 101 Color.white)); 102 assertFalse(r1.equals(r2)); 103 r2.setUpPaint(new GradientPaint (1.0f, 2.0f, Color.red, 3.0f, 4.0f, 104 Color.white)); 105 assertTrue(r1.equals(r2)); 106 107 r1.setDownPaint(new GradientPaint (5.0f, 6.0f, Color.green, 7.0f, 8.0f, 109 Color.yellow)); 110 assertFalse(r1.equals(r2)); 111 r2.setDownPaint(new GradientPaint (5.0f, 6.0f, Color.green, 7.0f, 8.0f, 112 Color.yellow)); 113 assertTrue(r1.equals(r2)); 114 115 r1.setDrawVolume(false); 117 assertFalse(r1.equals(r2)); 118 r2.setDrawVolume(false); 119 assertTrue(r1.equals(r2)); 120 121 r1.setCandleWidth(3.3); 123 assertFalse(r1.equals(r2)); 124 r2.setCandleWidth(3.3); 125 assertTrue(r1.equals(r2)); 126 127 r1.setMaxCandleWidthInMilliseconds(123); 129 assertFalse(r1.equals(r2)); 130 r2.setMaxCandleWidthInMilliseconds(123); 131 assertTrue(r1.equals(r2)); 132 133 r1.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_SMALLEST); 135 assertFalse(r1.equals(r2)); 136 r2.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_SMALLEST); 137 assertTrue(r1.equals(r2)); 138 139 r1.setAutoWidthFactor(0.22); 141 assertFalse(r1.equals(r2)); 142 r2.setAutoWidthFactor(0.22); 143 assertTrue(r1.equals(r2)); 144 145 r1.setAutoWidthGap(1.1); 147 assertFalse(r1.equals(r2)); 148 r2.setAutoWidthGap(1.1); 149 assertTrue(r1.equals(r2)); 150 } 151 152 155 public void testHashcode() { 156 CandlestickRenderer r1 = new CandlestickRenderer(); 157 CandlestickRenderer r2 = new CandlestickRenderer(); 158 assertTrue(r1.equals(r2)); 159 int h1 = r1.hashCode(); 160 int h2 = r2.hashCode(); 161 assertEquals(h1, h2); 162 } 163 164 167 public void testCloning() { 168 CandlestickRenderer r1 = new CandlestickRenderer(); 169 CandlestickRenderer r2 = null; 170 try { 171 r2 = (CandlestickRenderer) r1.clone(); 172 } 173 catch (CloneNotSupportedException e) { 174 e.printStackTrace(); 175 } 176 assertTrue(r1 != r2); 177 assertTrue(r1.getClass() == r2.getClass()); 178 assertTrue(r1.equals(r2)); 179 } 180 181 184 public void testSerialization() { 185 186 CandlestickRenderer r1 = new CandlestickRenderer(); 187 CandlestickRenderer r2 = null; 188 189 try { 190 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 191 ObjectOutput out = new ObjectOutputStream (buffer); 192 out.writeObject(r1); 193 out.close(); 194 195 ObjectInput in = new ObjectInputStream ( 196 new ByteArrayInputStream (buffer.toByteArray())); 197 r2 = (CandlestickRenderer) in.readObject(); 198 in.close(); 199 } 200 catch (Exception e) { 201 e.printStackTrace(); 202 } 203 assertEquals(r1, r2); 204 205 } 206 207 210 public void testFindRangeBounds() { 211 CandlestickRenderer renderer = new CandlestickRenderer(); 212 213 OHLCDataItem item1 = new OHLCDataItem(new Date (1L), 2.0, 4.0, 1.0, 3.0, 214 100); 215 OHLCDataset dataset = new DefaultOHLCDataset("S1", 216 new OHLCDataItem[] {item1}); 217 Range range = renderer.findRangeBounds(dataset); 218 assertEquals(new Range(1.0, 4.0), range); 219 220 OHLCDataItem item2 = new OHLCDataItem(new Date (1L), -1.0, 3.0, -1.0, 221 3.0, 100); 222 dataset = new DefaultOHLCDataset("S1", new OHLCDataItem[] {item1, 223 item2}); 224 range = renderer.findRangeBounds(dataset); 225 assertEquals(new Range(-1.0, 4.0), range); 226 227 dataset = new DefaultOHLCDataset("S1", new OHLCDataItem[] {}); 229 range = renderer.findRangeBounds(dataset); 230 assertNull(range); 231 232 range = renderer.findRangeBounds(null); 234 assertNull(range); 235 } 236 237 } 238 | Popular Tags |