diff --git a/core/src/test/java/org/acegisecurity/providers/jaas/JaasAuthenticationProviderTests.java b/core/src/test/java/org/acegisecurity/providers/jaas/JaasAuthenticationProviderTests.java index e00c0d4eb1..ad87510621 100644 --- a/core/src/test/java/org/acegisecurity/providers/jaas/JaasAuthenticationProviderTests.java +++ b/core/src/test/java/org/acegisecurity/providers/jaas/JaasAuthenticationProviderTests.java @@ -6,7 +6,8 @@ import net.sf.acegisecurity.AuthenticationException; import net.sf.acegisecurity.GrantedAuthority; import net.sf.acegisecurity.GrantedAuthorityImpl; import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken; -import org.springframework.context.support.FileSystemXmlApplicationContext; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.Arrays; import java.util.List; @@ -21,10 +22,13 @@ import java.util.List; public class JaasAuthenticationProviderTests extends TestCase { private JaasAuthenticationProvider jaasProvider; + private ApplicationContext context; + private JaasEventCheck eventCheck; protected void setUp() throws Exception { String resName = "/" + getClass().getName().replace('.', '/') + ".xml"; - FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(getClass().getResource(resName).toString()); + context = new ClassPathXmlApplicationContext(resName); + eventCheck = (JaasEventCheck) context.getBean("eventCheck"); jaasProvider = (JaasAuthenticationProvider) context.getBean("jaasAuthenticationProvider"); } @@ -40,8 +44,15 @@ public class JaasAuthenticationProviderTests extends TestCase { UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user", "password", defaultAuths); + assertTrue(jaasProvider.supports(UsernamePasswordAuthenticationToken.class)); + Authentication auth = jaasProvider.authenticate(token); + assertNotNull(jaasProvider.getAuthorityGranters()); + assertNotNull(jaasProvider.getCallbackHandlers()); + assertNotNull(jaasProvider.getLoginConfig()); + assertNotNull(jaasProvider.getLoginContextName()); + List list = Arrays.asList(auth.getAuthorities()); assertTrue("GrantedAuthorities does not contain ROLE_TEST", @@ -50,6 +61,22 @@ public class JaasAuthenticationProviderTests extends TestCase { assertTrue("GrantedAuthorities does not contain ROLE_1", list.contains(role1)); assertTrue("GrantedAuthorities does not contain ROLE_2", list.contains(role2)); + + boolean foundit = false; + for (int i = 0; i < list.size(); i++) { + Object obj = list.get(i); + if (obj instanceof JaasGrantedAuthority) { + JaasGrantedAuthority grant = (JaasGrantedAuthority) obj; + assertNotNull("Principal was null on JaasGrantedAuthority", grant.getPrincipal()); + foundit = true; + } + } + assertTrue("Could not find a JaasGrantedAuthority", foundit); + + assertNotNull("Success event not fired", eventCheck.successEvent); + assertEquals("Auth objects are not equal", auth, eventCheck.successEvent.getAuthentication()); + + assertNull("Failure event was fired", eventCheck.failedEvent); } public void testBadUser() { @@ -58,6 +85,10 @@ public class JaasAuthenticationProviderTests extends TestCase { fail("LoginException should have been thrown for the bad user"); } catch (AuthenticationException e) { } + + assertNotNull("Failure event not fired", eventCheck.failedEvent); + assertNotNull("Failure event exception was null", eventCheck.failedEvent.getException()); + assertNull("Success event was fired", eventCheck.successEvent); } public void testBadPassword() { @@ -66,6 +97,10 @@ public class JaasAuthenticationProviderTests extends TestCase { fail("LoginException should have been thrown for the bad password"); } catch (AuthenticationException e) { } + + assertNotNull("Failure event not fired", eventCheck.failedEvent); + assertNotNull("Failure event exception was null", eventCheck.failedEvent.getException()); + assertNull("Success event was fired", eventCheck.successEvent); } } diff --git a/core/src/test/java/org/acegisecurity/providers/jaas/JaasAuthenticationProviderTests.xml b/core/src/test/java/org/acegisecurity/providers/jaas/JaasAuthenticationProviderTests.xml index e9d37156cb..d92876b877 100644 --- a/core/src/test/java/org/acegisecurity/providers/jaas/JaasAuthenticationProviderTests.xml +++ b/core/src/test/java/org/acegisecurity/providers/jaas/JaasAuthenticationProviderTests.xml @@ -2,6 +2,9 @@ + + + JAASTest diff --git a/core/src/test/java/org/acegisecurity/providers/jaas/TestAuthorityGranter.java b/core/src/test/java/org/acegisecurity/providers/jaas/TestAuthorityGranter.java index 860ad61604..774d563dce 100644 --- a/core/src/test/java/org/acegisecurity/providers/jaas/TestAuthorityGranter.java +++ b/core/src/test/java/org/acegisecurity/providers/jaas/TestAuthorityGranter.java @@ -5,14 +5,15 @@ import java.security.Principal; /** * Insert comments here... *
- * + * * @author Ray Krueger * @version $Id$ */ public class TestAuthorityGranter implements AuthorityGranter { public String grant(Principal principal) { + String role = null; if (principal.getName().equals("TEST_PRINCIPAL")) - return "ROLE_TEST"; - return null; + role = "ROLE_TEST"; + return role; } } diff --git a/core/src/test/java/org/acegisecurity/providers/jaas/TestCallbackHandler.java b/core/src/test/java/org/acegisecurity/providers/jaas/TestCallbackHandler.java index 15cc82a798..1cb400426d 100644 --- a/core/src/test/java/org/acegisecurity/providers/jaas/TestCallbackHandler.java +++ b/core/src/test/java/org/acegisecurity/providers/jaas/TestCallbackHandler.java @@ -23,12 +23,9 @@ public class TestCallbackHandler implements JaasAuthenticationCallbackHandler { } public void handle(Callback callback) throws IOException, UnsupportedCallbackException { - - if (auth == null) throw new RuntimeException("TEST FAILURE: setAuthentication was never called"); - if (callback instanceof TextInputCallback) { TextInputCallback tic = (TextInputCallback) callback; - tic.setText(getClass().getName()); + tic.setText(auth.getPrincipal().toString()); } } } diff --git a/core/src/test/java/org/acegisecurity/providers/jaas/TestLoginModule.java b/core/src/test/java/org/acegisecurity/providers/jaas/TestLoginModule.java index dfdcdbbc30..8dc412b5ea 100644 --- a/core/src/test/java/org/acegisecurity/providers/jaas/TestLoginModule.java +++ b/core/src/test/java/org/acegisecurity/providers/jaas/TestLoginModule.java @@ -43,6 +43,12 @@ public class TestLoginModule implements LoginModule { return "TEST_PRINCIPAL"; } }); + + subject.getPrincipals().add(new Principal() { + public String getName() { + return "NULL_PRINCIPAL"; + } + }); return true; } @@ -63,11 +69,7 @@ public class TestLoginModule implements LoginModule { password = new String(passwordCallback.getPassword()); user = nameCallback.getName(); - if (!TestCallbackHandler.class.getName().equals(textCallback.getText())) - throw new RuntimeException("TEST FAILURE: " + textCallback.getText() + "!=" + TestCallbackHandler.class.getName()); - } catch (Exception e) { - e.printStackTrace(); throw new RuntimeException(e); } }