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.

RepeatablePageMasterAlternativesTestCase.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 static org.junit.Assert.assertTrue;
  21. import static org.mockito.Matchers.anyBoolean;
  22. import static org.mockito.Matchers.anyInt;
  23. import static org.mockito.Mockito.mock;
  24. import static org.mockito.Mockito.when;
  25. import org.apache.fop.fo.Constants;
  26. import org.apache.fop.fo.PropertyList;
  27. import org.apache.fop.fo.expr.NumericProperty;
  28. import org.apache.fop.fo.properties.Property;
  29. /**
  30. * Unit Test for RepeatablePageMasterAlternatives
  31. *
  32. */
  33. public class RepeatablePageMasterAlternativesTestCase implements Constants {
  34. /**
  35. *
  36. * @throws Exception exception
  37. */
  38. @Test
  39. public void testIsInfinite1() throws Exception {
  40. // Create fixture
  41. Property maximumRepeats = mock(Property.class);
  42. ConditionalPageMasterReference cpmr = createCPMR("empty");
  43. when(maximumRepeats.getEnum()).thenReturn(EN_NO_LIMIT);
  44. RepeatablePageMasterAlternatives objectUnderTest
  45. = createRepeatablePageMasterAlternatives(cpmr, maximumRepeats);
  46. assertTrue("is infinite", objectUnderTest.isInfinite());
  47. }
  48. /**
  49. *
  50. * @throws Exception exception
  51. */
  52. @Test
  53. public void testIsInfinite2() throws Exception {
  54. // Create fixture
  55. Property maximumRepeats = mock(Property.class);
  56. ConditionalPageMasterReference cpmr = createCPMR("empty");
  57. NumericProperty numericProperty = mock(NumericProperty.class);
  58. final int maxRepeatNum = 0;
  59. assertTrue(maxRepeatNum != EN_NO_LIMIT);
  60. when(maximumRepeats.getEnum()).thenReturn(maxRepeatNum);
  61. when(maximumRepeats.getNumeric()).thenReturn(numericProperty);
  62. RepeatablePageMasterAlternatives objectUnderTest
  63. = createRepeatablePageMasterAlternatives(createCPMR("empty"),
  64. maximumRepeats);
  65. assertTrue("is infinite", !objectUnderTest.isInfinite());
  66. }
  67. /**
  68. * Test that an infinite sequence of empty page masters has
  69. * willTerminiate() returning false
  70. * @throws Exception exception
  71. */
  72. @Test
  73. public void testCanProcess1() throws Exception {
  74. // Create fixture
  75. Property maximumRepeats = mock(Property.class);
  76. ConditionalPageMasterReference cpmr = createCPMR("empty");
  77. when(maximumRepeats.getEnum()).thenReturn(EN_NO_LIMIT);
  78. when(cpmr.isValid(anyBoolean(), anyBoolean(), anyBoolean(), anyBoolean()))
  79. .thenReturn(true);
  80. RepeatablePageMasterAlternatives objectUnderTest
  81. = createRepeatablePageMasterAlternatives(cpmr, maximumRepeats);
  82. //Fixture assertion
  83. assertTrue("Should be infinite", objectUnderTest.isInfinite());
  84. //Test assertion
  85. assertTrue("Infinite sequences that do not process the main flow will "
  86. + " not terminate",
  87. !objectUnderTest.canProcess("main-flow"));
  88. }
  89. /**
  90. * Test that a finite sequence of simple page masters has
  91. * willTerminate() returning true
  92. *
  93. * @throws Exception exception
  94. */
  95. @Test
  96. public void testCanProcess2() throws Exception {
  97. // Create fixture
  98. Property maximumRepeats = mock(Property.class);
  99. NumericProperty numericProperty = mock(NumericProperty.class);
  100. final int maxRepeatNum = 0;
  101. when(maximumRepeats.getEnum()).thenReturn(maxRepeatNum);
  102. when(maximumRepeats.getNumeric()).thenReturn(numericProperty);
  103. RepeatablePageMasterAlternatives objectUnderTest
  104. = createRepeatablePageMasterAlternatives(createCPMR("empty"),
  105. maximumRepeats);
  106. //Fixture assertion
  107. assertTrue("Should be finite sequence", !objectUnderTest.isInfinite());
  108. //Test assertion
  109. assertTrue("Finite sequences will terminate",
  110. objectUnderTest.canProcess("main-flow"));
  111. }
  112. private ConditionalPageMasterReference createCPMR(String regionName) {
  113. ConditionalPageMasterReference cpmr = mock(ConditionalPageMasterReference.class);
  114. SimplePageMaster master = mock(SimplePageMaster.class);
  115. Region region = mock(Region.class);
  116. when(master.getRegion(anyInt())).thenReturn(region);
  117. when(region.getRegionName()).thenReturn(regionName);
  118. when(cpmr.getMaster()).thenReturn(master);
  119. return cpmr;
  120. }
  121. private RepeatablePageMasterAlternatives createRepeatablePageMasterAlternatives(
  122. ConditionalPageMasterReference cpmr, Property maximumRepeats) throws Exception {
  123. PropertyList pList = mock(PropertyList.class);
  124. when(pList.get(anyInt())).thenReturn(maximumRepeats);
  125. PageSequenceMaster parent = mock(PageSequenceMaster.class);
  126. when(parent.getName()).thenReturn("fo:page-sequence-master");
  127. RepeatablePageMasterAlternatives sut = new RepeatablePageMasterAlternatives(parent);
  128. sut.startOfNode();
  129. sut.bind(pList);
  130. sut.addConditionalPageMasterReference(cpmr);
  131. return sut;
  132. }
  133. }