This project has retired. For details please refer to its Attic page.
AbstractAny23TestBase xref
View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  
16  package org.apache.any23;
17  
18  import java.io.File;
19  import java.io.FileNotFoundException;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import org.apache.any23.source.DocumentSource;
25  import org.apache.any23.source.FileDocumentSource;
26  import org.apache.commons.io.IOUtils;
27  import org.junit.Assert;
28  import org.junit.Before;
29  import org.junit.Rule;
30  import org.junit.rules.TemporaryFolder;
31  
32  /**
33   * This file encapsulates access to test resource files using temporary files that are automatically cleaned up by JUnit
34   * after each test.
35   * 
36   * @author Peter Ansell p_ansell@yahoo.com
37   */
38  public class AbstractAny23TestBase {
39  
40      @Rule
41      public TemporaryFolder testFolder = new TemporaryFolder();
42      protected File tempDirectory;
43  
44      public AbstractAny23TestBase() {
45          super();
46      }
47  
48      @Before
49      public void setUp() throws Exception {
50          tempDirectory = testFolder.newFolder();
51      }
52  
53      protected DocumentSource getDocumentSourceFromResource(String resourceLocation) throws IOException {
54          return new FileDocumentSource(copyResourceToTempFile(resourceLocation));
55      }
56  
57      protected DocumentSource getDocumentSourceFromResource(String resourceLocation, String baseUri) throws IOException {
58          return new FileDocumentSource(copyResourceToTempFile(resourceLocation), baseUri);
59      }
60  
61      /**
62       * Copies a resource to a temporary directory and returns a file handle that can be used to access the resource as a
63       * file from the temp directory.
64       * 
65       * @param resourceLocation
66       *            The absolute location of the resource in the classpath, which can be used with
67       *            this.getClass().getResourceAsStream.
68       * 
69       * @return temporary {@link java.io.File}
70       * 
71       * @throws FileNotFoundException
72       *             if the temp file location cannot be converted to a {@link java.io.FileOutputStream}
73       * @throws IOException
74       *             if there is an issue with the input
75       */
76      protected 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);
80  
81          File tempFile = File.createTempFile("any23test-", "-" + fileEnding, tempDirectory);
82  
83          FileOutputStream output = new FileOutputStream(tempFile);
84  
85          InputStream input = this.getClass().getResourceAsStream(resourceLocation);
86  
87          Assert.assertNotNull("Test resource was not found: " + resourceLocation, input);
88  
89          IOUtils.copy(input, output);
90  
91          return tempFile;
92      }
93  
94  }