    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
    18
    19  package org.apache.commons.beanutils;
    20
    21
    22  import java.lang.reflect.InvocationHandler;
    23  import java.lang.reflect.Method;
    24  import java.lang.reflect.Proxy;
    25  import java.sql.ResultSetMetaData;
    26  import java.sql.SQLException;
    27  import java.sql.Types;
    28
    29
    30  /***
    31   * <p>Mock object that implements enough of
    32   * <code>java.sql.ResultSetMetaData</code>
    33   * to exercise the {@link ResultSetDynaClass} functionality.</p>
    34   *
    35   * @author Craig R. McClanahan
    36   * @version $Revision: 561644 $ $Date: 2007-08-01 05:31:50 +0100 (Wed, 01 Aug 2007) $
    37   */
    38
    39  public class TestResultSetMetaData implements InvocationHandler {
    40
    41
    42      // ----------------------------------------------------- Instance Variables
    43
    44
    45      /***
    46       * <p>Array of column names and class names for metadata.</p>
    47       */
    48      protected String metadata[][] = {
    49          { "bigDecimalProperty", java.math.BigDecimal.class.getName() },
    50          { "booleanProperty", Boolean.class.getName() },
    51          { "byteProperty", Byte.class.getName() },
    52          { "dateProperty", java.sql.Date.class.getName() },
    53          { "doubleProperty", Double.class.getName() },
    54          { "floatProperty", Float.class.getName() },
    55          { "intProperty", Integer.class.getName() },
    56          { "longProperty", Long.class.getName() },
    57          { "nullProperty", String.class.getName() },
    58          { "shortProperty", Short.class.getName() },
    59          { "stringProperty", String.class.getName() },
    60          { "timeProperty", java.sql.Time.class.getName() },
    61          { "timestampProperty", java.sql.Timestamp.class.getName() },
    62      };
    63
    64
    65      /***
    66       * Factory method for creating {@link ResultSetMetaData} proxies.
    67       *
    68       * @return A result set meta data proxy
    69       */
    70      public static ResultSetMetaData createProxy() {
    71          return TestResultSetMetaData.createProxy(new TestResultSetMetaData());
    72      }
    73
    74      /***
    75       * Factory method for creating {@link ResultSetMetaData} proxies.
    76       * @param invocationHandler Invocation Handler
    77       * @return A result set meta data proxy
    78       */
    79      public static ResultSetMetaData createProxy(InvocationHandler invocationHandler) {
    80          ClassLoader classLoader = ResultSetMetaData.class.getClassLoader();
    81          Class[] interfaces = new Class[] { ResultSetMetaData.class };
    82          return (ResultSetMetaData)Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
    83      }
    84
    85      /***
    86       * Handles method invocation on the {@link ResultSetMetaData} proxy.
    87       *
    88       * @param proxy The proxy ResultSet object
    89       * @param method the method being invoked
    90       * @param args The method arguments
    91       * @return The result of invoking the method.
    92       * @throws Throwable if an error occurs.
    93       */
    94      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    95          String methodName = method.getName();
    96          if ("getColumnClassName".equals(methodName)) {
    97              return getColumnClassName(((Integer)args[0]).intValue());
    98          } if ("getColumnCount".equals(methodName)) {
    99              return new Integer(getColumnCount());
    100         } if ("getColumnName".equals(methodName)) {
    101             return getColumnName(((Integer)args[0]).intValue());
    102         } if ("getColumnType".equals(methodName)) {
    103             return getColumnType(((Integer)args[0]).intValue());
    104         }
    105
    106         throw new UnsupportedOperationException(methodName + " not implemented");
    107     }
    108
    109     // ---------------------------------------------------- Implemented Methods
    110
    111
    112     public String getColumnClassName(int columnIndex) throws SQLException {
    113         return (metadata[columnIndex - 1][1]);
    114     }
    115
    116
    117     public int getColumnCount() throws SQLException {
    118         return (metadata.length);
    119     }
    120
    121     public String getColumnName(int columnIndex) throws SQLException {
    122         return (metadata[columnIndex - 1][0]);
    123     }
    124
    125
    126     public Integer getColumnType(int columnIndex) throws SQLException {
    127         String columnName = getColumnName(columnIndex);
    128         int sqlType = Types.OTHER;
    129         if (columnName.equals("bigDecimalProperty")) {
    130             sqlType = Types.DECIMAL;
    131 // Types.BOOLEAN only introduced in JDK 1.4
    132 //        } else if (columnName.equals("booleanProperty")) {
    133 //            sqlType = Types.BOOLEAN;
    134         } else if (columnName.equals("byteProperty")) {
    135             sqlType = Types.TINYINT;
    136         } else if (columnName.equals("dateProperty")) {
    137             sqlType = Types.DATE;
    138         } else if (columnName.equals("doubleProperty")) {
    139             sqlType = Types.DOUBLE;
    140         } else if (columnName.equals("floatProperty")) {
    141             sqlType = Types.FLOAT;
    142         } else if (columnName.equals("intProperty")) {
    143             sqlType = Types.INTEGER;
    144         } else if (columnName.equals("longProperty")) {
    145             sqlType = Types.BIGINT;
    146         } else if (columnName.equals("nullProperty")) {
    147             sqlType = Types.VARCHAR;
    148         } else if (columnName.equals("shortProperty")) {
    149             sqlType = Types.SMALLINT;
    150         } else if (columnName.equals("stringProperty")) {
    151             sqlType = Types.VARCHAR;
    152         } else if (columnName.equals("timeProperty")) {
    153             sqlType = Types.TIME;
    154         } else if (columnName.equals("timestampProperty")) {
    155             sqlType = Types.TIMESTAMP;
    156         } else {
    157             sqlType = Types.OTHER;
    158         }
    159         return new Integer(sqlType);
    160     }
    161
    162
    163     // -------------------------------------------------- Unimplemented Methods
    164
    165
    166     public String getCatalogName(int columnIndex) throws SQLException {
    167         throw new UnsupportedOperationException();
    168     }
    169
    170
    171     public int getColumnDisplaySize(int columnIndex) throws SQLException {
    172         throw new UnsupportedOperationException();
    173     }
    174
    175
    176     public String getColumnLabel(int columnIndex) throws SQLException {
    177         throw new UnsupportedOperationException();
    178     }
    179
    180
    181     public String getColumnTypeName(int columnIndex) throws SQLException {
    182         throw new UnsupportedOperationException();
    183     }
    184
    185
    186     public int getPrecision(int columnIndex) throws SQLException {
    187         throw new UnsupportedOperationException();
    188     }
    189
    190
    191     public int getScale(int columnIndex) throws SQLException {
    192         throw new UnsupportedOperationException();
    193     }
    194
    195
    196     public String getSchemaName(int columnIndex) throws SQLException {
    197         throw new UnsupportedOperationException();
    198     }
    199
    200
    201     public String getTableName(int columnIndex) throws SQLException {
    202         throw new UnsupportedOperationException();
    203     }
    204
    205
    206     public boolean isAutoIncrement(int columnIndex) throws SQLException {
    207         throw new UnsupportedOperationException();
    208     }
    209
    210
    211     public boolean isCaseSensitive(int columnIndex) throws SQLException {
    212         throw new UnsupportedOperationException();
    213     }
    214
    215
    216     public boolean isCurrency(int columnIndex) throws SQLException {
    217         throw new UnsupportedOperationException();
    218     }
    219
    220
    221     public boolean isDefinitelyWritable(int columnIndex) throws SQLException {
    222         throw new UnsupportedOperationException();
    223     }
    224
    225
    226     public int isNullable(int columnIndex) throws SQLException {
    227         throw new UnsupportedOperationException();
    228     }
    229
    230
    231     public boolean isReadOnly(int columnIndex) throws SQLException {
    232         throw new UnsupportedOperationException();
    233     }
    234
    235
    236     public boolean isSearchable(int columnIndex) throws SQLException {
    237         throw new UnsupportedOperationException();
    238     }
    239
    240
    241     public boolean isSigned(int columnIndex) throws SQLException {
    242         throw new UnsupportedOperationException();
    243     }
    244
    245
    246     public boolean isWritable(int columnIndex) throws SQLException {
    247         throw new UnsupportedOperationException();
    248     }
    249
    250
    251 }