    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 java.io.Serializable;
    20  import java.util.Map;
    21
    22  import junit.framework.Test;
    23  import junit.framework.TestCase;
    24  import junit.framework.TestSuite;
    25
    26  import org.apache.commons.beanutils.BeanUtils;
    27  import org.apache.commons.logging.Log;
    28  import org.apache.commons.logging.LogFactory;
    29
    30  /***
    31   * Beanutils's describe() method cannot determine reader methods
    32   * for anonymous class - see Jira issue# BEANUTILS-157.
    33   * <p />
    34   * See https://issues.apache.org/jira/browse/BEANUTILS-157
    35   * <p />
    36   *
    37   * @version $Revision: 555173 $ $Date: 2007-07-11 06:25:07 +0100 (Wed, 11 Jul 2007) $
    38   */
    39  public class Jira157TestCase extends TestCase {
    40
    41      private Log log = LogFactory.getLog(Jira157TestCase.class);
    42
    43      /***
    44       * Create a test case with the specified name.
    45       *
    46       * @param name The name of the test
    47       */
    48      public Jira157TestCase(String name) {
    49          super(name);
    50      }
    51
    52      /***
    53       * Run the Test.
    54       *
    55       * @param args Arguments
    56       */
    57      public static void main(String[] args) {
    58          junit.textui.TestRunner.run(suite());
    59      }
    60
    61      /***
    62       * Create a test suite for this test.
    63       *
    64       * @return a test suite
    65       */
    66      public static Test suite() {
    67          return (new TestSuite(Jira157TestCase.class));
    68      }
    69
    70      /***
    71       * Set up.
    72       *
    73       * @throws java.lang.Exception
    74       */
    75      protected void setUp() throws Exception {
    76          super.setUp();
    77      }
    78
    79      /***
    80       * Tear Down.
    81       *
    82       * @throws java.lang.Exception
    83       */
    84      protected void tearDown() throws Exception {
    85          super.tearDown();
    86      }
    87
    88      /***
    89       * Test with an private class that overrides a public method
    90       * of a "grand parent" public class.
    91       * <p />
    92       * See Jira issue# BEANUTILS-157.
    93       */
    94      public void testIssue_BEANUTILS_157_BeanUtils_Describe_Serializable() {
    95          Object bean = new Serializable() {
    96              public String getX() {
    97                  return "x-value";
    98              }
    99              public String getY() {
    100                 return "y-value";
    101             }
    102         };
    103         Map result = null;
    104         try {
    105             result = BeanUtils.describe(bean);
    106         } catch (Throwable t) {
    107             log.error("Describe Serializable: " + t.getMessage(), t);
    108             fail("Describe Serializable Threw exception: " + t);
    109         }
    110         assertEquals("Check Size", 1, result.size());
    111         assertTrue("Class", result.containsKey("class"));
    112     }
    113
    114     /***
    115      * Test with an private class that overrides a public method
    116      * of a "grand parent" public class.
    117      * <p />
    118      * See Jira issue# BEANUTILS-157.
    119      */
    120     public void testIssue_BEANUTILS_157_BeanUtils_Describe_Interface() {
    121         Object bean = new XY() {
    122             public String getX() {
    123                 return "x-value";
    124             }
    125             public String getY() {
    126                 return "y-value";
    127             }
    128         };
    129         Map result = null;
    130         try {
    131             result = BeanUtils.describe(bean);
    132         } catch (Throwable t) {
    133             log.error("Describe Interface: " + t.getMessage(), t);
    134             fail("Describe Interface Threw exception: " + t);
    135         }
    136         assertEquals("Check Size", 3, result.size());
    137         assertTrue("Class", result.containsKey("class"));
    138         assertTrue("X Key", result.containsKey("x"));
    139         assertTrue("Y Key", result.containsKey("y"));
    140         assertEquals("X Value", "x-value", result.get("x"));
    141         assertEquals("Y Value", "y-value", result.get("y"));
    142     }
    143
    144     /***
    145      * Test with an private class that overrides a public method
    146      * of a "grand parent" public class.
    147      * <p />
    148      * See Jira issue# BEANUTILS-157.
    149      */
    150     public void testIssue_BEANUTILS_157_BeanUtils_Describe_Bean() {
    151         Object bean = new FooBar();
    152         Map result = null;
    153         try {
    154             result = BeanUtils.describe(bean);
    155         } catch (Throwable t) {
    156             log.error("Describe Bean: " + t.getMessage(), t);
    157             fail("Describe Bean Threw exception: " + t);
    158         }
    159         assertEquals("Check Size", 2, result.size());
    160         assertTrue("Class", result.containsKey("class"));
    161         assertTrue("publicFoo Key", result.containsKey("publicFoo"));
    162         assertEquals("publicFoo Value", "PublicFoo Value", result.get("publicFoo"));
    163     }
    164
    165     public static interface XY {
    166         String getX();
    167         String getY();
    168     };
    169
    170     public static class FooBar {
    171         String getPackageFoo() {
    172             return "Package Value";
    173         }
    174         private String getPrivateFoo() {
    175             return "PrivateFoo Value";
    176         }
    177         protected String getProtectedFoo() {
    178             return "ProtectedFoo Value";
    179         }
    180         public String getPublicFoo() {
    181             return "PublicFoo Value";
    182         }
    183     };
    184 }