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