mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-06-29 15:22:15 +00:00
Improve test coverage and error detection at startup time.
This commit is contained in:
parent
699f97929a
commit
3a0e43337c
@ -112,11 +112,11 @@ import javax.security.auth.login.LoginException;
|
|||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
{ * {@link JaasAuthenticationCallbackHandler}s are passed to the
|
* {{@link JaasAuthenticationCallbackHandler}s are passed to the
|
||||||
* JaasAuthenticationProvider through the {@link
|
* JaasAuthenticationProvider through the {@link
|
||||||
* #setCallbackHandlers(net.sf.acegisecurity.providers.jaas.JaasAuthenticationCallbackHandler[])
|
* #setCallbackHandlers(net.sf.acegisecurity.providers.jaas.JaasAuthenticationCallbackHandler[])
|
||||||
* callbackHandlers} property.
|
* callbackHandlers} property. }
|
||||||
} * <pre>
|
* <pre>
|
||||||
* <property name="callbackHandlers">
|
* <property name="callbackHandlers">
|
||||||
* <list>
|
* <list>
|
||||||
* <bean class="net.sf.acegisecurity.providers.jaas.TestCallbackHandler"/>
|
* <bean class="net.sf.acegisecurity.providers.jaas.TestCallbackHandler"/>
|
||||||
@ -255,7 +255,7 @@ public class JaasAuthenticationProvider implements AuthenticationProvider,
|
|||||||
+ getClass());
|
+ getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (loginContextName == null) {
|
if ((loginContextName == null) || "".equals(loginContextName)) {
|
||||||
throw new ApplicationContextException(
|
throw new ApplicationContextException(
|
||||||
"loginContextName must be set on " + getClass());
|
"loginContextName must be set on " + getClass());
|
||||||
}
|
}
|
||||||
|
@ -21,9 +21,11 @@ import net.sf.acegisecurity.Authentication;
|
|||||||
import net.sf.acegisecurity.AuthenticationException;
|
import net.sf.acegisecurity.AuthenticationException;
|
||||||
import net.sf.acegisecurity.GrantedAuthority;
|
import net.sf.acegisecurity.GrantedAuthority;
|
||||||
import net.sf.acegisecurity.GrantedAuthorityImpl;
|
import net.sf.acegisecurity.GrantedAuthorityImpl;
|
||||||
|
import net.sf.acegisecurity.providers.TestingAuthenticationToken;
|
||||||
import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
|
import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextException;
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -71,6 +73,46 @@ public class JaasAuthenticationProviderTests extends TestCase {
|
|||||||
assertNull("Success event was fired", eventCheck.successEvent);
|
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 {
|
public void testFull() throws Exception {
|
||||||
GrantedAuthorityImpl role1 = new GrantedAuthorityImpl("ROLE_1");
|
GrantedAuthorityImpl role1 = new GrantedAuthorityImpl("ROLE_1");
|
||||||
GrantedAuthorityImpl role2 = new GrantedAuthorityImpl("ROLE_2");
|
GrantedAuthorityImpl role2 = new GrantedAuthorityImpl("ROLE_2");
|
||||||
@ -123,6 +165,12 @@ public class JaasAuthenticationProviderTests extends TestCase {
|
|||||||
assertNull("Failure event was fired", eventCheck.failedEvent);
|
assertNull("Failure event was fired", eventCheck.failedEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testUnsupportedAuthenticationObjectReturnsNull() {
|
||||||
|
assertNull(jaasProvider.authenticate(
|
||||||
|
new TestingAuthenticationToken("foo", "bar",
|
||||||
|
new GrantedAuthority[] {})));
|
||||||
|
}
|
||||||
|
|
||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
String resName = "/" + getClass().getName().replace('.', '/') + ".xml";
|
String resName = "/" + getClass().getName().replace('.', '/') + ".xml";
|
||||||
context = new ClassPathXmlApplicationContext(resName);
|
context = new ClassPathXmlApplicationContext(resName);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user