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.

HyphenationTestCase.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.io.ObjectOutputStream;
  24. import org.junit.Test;
  25. import static org.junit.Assert.assertEquals;
  26. import static org.junit.Assert.assertNull;
  27. import org.apache.commons.io.IOUtils;
  28. import org.apache.fop.apps.FopFactory;
  29. import org.apache.fop.apps.io.InternalResourceResolver;
  30. import org.apache.fop.apps.io.ResourceResolverFactory;
  31. import org.apache.fop.hyphenation.Hyphenation;
  32. import org.apache.fop.hyphenation.HyphenationException;
  33. import org.apache.fop.hyphenation.HyphenationTree;
  34. import org.apache.fop.hyphenation.Hyphenator;
  35. public class HyphenationTestCase {
  36. private FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
  37. @Test
  38. public void testHyphenator() {
  39. File f = new File("test/resources/fop");
  40. InternalResourceResolver resourceResolver = ResourceResolverFactory.createDefaultInternalResourceResolver(
  41. f.toURI());
  42. Hyphenation hyph = Hyphenator.hyphenate("fr.xml" + Hyphenator.XMLTYPE, null, resourceResolver, null,
  43. "hello", 0, 0, fopFactory.newFOUserAgent());
  44. assertEquals(hyph.toString(), "-hel-lo");
  45. }
  46. @Test
  47. public void testHyphenatorBinary() throws HyphenationException, IOException {
  48. File f = File.createTempFile("hyp", "fop");
  49. f.delete();
  50. f.mkdir();
  51. InternalResourceResolver resourceResolver = ResourceResolverFactory.createDefaultInternalResourceResolver(
  52. f.toURI());
  53. HyphenationTree hTree = new HyphenationTree();
  54. hTree.loadPatterns(new File("test/resources/fop/fr.xml").getAbsolutePath());
  55. File hyp = new File(f, "fr.hyp");
  56. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(hyp));
  57. out.writeObject(hTree);
  58. out.close();
  59. Hyphenation hyph = Hyphenator.hyphenate("fr.hyp" + Hyphenator.HYPTYPE, null, resourceResolver, null,
  60. "oello", 0, 0, fopFactory.newFOUserAgent());
  61. assertEquals(hyph.toString(), "oel-lo");
  62. hyp.delete();
  63. f.delete();
  64. }
  65. @Test
  66. public void testHyphenatorCache() throws IOException {
  67. File f = File.createTempFile("hyp", "fop");
  68. f.delete();
  69. f.mkdir();
  70. File frxml = new File(f, "fr.xml");
  71. IOUtils.copy(new FileInputStream("test/resources/fop/fr.xml"), new FileOutputStream(frxml));
  72. InternalResourceResolver resourceResolver = ResourceResolverFactory.createDefaultInternalResourceResolver(
  73. f.toURI());
  74. Hyphenation hyph = Hyphenator.hyphenate("fr.xml" + Hyphenator.XMLTYPE, null, resourceResolver, null,
  75. "hello", 0, 0, fopFactory.newFOUserAgent());
  76. assertEquals(hyph.toString(), "-hel-lo");
  77. FileOutputStream fos = new FileOutputStream(frxml);
  78. fos.write(("<hyphenation-info></hyphenation-info>").getBytes());
  79. fos.close();
  80. fopFactory = FopFactory.newInstance(new File(".").toURI());
  81. hyph = Hyphenator.hyphenate("fr.xml" + Hyphenator.XMLTYPE, null, resourceResolver, null,
  82. "hello", 0, 0, fopFactory.newFOUserAgent());
  83. assertNull(hyph);
  84. frxml.delete();
  85. f.delete();
  86. }
  87. }