private void testImproperTrustedDomainAuthentication(String url) throws Exception {
    // When trusted domain doesn't match requests domain, only the connection with correct user
    // and password goes through.
    Connection connection = DriverManager.getConnection(url, correctUser, correctPassword);
    connection.close();

    String partErrorMessage = "Peer indicated failure: Error validating the login";
    if (hs2TransportMode.equals(HS2_TRANSPORT_MODE_HTTP)) {
      partErrorMessage = "HTTP Response code: 401";
    }

    try (Connection conn = DriverManager.getConnection(url, wrongUser, correctPassword)) {
      Assert.fail("Expected Exception");
    } catch (SQLException e) {
      Assert.assertNotNull(e.getMessage());
      Assert.assertTrue(e.getMessage(), e.getMessage().contains(partErrorMessage));
    }

    try (Connection conn = DriverManager.getConnection(url, wrongUser, wrongPassword)) {
      Assert.fail("Expected Exception");
    } catch (SQLException e) {
      Assert.assertNotNull(e.getMessage());
      Assert.assertTrue(e.getMessage(), e.getMessage().contains(partErrorMessage));
    }

    try (Connection conn = DriverManager.getConnection(url, correctUser, wrongPassword)) {
      Assert.fail("Expected Exception");
    } catch (SQLException e) {
      Assert.assertNotNull(e.getMessage());
      Assert.assertTrue(e.getMessage(), e.getMessage().contains(partErrorMessage));
    }
  }
An abstract implementation of TrustDomainAuthentication Test.
itests/hive-unit/src/test/java/org/apache/hive/service/auth/TrustDomainAuthenticationTest.java