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,7 +41,7 @@ public class ActiveDirectoryFactoryTests extends ElasticsearchTestCase {
buildAdSettings(AD_LDAP_URL, AD_DOMAIN));
String userName = "ironman";
LdapConnection ldap = connectionFactory.bind(userName, SecuredStringTests.build(PASSWORD));
try (LdapConnection ldap = connectionFactory.bind(userName, SecuredStringTests.build(PASSWORD))) {
String userDN = ldap.getAuthenticatedUserDn();
List<String> groups = ldap.getGroupsFromUserAttrs(userDN);
@ -53,6 +53,7 @@ public class ActiveDirectoryFactoryTests extends ElasticsearchTestCase {
containsString("Avengers"),
containsString("SHIELD")));
}
}
@Test
public void testAdAuth_avengers() {
@ -61,9 +62,9 @@ public class ActiveDirectoryFactoryTests extends ElasticsearchTestCase {
String[] users = new String[]{"cap", "hawkeye", "hulk", "ironman", "thor", "blackwidow", };
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")));
ldap.close();
}
}
}
@ -74,7 +75,7 @@ public class ActiveDirectoryFactoryTests extends ElasticsearchTestCase {
"CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com"));
String userName = "hulk";
LdapConnection ldap = connectionFactory.bind(userName, SecuredStringTests.build(PASSWORD));
try (LdapConnection ldap = connectionFactory.bind(userName, SecuredStringTests.build(PASSWORD))) {
String userDN = ldap.getAuthenticatedUserDn();
List<String> groups = ldap.getGroupsFromUserAttrs(userDN);
@ -85,6 +86,7 @@ public class ActiveDirectoryFactoryTests extends ElasticsearchTestCase {
containsString("Geniuses"),
containsString("Philanthropists")));
}
}
@Test
public void testAD_standardLdapConnection(){
@ -95,8 +97,7 @@ public class ActiveDirectoryFactoryTests extends ElasticsearchTestCase {
LdapTest.buildLdapSettings(AD_LDAP_URL, userTemplate, groupSearchBase, isSubTreeSearch));
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());
@ -111,6 +112,7 @@ public class ActiveDirectoryFactoryTests extends ElasticsearchTestCase {
containsString("Geniuses"),
containsString("Philanthropists")));
}
}
public static Settings buildAdSettings(String ldapUrl, String adDomainName) {
return ImmutableSettings.builder()