View Javadoc

1   /*
2    * Copyright 2008-2013 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.InputStream;
20  
21  /**
22   * Utilities to make unit tests with streams to resources from the class path
23   * easier.
24   *
25   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
26   * @version $Revision:591 $
27   */
28  public final class IoTestUtils
29  {
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    // --- members --------------------------------------------------------------
35  
36    // ****************************** Initializer *******************************
37  
38    // ****************************** Constructors ******************************
39  
40    /**
41     * Utility class pattern.
42     */
43    private IoTestUtils()
44    {
45    }
46  
47    // ****************************** Inner Classes *****************************
48  
49    // ********************************* Methods ********************************
50  
51    // --- init -----------------------------------------------------------------
52  
53    // --- get&set --------------------------------------------------------------
54  
55    // --- business -------------------------------------------------------------
56  
57    /**
58     * Returns a stream to a resource on the class path loaded by the class loader
59     * of this class.
60     *
61     * @param resourceName name of the resource on the class path.
62     * @return the opened buffered stream. The client is responsible to close it.
63     * @throws IllegalArgumentException if {@code resourceName} cannot be found.
64     */
65    public static InputStream openStreamFromResource(final String resourceName)
66      throws IllegalArgumentException
67    {
68      final ClassLoader classLoader = IoTestUtils.class.getClassLoader(); // NOPMD
69      return openStreamFromResource(classLoader, resourceName);
70    }
71  
72    /**
73     * Returns a stream to a resource on the class path loaded by the class loader
74     * that has loaded the given {@code type}.
75     *
76     * @param type the type to refer to the class loader to load the resource
77     *          from. The resource is relative to this type.
78     * @param resourceName name of the resource on the class path.
79     * @return the opened buffered stream. The client is responsible to close it.
80     * @throws IllegalArgumentException if {@code resourceName} cannot be found.
81     */
82    public static InputStream openStreamFromResourceByTypeClassLoader(
83        final Class<?> type, final String resourceName)
84      throws IllegalArgumentException
85    {
86      try
87      {
88        return openStreamFromResource(type.getClassLoader(), resourceName);
89      }
90      catch (final IllegalArgumentException e)
91      {
92        throw new IllegalArgumentException(
93            "Cannot find resource '" // NOPMD
94                + resourceName
95                + "' on the class path using the class loader of type '"
96                + type.getClass().getName() + "'.", e);
97      }
98    }
99  
100   /**
101    * Returns a stream to a resource on the class path.
102    *
103    * @param classLoader the class loader to load the resource from.
104    * @param resourceName name of the resource on the class path.
105    * @return the opened buffered stream. The client is responsible to close it.
106    * @throws IllegalArgumentException if {@code resourceName} cannot be found.
107    */
108   public static InputStream openStreamFromResource(
109       final ClassLoader classLoader, final String resourceName)
110     throws IllegalArgumentException
111   {
112     final InputStream input = classLoader.getResourceAsStream(resourceName);
113     if (input == null)
114     {
115       throw new IllegalArgumentException(
116           "Cannot find resource '" + resourceName
117               + "' on the class path using the given class loader.");
118     }
119 
120     final BufferedInputStream bin = new BufferedInputStream(input);
121     return bin;
122   }
123 
124   /**
125    * Returns the specified file from a resource relative to the given type.
126    *
127    * @param type the type to refer to the class loader to load the resource
128    *          from. The resource is relative to this type.
129    * @param relativeResourceName name of the resource on the class path,
130    *          relative to the passed in {@code type}.
131    * @return the opened buffered stream. The client is responsible to close it.
132    * @throws IllegalArgumentException if {@code relativeResourceName} cannot be
133    *           found.
134    */
135   public static InputStream openStreamFromResource(final Class<?> type,
136       final String relativeResourceName) throws IllegalArgumentException
137   {
138     final InputStream input = type.getResourceAsStream(relativeResourceName);
139     if (input == null)
140     {
141       throw new IllegalArgumentException(
142           "Cannot find resource '" + relativeResourceName
143               + "' on the class path relative to type '"
144               + type.getClass().getName() + "'.");
145     }
146 
147     final BufferedInputStream bin = new BufferedInputStream(input);
148     return bin;
149   }
150 
151   // --- object basics --------------------------------------------------------
152 
153 }