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.lang;
17  
18  import static org.junit.Assert.assertNotNull;
19  import static org.junit.Assert.fail;
20  
21  import java.lang.reflect.Constructor;
22  import java.lang.reflect.InvocationTargetException;
23  
24  import org.apache.commons.lang.ArrayUtils;
25  
26  /**
27   * Provides utility functions on the language level.
28   *
29   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
30   * @version $Revision:591 $
31   */
32  public final class LangTestUtils
33  {
34    // ********************************* Fields *********************************
35  
36    // --- constants ------------------------------------------------------------
37  
38    // --- members --------------------------------------------------------------
39  
40    // ****************************** Initializer *******************************
41  
42    // ****************************** Constructors ******************************
43  
44    /**
45     * Utility class pattern.
46     */
47    private LangTestUtils()
48    {
49    }
50  
51    // ****************************** Inner Classes *****************************
52  
53    // ********************************* Methods ********************************
54  
55    // --- init -----------------------------------------------------------------
56  
57    // --- get&set --------------------------------------------------------------
58  
59    // --- business -------------------------------------------------------------
60  
61    /* CHECKSTYLE:OFF */
62    /**
63     * Creates an instance of the private no-args constructor.
64     * <p>
65     * This is a kind of dysfunction to get rid of missing test coverage for
66     * private constructors of constant or utility classes. To keep the overhead
67     * for this dysfuntion small, this method is provided.
68     * </p>
69     *
70     * @param clazz the class to instantiate with the private no-args constructor.
71     * @return the instance of the class.
72     * @throws NoSuchMethodException if there is no private no-args constructor.
73     * @throws SecurityException if the request to make the private constructor
74     *           accessible is denied.
75     * @throws InstantiationException if the class that declares the underlying
76     *           constructor represents an abstract class.
77     * @throws InvocationTargetException if the underlying constructor throws an
78     *           exception.
79     * @throws ExceptionInInitializerError if the initialization provoked by this
80     *           method fails.
81     * @throws IllegalStateException if something strange happens that we did not
82     *           take into account.
83     * @see #runPrivateConstructorTest(Class)
84     */
85    public static Object createInstanceForPrivateNoArgsConstructor(
86        final Class<?> clazz) throws SecurityException, NoSuchMethodException,
87      InstantiationException, InvocationTargetException,
88      ExceptionInInitializerError, IllegalStateException
89    /* CHECKSTYLE:ON */
90    {
91      final Constructor<?> constructor =
92          clazz.getDeclaredConstructor(ArrayUtils.EMPTY_CLASS_ARRAY);
93      constructor.setAccessible(true);
94      try
95      {
96        final Object instance =
97            constructor.newInstance(ArrayUtils.EMPTY_OBJECT_ARRAY);
98        return instance;
99      }
100     catch (final IllegalAccessException e)
101     {
102       throw new IllegalStateException(
103           "Caught an exception that should never been raised."
104               + " We set the constructor to be accessible, but the exception"
105               + " informs us that it is not. Strange...", e);
106     }
107     catch (final IllegalArgumentException e)
108     {
109       throw new IllegalStateException(
110           "Caught an exception that should never been raised."
111               + " We have not parameters to set, but get an illegal"
112               + " argument exception. Strange...", e);
113     }
114   }
115 
116   /**
117    * Tests creating an instance for a private constructor.
118    * <p>
119    * This is a kind of dysfunction to get rid of missing test coverage for
120    * private constructors of constant or utility classes. To keep the overhead
121    * for this dysfuntion small, this method is provided.
122    * </p>
123    * {@example "Example Usage" public void testPrivateConstructor()
124    * LangTestUtils.runPrivateConstructorTest(FileNameUtils.class); }
125    *
126    * @param clazz the class to instantiate with the private no-args constructor.
127    * @see #createInstanceForPrivateNoArgsConstructor(Class)
128    */
129   public static void runPrivateConstructorTest(final Class<?> clazz)
130   {
131     try
132     {
133       final Object instance = createInstanceForPrivateNoArgsConstructor(clazz);
134       assertNotNull("Constructor should create an instance.", instance);
135     }
136     catch (final Exception e)
137     {
138       fail("Unexpected exception raised: " + e.getMessage());
139     }
140   }
141 
142   /**
143    * Removes all whitespaces from the input string.
144    *
145    * @param input the input string to remove all whitespaces.
146    * @return the input stream without whitespaces or <code>null</code> if the
147    *         input string is <code>null</code>.
148    */
149   public static String removeAllWhitespaces(final String input)
150   {
151     if (input != null)
152     {
153       return input.replaceAll("\\s+", "");
154     }
155     return null;
156   }
157 
158   // --- object basics --------------------------------------------------------
159 
160 }