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  package org.apache.commons.dbutils;
18  
19  import java.lang.reflect.InvocationHandler;
20  import java.lang.reflect.Method;
21  import java.sql.ResultSetMetaData;
22  
23  /**
24   * MockResultSetMetaData dynamically implements the ResultSetMetaData
25   * interface.
26   */
27  public class MockResultSetMetaData implements InvocationHandler {
28  
29      /**
30       * Create a {@code MockResultSetMetaData} proxy object.  This is
31       * equivalent to:
32       * <pre>
33       * ProxyFactory.instance().createResultSetMetaData(new MockResultSetMetaData(columnNames));
34       * </pre>
35       *
36       * @param columnNames
37       * @return the proxy object
38       */
39      public static ResultSetMetaData create(final String[] columnNames) {
40          return ProxyFactory.instance().createResultSetMetaData(
41              new MockResultSetMetaData(columnNames));
42      }
43      private String[] columnNames = null;
44  
45      private String[] columnLabels = null;
46  
47      public MockResultSetMetaData(final String[] columnNames) {
48          this.columnNames = columnNames;
49          this.columnLabels = new String[columnNames.length];
50  
51      }
52  
53      public MockResultSetMetaData(final String[] columnNames, final String[] columnLabels) {
54          this.columnNames = columnNames;
55          this.columnLabels = columnLabels;
56  
57      }
58  
59      @Override
60      public Object invoke(final Object proxy, final Method method, final Object[] args)
61          throws Throwable {
62  
63          final String methodName = method.getName();
64  
65          if (methodName.equals("getColumnCount")) {
66              return Integer.valueOf(this.columnNames.length);
67  
68          }
69          if (
70                  methodName.equals("getColumnName")) {
71  
72                  final int col = ((Integer) args[0]).intValue() - 1;
73                  return this.columnNames[col];
74  
75          }
76          if (
77                  methodName.equals("getColumnLabel")) {
78  
79                  final int col = ((Integer) args[0]).intValue() - 1;
80                  return this.columnLabels[col];
81  
82          }
83          if (methodName.equals("hashCode")) {
84              return Integer.valueOf(System.identityHashCode(proxy));
85  
86          }
87          if (methodName.equals("toString")) {
88              return "MockResultSetMetaData " + System.identityHashCode(proxy);
89  
90          }
91          if (methodName.equals("equals")) {
92              return Boolean.valueOf(proxy == args[0]);
93  
94          }
95          throw new UnsupportedOperationException("Unsupported method: " + methodName);
96      }
97  }