From f7b41bf07e2d0c29d5955fe42c67051735bc86bc Mon Sep 17 00:00:00 2001 From: Mehdi Houshmand Date: Fri, 10 Aug 2012 13:49:59 +0000 Subject: [PATCH] Added tests for AFPResourceAccessor git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1371702 13f79535-47bb-0310-9956-ffa450edef68 --- .../afp/util/AFPResourceAccessorTestCase.java | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 test/java/org/apache/fop/afp/util/AFPResourceAccessorTestCase.java diff --git a/test/java/org/apache/fop/afp/util/AFPResourceAccessorTestCase.java b/test/java/org/apache/fop/afp/util/AFPResourceAccessorTestCase.java new file mode 100644 index 000000000..d729d336d --- /dev/null +++ b/test/java/org/apache/fop/afp/util/AFPResourceAccessorTestCase.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.fop.afp.util; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import org.apache.fop.apps.io.InternalResourceResolver; + +public class AFPResourceAccessorTestCase { + + private InternalResourceResolver nullBaseResourceResolver; + private InternalResourceResolver absoluteBaseResourceResolver; + private InternalResourceResolver relativeBaseResourceResolver; + private final URI absoluteBaseURI = URI.create("this:///purely.for.testing"); + private final URI relativeBaseURI = URI.create("./this.is.purely.for.testing"); + private AFPResourceAccessor nullBaseURISut; + private AFPResourceAccessor absoluteBaseURISut; + private AFPResourceAccessor relativeBaseURISut; + + @Before + public void setUp() { + nullBaseResourceResolver = mock(InternalResourceResolver.class); + absoluteBaseResourceResolver = mock(InternalResourceResolver.class); + relativeBaseResourceResolver = mock(InternalResourceResolver.class); + nullBaseURISut = new AFPResourceAccessor(nullBaseResourceResolver); + absoluteBaseURISut = new AFPResourceAccessor(absoluteBaseResourceResolver, + absoluteBaseURI.toASCIIString()); + relativeBaseURISut = new AFPResourceAccessor(relativeBaseResourceResolver, + relativeBaseURI.toASCIIString()); + } + + @Test + public void testCreateInputStream() throws IOException, URISyntaxException { + URI testURI = URI.create("test"); + nullBaseURISut.createInputStream(testURI); + verify(nullBaseResourceResolver).getResource(testURI); + + absoluteBaseURISut.createInputStream(testURI); + verify(absoluteBaseResourceResolver).getResource(getActualURI(absoluteBaseURI, testURI)); + + relativeBaseURISut.createInputStream(testURI); + verify(relativeBaseResourceResolver).getResource(getActualURI(relativeBaseURI, testURI)); + } + + private URI getActualURI(URI baseURI, URI testURI) throws URISyntaxException { + return InternalResourceResolver.getBaseURI(baseURI.toASCIIString()).resolve(testURI); + } + + @Test + public void testResolveURI() throws URISyntaxException { + String testURI = "anotherTestURI"; + assertEquals(URI.create("./" + testURI), nullBaseURISut.resolveURI(testURI)); + + assertEquals(getActualURI(absoluteBaseURI, URI.create(testURI)), + absoluteBaseURISut.resolveURI(testURI)); + + assertEquals(getActualURI(relativeBaseURI, URI.create(testURI)), + relativeBaseURISut.resolveURI(testURI)); + } +} -- 2.39.5