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 18 package org.apache.fop.afp.util; 19 20 import java.io.IOException; 21 import java.net.URI; 22 import java.net.URISyntaxException; 23 24 import org.junit.Before; 25 import org.junit.Test; 26 27 import static org.junit.Assert.assertEquals; 28 import static org.mockito.Mockito.mock; 29 import static org.mockito.Mockito.verify; 30 31 import org.apache.fop.apps.io.InternalResourceResolver; 32 33 public class AFPResourceAccessorTestCase { 34 35 private InternalResourceResolver nullBaseResourceResolver; 36 private InternalResourceResolver absoluteBaseResourceResolver; 37 private InternalResourceResolver relativeBaseResourceResolver; 38 private final URI absoluteBaseURI = URI.create("this:///purely.for.testing"); 39 private final URI relativeBaseURI = URI.create("./this.is.purely.for.testing"); 40 private AFPResourceAccessor nullBaseURISut; 41 private AFPResourceAccessor absoluteBaseURISut; 42 private AFPResourceAccessor relativeBaseURISut; 43 44 @Before setUp()45 public void setUp() { 46 nullBaseResourceResolver = mock(InternalResourceResolver.class); 47 absoluteBaseResourceResolver = mock(InternalResourceResolver.class); 48 relativeBaseResourceResolver = mock(InternalResourceResolver.class); 49 nullBaseURISut = new AFPResourceAccessor(nullBaseResourceResolver); 50 absoluteBaseURISut = new AFPResourceAccessor(absoluteBaseResourceResolver, 51 absoluteBaseURI.toASCIIString()); 52 relativeBaseURISut = new AFPResourceAccessor(relativeBaseResourceResolver, 53 relativeBaseURI.toASCIIString()); 54 } 55 56 @Test testCreateInputStream()57 public void testCreateInputStream() throws IOException, URISyntaxException { 58 URI testURI = URI.create("test"); 59 nullBaseURISut.createInputStream(testURI); 60 verify(nullBaseResourceResolver).getResource(testURI); 61 62 absoluteBaseURISut.createInputStream(testURI); 63 verify(absoluteBaseResourceResolver).getResource(getActualURI(absoluteBaseURI, testURI)); 64 65 relativeBaseURISut.createInputStream(testURI); 66 verify(relativeBaseResourceResolver).getResource(getActualURI(relativeBaseURI, testURI)); 67 } 68 getActualURI(URI baseURI, URI testURI)69 private URI getActualURI(URI baseURI, URI testURI) throws URISyntaxException { 70 return InternalResourceResolver.getBaseURI(baseURI.toASCIIString()).resolve(testURI); 71 } 72 73 @Test testResolveURI()74 public void testResolveURI() throws URISyntaxException { 75 String testURI = "anotherTestURI"; 76 assertEquals(URI.create("./" + testURI), nullBaseURISut.resolveURI(testURI)); 77 78 assertEquals(getActualURI(absoluteBaseURI, URI.create(testURI)), 79 absoluteBaseURISut.resolveURI(testURI)); 80 81 assertEquals(getActualURI(relativeBaseURI, URI.create(testURI)), 82 relativeBaseURISut.resolveURI(testURI)); 83 } 84 } 85