001: //========================================================================
002: //$Id: TestIntrospectionUtil.java 1448 2006-12-29 20:46:57Z janb $
003: //Copyright 2006 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.util;
017:
018: import java.lang.reflect.Field;
019: import java.lang.reflect.Method;
020:
021: import junit.framework.TestCase;
022:
023: /**
024: * TestInjection
025: *
026: *
027: */
028: public class TestIntrospectionUtil extends TestCase {
029: public final Class[] __INTEGER_ARG = new Class[] { Integer.class };
030: Field privateAField;
031: Field protectedAField;
032: Field publicAField;
033: Field defaultAField;
034: Field privateBField;
035: Field protectedBField;
036: Field publicBField;
037: Field defaultBField;
038: Method privateCMethod;
039: Method protectedCMethod;
040: Method publicCMethod;
041: Method defaultCMethod;
042: Method privateDMethod;
043: Method protectedDMethod;
044: Method publicDMethod;
045: Method defaultDMethod;
046:
047: public class ServletA {
048: private Integer privateA;
049: protected Integer protectedA;
050: Integer defaultA;
051: public Integer publicA;
052: }
053:
054: public class ServletB extends ServletA {
055: private String privateB;
056: protected String protectedB;
057: public String publicB;
058: String defaultB;
059: }
060:
061: public class ServletC {
062: private void setPrivateC(Integer c) {
063: }
064:
065: protected void setProtectedC(Integer c) {
066: }
067:
068: public void setPublicC(Integer c) {
069: }
070:
071: void setDefaultC(Integer c) {
072: }
073: }
074:
075: public class ServletD extends ServletC {
076: private void setPrivateD(Integer d) {
077: }
078:
079: protected void setProtectedD(Integer d) {
080: }
081:
082: public void setPublicD(Integer d) {
083: }
084:
085: void setDefaultD(Integer d) {
086: }
087: }
088:
089: public void setUp() throws Exception {
090: privateAField = ServletA.class.getDeclaredField("privateA");
091: protectedAField = ServletA.class.getDeclaredField("protectedA");
092: publicAField = ServletA.class.getDeclaredField("publicA");
093: defaultAField = ServletA.class.getDeclaredField("defaultA");
094: privateBField = ServletB.class.getDeclaredField("privateB");
095: protectedBField = ServletB.class.getDeclaredField("protectedB");
096: publicBField = ServletB.class.getDeclaredField("publicB");
097: defaultBField = ServletB.class.getDeclaredField("defaultB");
098: privateCMethod = ServletC.class.getDeclaredMethod(
099: "setPrivateC", __INTEGER_ARG);
100: protectedCMethod = ServletC.class.getDeclaredMethod(
101: "setProtectedC", __INTEGER_ARG);
102: publicCMethod = ServletC.class.getDeclaredMethod("setPublicC",
103: __INTEGER_ARG);
104: defaultCMethod = ServletC.class.getDeclaredMethod(
105: "setDefaultC", __INTEGER_ARG);
106: privateDMethod = ServletD.class.getDeclaredMethod(
107: "setPrivateD", __INTEGER_ARG);
108: protectedDMethod = ServletD.class.getDeclaredMethod(
109: "setProtectedD", __INTEGER_ARG);
110: publicDMethod = ServletD.class.getDeclaredMethod("setPublicD",
111: __INTEGER_ARG);
112: defaultDMethod = ServletD.class.getDeclaredMethod(
113: "setDefaultD", __INTEGER_ARG);
114: }
115:
116: public void testFieldPrivate() throws Exception {
117: //direct
118: Field f = IntrospectionUtil.findField(ServletA.class,
119: "privateA", Integer.class, true, false);
120: assertEquals(privateAField, f);
121:
122: //inheritance
123: try {
124: IntrospectionUtil.findField(ServletB.class, "privateA",
125: Integer.class, true, false);
126: fail("Private fields should not be inherited");
127: } catch (NoSuchFieldException e) {
128: //expected
129: }
130: }
131:
132: public void testFieldProtected() throws Exception {
133: //direct
134: Field f = IntrospectionUtil.findField(ServletA.class,
135: "protectedA", Integer.class, true, false);
136: assertEquals(f, protectedAField);
137:
138: //inheritance
139: f = IntrospectionUtil.findField(ServletB.class, "protectedA",
140: Integer.class, true, false);
141: assertEquals(f, protectedAField);
142: }
143:
144: public void testFieldPublic() throws Exception {
145: //direct
146: Field f = IntrospectionUtil.findField(ServletA.class,
147: "publicA", Integer.class, true, false);
148: assertEquals(f, publicAField);
149:
150: //inheritance
151: f = IntrospectionUtil.findField(ServletB.class, "publicA",
152: Integer.class, true, false);
153: assertEquals(f, publicAField);
154: }
155:
156: public void testFieldDefault() throws Exception {
157: //direct
158: Field f = IntrospectionUtil.findField(ServletA.class,
159: "defaultA", Integer.class, true, false);
160: assertEquals(f, defaultAField);
161:
162: //inheritance
163: f = IntrospectionUtil.findField(ServletB.class, "defaultA",
164: Integer.class, true, false);
165: assertEquals(f, defaultAField);
166: }
167:
168: public void testMethodPrivate() throws Exception {
169: //direct
170: Method m = IntrospectionUtil.findMethod(ServletC.class,
171: "setPrivateC", __INTEGER_ARG, true, false);
172: assertEquals(m, privateCMethod);
173:
174: //inheritance
175: try {
176: IntrospectionUtil.findMethod(ServletD.class, "setPrivateC",
177: __INTEGER_ARG, true, false);
178: fail();
179: } catch (NoSuchMethodException e) {
180: //expected
181: }
182: }
183:
184: public void testMethodProtected() throws Exception {
185: // direct
186: Method m = IntrospectionUtil.findMethod(ServletC.class,
187: "setProtectedC", __INTEGER_ARG, true, false);
188: assertEquals(m, protectedCMethod);
189:
190: //inherited
191: m = IntrospectionUtil.findMethod(ServletD.class,
192: "setProtectedC", __INTEGER_ARG, true, false);
193: assertEquals(m, protectedCMethod);
194: }
195:
196: public void testMethodPublic() throws Exception {
197: // direct
198: Method m = IntrospectionUtil.findMethod(ServletC.class,
199: "setPublicC", __INTEGER_ARG, true, false);
200: assertEquals(m, publicCMethod);
201:
202: //inherited
203: m = IntrospectionUtil.findMethod(ServletD.class, "setPublicC",
204: __INTEGER_ARG, true, false);
205: assertEquals(m, publicCMethod);
206: }
207:
208: public void testMethodDefault() throws Exception {
209: // direct
210: Method m = IntrospectionUtil.findMethod(ServletC.class,
211: "setDefaultC", __INTEGER_ARG, true, false);
212: assertEquals(m, defaultCMethod);
213:
214: //inherited
215: m = IntrospectionUtil.findMethod(ServletD.class, "setDefaultC",
216: __INTEGER_ARG, true, false);
217: assertEquals(m, defaultCMethod);
218: }
219: }
|