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.

HeaderColumnTestCase.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.flow.table;
  19. import org.junit.Test;
  20. import org.xml.sax.Attributes;
  21. import org.xml.sax.Locator;
  22. import org.xml.sax.helpers.AttributesImpl;
  23. import static org.junit.Assert.assertEquals;
  24. import static org.mockito.ArgumentMatchers.nullable;
  25. import static org.mockito.Matchers.any;
  26. import static org.mockito.Matchers.anyString;
  27. import static org.mockito.Matchers.eq;
  28. import static org.mockito.Mockito.mock;
  29. import static org.mockito.Mockito.verify;
  30. import static org.mockito.Mockito.when;
  31. import org.apache.fop.apps.FOUserAgent;
  32. import org.apache.fop.events.EventBroadcaster;
  33. import org.apache.fop.fo.ElementMappingRegistry;
  34. import org.apache.fop.fo.FOEventHandler;
  35. import org.apache.fop.fo.FONodeMocks;
  36. import org.apache.fop.fo.FOValidationEventProducer;
  37. import org.apache.fop.fo.PropertyList;
  38. import org.apache.fop.fo.StaticPropertyList;
  39. import org.apache.fop.fo.ValidationException;
  40. import org.apache.fop.fo.expr.PropertyException;
  41. import org.apache.fop.fo.extensions.ExtensionElementMapping;
  42. import org.apache.fop.util.XMLUtil;
  43. /**
  44. * Tests that the fox:header property is correctly parsed and set up at the FO tree level.
  45. */
  46. public class HeaderColumnTestCase {
  47. @Test
  48. public void testWrongValue() throws ValidationException {
  49. Table parent = createTableParent();
  50. EventBroadcaster mockEventBroadcaster = FONodeMocks.mockGetEventBroadcaster(
  51. parent.getFOEventHandler().getUserAgent());
  52. FOValidationEventProducer eventProducer = mockGetEventProducerFor(mockEventBroadcaster);
  53. TableColumn column = new TableColumn(parent);
  54. PropertyList propertyList = new StaticPropertyList(column, null);
  55. Attributes atts = createScopeAttribute("blah");
  56. propertyList.addAttributesToList(atts);
  57. verify(eventProducer).invalidPropertyValue(any(), eq("fo:table-column"),
  58. eq("fox:header"), eq("blah"), any(PropertyException.class), nullable(Locator.class));
  59. }
  60. @Test
  61. public void testCorrectValue() throws Exception {
  62. testCorrectValue(true);
  63. testCorrectValue(false);
  64. }
  65. private void testCorrectValue(boolean expectedValue) throws Exception {
  66. Table parent = createTableParent();
  67. FONodeMocks.mockGetColumnNumberManager(parent);
  68. TableColumn column = new TableColumn(parent, true);
  69. PropertyList propertyList = new StaticPropertyList(column, null);
  70. Attributes atts = createScopeAttribute(String.valueOf(expectedValue));
  71. propertyList.addAttributesToList(atts);
  72. column.bind(propertyList);
  73. assertEquals(expectedValue, column.isHeader());
  74. }
  75. private Table createTableParent() {
  76. Table parent = mock(Table.class);
  77. FOEventHandler mockFOEventHandler = FONodeMocks.mockGetFOEventHandler(parent);
  78. FOUserAgent mockUserAgent = mockFOEventHandler.getUserAgent();
  79. mockGetElementMappingRegistry(mockUserAgent);
  80. return parent;
  81. }
  82. private Attributes createScopeAttribute(String value) {
  83. AttributesImpl atts = new AttributesImpl();
  84. atts.addAttribute(ExtensionElementMapping.URI, "header", "fox:header", XMLUtil.CDATA, value);
  85. return atts;
  86. }
  87. private ElementMappingRegistry mockGetElementMappingRegistry(FOUserAgent mockUserAgent) {
  88. ElementMappingRegistry mockRegistry = mock(ElementMappingRegistry.class);
  89. when(mockRegistry.getElementMapping(anyString())).thenReturn(new ExtensionElementMapping());
  90. when(mockUserAgent.getElementMappingRegistry()).thenReturn(mockRegistry);
  91. return mockRegistry;
  92. }
  93. private FOValidationEventProducer mockGetEventProducerFor(EventBroadcaster mockEventBroadcaster) {
  94. FOValidationEventProducer mockEventProducer = mock(FOValidationEventProducer.class);
  95. when(mockEventBroadcaster.getEventProducerFor(eq(FOValidationEventProducer.class)))
  96. .thenReturn(mockEventProducer);
  97. return mockEventProducer;
  98. }
  99. }