    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.beanutils.bugs;
    18
    19  import junit.framework.Test;
    20  import junit.framework.TestCase;
    21  import junit.framework.TestSuite;
    22
    23  import org.apache.commons.beanutils.PropertyUtils;
    24  import org.apache.commons.beanutils.bugs.other.Jira18BeanFactory;
    25  import org.apache.commons.logging.Log;
    26  import org.apache.commons.logging.LogFactory;
    27
    28  /***
    29   * Test case for Jira issue# BEANUTILS-18.
    30   * <p />
    31   * See https://issues.apache.org/jira/browse/BEANUTILS-18
    32   * <p />
    33   *
    34   *
    35   * <p />
    36   * This test case demonstrates the issue.
    37   *
    38   * @version $Revision: 556237 $ $Date: 2007-07-14 08:27:18 +0100 (Sat, 14 Jul 2007) $
    39   */
    40  public class Jira18TestCase extends TestCase {
    41
    42      private Log log = LogFactory.getLog(Jira18TestCase.class);
    43      private Object bean;
    44
    45      /***
    46       * Create a test case with the specified name.
    47       *
    48       * @param name The name of the test
    49       */
    50      public Jira18TestCase(String name) {
    51          super(name);
    52      }
    53
    54      /***
    55       * Run the Test.
    56       *
    57       * @param args Arguments
    58       */
    59      public static void main(String[] args) {
    60          junit.textui.TestRunner.run(suite());
    61      }
    62
    63      /***
    64       * Create a test suite for this test.
    65       *
    66       * @return a test suite
    67       */
    68      public static Test suite() {
    69          return (new TestSuite(Jira18TestCase.class));
    70      }
    71
    72      /***
    73       * Set up.
    74       *
    75       * @throws java.lang.Exception
    76       */
    77      protected void setUp() throws Exception {
    78          super.setUp();
    79          bean = Jira18BeanFactory.createBean();
    80      }
    81
    82      /***
    83       * Tear Down.
    84       *
    85       * @throws java.lang.Exception
    86       */
    87      protected void tearDown() throws Exception {
    88          super.tearDown();
    89      }
    90
    91      /***
    92       * Test {@link PropertyUtils#isReadable(Object, String)}
    93       * for simple properties.
    94       */
    95      public void testIssue_BEANUTILS_18_PropertyUtils_isReadable() {
    96          boolean result = false;
    97          try {
    98              result = PropertyUtils.isReadable(bean, "simple");
    99          } catch (Throwable t) {
    100             log.error("ERROR " + t, t);
    101             fail("Threw exception: " + t);
    102         }
    103         assertFalse("PropertyUtils.isReadable(bean, \"simple\") returned true", result);
    104     }
    105
    106     /***
    107      * Test {@link PropertyUtils#isWriteable(Object, String)}
    108      * for simple properties.
    109      */
    110     public void testIssue_BEANUTILS_18_PropertyUtils_isWriteable() {
    111         boolean result = false;
    112         try {
    113             result = PropertyUtils.isWriteable(bean, "simple");
    114         } catch (Throwable t) {
    115             log.error("ERROR " + t, t);
    116             fail("Threw exception: " + t);
    117         }
    118         assertFalse("PropertyUtils.isWriteable(bean, \"simple\") returned true", result);
    119     }
    120
    121     /***
    122      * Test {@link PropertyUtils#isReadable(Object, String)}
    123      * for indexed properties.
    124      */
    125     public void testIssue_BEANUTILS_18_PropertyUtils_isReadable_Indexed() {
    126         boolean result = false;
    127         try {
    128             result = PropertyUtils.isReadable(bean, "indexed");
    129         } catch (Throwable t) {
    130             log.error("ERROR " + t, t);
    131             fail("Threw exception: " + t);
    132         }
    133         assertFalse("PropertyUtils.isReadable(bean, \"indexed\") returned true", result);
    134     }
    135
    136     /***
    137      * Test {@link PropertyUtils#isWriteable(Object, String)}
    138      * for indexed properties.
    139      */
    140     public void testIssue_BEANUTILS_18_PropertyUtils_isWriteable_Indexed() {
    141         boolean result = false;
    142         try {
    143             result = PropertyUtils.isWriteable(bean, "indexed");
    144         } catch (Throwable t) {
    145             log.error("ERROR " + t, t);
    146             fail("Threw exception: " + t);
    147         }
    148         assertFalse("PropertyUtils.isWriteable(bean, \"indexed\") returned true", result);
    149     }
    150
    151     /***
    152      * Test {@link PropertyUtils#isReadable(Object, String)}
    153      * for Mapped properties.
    154      */
    155     public void testIssue_BEANUTILS_18_PropertyUtils_isReadable_Mapped() {
    156         boolean result = false;
    157         try {
    158             result = PropertyUtils.isReadable(bean, "mapped");
    159         } catch (Throwable t) {
    160             log.error("ERROR " + t, t);
    161             fail("Threw exception: " + t);
    162         }
    163         assertFalse("PropertyUtils.isReadable(bean, \"mapped\") returned true", result);
    164     }
    165
    166     /***
    167      * Test {@link PropertyUtils#isWriteable(Object, String)}
    168      * for Mapped properties.
    169      */
    170     public void testIssue_BEANUTILS_18_PropertyUtils_isWriteable_Mapped() {
    171         boolean result = false;
    172         try {
    173             result = PropertyUtils.isWriteable(bean, "mapped");
    174         } catch (Throwable t) {
    175             log.error("ERROR " + t, t);
    176             fail("Threw exception: " + t);
    177         }
    178         assertFalse("PropertyUtils.isWriteable(bean, \"mapped\") returned true", result);
    179     }
    180
    181     /***
    182      * Test {@link PropertyUtils#getProperty(Object, String)}
    183      * for simple properties.
    184      */
    185     public void testIssue_BEANUTILS_18_PropertyUtils_getProperty() {
    186         boolean threwNoSuchMethodException = false;
    187         Object result = null;
    188         try {
    189             result = PropertyUtils.getProperty(bean, "simple");
    190         } catch (NoSuchMethodException ex) {
    191             threwNoSuchMethodException = true; // expected result
    192         } catch (Throwable t) {
    193             log.error("ERROR " + t, t);
    194             fail("Threw exception: " + t);
    195         }
    196         assertTrue("Expected NoSuchMethodException but returned '" + result + "'", threwNoSuchMethodException);
    197     }
    198
    199     /***
    200      * Test {@link PropertyUtils#setProperty(Object, String, Object)}
    201      * for simple properties.
    202      */
    203     public void testIssue_BEANUTILS_18_PropertyUtils_setProperty() {
    204         boolean threwNoSuchMethodException = false;
    205         try {
    206             PropertyUtils.setProperty(bean, "simple", "BAR");
    207         } catch (NoSuchMethodException ex) {
    208             threwNoSuchMethodException = true; // expected result
    209         } catch (Throwable t) {
    210             log.error("ERROR " + t, t);
    211             fail("Threw exception: " + t);
    212         }
    213         assertTrue("Expected NoSuchMethodException", threwNoSuchMethodException);
    214     }
    215
    216     /***
    217      * Test {@link PropertyUtils#getProperty(Object, String)}
    218      * for indexed properties.
    219      */
    220     public void testIssue_BEANUTILS_18_PropertyUtils_getProperty_Indexed() {
    221         boolean threwNoSuchMethodException = false;
    222         Object result = null;
    223         try {
    224             result = PropertyUtils.getProperty(bean, "indexed[0]");
    225         } catch (NoSuchMethodException ex) {
    226             threwNoSuchMethodException = true; // expected result
    227         } catch (Throwable t) {
    228             log.error("ERROR " + t, t);
    229             fail("Threw exception: " + t);
    230         }
    231         assertTrue("Expected NoSuchMethodException but returned '" + result + "'", threwNoSuchMethodException);
    232     }
    233
    234     /***
    235      * Test {@link PropertyUtils#setProperty(Object, String, Object)}
    236      * for indexed properties.
    237      */
    238     public void testIssue_BEANUTILS_18_PropertyUtils_setProperty_Indexed() {
    239         boolean threwNoSuchMethodException = false;
    240         try {
    241             PropertyUtils.setProperty(bean, "indexed[0]", "BAR");
    242         } catch (NoSuchMethodException ex) {
    243             threwNoSuchMethodException = true; // expected result
    244         } catch (Throwable t) {
    245             log.error("ERROR " + t, t);
    246             fail("Threw exception: " + t);
    247         }
    248         assertTrue("Expected NoSuchMethodException", threwNoSuchMethodException);
    249     }
    250
    251     /***
    252      * Test {@link PropertyUtils#getProperty(Object, String)}
    253      * for mapped properties.
    254      */
    255     public void testIssue_BEANUTILS_18_PropertyUtils_getProperty_Mapped() {
    256         boolean threwNoSuchMethodException = false;
    257         Object result = null;
    258         try {
    259             result = PropertyUtils.getProperty(bean, "mapped(foo-key)");
    260         } catch (NoSuchMethodException ex) {
    261             threwNoSuchMethodException = true; // expected result
    262         } catch (Throwable t) {
    263             log.error("ERROR " + t, t);
    264             fail("Threw exception: " + t);
    265         }
    266         assertTrue("Expected NoSuchMethodException but returned '" + result + "'", threwNoSuchMethodException);
    267     }
    268
    269     /***
    270      * Test {@link PropertyUtils#setProperty(Object, String, Object)}
    271      * for mapped properties.
    272      */
    273     public void testIssue_BEANUTILS_18_PropertyUtils_setProperty_Mapped() {
    274         boolean threwNoSuchMethodException = false;
    275         try {
    276             PropertyUtils.setProperty(bean, "mapped(foo-key)", "BAR");
    277         } catch (NoSuchMethodException ex) {
    278             threwNoSuchMethodException = true; // expected result
    279         } catch (Throwable t) {
    280             log.error("ERROR " + t, t);
    281             fail("Threw exception: " + t);
    282         }
    283         assertTrue("Expected NoSuchMethodException", threwNoSuchMethodException);
    284     }
    285 }