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.5KB

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. PDFStructElem th = new PDFStructElem(null, Table.TH);
  46. document.registerStructureElement(th, scope);
  47. verify(controller).addTableHeaderScopeAttribute(any(PDFStructElem.class), eq(scope));
  48. }
  49. @Test
  50. public void versionControllerMayDelegateToScope() {
  51. fixedController14doesNotAddAttribute();
  52. fixedController15addsAttribute();
  53. dynamicControllerAddsAttribute();
  54. }
  55. private void fixedController14doesNotAddAttribute() {
  56. controller = VersionController.getFixedVersionController(Version.V1_4);
  57. scopeMustNotBeAdded();
  58. }
  59. private void fixedController15addsAttribute() {
  60. controller = VersionController.getFixedVersionController(Version.V1_5);
  61. scopeMustBeAdded();
  62. }
  63. private void dynamicControllerAddsAttribute() {
  64. PDFDocument document = new PDFDocument("Test");
  65. controller = VersionController.getDynamicVersionController(Version.V1_4, document);
  66. scopeMustBeAdded();
  67. assertEquals(Version.V1_5, controller.getPDFVersion());
  68. }
  69. private void scopeMustBeAdded() {
  70. scopeMustBeAdded(times(1));
  71. }
  72. private void scopeMustNotBeAdded() {
  73. scopeMustBeAdded(never());
  74. }
  75. private void scopeMustBeAdded(VerificationMode nTimes) {
  76. PDFStructElem structElem = mock(PDFStructElem.class);
  77. controller.addTableHeaderScopeAttribute(structElem, Scope.COLUMN);
  78. verify(structElem, nTimes).put(eq(ATTRIBUTE_ENTRY), any());
  79. }
  80. @Test
  81. public void scopeAddsTheAttribute() {
  82. for (Scope scope : Scope.values()) {
  83. scopeAttributeMustBeAdded(scope);
  84. }
  85. }
  86. private void scopeAttributeMustBeAdded(Scope scope) {
  87. PDFStructElem structElem = mock(PDFStructElem.class);
  88. Scope.addScopeAttribute(structElem, scope);
  89. verify(structElem).put(eq(ATTRIBUTE_ENTRY), scopeAttribute(scope));
  90. }
  91. private PDFDictionary scopeAttribute(Scope scope) {
  92. return argThat(new IsScopeAttribute(scope));
  93. }
  94. private static class IsScopeAttribute implements ArgumentMatcher<PDFDictionary> {
  95. private final Scope expectedScope;
  96. public IsScopeAttribute(Scope expectedScope) {
  97. this.expectedScope = expectedScope;
  98. }
  99. @Override
  100. public boolean matches(PDFDictionary argument) {
  101. PDFDictionary attribute = argument;
  102. return "/Table".equals(attribute.get("O").toString())
  103. && expectedScope.getName().toString().equals(attribute.get("Scope").toString());
  104. }
  105. }
  106. }