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