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