Tests: Use try block to close resources and prevent thread leaks

Original commit: elastic/x-pack-elasticsearch@3c8b4f6ead
This commit is contained in:
Alexander Reelsen 2014-11-06 18:31:12 +01:00
parent 2f4fb2cf48
commit eaf6636c07
1 changed files with 37 additions and 35 deletions

View File

@ -41,17 +41,18 @@ public class ActiveDirectoryFactoryTests extends ElasticsearchTestCase {
buildAdSettings(AD_LDAP_URL, AD_DOMAIN)); buildAdSettings(AD_LDAP_URL, AD_DOMAIN));
String userName = "ironman"; String userName = "ironman";
LdapConnection ldap = connectionFactory.bind(userName, SecuredStringTests.build(PASSWORD)); try (LdapConnection ldap = connectionFactory.bind(userName, SecuredStringTests.build(PASSWORD))) {
String userDN = ldap.getAuthenticatedUserDn(); String userDN = ldap.getAuthenticatedUserDn();
List<String> groups = ldap.getGroupsFromUserAttrs(userDN); List<String> groups = ldap.getGroupsFromUserAttrs(userDN);
assertThat(groups, containsInAnyOrder( assertThat(groups, containsInAnyOrder(
containsString("Geniuses"), containsString("Geniuses"),
containsString("Billionaire"), containsString("Billionaire"),
containsString("Playboy"), containsString("Playboy"),
containsString("Philanthropists"), containsString("Philanthropists"),
containsString("Avengers"), containsString("Avengers"),
containsString("SHIELD"))); containsString("SHIELD")));
}
} }
@Test @Test
@ -61,9 +62,9 @@ public class ActiveDirectoryFactoryTests extends ElasticsearchTestCase {
String[] users = new String[]{"cap", "hawkeye", "hulk", "ironman", "thor", "blackwidow", }; String[] users = new String[]{"cap", "hawkeye", "hulk", "ironman", "thor", "blackwidow", };
for(String user: users) { for(String user: users) {
LdapConnection ldap = connectionFactory.bind(user, SecuredStringTests.build(PASSWORD)); try (LdapConnection ldap = connectionFactory.bind(user, SecuredStringTests.build(PASSWORD))) {
assertThat("group avenger test for user "+user, ldap.getGroups(), hasItem(Matchers.containsString("Avengers"))); assertThat("group avenger test for user "+user, ldap.getGroups(), hasItem(Matchers.containsString("Avengers")));
ldap.close(); }
} }
} }
@ -74,16 +75,17 @@ public class ActiveDirectoryFactoryTests extends ElasticsearchTestCase {
"CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com")); "CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com"));
String userName = "hulk"; String userName = "hulk";
LdapConnection ldap = connectionFactory.bind(userName, SecuredStringTests.build(PASSWORD)); try (LdapConnection ldap = connectionFactory.bind(userName, SecuredStringTests.build(PASSWORD))) {
String userDN = ldap.getAuthenticatedUserDn(); String userDN = ldap.getAuthenticatedUserDn();
List<String> groups = ldap.getGroupsFromUserAttrs(userDN); List<String> groups = ldap.getGroupsFromUserAttrs(userDN);
assertThat(groups, containsInAnyOrder( assertThat(groups, containsInAnyOrder(
containsString("Avengers"), containsString("Avengers"),
containsString("SHIELD"), containsString("SHIELD"),
containsString("Geniuses"), containsString("Geniuses"),
containsString("Philanthropists"))); containsString("Philanthropists")));
}
} }
@Test @Test
@ -95,21 +97,21 @@ public class ActiveDirectoryFactoryTests extends ElasticsearchTestCase {
LdapTest.buildLdapSettings(AD_LDAP_URL, userTemplate, groupSearchBase, isSubTreeSearch)); LdapTest.buildLdapSettings(AD_LDAP_URL, userTemplate, groupSearchBase, isSubTreeSearch));
String user = "Bruce Banner"; String user = "Bruce Banner";
LdapConnection ldap = connectionFactory.bind(user, SecuredStringTests.build(PASSWORD)); try (LdapConnection ldap = connectionFactory.bind(user, SecuredStringTests.build(PASSWORD))) {
List<String> groups = ldap.getGroupsFromUserAttrs(ldap.getAuthenticatedUserDn());
List<String> groups2 = ldap.getGroupsFromSearch(ldap.getAuthenticatedUserDn());
List<String> groups = ldap.getGroupsFromUserAttrs(ldap.getAuthenticatedUserDn()); assertThat(groups, containsInAnyOrder(
List<String> groups2 = ldap.getGroupsFromSearch(ldap.getAuthenticatedUserDn()); containsString("Avengers"),
containsString("SHIELD"),
assertThat(groups, containsInAnyOrder( containsString("Geniuses"),
containsString("Avengers"), containsString("Philanthropists")));
containsString("SHIELD"), assertThat(groups2, containsInAnyOrder(
containsString("Geniuses"), containsString("Avengers"),
containsString("Philanthropists"))); containsString("SHIELD"),
assertThat(groups2, containsInAnyOrder( containsString("Geniuses"),
containsString("Avengers"), containsString("Philanthropists")));
containsString("SHIELD"), }
containsString("Geniuses"),
containsString("Philanthropists")));
} }
public static Settings buildAdSettings(String ldapUrl, String adDomainName) { public static Settings buildAdSettings(String ldapUrl, String adDomainName) {