1 42 43 package org.jfree.chart.renderer.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.chart.JFreeChart; 57 import org.jfree.chart.axis.CategoryAxis; 58 import org.jfree.chart.axis.NumberAxis; 59 import org.jfree.chart.plot.CategoryPlot; 60 import org.jfree.chart.renderer.category.LevelRenderer; 61 import org.jfree.data.category.DefaultCategoryDataset; 62 63 66 public class LevelRendererTests extends TestCase { 67 68 73 public static Test suite() { 74 return new TestSuite(LevelRendererTests.class); 75 } 76 77 82 public LevelRendererTests(String name) { 83 super(name); 84 } 85 86 89 public void testEquals() { 90 LevelRenderer r1 = new LevelRenderer(); 91 LevelRenderer r2 = new LevelRenderer(); 92 assertTrue(r1.equals(r2)); 93 assertTrue(r2.equals(r1)); 94 95 r1.setItemMargin(0.123); 96 assertFalse(r1.equals(r2)); 97 r2.setItemMargin(0.123); 98 assertTrue(r1.equals(r2)); 99 100 r1.setMaximumItemWidth(0.234); 101 assertFalse(r1.equals(r2)); 102 r2.setMaximumItemWidth(0.234); 103 assertTrue(r1.equals(r2)); 104 105 } 106 107 110 public void testHashcode() { 111 LevelRenderer r1 = new LevelRenderer(); 112 LevelRenderer r2 = new LevelRenderer(); 113 assertTrue(r1.equals(r2)); 114 int h1 = r1.hashCode(); 115 int h2 = r2.hashCode(); 116 assertEquals(h1, h2); 117 } 118 119 122 public void testCloning() { 123 LevelRenderer r1 = new LevelRenderer(); 124 r1.setItemMargin(0.123); 125 r1.setMaximumItemWidth(0.234); 126 LevelRenderer r2 = null; 127 try { 128 r2 = (LevelRenderer) r1.clone(); 129 } 130 catch (CloneNotSupportedException e) { 131 System.err.println("Failed to clone."); 132 } 133 assertTrue(r1 != r2); 134 assertTrue(r1.getClass() == r2.getClass()); 135 assertTrue(r1.equals(r2)); 136 137 assertTrue(checkIndependence(r1, r2)); 138 139 } 140 141 149 private boolean checkIndependence(LevelRenderer r1, LevelRenderer r2) { 150 151 boolean b0 = r1.equals(r2); 153 154 r1.setItemMargin(0.0); 156 boolean b1 = !r1.equals(r2); 157 r2.setItemMargin(0.0); 158 boolean b2 = r1.equals(r2); 159 160 return b0 && b1 && b2; 161 162 } 163 164 167 public void testSerialization() { 168 169 LevelRenderer r1 = new LevelRenderer(); 170 LevelRenderer r2 = null; 171 172 try { 173 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 174 ObjectOutput out = new ObjectOutputStream (buffer); 175 out.writeObject(r1); 176 out.close(); 177 178 ObjectInput in = new ObjectInputStream ( 179 new ByteArrayInputStream (buffer.toByteArray()) 180 ); 181 r2 = (LevelRenderer) in.readObject(); 182 in.close(); 183 } 184 catch (Exception e) { 185 e.printStackTrace(); 186 } 187 assertEquals(r1, r2); 188 189 } 190 191 195 public void testDrawWithNullInfo() { 196 boolean success = false; 197 try { 198 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 199 dataset.addValue(1.0, "S1", "C1"); 200 CategoryPlot plot = new CategoryPlot(dataset, 201 new CategoryAxis("Category"), new NumberAxis("Value"), 202 new LevelRenderer()); 203 JFreeChart chart = new JFreeChart(plot); 204 chart.createBufferedImage(300, 200, 205 null); 206 success = true; 207 } 208 catch (NullPointerException e) { 209 e.printStackTrace(); 210 success = false; 211 } 212 assertTrue(success); 213 } 214 215 } 216 | Popular Tags |