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;
021:
022: import java.awt.Point;
023: import java.awt.Polygon;
024: import java.awt.Rectangle;
025: import java.awt.geom.AffineTransform;
026: import java.awt.geom.PathIterator;
027: import java.awt.geom.Rectangle2D;
028: import java.awt.geom.ShapeTestCase;
029:
030: public class PolygonTest extends ShapeTestCase {
031:
032: static {
033: SERIALIZATION_TEST = true;
034: }
035:
036: public PolygonTest(String name) {
037: super (name);
038: // filterImage = createFilter("^(polygon).*([.]ico)$", "(.*)((affine)|(flat)|(bounds))(.*)");
039: }
040:
041: public void testCreate1() {
042: Polygon pg = new Polygon();
043: assertEquals(0, pg.npoints);
044: assertNotNull(pg.xpoints);
045: assertNotNull(pg.ypoints);
046: }
047:
048: public void testCreate2() {
049: int[] x = new int[] { 1, 2, 3, 4, 5, 6 };
050: int[] y = new int[] { 7, 8, 9, 10, 11, 12 };
051:
052: Polygon pg = new Polygon(x, y, 0);
053: assertEquals(0, pg.npoints);
054: assertEquals(0, pg.xpoints.length);
055: assertEquals(0, pg.ypoints.length);
056:
057: pg = new Polygon(x, y, 3);
058: assertEquals(3, pg.npoints);
059: assertEquals(new int[] { 1, 2, 3 }, pg.xpoints, 3);
060: assertEquals(new int[] { 7, 8, 9 }, pg.ypoints, 3);
061: assertEquals(3, pg.xpoints.length);
062: assertEquals(3, pg.ypoints.length);
063:
064: // Regression for HARMONY-1445
065: x[1] = 99;
066: y[2] = 77;
067: assertTrue(pg.xpoints[1] != 99);
068: assertTrue(pg.ypoints[2] != 77);
069: }
070:
071: public void testCreate3() {
072: try {
073: new Polygon(null, new int[] { 1, 2 }, 2);
074: fail("Expected exception NullPointerException");
075: } catch (NullPointerException e) {
076: }
077:
078: try {
079: new Polygon(new int[] { 1, 2 }, null, 2);
080: fail("Expected exception NullPointerException");
081: } catch (NullPointerException e) {
082: }
083:
084: try {
085: new Polygon(new int[] { 1, 2 }, new int[] { 1, 2, 4, 5 }, 3);
086: fail("Expected exception IndexOutOfBoundsException");
087: } catch (IndexOutOfBoundsException e) {
088: }
089:
090: try {
091: new Polygon(new int[] { 1, 2 }, new int[] { 1, 2, 4, 5 }, 5);
092: fail("Expected exception IndexOutOfBoundsException");
093: } catch (IndexOutOfBoundsException e) {
094: }
095:
096: try {
097: new Polygon(new int[] { 1, 2 }, new int[] { 1, 2, 4, 5 },
098: -1);
099: fail("Expected exception NegativeArraySizeException");
100: } catch (NegativeArraySizeException e) {
101: }
102: }
103:
104: public void testReset() {
105: Polygon pg = new Polygon(new int[] { 1, 2, 3, 4, 5, 6 },
106: new int[] { 7, 8, 9, 10, 11, 12 }, 3);
107: assertTrue(pg.npoints > 0);
108: pg.reset();
109: assertTrue(pg.npoints == 0);
110: }
111:
112: public void testInvalidate() {
113: Polygon pg = new Polygon(new int[] { 1, 2, 3 }, new int[] { 4,
114: 5, 6 }, 3);
115: assertEquals(new Rectangle(1, 4, 2, 2), pg.getBounds());
116: pg.xpoints[0] = 0;
117: pg.ypoints[0] = -1;
118: assertEquals(new Rectangle(1, 4, 2, 2), pg.getBounds());
119: pg.invalidate();
120: assertEquals(new Rectangle(0, -1, 3, 7), pg.getBounds());
121: }
122:
123: public void testAddPoint() {
124: Polygon pg = new Polygon(new int[] { 1, 2, 3, 4, 5, 6 },
125: new int[] { 7, 8, 9, 10, 11, 12 }, 3);
126: assertEquals(3, pg.npoints);
127: pg.addPoint(13, 14);
128: assertEquals(4, pg.npoints);
129: assertEquals(13, pg.xpoints[pg.npoints - 1]);
130: assertEquals(14, pg.ypoints[pg.npoints - 1]);
131: }
132:
133: public void testGetBounds() {
134: Polygon pg = new Polygon();
135: assertEquals(new Rectangle(), pg.getBounds());
136: pg.addPoint(1, 2);
137: assertEquals(new Rectangle(1, 2, 0, 0), pg.getBounds());
138: pg.addPoint(3, 5);
139: assertEquals(new Rectangle(1, 2, 2, 3), pg.getBounds());
140: pg.addPoint(4, 1);
141: assertEquals(new Rectangle(1, 1, 3, 4), pg.getBounds());
142: pg.translate(0, 0);
143: assertEquals(new Rectangle(1, 1, 3, 4), pg.getBounds());
144: pg.translate(2, 3);
145: assertEquals(new Rectangle(3, 4, 3, 4), pg.getBounds());
146: }
147:
148: @SuppressWarnings("deprecation")
149: public void testGetBoundingBox() {
150: Polygon pg = new Polygon();
151: assertEquals(new Rectangle(), pg.getBoundingBox());
152: pg.addPoint(1, 2);
153: assertEquals(new Rectangle(1, 2, 0, 0), pg.getBoundingBox());
154: pg.addPoint(3, 5);
155: assertEquals(new Rectangle(1, 2, 2, 3), pg.getBoundingBox());
156: pg.addPoint(4, 1);
157: assertEquals(new Rectangle(1, 1, 3, 4), pg.getBoundingBox());
158: pg.translate(0, 0);
159: assertEquals(new Rectangle(1, 1, 3, 4), pg.getBoundingBox());
160: pg.translate(2, 3);
161: assertEquals(new Rectangle(3, 4, 3, 4), pg.getBoundingBox());
162: }
163:
164: public void testGetBounds2D() {
165: Polygon pg = new Polygon();
166: assertEquals(new Rectangle2D.Double(), pg.getBounds2D());
167: pg.addPoint(1, 2);
168: assertEquals(new Rectangle2D.Double(1, 2, 0, 0), pg
169: .getBounds2D());
170: pg.addPoint(3, 5);
171: assertEquals(new Rectangle2D.Double(1, 2, 2, 3), pg
172: .getBounds2D());
173: pg.addPoint(4, 1);
174: assertEquals(new Rectangle2D.Double(1, 1, 3, 4), pg
175: .getBounds2D());
176: pg.translate(0, 0);
177: assertEquals(new Rectangle(1, 1, 3, 4), pg.getBounds2D());
178: pg.translate(2, 3);
179: assertEquals(new Rectangle(3, 4, 3, 4), pg.getBounds2D());
180: }
181:
182: public void testTranslate() {
183: Polygon pg = new Polygon(new int[] { 1, 2, 3 }, new int[] { 7,
184: 8, 9 }, 3);
185: pg.translate(0, 0);
186: assertEquals(3, pg.npoints);
187: assertEquals(new int[] { 1, 2, 3 }, pg.xpoints, 3);
188: assertEquals(new int[] { 7, 8, 9 }, pg.ypoints, 3);
189:
190: pg.translate(2, 3);
191: assertEquals(3, pg.npoints);
192: assertEquals(new int[] { 3, 4, 5 }, pg.xpoints, 3);
193: assertEquals(new int[] { 10, 11, 12 }, pg.ypoints, 3);
194: }
195:
196: @SuppressWarnings("deprecation")
197: public void testInside() {
198: Polygon pg = new Polygon();
199: pg.addPoint(1, 2);
200: pg.addPoint(3, 5);
201: pg.addPoint(4, 1);
202: assertTrue(pg.inside(2, 2));
203: assertFalse(pg.inside(5, 5));
204: }
205:
206: public void testContains1() {
207: Polygon pg = new Polygon();
208: pg.addPoint(1, 2);
209: pg.addPoint(3, 5);
210: pg.addPoint(4, 1);
211: assertTrue(pg.contains(2, 2));
212: assertFalse(pg.contains(5, 5));
213: }
214:
215: public void testContains2() {
216: Polygon pg = new Polygon();
217: pg.addPoint(1, 2);
218: pg.addPoint(3, 5);
219: pg.addPoint(4, 1);
220: assertTrue(pg.contains(new Point(2, 2)));
221: assertFalse(pg.contains(new Point(5, 5)));
222: }
223:
224: void checkPathIteratorDouble(PathIterator p, double[] values) {
225: checkPathRule(p, PathIterator.WIND_EVEN_ODD);
226: checkPathMove(p, false, values[0], values[1], 0.0);
227: for (int i = 2; i < values.length;) {
228: checkPathLine(p, false, values[i++], values[i++], 0.0);
229: }
230: checkPathClose(p, true);
231: }
232:
233: void checkPathIteratorFloat(PathIterator p, float[] values) {
234: checkPathRule(p, PathIterator.WIND_EVEN_ODD);
235: checkPathMove(p, false, values[0], values[1], 0.0f);
236: for (int i = 2; i < values.length;) {
237: checkPathLine(p, false, values[i++], values[i++], 0.0f);
238: }
239: checkPathClose(p, true);
240: }
241:
242: public void testGetPathIteratorDouble() {
243: Polygon pg = new Polygon(new int[] { 1, 2, 3 }, new int[] { 4,
244: 5, 6 }, 3);
245: checkPathIteratorDouble(pg.getPathIterator(null), new double[] {
246: 1, 4, 2, 5, 3, 6 });
247: }
248:
249: public void testGetPathIteratorFloat() {
250: Polygon pg = new Polygon(new int[] { 1, 2, 3 }, new int[] { 4,
251: 5, 6 }, 3);
252: checkPathIteratorFloat(pg.getPathIterator(null), new float[] {
253: 1, 4, 2, 5, 3, 6 });
254: }
255:
256: public void testGetPathIteratorDoubleFlat() {
257: Polygon pg = new Polygon(new int[] { 1, 2, 3 }, new int[] { 4,
258: 5, 6 }, 3);
259: checkPathIteratorDouble(pg.getPathIterator(null, 2),
260: new double[] { 1, 4, 2, 5, 3, 6 });
261: }
262:
263: public void testGetPathIteratorFloatFlat() {
264: Polygon pg = new Polygon(new int[] { 1, 2, 3 }, new int[] { 4,
265: 5, 6 }, 3);
266: checkPathIteratorFloat(pg.getPathIterator(null, 5),
267: new float[] { 1, 4, 2, 5, 3, 6 });
268: }
269:
270: public void testGetPathIteratorDoubleAffine() {
271: Polygon pg = new Polygon(new int[] { 1, 2, 3 }, new int[] { 4,
272: 5, 6 }, 3);
273: checkPathIteratorDouble(pg.getPathIterator(AffineTransform
274: .getTranslateInstance(3, 1)), new double[] { 4, 5, 5,
275: 6, 6, 7 });
276: }
277:
278: public void testGetPathIteratorFloatAffine() {
279: Polygon pg = new Polygon(new int[] { 1, 2, 3 }, new int[] { 4,
280: 5, 6 }, 3);
281: checkPathIteratorFloat(pg.getPathIterator(AffineTransform
282: .getTranslateInstance(3, 1)), new float[] { 4, 5, 5, 6,
283: 6, 7 });
284: }
285:
286: public void testGetPathIteratorEmpty() {
287: // Regression for HARMONY-1572
288: Polygon pg = new Polygon(new int[] { 1, 2, 3 }, new int[] { 4,
289: 5, 6 }, 0);
290: PathIterator p = pg.getPathIterator(null);
291: checkPathRule(p, PathIterator.WIND_EVEN_ODD);
292: checkPathDone(p, true);
293: }
294:
295: @Override
296: public String objToStr(Object obj) {
297: Polygon p = (Polygon) obj;
298: String data = p.npoints + " [";
299: Rectangle bounds = p.getBounds();
300: if (bounds == null) {
301: data += "null";
302: } else {
303: data += bounds.x + "," + bounds.y + "," + bounds.width
304: + "," + bounds.height;
305: }
306: data += "] (";
307: for (int i = 0; i < p.npoints; i++) {
308: data += p.xpoints[i];
309: if (i < p.npoints - 1) {
310: data += ",";
311: }
312: }
313: data += ") (";
314: for (int i = 0; i < p.npoints; i++) {
315: data += p.ypoints[i];
316: if (i < p.npoints - 1) {
317: data += ",";
318: }
319: }
320: data += ")";
321: return obj.getClass().getName() + "[" + data + "]";
322: }
323:
324: public void testSerializeRead1() {
325: checkRead(new Polygon());
326: }
327:
328: public void testSerializeRead2() {
329: checkRead(new Polygon(new int[] { 1, 2, 3 }, new int[] { 4, 5,
330: 6 }, 3));
331: }
332:
333: public void testSerializeWrite1() {
334: checkWrite(new Polygon());
335: }
336:
337: public void testSerializeWrite2() {
338: checkWrite(new Polygon(new int[] { 1, 2, 3 }, new int[] { 4, 5,
339: 6 }, 3));
340: }
341:
342: public void createSerializeTemplates() {
343: saveSerialized(new Polygon());
344: saveSerialized(new Polygon(new int[] { 1, 2, 3 }, new int[] {
345: 4, 5, 6 }, 3));
346: }
347:
348: public static void main(String[] args) {
349: // new PolygonTest("").createSerializeTemplates();
350: junit.textui.TestRunner.run(PolygonTest.class);
351: }
352:
353: }
|