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.

FontManagerConfiguratorTestCase.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.fonts;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.net.URI;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import org.xml.sax.SAXException;
  25. import static org.junit.Assert.assertEquals;
  26. import org.apache.fop.apps.FOPException;
  27. import org.apache.fop.apps.FopConfBuilder;
  28. import org.apache.fop.apps.FopConfParser;
  29. import org.apache.fop.apps.FopFactory;
  30. import static org.apache.fop.apps.FopConfParserTestCase.getFopFactory;
  31. /**
  32. * A test case for {@link FontManagerConfigurator}.
  33. */
  34. public class FontManagerConfiguratorTestCase {
  35. private FopConfBuilder builder;
  36. public final URI baseURI = new File("test/config/").getAbsoluteFile().toURI();
  37. @Before
  38. public void setUp() {
  39. builder = new FopConfBuilder();
  40. }
  41. private FontManager setBaseAndGetManager(String fontBase) {
  42. builder.setFontBaseURI(fontBase);
  43. return getManager();
  44. }
  45. private FontManager getManager() {
  46. FopFactory factory = getFopFactory(builder.build(), baseURI);
  47. return factory.getFontManager();
  48. }
  49. @Test(expected = FOPException.class)
  50. public void invalidURI() throws SAXException, IOException {
  51. builder.setFontBaseURI("$$%%**~{}][");
  52. FopConfParser confParser = new FopConfParser(builder.build(), baseURI);
  53. confParser.getFopFactoryBuilder().build();
  54. }
  55. @Test
  56. public void relativeFontBaseURITest() {
  57. String actualBase = "../../resources/fonts/ttf/";
  58. FontManager fontManager = setBaseAndGetManager(actualBase);
  59. URI expectedURI = baseURI.resolve(actualBase);
  60. assertEquals(expectedURI, fontManager.getResourceResolver().getBaseURI());
  61. }
  62. @Test
  63. public void currentRelativeFontBaseTest() {
  64. String actualBase = ".";
  65. FontManager fontManager = setBaseAndGetManager(actualBase);
  66. assertEquals(baseURI, fontManager.getResourceResolver().getBaseURI());
  67. }
  68. /**
  69. * This test is an interesting one; it's basically testing that if a base URI pointing to a
  70. * directory that doesn't exist is used, an error is not thrown. The URI resolver should handle
  71. * any {@link java.io.FileNotFoundException}s, not the configuration. We're NOT testing whether a font
  72. * can be resolved here, just that the URI resolver accepts it as its base URI.
  73. */
  74. @Test
  75. public void fontBaseDoesntExist() {
  76. // TODO: Sort this out
  77. String actualBase = "non-existing-dir/";
  78. FontManager fontManager = setBaseAndGetManager(actualBase);
  79. assertEquals(baseURI.resolve("non-existing-dir/"),
  80. fontManager.getResourceResolver().getBaseURI());
  81. }
  82. /**
  83. * Tests that when no <font-base> is given, it falls back to the URI used in <base>.
  84. */
  85. @Test
  86. public void noFontBaseURITest() {
  87. String actualBase = "../../resources/images/";
  88. builder.setBaseURI(actualBase);
  89. FontManager fontManager = getManager();
  90. assertEquals(baseURI.resolve(actualBase),
  91. fontManager.getResourceResolver().getBaseURI());
  92. }
  93. @Test
  94. public void absoluteBaseURI() {
  95. String absoluteBase = "test:///absolute/";
  96. FontManager fontManager = setBaseAndGetManager(absoluteBase);
  97. assertEquals(URI.create(absoluteBase), fontManager.getResourceResolver().getBaseURI());
  98. }
  99. }