1 /*
2 Copyright (c) 2003, Laurent Caillette.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modifica-
6 tion, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10
11 2. Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14
15 3. The end-user documentation included with the redistribution, if any, must
16 include the following acknowledgment: "This product includes software
17 written by Laurent Caillette."
18 Alternately, this acknowledgment may appear in the software itself, if
19 and wherever such third-party acknowledgments normally appear.
20
21 4. The name "Laurent Caillette" must not be used to endorse or
22 promote products derived from this software without
23 prior written permission. For written permission, please contact
24 laurent.caillette@laposte.net
25
26 5. Products derived from this software may not be called
27 "Laurent Caillette", nor may "Laurent Caillette" appear
28 in their name, without prior written permission of the
29 author.
30
31 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
32 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
33 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
34 AUTHOR (LAURENT CAILLETTE) BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
36 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
37 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
42 package tweed.system.interceptor;
43 import java.lang.reflect.Method;
44 import junit.framework.TestCase;
45 import junit.framework.Test;
46 import junit.framework.TestSuite;
47 import junit.textui.TestRunner;
48
49 /***
50 *
51 * @author Laurent Caillette
52 * @version $Id$
53 */
54 public class MethodExtractorTest extends TestCase {
55
56
57 private final static String GETMYINTPROPERTY_METHODNAME = "getMyIntProperty" ;
58 private final static Class[] NOPARAMETERS = new Class[ 0 ] ;
59
60 public void testMyBean() {
61
62 OverridesFinder finder = null ;
63 Override[] methods = null ;
64 try {
65 finder = new OverridesFinder( MyBean.class ) ;
66 methods = finder.getMethodsToOverride() ;
67 } catch( PropertyDefinitionException ex ) {
68 throw new RuntimeException( ex ) ;
69 }
70 int index ;
71
72 index = findGetter( methods, GETMYINTPROPERTY_METHODNAME, Integer.TYPE ) ;
73 Method getMyIntPropertyMethod = findMethodQuiet(
74 MyBean.class,
75 GETMYINTPROPERTY_METHODNAME,
76 NOPARAMETERS
77 ) ;
78 assertEquals( getMyIntPropertyMethod, methods[ index ].getMethod() ) ;
79
80 }
81
82 private int findGetter(
83 Override[] properties,
84 String name,
85 Class type
86 ) {
87 for( int i = 0 ; i < properties.length ; i++ ) {
88 if( name.equals( properties[ i ].getMethod().getName() ) ) {
89 return i ;
90 }
91 }
92 fail( "Could not find property " + name ) ;
93 return 0 ; // never
94 }
95
96 private Method findMethodQuiet(
97 Class declaringClass,
98 String methodName,
99 Class[] parameterTypes
100 ) {
101 try {
102 return declaringClass.getMethod( methodName, parameterTypes ) ;
103 } catch( NoSuchMethodException ex ) {
104 throw new RuntimeException( ex ) ;
105 } catch( SecurityException ex ) {
106 throw new RuntimeException( ex ) ;
107 }
108 }
109
110 // =====
111 // JUnit
112 // =====
113
114 public MethodExtractorTest( final String name ) {
115 super( name );
116 }
117
118 public static Test suite() {
119 TestSuite suite = new TestSuite();
120 suite.addTestSuite( MethodExtractorTest.class );
121 return suite;
122 }
123
124 public static void main( String[] args ) {
125 TestRunner.run( MethodExtractorTest.suite() );
126 }
127
128 }
This page was automatically generated by Maven