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

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