1 42 43 package org.jfree.data.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 import org.jfree.data.ComparableObjectItem; 56 import org.jfree.data.ComparableObjectSeries; 57 58 61 public class ComparableObjectSeriesTests extends TestCase { 62 63 static class MyComparableObjectSeries extends ComparableObjectSeries { 64 69 public MyComparableObjectSeries(Comparable key) { 70 super(key); 71 } 72 79 public MyComparableObjectSeries(Comparable key, boolean autoSort, 80 boolean allowDuplicateXValues) { 81 super(key, autoSort, allowDuplicateXValues); 82 } 83 public void add(Comparable x, Object y) { 84 super.add(x, y); 85 } 86 87 public ComparableObjectItem remove(Comparable x) { 88 return super.remove(x); 89 } 90 } 91 92 97 public static Test suite() { 98 return new TestSuite(ComparableObjectSeriesTests.class); 99 } 100 101 106 public ComparableObjectSeriesTests(String name) { 107 super(name); 108 } 109 110 113 public void testConstructor1() { 114 ComparableObjectSeries s1 = new ComparableObjectSeries("s1"); 115 assertEquals("s1", s1.getKey()); 116 assertNull(s1.getDescription()); 117 assertTrue(s1.getAllowDuplicateXValues()); 118 assertTrue(s1.getAutoSort()); 119 assertEquals(0, s1.getItemCount()); 120 assertEquals(Integer.MAX_VALUE, s1.getMaximumItemCount()); 121 122 boolean pass = false; 124 try { 125 s1 = new ComparableObjectSeries(null); 126 } 127 catch (IllegalArgumentException e) { 128 pass = true; 129 } 130 assertTrue(pass); 131 } 132 133 136 public void testEquals() { 137 MyComparableObjectSeries s1 = new MyComparableObjectSeries("A"); 138 MyComparableObjectSeries s2 = new MyComparableObjectSeries("A"); 139 assertTrue(s1.equals(s2)); 140 assertTrue(s2.equals(s1)); 141 142 s1 = new MyComparableObjectSeries("B"); 144 assertFalse(s1.equals(s2)); 145 s2 = new MyComparableObjectSeries("B"); 146 assertTrue(s1.equals(s2)); 147 148 s1 = new MyComparableObjectSeries("B", false, true); 150 assertFalse(s1.equals(s2)); 151 s2 = new MyComparableObjectSeries("B", false, true); 152 assertTrue(s1.equals(s2)); 153 154 s1 = new MyComparableObjectSeries("B", false, false); 156 assertFalse(s1.equals(s2)); 157 s2 = new MyComparableObjectSeries("B", false, false); 158 assertTrue(s1.equals(s2)); 159 160 s1.add(new Integer (1), "ABC"); 162 assertFalse(s1.equals(s2)); 163 s2.add(new Integer (1), "ABC"); 164 assertTrue(s1.equals(s2)); 165 166 s1.add(new Integer (0), "DEF"); 168 assertFalse(s1.equals(s2)); 169 s2.add(new Integer (0), "DEF"); 170 assertTrue(s1.equals(s2)); 171 172 s1.remove(new Integer (1)); 174 assertFalse(s1.equals(s2)); 175 s2.remove(new Integer (1)); 176 assertTrue(s1.equals(s2)); 177 } 178 179 182 public void testCloning() { 183 MyComparableObjectSeries s1 = new MyComparableObjectSeries("A"); 184 s1.add(new Integer (1), "ABC"); 185 MyComparableObjectSeries s2 = null; 186 try { 187 s2 = (MyComparableObjectSeries) s1.clone(); 188 } 189 catch (CloneNotSupportedException e) { 190 e.printStackTrace(); 191 } 192 assertTrue(s1 != s2); 193 assertTrue(s1.getClass() == s2.getClass()); 194 assertTrue(s1.equals(s2)); 195 } 196 197 200 public void testSerialization() { 201 MyComparableObjectSeries s1 = new MyComparableObjectSeries("A"); 202 s1.add(new Integer (1), "ABC"); 203 MyComparableObjectSeries s2 = null; 204 try { 205 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 206 ObjectOutput out = new ObjectOutputStream (buffer); 207 out.writeObject(s1); 208 out.close(); 209 210 ObjectInput in = new ObjectInputStream ( 211 new ByteArrayInputStream (buffer.toByteArray())); 212 s2 = (MyComparableObjectSeries) in.readObject(); 213 in.close(); 214 } 215 catch (Exception e) { 216 e.printStackTrace(); 217 } 218 assertEquals(s1, s2); 219 } 220 221 } 222 | Popular Tags |