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: import java.awt.Rectangle;
023: import java.awt.Shape;
024: import java.awt.Tools;
025:
026: public class GeneralPathTest extends ShapeTestCase {
027:
028: // Test append
029: float[][][][] shapes1 = new float[][][][] {
030: // Source shape, shape to append, result shape, flag connect
031: // Null means empty GeneralPath
032: { null, null, null, { { 0 } } },
033: { null, null, null, { { 1 } } },
034:
035: { null, { { PathIterator.SEG_MOVETO, 1, 2 } },
036: { { PathIterator.SEG_MOVETO, 1, 2 } }, { { 0 } } },
037: { null, { { PathIterator.SEG_MOVETO, 1, 2 } },
038: { { PathIterator.SEG_MOVETO, 1, 2 } }, { { 1 } } },
039:
040: { { { PathIterator.SEG_MOVETO, 1, 2 } }, null,
041: { { PathIterator.SEG_MOVETO, 1, 2 } }, { { 0 } } },
042: { { { PathIterator.SEG_MOVETO, 1, 2 } }, null,
043: { { PathIterator.SEG_MOVETO, 1, 2 } }, { { 1 } } },
044:
045: {
046: { { PathIterator.SEG_MOVETO, 1, 2 } },
047:
048: { { PathIterator.SEG_MOVETO, 5, 6 },
049: { PathIterator.SEG_LINETO, 7, 8 } },
050:
051: { { PathIterator.SEG_MOVETO, 1, 2 },
052: { PathIterator.SEG_MOVETO, 5, 6 },
053: { PathIterator.SEG_LINETO, 7, 8 } },
054: { { 0 } } },
055:
056: {
057: { { PathIterator.SEG_MOVETO, 1, 2 } },
058:
059: { { PathIterator.SEG_MOVETO, 5, 6 },
060: { PathIterator.SEG_LINETO, 7, 8 } },
061:
062: { { PathIterator.SEG_MOVETO, 1, 2 },
063: { PathIterator.SEG_LINETO, 5, 6 },
064: { PathIterator.SEG_LINETO, 7, 8 } },
065: { { 1 } } },
066:
067: {
068: { { PathIterator.SEG_MOVETO, 1, 2 },
069: { PathIterator.SEG_LINETO, 3, 4 } },
070:
071: { { PathIterator.SEG_MOVETO, 5, 6 },
072: { PathIterator.SEG_LINETO, 7, 8 } },
073:
074: { { PathIterator.SEG_MOVETO, 1, 2 },
075: { PathIterator.SEG_LINETO, 3, 4 },
076: { PathIterator.SEG_MOVETO, 5, 6 },
077: { PathIterator.SEG_LINETO, 7, 8 } },
078: { { 0 } } },
079:
080: {
081: { { PathIterator.SEG_MOVETO, 1, 2 },
082: { PathIterator.SEG_LINETO, 3, 4 } },
083:
084: { { PathIterator.SEG_MOVETO, 5, 6 },
085: { PathIterator.SEG_LINETO, 7, 8 } },
086:
087: { { PathIterator.SEG_MOVETO, 1, 2 },
088: { PathIterator.SEG_LINETO, 3, 4 },
089: { PathIterator.SEG_LINETO, 5, 6 },
090: { PathIterator.SEG_LINETO, 7, 8 } },
091: { { 1 } } } };
092:
093: // Test transform/createTransformedShape
094: float[][][][] shapes2 = new float[][][][] {
095: // Source path, transform matrix, result path
096: {
097: { { PathIterator.SEG_MOVETO, 1, 2 },
098: { PathIterator.SEG_LINETO, 3, 4 },
099: { PathIterator.SEG_LINETO, 5, 6 } },
100: { { 2, 0, 0, 2, 0, 0 } },
101: { { PathIterator.SEG_MOVETO, 2, 4 },
102: { PathIterator.SEG_LINETO, 6, 8 },
103: { PathIterator.SEG_LINETO, 10, 12 } } } };
104:
105: // Test getBounds/getBounds2D
106: float[][][][] bounds = new float[][][][] {
107: // Source path, boundary
108: { null, { { 0, 0, 0, 0 } } },
109:
110: { { { PathIterator.SEG_MOVETO, 1, 2 } }, { { 1, 2, 0, 0 } } },
111:
112: {
113: { { PathIterator.SEG_MOVETO, 1, 2 },
114: { PathIterator.SEG_LINETO, 3, 4 } },
115: { { 1, 2, 2, 2 } } },
116:
117: {
118: { { PathIterator.SEG_MOVETO, 1, 2 },
119: { PathIterator.SEG_LINETO, 3, 4 },
120: { PathIterator.SEG_LINETO, 5, 1 } },
121: { { 1, 1, 4, 3 } } }
122:
123: };
124:
125: GeneralPath g;
126:
127: public GeneralPathTest(String name) {
128: super (name);
129: }
130:
131: @Override
132: protected void setUp() throws Exception {
133: super .setUp();
134: g = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
135: }
136:
137: @Override
138: protected void tearDown() throws Exception {
139: super .tearDown();
140: }
141:
142: public void testCreate1() {
143: assertEquals(new GeneralPath(), new GeneralPath(
144: GeneralPath.WIND_NON_ZERO), 0.0);
145: assertEquals(new GeneralPath(), new GeneralPath(
146: GeneralPath.WIND_NON_ZERO), 0.0f);
147: }
148:
149: public void testCreate2() {
150: assertEquals(new GeneralPath(GeneralPath.WIND_NON_ZERO, 20),
151: new GeneralPath(GeneralPath.WIND_NON_ZERO), 0.0);
152: assertEquals(new GeneralPath(GeneralPath.WIND_NON_ZERO, 20),
153: new GeneralPath(GeneralPath.WIND_NON_ZERO), 0.0f);
154: }
155:
156: public void testCreate3() {
157: g.moveTo(5, 6);
158: g.lineTo(7, 8);
159: assertEquals(g, new GeneralPath(g), 0.0);
160: assertEquals(g, new GeneralPath(g), 0.0f);
161: }
162:
163: public void testConstants() {
164: assertEquals("WIND_EVEN_ODD", PathIterator.WIND_EVEN_ODD,
165: GeneralPath.WIND_EVEN_ODD);
166: assertEquals("WIND_NON_ZERO", PathIterator.WIND_NON_ZERO,
167: GeneralPath.WIND_NON_ZERO);
168: }
169:
170: public void testGetWindingRule() {
171: assertEquals("Rule", GeneralPath.WIND_EVEN_ODD, g
172: .getWindingRule());
173: }
174:
175: public void testSetWindingRule() {
176: g.setWindingRule(GeneralPath.WIND_NON_ZERO);
177: assertEquals("Rule", GeneralPath.WIND_NON_ZERO, g
178: .getWindingRule());
179: g.setWindingRule(GeneralPath.WIND_EVEN_ODD);
180: assertEquals("Rule", GeneralPath.WIND_EVEN_ODD, g
181: .getWindingRule());
182: try {
183: g.setWindingRule(-1);
184: fail("Expected IllegalArgumentException");
185: } catch (IllegalArgumentException e) {
186: // IT'S OK
187: }
188: }
189:
190: public void testIllegalPathStateException() {
191: try {
192: g.lineTo(10, 20);
193: fail("GeneralPath.lineTo() should throw exception IllegalPathStateException");
194: } catch (IllegalPathStateException e) {
195: }
196: try {
197: g.quadTo(10, 20, 30, 40);
198: fail("GeneralPath.quadTo() should throw exception IllegalPathStateException");
199: } catch (IllegalPathStateException e) {
200: }
201: try {
202: g.curveTo(10, 20, 30, 40, 50, 60);
203: fail("GeneralPath.curveTo() should throw exception IllegalPathStateException");
204: } catch (IllegalPathStateException e) {
205: }
206: try {
207: g.closePath();
208: fail("GeneralPath.closePath() should throw exception IllegalPathStateException");
209: } catch (IllegalPathStateException e) {
210: }
211: }
212:
213: public void testMoveToDouble() {
214: g.moveTo(10, 20);
215: PathIterator p = g.getPathIterator(null);
216: checkPathMove(p, true, 10, 20, 0.0);
217: }
218:
219: public void testMoveToFloat() {
220: g.moveTo(10, 20);
221: PathIterator p = g.getPathIterator(null);
222: checkPathMove(p, true, 10, 20, 0.0f);
223: }
224:
225: public void testLineToDouble() {
226: g.moveTo(10, 20);
227: g.lineTo(30, 40);
228: PathIterator p = g.getPathIterator(null);
229: checkPathMove(p, false, 10, 20, 0.0);
230: checkPathLine(p, true, 30, 40, 0.0);
231: }
232:
233: public void testLineToFloat() {
234: g.moveTo(10, 20);
235: g.lineTo(30, 40);
236: PathIterator p = g.getPathIterator(null);
237: checkPathMove(p, false, 10, 20, 0.0f);
238: checkPathLine(p, true, 30, 40, 0.0f);
239: }
240:
241: public void testQuadToDouble() {
242: g.moveTo(10, 20);
243: g.quadTo(30, 40, 50, 60);
244: PathIterator p = g.getPathIterator(null);
245: checkPathMove(p, false, 10, 20, 0.0);
246: checkPathQuad(p, true, 30, 40, 50, 60, 0.0);
247: }
248:
249: public void testQuadToFloat() {
250: g.moveTo(10, 20);
251: g.quadTo(30, 40, 50, 60);
252: PathIterator p = g.getPathIterator(null);
253: checkPathMove(p, false, 10, 20, 0.0f);
254: checkPathQuad(p, true, 30, 40, 50, 60, 0.0f);
255: }
256:
257: public void testCurveToDouble() {
258: g.moveTo(10, 20);
259: g.curveTo(30, 40, 50, 60, 70, 80);
260: PathIterator p = g.getPathIterator(null);
261: checkPathMove(p, false, 10, 20, 0.0);
262: checkPathCubic(p, true, 30, 40, 50, 60, 70, 80, 0.0);
263: }
264:
265: public void testCurveToFloat() {
266: g.moveTo(10, 20);
267: g.curveTo(30, 40, 50, 60, 70, 80);
268: PathIterator p = g.getPathIterator(null);
269: checkPathMove(p, false, 10, 20, 0.0f);
270: checkPathCubic(p, true, 30, 40, 50, 60, 70, 80, 0.0f);
271: }
272:
273: public void testClosePathDouble() {
274: g.moveTo(10, 20);
275: g.closePath();
276: PathIterator p = g.getPathIterator(null);
277: checkPathMove(p, false, 10, 20, 0.0);
278: checkPathClose(p, true);
279: }
280:
281: public void testClosePathFloat() {
282: g.moveTo(10, 20);
283: g.closePath();
284: PathIterator p = g.getPathIterator(null);
285: checkPathMove(p, false, 10, 20, 0.0f);
286: checkPathClose(p, true);
287: }
288:
289: public void testClosePath2() {
290: g.moveTo(10, 20);
291: g.closePath();
292: g.closePath();
293: PathIterator p = g.getPathIterator(null);
294: checkPathMove(p, false, 10, 20, 0.0);
295: checkPathClose(p, true);
296: }
297:
298: public void testClosePath3() {
299: g.moveTo(10, 20);
300: g.lineTo(30, 40);
301: g.closePath();
302: g.closePath();
303: PathIterator p = g.getPathIterator(null);
304: checkPathMove(p, false, 10, 20, 0.0);
305: checkPathLine(p, false, 30, 40, 0.0);
306: checkPathClose(p, true);
307: }
308:
309: GeneralPath createPath(float[][] segments) {
310: GeneralPath p = new GeneralPath();
311: if (segments != null) {
312: for (float[] element : segments) {
313: switch ((int) element[0]) {
314: case PathIterator.SEG_MOVETO:
315: p.moveTo(element[1], element[2]);
316: break;
317: case PathIterator.SEG_LINETO:
318: p.lineTo(element[1], element[2]);
319: break;
320: case PathIterator.SEG_QUADTO:
321: p.quadTo(element[1], element[2], element[3],
322: element[4]);
323: break;
324: case PathIterator.SEG_CUBICTO:
325: p.curveTo(element[1], element[2], element[3],
326: element[4], element[5], element[6]);
327: break;
328: case PathIterator.SEG_CLOSE:
329: p.closePath();
330: break;
331: }
332: }
333: }
334: return p;
335: }
336:
337: public void testAppendShape() {
338: for (float[][][] element : shapes1) {
339: GeneralPath src1 = createPath(element[0]);
340: GeneralPath src2 = createPath(element[1]);
341: GeneralPath dst = createPath(element[2]);
342: boolean connect = element[3][0][0] == 1;
343: src1.append(src2, connect);
344: assertEquals(dst, src1, 0.0);
345: assertEquals(dst, src1, 0.0f);
346: }
347: }
348:
349: public void testAppendPath() {
350: for (float[][][] element : shapes1) {
351: GeneralPath src1 = createPath(element[0]);
352: GeneralPath src2 = createPath(element[1]);
353: GeneralPath dst = createPath(element[2]);
354: boolean connect = element[3][0][0] == 1;
355: src1.append(src2.getPathIterator(null), connect);
356: assertEquals(dst, src1, 0.0);
357: assertEquals(dst, src1, 0.0f);
358: }
359: }
360:
361: public void testGetCurrentPoint() {
362: assertNull(g.getCurrentPoint());
363: g.moveTo(10, 20);
364: assertEquals(new Point2D.Float(10, 20), g.getCurrentPoint());
365: g.lineTo(30, 40);
366: assertEquals(new Point2D.Float(30, 40), g.getCurrentPoint());
367: g.quadTo(50, 60, 70, 80);
368: assertEquals(new Point2D.Float(70, 80), g.getCurrentPoint());
369: g.curveTo(90, 100, 110, 120, 130, 140);
370: assertEquals(new Point2D.Float(130, 140), g.getCurrentPoint());
371: g.closePath();
372: assertEquals(new Point2D.Float(10, 20), g.getCurrentPoint());
373: g.moveTo(150, 160);
374: assertEquals(new Point2D.Float(150, 160), g.getCurrentPoint());
375: g.lineTo(170, 180);
376: assertEquals(new Point2D.Float(170, 180), g.getCurrentPoint());
377: g.closePath();
378: assertEquals(new Point2D.Float(150, 160), g.getCurrentPoint());
379: }
380:
381: public void testReset1() {
382: g.moveTo(10, 20);
383: g.lineTo(30, 40);
384: g.closePath();
385: g.quadTo(50, 60, 70, 80);
386: g.reset();
387: PathIterator p = g.getPathIterator(null);
388: assertTrue(p.isDone());
389: }
390:
391: public void testReset2() {
392: g.moveTo(10, 20);
393: g.lineTo(30, 40);
394: g.closePath();
395: g.quadTo(50, 60, 70, 80);
396: g.reset();
397: g.moveTo(10, 20);
398: g.curveTo(60, 70, 80, 90, 100, 110);
399: PathIterator p = g.getPathIterator(null);
400: checkPathMove(p, false, 10, 20, 0.0);
401: checkPathCubic(p, true, 60, 70, 80, 90, 100, 110, 0.0);
402: }
403:
404: public void testTransform() {
405: for (float[][][] element : shapes2) {
406: GeneralPath src = createPath(element[0]);
407: GeneralPath dst = createPath(element[2]);
408: AffineTransform t = new AffineTransform(element[1][0]);
409: src.transform(t);
410: assertEquals(dst, src, 0.0);
411: assertEquals(dst, src, 0.0f);
412: }
413: }
414:
415: public void testCreateTransformedShape() {
416: for (float[][][] element : shapes2) {
417: GeneralPath src = createPath(element[0]);
418: Shape dst1 = createPath(element[2]);
419: AffineTransform t = new AffineTransform(element[1][0]);
420: Shape dst2 = src.createTransformedShape(t);
421: assertTrue(Tools.Shape.equals(dst1, dst2, 0.0));
422: assertTrue(Tools.Shape.equals(dst1, dst2, 0.0f));
423: }
424: }
425:
426: public void testGetBounds2D() {
427: for (float[][][] element : bounds) {
428: GeneralPath src = createPath(element[0]);
429: Rectangle2D bound = new Rectangle2D.Float(element[1][0][0],
430: element[1][0][1], element[1][0][2],
431: element[1][0][3]);
432: assertEquals(bound, src.getBounds2D(), 0.0);
433: }
434: }
435:
436: public void testGetBounds() {
437: for (float[][][] element : bounds) {
438: GeneralPath src = createPath(element[0]);
439: Rectangle2D bound = new Rectangle((int) element[1][0][0],
440: (int) element[1][0][1], (int) element[1][0][2],
441: (int) element[1][0][3]);
442: assertEquals(bound, src.getBounds(), 0.0);
443: }
444: }
445:
446: public void testClone() {
447: assertEquals(g, (GeneralPath) g.clone(), 0.0);
448: g.moveTo(10, 20);
449: g.lineTo(30, 40);
450: assertEquals(g, (GeneralPath) g.clone(), 0.0);
451: g.quadTo(30, 40, 50, 60);
452: assertEquals(g, (GeneralPath) g.clone(), 0.0);
453: g.closePath();
454: assertEquals(g, (GeneralPath) g.clone(), 0.0);
455: }
456:
457: public static void main(String[] args) {
458: junit.textui.TestRunner.run(GeneralPathTest.class);
459: }
460:
461: }
|