1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package test.rowset;
  24 
  25 import java.sql.ResultSetMetaData;
  26 import java.sql.SQLException;
  27 import java.sql.Types;
  28 import javax.sql.RowSetMetaData;
  29 import javax.sql.rowset.RowSetMetaDataImpl;
  30 import static org.testng.Assert.*;
  31 import org.testng.annotations.BeforeMethod;
  32 import org.testng.annotations.DataProvider;
  33 import org.testng.annotations.Test;
  34 import util.BaseTest;
  35 
  36 public class RowSetMetaDataTests extends BaseTest {
  37 
  38     // Max columns used in the tests
  39     private final int MAX_COLUMNS = 5;
  40     // Instance to be used within the tests
  41     private RowSetMetaDataImpl rsmd;
  42 
  43     @BeforeMethod
  44     public void setUpMethod() throws Exception {
  45         rsmd = new RowSetMetaDataImpl();
  46         rsmd.setColumnCount(MAX_COLUMNS);
  47     }
  48 
  49     /*
  50      * Validate a SQLException is thrown for an invalid column index
  51      */
  52     @Test(dataProvider = "invalidColumnRanges",
  53             expectedExceptions = SQLException.class)
  54     public void test(Integer col) throws Exception {
  55         rsmd.getCatalogName(col);
  56     }
  57 
  58     /*
  59      * Validate a SQLException is thrown for an invalid column index
  60      */
  61     @Test(dataProvider = "invalidColumnRanges",
  62             expectedExceptions = SQLException.class)
  63     public void test01(Integer col) throws Exception {
  64         rsmd.getColumnClassName(col);
  65     }
  66 
  67     /*
  68      * Validate a SQLException is thrown for an invalid column index
  69      */
  70     @Test(dataProvider = "invalidColumnRanges",
  71             expectedExceptions = SQLException.class)
  72     public void test02(Integer col) throws Exception {
  73         rsmd.getColumnDisplaySize(col);
  74     }
  75 
  76     /*
  77      * Validate a SQLException is thrown for an invalid column index
  78      */
  79     @Test(dataProvider = "invalidColumnRanges",
  80             expectedExceptions = SQLException.class)
  81     public void test03(Integer col) throws Exception {
  82         rsmd.getColumnLabel(col);
  83     }
  84 
  85     /*
  86      * Validate a SQLException is thrown for an invalid column index
  87      */
  88     @Test(dataProvider = "invalidColumnRanges",
  89             expectedExceptions = SQLException.class)
  90     public void test04(Integer col) throws Exception {
  91         rsmd.getColumnName(col);
  92     }
  93 
  94     /*
  95      * Validate a SQLException is thrown for an invalid column index
  96      */
  97     @Test(dataProvider = "invalidColumnRanges",
  98             expectedExceptions = SQLException.class)
  99     public void test05(Integer col) throws Exception {
 100         rsmd.getColumnType(col);
 101     }
 102 
 103     /*
 104      * Validate a SQLException is thrown for an invalid column index
 105      */
 106     @Test(dataProvider = "invalidColumnRanges",
 107             expectedExceptions = SQLException.class)
 108     public void test06(Integer col) throws Exception {
 109         rsmd.getColumnTypeName(col);
 110     }
 111 
 112     /*
 113      * Validate a SQLException is thrown for an invalid column index
 114      */
 115     @Test(dataProvider = "invalidColumnRanges",
 116             expectedExceptions = SQLException.class)
 117     public void test07(Integer col) throws Exception {
 118         rsmd.getPrecision(col);
 119     }
 120 
 121     /*
 122      * Validate a SQLException is thrown for an invalid column index
 123      */
 124     @Test(dataProvider = "invalidColumnRanges",
 125             expectedExceptions = SQLException.class)
 126     public void test08(Integer col) throws Exception {
 127         rsmd.getScale(col);
 128     }
 129 
 130     /*
 131      * Validate a SQLException is thrown for an invalid column index
 132      */
 133     @Test(dataProvider = "invalidColumnRanges",
 134             expectedExceptions = SQLException.class)
 135     public void test09(Integer col) throws Exception {
 136         rsmd.getSchemaName(col);
 137     }
 138 
 139     /*
 140      * Validate a SQLException is thrown for an invalid column index
 141      */
 142     @Test(dataProvider = "invalidColumnRanges",
 143             expectedExceptions = SQLException.class)
 144     public void test10(Integer col) throws Exception {
 145         rsmd.getTableName(col);
 146     }
 147 
 148     /*
 149      * Validate a SQLException is thrown for an invalid column index
 150      */
 151     @Test(dataProvider = "invalidColumnRanges",
 152             expectedExceptions = SQLException.class)
 153     public void test11(Integer col) throws Exception {
 154         rsmd.isAutoIncrement(col);
 155     }
 156 
 157     /*
 158      * Validate a SQLException is thrown for an invalid column index
 159      */
 160     @Test(dataProvider = "invalidColumnRanges",
 161             expectedExceptions = SQLException.class)
 162     public void test12(Integer col) throws Exception {
 163         rsmd.isCaseSensitive(col);
 164     }
 165 
 166     /*
 167      * Validate a SQLException is thrown for an invalid column index
 168      */
 169     @Test(dataProvider = "invalidColumnRanges",
 170             expectedExceptions = SQLException.class)
 171     public void test13(Integer col) throws Exception {
 172         rsmd.isCurrency(col);
 173     }
 174 
 175     /*
 176      * Validate a SQLException is thrown for an invalid column index
 177      */
 178     @Test(dataProvider = "invalidColumnRanges",
 179             expectedExceptions = SQLException.class)
 180     public void test14(Integer col) throws Exception {
 181         rsmd.isDefinitelyWritable(col);
 182     }
 183 
 184     /*
 185      * Validate a SQLException is thrown for an invalid column index
 186      */
 187     @Test(dataProvider = "invalidColumnRanges",
 188             expectedExceptions = SQLException.class)
 189     public void test15(Integer col) throws Exception {
 190         rsmd.isNullable(col);
 191     }
 192 
 193     /*
 194      * Validate a SQLException is thrown for an invalid column index
 195      */
 196     @Test(dataProvider = "invalidColumnRanges",
 197             expectedExceptions = SQLException.class)
 198     public void test16(Integer col) throws Exception {
 199         rsmd.isReadOnly(col);
 200     }
 201 
 202     /*
 203      * Validate a SQLException is thrown for an invalid column index
 204      */
 205     @Test(dataProvider = "invalidColumnRanges",
 206             expectedExceptions = SQLException.class)
 207     public void test17(Integer col) throws Exception {
 208         rsmd.isSearchable(col);
 209     }
 210 
 211     /*
 212      * Validate a SQLException is thrown for an invalid column index
 213      */
 214     @Test(dataProvider = "invalidColumnRanges",
 215             expectedExceptions = SQLException.class)
 216     public void test18(Integer col) throws Exception {
 217         rsmd.isSigned(col);
 218     }
 219 
 220     /*
 221      * Validate a SQLException is thrown for an invalid column index
 222      */
 223     @Test(dataProvider = "invalidColumnRanges",
 224             expectedExceptions = SQLException.class)
 225     public void test19(Integer col) throws Exception {
 226         rsmd.isWritable(col);
 227     }
 228 
 229     /*
 230      * Validate a SQLException is thrown for an invalid column index
 231      */
 232     @Test(dataProvider = "invalidColumnRanges",
 233             expectedExceptions = SQLException.class)
 234     public void test20(Integer col) throws Exception {
 235         rsmd.setAutoIncrement(col, true);
 236     }
 237 
 238     /*
 239      * Validate a SQLException is thrown for an invalid column index
 240      */
 241     @Test(dataProvider = "invalidColumnRanges",
 242             expectedExceptions = SQLException.class)
 243     public void test21(Integer col) throws Exception {
 244         rsmd.setCaseSensitive(col, true);
 245     }
 246 
 247     /*
 248      * Validate a SQLException is thrown for an invalid column index
 249      */
 250     @Test(dataProvider = "invalidColumnRanges",
 251             expectedExceptions = SQLException.class)
 252     public void test22(Integer col) throws Exception {
 253         rsmd.setCatalogName(col, null);
 254     }
 255 
 256     /*
 257      * Validate a SQLException is thrown for an invalid column index
 258      */
 259     @Test(dataProvider = "invalidColumnRanges",
 260             expectedExceptions = SQLException.class)
 261     public void test23(Integer col) throws Exception {
 262         rsmd.setColumnDisplaySize(col, 5);
 263     }
 264 
 265     /*
 266      * Validate a SQLException is thrown for an invalid column index
 267      */
 268     @Test(dataProvider = "invalidColumnRanges",
 269             expectedExceptions = SQLException.class)
 270     public void test24(Integer col) throws Exception {
 271         rsmd.setColumnLabel(col, "label");
 272     }
 273 
 274     /*
 275      * Validate a SQLException is thrown for an invalid column index
 276      */
 277     @Test(dataProvider = "invalidColumnRanges",
 278             expectedExceptions = SQLException.class)
 279     public void test25(Integer col) throws Exception {
 280         rsmd.setColumnName(col, "F1");
 281     }
 282 
 283     /*
 284      * Validate a SQLException is thrown for an invalid column index
 285      */
 286     @Test(dataProvider = "invalidColumnRanges",
 287             expectedExceptions = SQLException.class)
 288     public void test26(Integer col) throws Exception {
 289         rsmd.setColumnType(col, Types.CHAR);
 290     }
 291 
 292     /*
 293      * Validate a SQLException is thrown for an invalid column index
 294      */
 295     @Test(dataProvider = "invalidColumnRanges",
 296             expectedExceptions = SQLException.class)
 297     public void test27(Integer col) throws Exception {
 298         rsmd.setColumnTypeName(col, "F1");
 299     }
 300 
 301     /*
 302      * Validate a SQLException is thrown for an invalid column index
 303      */
 304     @Test(dataProvider = "invalidColumnRanges",
 305             expectedExceptions = SQLException.class)
 306     public void test28(Integer col) throws Exception {
 307         rsmd.setCurrency(col, true);
 308     }
 309 
 310     /*
 311      * Validate a SQLException is thrown for an invalid column index
 312      */
 313     @Test(dataProvider = "invalidColumnRanges",
 314             expectedExceptions = SQLException.class)
 315     public void test29(Integer col) throws Exception {
 316         rsmd.setNullable(col, ResultSetMetaData.columnNoNulls);
 317     }
 318 
 319     /*
 320      * Validate a SQLException is thrown for an invalid column index
 321      */
 322     @Test(dataProvider = "invalidColumnRanges",
 323             expectedExceptions = SQLException.class)
 324     public void test30(Integer col) throws Exception {
 325         rsmd.setPrecision(col, 2);
 326     }
 327 
 328     /*
 329      * Validate a SQLException is thrown for an invalid column index
 330      */
 331     @Test(dataProvider = "invalidColumnRanges",
 332             expectedExceptions = SQLException.class)
 333     public void test31(Integer col) throws Exception {
 334         rsmd.setScale(col, 2);
 335     }
 336 
 337     /*
 338      * Validate a SQLException is thrown for an invalid column index
 339      */
 340     @Test(dataProvider = "invalidColumnRanges",
 341             expectedExceptions = SQLException.class)
 342     public void test32(Integer col) throws Exception {
 343         rsmd.setSchemaName(col, "Gotham");
 344     }
 345 
 346     /*
 347      * Validate a SQLException is thrown for an invalid column index
 348      */
 349     @Test(dataProvider = "invalidColumnRanges",
 350             expectedExceptions = SQLException.class)
 351     public void test33(Integer col) throws Exception {
 352         rsmd.setSearchable(col, false);
 353     }
 354 
 355     /*
 356      * Validate a SQLException is thrown for an invalid column index
 357      */
 358     @Test(dataProvider = "invalidColumnRanges",
 359             expectedExceptions = SQLException.class)
 360     public void test34(Integer col) throws Exception {
 361         rsmd.setSigned(col, false);
 362     }
 363 
 364     /*
 365      * Validate a SQLException is thrown for an invalid column index
 366      */
 367     @Test(dataProvider = "invalidColumnRanges",
 368             expectedExceptions = SQLException.class)
 369     public void test35(Integer col) throws Exception {
 370         rsmd.setTableName(col, "SUPERHEROS");
 371     }
 372 
 373     /*
 374      * Validate that the correct class name is returned for the column
 375      * Note:  Once setColumnClassName is added to RowSetMetaData, this
 376      * method will need to change.
 377      */
 378     @Test(dataProvider = "columnClassNames")
 379     public void test36(Integer type, String name) throws Exception {
 380         rsmd.setColumnType(1, type);
 381         assertTrue(rsmd.getColumnClassName(1).equals(name));
 382     }
 383 
 384     /*
 385      * Validate that all of the methods are accessible and the correct value
 386      * is returned for each column
 387      */
 388     @Test(dataProvider = "columnRanges")
 389     public void test37(Integer col) throws Exception {
 390         rsmd.setAutoIncrement(col, true);
 391         assertTrue(rsmd.isAutoIncrement(col));
 392         rsmd.setCaseSensitive(col, true);
 393         assertTrue(rsmd.isCaseSensitive(col));
 394         rsmd.setCatalogName(col, "Gotham");
 395         assertTrue(rsmd.getCatalogName(col).equals("Gotham"));
 396         rsmd.setColumnDisplaySize(col, 20);
 397         assertTrue(rsmd.getColumnDisplaySize(col) == 20);
 398         rsmd.setColumnLabel(col, "F1");
 399         assertTrue(rsmd.getColumnLabel(col).equals("F1"));
 400         rsmd.setColumnName(col, "F1");
 401         assertTrue(rsmd.getColumnName(col).equals("F1"));
 402         rsmd.setColumnType(col, Types.INTEGER);
 403         assertTrue(rsmd.getColumnType(col) == Types.INTEGER);
 404         assertTrue(rsmd.getColumnClassName(col).equals(Integer.class.getName()));
 405         rsmd.setColumnTypeName(col, "INTEGER");
 406         assertTrue(rsmd.getColumnTypeName(col).equals("INTEGER"));
 407         rsmd.setCurrency(col, true);
 408         assertTrue(rsmd.isCurrency(col));
 409         rsmd.setNullable(col, ResultSetMetaData.columnNoNulls);
 410         assertTrue(rsmd.isNullable(col) == ResultSetMetaData.columnNoNulls);
 411         rsmd.setPrecision(col, 2);
 412         assertTrue(rsmd.getPrecision(col) == 2);
 413         rsmd.setScale(col, 2);
 414         assertTrue(rsmd.getScale(col) == 2);
 415         rsmd.setSchemaName(col, "GOTHAM");
 416         assertTrue(rsmd.getSchemaName(col).equals("GOTHAM"));
 417         rsmd.setSearchable(col, false);
 418         assertFalse(rsmd.isSearchable(col));
 419         rsmd.setSigned(col, false);
 420         assertFalse(rsmd.isSigned(col));
 421         rsmd.setTableName(col, "SUPERHEROS");
 422         assertTrue(rsmd.getTableName(col).equals("SUPERHEROS"));
 423         rsmd.isReadOnly(col);
 424         rsmd.isDefinitelyWritable(col);
 425         rsmd.isWritable(col);
 426 
 427     }
 428 
 429     /*
 430      * Validate that the proper values are accepted by setNullable
 431      */
 432     @Test(dataProvider = "validSetNullableValues")
 433     public void test38(Integer val) throws Exception {
 434         rsmd.setNullable(1, val);
 435     }
 436 
 437     /*
 438      * Validate that the correct type is returned for the column
 439      */
 440     @Test(dataProvider = "jdbcTypes")
 441     public void test39(Integer type) throws Exception {
 442         rsmd.setColumnType(1, type);
 443         assertTrue(type == rsmd.getColumnType(1));
 444     }
 445 
 446     /*
 447      * Validate that the correct value is returned from the isXXX methods
 448      */
 449     @Test(dataProvider = "trueFalse")
 450     public void test40(Boolean b) throws Exception {
 451         rsmd.setAutoIncrement(1, b);
 452         rsmd.setCaseSensitive(1, b);
 453         rsmd.setCurrency(1, b);
 454         rsmd.setSearchable(1, b);
 455         rsmd.setSigned(1, b);
 456         assertTrue(rsmd.isAutoIncrement(1) == b);
 457         assertTrue(rsmd.isCaseSensitive(1) == b);
 458         assertTrue(rsmd.isCurrency(1) == b);
 459         assertTrue(rsmd.isSearchable(1) == b);
 460         assertTrue(rsmd.isSigned(1) == b);
 461     }
 462 
 463     /*
 464      * Validate isWrapperFor and unwrap work correctly
 465      */
 466     @SuppressWarnings("unchecked")
 467     @Test
 468     public void test99() throws Exception {
 469         RowSetMetaData rsmd1 = rsmd;
 470         ResultSetMetaData rsmd2 = rsmd;
 471         Class clzz = rsmd.getClass();
 472         assertTrue(rsmd1.isWrapperFor(clzz));
 473         assertTrue(rsmd2.isWrapperFor(clzz));
 474         RowSetMetaDataImpl rsmdi = (RowSetMetaDataImpl) rsmd2.unwrap(clzz);
 475 
 476         // False should be returned
 477         assertFalse(rsmd1.isWrapperFor(this.getClass()));
 478         assertFalse(rsmd2.isWrapperFor(this.getClass()));
 479     }
 480 
 481     /*
 482      * DataProvider used to provide Date which are not valid and are used
 483      * to validate that an IllegalArgumentException will be thrown from the
 484      * valueOf method
 485      */
 486     @DataProvider(name = "validSetNullableValues")
 487     private Object[][] validSetNullableValues() {
 488         return new Object[][]{
 489             {ResultSetMetaData.columnNoNulls},
 490             {ResultSetMetaData.columnNullable},
 491             {ResultSetMetaData.columnNullableUnknown}
 492         };
 493     }
 494 
 495     /*
 496      * DataProvider used to provide column indexes that are out of range so that
 497      * SQLException is thrown
 498      */
 499     @DataProvider(name = "invalidColumnRanges")
 500     private Object[][] invalidColumnRanges() {
 501         return new Object[][]{
 502             {-1},
 503             {0},
 504             {MAX_COLUMNS + 1}
 505         };
 506     }
 507 
 508     /*
 509      * DataProvider used to provide the valid column ranges for the
 510      * RowSetMetaDataImpl object
 511      */
 512     @DataProvider(name = "columnRanges")
 513     private Object[][] columnRanges() {
 514         Object[][] o = new Object[MAX_COLUMNS][1];
 515         for (int i = 1; i <= MAX_COLUMNS; i++) {
 516             o[i - 1][0] = i;
 517         }
 518         return o;
 519     }
 520 
 521     /*
 522      * DataProvider used to specify the value to set via setColumnType and
 523      * the expected value to be returned from getColumnClassName
 524      */
 525     @DataProvider(name = "columnClassNames")
 526     private Object[][] columnClassNames() {
 527         return new Object[][]{
 528             {Types.CHAR, "java.lang.String"},
 529             {Types.NCHAR, "java.lang.String"},
 530             {Types.VARCHAR, "java.lang.String"},
 531             {Types.NVARCHAR, "java.lang.String"},
 532             {Types.LONGVARCHAR, "java.lang.String"},
 533             {Types.LONGNVARCHAR, "java.lang.String"},
 534             {Types.NUMERIC, "java.math.BigDecimal"},
 535             {Types.DECIMAL, "java.math.BigDecimal"},
 536             {Types.BIT, "java.lang.Boolean"},
 537             {Types.TINYINT, "java.lang.Byte"},
 538             {Types.SMALLINT, "java.lang.Short"},
 539             {Types.INTEGER, "java.lang.Integer"},
 540             {Types.FLOAT, "java.lang.Double"},
 541             {Types.DOUBLE, "java.lang.Double"},
 542             {Types.BINARY, "byte[]"},
 543             {Types.VARBINARY, "byte[]"},
 544             {Types.LONGVARBINARY, "byte[]"},
 545             {Types.DATE, "java.sql.Date"},
 546             {Types.TIME, "java.sql.Time"},
 547             {Types.TIMESTAMP, "java.sql.Timestamp"},
 548             {Types.CLOB, "java.sql.Clob"},
 549             {Types.BLOB, "java.sql.Blob"}
 550 
 551         };
 552 
 553     }
 554 
 555 }