001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.beanutils.bugs;
018:
019: import java.io.Serializable;
020: import java.util.Map;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: import org.apache.commons.beanutils.BeanUtils;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: /**
031: * Beanutils's describe() method cannot determine reader methods
032: * for anonymous class - see Jira issue# BEANUTILS-157.
033: * <p />
034: * See https://issues.apache.org/jira/browse/BEANUTILS-157
035: * <p />
036: *
037: * @version $Revision: 555173 $ $Date: 2007-07-11 06:25:07 +0100 (Wed, 11 Jul 2007) $
038: */
039: public class Jira157TestCase extends TestCase {
040:
041: private Log log = LogFactory.getLog(Jira157TestCase.class);
042:
043: /**
044: * Create a test case with the specified name.
045: *
046: * @param name The name of the test
047: */
048: public Jira157TestCase(String name) {
049: super (name);
050: }
051:
052: /**
053: * Run the Test.
054: *
055: * @param args Arguments
056: */
057: public static void main(String[] args) {
058: junit.textui.TestRunner.run(suite());
059: }
060:
061: /**
062: * Create a test suite for this test.
063: *
064: * @return a test suite
065: */
066: public static Test suite() {
067: return (new TestSuite(Jira157TestCase.class));
068: }
069:
070: /**
071: * Set up.
072: *
073: * @throws java.lang.Exception
074: */
075: protected void setUp() throws Exception {
076: super .setUp();
077: }
078:
079: /**
080: * Tear Down.
081: *
082: * @throws java.lang.Exception
083: */
084: protected void tearDown() throws Exception {
085: super .tearDown();
086: }
087:
088: /**
089: * Test with an private class that overrides a public method
090: * of a "grand parent" public class.
091: * <p />
092: * See Jira issue# BEANUTILS-157.
093: */
094: public void testIssue_BEANUTILS_157_BeanUtils_Describe_Serializable() {
095: Object bean = new Serializable() {
096: public String getX() {
097: return "x-value";
098: }
099:
100: public String getY() {
101: return "y-value";
102: }
103: };
104: Map result = null;
105: try {
106: result = BeanUtils.describe(bean);
107: } catch (Throwable t) {
108: log.error("Describe Serializable: " + t.getMessage(), t);
109: fail("Describe Serializable Threw exception: " + t);
110: }
111: assertEquals("Check Size", 1, result.size());
112: assertTrue("Class", result.containsKey("class"));
113: }
114:
115: /**
116: * Test with an private class that overrides a public method
117: * of a "grand parent" public class.
118: * <p />
119: * See Jira issue# BEANUTILS-157.
120: */
121: public void testIssue_BEANUTILS_157_BeanUtils_Describe_Interface() {
122: Object bean = new XY() {
123: public String getX() {
124: return "x-value";
125: }
126:
127: public String getY() {
128: return "y-value";
129: }
130: };
131: Map result = null;
132: try {
133: result = BeanUtils.describe(bean);
134: } catch (Throwable t) {
135: log.error("Describe Interface: " + t.getMessage(), t);
136: fail("Describe Interface Threw exception: " + t);
137: }
138: assertEquals("Check Size", 3, result.size());
139: assertTrue("Class", result.containsKey("class"));
140: assertTrue("X Key", result.containsKey("x"));
141: assertTrue("Y Key", result.containsKey("y"));
142: assertEquals("X Value", "x-value", result.get("x"));
143: assertEquals("Y Value", "y-value", result.get("y"));
144: }
145:
146: /**
147: * Test with an private class that overrides a public method
148: * of a "grand parent" public class.
149: * <p />
150: * See Jira issue# BEANUTILS-157.
151: */
152: public void testIssue_BEANUTILS_157_BeanUtils_Describe_Bean() {
153: Object bean = new FooBar();
154: Map result = null;
155: try {
156: result = BeanUtils.describe(bean);
157: } catch (Throwable t) {
158: log.error("Describe Bean: " + t.getMessage(), t);
159: fail("Describe Bean Threw exception: " + t);
160: }
161: assertEquals("Check Size", 2, result.size());
162: assertTrue("Class", result.containsKey("class"));
163: assertTrue("publicFoo Key", result.containsKey("publicFoo"));
164: assertEquals("publicFoo Value", "PublicFoo Value", result
165: .get("publicFoo"));
166: }
167:
168: public static interface XY {
169: String getX();
170:
171: String getY();
172: };
173:
174: public static class FooBar {
175: String getPackageFoo() {
176: return "Package Value";
177: }
178:
179: private String getPrivateFoo() {
180: return "PrivateFoo Value";
181: }
182:
183: protected String getProtectedFoo() {
184: return "ProtectedFoo Value";
185: }
186:
187: public String getPublicFoo() {
188: return "PublicFoo Value";
189: }
190: };
191: }
|