1 43 44 package org.jfree.ui.junit; 45 46 import java.awt.geom.Rectangle2D ; 47 import java.io.ByteArrayInputStream ; 48 import java.io.ByteArrayOutputStream ; 49 import java.io.ObjectInput ; 50 import java.io.ObjectInputStream ; 51 import java.io.ObjectOutput ; 52 import java.io.ObjectOutputStream ; 53 54 import junit.framework.Test; 55 import junit.framework.TestCase; 56 import junit.framework.TestSuite; 57 58 import org.jfree.ui.RectangleInsets; 59 import org.jfree.ui.LengthAdjustmentType; 60 import org.jfree.util.UnitType; 61 62 65 public class RectangleInsetsTests extends TestCase { 66 67 72 public static Test suite() { 73 return new TestSuite(RectangleInsetsTests.class); 74 } 75 76 81 public RectangleInsetsTests(final String name) { 82 super(name); 83 } 84 85 88 public void testCreateAdjustedRectangleAbsolute() { 89 90 RectangleInsets i1 = new RectangleInsets( 91 UnitType.ABSOLUTE, 1.0, 2.0, 3.0, 4.0 92 ); 93 Rectangle2D base = new Rectangle2D.Double (10.0, 20.0, 30.0, 40.0); 94 95 Rectangle2D adjusted = i1.createAdjustedRectangle( 97 base, LengthAdjustmentType.NO_CHANGE, LengthAdjustmentType.NO_CHANGE 98 ); 99 assertEquals(new Rectangle2D.Double (10.0, 20.0, 30.0, 40.0), adjusted); 100 101 adjusted = i1.createAdjustedRectangle( 103 base, LengthAdjustmentType.NO_CHANGE, LengthAdjustmentType.EXPAND 104 ); 105 assertEquals(new Rectangle2D.Double (10.0, 19.0, 30.0, 44.0), adjusted); 106 107 adjusted = i1.createAdjustedRectangle( 109 base, LengthAdjustmentType.NO_CHANGE, LengthAdjustmentType.CONTRACT 110 ); 111 assertEquals(new Rectangle2D.Double (10.0, 21.0, 30.0, 36.0), adjusted); 112 113 adjusted = i1.createAdjustedRectangle( 115 base, LengthAdjustmentType.EXPAND, LengthAdjustmentType.NO_CHANGE 116 ); 117 assertEquals(new Rectangle2D.Double (8.0, 20.0, 36.0, 40.0), adjusted); 118 119 adjusted = i1.createAdjustedRectangle( 121 base, LengthAdjustmentType.CONTRACT, LengthAdjustmentType.NO_CHANGE 122 ); 123 assertEquals(new Rectangle2D.Double (12.0, 20.0, 24.0, 40.0), adjusted); 124 125 adjusted = i1.createAdjustedRectangle( 127 base, LengthAdjustmentType.EXPAND, LengthAdjustmentType.EXPAND 128 ); 129 assertEquals(new Rectangle2D.Double (8.0, 19.0, 36.0, 44.0), adjusted); 130 131 adjusted = i1.createAdjustedRectangle( 133 base, LengthAdjustmentType.EXPAND, LengthAdjustmentType.CONTRACT 134 ); 135 assertEquals(new Rectangle2D.Double (8.0, 21.0, 36.0, 36.0), adjusted); 136 137 adjusted = i1.createAdjustedRectangle( 139 base, LengthAdjustmentType.CONTRACT, LengthAdjustmentType.CONTRACT 140 ); 141 assertEquals(new Rectangle2D.Double (12.0, 21.0, 24.0, 36.0), adjusted); 142 143 adjusted = i1.createAdjustedRectangle( 145 base, LengthAdjustmentType.CONTRACT, LengthAdjustmentType.EXPAND 146 ); 147 assertEquals(new Rectangle2D.Double (12.0, 19.0, 24.0, 44.0), adjusted); 148 149 } 150 151 private static final double EPSILON = 0.0000001; 152 153 156 public void testCreateAdjustedRectangleRelative() { 157 158 RectangleInsets i1 = new RectangleInsets( 159 UnitType.RELATIVE, 0.04, 0.03, 0.02, 0.01 160 ); 161 Rectangle2D base = new Rectangle2D.Double (10.0, 20.0, 30.0, 40.0); 162 163 Rectangle2D adjusted = i1.createAdjustedRectangle( 165 base, LengthAdjustmentType.NO_CHANGE, LengthAdjustmentType.NO_CHANGE 166 ); 167 assertEquals(new Rectangle2D.Double (10.0, 20.0, 30.0, 40.0), adjusted); 168 169 adjusted = i1.createAdjustedRectangle( 171 base, LengthAdjustmentType.NO_CHANGE, LengthAdjustmentType.EXPAND 172 ); 173 assertEquals(10.0, adjusted.getX(), EPSILON); 174 assertEquals(18.2978723404, adjusted.getY(), EPSILON); 175 assertEquals(30.0, adjusted.getWidth(), EPSILON); 176 assertEquals(42.553191489, adjusted.getHeight(), EPSILON); 177 178 adjusted = i1.createAdjustedRectangle( 180 base, LengthAdjustmentType.NO_CHANGE, LengthAdjustmentType.CONTRACT 181 ); 182 assertEquals(10.0, adjusted.getX(), EPSILON); 183 assertEquals(21.6, adjusted.getY(), EPSILON); 184 assertEquals(30.0, adjusted.getWidth(), EPSILON); 185 assertEquals(37.6, adjusted.getHeight(), EPSILON); 186 187 adjusted = i1.createAdjustedRectangle( 189 base, LengthAdjustmentType.EXPAND, LengthAdjustmentType.NO_CHANGE 190 ); 191 assertEquals(9.0625, adjusted.getX(), EPSILON); 192 assertEquals(20.0, adjusted.getY(), EPSILON); 193 assertEquals(31.25, adjusted.getWidth(), EPSILON); 194 assertEquals(40.0, adjusted.getHeight(), EPSILON); 195 196 adjusted = i1.createAdjustedRectangle( 198 base, LengthAdjustmentType.CONTRACT, LengthAdjustmentType.NO_CHANGE 199 ); 200 assertEquals(10.9, adjusted.getX(), EPSILON); 201 assertEquals(20.0, adjusted.getY(), EPSILON); 202 assertEquals(28.8, adjusted.getWidth(), EPSILON); 203 assertEquals(40.0, adjusted.getHeight(), EPSILON); 204 205 adjusted = i1.createAdjustedRectangle( 207 base, LengthAdjustmentType.EXPAND, LengthAdjustmentType.EXPAND 208 ); 209 assertEquals(9.0625, adjusted.getX(), EPSILON); 210 assertEquals(18.2978723404, adjusted.getY(), EPSILON); 211 assertEquals(31.25, adjusted.getWidth(), EPSILON); 212 assertEquals(42.553191489, adjusted.getHeight(), EPSILON); 213 214 adjusted = i1.createAdjustedRectangle( 216 base, LengthAdjustmentType.EXPAND, LengthAdjustmentType.CONTRACT 217 ); 218 assertEquals(9.0625, adjusted.getX(), EPSILON); 219 assertEquals(21.6, adjusted.getY(), EPSILON); 220 assertEquals(31.25, adjusted.getWidth(), EPSILON); 221 assertEquals(37.6, adjusted.getHeight(), EPSILON); 222 223 adjusted = i1.createAdjustedRectangle( 225 base, LengthAdjustmentType.CONTRACT, LengthAdjustmentType.CONTRACT 226 ); 227 assertEquals(10.9, adjusted.getX(), EPSILON); 228 assertEquals(21.6, adjusted.getY(), EPSILON); 229 assertEquals(28.8, adjusted.getWidth(), EPSILON); 230 assertEquals(37.6, adjusted.getHeight(), EPSILON); 231 232 adjusted = i1.createAdjustedRectangle( 234 base, LengthAdjustmentType.CONTRACT, LengthAdjustmentType.EXPAND 235 ); 236 assertEquals(10.9, adjusted.getX(), EPSILON); 237 assertEquals(18.2978723404, adjusted.getY(), EPSILON); 238 assertEquals(28.8, adjusted.getWidth(), EPSILON); 239 assertEquals(42.553191489, adjusted.getHeight(), EPSILON); 240 241 } 242 243 246 public void testEquals() { 247 RectangleInsets i1 = new RectangleInsets( 248 UnitType.ABSOLUTE, 1.0, 2.0, 3.0, 4.0 249 ); 250 RectangleInsets i2 = new RectangleInsets( 251 UnitType.ABSOLUTE, 1.0, 2.0, 3.0, 4.0 252 ); 253 assertTrue(i1.equals(i2)); 254 assertTrue(i2.equals(i1)); 255 256 i1 = new RectangleInsets(UnitType.RELATIVE, 1.0, 2.0, 3.0, 4.0); 257 assertFalse(i1.equals(i2)); 258 i2 = new RectangleInsets(UnitType.RELATIVE, 1.0, 2.0, 3.0, 4.0); 259 assertTrue(i1.equals(i2)); 260 261 i1 = new RectangleInsets(UnitType.RELATIVE, 0.0, 2.0, 3.0, 4.0); 262 assertFalse(i1.equals(i2)); 263 i2 = new RectangleInsets(UnitType.RELATIVE, 0.0, 2.0, 3.0, 4.0); 264 assertTrue(i1.equals(i2)); 265 266 i1 = new RectangleInsets(UnitType.RELATIVE, 0.0, 0.0, 3.0, 4.0); 267 assertFalse(i1.equals(i2)); 268 i2 = new RectangleInsets(UnitType.RELATIVE, 0.0, 0.0, 3.0, 4.0); 269 assertTrue(i1.equals(i2)); 270 271 i1 = new RectangleInsets(UnitType.RELATIVE, 0.0, 0.0, 0.0, 4.0); 272 assertFalse(i1.equals(i2)); 273 i2 = new RectangleInsets(UnitType.RELATIVE, 0.0, 0.0, 0.0, 4.0); 274 assertTrue(i1.equals(i2)); 275 276 i1 = new RectangleInsets(UnitType.RELATIVE, 0.0, 0.0, 0.0, 0.0); 277 assertFalse(i1.equals(i2)); 278 i2 = new RectangleInsets(UnitType.RELATIVE, 0.0, 0.0, 0.0, 0.0); 279 assertTrue(i1.equals(i2)); 280 281 } 282 283 286 public void testSerialization() { 287 final RectangleInsets i1 = new RectangleInsets( 288 UnitType.ABSOLUTE, 1.0, 2.0, 3.0, 4.0 289 ); 290 RectangleInsets i2 = null; 291 try { 292 final ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 293 final ObjectOutput out = new ObjectOutputStream (buffer); 294 out.writeObject(i1); 295 out.close(); 296 297 final ObjectInput in = new ObjectInputStream ( 298 new ByteArrayInputStream (buffer.toByteArray()) 299 ); 300 i2 = (RectangleInsets) in.readObject(); 301 in.close(); 302 } 303 catch (Exception e) { 304 System.out.println(e.toString()); 305 } 306 assertTrue(i1.equals(i2)); 307 } 308 309 314 public static void main(String [] args) { 315 junit.textui.TestRunner.run(suite()); 316 } 317 318 } 319 | Popular Tags |