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    package org.apache.commons.dbutils;
018    
019    import java.lang.reflect.InvocationHandler;
020    import java.lang.reflect.Method;
021    import java.sql.ResultSetMetaData;
022    
023    /**
024     * MockResultSetMetaData dynamically implements the ResultSetMetaData 
025     * interface.
026     */
027    public class MockResultSetMetaData implements InvocationHandler {
028    
029        private String[] columnNames = null;
030    
031        /**
032         * Create a <code>MockResultSetMetaData</code> proxy object.  This is 
033         * equivalent to:
034         * <pre>
035         * ProxyFactory.instance().createResultSetMetaData(new MockResultSetMetaData(columnNames));
036         * </pre>
037         * 
038         * @param columnNames
039         * @return the proxy object
040         */
041        public static ResultSetMetaData create(String[] columnNames) {
042            return ProxyFactory.instance().createResultSetMetaData(
043                new MockResultSetMetaData(columnNames));
044        }
045    
046        public MockResultSetMetaData(String[] columnNames) {
047            super();
048            this.columnNames = columnNames;
049    
050        }
051    
052        public Object invoke(Object proxy, Method method, Object[] args)
053            throws Throwable {
054    
055            String methodName = method.getName();
056    
057            if (methodName.equals("getColumnCount")) {
058                return new Integer(this.columnNames.length);
059    
060            } else if (
061                methodName.equals("getColumnName")
062                    || methodName.equals("getColumnLabel")) {
063    
064                int col = ((Integer) args[0]).intValue() - 1;
065                return this.columnNames[col];
066    
067            } else if (methodName.equals("hashCode")) {
068                return new Integer(System.identityHashCode(proxy));
069    
070            } else if (methodName.equals("toString")) {
071                return "MockResultSetMetaData " + System.identityHashCode(proxy);
072    
073            } else if (methodName.equals("equals")) { 
074                return new Boolean(proxy == args[0]); 
075    
076            } else {
077                throw new UnsupportedOperationException("Unsupported method: " + methodName);
078            }
079        }
080    }