You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MarkersTestCase.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id:$ */
  18. package org.apache.fop.fo.flow;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import org.junit.Test;
  22. import static org.junit.Assert.assertEquals;
  23. import static org.junit.Assert.fail;
  24. import static org.mockito.Mockito.mock;
  25. import static org.mockito.Mockito.when;
  26. import org.apache.fop.fo.Constants;
  27. import org.apache.fop.fo.flow.Marker;
  28. import org.apache.fop.fo.flow.Markers;
  29. import org.apache.fop.fo.flow.RetrieveMarker;
  30. import org.apache.fop.fo.flow.RetrieveTableMarker;
  31. public class MarkersTestCase {
  32. @Test
  33. public void testRegisterAndResolve() {
  34. // consider 3 regions, and a boundary; the first region starts before the boundary and ends inside
  35. // the boundary, the second region is fully inside the boundary, and the third region starts inside
  36. // the boundary and ends after the boundary. in every region there are 2 markers, A and B.
  37. // ======== region 1
  38. Map<String, Marker> markers_region_1 = new HashMap<String, Marker>();
  39. Marker marker_1A = mock(Marker.class);
  40. Marker marker_1B = mock(Marker.class);
  41. markers_region_1.put("A", marker_1A);
  42. markers_region_1.put("B", marker_1B);
  43. // ======== region 2
  44. Map<String, Marker> markers_region_2 = new HashMap<String, Marker>();
  45. Marker marker_2A = mock(Marker.class);
  46. Marker marker_2B = mock(Marker.class);
  47. markers_region_2.put("A", marker_2A);
  48. markers_region_2.put("B", marker_2B);
  49. // ======== region 3
  50. Map<String, Marker> markers_region_3 = new HashMap<String, Marker>();
  51. Marker marker_3A = mock(Marker.class);
  52. Marker marker_3B = mock(Marker.class);
  53. markers_region_3.put("A", marker_3A);
  54. markers_region_3.put("B", marker_3B);
  55. // instantiate markers for the boundary
  56. Markers markers = new Markers();
  57. // register the markers for the different regions
  58. // region 1
  59. markers.register(markers_region_1, true, false, true);
  60. markers.register(markers_region_1, false, false, true);
  61. // region 2
  62. markers.register(markers_region_2, true, true, true);
  63. markers.register(markers_region_2, false, true, true);
  64. // region 3
  65. markers.register(markers_region_3, true, true, false);
  66. markers.register(markers_region_3, false, true, false);
  67. // now prepare a RetrieveMarker
  68. RetrieveMarker rm = mock(RetrieveMarker.class);
  69. when(rm.getRetrieveClassName()).thenReturn("A");
  70. when(rm.getLocalName()).thenReturn("retrieve-marker");
  71. when(rm.getPositionLabel()).thenReturn("position-label"); // not relevant for the test
  72. // and resolve the marker for different positions
  73. // EN_FSWP
  74. when(rm.getPosition()).thenReturn(Constants.EN_FSWP);
  75. // expect marker_2A
  76. assertEquals(marker_2A, markers.resolve(rm));
  77. // EN_LSWP
  78. when(rm.getPosition()).thenReturn(Constants.EN_LSWP);
  79. // expect marker_3A
  80. assertEquals(marker_3A, markers.resolve(rm));
  81. // EN_LEWP
  82. when(rm.getPosition()).thenReturn(Constants.EN_LEWP);
  83. // expect marker_2A
  84. assertEquals(marker_2A, markers.resolve(rm));
  85. // EN_FIC
  86. when(rm.getPosition()).thenReturn(Constants.EN_FIC);
  87. // expect marker_1A
  88. assertEquals(marker_1A, markers.resolve(rm));
  89. // now prepare a RetrieveTableMarker
  90. RetrieveTableMarker rtm = mock(RetrieveTableMarker.class);
  91. when(rtm.getRetrieveClassName()).thenReturn("B");
  92. when(rtm.getLocalName()).thenReturn("retrieve-table-marker");
  93. when(rtm.getPositionLabel()).thenReturn("position-label"); // not relevant for the test
  94. // and resolve the marker for different positions
  95. // EN_FIRST_STARTING
  96. when(rtm.getPosition()).thenReturn(Constants.EN_FIRST_STARTING);
  97. // expect marker_2B
  98. assertEquals(marker_2B, markers.resolve(rtm));
  99. // EN_LAST_STARTING
  100. when(rtm.getPosition()).thenReturn(Constants.EN_LAST_STARTING);
  101. // expect marker_3B
  102. assertEquals(marker_3B, markers.resolve(rtm));
  103. // EN_LAST_ENDING
  104. when(rtm.getPosition()).thenReturn(Constants.EN_LAST_ENDING);
  105. // expect marker_2B
  106. assertEquals(marker_2B, markers.resolve(rtm));
  107. // EN_FIRST_INCLUDING_CARRYOVER
  108. when(rtm.getPosition()).thenReturn(Constants.EN_FIRST_INCLUDING_CARRYOVER);
  109. // expect marker_1B
  110. assertEquals(marker_1B, markers.resolve(rtm));
  111. // test also an invalid position
  112. when(rm.getPosition()).thenReturn(Constants.EN_ABSOLUTE);
  113. try {
  114. Marker m = markers.resolve(rm);
  115. fail("Expected an exception... instead got:" + m.toString());
  116. } catch (RuntimeException re) {
  117. // do nothing
  118. }
  119. }
  120. }