### Question:
SecurityUtil { public static String getServerPrincipal(String principalConfig, String hostname) throws IOException { String[] components = getComponents(principalConfig); if (components == null || components.length != 3 || !components[1].equals(HOSTNAME_PATTERN)) { return principalConfig; } else { return replacePattern(components, hostname); } }  static void fetchServiceTicket(URL remoteHost); static String getServerPrincipal(String principalConfig,
      String hostname); static String getServerPrincipal(String principalConfig,
      InetAddress addr); static void login(final Configuration conf,
      final String keytabFileKey, final String userNameKey); static void login(final Configuration conf,
      final String keytabFileKey, final String userNameKey, String hostname); static String buildDTServiceName(URI uri, int defPort); static String getHostFromPrincipal(String principalName); @InterfaceAudience.Private static void setSecurityInfoProviders(SecurityInfo... providers); static KerberosInfo getKerberosInfo(Class<?> protocol, Configuration conf); static TokenInfo getTokenInfo(Class<?> protocol, Configuration conf); static final Log LOG; static final String HOSTNAME_PATTERN; }### Answer:
@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); InetAddress notUsed = Mockito.mock(InetAddress.class); assertEquals(shouldNotReplace, SecurityUtil.getServerPrincipal(shouldNotReplace, notUsed)); Mockito.verify(notUsed, Mockito.never()).getCanonicalHostName(); }