@Test
public void testEnsureKeytabFileContainsNoDuplicates() throws Exception {

  KerberosOperationHandler handler = createHandler();
  File file = folder.newFile();
  final String principal1 = "principal1@REALM.COM";
  final String principal2 = "principal2@REALM.COM";
  Set<String> seenEntries = new HashSet<String>();
  Assert.assertTrue(handler.createKeytabFile(principal1, "some password", 0, file));
  Assert.assertTrue(handler.createKeytabFile(principal2, "some password", 0, file));
  // Attempt to add duplicate entries
  Assert.assertTrue(handler.createKeytabFile(principal2, "some password", 0, file));
  Keytab keytab = Keytab.read(file);
  Assert.assertNotNull(keytab);
  List<KeytabEntry> entries = keytab.getEntries();
  Assert.assertNotNull(entries);
  Assert.assertFalse(entries.isEmpty());
  for (KeytabEntry entry : entries) {
    String seenEntry = String.format("%s|%s", entry.getPrincipalName(), entry.getKey().getKeyType().toString());
    Assert.assertFalse(seenEntries.contains(seenEntry));
    seenEntries.add(seenEntry);
  }
}