summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/libcore/java/sql/OldResultSetTest.java
blob: a1654cf164e89218c90cddb485092c38d13ccaa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package libcore.java.sql;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import tests.support.DatabaseCreator;

public final class OldResultSetTest extends OldSQLTest {

    ResultSet target = null;
    ResultSet emptyTarget = null;
    ResultSet scrollableTarget = null;
    ResultSet writableTarget = null;
    Statement stForward = null;
    Statement stScrollable = null;
    Statement stWritable = null;
    final String selectAllAnimals = "select id, name from zoo";
    final String selectEmptyTable = "select * from "+DatabaseCreator.SIMPLE_TABLE1;

    @Override public void setUp() throws Exception {
        super.setUp();

        conn.setAutoCommit(false);
        stForward = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
                ResultSet.CONCUR_READ_ONLY);
        stForward.execute(selectAllAnimals);
        target = stForward.getResultSet();
        assertNotNull(target);

        // empty table
        stForward = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
                ResultSet.CONCUR_READ_ONLY);
        stForward.execute(DatabaseCreator.CREATE_TABLE_SIMPLE1);
        stForward.execute(selectEmptyTable);
        emptyTarget = stForward.getResultSet();
    }

    public void tearDown() throws SQLException {
        super.tearDown();
        target.close();
        stForward.close();
    }

    public void testAbsolute() throws SQLException {
        assertTrue(target.isBeforeFirst());
        assertFalse(target.absolute(0));
        assertTrue(target.absolute(1));
        assertTrue(target.isFirst());
        assertTrue(target.absolute(-1));
        assertTrue(target.isLast());
        target.next();
        assertTrue(target.isAfterLast());
    }

    // res.close() does not wrap up
    public void testAfterLast() throws SQLException {
        target.afterLast();
        assertTrue(target.isAfterLast());
        assertFalse(target.next());

        emptyTarget.afterLast();
        assertFalse(emptyTarget.isAfterLast());

        try {
            target.close();
            target.afterLast();
            fail("Should get SQLException");
        } catch (SQLException e) {
        }
    }

    // statement.close() does not wrap up
    public void testBeforeFirst() throws SQLException {
        target.beforeFirst();
        assertTrue(target.isBeforeFirst());
        assertTrue(target.next());
        assertFalse(target.isBeforeFirst());

        emptyTarget.beforeFirst();
        assertFalse(emptyTarget.isBeforeFirst());

        try {
            target.close();
            target.beforeFirst();
            fail("Should get SQLException");
        } catch (SQLException e) {
        }
    }

    /**
     * According to the JDBC spec close has to "Releases this ResultSet
     * object's database and JDBC resources immediately", and this implies
     * the fields should be released as well (so that garbage collection
     *  can take place)
     */
    public void testClose1() {
        try {
            target.close();
            target.next();
            fail("Should get SQLException");
        } catch (SQLException e) {
            //ok
        }
    }

    /**
     * Test that exception in one prepared statement does not affect second
     * statement. (Atomicity Rule)
     */
    public void testClose() throws SQLException {
        PreparedStatement ps1 = null;
        PreparedStatement ps2 = null;
        try {
            Statement s = conn.createStatement();
            s.addBatch("create table t1 (a text);");

            s.addBatch("insert into t1 values('abc');");
            s.addBatch("insert into t1 values('def');");
            s.addBatch("insert into t1 values('ghi');");
            s.executeBatch();
            s.close();

            conn.commit();
            ps1 = conn.prepareStatement("select * from t1");
            ps2 = conn.prepareStatement("select * from t1 whe a like '?000'");

            ResultSet rs1 = ps1.executeQuery();
            try {
                ResultSet rs2 = ps2.executeQuery();
                while (rs2.next()){
                    // do nothing
                }
                fail("Should get SQLException");
            } catch (SQLException sqle) {
                // ok : Division by zero
            }

            // Although exception happened on ps2 rs1 should still work
            // Isolation property if ACID rules

            while (rs1.next()) {
                // do nothing: switching of rows should be possible
            }

            conn.commit();

            rs1.close();
            ps1.close();
            ps2.close();
        } finally {
            try {
                if (ps1 != null) ps1.close();
                if (ps2 != null) ps2.close();
                conn.rollback();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public void testFindColumn() throws SQLException {
        assertEquals(1, target.findColumn("id"));
        assertEquals(2, target.findColumn("name"));

        try {
            target.findColumn("bla");
            fail("Should get SQLException");
        } catch (SQLException e) {
            // ok
        }
    }

    // statement.close() does not wrap up
    public void testtestFirst() throws SQLException {
        assertFalse(emptyTarget.first());
        assertTrue(target.first());

        try {
            target.close();
            // releases all resources such that it can be finalized!
            target.first();
            fail("Should get SQLException");
        } catch (SQLException e) {
        }
    }

    // statement.close() does not wrap up
    public void testtestIsAfterLast() throws SQLException {
        assertFalse(target.isAfterLast());
        target.absolute(-1); // last
        target.next();
        assertTrue(target.isAfterLast());
        assertFalse(emptyTarget.isAfterLast());

        try {
            target.close();
            // releases all resources such that it can be finalized!
            target.isAfterLast();
            fail("Should get SQLException");
        } catch (SQLException e) {
        }
    }

    // In Second code block assertion fails. statement.close() does not wrap up
    public void testtestIsBeforeFirst() throws SQLException {
        assertTrue(target.isBeforeFirst());
        assertTrue(target.next());
        assertFalse(target.isBeforeFirst());
        assertTrue(target.isFirst());
        assertTrue(emptyTarget.isBeforeFirst());

        try {
            target.close();
            // releases all resources such that it can be finalized!
            target.isBeforeFirst();
            fail("Should get SQLException");
        } catch (SQLException e) {
            //ok
        }
    }

    // statement.close() does not wrap up
    public void testtestIsFirst() throws SQLException {
        assertFalse(target.isFirst());
        target.first();
        assertTrue(target.isFirst());
        target.next();
        assertFalse(target.isFirst());

        assertFalse(emptyTarget.isFirst());

        try {
            target.close();
            // releases all resources such that it can be finalized!
            target.isFirst();
            fail("Should get SQLException");
        } catch (SQLException e) {
        }
    }

    /**
     * Second block first assertion fails. Is Last should evaluate true if the
     * row on which the cursor is actually provides a result.statment.close()
     * does not wrap up
     */
    public void testtestIsLast() throws SQLException {
        assertFalse(target.isLast());
        target.absolute(-1);
        assertTrue(target.isLast());

        //check default value no valid row
        assertFalse(emptyTarget.isLast());
        assertFalse(emptyTarget.next());
        assertFalse(emptyTarget.isLast());

        try {
            target.close();
            target.isLast();
            fail("Should get SQLException");
        } catch (SQLException e) {
            // ok
        }
    }

    // statement.close() does not wrap up
    public void testtestLast() throws SQLException {
        assertFalse(target.isLast());
        target.last();
        assertTrue(target.isLast());

        try {
            target.close();
            target.last();
            fail("Should get SQLException");
        } catch (SQLException e) {
            // ok
        }
    }

    /**
     * SQLException checking test fails. Clearing of warnings and closed streams
     * not supported.
     */
    public void testNext() throws SQLException {
        //before first - first
        assertTrue(target.next());
        //first - second
        assertTrue(target.next());
        //after last
        assertFalse(target.next());
        assertTrue(target.isAfterLast());
        // one more
        assertFalse(target.next());

        assertFalse(emptyTarget.next());

        target.close();
        try {
            target.next();
            fail("Exception expected");
        } catch (SQLException e) {
            //ok
        }
    }

    public void testPrevious() throws SQLException {
        target.first();
        target.previous();
        assertTrue(target.isBeforeFirst());

        target.last();
        target.next();
        target.previous();
        assertFalse(target.isAfterLast());

        target.close();
        try {
            target.previous();
            fail("Exception expected");
        } catch (SQLException e) {
            //ok
        }
    }

    // no exception is thrown when moving cursor backwards on forward only statement
    public void testRelative() throws SQLException {

        // forward only
        int initialRow = target.getRow();
        assertFalse(target.relative(0));
        assertEquals(initialRow, target.getRow());

        assertTrue(target.relative(1));
        assertTrue(target.isFirst());
        assertEquals(1, target.getRow());

        assertTrue(target.relative(1));
        assertFalse(target.isFirst());
        assertEquals(2, target.getRow());
        assertFalse(target.relative(2));

        try {
            // should not be able to scroll backwards in forward only RS
            target.relative(-2);
            assertEquals(2,target.getRow());
            fail("Should get SQLException");
        } catch (SQLException e) {
            // ok
        } catch (Exception e) {
            fail("Unexpected exception: " + e.getMessage());
        }

        assertFalse(emptyTarget.relative(Integer.MAX_VALUE));
        assertTrue(emptyTarget.isAfterLast());
    }

    // Scrollable resultSet. Not supported
    public void testRelativeScrollableResultSet() throws SQLException {
     // scrollable resultSet
        int initialRow = scrollableTarget.getRow();
        assertFalse(scrollableTarget.relative(0));
        assertEquals(initialRow, scrollableTarget.getRow());

        assertTrue(scrollableTarget.relative(1));
        assertTrue(scrollableTarget.isFirst());
        assertEquals(1, scrollableTarget.getRow());

        assertTrue(scrollableTarget.relative(1));
        assertFalse(scrollableTarget.isFirst());

        assertEquals(2, scrollableTarget.getRow());
        assertFalse(scrollableTarget.relative(2));
        scrollableTarget.relative(-2);
        assertEquals(2,scrollableTarget.getRow());

        assertFalse(scrollableTarget.relative(Integer.MIN_VALUE));
        assertTrue(scrollableTarget.isBeforeFirst());

        stScrollable.close();
        try {
            scrollableTarget.relative(1);
            fail("Exception expected");
        } catch (SQLException e) {
            //ok
        }
    }

    // not supported
    public void testUpdateObjectStringObject() throws SQLException {
        writableTarget.next();
        writableTarget.updateObject("family","bird");

        try {
           target.next();
           target.updateObject("family","bird");
           fail("SQLException was not thrown");
        } catch (SQLException e) {
           fail("Unexpected exception: " + e.getMessage());
        }
    }

    /**
     * Only exception testing. Missing testing for wrong type
     */
    public void testUpdateStringStringString() throws Exception {
        writableTarget.next();
        writableTarget.updateString("family","bird");

         // non writable target.
         try {
            target.next();
            target.updateString("family","bird");
            fail("SQLException was not thrown");
         } catch (SQLException e) {
            //ok
         }


         // writable but wrong type
        target.updateString(1,"test");

        target.close();

        // Exception test
        try {
            target.updateString("family", "test");
            fail("Exception expected");
        } catch (SQLException e) {
            //ok
        }
    }

    /**
     * Test method for {@link java.sql.ResultSet#wasNull()}.
     * Spec sais: if something was read... -> if nothing was read it should be false
     */
    public void testWasNull() throws SQLException {
        // Check default: select statement executed but no get on target called yet
        // Either false or throw an exception.
        try {
            assertFalse(target.wasNull());
        } catch (SQLException e) {
            //ok
        }

        stForward.execute("insert into zoo values(8,null,null);");
        stForward.execute(selectAllAnimals);
        target = stForward.getResultSet();
        assertNotNull(target);
        assertTrue(target.last());
        assertNull(target.getObject(2));
        assertTrue(target.wasNull());
        assertNotNull(target.getObject(1));
        assertFalse(target.wasNull());

        target.close();
        try {
            target.wasNull();
            fail("Exception expected");
        } catch (SQLException e) {
            //ok
        }
    }
}