001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dmitry A. Durnev, Denis M. Kishenko
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.awt.Dimension;
023: import java.awt.Point;
024: import java.awt.Rectangle;
025: import java.awt.geom.Rectangle2D;
026:
027: public class RectangleTest extends SerializeTestCase {
028:
029: static {
030: SERIALIZATION_TEST = true;
031: }
032:
033: Rectangle r;
034:
035: public RectangleTest(String name) {
036: super (name);
037: }
038:
039: @Override
040: protected void setUp() throws Exception {
041: super .setUp();
042: r = new Rectangle(1, 2, 3, 4);
043: }
044:
045: @Override
046: protected void tearDown() throws Exception {
047: r = null;
048: super .tearDown();
049: }
050:
051: public void testCreate1() {
052: assertEquals(new Rectangle(0, 0, 0, 0), new Rectangle());
053: }
054:
055: public void testCreate2() {
056: assertEquals(new Rectangle(0, 0, 5, 6), new Rectangle(5, 6));
057: }
058:
059: public void testCreate3() {
060: assertEquals(new Rectangle(0, 0, 5, 6), new Rectangle(
061: new Dimension(5, 6)));
062: }
063:
064: public void testCreate4() {
065: assertEquals(new Rectangle(5, 6, 0, 0), new Rectangle(
066: new Point(5, 6)));
067: }
068:
069: public void testCreate5() {
070: assertEquals(new Rectangle(5, 6, 7, 8), new Rectangle(
071: new Point(5, 6), new Dimension(7, 8)));
072: }
073:
074: public void testCreate6() {
075: assertEquals(new Rectangle(5, 6, 7, 8), new Rectangle(
076: new Rectangle(5, 6, 7, 8)));
077: }
078:
079: public void testGetX() {
080: assertEquals(1, (int) r.getX());
081: }
082:
083: public void testGetY() {
084: assertEquals(2, (int) r.getY());
085: }
086:
087: public void testGetWidth() {
088: assertEquals(3, (int) r.getWidth());
089: }
090:
091: public void testGetHeight() {
092: assertEquals(4, (int) r.getHeight());
093: }
094:
095: public void testIsEmpty() {
096: assertTrue(!new Rectangle(1, 2, 3, 4).isEmpty());
097: assertTrue(new Rectangle(1, 2, -3, 4).isEmpty());
098: assertTrue(new Rectangle(1, 2, 3, -4).isEmpty());
099: assertTrue(new Rectangle(1, 2, -3, -4).isEmpty());
100: }
101:
102: public void testEquals() {
103: assertTrue(r.equals(new Rectangle(1, 2, 3, 4)));
104: assertTrue(!r.equals(new Rectangle(0, 2, 3, 4)));
105: assertTrue(!r.equals(new Rectangle(1, 0, 3, 4)));
106: assertTrue(!r.equals(new Rectangle(1, 2, 0, 4)));
107: assertTrue(!r.equals(new Rectangle(1, 2, 3, 0)));
108: }
109:
110: public void testToString() {
111: assertEquals("java.awt.Rectangle[x=1,y=2,width=3,height=4]", r
112: .toString());
113: }
114:
115: public void testGetSize() {
116: assertEquals(new Dimension(3, 4), r.getSize());
117: }
118:
119: public void testSetSize1() {
120: r.setSize(new Dimension(5, 6));
121: assertEquals(new Dimension(5, 6), r.getSize());
122: }
123:
124: public void testSetSize2() {
125: r.setSize(5, 6);
126: assertEquals(new Dimension(5, 6), r.getSize());
127: }
128:
129: public void testGetLocation() {
130: assertEquals(new Point(1, 2), r.getLocation());
131: }
132:
133: public void testSetLocation1() {
134: r.setLocation(5, 6);
135: assertEquals(new Point(5, 6), r.getLocation());
136: }
137:
138: public void testSetLocation2() {
139: r.setLocation(new Point(5, 6));
140: assertEquals(new Point(5, 6), r.getLocation());
141: }
142:
143: @SuppressWarnings("deprecation")
144: public void testMove() {
145: r.move(5, 6);
146: assertEquals(new Point(5, 6), r.getLocation());
147: }
148:
149: public void testSetRect() {
150: r.setRect(5, 6, 7, 8);
151: assertEquals(new Rectangle(5, 6, 7, 8), r);
152: r.setRect(9.3, 10.7, 11.5, 12.5);
153: assertEquals(new Rectangle(9, 10, 12, 14), r);
154: r.setRect(13.2, 14.7, 15.3, 16.4);
155: assertEquals(new Rectangle(13, 14, 16, 18), r);
156: }
157:
158: @SuppressWarnings("deprecation")
159: public void testResize() {
160: r.resize(5, 6);
161: assertEquals(new Rectangle(1, 2, 5, 6), r);
162: }
163:
164: @SuppressWarnings("deprecation")
165: public void testReshape() {
166: r.reshape(5, 6, 7, 8);
167: assertEquals(new Rectangle(5, 6, 7, 8), r);
168: }
169:
170: public void testGetBounds() {
171: assertEquals(new Rectangle(1, 2, 3, 4), r.getBounds());
172: }
173:
174: public void testGetBounds2D() {
175: assertEquals(new Rectangle(1, 2, 3, 4), r.getBounds2D());
176: }
177:
178: public void setBounds1() {
179: r.setBounds(5, 6, 7, 8);
180: assertEquals(new Rectangle(5, 6, 7, 8), r.getBounds());
181: }
182:
183: public void setBounds2() {
184: r.setBounds(new Rectangle(5, 6, 7, 8));
185: assertEquals(new Rectangle(5, 6, 7, 8), r.getBounds());
186: }
187:
188: public void testGrow() {
189: r.grow(0, 0);
190: assertEquals(new Rectangle(1, 2, 3, 4), r);
191: r.grow(1, 1);
192: assertEquals(new Rectangle(0, 1, 5, 6), r);
193: r.grow(-1, -1);
194: assertEquals(new Rectangle(1, 2, 3, 4), r);
195: }
196:
197: public void testTranslate() {
198: r.translate(0, 0);
199: assertEquals(new Point(1, 2), r.getLocation());
200: r.translate(5, 6);
201: assertEquals(new Point(6, 8), r.getLocation());
202: r.translate(-5, -6);
203: assertEquals(new Point(1, 2), r.getLocation());
204: }
205:
206: public void testAddPoint1() {
207: r.add(0, 0);
208: assertEquals(new Rectangle(0, 0, 4, 6), r);
209: r.add(1, 1);
210: assertEquals(new Rectangle(0, 0, 4, 6), r);
211: r.add(7, 8);
212: assertEquals(new Rectangle(0, 0, 7, 8), r);
213: r.add(5, 9);
214: assertEquals(new Rectangle(0, 0, 7, 9), r);
215: r.add(9, 6);
216: assertEquals(new Rectangle(0, 0, 9, 9), r);
217: }
218:
219: public void testAddPoint2() {
220: r.add(new Point(0, 0));
221: assertEquals(new Rectangle(0, 0, 4, 6), r);
222: r.add(new Point(1, 1));
223: assertEquals(new Rectangle(0, 0, 4, 6), r);
224: r.add(new Point(7, 8));
225: assertEquals(new Rectangle(0, 0, 7, 8), r);
226: r.add(new Point(5, 9));
227: assertEquals(new Rectangle(0, 0, 7, 9), r);
228: r.add(new Point(9, 6));
229: assertEquals(new Rectangle(0, 0, 9, 9), r);
230: }
231:
232: public void testAddRect() {
233: r.add(new Rectangle(1, 2, 3, 4)); // The same
234: assertEquals(new Rectangle(1, 2, 3, 4), r);
235: r.add(new Rectangle(2, 3, 2, 3)); // Inside
236: assertEquals(new Rectangle(1, 2, 3, 4), r);
237: r.add(new Rectangle(0, 0, 1, 2)); // Outside
238: assertEquals(new Rectangle(0, 0, 4, 6), r);
239: r.add(new Rectangle(2, 3, 3, 4)); // Intersect
240: assertEquals(new Rectangle(0, 0, 5, 7), r);
241: r.add(new Rectangle(-1, -1, 6, 8)); // Cover
242: assertEquals(new Rectangle(-1, -1, 6, 8), r);
243: }
244:
245: public void testContainsPoint1() {
246: assertTrue(r.contains(2, 3));
247: assertTrue(!r.contains(0, 0));
248: assertTrue(!r.contains(0, 5));
249: assertTrue(!r.contains(6, 5));
250: assertTrue(!r.contains(6, 0));
251: }
252:
253: public void testContainsPoint2() {
254: assertTrue(r.contains(new Point(2, 3)));
255: assertTrue(!r.contains(new Point(0, 0)));
256: assertTrue(!r.contains(new Point(0, 5)));
257: assertTrue(!r.contains(new Point(6, 5)));
258: assertTrue(!r.contains(new Point(6, 0)));
259: }
260:
261: @SuppressWarnings("deprecation")
262: public void testInside() {
263: assertTrue(r.inside(2, 3));
264: assertTrue(!r.inside(0, 0));
265: assertTrue(!r.inside(0, 7));
266: assertTrue(!r.inside(5, 7));
267: assertTrue(!r.inside(5, 0));
268: }
269:
270: public void testContainsRect1() {
271: assertTrue(!r.contains(0, 0, 0, 0)); // Empty
272: assertTrue(r.contains(1, 2, 3, 4)); // The same
273: assertTrue(r.contains(2, 3, 1, 2)); // Inside
274: assertTrue(!r.contains(5, 7, 1, 2)); // Outside
275: assertTrue(!r.contains(2, 3, 5, 6)); // Intersect
276: assertTrue(!r.contains(0, 0, 5, 6)); // Cover
277: }
278:
279: public void testContainsRect2() {
280: assertTrue(!r.contains(new Rectangle(0, 0, 0, 0))); // Empty
281: assertTrue(r.contains(new Rectangle(1, 2, 3, 4))); // The same
282: assertTrue(r.contains(new Rectangle(2, 3, 1, 2))); // Inside
283: assertTrue(!r.contains(new Rectangle(5, 7, 1, 2))); // Outside
284: assertTrue(!r.contains(new Rectangle(2, 3, 5, 6))); // Intersect
285: assertTrue(!r.contains(new Rectangle(0, 0, 5, 6))); // Cover
286: }
287:
288: public void testIntersects() {
289: assertTrue(!r.intersects(0, 0, 0, 0)); // Empty
290: assertTrue(r.intersects(1, 2, 3, 4)); // The same
291: assertTrue(r.intersects(2, 3, 1, 2)); // Inside
292: assertTrue(!r.intersects(5, 7, 1, 2)); // Outside
293: assertTrue(r.intersects(2, 3, 5, 6)); // Intersect
294: assertTrue(r.intersects(0, 0, 5, 6)); // Cover
295: }
296:
297: public void testIntersection() {
298: assertEquals(new Rectangle(1, 2, -1, -2), r
299: .intersection(new Rectangle())); // Empty
300: assertEquals(new Rectangle(1, 2, 3, 4), r
301: .intersection(new Rectangle(1, 2, 3, 4))); // The same
302: assertEquals(new Rectangle(2, 3, 1, 2), r
303: .intersection(new Rectangle(2, 3, 1, 2))); // Inside
304: assertEquals(new Rectangle(5, 7, -1, -1), r
305: .intersection(new Rectangle(5, 7, 1, 2))); // Outside
306: assertEquals(new Rectangle(2, 3, 2, 3), r
307: .intersection(new Rectangle(2, 3, 5, 6))); // Intersect
308: assertEquals(new Rectangle(1, 2, 3, 4), r
309: .intersection(new Rectangle(0, 0, 5, 6))); // Cover
310: }
311:
312: public void testCreateIntersection1() {
313: // assertEquals(r.createIntersection(new Rectangle()), new Rectangle()); // Empty
314: assertEquals(new Rectangle(1, 2, 3, 4), r
315: .createIntersection(new Rectangle(1, 2, 3, 4))); // The same
316: assertEquals(new Rectangle(2, 3, 1, 2), r
317: .createIntersection(new Rectangle(2, 3, 1, 2))); // Inside
318: assertEquals(new Rectangle(5, 7, -1, -1), r
319: .createIntersection(new Rectangle(5, 7, 1, 2))); // Outside
320: assertEquals(new Rectangle(2, 3, 2, 3), r
321: .createIntersection(new Rectangle(2, 3, 5, 6))); // Intersect
322: assertEquals(new Rectangle(1, 2, 3, 4), r
323: .createIntersection(new Rectangle(0, 0, 5, 6))); // Cover
324: }
325:
326: public void testCreateIntersection2() {
327: // assertEquals(r.createIntersection(new Rectangle2D.Double()), new Rectangle2D.Double()); // Empty
328: assertEquals(new Rectangle2D.Double(1, 2, 3, 4), r
329: .createIntersection(new Rectangle2D.Double(1, 2, 3, 4))); // The same
330: assertEquals(new Rectangle2D.Double(2, 3, 1, 2), r
331: .createIntersection(new Rectangle2D.Double(2, 3, 1, 2))); // Inside
332: assertEquals(new Rectangle2D.Double(5, 7, -1, -1), r
333: .createIntersection(new Rectangle2D.Double(5, 7, 1, 2))); // Outside
334: assertEquals(new Rectangle2D.Double(2, 3, 2, 3), r
335: .createIntersection(new Rectangle2D.Double(2, 3, 5, 6))); // Intersect
336: assertEquals(new Rectangle2D.Double(1, 2, 3, 4), r
337: .createIntersection(new Rectangle2D.Double(0, 0, 5, 6))); // Cover
338: }
339:
340: public void testOutcode() {
341: assertEquals(Rectangle2D.OUT_LEFT | Rectangle2D.OUT_TOP, r
342: .outcode(0, 0));
343: assertEquals(Rectangle2D.OUT_TOP, r.outcode(2, 0));
344: assertEquals(Rectangle2D.OUT_RIGHT | Rectangle2D.OUT_TOP, r
345: .outcode(5, 0));
346: assertEquals(Rectangle2D.OUT_RIGHT, r.outcode(5, 3));
347: assertEquals(Rectangle2D.OUT_RIGHT | Rectangle2D.OUT_BOTTOM, r
348: .outcode(5, 7));
349: assertEquals(Rectangle2D.OUT_BOTTOM, r.outcode(2, 7));
350: assertEquals(Rectangle2D.OUT_LEFT | Rectangle2D.OUT_BOTTOM, r
351: .outcode(0, 7));
352: assertEquals(Rectangle2D.OUT_LEFT, r.outcode(0, 3));
353: assertEquals(0, r.outcode(2, 3));
354: }
355:
356: public void testUnion() {
357: // assertEquals(r.createUnion(new Rectangle()), new Rectangle(1, 2, 3, 4)); // Empty
358: assertEquals(new Rectangle(1, 2, 3, 4), r
359: .createUnion(new Rectangle(1, 2, 3, 4))); // The same
360: assertEquals(new Rectangle(1, 2, 3, 4), r
361: .createUnion(new Rectangle(2, 3, 1, 2))); // Inside
362: assertEquals(new Rectangle(1, 2, 5, 7), r
363: .createUnion(new Rectangle(5, 7, 1, 2))); // Outside
364: assertEquals(new Rectangle(1, 2, 6, 7), r
365: .createUnion(new Rectangle(2, 3, 5, 6))); // Intersect
366: assertEquals(new Rectangle(0, 0, 5, 6), r
367: .createUnion(new Rectangle(0, 0, 5, 6))); // Cover
368: }
369:
370: public void testCreateUnion() {
371: // assertEquals(r.createUnion(new Rectangle2D.Double()), new Rectangle2D.Double(1, 2, 3, 4)); // Empty
372: assertEquals(new Rectangle2D.Double(1, 2, 3, 4), r
373: .createUnion(new Rectangle2D.Double(1, 2, 3, 4))); // The same
374: assertEquals(new Rectangle2D.Double(1, 2, 3, 4), r
375: .createUnion(new Rectangle2D.Double(2, 3, 1, 2))); // Inside
376: assertEquals(new Rectangle2D.Double(1, 2, 5, 7), r
377: .createUnion(new Rectangle2D.Double(5, 7, 1, 2))); // Outside
378: assertEquals(new Rectangle2D.Double(1, 2, 6, 7), r
379: .createUnion(new Rectangle2D.Double(2, 3, 5, 6))); // Intersect
380: assertEquals(new Rectangle2D.Double(0, 0, 5, 6), r
381: .createUnion(new Rectangle2D.Double(0, 0, 5, 6))); // Cover
382: }
383:
384: public void testSerializeRead1() {
385: checkRead(new Rectangle());
386: }
387:
388: public void testSerializeRead2() {
389: checkRead(new Rectangle(1, 2));
390: }
391:
392: public void testSerializeRead3() {
393: checkRead(new Rectangle(1, 2, 3, 4));
394: }
395:
396: public void testSerializeWrite1() {
397: checkWrite(new Rectangle());
398: }
399:
400: public void testSerializeWrite2() {
401: checkWrite(new Rectangle(1, 2));
402: }
403:
404: public void testSerializeWrite3() {
405: checkWrite(new Rectangle(1, 2, 3, 4));
406: }
407:
408: public void createSerializeTemplates() {
409: saveSerialized(new Rectangle());
410: saveSerialized(new Rectangle(1, 2));
411: saveSerialized(new Rectangle(1, 2, 3, 4));
412: }
413:
414: public static void main(String[] args) {
415: // new RectangleTest("").createSerializeTemplates();
416: junit.textui.TestRunner.run(RectangleTest.class);
417: }
418:
419: }
|