~ubuntu-branches/ubuntu/oneiric/libpgjava/oneiric

« back to all changes in this revision

Viewing changes to src/interfaces/jdbc/org/postgresql/test/jdbc2/RefCursorTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2005-04-21 14:25:11 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050421142511-wibh5vc31fkrorx7
Tags: 7.4.7-3
Built with sources...

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.postgresql.test.jdbc2;
 
2
 
 
3
import org.postgresql.test.TestUtil;
 
4
import java.sql.CallableStatement;
 
5
import java.sql.Connection;
 
6
import java.sql.ResultSet;
 
7
import java.sql.Statement;
 
8
import java.sql.Types;
 
9
 
 
10
import junit.framework.TestCase;
 
11
 
 
12
/*
 
13
 * RefCursor ResultSet tests.
 
14
 * This test case is basically the same as the ResultSet test case.
 
15
 *
 
16
 * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
 
17
 */
 
18
public class RefCursorTest extends TestCase
 
19
{
 
20
        private Connection con;
 
21
 
 
22
        public RefCursorTest(String name)
 
23
        {
 
24
                super(name);
 
25
        }
 
26
 
 
27
        protected void setUp() throws Exception
 
28
        {
 
29
                // this is the same as the ResultSet setup.
 
30
                con = TestUtil.openDB();
 
31
                Statement stmt = con.createStatement();
 
32
 
 
33
                TestUtil.createTable(con, "testrs", "id integer");
 
34
 
 
35
                stmt.executeUpdate("INSERT INTO testrs VALUES (1)");
 
36
                stmt.executeUpdate("INSERT INTO testrs VALUES (2)");
 
37
                stmt.executeUpdate("INSERT INTO testrs VALUES (3)");
 
38
                stmt.executeUpdate("INSERT INTO testrs VALUES (4)");
 
39
                stmt.executeUpdate("INSERT INTO testrs VALUES (6)");
 
40
                stmt.executeUpdate("INSERT INTO testrs VALUES (9)");
 
41
 
 
42
 
 
43
                // Create the functions.
 
44
                stmt.execute ("CREATE OR REPLACE FUNCTION testspg__getRefcursor () RETURNS refcursor AS '"
 
45
                              + "declare v_resset; begin open v_resset for select id from testrs order by id; "
 
46
                              + "return v_resset; end;' LANGUAGE 'plpgsql';");
 
47
                stmt.execute ("CREATE OR REPLACE FUNCTION testspg__getEmptyRefcursor () RETURNS refcursor AS '"
 
48
                              + "declare v_resset; begin open v_resset for select id from testrs where id < 1 order by id; "
 
49
                              + "return v_resset; end;' LANGUAGE 'plpgsql';");
 
50
                stmt.close();
 
51
        }
 
52
 
 
53
        protected void tearDown() throws Exception
 
54
        {
 
55
                Statement stmt = con.createStatement ();
 
56
                stmt.execute ("drop FUNCTION testspg__getRefcursor ();");
 
57
                stmt.execute ("drop FUNCTION testspg__getEmptyRefcursor ();");
 
58
                TestUtil.dropTable(con, "testrs");
 
59
                TestUtil.closeDB(con);
 
60
        }
 
61
 
 
62
        public void testResult() throws Exception
 
63
        {
 
64
                CallableStatement call = con.prepareCall("{ ? = call testspg__getRefcursor () }");
 
65
                call.registerOutParameter(1, Types.OTHER);
 
66
                call.execute();
 
67
                ResultSet rs = (ResultSet) call.getObject(1);
 
68
 
 
69
                assertTrue(rs.next());
 
70
                assertTrue(rs.getInt(1) == 1);
 
71
 
 
72
                assertTrue(rs.next());
 
73
                assertTrue(rs.getInt(1) == 2);
 
74
 
 
75
                assertTrue(rs.next());
 
76
                assertTrue(rs.getInt(1) == 3);
 
77
 
 
78
                assertTrue(rs.next());
 
79
                assertTrue(rs.getInt(1) == 4);
 
80
 
 
81
                assertTrue(rs.next());
 
82
                assertTrue(rs.getInt(1) == 6);
 
83
 
 
84
                assertTrue(rs.next());
 
85
                assertTrue(rs.getInt(1) == 9);
 
86
 
 
87
                assertTrue(!rs.next());
 
88
                
 
89
                call.close();
 
90
        }
 
91
 
 
92
 
 
93
        public void testEmptyResult() throws Exception
 
94
        {
 
95
                CallableStatement call = con.prepareCall("{ ? = call testspg__getRefcursor () }");
 
96
                call.registerOutParameter(1, Types.OTHER);
 
97
                call.execute();
 
98
 
 
99
                ResultSet rs = (ResultSet) call.getObject(1);
 
100
                assertTrue(!rs.next());
 
101
 
 
102
                call.close();
 
103
        }
 
104
}