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: package java.awt.geom;
018:
019: import java.awt.Shape;
020: import java.awt.geom.AffineTransform;
021: import java.awt.geom.GeomTestCase;
022:
023: public class AffineTransformTest extends GeomTestCase {
024:
025: static {
026: SERIALIZATION_TEST = true;
027: }
028:
029: final static double ROTATION_DELTA = 1E-5;
030:
031: double[][] type = new double[][] {
032:
033: { 1, 0, 0, 1, 0, 0, AffineTransform.TYPE_IDENTITY },
034: { 1, 0, 0, 1, 2, 4, AffineTransform.TYPE_TRANSLATION },
035: { 1, 0, 0, 1, 2, 0, AffineTransform.TYPE_TRANSLATION },
036: { 1, 0, 0, 1, 0, 4, AffineTransform.TYPE_TRANSLATION },
037: { 1, 0, 0, 1, -2, -4, AffineTransform.TYPE_TRANSLATION },
038: { 3, 0, 0, 3, 0, 0, AffineTransform.TYPE_UNIFORM_SCALE },
039: {
040: -3,
041: 0,
042: 0,
043: -3,
044: 0,
045: 0,
046: AffineTransform.TYPE_UNIFORM_SCALE
047: | AffineTransform.TYPE_QUADRANT_ROTATION },
048: { 3, 0, 0, 5, 0, 0, AffineTransform.TYPE_GENERAL_SCALE },
049: {
050: -3,
051: 0,
052: 0,
053: -5,
054: 0,
055: 0,
056: AffineTransform.TYPE_GENERAL_SCALE
057: | AffineTransform.TYPE_QUADRANT_ROTATION },
058: { 0, 1, -1, 0, 0, 0, AffineTransform.TYPE_QUADRANT_ROTATION },
059: { 0, -1, 1, 0, 0, 0, AffineTransform.TYPE_QUADRANT_ROTATION },
060: { -1, 0, 0, -1, 0, 0,
061: AffineTransform.TYPE_QUADRANT_ROTATION },
062:
063: {
064: 0,
065: 1,
066: 1,
067: 0,
068: 0,
069: 0,
070: AffineTransform.TYPE_FLIP
071: | AffineTransform.TYPE_QUADRANT_ROTATION },
072: {
073: 0,
074: -1,
075: -1,
076: 0,
077: 0,
078: 0,
079: AffineTransform.TYPE_FLIP
080: | AffineTransform.TYPE_QUADRANT_ROTATION },
081: {
082: -1,
083: 0,
084: 0,
085: 1,
086: 3,
087: 4,
088: AffineTransform.TYPE_FLIP
089: | AffineTransform.TYPE_QUADRANT_ROTATION
090: | AffineTransform.TYPE_TRANSLATION },
091: {
092: 0,
093: 2,
094: 2,
095: 0,
096: 0,
097: 0,
098: AffineTransform.TYPE_FLIP
099: | AffineTransform.TYPE_QUADRANT_ROTATION
100: | AffineTransform.TYPE_UNIFORM_SCALE },
101: {
102: 0,
103: 3,
104: 2,
105: 0,
106: 0,
107: 0,
108: AffineTransform.TYPE_FLIP
109: | AffineTransform.TYPE_QUADRANT_ROTATION
110: | AffineTransform.TYPE_GENERAL_SCALE }, };
111:
112: // Check concatenate/preConcatenate
113: double[][][] matrix = new double[][][] {
114: { { 1, 0, 0, 1, 0, 0 }, { 1, 2, 3, 4, 5, 6 },
115: { 1, 2, 3, 4, 5, 6 } },
116: { { 2, 3, 4, 5, 6, 7 }, { 1, 0, 1, 0, 1, 0 },
117: { 5, 0, 9, 0, 14, 0 } },
118: { { 2, 3, 4, 5, 6, 7 }, { 0, 1, 0, 1, 0, 1 },
119: { 0, 5, 0, 9, 0, 14 } },
120: { { 2, 3, 4, 5, 6, 7 }, { 7, 6, 5, 4, 3, 2 },
121: { 29, 24, 53, 44, 80, 66 } }, };
122:
123: // Check createInvers
124: double[][][] invers = new double[][][] {
125: { { 1, 0, 0, 1, 0, 0 }, { 1, 0, 0, 1, 0, 0 } },
126: { { 0, 1, -1, 0, 0, 0 }, { 0, -1, 1, 0, 0, 0 } },
127: { { 1, 1, 1, 1, 0, 0 }, null },
128: { { 4, 6, 3, 5, 1, 2 }, { 2.5, -3, -1.5, 2, 0.5, -1 } },
129: { { 0, 1, -2, 0, 3, 4 }, { 0, -0.5, 1, 0, -4, 1.5 } } };
130:
131: // Check transform/inversTransform/deltaTransform
132: float[][][] points = new float[][][] {
133: // Matrix
134: { { 0, 1, -2, 0, 3, 4 },
135: // Source point, transform point, invers point, delta point
136: { 0, 0, 3, 4, -4, 1.5f, 0, 0 },
137: { 1, 1, 1, 5, -3, 1, -2, 1 } },
138:
139: };
140:
141: double[][] equal = new double[][] { { 1, 2, 3, 4, 5, 6 },
142: { 0, 2, 3, 4, 5, 6 }, { 1, 0, 3, 4, 5, 6 },
143: { 1, 2, 0, 4, 5, 6 }, { 1, 2, 3, 0, 5, 6 },
144: { 1, 2, 3, 4, 0, 6 }, { 1, 2, 3, 4, 5, 0 } };
145:
146: AffineTransform t;
147:
148: public AffineTransformTest(String name) {
149: super (name);
150: }
151:
152: @Override
153: protected void setUp() throws Exception {
154: super .setUp();
155: t = new AffineTransform(2, 3, 4, 5, 6, 7);
156: }
157:
158: @Override
159: protected void tearDown() throws Exception {
160: t = null;
161: super .tearDown();
162: }
163:
164: void assertEquals(int type, double[] matrix, double delta,
165: AffineTransform t) {
166: double[] actual = new double[matrix.length];
167: t.getMatrix(actual);
168: assertEquals(matrixToStr(matrix) + " Type", type, t.getType());
169: assertEquals("Matrix", matrix, actual, matrix.length, delta);
170: }
171:
172: public void testCreate1() {
173: assertEquals(AffineTransform.TYPE_IDENTITY, new double[] { 1,
174: 0, 0, 1, 0, 0 }, 0.0, new AffineTransform());
175: }
176:
177: public void testCreate2() {
178: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
179: new double[] { 2, 3, 4, 5, 6, 7 }, 0.0,
180: new AffineTransform(2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f));
181: }
182:
183: public void testCreate3() {
184: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
185: new double[] { 2, 3, 4, 5, 6, 7 }, 0.0,
186: new AffineTransform(2.0, 3.0, 4.0, 5.0, 6.0, 7.0));
187: }
188:
189: public void testCreate4() {
190: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
191: new double[] { 2, 3, 4, 5, 6, 7 }, 0.0,
192: new AffineTransform(new float[] { 2, 3, 4, 5, 6, 7 }));
193: }
194:
195: public void testCreate5() {
196: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
197: new double[] { 2, 3, 4, 5, 6, 7 }, 0.0,
198: new AffineTransform(new double[] { 2, 3, 4, 5, 6, 7 }));
199: }
200:
201: public void testCreate6() {
202: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
203: new double[] { 2, 3, 4, 5, 6, 7 }, 0.0,
204: new AffineTransform(new AffineTransform(2, 3, 4, 5, 6,
205: 7)));
206: }
207:
208: String matrixToStr(double[] matrix) {
209: return "[" + doubleToStr(matrix[0]) + ","
210: + doubleToStr(matrix[1]) + "," + doubleToStr(matrix[2])
211: + "," + doubleToStr(matrix[3]) + ","
212: + doubleToStr(matrix[4]) + "," + doubleToStr(matrix[5])
213: + "]";
214: }
215:
216: boolean typeHas(int type, int flag) {
217: return (type & flag) == flag;
218: }
219:
220: String typeToStr(int type) {
221: String s = "";
222: if (type == AffineTransform.TYPE_IDENTITY) {
223: s += "IDENTITY,";
224: } else {
225: if (typeHas(type, AffineTransform.TYPE_FLIP)) {
226: s += "FLIP,";
227: }
228: if (typeHas(type, AffineTransform.TYPE_GENERAL_ROTATION)) {
229: s += "GEN_ROTATION,";
230: }
231: if (typeHas(type, AffineTransform.TYPE_GENERAL_SCALE)) {
232: s += "GEN_SCALE,";
233: }
234: if (typeHas(type, AffineTransform.TYPE_GENERAL_TRANSFORM)) {
235: s += "GEN_TRANSFORM,";
236: }
237: if (typeHas(type, AffineTransform.TYPE_QUADRANT_ROTATION)) {
238: s += "QUAD_ROTATION,";
239: }
240: if (typeHas(type, AffineTransform.TYPE_TRANSLATION)) {
241: s += "TRANSLATION,";
242: }
243: if (typeHas(type, AffineTransform.TYPE_UNIFORM_SCALE)) {
244: s += "UN_SCALE,";
245: }
246: int all = AffineTransform.TYPE_FLIP
247: | AffineTransform.TYPE_GENERAL_ROTATION
248: | AffineTransform.TYPE_GENERAL_SCALE
249: | AffineTransform.TYPE_GENERAL_TRANSFORM
250: | AffineTransform.TYPE_IDENTITY
251: | AffineTransform.TYPE_QUADRANT_ROTATION
252: | AffineTransform.TYPE_TRANSLATION
253: | AffineTransform.TYPE_UNIFORM_SCALE;
254: if ((type & ~all) != 0) {
255: s += Integer.toString(type & ~all) + ",";
256: }
257: }
258: return Integer.toString(type);
259: }
260:
261: public void testGetType() {
262: for (double[] element : type) {
263: assertEquals(matrixToStr(element) + " Type",
264: (int) element[6], new AffineTransform(element)
265: .getType());
266: }
267: }
268:
269: public void testGetScaleX() {
270: assertEquals(2.0, t.getScaleX(), 0.0);
271: }
272:
273: public void testGetScaleY() {
274: assertEquals(5.0, t.getScaleY(), 0.0);
275: }
276:
277: public void testGetShearX() {
278: assertEquals(4.0, t.getShearX(), 0.0);
279: }
280:
281: public void testGetShearY() {
282: assertEquals(3.0, t.getShearY(), 0.0);
283: }
284:
285: public void testGetTranslateX() {
286: assertEquals(6.0, t.getTranslateX(), 0.0);
287: }
288:
289: public void testGetTranslateY() {
290: assertEquals(7.0, t.getTranslateY(), 0.0);
291: }
292:
293: public void testIsEdentity() {
294: assertFalse(t.isIdentity());
295: assertTrue(new AffineTransform(1, 0, 0, 1, 0, 0).isIdentity());
296: }
297:
298: public void testGetMatrix() {
299: double[] matrix = new double[] { 0, 0, 0, 0 };
300: t.getMatrix(matrix);
301: assertEquals(new double[] { 2, 3, 4, 5 }, matrix, 4, 0.0);
302: matrix = new double[] { 0, 0, 0, 0, 0, 0 };
303: t.getMatrix(matrix);
304: assertEquals(new double[] { 2, 3, 4, 5, 6, 7 }, matrix, 6, 0.0);
305: }
306:
307: public void testGetDeterminant() {
308: assertEquals(-2, t.getDeterminant(), 0.0);
309: }
310:
311: public void testSetTransform() {
312: t.setTransform(8, 9, 10, 11, 12, 13);
313: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
314: new double[] { 8, 9, 10, 11, 12, 13 }, 0.0, t);
315: }
316:
317: public void testSetTransform2() {
318: t.setTransform(new AffineTransform(8, 9, 10, 11, 12, 13));
319: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
320: new double[] { 8, 9, 10, 11, 12, 13 }, 0.0, t);
321: }
322:
323: public void testSetToIdentity() {
324: t.setToIdentity();
325: assertEquals(AffineTransform.TYPE_IDENTITY, new double[] { 1,
326: 0, 0, 1, 0, 0 }, 0.0, t);
327: }
328:
329: public void testSetToTranslation() {
330: t.setToTranslation(8, 9);
331: assertEquals(AffineTransform.TYPE_TRANSLATION, new double[] {
332: 1, 0, 0, 1, 8, 9 }, 0.0, t);
333: t.setToTranslation(0, 0);
334: assertEquals(AffineTransform.TYPE_IDENTITY, new double[] { 1,
335: 0, 0, 1, 0, 0 }, 0.0, t);
336: }
337:
338: public void testSetToScale() {
339: t.setToScale(8, 9);
340: assertEquals(AffineTransform.TYPE_GENERAL_SCALE, new double[] {
341: 8, 0, 0, 9, 0, 0 }, 0.0, t);
342: t.setToScale(1, 1);
343: assertEquals(AffineTransform.TYPE_IDENTITY, new double[] { 1,
344: 0, 0, 1, 0, 0 }, 0.0, t);
345: }
346:
347: public void testSetToShear() {
348: t.setToShear(8, 9);
349: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
350: new double[] { 1, 9, 8, 1, 0, 0 }, 0.0, t);
351: t.setToShear(0, 0);
352: assertEquals(AffineTransform.TYPE_IDENTITY, new double[] { 1,
353: 0, 0, 1, 0, 0 }, 0.0, t);
354: }
355:
356: public void testSetToRotation1() {
357: t.setToRotation(Math.PI * 0.5);
358: assertEquals(AffineTransform.TYPE_QUADRANT_ROTATION,
359: new double[] { 0, 1, -1, 0, 0, 0 }, ROTATION_DELTA, t);
360: t.setToRotation(Math.PI * 1.5);
361: assertEquals(AffineTransform.TYPE_QUADRANT_ROTATION,
362: new double[] { 0, -1, 1, 0, 0, 0 }, ROTATION_DELTA, t);
363: t.setToRotation(0);
364: assertEquals(AffineTransform.TYPE_IDENTITY, new double[] { 1,
365: 0, 0, 1, 0, 0 }, ROTATION_DELTA, t);
366: t.setToRotation(Math.PI * 2.0);
367: assertEquals(AffineTransform.TYPE_IDENTITY, new double[] { 1,
368: 0, 0, 1, 0, 0 }, ROTATION_DELTA, t);
369: }
370:
371: public void testSetToRotation2() {
372: t.setToRotation(0, 8, 9);
373: assertEquals(AffineTransform.TYPE_IDENTITY, new double[] { 1,
374: 0, 0, 1, 0, 0 }, ROTATION_DELTA, t);
375: t.setToRotation(Math.PI * 0.5, 8, 9);
376: assertEquals(AffineTransform.TYPE_TRANSLATION
377: | AffineTransform.TYPE_QUADRANT_ROTATION, new double[] {
378: 0, 1, -1, 0, 17, 1 }, ROTATION_DELTA, t);
379: t.setToRotation(Math.PI * 0.5, 0, 0);
380: assertEquals(AffineTransform.TYPE_QUADRANT_ROTATION,
381: new double[] { 0, 1, -1, 0, 0, 0 }, ROTATION_DELTA, t);
382: }
383:
384: public void testGetTranslateInstance() {
385: assertEquals(AffineTransform.TYPE_TRANSLATION, new double[] {
386: 1, 0, 0, 1, 8, 9 }, 0.0, AffineTransform
387: .getTranslateInstance(8, 9));
388: assertEquals(AffineTransform.TYPE_IDENTITY, new double[] { 1,
389: 0, 0, 1, 0, 0 }, 0.0, AffineTransform
390: .getTranslateInstance(0, 0));
391: }
392:
393: public void testGetScaleInstance() {
394: assertEquals(AffineTransform.TYPE_GENERAL_SCALE, new double[] {
395: 8, 0, 0, 9, 0, 0 }, 0.0, AffineTransform
396: .getScaleInstance(8, 9));
397: assertEquals(AffineTransform.TYPE_IDENTITY, new double[] { 1,
398: 0, 0, 1, 0, 0 }, 0.0, AffineTransform.getScaleInstance(
399: 1, 1));
400: }
401:
402: public void testGetShearInstance() {
403: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
404: new double[] { 1, 9, 8, 1, 0, 0 }, 0.0, AffineTransform
405: .getShearInstance(8, 9));
406: assertEquals(AffineTransform.TYPE_IDENTITY, new double[] { 1,
407: 0, 0, 1, 0, 0 }, 0.0, AffineTransform.getShearInstance(
408: 0, 0));
409: }
410:
411: public void testGetRotateInstance1() {
412: assertEquals(AffineTransform.TYPE_QUADRANT_ROTATION,
413: new double[] { 0, 1, -1, 0, 0, 0 }, ROTATION_DELTA,
414: AffineTransform.getRotateInstance(Math.PI * 0.5));
415: assertEquals(AffineTransform.TYPE_QUADRANT_ROTATION,
416: new double[] { 0, -1, 1, 0, 0, 0 }, ROTATION_DELTA,
417: AffineTransform.getRotateInstance(Math.PI * 1.5));
418: assertEquals(AffineTransform.TYPE_IDENTITY, new double[] { 1,
419: 0, 0, 1, 0, 0 }, ROTATION_DELTA, AffineTransform
420: .getRotateInstance(0));
421: assertEquals(AffineTransform.TYPE_IDENTITY, new double[] { 1,
422: 0, 0, 1, 0, 0 }, ROTATION_DELTA, AffineTransform
423: .getRotateInstance(Math.PI * 2.0));
424: }
425:
426: public void testGetRotateInstance2() {
427: assertEquals(AffineTransform.TYPE_IDENTITY, new double[] { 1,
428: 0, 0, 1, 0, 0 }, ROTATION_DELTA, AffineTransform
429: .getRotateInstance(0, 8, 9));
430: assertEquals(AffineTransform.TYPE_TRANSLATION
431: | AffineTransform.TYPE_QUADRANT_ROTATION, new double[] {
432: 0, 1, -1, 0, 17, 1 }, ROTATION_DELTA, AffineTransform
433: .getRotateInstance(Math.PI * 0.5, 8, 9));
434: assertEquals(AffineTransform.TYPE_QUADRANT_ROTATION,
435: new double[] { 0, 1, -1, 0, 0, 0 }, ROTATION_DELTA,
436: AffineTransform.getRotateInstance(Math.PI * 0.5, 0, 0));
437: }
438:
439: public void testTranslate() {
440: t.translate(0, 0);
441: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
442: new double[] { 2, 3, 4, 5, 6, 7 }, 0.0, t);
443: t.translate(8, 9);
444: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
445: new double[] { 2, 3, 4, 5, 58, 76 }, 0.0, t);
446: }
447:
448: public void testScale() {
449: t.scale(1, 1);
450: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
451: new double[] { 2, 3, 4, 5, 6, 7 }, 0.0, t);
452: t.scale(2, 3);
453: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
454: new double[] { 4, 6, 12, 15, 6, 7 }, 0.0, t);
455: }
456:
457: public void testShear() {
458: t.shear(0, 0);
459: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
460: new double[] { 2, 3, 4, 5, 6, 7 }, 0.0, t);
461: t.shear(3, 2);
462: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
463: new double[] { 10, 13, 10, 14, 6, 7 }, 0.0, t);
464: }
465:
466: public void testRotate1() {
467: t.rotate(0);
468: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
469: new double[] { 2, 3, 4, 5, 6, 7 }, 0.0, t);
470: t.rotate(Math.PI * 2.0);
471: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
472: new double[] { 2, 3, 4, 5, 6, 7 }, 0.0, t);
473: t.rotate(Math.PI * 0.5);
474: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
475: new double[] { 4, 5, -2, -3, 6, 7 }, 0.0, t);
476: }
477:
478: public void testRotate2() {
479: t.rotate(0, 8, 9);
480: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
481: new double[] { 2, 3, 4, 5, 6, 7 }, 0.0, t);
482: t.rotate(Math.PI * 2.0, 8, 9);
483: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
484: new double[] { 2, 3, 4, 5, 6, 7 }, 0.0, t);
485: t.rotate(Math.PI * 1.5, 8, 9);
486: assertEquals(AffineTransform.TYPE_GENERAL_TRANSFORM,
487: new double[] { -4, -5, 2, 3, 72, 89 },
488: // new double[]{3, -2, 5, -4, 6, 11},
489: 0.0, t);
490: }
491:
492: public void testConcatenate() {
493: for (double[][] element : matrix) {
494: AffineTransform a = new AffineTransform(element[0]);
495: AffineTransform b = new AffineTransform(element[1]);
496: AffineTransform c = new AffineTransform(element[2]);
497: b.concatenate(a);
498: assertEquals(c, b);
499: }
500: }
501:
502: public void testPreConcatenate() {
503: for (double[][] element : matrix) {
504: AffineTransform a = new AffineTransform(element[0]);
505: AffineTransform b = new AffineTransform(element[1]);
506: AffineTransform c = new AffineTransform(element[2]);
507: a.preConcatenate(b);
508: assertEquals(c, a);
509: }
510: }
511:
512: public void testCreateInvers() {
513: for (double[][] element : invers) {
514: try {
515: AffineTransform at = new AffineTransform(element[0]);
516: AffineTransform it = at.createInverse();
517: if (element[1] == null) {
518: fail(at
519: + " Expected exception NoninvertibleTransformException");
520: } else {
521: assertEquals(new AffineTransform(element[1]), it);
522: }
523: at.concatenate(it);
524: assertTrue(at.isIdentity());
525: } catch (NoninvertibleTransformException e) {
526: if (element[1] != null) {
527: fail(e.toString());
528: }
529: }
530: }
531: }
532:
533: public void testTransformPoint() {
534: for (float[][] element : points) {
535: AffineTransform at = new AffineTransform(element[0]);
536: for (int j = 1; j < element.length; j++) {
537: float x1 = element[j][0];
538: float y1 = element[j][1];
539: float x2 = element[j][2];
540: float y2 = element[j][3];
541:
542: assertEquals(new Point2D.Double(x2, y2), at.transform(
543: new Point2D.Double(x1, y1), null));
544: assertEquals(new Point2D.Float(x2, y2), at.transform(
545: new Point2D.Float(x1, y1), null));
546:
547: Point2D dst = new Point2D.Double();
548: assertEquals(new Point2D.Double(x2, y2), at.transform(
549: new Point2D.Double(x1, y1), dst));
550: assertEquals(new Point2D.Double(x2, y2), dst);
551: }
552: }
553: }
554:
555: public void testTransformPointArray() {
556: AffineTransform at = new AffineTransform(0, 1, -2, 0, 3, 4);
557: Point2D[] src = new Point2D[] { null, new Point2D.Double(0, 0),
558: new Point2D.Float(1, 1), null };
559: Point2D[] dst = new Point2D[4];
560: at.transform(src, 1, dst, 2, 2);
561: assertEquals(new Point2D.Double(3, 4), dst[2]);
562: assertEquals(new Point2D.Float(1, 5), dst[3]);
563: }
564:
565: public void testTransformPointArrayBad() {
566: // Regression test HARMONY-1405
567:
568: AffineTransform at = new AffineTransform();
569: try {
570: at.transform(new Point2D[] { null, null, null, null }, 0,
571: new Point2D[] { null, null, null, null }, -1, 1);
572: fail("Expected NPE");
573: } catch (NullPointerException e) {
574: // expected
575: }
576:
577: try {
578: at.transform(new Point2D[] { null, null, null, null }, 1,
579: new Point2D[] { null, null, null, null }, 10, 1);
580: fail("Expected NPE");
581: } catch (NullPointerException e) {
582: // expected
583: }
584: }
585:
586: public void testTransformArray2() {
587: AffineTransform at = new AffineTransform(0, 1, -2, 0, 3, 4);
588: double[] src = new double[] { 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 };
589: double[] dst = new double[6];
590: double[] expected = new double[] { 0, 0, 3, 4, 1, 5 };
591: at.transform(src, 4, dst, 2, 2);
592: assertEquals(expected, dst, 6, 0.0);
593: }
594:
595: public void testTransformArray3() {
596: AffineTransform at = new AffineTransform(0, 1, -2, 0, 3, 4);
597: float[] src = new float[] { 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 };
598: float[] dst = new float[6];
599: float[] expected = new float[] { 0, 0, 3, 4, 1, 5 };
600: at.transform(src, 4, dst, 2, 2);
601: assertEquals(expected, dst, 6, 0.0f);
602: }
603:
604: public void testTransformArray4() {
605: AffineTransform at = new AffineTransform(0, 1, -2, 0, 3, 4);
606: float[] src = new float[] { 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 };
607: double[] dst = new double[6];
608: double[] expected = new double[] { 0, 0, 3, 4, 1, 5 };
609: at.transform(src, 4, dst, 2, 2);
610: assertEquals(expected, dst, 6, 0.0);
611: }
612:
613: public void testTransformArray5() {
614: AffineTransform at = new AffineTransform(0, 1, -2, 0, 3, 4);
615: double[] src = new double[] { 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 };
616: float[] dst = new float[6];
617: float[] expected = new float[] { 0, 0, 3, 4, 1, 5 };
618: at.transform(src, 4, dst, 2, 2);
619: assertEquals(expected, dst, 6, 0.0f);
620: }
621:
622: public void testTransformArrayOverlap1() {
623: // Regresion test HARMONY-1603
624: AffineTransform at = AffineTransform.getTranslateInstance(2, 3);
625: float[] src = new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
626: 12, 13, 14 };
627: float[] expected = new float[] { 1, 2, 3, 4, 3, 5, 5, 7, 7, 9,
628: 9, 11, 13, 14 };
629: at.transform(src, 0, src, 4, 4);
630: assertEquals(expected, src, src.length, 0);
631: }
632:
633: public void testTransformArrayOverlap2() {
634: // Regresion test HARMONY-1603
635: AffineTransform at = AffineTransform.getTranslateInstance(2, 3);
636: double[] src = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
637: 11, 12, 13, 14 };
638: double[] expected = new double[] { 1, 2, 3, 4, 3, 5, 5, 7, 7,
639: 9, 9, 11, 13, 14 };
640: at.transform(src, 0, src, 4, 4);
641: assertEquals(expected, src, src.length, 0);
642: }
643:
644: public void testDeltaTransform1() {
645: for (float[][] element : points) {
646: AffineTransform at = new AffineTransform(element[0]);
647: for (int j = 1; j < element.length; j++) {
648: float x1 = element[j][0];
649: float y1 = element[j][1];
650: float x2 = element[j][6];
651: float y2 = element[j][7];
652:
653: assertEquals(new Point2D.Double(x2, y2), at
654: .deltaTransform(new Point2D.Double(x1, y1),
655: null));
656: assertEquals(new Point2D.Float(x2, y2),
657: at.deltaTransform(new Point2D.Float(x1, y1),
658: null));
659:
660: Point2D dst = new Point2D.Double();
661: assertEquals(new Point2D.Double(x2, y2),
662: at.deltaTransform(new Point2D.Double(x1, y1),
663: dst));
664: assertEquals(new Point2D.Double(x2, y2), dst);
665: }
666: }
667: }
668:
669: public void testDeltaTransform2() {
670: AffineTransform at = new AffineTransform(0, 1, -2, 0, 3, 4);
671: double[] src = new double[] { 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 };
672: double[] dst = new double[6];
673: double[] expected = new double[] { 0, 0, 0, 0, -2, 1 };
674: at.deltaTransform(src, 4, dst, 2, 2);
675: assertEquals(expected, dst, 6, 0.0);
676: }
677:
678: public void testInversTransform1() {
679: try {
680: new AffineTransform(1, 1, 1, 1, 1, 1).inverseTransform(
681: new Point2D.Double(), null);
682: fail("Expected exception NoninvertibleTransformException");
683: } catch (NoninvertibleTransformException e) {
684: }
685:
686: for (float[][] element : points) {
687: AffineTransform at = new AffineTransform(element[0]);
688: for (int j = 1; j < element.length; j++) {
689: float x1 = element[j][0];
690: float y1 = element[j][1];
691: float x2 = element[j][4];
692: float y2 = element[j][5];
693:
694: try {
695: assertEquals(new Point2D.Double(x2, y2), at
696: .inverseTransform(
697: new Point2D.Double(x1, y1), null));
698: assertEquals(new Point2D.Float(x2, y2), at
699: .inverseTransform(
700: new Point2D.Float(x1, y1), null));
701:
702: Point2D dst = new Point2D.Double();
703: assertEquals(new Point2D.Double(x2, y2), at
704: .inverseTransform(
705: new Point2D.Double(x1, y1), dst));
706: assertEquals(new Point2D.Double(x2, y2), dst);
707: } catch (NoninvertibleTransformException e) {
708: fail(e.toString());
709: }
710: }
711: }
712: }
713:
714: public void testInversTransform2() {
715: AffineTransform at = new AffineTransform(0, 1, -2, 0, 3, 4);
716: double[] src = new double[] { 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 };
717: double[] dst = new double[6];
718: double[] expected = new double[] { 0, 0, -4, 1.5, -3, 1 };
719: try {
720: at.inverseTransform(src, 4, dst, 2, 2);
721: assertEquals(expected, dst, 6, 0.0);
722: } catch (NoninvertibleTransformException e) {
723: fail(e.toString());
724: }
725: }
726:
727: public void testCreateTransformedShape() {
728: AffineTransform at = new AffineTransform(0, 1, -2, 0, 3, 4);
729: Shape actual = at.createTransformedShape(new Line2D.Double(1,
730: 2, 3, 4));
731: GeneralPath expected = new GeneralPath();
732: expected.moveTo(-1, 5);
733: expected.lineTo(-5, 7);
734: assertEquals(expected.getPathIterator(null), actual
735: .getPathIterator(null), 0.0);
736: }
737:
738: public void testEquals() {
739: for (int i = 0; i < equal.length; i++) {
740: AffineTransform t1 = new AffineTransform(equal[i]);
741: for (int j = 0; j < equal.length; j++) {
742: AffineTransform t2 = new AffineTransform(equal[j]);
743: if (i == j) {
744: assertTrue(t1.equals(t2));
745: } else {
746: assertFalse(t1.equals(t2));
747: }
748: }
749: }
750: }
751:
752: public void testHashCode() {
753: for (int i = 0; i < equal.length; i++) {
754: AffineTransform t1 = new AffineTransform(equal[i]);
755: for (int j = 0; j < equal.length; j++) {
756: AffineTransform t2 = new AffineTransform(equal[j]);
757: if (i == j) {
758: assertTrue("HashCode " + t1 + " " + t2, t1
759: .hashCode() == t2.hashCode());
760: } else {
761: assertTrue("HashCode " + t1 + " " + t2, t1
762: .hashCode() != t2.hashCode());
763: }
764: }
765: }
766: }
767:
768: public void testClone() {
769: assertEquals(t, t.clone());
770: }
771:
772: public void testToString() {
773: assertEquals(
774: "java.awt.geom.AffineTransform[[2.0, 4.0, 6.0], [3.0, 5.0, 7.0]]",
775: t.toString());
776: }
777:
778: @Override
779: public String objToStr(Object obj) {
780: double[] m = new double[6];
781: ((AffineTransform) obj).getMatrix(m);
782: return obj.getClass().getName() + "[[" + m[0] + ", " + m[2]
783: + ", " + m[4] + "], [" + m[1] + ", " + m[3] + ", "
784: + m[5] + "]]";
785: }
786:
787: public void testSerializeRead1() {
788: checkRead(new AffineTransform());
789: }
790:
791: public void testSerializeRead2() {
792: checkRead(new AffineTransform(1, 2, 3, 4, 5, 6));
793: }
794:
795: public void testSerializeWrite1() {
796: checkWrite(new AffineTransform());
797: }
798:
799: public void testSerializeWrite2() {
800: checkWrite(new AffineTransform(1, 2, 3, 4, 5, 6));
801: }
802:
803: public void createSerializeTemplates() {
804: saveSerialized(new AffineTransform());
805: saveSerialized(new AffineTransform(1, 2, 3, 4, 5, 6));
806: }
807:
808: public static void main(String[] args) {
809: // new AffineTransformTest("").createSerializeTemplates();
810: junit.textui.TestRunner.run(AffineTransformTest.class);
811: }
812:
813: }
|