/*
 * Copyright (c) 2007, PostgreSQL Global Development Group
 * See the LICENSE file in the project root for more information.
 */

package org.postgresql.test.jdbc4;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.postgresql.core.BaseConnection;
import org.postgresql.core.TransactionState;
import org.postgresql.test.TestUtil;
import org.postgresql.test.jdbc2.BaseTest4;

import org.junit.Test;

import java.sql.Connection;
import java.sql.Statement;

public class IsValidTest extends BaseTest4 {

  private TransactionState getTransactionState(Connection conn) {
    return ((BaseConnection) conn).getTransactionState();
  }

  @Test
  public void testIsValid() throws Exception {
    try {
      assertTrue(con.isValid(0));
    } finally {
      TestUtil.closeDB(con);
    }
    assertFalse(con.isValid(0));
  }

  /**
   * Test that the transaction state is left unchanged.
   */
  @Test
  public void testTransactionState() throws Exception {
    TransactionState transactionState = getTransactionState(con);
    con.isValid(0);
    assertEquals("Transaction state has been changed", transactionState,
        getTransactionState(con));

    con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
    con.setAutoCommit(false);
    try {
      transactionState = getTransactionState(con);
      con.isValid(0);
      assertEquals("Transaction state has been changed", transactionState,
          getTransactionState(con));

      Statement stmt = con.createStatement();
      stmt.execute("SELECT 1");
      transactionState = getTransactionState(con);
      con.isValid(0);
      assertEquals("Transaction state has been changed", transactionState,
          getTransactionState(con));
    } finally {
      try {
        con.setAutoCommit(true);
      } catch (final Exception e) {
      }
    }
  }

}

@@ -334,24 +334,66 @@
 Connection;%0A
+import org.postgresql.core.ServerVersion;%0A
 import org.p
@@ -575,25 +575,81 @@
 ava.sql.
-Statement
+ResultSet;%0Aimport java.sql.Statement;%0Aimport java.util.Properties
 ;%0A%0Apubli
@@ -2010,10 +2010,882 @@
  %7D%0A  %7D%0A%0A
+  @Test%0A  public void testIsValidRemoteClose() throws Exception %7B%0A%0A    assumeMinimumServerVersion(%22Unable to use pg_terminate_backend before version 8.4%22, ServerVersion.v8_4);%0A%0A    Properties props = new Properties();%0A    updateProperties(props);%0A    Connection con2 = TestUtil.openPrivilegedDB();%0A%0A    try %7B%0A%0A      assertTrue(%22First Connection should be valid%22, con.isValid(0));%0A%0A      String pid;%0A      Statement s = con.createStatement();%0A%0A      try %7B%0A        s.execute(%22select pg_backend_pid()%22);%0A        ResultSet rs = s.getResultSet();%0A        rs.next();%0A        pid = rs.getString(1);%0A      %7D finally %7B%0A        TestUtil.closeQuietly(s);%0A      %7D%0A%0A      TestUtil.execute(%22select pg_terminate_backend(%22 + pid + %22)%22, con2);%0A%0A      assertFalse(%22The Second connection should now be invalid%22, con.isValid(0));%0A%0A    %7D finally %7B%0A      TestUtil.closeQuietly(con2);%0A    %7D%0A  %7D%0A
 %7D%0A

Check that JDBC Connections that are closed by the server do not report as valid (#1533)