DS-3762: Added test for Context.close() in try-with-resources block

This commit is contained in:
Tom Desair
2017-12-06 00:47:50 +01:00
parent f4c07c947d
commit 2d537abd7e

View File

@@ -270,6 +270,45 @@ public class ContextTest extends AbstractUnitTest
cleanupContext(instance);
cleanupContext(newInstance);
}
/**
* Test of close method, of class Context.
*/
@Test
public void testClose() throws SQLException, AuthorizeException
{
new NonStrictExpectations(authorizeService.getClass())
{{
// Allow Admin permissions - needed to create a new EPerson
authorizeService.isAdmin((Context) any); result = true;
}};
String createdEmail = "susie@email.com";
// To test close() we need a new Context object in a try-with-resources block
try(Context instance = new Context()) {
// Create a new EPerson (DO NOT COMMIT IT)
EPerson newUser = ePersonService.create(instance);
newUser.setFirstName(context, "Susan");
newUser.setLastName(context, "Doe");
newUser.setEmail(createdEmail);
newUser.setCanLogIn(true);
newUser.setLanguage(context, I18nUtil.getDefaultLocale().getLanguage());
}
// Open a new context, let's make sure that EPerson isn't there
Context newInstance = new Context();
EPerson found = ePersonService.findByEmail(newInstance, createdEmail);
assertThat("testClose 0", found, nullValue());
// Cleanup our contexts
cleanupContext(newInstance);
//Calling close on a finished context should not result in errors
newInstance.close();
}
/**
* Test of abort method, of class Context.