Explorar o código

Improve test coverage and error detection at startup time.

Ben Alex %!s(int64=21) %!d(string=hai) anos
pai
achega
3a0e43337c

+ 4 - 4
core/src/main/java/org/acegisecurity/providers/jaas/JaasAuthenticationProvider.java

@@ -112,11 +112,11 @@ import javax.security.auth.login.LoginException;
  * </p>
  * 
  * <p>
-{ * {@link JaasAuthenticationCallbackHandler}s are passed to the
+ * {{@link JaasAuthenticationCallbackHandler}s are passed to the
  * JaasAuthenticationProvider through the {@link
  * #setCallbackHandlers(net.sf.acegisecurity.providers.jaas.JaasAuthenticationCallbackHandler[])
- * callbackHandlers} property.
-} * <pre>
+ * callbackHandlers} property. }
+ * <pre>
  *   &lt;property name="callbackHandlers"&gt;
  *       &lt;list&gt;
  *           &lt;bean class="net.sf.acegisecurity.providers.jaas.TestCallbackHandler"/&gt;
@@ -255,7 +255,7 @@ public class JaasAuthenticationProvider implements AuthenticationProvider,
                 + getClass());
         }
 
-        if (loginContextName == null) {
+        if ((loginContextName == null) || "".equals(loginContextName)) {
             throw new ApplicationContextException(
                 "loginContextName must be set on " + getClass());
         }

+ 48 - 0
core/src/test/java/org/acegisecurity/providers/jaas/JaasAuthenticationProviderTests.java

@@ -21,9 +21,11 @@ import net.sf.acegisecurity.Authentication;
 import net.sf.acegisecurity.AuthenticationException;
 import net.sf.acegisecurity.GrantedAuthority;
 import net.sf.acegisecurity.GrantedAuthorityImpl;
+import net.sf.acegisecurity.providers.TestingAuthenticationToken;
 import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
 
 import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextException;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 import java.util.Arrays;
@@ -71,6 +73,46 @@ public class JaasAuthenticationProviderTests extends TestCase {
         assertNull("Success event was fired", eventCheck.successEvent);
     }
 
+    public void testDetectsMissingLoginConfig() throws Exception {
+        JaasAuthenticationProvider myJaasProvider = new JaasAuthenticationProvider();
+        myJaasProvider.setApplicationContext(context);
+        myJaasProvider.setAuthorityGranters(jaasProvider.getAuthorityGranters());
+        myJaasProvider.setCallbackHandlers(jaasProvider.getCallbackHandlers());
+        myJaasProvider.setLoginContextName(jaasProvider.getLoginContextName());
+
+        try {
+            myJaasProvider.afterPropertiesSet();
+            fail("Should have thrown ApplicationContextException");
+        } catch (ApplicationContextException expected) {
+            assertTrue(expected.getMessage().startsWith("loginConfig must be set on"));
+        }
+    }
+
+    public void testDetectsMissingLoginContextName() throws Exception {
+        JaasAuthenticationProvider myJaasProvider = new JaasAuthenticationProvider();
+        myJaasProvider.setApplicationContext(context);
+        myJaasProvider.setAuthorityGranters(jaasProvider.getAuthorityGranters());
+        myJaasProvider.setCallbackHandlers(jaasProvider.getCallbackHandlers());
+        myJaasProvider.setLoginConfig(jaasProvider.getLoginConfig());
+        myJaasProvider.setLoginContextName(null);
+
+        try {
+            myJaasProvider.afterPropertiesSet();
+            fail("Should have thrown ApplicationContextException");
+        } catch (ApplicationContextException expected) {
+            assertTrue(expected.getMessage().startsWith("loginContextName must be set on"));
+        }
+
+        myJaasProvider.setLoginContextName("");
+
+        try {
+            myJaasProvider.afterPropertiesSet();
+            fail("Should have thrown ApplicationContextException");
+        } catch (ApplicationContextException expected) {
+            assertTrue(expected.getMessage().startsWith("loginContextName must be set on"));
+        }
+    }
+
     public void testFull() throws Exception {
         GrantedAuthorityImpl role1 = new GrantedAuthorityImpl("ROLE_1");
         GrantedAuthorityImpl role2 = new GrantedAuthorityImpl("ROLE_2");
@@ -123,6 +165,12 @@ public class JaasAuthenticationProviderTests extends TestCase {
         assertNull("Failure event was fired", eventCheck.failedEvent);
     }
 
+    public void testUnsupportedAuthenticationObjectReturnsNull() {
+        assertNull(jaasProvider.authenticate(
+                new TestingAuthenticationToken("foo", "bar",
+                    new GrantedAuthority[] {})));
+    }
+
     protected void setUp() throws Exception {
         String resName = "/" + getClass().getName().replace('.', '/') + ".xml";
         context = new ClassPathXmlApplicationContext(resName);