1   /*
2    * Copyright (c) 1998, 2005 Gargoyle Software Inc. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    *
7    * 1. Redistributions of source code must retain the above copyright notice,
8    *    this list of conditions and the following disclaimer.
9    * 2. Redistributions in binary form must reproduce the above copyright notice,
10   *    this list of conditions and the following disclaimer in the documentation
11   *    and/or other materials provided with the distribution.
12   * 3. The end-user documentation included with the redistribution, if any, must
13   *    include the following acknowledgment:
14   *
15   *       "This product includes software developed by Gargoyle Software Inc.
16   *        (http://www.GargoyleSoftware.com/)."
17   *
18   *    Alternately, this acknowledgment may appear in the software itself, if
19   *    and wherever such third-party acknowledgments normally appear.
20   * 4. The name "Gargoyle Software" must not be used to endorse or promote
21   *    products derived from this software without prior written permission.
22   *    For written permission, please contact info@GargoyleSoftware.com.
23   * 5. Products derived from this software may not be called "GSBase", nor may
24   *    "GSBase" appear in their name, without prior written permission of
25   *    Gargoyle Software Inc.
26   *
27   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30   * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   */
38  package com.gargoylesoftware.base.gui;
39  
40  import java.awt.event.ActionEvent;
41  import java.lang.reflect.InvocationTargetException;
42  import java.util.ArrayList;
43  import java.util.List;
44  import junit.framework.TestCase;
45  
46  /***
47   * Tests for ReflectedAction
48   *
49   * @version  $Revision: 1.10 $
50   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
51   */
52  public class ReflectedActionTest extends TestCase {
53      private List resultList_ = new ArrayList();
54  
55      /***
56       * Create a test
57       * @param name The name of the test
58       */
59      public ReflectedActionTest( final String name ) {
60          super(name);
61      }
62  
63  
64      /***
65       * Create an instance with a null object
66       */
67      public void testConstructor_NullObject() {
68  
69          try {
70              new ReflectedAction(null, "foo");
71              fail("Expected exception for null object");
72          }
73          catch( final NullPointerException e ) {
74              // Expected path
75          }
76      }
77  
78  
79      /***
80       * Create an instance with a null method name
81       */
82      public void testConstructor_NullMethodName() {
83  
84          try {
85              new ReflectedAction(this, null);
86              fail("Expected exception for null method name");
87          }
88          catch( final NullPointerException e ) {
89              // Expected path
90          }
91      }
92  
93  
94      /***
95       * Create an instance with a bad method name
96       */
97      public void testConstructor_BadMethodName() {
98  
99          try {
100             new ReflectedAction(this, "foo");
101             fail("Expected exception for method name that doesn't exist [foo]");
102         }
103         catch( final IllegalArgumentException e ) {
104             // Expected path
105         }
106 
107         try {
108             new ReflectedAction(this, "");
109             fail("Expected exception for empty method name");
110         }
111         catch( final IllegalArgumentException e ) {
112             // Expected path
113         }
114 
115         try {
116             new ReflectedAction(this, "methodWithTwoParameters");
117             fail("Expected exception for empty method name");
118         }
119         catch( final IllegalArgumentException e ) {
120             // Expected path
121         }
122     }
123 
124 
125     /***
126      * Create an instance with legal values
127      */
128     public void testConstructor() {
129         new ReflectedAction(this, "methodWithNoParameters");
130         new ReflectedAction(this, "methodWithActionEventParameter");
131     }
132 
133 
134     /***
135      * Create the actionPerformed() method
136      */
137     public void testActionPerformed() {
138         resultList_.clear();
139 
140         ReflectedAction action;
141         final ActionEvent actionEvent = new ActionEvent(this, 0, "");
142 
143         action = new ReflectedAction(this, "methodWithNoParameters");
144         action.actionPerformed( actionEvent );
145         assertEquals( "resultList.size()", 1, resultList_.size());
146         assertEquals( "resultList[0]", "methodWithNoParameters", resultList_.get(0));
147 
148         action = new ReflectedAction(this, "methodWithActionEventParameter");
149         action.actionPerformed( actionEvent );
150         assertEquals( "resultList.size()", 2, resultList_.size());
151         assertEquals( "resultList[1]", actionEvent, resultList_.get(1));
152     }
153 
154 
155     /***
156      * Test the actionPerformed() method when it throws an exception
157      */
158     public void testActionPerformed_ExceptionThrown() {
159         /*** Fake action for testing */
160         class ReflectedActionWithErrorHandling extends ReflectedAction {
161             private static final long serialVersionUID = 1L;
162 			private Exception exception = null;
163             /***
164              * @param object Object to invoke
165              * @param methodName Method to invoke
166              */
167             public ReflectedActionWithErrorHandling(final Object object, final String methodName ) {
168                 super(object, methodName);
169             }
170             /*** @param e Exception that was thrown */
171             protected void exceptionThrown( final Exception e ) {
172                 exception = e;
173             }
174             /*** @return The exception that was thrown */
175             public Exception getException() {
176                 return exception;
177             }
178         }
179 
180         final ReflectedActionWithErrorHandling action
181             = new ReflectedActionWithErrorHandling(this, "methodThatThrowsException");
182         final ActionEvent actionEvent = new ActionEvent(this, 0, "");
183 
184         action.actionPerformed(actionEvent);
185         assertNotNull(action.getException());
186         assertTrue("Outer exception is InvocationTargetException",
187             action.getException() instanceof InvocationTargetException );
188         assertTrue("Inner exception is IllegalStateException",
189             ((InvocationTargetException)action.getException()).getTargetException() instanceof IllegalStateException );
190     }
191 
192 
193     /***
194      * Checkstyle warns us about unused private methods.  There are a number
195      * of private methods in this class that are required for the tests but
196      * are never called directly so checkstyle complains about them.  This
197      * method (which should never be executed) calls all these private methods
198      * just to fool checkstyle.
199      * @throws Exception If something goes wrong.
200      */
201     void fakeOutCheckstyle() throws Exception {
202         methodWithNoParameters();
203         methodWithActionEventParameter(null);
204         methodWithTwoParameters(null, null);
205         methodThatThrowsException();
206     }
207     
208     private void methodWithNoParameters() {
209         resultList_.add("methodWithNoParameters");
210     }
211 
212 
213     private void methodWithActionEventParameter( final ActionEvent event ) {
214         resultList_.add(event);
215     }
216 
217 
218     private void methodWithTwoParameters( final ActionEvent event, final String s ) {
219     }
220 
221 
222     private void methodThatThrowsException() {
223         throw new IllegalStateException();
224     }
225 }