1   /*
2    * PRELYTIS.
3    * Copyright 2007, PRELYTIS S.A., and individual contributors
4    * as indicated by the @author tags. See the copyright.txt file in the
5    * distribution for a full listing of individual contributors.
6    *
7    * This is free software; you can redistribute it and/or modify it
8    * under the terms of the GNU Lesser General Public License as
9    * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */
22  
23  package org.jdbc4olap.jdbc;
24  
25  import junit.framework.TestCase;
26  
27  import java.sql.ResultSetMetaData;
28  import java.sql.SQLException;
29  import java.sql.Types;
30  
31  /**
32   * @author Dan Rollo
33   * Date: Jul 9, 2008
34   * Time: 12:29:56 PM
35   */
36  public class OlapResultSetMetaDataTest extends TestCase {
37  
38      private static final int[] EMPTY_INT_ARRAY = new int[]{};
39  
40  
41      public void testAddMetaData() throws Exception {
42          final OlapResultSet rs = new OlapResultSet();
43  
44          assertMD(rs, null, null);
45          try {
46              assertMD(rs, OlapDatabaseMetaDataTest.EMPTY_STRING_ARRAY, null);
47              fail("Bad args should fail");
48          } catch (IllegalStateException e) {
49              assertEquals("Array mismatch: columnNames.length: 0, columnTypes is null", e.getMessage());
50          }
51          try {
52              assertMD(rs, null, EMPTY_INT_ARRAY);
53              fail("Bad args should fail");
54          } catch (IllegalStateException e) {
55              assertEquals("Array mismatch: columnNames is null, columnTypes.length: 0", e.getMessage());
56          }
57  
58          assertMD(rs, new String[] {"uno"}, new int[] {Types.VARCHAR});
59  
60          assertMD(rs, new String[] {"uno", "DOS"}, new int[] {Types.VARCHAR , Types.INTEGER});
61  
62          assertMD(rs, new String[] {"uno", "DOS", "tres"},  new int[] {Types.VARCHAR , Types.INTEGER, Types.BOOLEAN});
63      }
64  
65      private static void assertMD(final OlapResultSet rs, final String[] colNames, final int[] colTypes) throws SQLException {
66          OlapResultSetMetaData.setMetaData(rs, colNames, colTypes);
67          assertResultSetMD(rs, colNames, colTypes);
68      }
69  
70      private static void assertResultSetMD(final OlapResultSet rs, final String[] colNames, final int[] colTypes) throws SQLException {
71          final ResultSetMetaData md = rs.getMetaData();
72  
73          if (colNames == null) {
74              assertEquals(0, md.getColumnCount());
75              return;
76          }
77  
78          assertEquals(colNames.length, md.getColumnCount());
79  
80          for (int i = 0; i < colNames.length; i++) {
81              assertEquals(colNames[i], md.getColumnName(i + 1));
82              assertEquals(colNames[i], md.getColumnLabel(i + 1));
83          }
84  
85          for (int i = 0; i < colTypes.length; i++) {
86  
87              final int actualColType = md.getColumnType(i + 1);
88              assertEquals(colTypes[i], actualColType);
89  
90              final String msgMissingTypeMap = "Column Type with unknown mapping: " + actualColType + ", Should we add it?";
91              assertEquals(msgMissingTypeMap, OlapResultSetMetaData.TYPE_NAME_MAP.get(actualColType), md.getColumnTypeName(i + 1));
92              assertTrue(msgMissingTypeMap, OlapResultSetMetaData.TYPE_NAME_MAP.containsKey(actualColType));
93  
94              assertEquals(md.getColumnLabel(i + 1).length() + OlapResultSetMetaData.COL_DISPLAY_SIZE_PAD,
95                      md.getColumnDisplaySize(i + 1));
96  
97              // @todo Check this
98              md.getCatalogName(i + 1);
99              md.getColumnClassName(i + 1);
100 
101             md.getPrecision(i + 1);
102             md.getScale(i + 1);
103             md.getSchemaName(i + 1);
104             md.getTableName(i + 1);
105         }
106     }
107 
108     public void testAddMetaDataStringCols() throws Exception {
109         final OlapResultSet rs = new OlapResultSet();
110 
111         assertMDStringCols(rs, null);
112         assertMDStringCols(rs, OlapDatabaseMetaDataTest.EMPTY_STRING_ARRAY);
113         assertMDStringCols(rs, new String[] {"uno"});
114         assertMDStringCols(rs, new String[] {"uno", "DOS"});
115         assertMDStringCols(rs, new String[] {"uno", "DOS", "tres"});
116     }
117 
118     private static void assertMDStringCols(final OlapResultSet rs, final String[] colNames) throws SQLException {
119         OlapResultSetMetaData.setMetaDataStringCols(rs, colNames);
120         assertResultSetMD(rs, colNames, OlapResultSetMetaData.createStringColTypesForNames(colNames));
121     }
122 
123     public void testCreate() throws Exception {
124         final OlapResultSetMetaData md = new OlapResultSetMetaData(null);
125         assertEquals(0, md.getColumnCount());
126     }
127 
128     public void testGetColumnLabel() throws Exception {
129         final OlapResultSet rs = new OlapResultSet();
130         OlapResultSetMetaData.setMetaData(rs, new String[] {"uno", "DOS"}, new int[] {Types.VARCHAR, Types.VARCHAR},
131                 new String[] {"unoLabel", null});
132         final OlapResultSetMetaData md = (OlapResultSetMetaData) rs.getMetaData();
133         assertEquals("uno", md.getColumnName(1));
134         assertEquals("unoLabel", md.getColumnLabel(1));
135         assertEquals("DOS", md.getColumnName(2));
136         assertEquals("DOS", md.getColumnLabel(2));
137     }
138 }