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.

PageSequenceMasterTestCase.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.pagination;
  19. import org.junit.Test;
  20. import org.xml.sax.Locator;
  21. import static org.junit.Assert.fail;
  22. import static org.mockito.ArgumentMatchers.nullable;
  23. import static org.mockito.Matchers.anyBoolean;
  24. import static org.mockito.Matchers.anyInt;
  25. import static org.mockito.Matchers.anyObject;
  26. import static org.mockito.Matchers.anyString;
  27. import static org.mockito.Matchers.eq;
  28. import static org.mockito.Mockito.mock;
  29. import static org.mockito.Mockito.verify;
  30. import static org.mockito.Mockito.when;
  31. import org.apache.fop.apps.FOPException;
  32. import org.apache.fop.fo.FONode;
  33. import org.apache.fop.layoutmgr.BlockLevelEventProducer;
  34. /**
  35. * Unit Test for PageSequenceMaster
  36. *
  37. */
  38. public class PageSequenceMasterTestCase {
  39. /**
  40. * Test that block level events are produced in line with
  41. * XSL:FO - 6.4.8 fo:page-sequence-master -
  42. * "It is an error if the entire sequence of sub-sequence-specifiers children is exhausted
  43. * while some areas returned by an fo:flow are not placed. Implementations may recover,
  44. * if possible, by re-using the sub-sequence-specifier that was last used to generate a page."
  45. *
  46. * @throws Exception exception
  47. */
  48. @Test
  49. public void testGetNextSimplePageMasterExhausted() throws Exception {
  50. //Test when the last sub-sequence specifier is not repeatable
  51. testGetNextSimplePageMasterExhausted(true);
  52. //Test when the last sub-sequence specifier is repeatable
  53. testGetNextSimplePageMasterExhausted(false);
  54. }
  55. private void testGetNextSimplePageMasterExhausted(boolean canResume) throws Exception {
  56. SimplePageMaster spm = mock(SimplePageMaster.class);
  57. SubSequenceSpecifier mockSinglePageMasterReference
  58. = mock(SubSequenceSpecifier.class);
  59. BlockLevelEventProducer mockBlockLevelEventProducer = mock(BlockLevelEventProducer.class);
  60. // subject under test
  61. PageSequenceMaster pageSequenceMaster = createPageSequenceMaster(
  62. mockBlockLevelEventProducer);
  63. pageSequenceMaster.addSubsequenceSpecifier(mockSinglePageMasterReference);
  64. //Setup to mock the exhaustion of the last sub-sequence specifier
  65. when(mockSinglePageMasterReference.getNextPageMaster(anyBoolean(), anyBoolean(),
  66. anyBoolean(), anyBoolean())).thenReturn(null, spm);
  67. //Need this for the method to return normally
  68. when(mockSinglePageMasterReference.canProcess(nullable(String.class))).thenReturn(true);
  69. when(mockSinglePageMasterReference.isReusable()).thenReturn(canResume);
  70. pageSequenceMaster.getNextSimplePageMaster(false, false, false, false, null);
  71. verify(mockBlockLevelEventProducer).pageSequenceMasterExhausted((Locator)anyObject(),
  72. nullable(String.class), eq(canResume), (Locator)anyObject());
  73. }
  74. /**
  75. * Test that PageProductionException is thrown if the final simple-page-master
  76. * cannot handle the main-flow of the page sequence
  77. * @throws Exception exception
  78. */
  79. @Test
  80. public void testGetNextSimplePageMasterException() throws Exception {
  81. final String mainFlowRegionName = "main";
  82. final String emptyFlowRegionName = "empty";
  83. // This will represent a page master that does not map to the main flow
  84. // of the page sequence
  85. SimplePageMaster mockEmptySPM = mock(SimplePageMaster.class);
  86. Region mockRegion = mock(Region.class);
  87. SinglePageMasterReference mockSinglePageMasterReference
  88. = mock(SinglePageMasterReference.class);
  89. BlockLevelEventProducer mockBlockLevelEventProducer = mock(BlockLevelEventProducer.class);
  90. LayoutMasterSet mockLayoutMasterSet = mock(LayoutMasterSet.class);
  91. //The layout master set should return the empty page master
  92. when(mockLayoutMasterSet.getSimplePageMaster(anyString())).thenReturn(mockEmptySPM);
  93. when(mockEmptySPM.getRegion(anyInt())).thenReturn(mockRegion);
  94. when(mockRegion.getRegionName()).thenReturn(emptyFlowRegionName);
  95. when(mockSinglePageMasterReference.getNextPageMaster(anyBoolean(), anyBoolean(),
  96. anyBoolean(), anyBoolean()))
  97. .thenReturn(null, mockEmptySPM);
  98. PageSequenceMaster pageSequenceMaster = createPageSequenceMaster(mockLayoutMasterSet,
  99. mockBlockLevelEventProducer);
  100. pageSequenceMaster.startOfNode();
  101. pageSequenceMaster.addSubsequenceSpecifier(mockSinglePageMasterReference);
  102. try {
  103. pageSequenceMaster.getNextSimplePageMaster(false, false, false, false,
  104. mainFlowRegionName);
  105. fail("The next simple page master does not refer to the main flow");
  106. } catch (PageProductionException ppe) {
  107. //Passed test
  108. }
  109. }
  110. private PageSequenceMaster createPageSequenceMaster(
  111. BlockLevelEventProducer blockLevelEventProducer) throws FOPException {
  112. return createPageSequenceMaster(mock(LayoutMasterSet.class), blockLevelEventProducer);
  113. }
  114. private PageSequenceMaster createPageSequenceMaster(LayoutMasterSet layoutMasterSet,
  115. BlockLevelEventProducer blockLevelEventProducer) throws FOPException {
  116. FONode mockParent = mock(FONode.class);
  117. Root mockRoot = mock(Root.class);
  118. //Stub generic components
  119. when(mockParent.getRoot()).thenReturn(mockRoot);
  120. when(mockRoot.getLayoutMasterSet()).thenReturn(layoutMasterSet);
  121. PageSequenceMaster psm = new PageSequenceMaster(mockParent, blockLevelEventProducer);
  122. psm.startOfNode();
  123. return psm;
  124. }
  125. }