1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license3 * agreements. See the NOTICE file distributed with this work for additional information regarding4 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the5 * "License"); you may not use this file except in compliance with the License. You may obtain a6 * copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software distributed under the License11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express12 * or implied. See the License for the specific language governing permissions and limitations under13 * the License.14 */1516package org.apache.any23;
1718import java.io.File;
19import java.io.FileNotFoundException;
20import java.io.FileOutputStream;
21import java.io.IOException;
22import java.io.InputStream;
2324import org.apache.any23.source.DocumentSource;
25import org.apache.any23.source.FileDocumentSource;
26import org.apache.commons.io.IOUtils;
27import org.junit.Assert;
28import org.junit.Before;
29import org.junit.Rule;
30import org.junit.rules.TemporaryFolder;
3132/**33 * This file encapsulates access to test resource files using temporary files that are automatically cleaned up by JUnit34 * after each test.35 * 36 * @author Peter Ansell p_ansell@yahoo.com37 */38publicclassAbstractAny23TestBase {
3940 @Rule
41public TemporaryFolder testFolder = new TemporaryFolder();
42protected File tempDirectory;
4344publicAbstractAny23TestBase() {
45super();
46 }
4748 @Before
49publicvoid setUp() throws Exception {
50 tempDirectory = testFolder.newFolder();
51 }
5253protected DocumentSource getDocumentSourceFromResource(String resourceLocation) throws IOException {
54returnnew FileDocumentSource(copyResourceToTempFile(resourceLocation));
55 }
5657protected DocumentSource getDocumentSourceFromResource(String resourceLocation, String baseUri) throws IOException {
58returnnew FileDocumentSource(copyResourceToTempFile(resourceLocation), baseUri);
59 }
6061/**62 * Copies a resource to a temporary directory and returns a file handle that can be used to access the resource as a63 * file from the temp directory.64 * 65 * @param resourceLocation66 * The absolute location of the resource in the classpath, which can be used with67 * this.getClass().getResourceAsStream.68 * 69 * @return temporary {@link java.io.File}70 * 71 * @throws FileNotFoundException72 * if the temp file location cannot be converted to a {@link java.io.FileOutputStream}73 * @throws IOException74 * if there is an issue with the input75 */76protected File copyResourceToTempFile(String resourceLocation) throws FileNotFoundException, IOException {
77 Assert.assertNotNull("Temporary directory was null. Did you forget to call super.setUp() to initialise it?",
78 tempDirectory);
79 String fileEnding = resourceLocation.substring(resourceLocation.lastIndexOf("/") + 1);
8081 File tempFile = File.createTempFile("any23test-", "-" + fileEnding, tempDirectory);
8283 FileOutputStream output = new FileOutputStream(tempFile);
8485 InputStream input = this.getClass().getResourceAsStream(resourceLocation);
8687 Assert.assertNotNull("Test resource was not found: " + resourceLocation, input);
8889 IOUtils.copy(input, output);
9091return tempFile;
92 }
9394 }