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.

TableHeaderScopeTestCase.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.pdf;
  19. import org.junit.Test;
  20. import org.mockito.ArgumentMatcher;
  21. import org.mockito.verification.VerificationMode;
  22. import static org.junit.Assert.assertEquals;
  23. import static org.mockito.Matchers.any;
  24. import static org.mockito.Matchers.argThat;
  25. import static org.mockito.Matchers.eq;
  26. import static org.mockito.Mockito.mock;
  27. import static org.mockito.Mockito.never;
  28. import static org.mockito.Mockito.times;
  29. import static org.mockito.Mockito.verify;
  30. import org.apache.fop.pdf.StandardStructureAttributes.Table.Scope;
  31. import org.apache.fop.pdf.StandardStructureTypes.Table;
  32. public class TableHeaderScopeTestCase {
  33. private static final String ATTRIBUTE_ENTRY = "A";
  34. private VersionController controller;
  35. @Test
  36. public void pdfDocumentDelegatesToVersionController() {
  37. for (Scope scope : Scope.values()) {
  38. testMakeStructureElementWithScope(scope);
  39. }
  40. }
  41. private void testMakeStructureElementWithScope(Scope scope) {
  42. VersionController controller = mock(VersionController.class);
  43. PDFDocument document = new PDFDocument("Test", controller);
  44. document.makeStructTreeRoot(null);
  45. document.makeStructureElement(Table.TH, null, scope);
  46. verify(controller).addTableHeaderScopeAttribute(any(PDFStructElem.class), eq(scope));
  47. }
  48. @Test
  49. public void versionControllerMayDelegateToScope() {
  50. fixedController14doesNotAddAttribute();
  51. fixedController15addsAttribute();
  52. dynamicControllerAddsAttribute();
  53. }
  54. private void fixedController14doesNotAddAttribute() {
  55. controller = VersionController.getFixedVersionController(Version.V1_4);
  56. scopeMustNotBeAdded();
  57. }
  58. private void fixedController15addsAttribute() {
  59. controller = VersionController.getFixedVersionController(Version.V1_5);
  60. scopeMustBeAdded();
  61. }
  62. private void dynamicControllerAddsAttribute() {
  63. PDFDocument document = new PDFDocument("Test");
  64. controller = VersionController.getDynamicVersionController(Version.V1_4, document);
  65. scopeMustBeAdded();
  66. assertEquals(Version.V1_5, controller.getPDFVersion());
  67. }
  68. private void scopeMustBeAdded() {
  69. scopeMustBeAdded(times(1));
  70. }
  71. private void scopeMustNotBeAdded() {
  72. scopeMustBeAdded(never());
  73. }
  74. private void scopeMustBeAdded(VerificationMode nTimes) {
  75. PDFStructElem structElem = mock(PDFStructElem.class);
  76. controller.addTableHeaderScopeAttribute(structElem, Scope.COLUMN);
  77. verify(structElem, nTimes).put(eq(ATTRIBUTE_ENTRY), any());
  78. }
  79. @Test
  80. public void scopeAddsTheAttribute() {
  81. for (Scope scope : Scope.values()) {
  82. scopeAttributeMustBeAdded(scope);
  83. }
  84. }
  85. private void scopeAttributeMustBeAdded(Scope scope) {
  86. PDFStructElem structElem = mock(PDFStructElem.class);
  87. Scope.addScopeAttribute(structElem, scope);
  88. verify(structElem).put(eq(ATTRIBUTE_ENTRY), scopeAttribute(scope));
  89. }
  90. private PDFDictionary scopeAttribute(Scope scope) {
  91. return argThat(new isScopeAttribute(scope));
  92. }
  93. private static class isScopeAttribute extends ArgumentMatcher<PDFDictionary> {
  94. private final Scope expectedScope;
  95. public isScopeAttribute(Scope expectedScope) {
  96. this.expectedScope = expectedScope;
  97. }
  98. @Override
  99. public boolean matches(Object argument) {
  100. PDFDictionary attribute = (PDFDictionary) argument;
  101. return "/Table".equals(attribute.get("O").toString())
  102. && expectedScope.getName().toString().equals(attribute.get("Scope").toString());
  103. }
  104. }
  105. }