1 43 44 package org.jfree.chart.axis.junit; 45 46 import java.io.ByteArrayInputStream ; 47 import java.io.ByteArrayOutputStream ; 48 import java.io.ObjectInput ; 49 import java.io.ObjectInputStream ; 50 import java.io.ObjectOutput ; 51 import java.io.ObjectOutputStream ; 52 53 import junit.framework.Test; 54 import junit.framework.TestCase; 55 import junit.framework.TestSuite; 56 57 import org.jfree.chart.axis.CategoryLabelPosition; 58 import org.jfree.chart.axis.CategoryLabelPositions; 59 import org.jfree.text.TextBlockAnchor; 60 import org.jfree.ui.RectangleAnchor; 61 62 65 public class CategoryLabelPositionsTests extends TestCase { 66 67 72 public static Test suite() { 73 return new TestSuite(CategoryLabelPositionsTests.class); 74 } 75 76 81 public CategoryLabelPositionsTests(String name) { 82 super(name); 83 } 84 85 private static final RectangleAnchor RA_TOP = RectangleAnchor.TOP; 86 private static final RectangleAnchor RA_BOTTOM = RectangleAnchor.BOTTOM; 87 88 91 public void testEquals() { 92 CategoryLabelPositions p1 = new CategoryLabelPositions( 93 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 94 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 95 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 96 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER) 97 ); 98 CategoryLabelPositions p2 = new CategoryLabelPositions( 99 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 100 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 101 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 102 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER) 103 ); 104 assertEquals(p1, p2); 105 106 p1 = new CategoryLabelPositions( 107 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 108 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 109 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 110 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER) 111 ); 112 assertTrue(!p1.equals(p2)); 113 p2 = new CategoryLabelPositions( 114 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 115 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 116 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 117 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER) 118 ); 119 assertTrue(p1.equals(p2)); 120 121 p1 = new CategoryLabelPositions( 122 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 123 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 124 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 125 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER) 126 ); 127 assertTrue(!p1.equals(p2)); 128 p2 = new CategoryLabelPositions( 129 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 130 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 131 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 132 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER) 133 ); 134 assertTrue(p1.equals(p2)); 135 136 p1 = new CategoryLabelPositions( 137 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 138 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 139 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 140 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER) 141 ); 142 assertTrue(!p1.equals(p2)); 143 p2 = new CategoryLabelPositions( 144 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 145 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 146 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 147 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER) 148 ); 149 assertTrue(p1.equals(p2)); 150 151 p1 = new CategoryLabelPositions( 152 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 153 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 154 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 155 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER) 156 ); 157 assertTrue(!p1.equals(p2)); 158 p2 = new CategoryLabelPositions( 159 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 160 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 161 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER), 162 new CategoryLabelPosition(RA_BOTTOM, TextBlockAnchor.TOP_CENTER) 163 ); 164 assertTrue(p1.equals(p2)); 165 } 166 167 170 public void testHashCode() { 171 CategoryLabelPositions p1 = new CategoryLabelPositions( 172 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 173 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 174 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 175 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER) 176 ); 177 CategoryLabelPositions p2 = new CategoryLabelPositions( 178 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 179 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 180 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER), 181 new CategoryLabelPosition(RA_TOP, TextBlockAnchor.CENTER) 182 ); 183 assertTrue(p1.equals(p2)); 184 int h1 = p1.hashCode(); 185 int h2 = p2.hashCode(); 186 assertEquals(h1, h2); 187 } 188 189 192 public void testSerialization() { 193 194 CategoryLabelPositions p1 = CategoryLabelPositions.STANDARD; 195 CategoryLabelPositions p2 = null; 196 197 try { 198 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 199 ObjectOutput out = new ObjectOutputStream (buffer); 200 out.writeObject(p1); 201 out.close(); 202 203 ObjectInput in = new ObjectInputStream ( 204 new ByteArrayInputStream (buffer.toByteArray()) 205 ); 206 p2 = (CategoryLabelPositions) in.readObject(); 207 in.close(); 208 } 209 catch (Exception e) { 210 System.out.println(e.toString()); 211 } 212 assertEquals(p1, p2); 213 } 214 215 } 216 | Popular Tags |