• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package libcore.java.sql;
18 
19 import java.sql.ResultSet;
20 import java.sql.ResultSetMetaData;
21 import java.sql.SQLException;
22 import java.sql.Statement;
23 import java.sql.Types;
24 
25 public final class OldResultSetMetaDataTest extends OldSQLTest {
26 
27     ResultSetMetaData rsmd = null;
28     Statement st = null;
29     ResultSet rs = null;
30 
31     @Override
setUp()32     public void setUp() throws Exception {
33         super.setUp();
34         try {
35             conn.setAutoCommit(false);
36             assertFalse(conn.getAutoCommit());
37             String query = "select * from zoo";
38             st = conn.createStatement();
39             st.execute(query);
40             rs = st.getResultSet();
41             rsmd = rs.getMetaData();
42         } catch (SQLException e) {
43             fail("Couldn't get ResultSetMetaData object");
44         }
45     }
46 
47     @Override
tearDown()48     public void tearDown() throws SQLException {
49         try {
50             rs.close();
51             st.close();
52         } catch (SQLException e) {
53             fail("Couldn't close Statement object");
54         }
55         super.tearDown();
56     }
57 
58     // not supported
testGetCatalogName()59     public void testGetCatalogName() throws SQLException {
60         try {
61             assertNotNull(rsmd.getCatalogName(1));
62         } catch (SQLException e) {
63             fail("SQLException is thrown: " + e.getMessage());
64         }
65 
66         try {
67             conn.close();
68             rsmd.getCatalogName(0);
69             fail("Exception expected");
70         } catch (SQLException e) {
71             //ok
72         }
73     }
74 
testGetColumnClassName()75     public void testGetColumnClassName() {
76         try {
77             assertNotNull(rsmd);
78             assertEquals(Short.class.getName(), rsmd.getColumnClassName(1));
79             assertEquals(String.class.getName(), rsmd.getColumnClassName(2));
80             assertEquals(String.class.getName(), rsmd.getColumnClassName(3));
81         } catch (SQLException e) {
82             fail("SQLException is thrown: " + e.getMessage());
83         }
84 
85         try {
86             String name  = rsmd.getColumnClassName(0);
87             assertNull(name);
88         } catch (SQLException e) {
89             fail("SQLException is thrown");
90         }
91 
92         try {
93             String name  = rsmd.getColumnClassName(4);
94             assertNull(name);
95         } catch (SQLException e) {
96             fail("SQLException is thrown");
97         }
98     }
99 
100     // SQLException checking test fails
testGetColumnCount()101     public void testGetColumnCount() {
102         try {
103             assertEquals(3, rsmd.getColumnCount());
104         } catch (SQLException e) {
105             fail("SQLException is thrown: " + e.getMessage());
106         }
107 
108         try {
109             rs.close();
110             rsmd.getColumnCount();
111             fail("Exception expected");
112         } catch (SQLException e) {
113             //ok
114         }
115 
116     }
117 
118     // Column label has format TABLE.COLUMN expected: COLUMN
testGetColumnLabel()119     public void testGetColumnLabel() {
120         String[] labels = { "id", "name", "family" };
121         try {
122             for (int i = 0; i < rsmd.getColumnCount(); i++) {
123                 String label = rsmd.getColumnLabel(i + 1);
124                 assertTrue("expected "+labels[i] + "got "+label,labels[i].contains(label));
125             }
126         } catch (SQLException e) {
127             fail("SQLException is thrown: " + e.getMessage());
128         }
129 
130         try {
131             String label = rsmd.getColumnLabel(0);
132             fail("SQLException expected");
133         } catch (SQLException e) {
134             //ok
135         }
136 
137         try {
138             String label = rsmd.getColumnLabel(5);
139             fail("SQLException expected");
140         } catch (SQLException e) {
141             //ok
142         }
143     }
144 
145     // Column label has format TABLE.COLUMN expected: COLUMN
testGetColumnName()146     public void testGetColumnName() {
147         String[] labels = { "id", "name", "family" };
148         try {
149             for (int i = 0; i < rsmd.getColumnCount(); i++) {
150                 String name = rsmd.getColumnName(i + 1);
151                 assertEquals(labels[i], name);
152             }
153         } catch (SQLException e) {
154             fail("SQLException is thrown: " + e.getMessage());
155         }
156 
157         try {
158             String label = rsmd.getColumnName(0);
159             fail("SQLException is not thrown");
160         } catch (SQLException e) {
161             //ok
162         }
163 
164         try {
165             String label = rsmd.getColumnName(5);
166             fail("SQLException is not thrown");
167         } catch (SQLException e) {
168             //ok
169         }
170     }
171 
172     /**
173      * For extensive tests see: ResultSetGetterTest.testGetMetaData
174      */
testGetColumnType()175     public void testGetColumnType() {
176         int[] types = { Types.SMALLINT, Types.VARCHAR, Types.VARCHAR};
177         try {
178             for (int i = 0; i < rsmd.getColumnCount(); i++) {
179                 int type = rsmd.getColumnType(i + 1);
180                 assertEquals(types[i], type);
181             }
182         } catch (SQLException e) {
183             fail("SQLException is thrown: " + e.getMessage());
184         }
185 
186         try {
187             rsmd.getColumnType(0);
188             fail("SQLException is not thrown");
189         } catch (SQLException e) {
190             // expected
191         }
192         try {
193             rsmd.getColumnType(5);
194             fail("SQLException is not thrown");
195         } catch (SQLException e) {
196             // expected
197         }
198     }
199 
200     /**
201      * for extensive tests see: ResultSetGetterTest.testGetMetaData
202      */
testGetColumnTypeName()203     public void testGetColumnTypeName() {
204         try {
205             assertTrue("smallint".equalsIgnoreCase(rsmd.getColumnTypeName(1)));
206             assertTrue("varchar".equalsIgnoreCase(rsmd.getColumnTypeName(2)));
207             assertTrue("varchar".equalsIgnoreCase(rsmd.getColumnTypeName(3)));
208         } catch (SQLException e) {
209             fail("SQLException is thrown: " + e.getMessage());
210         }
211 
212         try {
213             rsmd.getColumnTypeName(0);
214             fail("SQLException is not thrown");
215         } catch (SQLException e) {
216             // expected
217         }
218         try {
219             rsmd.getColumnTypeName(5);
220             fail("SQLException is not thrown");
221         } catch (SQLException e) {
222             // expected
223         }
224     }
225 
226     // For int = 0, exception expected
testGetTableName()227     public void testGetTableName() throws SQLException {
228         try {
229             assertEquals("zoo", rsmd.getTableName(1));
230         } catch (SQLException e) {
231             fail("SQLException is thrown: " + e.getMessage());
232         }
233         Statement st1 = null;
234         ResultSet rs1 = null;
235         try {
236 
237             String create = "create table hutch (id integer not null, animal_id integer, address char(20), primary key (id));";
238             String insert1 = "insert into hutch (id, animal_id, address) values (1, 2, 'Birds-house, 1');";
239             String insert2 = "insert into hutch (id, animal_id, address) values (2, 1, 'Horse-house, 5');";
240             String query = "select name, animal_id from hutch, zoo where zoo.id = 1" ;
241             st1 = conn.createStatement();
242             st1.executeUpdate(create);
243             st1.executeUpdate(insert1);
244             st1.executeUpdate(insert2);
245 
246             rs1 = st1.executeQuery(query);
247             assertNotNull(rs1);
248             ResultSetMetaData rsmd1 = rs1.getMetaData();
249             assertEquals("zoo", rsmd1.getTableName(1));
250             assertEquals("hutch", rsmd1.getTableName(2));
251         } catch (SQLException e) {
252             fail("SQLException is thrown: " + e.getMessage());
253         } finally {
254             try {
255                 if (rs1 != null) rs1.close();
256                 if (st1 != null) {
257                     st1.executeUpdate("drop table if exists hutch");
258                     st1.close();
259                 }
260             } catch (SQLException sqle) {
261             }
262         }
263         //Exception Text
264         try {
265             String name = rsmd.getTableName(0);
266             fail("SQLException Expected");
267         } catch (SQLException e) {
268             // ok
269         }
270     }
271 
272     // not supported
testGetPrecision()273     public void testGetPrecision() throws SQLException {
274         Statement st2 = null;
275         Statement st3 = null;
276         ResultSetMetaData rsmd2 = null;
277         try {
278         int precisionNum = 10;
279         int scale = 3;
280         int precicisionReal = 10;
281         String createTable = "create table DecimalNumbers ( valueDouble DOUBLE,"+
282         "valueFloat FLOAT , scaleTest NUMERIC("+precisionNum+","+scale+"),"+
283         " valueReal REAL("+precicisionReal+")  );";
284         String insert = "insert into DecimalNumbers values (1.5, 20.55, 30.666, 100000);";
285         String select = "select * from DecimalNumbers;";
286         st2 = conn.createStatement();
287         st2.executeUpdate(createTable);
288         st2.executeUpdate(insert);
289 
290         st2.close();
291 
292         st3 = conn.createStatement();
293         rs = st3.executeQuery(select);
294         assertTrue(rs.next());
295         rsmd2 = rs.getMetaData();
296 
297         assertNotNull(rsmd2);
298         assertEquals(precicisionReal, rsmd2.getPrecision(4));
299         assertEquals(precisionNum,rsmd2.getPrecision(3));
300         assertTrue(rsmd2.getPrecision(2) > 0);
301         assertTrue(rsmd2.getPrecision(1) > 0);
302 
303         // non numeric field
304         try {
305             rsmd.getPrecision(3);
306         } catch (SQLException e1) {
307             System.out.println("ResultSetMetaDataTest.testGetPrecision()"+e1.getMessage());
308             e1.printStackTrace();
309         }
310 
311 
312         try {
313             rsmd.getPrecision(0);
314             fail("SQLException is not thrown");
315         } catch (SQLException e) {
316             // expected
317         }
318         try {
319             rsmd.getPrecision(5);
320             fail("SQLException is not thrown");
321         } catch (SQLException e) {
322             // expected
323         }
324 
325         try {
326             rs.close();
327             rsmd.getPrecision(1);
328             fail("Exception expected");
329         } catch (SQLException e) {
330             //ok
331         }
332         } finally  {
333             if (st2 != null) st2.close();
334             if (st3 != null) st3.close();
335         }
336     }
337 
338     /**
339      * Always returns 0, exception tests fail no positive test case for
340      * black-box test possible: no default value indicated.
341      *
342      * Not supported
343      */
testGetScale()344     public void testGetScale() throws SQLException {
345         try {
346         int scale = 3;
347         String createTable = "create table DecimalNumbers ( valueDouble DOUBLE,"+
348         "valueFloat FLOAT , scaleTest NUMERIC(10,"+scale+")  );";
349         String insert = "insert into DecimalNumbers values (1.5, 20.55, 30.666);";
350         String select = "select * from DecimalNumbers;";
351 
352         Statement st = conn.createStatement();
353         st.executeUpdate(createTable);
354         st.executeUpdate(insert);
355 
356         rs = st.executeQuery(select);
357         ResultSetMetaData rsmd2 = rs.getMetaData();
358 
359         assertNotNull(rsmd2);
360         assertEquals(scale,rsmd2.getScale(3));
361         assertTrue(rsmd2.getScale(1) > 0);
362         assertTrue(rsmd2.getScale(2) > 0);
363 
364         try {
365             rsmd.getScale(0);
366             fail("SQLException is not thrown");
367         } catch (SQLException e) {
368             // expected
369         }
370         try {
371             rsmd.getScale(5);
372             fail("SQLException is not thrown");
373         } catch (SQLException e) {
374             // expected
375         }
376 
377 
378         try {
379             conn.close();
380             rsmd.getScale(1);
381             fail("Exception expected");
382         } catch (SQLException e) {
383             //ok
384         }
385         } finally  {
386             st.cancel();
387         }
388     }
389 
390     // not supported
testGetSchema()391     public void testGetSchema() {
392         try {
393             assertNull("Functionality is now supported. Change test",rsmd.getSchemaName(2));
394         } catch (SQLException e1) {
395             fail("ResultSetMetaDataTest.testGetScale()"+e1.getMessage());
396             e1.printStackTrace();
397         }
398 
399         try {
400             rsmd.getSchemaName(0);
401             fail("SQLException is not thrown");
402         } catch (SQLException e) {
403             // expected
404         }
405         try {
406             rsmd.getSchemaName(5);
407             fail("SQLException is not thrown");
408         } catch (SQLException e) {
409             // expected
410         }
411 
412         try {
413             conn.close();
414             rsmd.getSchemaName(2);
415             fail("Exception expected");
416         } catch (SQLException e) {
417             //ok
418         }
419 
420     }
421 
422     /**
423      * Tests fail: always returns false, failing statements commented out.
424      * Feature only partially implemented.Missing: Test positive case
425      *
426      * Not supported.
427      */
testisAutoIncrement()428     public void testisAutoIncrement() {
429         try {
430             assertFalse(rsmd.isAutoIncrement(1));
431         } catch (SQLException e1) {
432             fail("ResultSetMetaDataTest.testGetScale()"+e1.getMessage());
433             e1.printStackTrace();
434         }
435 
436         /*
437         // Exception testing
438 
439         try {
440             rsmd.isAutoIncrement(0);
441             fail("SQLException is not thrown");
442         } catch (SQLException e) {
443             // expected
444         }
445         try {
446             rsmd.isAutoIncrement(5);
447             fail("SQLException is not thrown");
448         } catch (SQLException e) {
449             // expected
450         }
451         */
452 
453         try {
454             conn.close();
455             rsmd.getSchemaName(2);
456             fail("Exception expected");
457         } catch (SQLException e) {
458             //ok
459         }
460 
461     }
462 
463     // not supported
testIsCaseSensitive()464     public void testIsCaseSensitive() {
465         try {
466             assertFalse(rsmd.isCaseSensitive(1));
467             assertFalse(rsmd.isCaseSensitive(2));
468             assertFalse(rsmd.isCaseSensitive(3));
469         } catch (SQLException e1) {
470             fail("ResultSetMetaDataTest.testGetScale()"+e1.getMessage());
471             e1.printStackTrace();
472         }
473 
474         /*
475         // Exception testing
476 
477         try {
478             rsmd.isCaseSensitive(0);
479             fail("SQLException is not thrown");
480         } catch (SQLException e) {
481             // expected
482         }
483         try {
484             rsmd.isCaseSensitive(5);
485             fail("SQLException is not thrown");
486         } catch (SQLException e) {
487             // expected
488         }
489         */
490 
491         try {
492             conn.close();
493             rsmd.isCaseSensitive(1);
494             fail("Exception expected");
495         } catch (SQLException e) {
496             //ok
497         }
498     }
499 
500     /**
501      * Tests fail: always returns false. Exceptions and tests non Numeric fields
502      * fail, failing statements commented out. Feature only partially
503      * implemented. May be an optional feature.
504      *
505      * Not supported.
506      */
testIsCurrency()507     public void testIsCurrency() {
508         try {
509             assertFalse(rsmd.isCurrency(1));
510         } catch (SQLException e1) {
511             fail("ResultSetMetaDataTest.testGetScale()"+e1.getMessage());
512             e1.printStackTrace();
513         }
514 
515         // Exception testing
516         try {
517             rsmd.isCurrency(0);
518             fail("SQLException is not thrown");
519         } catch (SQLException e) {
520             // expected
521         }
522         try {
523             rsmd.isCurrency(5);
524             fail("SQLException is not thrown");
525         } catch (SQLException e) {
526             // expected
527         }
528 
529         try {
530             rs.close();
531             rsmd.isCurrency(1);
532             fail("Exception expected");
533         } catch (SQLException e) {
534             //ok
535         }
536     }
537 
538     // not supported
testIsDefinitlyWritable()539     public void testIsDefinitlyWritable() {
540         try {
541             assertTrue(rsmd.isDefinitelyWritable(1));
542         } catch (SQLException e1) {
543             fail("ResultSetMetaDataTest.testisDefinitelyWritable()"
544                     + e1.getMessage());
545             e1.printStackTrace();
546         }
547 
548         // Exception testing
549 
550         try {
551             rsmd.isDefinitelyWritable(0);
552             fail("SQLException is not thrown");
553         } catch (SQLException e) {
554             // expected
555         }
556         try {
557             rsmd.isDefinitelyWritable(5);
558             fail("SQLException is not thrown");
559         } catch (SQLException e) {
560             // expected
561         }
562     }
563 
564     /**
565      * Tests fail: always returns ResultSetMetaData.columnNullableUnknown.
566      * Exceptions fail, failing statements commented out. Feature only
567      * partially implemented. May be an optional feature.
568      *
569      * Not supported.
570      */
testIsNullable()571     public void testIsNullable() {
572         try {
573             assertEquals(ResultSetMetaData.columnNullable, rsmd
574                     .isNullable(1));
575             assertEquals(ResultSetMetaData.columnNullable, rsmd
576                     .isNullable(2));
577             assertEquals(ResultSetMetaData.columnNullable, rsmd
578                     .isNullable(3));
579         } catch (SQLException e1) {
580             fail("ResultSetMetaDataTest.isNullable()" + e1.getMessage());
581             e1.printStackTrace();
582         }
583 
584         /*
585         // Exception testing
586 
587         try {
588             rsmd.isNullable(0);
589             fail("SQLException is not thrown");
590         } catch (SQLException e) {
591             // expected
592         }
593         try {
594             rsmd.isNullable(5);
595             fail("SQLException is not thrown");
596         } catch (SQLException e) {
597             // expected
598         }
599         */
600 
601     }
602 
603     /**
604      * Cannot know from blackbox test if readonly or writable. Exceptions fail,
605      * Feature only partially implemented.
606      */
testIsReadOnly()607     public void testIsReadOnly() {
608         try {
609             assertFalse(rsmd.isReadOnly(1));
610         } catch (SQLException e1) {
611             fail("ResultSetMetaDataTest.isReadOnly" + e1.getMessage());
612             e1.printStackTrace();
613         }
614 
615         // Exception testing
616 
617         try {
618             rsmd.isReadOnly(0);
619             fail("SQLException is not thrown");
620         } catch (SQLException e) {
621             // expected
622         }
623     }
624 
625     /**
626      * Tests fail: always returns false. Exceptions fail, Feature only partially
627      * implemented. Missing: test for searchable field.
628      */
testIsSearchable()629     public void testIsSearchable() {
630         try {
631             assertTrue(rsmd.isSearchable(1));
632             assertTrue(rsmd.isSearchable(2));
633             assertTrue(rsmd.isSearchable(3));
634         } catch (SQLException e1) {
635             fail("ResultSetMetaDataTest.isReadOnly" + e1.getMessage());
636             e1.printStackTrace();
637         }
638 
639         // Exception testing
640 
641         try {
642             rsmd.isSearchable(0);
643             fail("SQLException is not thrown");
644         } catch (SQLException e) {
645             // expected
646         }
647     }
648 
649     /**
650      * Tests fail: always returns false. Exceptions and tests on non numeric
651      * fields fail, Feature only partially implemented. Missing: test positive
652      * result
653      */
testIsSigned()654     public void testIsSigned() {
655         try {
656             assertFalse(rsmd.isSigned(1));
657         } catch (SQLException e1) {
658             fail("ResultSetMetaDataTest.isSigned" + e1.getMessage());
659             e1.printStackTrace();
660         }
661 
662         // Exception testing
663 
664         try {
665             rsmd.isSigned(0);
666             fail("SQLException is not thrown");
667         } catch (SQLException e) {
668             // expected
669         }
670     }
671 
672     /**
673      * Analogous to is Readonly.  Exceptions and tests on non numeric fields
674      * fail, Failing statements commented out. Feature only partially
675      * implemented.
676      */
testIsWritable()677     public void testIsWritable() {
678         try {
679             assertTrue(rsmd.isWritable(1));
680             assertTrue(rsmd.isWritable(2));
681             assertTrue(rsmd.isWritable(3));
682         } catch (SQLException e1) {
683             fail("ResultSetMetaDataTest.isWritable" + e1.getMessage());
684             e1.printStackTrace();
685         }
686 
687         // Exception testing
688 
689         try {
690             rsmd.isWritable(0);
691             fail("SQLException is not thrown");
692         } catch (SQLException e) {
693             // expected
694         }
695     }
696 
697 
698     /**
699      * Tests fail. always returns 0. Missing case where display size greater than 0
700      */
testGetColumnDisplaySize()701     public void testGetColumnDisplaySize() {
702         try {
703             for (int i = 0; i < rsmd.getColumnCount(); i++) {
704                 int size = rsmd.getColumnDisplaySize(i + 1);
705                 assertTrue(size > 0);
706             }
707         } catch (SQLException e) {
708             fail("SQLException is thrown: " + e.getMessage());
709         }
710 
711         // Exception testing
712 
713         try {
714             rsmd.getColumnDisplaySize(0);
715             fail("SQLException is not thrown");
716         } catch (SQLException e) {
717             // expected
718         }
719         try {
720             rsmd.getColumnDisplaySize(5);
721             fail("SQLException is not thrown");
722         } catch (SQLException e) {
723             // expected
724         }
725     }
726 
727 }
728