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 junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022:
023: import org.apache.commons.beanutils.PropertyUtils;
024: import org.apache.commons.beanutils.bugs.other.Jira18BeanFactory;
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: /**
029: * Test case for Jira issue# BEANUTILS-18.
030: * <p />
031: * See https://issues.apache.org/jira/browse/BEANUTILS-18
032: * <p />
033: *
034: *
035: * <p />
036: * This test case demonstrates the issue.
037: *
038: * @version $Revision: 556237 $ $Date: 2007-07-14 08:27:18 +0100 (Sat, 14 Jul 2007) $
039: */
040: public class Jira18TestCase extends TestCase {
041:
042: private Log log = LogFactory.getLog(Jira18TestCase.class);
043: private Object bean;
044:
045: /**
046: * Create a test case with the specified name.
047: *
048: * @param name The name of the test
049: */
050: public Jira18TestCase(String name) {
051: super (name);
052: }
053:
054: /**
055: * Run the Test.
056: *
057: * @param args Arguments
058: */
059: public static void main(String[] args) {
060: junit.textui.TestRunner.run(suite());
061: }
062:
063: /**
064: * Create a test suite for this test.
065: *
066: * @return a test suite
067: */
068: public static Test suite() {
069: return (new TestSuite(Jira18TestCase.class));
070: }
071:
072: /**
073: * Set up.
074: *
075: * @throws java.lang.Exception
076: */
077: protected void setUp() throws Exception {
078: super .setUp();
079: bean = Jira18BeanFactory.createBean();
080: }
081:
082: /**
083: * Tear Down.
084: *
085: * @throws java.lang.Exception
086: */
087: protected void tearDown() throws Exception {
088: super .tearDown();
089: }
090:
091: /**
092: * Test {@link PropertyUtils#isReadable(Object, String)}
093: * for simple properties.
094: */
095: public void testIssue_BEANUTILS_18_PropertyUtils_isReadable() {
096: boolean result = false;
097: try {
098: result = PropertyUtils.isReadable(bean, "simple");
099: } catch (Throwable t) {
100: log.error("ERROR " + t, t);
101: fail("Threw exception: " + t);
102: }
103: assertFalse(
104: "PropertyUtils.isReadable(bean, \"simple\") returned true",
105: result);
106: }
107:
108: /**
109: * Test {@link PropertyUtils#isWriteable(Object, String)}
110: * for simple properties.
111: */
112: public void testIssue_BEANUTILS_18_PropertyUtils_isWriteable() {
113: boolean result = false;
114: try {
115: result = PropertyUtils.isWriteable(bean, "simple");
116: } catch (Throwable t) {
117: log.error("ERROR " + t, t);
118: fail("Threw exception: " + t);
119: }
120: assertFalse(
121: "PropertyUtils.isWriteable(bean, \"simple\") returned true",
122: result);
123: }
124:
125: /**
126: * Test {@link PropertyUtils#isReadable(Object, String)}
127: * for indexed properties.
128: */
129: public void testIssue_BEANUTILS_18_PropertyUtils_isReadable_Indexed() {
130: boolean result = false;
131: try {
132: result = PropertyUtils.isReadable(bean, "indexed");
133: } catch (Throwable t) {
134: log.error("ERROR " + t, t);
135: fail("Threw exception: " + t);
136: }
137: assertFalse(
138: "PropertyUtils.isReadable(bean, \"indexed\") returned true",
139: result);
140: }
141:
142: /**
143: * Test {@link PropertyUtils#isWriteable(Object, String)}
144: * for indexed properties.
145: */
146: public void testIssue_BEANUTILS_18_PropertyUtils_isWriteable_Indexed() {
147: boolean result = false;
148: try {
149: result = PropertyUtils.isWriteable(bean, "indexed");
150: } catch (Throwable t) {
151: log.error("ERROR " + t, t);
152: fail("Threw exception: " + t);
153: }
154: assertFalse(
155: "PropertyUtils.isWriteable(bean, \"indexed\") returned true",
156: result);
157: }
158:
159: /**
160: * Test {@link PropertyUtils#isReadable(Object, String)}
161: * for Mapped properties.
162: */
163: public void testIssue_BEANUTILS_18_PropertyUtils_isReadable_Mapped() {
164: boolean result = false;
165: try {
166: result = PropertyUtils.isReadable(bean, "mapped");
167: } catch (Throwable t) {
168: log.error("ERROR " + t, t);
169: fail("Threw exception: " + t);
170: }
171: assertFalse(
172: "PropertyUtils.isReadable(bean, \"mapped\") returned true",
173: result);
174: }
175:
176: /**
177: * Test {@link PropertyUtils#isWriteable(Object, String)}
178: * for Mapped properties.
179: */
180: public void testIssue_BEANUTILS_18_PropertyUtils_isWriteable_Mapped() {
181: boolean result = false;
182: try {
183: result = PropertyUtils.isWriteable(bean, "mapped");
184: } catch (Throwable t) {
185: log.error("ERROR " + t, t);
186: fail("Threw exception: " + t);
187: }
188: assertFalse(
189: "PropertyUtils.isWriteable(bean, \"mapped\") returned true",
190: result);
191: }
192:
193: /**
194: * Test {@link PropertyUtils#getProperty(Object, String)}
195: * for simple properties.
196: */
197: public void testIssue_BEANUTILS_18_PropertyUtils_getProperty() {
198: boolean threwNoSuchMethodException = false;
199: Object result = null;
200: try {
201: result = PropertyUtils.getProperty(bean, "simple");
202: } catch (NoSuchMethodException ex) {
203: threwNoSuchMethodException = true; // expected result
204: } catch (Throwable t) {
205: log.error("ERROR " + t, t);
206: fail("Threw exception: " + t);
207: }
208: assertTrue("Expected NoSuchMethodException but returned '"
209: + result + "'", threwNoSuchMethodException);
210: }
211:
212: /**
213: * Test {@link PropertyUtils#setProperty(Object, String, Object)}
214: * for simple properties.
215: */
216: public void testIssue_BEANUTILS_18_PropertyUtils_setProperty() {
217: boolean threwNoSuchMethodException = false;
218: try {
219: PropertyUtils.setProperty(bean, "simple", "BAR");
220: } catch (NoSuchMethodException ex) {
221: threwNoSuchMethodException = true; // expected result
222: } catch (Throwable t) {
223: log.error("ERROR " + t, t);
224: fail("Threw exception: " + t);
225: }
226: assertTrue("Expected NoSuchMethodException",
227: threwNoSuchMethodException);
228: }
229:
230: /**
231: * Test {@link PropertyUtils#getProperty(Object, String)}
232: * for indexed properties.
233: */
234: public void testIssue_BEANUTILS_18_PropertyUtils_getProperty_Indexed() {
235: boolean threwNoSuchMethodException = false;
236: Object result = null;
237: try {
238: result = PropertyUtils.getProperty(bean, "indexed[0]");
239: } catch (NoSuchMethodException ex) {
240: threwNoSuchMethodException = true; // expected result
241: } catch (Throwable t) {
242: log.error("ERROR " + t, t);
243: fail("Threw exception: " + t);
244: }
245: assertTrue("Expected NoSuchMethodException but returned '"
246: + result + "'", threwNoSuchMethodException);
247: }
248:
249: /**
250: * Test {@link PropertyUtils#setProperty(Object, String, Object)}
251: * for indexed properties.
252: */
253: public void testIssue_BEANUTILS_18_PropertyUtils_setProperty_Indexed() {
254: boolean threwNoSuchMethodException = false;
255: try {
256: PropertyUtils.setProperty(bean, "indexed[0]", "BAR");
257: } catch (NoSuchMethodException ex) {
258: threwNoSuchMethodException = true; // expected result
259: } catch (Throwable t) {
260: log.error("ERROR " + t, t);
261: fail("Threw exception: " + t);
262: }
263: assertTrue("Expected NoSuchMethodException",
264: threwNoSuchMethodException);
265: }
266:
267: /**
268: * Test {@link PropertyUtils#getProperty(Object, String)}
269: * for mapped properties.
270: */
271: public void testIssue_BEANUTILS_18_PropertyUtils_getProperty_Mapped() {
272: boolean threwNoSuchMethodException = false;
273: Object result = null;
274: try {
275: result = PropertyUtils.getProperty(bean, "mapped(foo-key)");
276: } catch (NoSuchMethodException ex) {
277: threwNoSuchMethodException = true; // expected result
278: } catch (Throwable t) {
279: log.error("ERROR " + t, t);
280: fail("Threw exception: " + t);
281: }
282: assertTrue("Expected NoSuchMethodException but returned '"
283: + result + "'", threwNoSuchMethodException);
284: }
285:
286: /**
287: * Test {@link PropertyUtils#setProperty(Object, String, Object)}
288: * for mapped properties.
289: */
290: public void testIssue_BEANUTILS_18_PropertyUtils_setProperty_Mapped() {
291: boolean threwNoSuchMethodException = false;
292: try {
293: PropertyUtils.setProperty(bean, "mapped(foo-key)", "BAR");
294: } catch (NoSuchMethodException ex) {
295: threwNoSuchMethodException = true; // expected result
296: } catch (Throwable t) {
297: log.error("ERROR " + t, t);
298: fail("Threw exception: " + t);
299: }
300: assertTrue("Expected NoSuchMethodException",
301: threwNoSuchMethodException);
302: }
303: }
|