View Javadoc

1   /*
2    * Copyright 2008-2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a 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
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.util.test.io;
17  
18  import java.io.BufferedInputStream;
19  import java.io.File;
20  import java.io.FileReader;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.Reader;
24  import java.net.URL;
25  import java.util.Properties;
26  
27  import org.apache.commons.io.IOUtils;
28  
29  /**
30   * Utilities to make unit tests with files easier.
31   *
32   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
33   * @version $Revision:591 $
34   */
35  public final class FileTestUtils
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    // --- members --------------------------------------------------------------
42  
43    // ****************************** Initializer *******************************
44  
45    // ****************************** Constructors ******************************
46  
47    /**
48     * Utility class pattern.
49     */
50    private FileTestUtils()
51    {
52    }
53  
54    // ****************************** Inner Classes *****************************
55  
56    // ********************************* Methods ********************************
57  
58    // --- init -----------------------------------------------------------------
59  
60    // --- get&set --------------------------------------------------------------
61  
62    // --- business -------------------------------------------------------------
63  
64    /**
65     * Returns the specified file from the class path.
66     *
67     * @param resourceName name of the resource (file or directory) in the class
68     *          path.
69     * @return the file representation of the resource or <code>null</code> if the
70     *         resource does not exist, is not accessible via the
71     *         <code>file:/</code> protocol or due to missing privileges.
72     */
73    public static File getFileFromResource(final String resourceName)
74    {
75      final ClassLoader classLoader = FileTestUtils.class.getClassLoader(); // NOPMD
76      return getFileFromResource(classLoader, resourceName);
77    }
78  
79    /**
80     * Returns the specified file from the specified class loader.
81     *
82     * @param classLoader the class loader to load the resource from.
83     * @param resourceName name of the resource (file or directory) in the class
84     *          path.
85     * @return the file representation of the resource or <code>null</code> if the
86     *         resource does not exist, is not accessible via the
87     *         <code>file:/</code> protocol or due to missing privileges.
88     */
89    public static File getFileFromResource(final ClassLoader classLoader,
90        final String resourceName)
91    {
92      final URL url = classLoader.getResource(resourceName);
93      final File file = getFileFrom(url);
94      return file;
95    }
96  
97    /**
98     * Returns the specified file from the class loader associated with the given
99     * type.
100    *
101    * @param type the type to refer to the class loader to load the resource
102    *          from. The resource is relative to this type.
103    * @param resourceName name of the resource (file or directory) in the class
104    *          path.
105    * @return the file representation of the resource or <code>null</code> if the
106    *         resource does not exist, is not accessible via the
107    *         <code>file:/</code> protocol or due to missing privileges.
108    */
109   public static File getFileFromResource(final Class<?> type,
110       final String resourceName)
111   {
112     return getFileFromResource(type.getClassLoader(), resourceName);
113   }
114 
115   /**
116    * Returns the specified file from a resource relative to the given type.
117    *
118    * @param type the type to refer to the class loader to load the resource
119    *          from. The resource is relative to this type.
120    * @param relativeResourceName name of the resource (file or directory) in the
121    *          class path, relative to the passed in <code>type</code>.
122    * @return the file representation of the resource or <code>null</code> if the
123    *         resource does not exist, is not accessible via the
124    *         <code>file:/</code> protocol or due to missing privileges.
125    */
126   public static File getFileFromRelativeResource(final Class<?> type,
127       final String relativeResourceName)
128   {
129     final URL url = type.getResource(relativeResourceName);
130     final File file = getFileFrom(url);
131     return file;
132   }
133 
134   private static File getFileFrom(final URL url)
135   {
136     if (url != null)
137     {
138       final String resourceAsString = url.toExternalForm();
139 
140       if (resourceAsString.startsWith("file:/"))
141       {
142         final int startIndex = isWindows() ? 6 : 5;
143         final String fileName = resourceAsString.substring(startIndex);
144         final File file = new File(fileName);
145         return file;
146       }
147     }
148     return null;
149   }
150 
151   private static boolean isWindows()
152   {
153     return File.separatorChar == '\\';
154   }
155 
156   /**
157    * Returns a properties object backed by the specified file from a resource
158    * relative to the given type with the simple name of the resource with an
159    * ending <code>.properties</code> .
160    *
161    * @param clazz the type to refer to the class loader to load the resource
162    *          from. The resource is relative to this type and has its simple
163    *          name.
164    * @return the properties object containing the properties or empty if the
165    *         resource does not exist or is empty.
166    */
167   public static Properties getPropertiesFromRelativePropertiesFileForClass(
168       final Class<?> clazz)
169   {
170 
171     final InputStream is =
172         clazz.getResourceAsStream(clazz.getSimpleName().concat(".properties"));
173     final BufferedInputStream buffer = new BufferedInputStream(is);
174     final Properties props = new Properties();
175     try
176     {
177       props.load(buffer);
178     }
179     catch (final IOException ioe)
180     {
181       // Do nohing
182     }
183     finally
184     {
185       IOUtils.closeQuietly(buffer);
186     }
187     return props;
188   }
189   // --- object basics --------------------------------------------------------
190 
191 }