001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.server;
007:
008: import java.sql.Connection;
009: import java.sql.DatabaseMetaData;
010: import java.sql.DriverManager;
011: import java.sql.ParameterMetaData;
012: import java.sql.PreparedStatement;
013: import java.sql.ResultSet;
014: import java.sql.SQLException;
015: import java.sql.Statement;
016:
017: import org.h2.test.TestBase;
018: import org.h2.tools.Server;
019:
020: /**
021: * Tests the PostgreSQL server protocol compliant implementation.
022: */
023: public class TestPgServer extends TestBase {
024:
025: public void test() throws Exception {
026: deleteDb("test");
027: Server server = Server.createPgServer(new String[] {
028: "-baseDir", baseDir, "-ifExists", "false",
029: "-pgAllowOthers", "false", "-pgPort", "5535" });
030: server.start();
031: try {
032: Class.forName("org.postgresql.Driver");
033: testPgClient();
034: } catch (ClassNotFoundException e) {
035: println("PostgreSQL JDBC driver not found - PgServer not tested");
036: } finally {
037: server.stop();
038: }
039: }
040:
041: private void testPgClient() throws Exception {
042: Connection conn = DriverManager.getConnection(
043: "jdbc:postgresql://localhost:5535/test", "sa", "sa");
044: Statement stat = conn.createStatement();
045: try {
046: stat.execute("select ***");
047: error();
048: } catch (SQLException e) {
049: checkNotGeneralException(e);
050: }
051: conn.close();
052: conn = DriverManager.getConnection(
053: "jdbc:postgresql://localhost:5535/test", "sa", "sa");
054: stat = conn.createStatement();
055: stat
056: .execute("create table test(id int primary key, name varchar)");
057: PreparedStatement prep = conn
058: .prepareStatement("insert into test values(?, ?)");
059: ParameterMetaData meta = prep.getParameterMetaData();
060: check(2, meta.getParameterCount());
061: prep.setInt(1, 1);
062: prep.setString(2, "Hello");
063: prep.execute();
064: ResultSet rs = stat.executeQuery("select * from test");
065: rs.next();
066: prep.close();
067: check(rs.getInt(1), 1);
068: check(rs.getString(2), "Hello");
069: checkFalse(rs.next());
070: prep = conn
071: .prepareStatement("select * from test where id = ? and name = ?");
072: prep.setInt(1, 1);
073: prep.setString(2, "Hello");
074: rs = prep.executeQuery();
075: rs.next();
076: check(rs.getInt(1), 1);
077: check(rs.getString(2), "Hello");
078: checkFalse(rs.next());
079: rs.close();
080: DatabaseMetaData dbMeta = conn.getMetaData();
081: rs = dbMeta.getTables(null, null, "TEST", null);
082: rs.next();
083: check(rs.getString("TABLE_NAME"), "TEST");
084: checkFalse(rs.next());
085: rs = dbMeta.getColumns(null, null, "TEST", null);
086: rs.next();
087: check(rs.getString("COLUMN_NAME"), "ID");
088: rs.next();
089: check(rs.getString("COLUMN_NAME"), "NAME");
090: checkFalse(rs.next());
091: rs = dbMeta.getIndexInfo(null, null, "TEST", false, false);
092: rs.next();
093: check(rs.getString("TABLE_NAME"), "TEST");
094: rs.next();
095: check(rs.getString("TABLE_NAME"), "TEST");
096: checkFalse(rs.next());
097: rs = stat
098: .executeQuery("select version(), pg_postmaster_start_time(), current_schema()");
099: rs.next();
100: String s = rs.getString(1);
101: check(s.indexOf("H2") >= 0);
102: check(s.indexOf("PostgreSQL") >= 0);
103: s = rs.getString(2);
104: s = rs.getString(3);
105: check(s, "PUBLIC");
106: checkFalse(rs.next());
107:
108: conn.setAutoCommit(false);
109: stat.execute("delete from test");
110: conn.rollback();
111: stat.execute("update test set name = 'Hallo'");
112: conn.commit();
113: rs = stat.executeQuery("select * from test order by id");
114: rs.next();
115: check(1, rs.getInt(1));
116: check("Hallo", rs.getString(2));
117: checkFalse(rs.next());
118:
119: rs = stat
120: .executeQuery("select id, name, pg_get_userbyid(id) from information_schema.users order by id");
121: rs.next();
122: check(rs.getString(2), rs.getString(3));
123: checkFalse(rs.next());
124:
125: conn.close();
126: }
127:
128: }
|