TransformUtils { public static AffineTransform parseTransform(String transform) { if (transform == null) { throw new SvgProcessingException(SvgLogMessageConstant.TRANSFORM_NULL); } if (transform.isEmpty()) { throw new SvgProcessingException(SvgLogMessageConstant.TRANSFORM_EMPTY); } AffineTransform matrix = new AffineTransform(); List<String> listWithTransformations = splitString(transform); for (String transformation : listWithTransformations) { AffineTransform newMatrix = transformationStringToMatrix(transformation); if (newMatrix != null) { matrix.concatenate(newMatrix); } } return matrix; } private  TransformUtils(); static AffineTransform parseTransform(String transform);  }
@Test public void leadingWhiteSpace() { AffineTransform actual = TransformUtils.parseTransform(" translate(1) translate(2)"); AffineTransform expected = AffineTransform.getTranslateInstance(2.25, 0); Assert.assertEquals(expected, actual); }
@Test public void middleWhiteSpace() { AffineTransform actual = TransformUtils.parseTransform("translate(1) translate(2)"); AffineTransform expected = AffineTransform.getTranslateInstance(2.25, 0); Assert.assertEquals(expected, actual); }
@Test public void mixedWhiteSpace() { AffineTransform actual = TransformUtils.parseTransform(" translate(1) translate(2) "); AffineTransform expected = AffineTransform.getTranslateInstance(2.25, 0); Assert.assertEquals(expected, actual); }
@Test public void nullStringTest() { junitExpectedException.expect(SvgProcessingException.class); junitExpectedException.expectMessage(SvgLogMessageConstant.TRANSFORM_NULL); TransformUtils.parseTransform(null); }
@Test public void emptyTest() { junitExpectedException.expect(SvgProcessingException.class); junitExpectedException.expectMessage(SvgLogMessageConstant.TRANSFORM_EMPTY); TransformUtils.parseTransform(""); }
@Test public void noTransformationTest() { junitExpectedException.expect(SvgProcessingException.class); junitExpectedException.expectMessage(SvgLogMessageConstant.INVALID_TRANSFORM_DECLARATION); TransformUtils.parseTransform("Lorem ipsum"); }
@Test public void wrongTypeOfValuesTest() { junitExpectedException.expect(NumberFormatException.class); TransformUtils.parseTransform("matrix(a b c d e f)"); }
@Test public void tooManyParenthesesTest() { junitExpectedException.expect(SvgProcessingException.class); junitExpectedException.expectMessage(SvgLogMessageConstant.INVALID_TRANSFORM_DECLARATION); TransformUtils.parseTransform("(((())))"); }
@Test public void noClosingParenthesisTest() { AffineTransform expected = new AffineTransform(0d, 0d, 0d, 0d, 0d, 0d); AffineTransform actual = TransformUtils.parseTransform("matrix(0 0 0 0 0 0"); Assert.assertEquals(expected, actual); }
@Test public void mixedCaseTest() { AffineTransform expected = new AffineTransform(0d, 0d, 0d, 0d, 0d, 0d); AffineTransform actual = TransformUtils.parseTransform("maTRix(0 0 0 0 0 0)"); Assert.assertEquals(expected, actual); }
@Test public void upperCaseTest() { AffineTransform expected = new AffineTransform(0d, 0d, 0d, 0d, 0d, 0d); AffineTransform actual = TransformUtils.parseTransform("MATRIX(0 0 0 0 0 0)"); Assert.assertEquals(expected, actual); }
@Test public void whitespaceTest() { AffineTransform expected = new AffineTransform(0d, 0d, 0d, 0d, 0d, 0d); AffineTransform actual = TransformUtils.parseTransform("matrix(0 0 0 0 0 0)"); Assert.assertEquals(expected, actual); }
@Test public void commasWithWhitespaceTest() { AffineTransform expected = new AffineTransform(10d,20d,30d,40d,37.5d, 45d); AffineTransform actual = TransformUtils.parseTransform("matrix(10, 20, 30, 40, 50, 60)"); Assert.assertEquals(expected, actual); }
@Test public void commasTest() { AffineTransform expected = new AffineTransform(10d,20d,30d,40d,37.5d, 45d); AffineTransform actual = TransformUtils.parseTransform("matrix(10,20,30,40,50,60)"); Assert.assertEquals(expected, actual); }
@Test public void combinedTransformTest() { AffineTransform actual = TransformUtils.parseTransform("translate(40,20) scale(3)"); AffineTransform expected = new AffineTransform(3.0,0d,0d,3.0d,30d,15d); Assert.assertEquals(actual, expected); }
@Test public void combinedReverseTransformTest() { AffineTransform actual = TransformUtils.parseTransform("scale(3) translate(40,20)"); AffineTransform expected = new AffineTransform(3d,0d,0d,3d,90d,45d); Assert.assertEquals(actual, expected); }
@Test public void doubleTransformationTest() { AffineTransform expected = new AffineTransform(9d, 0d, 0d, 9d, 0d, 0d); AffineTransform actual = TransformUtils.parseTransform("scale(3) scale(3)"); Assert.assertEquals(expected, actual); }
@Test public void oppositeTransformationSequenceTest() { AffineTransform expected = new AffineTransform(1,0,0,1,0,0); AffineTransform actual = TransformUtils.parseTransform("translate(10 10) translate(-10 -10)"); Assert.assertEquals(expected, actual); }
@Test public void unknownTransformationTest() { junitExpectedException.expect(SvgProcessingException.class); junitExpectedException.expectMessage(SvgLogMessageConstant.UNKNOWN_TRANSFORMATION_TYPE); TransformUtils.parseTransform("unknown(1 2 3)"); }
@Test public void trailingWhiteSpace() { AffineTransform actual = TransformUtils.parseTransform("translate(1) translate(2) "); AffineTransform expected = AffineTransform.getTranslateInstance(2.25, 0); Assert.assertEquals(expected, actual); }