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.

RendererFactoryTestCase.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.render;
  19. import static org.junit.Assert.assertTrue;
  20. import static org.junit.Assert.fail;
  21. import org.junit.Test;
  22. import org.apache.commons.io.output.NullOutputStream;
  23. import org.apache.fop.apps.FOPException;
  24. import org.apache.fop.apps.FOUserAgent;
  25. import org.apache.fop.apps.FopFactory;
  26. import org.apache.fop.apps.MimeConstants;
  27. import org.apache.fop.area.AreaTreeHandler;
  28. import org.apache.fop.fo.FOEventHandler;
  29. import org.apache.fop.render.intermediate.IFContext;
  30. import org.apache.fop.render.intermediate.IFDocumentHandler;
  31. import org.apache.fop.render.intermediate.IFRenderer;
  32. import org.apache.fop.render.pdf.PDFDocumentHandler;
  33. import org.apache.fop.render.rtf.RTFHandler;
  34. /**
  35. * Tests for {@link RendererFactory}.
  36. */
  37. public class RendererFactoryTestCase {
  38. @Test
  39. public void testDocumentHandlerLevel() throws Exception {
  40. FopFactory fopFactory = FopFactory.newInstance();
  41. RendererFactory factory = fopFactory.getRendererFactory();
  42. FOUserAgent ua;
  43. IFDocumentHandler handler;
  44. IFDocumentHandler overrideHandler;
  45. ua = fopFactory.newFOUserAgent();
  46. handler = factory.createDocumentHandler(ua, MimeConstants.MIME_PDF);
  47. ua = fopFactory.newFOUserAgent();
  48. overrideHandler = new PDFDocumentHandler();
  49. overrideHandler.setContext(new IFContext(ua));
  50. ua.setDocumentHandlerOverride(overrideHandler);
  51. handler = factory.createDocumentHandler(ua, null);
  52. assertTrue(handler == overrideHandler);
  53. ua = fopFactory.newFOUserAgent();
  54. try {
  55. handler = factory.createDocumentHandler(ua, "invalid/format");
  56. fail("Expected UnsupportedOperationException");
  57. } catch (UnsupportedOperationException uoe) {
  58. //expected
  59. }
  60. }
  61. @Test
  62. public void testRendererLevel() throws Exception {
  63. FopFactory fopFactory = FopFactory.newInstance();
  64. RendererFactory factory = fopFactory.getRendererFactory();
  65. FOUserAgent ua;
  66. Renderer renderer;
  67. ua = fopFactory.newFOUserAgent();
  68. renderer = factory.createRenderer(ua, MimeConstants.MIME_PDF);
  69. assertTrue(renderer instanceof IFRenderer);
  70. ua = fopFactory.newFOUserAgent();
  71. renderer = factory.createRenderer(ua, MimeConstants.MIME_FOP_IF);
  72. assertTrue(renderer instanceof IFRenderer);
  73. ua = fopFactory.newFOUserAgent();
  74. IFDocumentHandler overrideHandler;
  75. overrideHandler = new PDFDocumentHandler();
  76. overrideHandler.setContext(new IFContext(ua));
  77. ua.setDocumentHandlerOverride(overrideHandler);
  78. renderer = factory.createRenderer(ua, null);
  79. assertTrue(renderer instanceof IFRenderer);
  80. ua = fopFactory.newFOUserAgent();
  81. try {
  82. renderer = factory.createRenderer(ua, "invalid/format");
  83. fail("Expected UnsupportedOperationException");
  84. } catch (UnsupportedOperationException uoe) {
  85. //expected
  86. }
  87. }
  88. @Test
  89. public void testFOEventHandlerLevel() throws Exception {
  90. FopFactory fopFactory = FopFactory.newInstance();
  91. RendererFactory factory = fopFactory.getRendererFactory();
  92. FOUserAgent ua;
  93. FOEventHandler foEventHandler;
  94. FOEventHandler overrideFOEventHandler;
  95. ua = fopFactory.newFOUserAgent();
  96. foEventHandler = factory.createFOEventHandler(
  97. ua, MimeConstants.MIME_PDF, new NullOutputStream());
  98. assertTrue(foEventHandler instanceof AreaTreeHandler);
  99. ua = fopFactory.newFOUserAgent();
  100. foEventHandler = factory.createFOEventHandler(
  101. ua, MimeConstants.MIME_RTF, new NullOutputStream());
  102. assertTrue(foEventHandler instanceof RTFHandler);
  103. ua = fopFactory.newFOUserAgent();
  104. try {
  105. foEventHandler = factory.createFOEventHandler(
  106. ua, "invalid/format", new NullOutputStream());
  107. fail("Expected UnsupportedOperationException");
  108. } catch (UnsupportedOperationException uoe) {
  109. //expected
  110. }
  111. ua = fopFactory.newFOUserAgent();
  112. try {
  113. foEventHandler = factory.createFOEventHandler(
  114. ua, MimeConstants.MIME_PDF, null);
  115. fail("Expected FOPException because of missing OutputStream");
  116. } catch (FOPException fe) {
  117. //expected
  118. }
  119. ua = fopFactory.newFOUserAgent();
  120. overrideFOEventHandler = new RTFHandler(ua, new NullOutputStream());
  121. ua.setFOEventHandlerOverride(overrideFOEventHandler);
  122. foEventHandler = factory.createFOEventHandler(
  123. ua, null, null);
  124. assertTrue(foEventHandler == overrideFOEventHandler);
  125. }
  126. }