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