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.

ConserveMemoryTestCase.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. package org.apache.fop.render;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.File;
  21. import java.util.concurrent.ExecutorService;
  22. import java.util.concurrent.Executors;
  23. import java.util.concurrent.TimeUnit;
  24. import javax.xml.transform.Result;
  25. import javax.xml.transform.Source;
  26. import javax.xml.transform.Transformer;
  27. import javax.xml.transform.TransformerException;
  28. import javax.xml.transform.TransformerFactory;
  29. import javax.xml.transform.sax.SAXResult;
  30. import javax.xml.transform.stream.StreamSource;
  31. import org.junit.Test;
  32. import org.xml.sax.SAXException;
  33. import org.apache.fop.apps.FOUserAgent;
  34. import org.apache.fop.apps.Fop;
  35. import org.apache.fop.apps.FopFactory;
  36. public class ConserveMemoryTestCase {
  37. @Test
  38. public void testLink() throws Throwable {
  39. final String fo = "<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">\n"
  40. + " <fo:layout-master-set>\n"
  41. + " <fo:simple-page-master master-name=\"simple\" page-height=\"27.9cm\" page-width=\"21.6cm\">\n"
  42. + " <fo:region-body />\n"
  43. + " </fo:simple-page-master>\n"
  44. + " </fo:layout-master-set>\n"
  45. + " <fo:page-sequence master-reference=\"simple\">\n"
  46. + " <fo:flow flow-name=\"xsl-region-body\">\n"
  47. + " <fo:block><fo:basic-link internal-destination=\"a\">a</fo:basic-link></fo:block>\n"
  48. + " </fo:flow>\n"
  49. + " </fo:page-sequence>\n"
  50. + "</fo:root>";
  51. ExecutorService es = Executors.newCachedThreadPool();
  52. final Throwable[] ex = new Throwable[1];
  53. for (int i = 0; i < 5; i++) {
  54. Runnable thread = new Runnable() {
  55. public void run() {
  56. try {
  57. foToOutput(fo);
  58. } catch (Throwable e) {
  59. ex[0] = e;
  60. }
  61. }
  62. };
  63. es.execute(thread);
  64. }
  65. es.shutdown();
  66. es.awaitTermination(1, TimeUnit.MINUTES);
  67. if (ex[0] != null) {
  68. throw ex[0];
  69. }
  70. }
  71. private void foToOutput(String fo) throws SAXException, TransformerException {
  72. FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
  73. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  74. userAgent.setConserveMemoryPolicy(true);
  75. userAgent.setAccessibility(true);
  76. Fop fop = fopFactory.newFop("application/pdf", userAgent, new ByteArrayOutputStream());
  77. Transformer transformer = TransformerFactory.newInstance().newTransformer();
  78. Source src = new StreamSource(new ByteArrayInputStream(fo.getBytes()));
  79. Result res = new SAXResult(fop.getDefaultHandler());
  80. transformer.transform(src, res);
  81. }
  82. }