Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TestArcTo.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xdgf.usermodel.section.geometry;
  16. import com.microsoft.schemas.office.visio.x2012.main.CellType;
  17. import com.microsoft.schemas.office.visio.x2012.main.RowType;
  18. import com.microsoft.schemas.office.visio.x2012.main.SectionType;
  19. import com.microsoft.schemas.office.visio.x2012.main.TriggerType;
  20. import org.apache.poi.util.LocaleUtil;
  21. import org.apache.poi.xdgf.usermodel.section.GeometrySection;
  22. import org.junit.jupiter.api.Assertions;
  23. import org.junit.jupiter.api.Test;
  24. import java.awt.geom.Path2D;
  25. import java.awt.geom.PathIterator;
  26. import java.util.Arrays;
  27. import static org.junit.jupiter.api.Assertions.assertEquals;
  28. import static org.junit.jupiter.api.Assertions.assertNotNull;
  29. import static org.junit.jupiter.api.Assertions.assertNull;
  30. import static org.mockito.Mockito.mock;
  31. import static org.mockito.Mockito.when;
  32. public class TestArcTo {
  33. private static final double EPS = 0.000001;
  34. // We draw a circular arc with radius 100 from (0, 0) to (100, 100)
  35. private static final double X0 = 0.0;
  36. private static final double Y0 = 0.0;
  37. private static final double X = 100.0;
  38. private static final double Y = 100.0;
  39. private static final double A = 29.289322; // a = radius - sqrt(((x + x0) / 2) ^ 2 + ((y + y0) / 2) ^2)
  40. @Test
  41. public void shouldDrawCircularArcWhenArcHeightMoreThanZero() {
  42. ArcTo arcTo = createArcTo(A);
  43. Path2D.Double actualPath = new Path2D.Double();
  44. actualPath.moveTo(X0, Y0);
  45. // Shape isn't used while creating a circular arc
  46. arcTo.addToPath(actualPath, null);
  47. // This path can be used to draw a curve that approximates calculated arc.
  48. Path2D.Double expectedPath = new Path2D.Double();
  49. expectedPath.moveTo(X0, Y0);
  50. expectedPath.curveTo(26.521649, 0.0, 51.957040, 10.535684, 70.710678, 29.289321);
  51. expectedPath.curveTo(89.464316, 48.042960, 100.000000, 73.478351, X, Y);
  52. assertPath(expectedPath, actualPath);
  53. }
  54. @Test
  55. public void shouldDrawCircularArcWhenArcHeightLessThanZero() {
  56. ArcTo arcTo = createArcTo(-A);
  57. Path2D.Double actualPath = new Path2D.Double();
  58. actualPath.moveTo(X0, Y0);
  59. // Shape isn't used while creating a circular arc
  60. arcTo.addToPath(actualPath, null);
  61. // This path can be used to draw a curve that approximates calculated arc.
  62. Path2D.Double expectedPath = new Path2D.Double();
  63. expectedPath.moveTo(X0, Y0);
  64. expectedPath.curveTo(0.0, 26.521649, 10.535684, 51.957040, 29.289321, 70.710678);
  65. expectedPath.curveTo(48.042960, 89.464316, 73.478351, 100.000000, X, Y);
  66. assertPath(expectedPath, actualPath);
  67. }
  68. @Test
  69. public void shouldDrawLineInsteadOfArcWhenArcHeightIsZero() {
  70. ArcTo arcTo = createArcTo(0.0);
  71. Path2D.Double actualPath = new Path2D.Double();
  72. actualPath.moveTo(X0, Y0);
  73. // Shape isn't used while creating a circular arc
  74. arcTo.addToPath(actualPath, null);
  75. // This path can be used to draw a curve that approximates calculated arc.
  76. Path2D.Double expectedPath = new Path2D.Double();
  77. expectedPath.moveTo(X0, Y0);
  78. expectedPath.lineTo(X, Y);
  79. assertPath(expectedPath, actualPath);
  80. }
  81. @Test
  82. public void shouldNotDrawAnythingWhenArcIsDeleted() {
  83. RowType row = RowType.Factory.newInstance();
  84. row.setIX(0L);
  85. row.setDel(true);
  86. ArcTo arcTo = new ArcTo(row);
  87. Path2D.Double actualPath = new Path2D.Double();
  88. actualPath.moveTo(X0, Y0);
  89. // Shape isn't used while creating a circular arc
  90. arcTo.addToPath(actualPath, null);
  91. // This path can be used to draw a curve that approximates calculated arc.
  92. Path2D.Double expectedPath = new Path2D.Double();
  93. expectedPath.moveTo(X0, Y0);
  94. assertPath(expectedPath, actualPath);
  95. }
  96. // this test is mostly used to trigger inclusion of some
  97. // classes into poi-ooxml-lite
  98. @Test
  99. public void testSnapshot() {
  100. SectionType sectionType = mock(SectionType.class);
  101. RowType rowType = mock(RowType.class);
  102. when(sectionType.getCellArray()).thenReturn(new CellType[0]);
  103. when(sectionType.getRowArray()).thenReturn(new RowType[] {
  104. rowType
  105. });
  106. when(rowType.getIX()).thenReturn(0L);
  107. when(rowType.getT()).thenReturn("ArcTo");
  108. when(rowType.getCellArray()).thenReturn(new CellType[0]);
  109. GeometrySection section = new GeometrySection(sectionType, null);
  110. assertNotNull(section);
  111. TriggerType[] triggerArray = sectionType.getTriggerArray();
  112. assertNull(triggerArray);
  113. }
  114. private static ArcTo createArcTo(double a) {
  115. RowType row = RowType.Factory.newInstance();
  116. row.setIX(0L);
  117. row.setDel(false);
  118. CellType xCell = CellType.Factory.newInstance();
  119. xCell.setN("X");
  120. xCell.setV(Double.toString(X));
  121. CellType yCell = CellType.Factory.newInstance();
  122. yCell.setN("Y");
  123. yCell.setV(Double.toString(Y));
  124. CellType aCell = CellType.Factory.newInstance();
  125. aCell.setN("A");
  126. aCell.setV(Double.toString(a));
  127. CellType[] cells = new CellType[] { xCell , yCell, aCell };
  128. row.setCellArray(cells);
  129. return new ArcTo(row);
  130. }
  131. private static void assertPath(Path2D expected, Path2D actual) {
  132. PathIterator expectedIterator = expected.getPathIterator(null);
  133. PathIterator actualIterator = actual.getPathIterator(null);
  134. double[] expectedCoordinates = new double[6];
  135. double[] actualCoordinates = new double[6];
  136. while (!expectedIterator.isDone() && !actualIterator.isDone()) {
  137. int expectedSegmentType = expectedIterator.currentSegment(expectedCoordinates);
  138. int actualSegmentType = actualIterator.currentSegment(actualCoordinates);
  139. assertEquals(expectedSegmentType, actualSegmentType);
  140. assertCoordinates(expectedCoordinates, actualCoordinates);
  141. expectedIterator.next();
  142. actualIterator.next();
  143. }
  144. if (!expectedIterator.isDone() || !actualIterator.isDone()) {
  145. Assertions.fail("Path iterators have different number of segments");
  146. }
  147. }
  148. private static void assertCoordinates(double[] expected, double[] actual) {
  149. if (expected.length != actual.length) {
  150. Assertions.fail(String.format(
  151. LocaleUtil.getUserLocale(),
  152. "Given coordinates arrays have different length: expected=%s, actual=%s",
  153. Arrays.toString(expected), Arrays.toString(actual)));
  154. }
  155. for (int i = 0; i < expected.length; i++) {
  156. double e = expected[i];
  157. double a = actual[i];
  158. if (Math.abs(e - a) > EPS) {
  159. Assertions.fail(String.format(
  160. LocaleUtil.getUserLocale(),
  161. "expected <%f> but found <%f>", e, a));
  162. }
  163. }
  164. }
  165. }