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