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 57 60 public class ComparableObjectItemTests extends TestCase { 61 62 67 public static Test suite() { 68 return new TestSuite(ComparableObjectItemTests.class); 69 } 70 71 76 public ComparableObjectItemTests(String name) { 77 super(name); 78 } 79 80 83 public void testConstructor() { 84 boolean pass = false; 86 try { 87 new ComparableObjectItem(null, 88 "XYZ"); 89 } 90 catch (IllegalArgumentException e) { 91 pass = true; 92 } 93 assertTrue(pass); 94 } 95 96 99 public void testEquals() { 100 ComparableObjectItem item1 = new ComparableObjectItem(new Integer (1), 101 "XYZ"); 102 ComparableObjectItem item2 = new ComparableObjectItem(new Integer (1), 103 "XYZ"); 104 assertTrue(item1.equals(item2)); 105 assertTrue(item2.equals(item1)); 106 107 item1 = new ComparableObjectItem(new Integer (2), "XYZ"); 108 assertFalse(item1.equals(item2)); 109 item2 = new ComparableObjectItem(new Integer (2), "XYZ"); 110 assertTrue(item1.equals(item2)); 111 112 item1 = new ComparableObjectItem(new Integer (2), null); 113 assertFalse(item1.equals(item2)); 114 item2 = new ComparableObjectItem(new Integer (2), null); 115 assertTrue(item1.equals(item2)); 116 } 117 118 121 public void testCloning() { 122 ComparableObjectItem item1 = new ComparableObjectItem(new Integer (1), 123 "XYZ"); 124 ComparableObjectItem item2 = null; 125 try { 126 item2 = (ComparableObjectItem) item1.clone(); 127 } 128 catch (CloneNotSupportedException e) { 129 e.printStackTrace(); 130 } 131 assertTrue(item1 != item2); 132 assertTrue(item1.getClass() == item2.getClass()); 133 assertTrue(item1.equals(item2)); 134 } 135 136 139 public void testSerialization() { 140 ComparableObjectItem item1 = new ComparableObjectItem(new Integer (1), 141 "XYZ"); 142 ComparableObjectItem item2 = null; 143 try { 144 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 145 ObjectOutput out = new ObjectOutputStream (buffer); 146 out.writeObject(item1); 147 out.close(); 148 149 ObjectInput in = new ObjectInputStream ( 150 new ByteArrayInputStream (buffer.toByteArray())); 151 item2 = (ComparableObjectItem) in.readObject(); 152 in.close(); 153 } 154 catch (Exception e) { 155 e.printStackTrace(); 156 } 157 assertEquals(item1, item2); 158 } 159 160 163 public void testCompareTo() { 164 ComparableObjectItem item1 = new ComparableObjectItem(new Integer (1), 165 "XYZ"); 166 ComparableObjectItem item2 = new ComparableObjectItem(new Integer (2), 167 "XYZ"); 168 ComparableObjectItem item3 = new ComparableObjectItem(new Integer (3), 169 "XYZ"); 170 ComparableObjectItem item4 = new ComparableObjectItem(new Integer (1), 171 "XYZ"); 172 assertTrue(item2.compareTo(item1) > 0); 173 assertTrue(item3.compareTo(item1) > 0); 174 assertTrue(item4.compareTo(item1) == 0); 175 assertTrue(item1.compareTo(item2) < 0); 176 } 177 178 } 179 | Popular Tags |