1   /*
2    * "Peko" Visual Novel System
3    *
4    * All Rights Reserved.
5    * (c) Copyright 1999-2003 by Tsukuba Bunko.
6    *
7    * $Id: ReflectionUtilTest.java,v 1.1 2005/07/11 12:49:19 ppoi Exp $
8    */
9   package tsukuba_bunko.util.test;
10  
11  import	java.lang.reflect.Method;
12  
13  import	java.util.Arrays;
14  
15  import	junit.framework.TestCase;
16  
17  import	tsukuba_bunko.util.ReflectionUtil;
18  
19  
20  /***
21   * @author	$Author: ppoi $
22   * @version	$Revision: 1.1 $
23   */
24  public class ReflectionUtilTest extends TestCase {
25  
26  	/***
27  	 */
28  	public ReflectionUtilTest( String name )
29  	{
30  		super( name );
31  	}
32  
33  
34  
35  	public void test_findMethod_オーバーライドした場合かつ親クラスもたどる場合()
36  		throws Exception
37  	{
38  		Class[]	parameterTypes = new Class[]{ String.class, boolean.class };
39  
40  		Method	method = ReflectionUtil.findMethod( ReflectionTestClassB.class, "testMethodA", parameterTypes, true );
41  		assertNotNull( "検出失敗", method );
42  		assertEquals( "宣言されている型が違う", ReflectionTestClassB.class, method.getDeclaringClass() );
43  		assertTrue( "仮引数タイプが違う", Arrays.equals(method.getParameterTypes(), parameterTypes) );
44  	}
45  
46  	public void test_findMethod_オーバーライドした場合かつこのクラスだけの場合()
47  		throws Exception
48  	{
49  		Class[]	parameterTypes = new Class[]{ String.class, boolean.class };
50  
51  		Method	method = ReflectionUtil.findMethod( ReflectionTestClassB.class, "testMethodA", parameterTypes, false );
52  		assertNotNull( "検出失敗", method );
53  		assertEquals( "宣言されている型が違う", ReflectionTestClassB.class, method.getDeclaringClass() );
54  		assertTrue( "仮引数タイプが違う", Arrays.equals(method.getParameterTypes(), parameterTypes) );
55  	}
56  
57  	public void test_findMethod_オーバーライドしてない場合かつ親クラスもたどる場合スーパークラス版()
58  		throws Exception
59  	{
60  		Class[]	parameterTypes = new Class[]{ String.class, boolean.class };
61  
62  		Method	method = ReflectionUtil.findMethod( ReflectionTestClassB.class, "testMethodB", parameterTypes, true );
63  		assertNotNull( "検出失敗", method );
64  		assertEquals( "宣言されている型が違う", ReflectionTestClassA.class, method.getDeclaringClass() );
65  		assertTrue( "仮引数タイプが違う", Arrays.equals(method.getParameterTypes(), parameterTypes) );
66  	}
67  
68  	public void test_findMethod_オーバーライドしてない場合かつこのクラスだけの場合スーパークラス版()
69  		throws Exception
70  	{
71  		Class[]	parameterTypes = new Class[]{ String.class, boolean.class };
72  
73  		Method	method = ReflectionUtil.findMethod( ReflectionTestClassB.class, "testMethodB", parameterTypes, false );
74  		assertNull( "変なのが検出されている", method );
75  	}
76  
77  	public void test_findMethod_オーバーライドしてない場合かつ親クラスもたどる場合サブクラス版()
78  		throws Exception
79  	{
80  		Class[]	parameterTypes = new Class[]{ String.class };
81  
82  		Method	method = ReflectionUtil.findMethod( ReflectionTestClassB.class, "testMethodB", parameterTypes, true );
83  		assertNotNull( "検出失敗", method );
84  		assertEquals( "宣言されている型が違う", ReflectionTestClassB.class, method.getDeclaringClass() );
85  		assertTrue( "仮引数タイプが違う", Arrays.equals(method.getParameterTypes(), parameterTypes) );
86  	}
87  
88  	public void test_findMethod_オーバーライドしてない場合かつこのクラスだけの場合サブクラス版()
89  		throws Exception
90  	{
91  		Class[]	parameterTypes = new Class[]{ String.class };
92  
93  		Method	method = ReflectionUtil.findMethod( ReflectionTestClassB.class, "testMethodB", parameterTypes, false );
94  		assertNotNull( "検出失敗", method );
95  		assertEquals( "宣言されている型が違う", ReflectionTestClassB.class, method.getDeclaringClass() );
96  		assertTrue( "仮引数タイプが違う", Arrays.equals(method.getParameterTypes(), parameterTypes) );
97  	}
98  
99  	public void test_findMethod_サブクラス独自メソッドかつ親クラスもたどる場合()
100 		throws Exception
101 	{
102 		Class[]	parameterTypes = new Class[]{};
103 
104 		Method	method = ReflectionUtil.findMethod( ReflectionTestClassB.class, "testMethodC", parameterTypes, true );
105 		assertNotNull( "検出失敗", method );
106 		assertEquals( "宣言されている型が違う", ReflectionTestClassB.class, method.getDeclaringClass() );
107 		assertTrue( "仮引数タイプが違う", Arrays.equals(method.getParameterTypes(), parameterTypes) );
108 	}
109 
110 	public void test_findMethod_サブクラス独自メソッドかつこのクラスだけの場合()
111 		throws Exception
112 	{
113 		Class[]	parameterTypes = new Class[]{};
114 
115 		Method	method = ReflectionUtil.findMethod( ReflectionTestClassB.class, "testMethodC", parameterTypes, false );
116 		assertNotNull( "検出失敗", method );
117 		assertEquals( "宣言されている型が違う", ReflectionTestClassB.class, method.getDeclaringClass() );
118 		assertTrue( "仮引数タイプが違う", Arrays.equals(method.getParameterTypes(), parameterTypes) );
119 	}
120 
121 	public void test_findMethod_存在しないメソッドかつ親クラスもたどる場合()
122 		throws Exception
123 	{
124 		Class[]	parameterTypes = new Class[]{};
125 
126 		Method	method = ReflectionUtil.findMethod( ReflectionTestClassB.class, "testMethodD", parameterTypes, true );
127 		assertNull( "変なクラスが検出された", method );
128 	}
129 
130 	public void test_findMethod_存在しないメソッドかつこのクラスだけの場合()
131 		throws Exception
132 	{
133 		Class[]	parameterTypes = new Class[]{};
134 
135 		Method	method = ReflectionUtil.findMethod( ReflectionTestClassB.class, "testMethodD", parameterTypes, false );
136 		assertNull( "変なクラスが検出された", method );
137 	}
138 
139 	public void test_findMethod_スーパークラス独自メソッドかつ親クラスもたどる場合()
140 		throws Exception
141 	{
142 		Class[]	parameterTypes = new Class[]{};
143 
144 		Method	method = ReflectionUtil.findMethod( ReflectionTestClassB.class, "testMethodE", parameterTypes, true );
145 		assertNotNull( "検出失敗", method );
146 		assertEquals( "宣言されている型が違う", ReflectionTestClassA.class, method.getDeclaringClass() );
147 		assertTrue( "仮引数タイプが違う", Arrays.equals(method.getParameterTypes(), parameterTypes) );
148 	}
149 
150 	public void test_findMethod_スーパークラス独自メソッドかつこのクラスだけの場合()
151 		throws Exception
152 	{
153 		Class[]	parameterTypes = new Class[]{};
154 
155 		Method	method = ReflectionUtil.findMethod( ReflectionTestClassB.class, "testMethodE", parameterTypes, false );
156 		assertNull( "変なクラスが検出された", method );
157 	}
158 
159 	public void test_findMethod_存在しないオーバーロードかつ親クラスもたどる場合()
160 		throws Exception
161 	{
162 		Class[]	parameterTypes = new Class[]{};
163 
164 		Method	method = ReflectionUtil.findMethod( ReflectionTestClassB.class, "testMethodA", parameterTypes, true );
165 		assertNull( "変なクラスが検出された", method );
166 	}
167 
168 	public void test_findMethod_存在しないオーバーロードかつこのクラスだけの場合()
169 		throws Exception
170 	{
171 		Class[]	parameterTypes = new Class[]{};
172 
173 		Method	method = ReflectionUtil.findMethod( ReflectionTestClassB.class, "testMethodA", parameterTypes, false );
174 		assertNull( "変なクラスが検出された", method );
175 	}
176 }