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.

FONodeMocks.java 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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;
  19. import static org.mockito.Matchers.any;
  20. import static org.mockito.Matchers.anyString;
  21. import static org.mockito.Mockito.mock;
  22. import static org.mockito.Mockito.when;
  23. import java.io.IOException;
  24. import org.apache.xmlgraphics.image.loader.ImageException;
  25. import org.apache.xmlgraphics.image.loader.ImageManager;
  26. import org.apache.xmlgraphics.image.loader.ImageSessionContext;
  27. import org.apache.fop.apps.FOUserAgent;
  28. import org.apache.fop.apps.FopFactory;
  29. /**
  30. * A helper class for creating mocks of {@link FONode} and its descendants.
  31. */
  32. public final class FONodeMocks {
  33. private FONodeMocks() { }
  34. /**
  35. * Creates and returns a mock {@link FONode} configured with a mock
  36. * {@link FOEventHandler}. The FO event handler returns a mock {@link FOUserAgent},
  37. * which in turn returns a mock {@link FopFactory}, which returns a mock
  38. * {@link ImageManager}.
  39. *
  40. * @return a mock FO node
  41. */
  42. public static FONode mockFONode() {
  43. FONode mockFONode = mock(FONode.class);
  44. mockGetFOEventHandler(mockFONode);
  45. return mockFONode;
  46. }
  47. private static void mockGetFOEventHandler(FONode mockFONode) {
  48. FOEventHandler mockFOEventHandler = mock(FOEventHandler.class);
  49. mockGetUserAgent(mockFOEventHandler);
  50. when(mockFONode.getFOEventHandler()).thenReturn(mockFOEventHandler);
  51. }
  52. private static void mockGetUserAgent(FOEventHandler mockFOEventHandler) {
  53. FOUserAgent mockFOUserAgent = mock(FOUserAgent.class);
  54. mockGetFactory(mockFOUserAgent);
  55. when(mockFOEventHandler.getUserAgent()).thenReturn(mockFOUserAgent);
  56. }
  57. private static void mockGetFactory(FOUserAgent mockFOUserAgent) {
  58. FopFactory mockFopFactory = mock(FopFactory.class);
  59. mockGetImageManager(mockFopFactory);
  60. when(mockFOUserAgent.getFactory()).thenReturn(mockFopFactory);
  61. }
  62. private static void mockGetImageManager(FopFactory mockFopFactory) {
  63. try {
  64. ImageManager mockImageManager = mock(ImageManager.class);
  65. when(mockImageManager.getImageInfo(anyString(), any(ImageSessionContext.class)))
  66. .thenReturn(null);
  67. when(mockFopFactory.getImageManager()).thenReturn(mockImageManager);
  68. } catch (ImageException e) {
  69. throw new RuntimeException(e);
  70. } catch (IOException e) {
  71. throw new RuntimeException(e);
  72. }
  73. }
  74. }