1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   */ 
19  package org.apache.rat.test.utils;
20  
21  import java.io.BufferedReader;
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileNotFoundException;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.InputStreamReader;
28  import java.io.Reader;
29  
30  import org.apache.rat.document.impl.DocumentImplUtils;
31  
32  
33  /**
34   * Utility class, which provides static methods for creating
35   * test cases.
36   */
37  public class Resources {
38  	/**
39  	 * Private constructor, to prevent accidental instantiation.
40  	 */
41  	private Resources() {
42  		// Does nothing
43  	}
44  
45  	/**
46  	 * Locates a resource file in the class path.
47  	 */
48  	public static File getResourceFile(String pResource) throws IOException {
49  		final File f = new File("src/test/resources", pResource);
50  		if (!f.isFile()) {
51  			throw new FileNotFoundException("Unable to locate resource file: " + pResource);
52  		}
53  		return f;
54  	}
55  
56  	/**
57  	 * Locates a resource file in the class path and returns an {@link InputStream}.
58  	 */
59  	public static InputStream getResourceStream(String pResource) throws IOException {
60  		return new FileInputStream(getResourceFile(pResource));
61  	}
62  
63  	/**
64  	 * Locates a resource file in the class path and returns a {@link Reader}.
65  	 */
66  	public static Reader getResourceReader(String pResource) throws IOException {
67  		return new InputStreamReader(getResourceStream(pResource), "UTF-8");
68  	}
69  
70  	/**
71  	 * Locates a resource file in the class path and returns a {@link BufferedReader}.
72  	 */
73  	public static BufferedReader getBufferedResourceReader(String pResource) throws IOException {
74  		return new BufferedReader(getResourceReader(pResource));
75  	}
76  
77  	/**
78  	 * Locates the name of a directory, which contains the given
79  	 * resource file.
80  	 */
81  	public static String getResourceDirectory(String pResource) throws IOException {
82      	final File resource = getResourceFile(pResource);
83      	final File dir = resource.getParentFile();
84      	return DocumentImplUtils.toName(dir);
85  	}
86  }