View Javadoc
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 org.apache.rat.document.impl.DocumentImplUtils;
22  
23  import java.io.BufferedReader;
24  import java.io.File;
25  import java.io.FileFilter;
26  import java.io.FileInputStream;
27  import java.io.FileNotFoundException;
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.io.InputStreamReader;
31  import java.io.Reader;
32  
33  
34  /**
35   * Utility class, which provides static methods for creating
36   * test cases.
37   */
38  public class Resources {
39      /**
40       * Private constructor, to prevent accidental instantiation.
41       */
42      private Resources() {
43          // Does nothing
44      }
45  
46      public static final String SRC_TEST_RESOURCES = "src/test/resources";
47      public static final String SRC_MAIN_RESOURCES = "src/main/resources";
48      private static File TEST_RESOURCE_BASE_PATH = new File(SRC_TEST_RESOURCES);
49      private static File RESOURCE_BASE_PATH = new File(SRC_MAIN_RESOURCES);
50  
51      /**
52       * Locates a test resource file in the class path.
53       */
54      public static File getResourceFile(String pResource) throws IOException {
55          return getResourceFromBase(TEST_RESOURCE_BASE_PATH, pResource);
56      }
57  
58      /**
59       * Locates a main resource file in the class path.
60       */
61      public static File getMainResourceFile(String pResource) throws IOException {
62          return getResourceFromBase(RESOURCE_BASE_PATH, pResource);
63      }
64  
65      /**
66       * Try to to load the given file from baseDir, in case of errors try to add module names to fix behaviour from within IntelliJ.
67       */
68      private static File getResourceFromBase(File baseDir, String pResource) throws IOException {
69          File f = new File(baseDir, pResource);
70          if (!f.isFile()) {
71              throw new FileNotFoundException("Unable to locate resource file: " + pResource);
72          }
73          return f;
74      }
75  
76      /**
77       * Locates a set of resource files in the class path.
78       * In case of errors try to add module names to fix behaviour from within IntelliJ.
79       */
80      public static File[] getResourceFiles(String pResource) throws IOException {
81          File f = new File(TEST_RESOURCE_BASE_PATH, pResource);
82          if (!f.isDirectory()) {
83              throw new FileNotFoundException("Unable to locate resource directory: " + pResource);
84          }
85  
86          return f.listFiles(new FileFilter() {
87              public boolean accept(File pathname) {
88                  return pathname.isFile();
89              }
90          });
91      }
92  
93      /**
94       * Locates a resource file in the class path and returns an {@link InputStream}.
95       */
96      public static InputStream getResourceStream(String pResource) throws IOException {
97          return new FileInputStream(getResourceFile(pResource));
98      }
99  
100     /**
101      * Locates a resource file in the class path and returns a {@link Reader}.
102      */
103     public static Reader getResourceReader(String pResource) throws IOException {
104         return new InputStreamReader(getResourceStream(pResource), "UTF-8");
105     }
106 
107     /**
108      * Locates a resource file in the class path and returns a {@link BufferedReader}.
109      */
110     public static BufferedReader getBufferedResourceReader(String pResource) throws IOException {
111         return new BufferedReader(getResourceReader(pResource));
112     }
113 
114     /**
115      * Locates a resource file in the class path and returns a {@link BufferedReader}.
116      */
117     public static BufferedReader getBufferedReader(File file) throws IOException {
118         return new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
119     }
120 
121     /**
122      * Locates the name of a directory, which contains the given
123      * resource file.
124      */
125     public static String getResourceDirectory(String pResource) throws IOException {
126         final File resource = getResourceFile(pResource);
127         final File dir = resource.getParentFile();
128         return DocumentImplUtils.toName(dir);
129     }
130 }