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 Denis M. Kishenko
019: * @version $Revision$
020: */package java.awt.geom;
021:
022: public class AreaTest extends PathIteratorTestCase {
023:
024: public AreaTest(String name) {
025: super (name);
026: }
027:
028: @Override
029: protected void setUp() throws Exception {
030: }
031:
032: @Override
033: protected void tearDown() throws Exception {
034: }
035:
036: public void testConstructor() {
037: // Regression test HARMONY-1404
038: try {
039: new Area(null);
040: fail("Expected NPE");
041: } catch (NullPointerException e) {
042: // expected
043: }
044: }
045:
046: public void testContainsPoint() {
047: try {
048: // Regression test HARMONY-1404
049: Area emptyArea = new Area();
050: emptyArea.contains((Point2D) null);
051: fail("Expected NPE");
052: } catch (NullPointerException e) {
053: // expected
054: }
055:
056: Area area = new Area(new Ellipse2D.Double(200, 300, 400, 200));
057: assertTrue(area.contains(250, 350));
058: assertFalse(area.contains(200, 300));
059: assertFalse(area.contains(50, 50));
060:
061: assertTrue(area.contains(new Point2D.Double(500, 400)));
062: assertFalse(area.contains(new Point2D.Double(700, 400)));
063:
064: // Regression test HARMONY-4612
065: GeneralPath path = new GeneralPath();
066: path.moveTo(50, 100);
067: path.lineTo(100, 50);
068: path.lineTo(150, 100);
069: path.lineTo(100, 150);
070: path.closePath();
071:
072: Area areaPath = new Area(path);
073: assertFalse(areaPath.contains(100, 50));
074: }
075:
076: public void testContainsRect() {
077: // Regression test HARMONY-1476
078: GeneralPath path = new GeneralPath();
079: path.moveTo(100, 500);
080: path.lineTo(400, 100);
081: path.lineTo(700, 500);
082: path.closePath();
083:
084: Area area = new Area(path);
085: assertTrue(area.contains(new Rectangle2D.Double(300, 400, 100,
086: 50)));
087: assertFalse(area.contains(new Rectangle2D.Double(50, 400, 700,
088: 50)));
089:
090: GeneralPath path1 = new GeneralPath();
091: path1.moveTo(400, 500);
092: path1.quadTo(200, 200, 400, 100);
093: path1.quadTo(600, 200, 400, 500);
094: path1.closePath();
095:
096: Area area1 = new Area(path1);
097: assertTrue(area1.contains(350, 200, 50, 50));
098: assertFalse(area1.contains(100, 50, 600, 500));
099:
100: // Regression test HARMONY-1404
101: try {
102: Area emptyArea = new Area();
103: emptyArea.contains((Rectangle2D) null);
104: fail("Expected NPE");
105: } catch (NullPointerException e) {
106: // expected
107: }
108: }
109:
110: public void testIntersectsRect() {
111: // Regression test HARMONY-1476
112: GeneralPath path = new GeneralPath();
113: path.moveTo(100, 500);
114: path.lineTo(400, 100);
115: path.lineTo(700, 500);
116: path.closePath();
117:
118: Area area = new Area(path);
119: assertTrue(area.intersects(new Rectangle2D.Double(300, 400,
120: 100, 50)));
121: assertFalse(area.intersects(new Rectangle2D.Double(50, 50, 50,
122: 50)));
123:
124: GeneralPath path1 = new GeneralPath();
125: path1.moveTo(400, 500);
126: path1.quadTo(200, 200, 400, 100);
127: path1.quadTo(600, 200, 400, 500);
128: path1.closePath();
129:
130: Area area1 = new Area(path1);
131: assertTrue(area1.intersects(350, 200, 50, 50));
132: assertFalse(area1.intersects(500, 50, 100, 50));
133:
134: // Regression test HARMONY-1404
135: try {
136: Area emptyArea = new Area();
137: emptyArea.intersects((Rectangle2D) null);
138: fail("Expected NPE");
139: } catch (NullPointerException e) {
140: // expected
141: }
142: }
143:
144: public void testIsRectangle() {
145: // Regression test HARMONY-1476
146: Area area = new Area(new Rectangle2D.Double(200, 300, 400, 150));
147: assertTrue(area.isRectangular());
148:
149: GeneralPath path = new GeneralPath();
150: path.moveTo(200, 300);
151: path.lineTo(600, 300);
152: path.lineTo(600, 450);
153: path.lineTo(200, 450);
154: path.closePath();
155:
156: Area area1 = new Area(path);
157: assertTrue(area1.isRectangular());
158:
159: Area area2 = new Area(new Ellipse2D.Double(200, 300, 400, 150));
160: assertFalse(area2.isRectangular());
161: }
162:
163: public void testGetPathIterator() {
164: // Regression test HARMONY-1860
165: Area a = new Area();
166: PathIterator path = a.getPathIterator(null);
167: checkPathRule(path, PathIterator.WIND_EVEN_ODD);
168: checkPathDone(path, true);
169: }
170:
171: public void testCreateTransformedArea() {
172: // Regression test HARMONY-1880
173: AffineTransform t = AffineTransform.getScaleInstance(2, 3);
174: Area a1 = new Area();
175: Area a2 = a1.createTransformedArea(t);
176: PathIterator path = a2.getPathIterator(null);
177: checkPathRule(path, PathIterator.WIND_EVEN_ODD);
178: checkPathDone(path, true);
179: }
180:
181: public void testSubtract() {
182: // Regression test HARMONY-4410
183: Rectangle2D rect1 = new Rectangle2D.Double(300, 300, 200, 150);
184: Rectangle2D rect2 = new Rectangle2D.Double(350, 200, 300, 150);
185:
186: Area area1 = new Area(rect1);
187: Area area2 = new Area(rect2);
188:
189: Area a = (Area) area1.clone();
190: a.intersect(area2);
191: area1.add(area2);
192: area1.subtract(a);
193:
194: assertFalse(area1.contains(375, 325));
195: assertTrue(area1.contains(600, 300));
196: assertTrue(area1.contains(325, 325));
197: }
198:
199: public void testTransformPathIterator() {
200: // Regression test HARMONY-4680
201: AffineTransform transform = new AffineTransform(2.0, 0.0, 0.0,
202: 200.0 / 140.0, 0.0, 0.0);
203: Area ar = new Area(
204: new Rectangle2D.Double(100, 100, 50.0, 100.0));
205:
206: PathIterator path = ar.getPathIterator(null);
207: PathIterator transformedPath = ar.getPathIterator(transform);
208: double[] coords = new double[6];
209: double[] transformedCoords = new double[6];
210:
211: while (!path.isDone() && !transformedPath.isDone()) {
212: int rule1 = path.currentSegment(coords);
213: int rule2 = transformedPath
214: .currentSegment(transformedCoords);
215: assertTrue(rule1 == rule2);
216: switch (rule1) {
217: case PathIterator.SEG_MOVETO: {
218: transform.transform(coords, 0, coords, 0, 1);
219: assertTrue(coords[0] == transformedCoords[0]
220: && coords[1] == transformedCoords[1]);
221: break;
222: }
223: case PathIterator.SEG_LINETO: {
224: transform.transform(coords, 0, coords, 0, 1);
225: assertTrue(coords[0] == transformedCoords[0]
226: && coords[1] == transformedCoords[1]);
227: break;
228: }
229: case PathIterator.SEG_CLOSE: {
230: break;
231: }
232: }
233: path.next();
234: transformedPath.next();
235: }
236: assertTrue(path.isDone() && transformedPath.isDone());
237: }
238:
239: public static void main(String[] args) {
240: junit.textui.TestRunner.run(AreaTest.class);
241: }
242: }
|