001: /*-------------------------------------------------------------------------
002: *
003: * Copyright (c) 2004-2005, PostgreSQL Global Development Group
004: *
005: * IDENTIFICATION
006: * $PostgreSQL: pgjdbc/org/postgresql/test/jdbc3/TypesTest.java,v 1.11 2007/07/21 15:58:17 jurka Exp $
007: *
008: *-------------------------------------------------------------------------
009: */
010: package org.postgresql.test.jdbc3;
011:
012: import java.sql.*;
013: import junit.framework.TestCase;
014: import org.postgresql.test.TestUtil;
015:
016: public class TypesTest extends TestCase {
017:
018: private Connection _conn;
019:
020: public TypesTest(String name) {
021: super (name);
022: try {
023: Class.forName("org.postgresql.Driver");
024: } catch (Exception ex) {
025:
026: }
027: }
028:
029: protected void setUp() throws Exception {
030: _conn = TestUtil.openDB();
031: Statement stmt = _conn.createStatement();
032: stmt
033: .execute("CREATE OR REPLACE FUNCTION return_bool(boolean) RETURNS boolean AS 'BEGIN RETURN $1; END;' LANGUAGE 'plpgsql'");
034: stmt.close();
035: }
036:
037: protected void tearDown() throws SQLException {
038: Statement stmt = _conn.createStatement();
039: stmt.execute("DROP FUNCTION return_bool(boolean)");
040: stmt.close();
041: TestUtil.closeDB(_conn);
042: }
043:
044: public void testPreparedBoolean() throws SQLException {
045: PreparedStatement pstmt = _conn
046: .prepareStatement("SELECT ?,?,?,?");
047: pstmt.setNull(1, Types.BOOLEAN);
048: pstmt.setObject(2, null, Types.BOOLEAN);
049: pstmt.setBoolean(3, true);
050: pstmt.setObject(4, Boolean.FALSE);
051: ResultSet rs = pstmt.executeQuery();
052: assertTrue(rs.next());
053: assertTrue(!rs.getBoolean(1));
054: assertTrue(rs.wasNull());
055: assertNull(rs.getObject(2));
056: assertTrue(rs.getBoolean(3));
057: // Only the V3 protocol return will be strongly typed.
058: // The V2 path will return a String because it doesn't know
059: // any better.
060: if (TestUtil.isProtocolVersion(_conn, 3)) {
061: assertTrue(!((Boolean) rs.getObject(4)).booleanValue());
062: }
063: }
064:
065: public void testPreparedByte() throws SQLException {
066: PreparedStatement pstmt = _conn.prepareStatement("SELECT ?,?");
067: pstmt.setByte(1, (byte) 1);
068: pstmt.setObject(2, new Byte((byte) 2));
069: ResultSet rs = pstmt.executeQuery();
070: assertTrue(rs.next());
071: assertEquals((byte) 1, rs.getByte(1));
072: assertFalse(rs.wasNull());
073: assertEquals((byte) 2, rs.getByte(2));
074: assertFalse(rs.wasNull());
075: rs.close();
076: pstmt.close();
077: }
078:
079: public void testCallableBoolean() throws SQLException {
080: CallableStatement cs = _conn
081: .prepareCall("{? = call return_bool(?)}");
082: cs.registerOutParameter(1, Types.BOOLEAN);
083: cs.setBoolean(2, true);
084: cs.execute();
085: assertEquals(true, cs.getBoolean(1));
086: cs.close();
087: }
088:
089: public void testUnknownType() throws SQLException {
090: Statement stmt = _conn.createStatement();
091:
092: ResultSet rs = stmt
093: .executeQuery("select 'foo1' as icon1, 'foo2' as icon2 ");
094: assertTrue(rs.next());
095: assertTrue("failed returned [" + rs.getString("icon1") + "]",
096: rs.getString("icon1").equalsIgnoreCase("foo1"));
097: assertTrue("failed returned [" + rs.getString("icon2") + "]",
098: rs.getString("icon2").equalsIgnoreCase("foo2"));
099: }
100:
101: }
|