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
|
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/ServerCursorTest.java,v 1.5 2008/01/08 06:56:31 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.test.jdbc2;
import java.sql.*;
import junit.framework.TestCase;
import org.postgresql.test.TestUtil;
/*
* Tests for using non-zero setFetchSize().
*/
public class ServerCursorTest extends TestCase
{
private Connection con;
public ServerCursorTest(String name)
{
super(name);
}
protected void setUp() throws Exception
{
con = TestUtil.openDB();
TestUtil.createTable(con, "test_fetch", "value integer,data bytea");
con.setAutoCommit(false);
}
protected void tearDown() throws Exception
{
con.rollback();
con.setAutoCommit(true);
TestUtil.dropTable(con, "test_fetch");
TestUtil.closeDB(con);
}
protected void createRows(int count) throws Exception
{
PreparedStatement stmt = con.prepareStatement("insert into test_fetch(value,data) values(?,?)");
for (int i = 0; i < count; ++i)
{
stmt.setInt(1, i + 1);
stmt.setBytes(2, DATA_STRING.getBytes("UTF8"));
stmt.executeUpdate();
}
con.commit();
}
//Test regular cursor fetching
public void testBasicFetch() throws Exception
{
createRows(1);
PreparedStatement stmt = con.prepareStatement("declare test_cursor cursor for select * from test_fetch");
stmt.execute();
stmt = con.prepareStatement("fetch forward from test_cursor");
ResultSet rs = stmt.executeQuery();
while (rs.next())
{
//there should only be one row returned
assertEquals("query value error", 1, rs.getInt(1));
byte[] dataBytes = rs.getBytes(2);
assertEquals("binary data got munged", DATA_STRING, new String(dataBytes, "UTF8"));
}
}
//Test binary cursor fetching
public void testBinaryFetch() throws Exception
{
createRows(1);
PreparedStatement stmt = con.prepareStatement("declare test_cursor binary cursor for select * from test_fetch");
stmt.execute();
stmt = con.prepareStatement("fetch forward from test_cursor");
ResultSet rs = stmt.executeQuery();
while (rs.next())
{
//there should only be one row returned
byte[] dataBytes = rs.getBytes(2);
assertEquals("binary data got munged", DATA_STRING, new String(dataBytes, "UTF8"));
}
}
//This string contains a variety different data:
// three japanese characters representing "japanese" in japanese
// the four characters "\000"
// a null character
// the seven ascii characters "english"
private static final String DATA_STRING = "\u65E5\u672C\u8A9E\\000\u0000english";
}
|