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.

URIResolverWrapperTestCase.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.apps.io;
  19. import java.io.IOException;
  20. import java.net.URI;
  21. import java.net.URISyntaxException;
  22. import java.util.Arrays;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import static org.junit.Assert.assertEquals;
  28. import static org.junit.Assert.assertNull;
  29. import static org.junit.Assert.fail;
  30. import static org.mockito.Matchers.eq;
  31. import static org.mockito.Mockito.mock;
  32. import static org.mockito.Mockito.verify;
  33. import org.apache.xmlgraphics.io.ResourceResolver;
  34. public class URIResolverWrapperTestCase {
  35. private static final List<String> BASE_URIS = Collections.unmodifiableList(Arrays.asList(
  36. new String[] {
  37. ".",
  38. "../",
  39. "some/path",
  40. "file:///absolute/file/path"}
  41. ));
  42. private URI base;
  43. @Before
  44. public void setup() throws URISyntaxException {
  45. setBase(".");
  46. }
  47. private void setBase(String baseStr) throws URISyntaxException {
  48. base = new URI(baseStr);
  49. }
  50. @Test
  51. public void testResolveIn() throws Exception {
  52. String[] uris = new String[] {".", "resource", "path/to/resource"};
  53. for (String base : BASE_URIS) {
  54. setBase(base);
  55. for (String uriStr : uris) {
  56. URI uri = new URI(uriStr);
  57. URI expected = resolveFromBase(uri);
  58. test(uriStr, uri, expected);
  59. }
  60. }
  61. }
  62. @Test
  63. public void testResolveInBadUri() throws Exception {
  64. String[] uris = new String[] {"path\\to\\resource", "bad resource name"};
  65. for (String base : BASE_URIS) {
  66. setBase(base);
  67. for (String uriStr : uris) {
  68. assertBadSyntax(uriStr);
  69. URI uri = cleanURI(uriStr);
  70. URI expected = resolveFromBase(uri);
  71. test(uriStr, uri, expected);
  72. }
  73. }
  74. }
  75. @Test
  76. public void getBaseURI() throws URISyntaxException {
  77. assertEquals(InternalResourceResolver.getBaseURI("x/y/z/"), new URI("x/y/z/"));
  78. assertEquals(InternalResourceResolver.getBaseURI("x/y/z"), new URI("x/y/z/"));
  79. }
  80. @Test
  81. public void cleanURI() throws URISyntaxException {
  82. String[] uris = new String[] {".", "path/to/resource", "path\\to\\resource",
  83. "bad resource name"};
  84. for (String uri : uris) {
  85. assertEquals(InternalResourceResolver.cleanURI(uri), cleanURI(uri));
  86. }
  87. assertNull(InternalResourceResolver.cleanURI(null));
  88. }
  89. private void test(String uriStr, URI uri, URI expected) throws IOException, URISyntaxException {
  90. ResourceResolver resolver = mock(ResourceResolver.class);
  91. InternalResourceResolver sut = new InternalResourceResolver(base, resolver);
  92. sut.getResource(uriStr);
  93. verify(resolver).getResource(eq(expected));
  94. resolver = mock(ResourceResolver.class);
  95. sut = new InternalResourceResolver(base, resolver);
  96. sut.getResource(uri);
  97. verify(resolver).getResource(eq(expected));
  98. }
  99. private URI resolveFromBase(URI uri) {
  100. return base.resolve(uri);
  101. }
  102. private URI cleanURI(String raw) throws URISyntaxException {
  103. String fixedUri = raw.replace('\\', '/');
  104. fixedUri = fixedUri.replace(" ", "%20");
  105. return new URI(fixedUri);
  106. }
  107. private void assertBadSyntax(String badUri) {
  108. try {
  109. new URI(badUri);
  110. fail(badUri + " is correctly formed.");
  111. } catch (URISyntaxException e) {
  112. // PASS
  113. }
  114. }
  115. }