/*
 * Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made
 * available under the terms of the MIT License. See the LICENSE file in the project root for more information.
 */
package com.microsoft.sqlserver.jdbc.connection;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.microsoft.sqlserver.testframework.AbstractTest;

/*
 * This test is for validating that client process ID gets registered with the server when available to the driver.
 */
@RunWith(JUnitPlatform.class)
public class ClientProcessIdTest extends AbstractTest {

    private static int pid = 0;

    static {
        long pidLong = 0;
        try {
            pidLong = ProcessHandle.current().pid();
        } catch (NoClassDefFoundError e) { // ProcessHandle is Java 9+
        }
        pid = (pidLong > Integer.MAX_VALUE) ? 0 : (int)pidLong;
    }

    @Test
    public void testClientProcessId() throws SQLException {
        SQLServerDataSource ds = new SQLServerDataSource();
        ds.setURL(connectionString);
        String sqlSelect = "select host_process_id from sys.dm_exec_sessions where session_id = @@SPID";

        try (Connection con = ds.getConnection(); Statement stmt = con.createStatement()) {
            try (ResultSet rs = stmt.executeQuery(sqlSelect)) {
                if (rs.next()) {
                    assertEquals(rs.getInt(1), pid);
                } else {
                    assertTrue(false, "Expected row of data was not found.");
                }
            }
        }
    }
}
@@ -523,24 +523,59 @@
 atement;%0D%0A%0D%0A
+import org.junit.jupiter.api.Tag;%0D%0A
 import org.j
@@ -594,24 +594,24 @@
 .api.Test;%0D%0A
-
 import org.j
@@ -801,16 +801,75 @@
 tTest;%0D%0A
+import com.microsoft.sqlserver.testframework.Constants;%0D%0A%0D%0A
 %0D%0A/*%0D%0A *
@@ -1019,16 +1019,45 @@
 class)%0D%0A
+@Tag(Constants.xAzureSQLDW)%0D%0A
 public c
@@ -1390,16 +1390,17 @@
  : (int)
+ 
 pidLong;
@@ -2092,9 +2092,11 @@
 %0A    %7D%0D%0A
-
 %7D
+%0D%0A

Exclude Azure DW for client PID test (#1603)