001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils;
019:
020: import java.lang.reflect.InvocationHandler;
021: import java.lang.reflect.Method;
022: import java.lang.reflect.Proxy;
023: import java.sql.ResultSetMetaData;
024: import java.sql.SQLException;
025: import java.sql.Types;
026:
027: /**
028: * <p>Mock object that implements enough of
029: * <code>java.sql.ResultSetMetaData</code>
030: * to exercise the {@link ResultSetDynaClass} functionality.</p>
031: *
032: * @author Craig R. McClanahan
033: * @version $Revision: 561644 $ $Date: 2007-08-01 05:31:50 +0100 (Wed, 01 Aug 2007) $
034: */
035:
036: public class TestResultSetMetaData implements InvocationHandler {
037:
038: // ----------------------------------------------------- Instance Variables
039:
040: /**
041: * <p>Array of column names and class names for metadata.</p>
042: */
043: protected String metadata[][] = {
044: { "bigDecimalProperty",
045: java.math.BigDecimal.class.getName() },
046: { "booleanProperty", Boolean.class.getName() },
047: { "byteProperty", Byte.class.getName() },
048: { "dateProperty", java.sql.Date.class.getName() },
049: { "doubleProperty", Double.class.getName() },
050: { "floatProperty", Float.class.getName() },
051: { "intProperty", Integer.class.getName() },
052: { "longProperty", Long.class.getName() },
053: { "nullProperty", String.class.getName() },
054: { "shortProperty", Short.class.getName() },
055: { "stringProperty", String.class.getName() },
056: { "timeProperty", java.sql.Time.class.getName() },
057: { "timestampProperty", java.sql.Timestamp.class.getName() }, };
058:
059: /**
060: * Factory method for creating {@link ResultSetMetaData} proxies.
061: *
062: * @return A result set meta data proxy
063: */
064: public static ResultSetMetaData createProxy() {
065: return TestResultSetMetaData
066: .createProxy(new TestResultSetMetaData());
067: }
068:
069: /**
070: * Factory method for creating {@link ResultSetMetaData} proxies.
071: * @param invocationHandler Invocation Handler
072: * @return A result set meta data proxy
073: */
074: public static ResultSetMetaData createProxy(
075: InvocationHandler invocationHandler) {
076: ClassLoader classLoader = ResultSetMetaData.class
077: .getClassLoader();
078: Class[] interfaces = new Class[] { ResultSetMetaData.class };
079: return (ResultSetMetaData) Proxy.newProxyInstance(classLoader,
080: interfaces, invocationHandler);
081: }
082:
083: /**
084: * Handles method invocation on the {@link ResultSetMetaData} proxy.
085: *
086: * @param proxy The proxy ResultSet object
087: * @param method the method being invoked
088: * @param args The method arguments
089: * @return The result of invoking the method.
090: * @throws Throwable if an error occurs.
091: */
092: public Object invoke(Object proxy, Method method, Object[] args)
093: throws Throwable {
094: String methodName = method.getName();
095: if ("getColumnClassName".equals(methodName)) {
096: return getColumnClassName(((Integer) args[0]).intValue());
097: }
098: if ("getColumnCount".equals(methodName)) {
099: return new Integer(getColumnCount());
100: }
101: if ("getColumnName".equals(methodName)) {
102: return getColumnName(((Integer) args[0]).intValue());
103: }
104: if ("getColumnType".equals(methodName)) {
105: return getColumnType(((Integer) args[0]).intValue());
106: }
107:
108: throw new UnsupportedOperationException(methodName
109: + " not implemented");
110: }
111:
112: // ---------------------------------------------------- Implemented Methods
113:
114: public String getColumnClassName(int columnIndex)
115: throws SQLException {
116: return (metadata[columnIndex - 1][1]);
117: }
118:
119: public int getColumnCount() throws SQLException {
120: return (metadata.length);
121: }
122:
123: public String getColumnName(int columnIndex) throws SQLException {
124: return (metadata[columnIndex - 1][0]);
125: }
126:
127: public Integer getColumnType(int columnIndex) throws SQLException {
128: String columnName = getColumnName(columnIndex);
129: int sqlType = Types.OTHER;
130: if (columnName.equals("bigDecimalProperty")) {
131: sqlType = Types.DECIMAL;
132: // Types.BOOLEAN only introduced in JDK 1.4
133: // } else if (columnName.equals("booleanProperty")) {
134: // sqlType = Types.BOOLEAN;
135: } else if (columnName.equals("byteProperty")) {
136: sqlType = Types.TINYINT;
137: } else if (columnName.equals("dateProperty")) {
138: sqlType = Types.DATE;
139: } else if (columnName.equals("doubleProperty")) {
140: sqlType = Types.DOUBLE;
141: } else if (columnName.equals("floatProperty")) {
142: sqlType = Types.FLOAT;
143: } else if (columnName.equals("intProperty")) {
144: sqlType = Types.INTEGER;
145: } else if (columnName.equals("longProperty")) {
146: sqlType = Types.BIGINT;
147: } else if (columnName.equals("nullProperty")) {
148: sqlType = Types.VARCHAR;
149: } else if (columnName.equals("shortProperty")) {
150: sqlType = Types.SMALLINT;
151: } else if (columnName.equals("stringProperty")) {
152: sqlType = Types.VARCHAR;
153: } else if (columnName.equals("timeProperty")) {
154: sqlType = Types.TIME;
155: } else if (columnName.equals("timestampProperty")) {
156: sqlType = Types.TIMESTAMP;
157: } else {
158: sqlType = Types.OTHER;
159: }
160: return new Integer(sqlType);
161: }
162:
163: // -------------------------------------------------- Unimplemented Methods
164:
165: public String getCatalogName(int columnIndex) throws SQLException {
166: throw new UnsupportedOperationException();
167: }
168:
169: public int getColumnDisplaySize(int columnIndex)
170: throws SQLException {
171: throw new UnsupportedOperationException();
172: }
173:
174: public String getColumnLabel(int columnIndex) throws SQLException {
175: throw new UnsupportedOperationException();
176: }
177:
178: public String getColumnTypeName(int columnIndex)
179: throws SQLException {
180: throw new UnsupportedOperationException();
181: }
182:
183: public int getPrecision(int columnIndex) throws SQLException {
184: throw new UnsupportedOperationException();
185: }
186:
187: public int getScale(int columnIndex) throws SQLException {
188: throw new UnsupportedOperationException();
189: }
190:
191: public String getSchemaName(int columnIndex) throws SQLException {
192: throw new UnsupportedOperationException();
193: }
194:
195: public String getTableName(int columnIndex) throws SQLException {
196: throw new UnsupportedOperationException();
197: }
198:
199: public boolean isAutoIncrement(int columnIndex) throws SQLException {
200: throw new UnsupportedOperationException();
201: }
202:
203: public boolean isCaseSensitive(int columnIndex) throws SQLException {
204: throw new UnsupportedOperationException();
205: }
206:
207: public boolean isCurrency(int columnIndex) throws SQLException {
208: throw new UnsupportedOperationException();
209: }
210:
211: public boolean isDefinitelyWritable(int columnIndex)
212: throws SQLException {
213: throw new UnsupportedOperationException();
214: }
215:
216: public int isNullable(int columnIndex) throws SQLException {
217: throw new UnsupportedOperationException();
218: }
219:
220: public boolean isReadOnly(int columnIndex) throws SQLException {
221: throw new UnsupportedOperationException();
222: }
223:
224: public boolean isSearchable(int columnIndex) throws SQLException {
225: throw new UnsupportedOperationException();
226: }
227:
228: public boolean isSigned(int columnIndex) throws SQLException {
229: throw new UnsupportedOperationException();
230: }
231:
232: public boolean isWritable(int columnIndex) throws SQLException {
233: throw new UnsupportedOperationException();
234: }
235:
236: }
|