@Test
public void testGetServerPrincipal() throws IOException {

  String service = "hdfs/";
  String realm = "@REALM";
  String hostname = "foohost";
  String userPrincipal = "foo@FOOREALM";
  String shouldReplace = service + SecurityUtil.HOSTNAME_PATTERN + realm;
  String replaced = service + hostname + realm;
  verify(shouldReplace, hostname, replaced);
  String shouldNotReplace = service + SecurityUtil.HOSTNAME_PATTERN + "NAME"
      + realm;
  verify(shouldNotReplace, hostname, shouldNotReplace);
  verify(userPrincipal, hostname, userPrincipal);
  // testing reverse DNS lookup doesn't happen
  InetAddress notUsed = Mockito.mock(InetAddress.class);
  assertEquals(shouldNotReplace,
               SecurityUtil.getServerPrincipal(shouldNotReplace, notUsed));
  Mockito.verify(notUsed, Mockito.never()).getCanonicalHostName();
}