diff --git a/adapters/jboss/src/test/java/org/acegisecurity/adapters/jboss/JbossIntegrationFilterTests.java b/adapters/jboss/src/test/java/org/acegisecurity/adapters/jboss/JbossIntegrationFilterTests.java new file mode 100644 index 0000000000..6411bd8fba --- /dev/null +++ b/adapters/jboss/src/test/java/org/acegisecurity/adapters/jboss/JbossIntegrationFilterTests.java @@ -0,0 +1,130 @@ +/* Copyright 2004 Acegi Technology Pty Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.sf.acegisecurity.adapters.jboss; + +import junit.framework.TestCase; + +import net.sf.acegisecurity.GrantedAuthority; +import net.sf.acegisecurity.GrantedAuthorityImpl; +import net.sf.acegisecurity.adapters.MockHttpServletRequest; +import net.sf.acegisecurity.adapters.MockPrincipal; +import net.sf.acegisecurity.adapters.PrincipalAcegiUserToken; + +import java.security.Principal; + +import java.util.HashSet; +import java.util.Set; + +import javax.naming.Context; + +import javax.security.auth.Subject; + + +/** + * Tests {@link JbossIntegrationFilter}. + * + * @author Ben Alex + * @version $Id$ + */ +public class JbossIntegrationFilterTests extends TestCase { + //~ Constructors =========================================================== + + public JbossIntegrationFilterTests() { + super(); + } + + public JbossIntegrationFilterTests(String arg0) { + super(arg0); + } + + //~ Methods ================================================================ + + public final void setUp() throws Exception { + super.setUp(); + } + + public static void main(String[] args) { + junit.textui.TestRunner.run(JbossIntegrationFilterTests.class); + } + + public void testCorrectOperation() { + PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key", + "someone", "password", + new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")}); + + JbossIntegrationFilter filter = new MockJbossIntegrationFilter(new MockInitialContext( + makeIntoSubject(principal))); + + Object result = filter.extractFromContainer(new MockHttpServletRequest( + null)); + + if (!(result instanceof PrincipalAcegiUserToken)) { + fail("Should have returned PrincipalAcegiUserToken"); + } + + PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) result; + assertEquals(principal, result); + } + + public void testReturnsNullIfContextReturnsSomethingOtherThanASubject() { + JbossIntegrationFilter filter = new MockJbossIntegrationFilter(new MockInitialContext( + "THIS_IS_NOT_A_SUBJECT")); + assertEquals(null, + filter.extractFromContainer(new MockHttpServletRequest(null))); + } + + public void testReturnsNullIfInitialContextHasNullPrincipal() { + JbossIntegrationFilter filter = new MockJbossIntegrationFilter(new MockInitialContext( + makeIntoSubject(null))); + assertEquals(null, + filter.extractFromContainer(new MockHttpServletRequest(null))); + } + + public void testReturnsNullIfInitialContextHasNullSubject() { + JbossIntegrationFilter filter = new MockJbossIntegrationFilter(new MockInitialContext( + null)); + assertEquals(null, + filter.extractFromContainer(new MockHttpServletRequest(null))); + } + + public void testReturnsNullIfInitialContextIsNull() { + JbossIntegrationFilter filter = new MockJbossIntegrationFilter(null); + + Object result = filter.extractFromContainer(new MockHttpServletRequest( + null)); + assertEquals(null, filter.extractFromContainer(null)); + } + + public void testReturnsNullIfPrincipalNotAnAuthenticationImplementation() { + JbossIntegrationFilter filter = new MockJbossIntegrationFilter(new MockInitialContext( + makeIntoSubject(new MockPrincipal()))); + assertEquals(null, + filter.extractFromContainer(new MockHttpServletRequest(null))); + } + + public void testTestingObjectReturnsInitialContext() + throws Exception { + JbossIntegrationFilter filter = new JbossIntegrationFilter(); + assertTrue(filter.getLookupContext() instanceof Context); + } + + private Subject makeIntoSubject(Principal principal) { + Set principals = new HashSet(); + principals.add(principal); + + return new Subject(false, principals, new HashSet(), new HashSet()); + } +} diff --git a/adapters/jboss/src/test/java/org/acegisecurity/adapters/jboss/MockInitialContext.java b/adapters/jboss/src/test/java/org/acegisecurity/adapters/jboss/MockInitialContext.java new file mode 100644 index 0000000000..eadc6614d3 --- /dev/null +++ b/adapters/jboss/src/test/java/org/acegisecurity/adapters/jboss/MockInitialContext.java @@ -0,0 +1,171 @@ +/* Copyright 2004 Acegi Technology Pty Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.sf.acegisecurity.adapters.jboss; + +import java.util.Hashtable; + +import javax.naming.Context; +import javax.naming.Name; +import javax.naming.NameParser; +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; + + +/** + * Mocks a javax.naming.Context and returns an Object + * when queried for address java:comp/env/security/subject. + * + * @author Ben Alex + * @version $Id$ + */ +public class MockInitialContext implements Context { + //~ Instance fields ======================================================== + + private Object object; + + //~ Constructors =========================================================== + + public MockInitialContext(Object object) { + this.object = object; + } + + private MockInitialContext() { + super(); + } + + //~ Methods ================================================================ + + public Hashtable getEnvironment() throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getNameInNamespace() throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public NameParser getNameParser(String name) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public NameParser getNameParser(Name name) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public Object addToEnvironment(String propName, Object propVal) + throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public void bind(String name, Object obj) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public void bind(Name name, Object obj) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public void close() throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String composeName(String name, String prefix) + throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public Name composeName(Name name, Name prefix) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public Context createSubcontext(String name) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public Context createSubcontext(Name name) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public void destroySubcontext(String name) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public void destroySubcontext(Name name) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public NamingEnumeration list(String name) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public NamingEnumeration list(Name name) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public NamingEnumeration listBindings(String name) + throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public NamingEnumeration listBindings(Name name) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public Object lookup(String name) throws NamingException { + return this.object; + } + + public Object lookup(Name name) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public Object lookupLink(String name) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public Object lookupLink(Name name) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public void rebind(String name, Object obj) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public void rebind(Name name, Object obj) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public Object removeFromEnvironment(String propName) + throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public void rename(String oldName, String newName) + throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public void rename(Name oldName, Name newName) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public void unbind(String name) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public void unbind(Name name) throws NamingException { + throw new UnsupportedOperationException("mock method not implemented"); + } +} diff --git a/adapters/jboss/src/test/java/org/acegisecurity/adapters/jboss/MockJbossIntegrationFilter.java b/adapters/jboss/src/test/java/org/acegisecurity/adapters/jboss/MockJbossIntegrationFilter.java new file mode 100644 index 0000000000..24be99f68e --- /dev/null +++ b/adapters/jboss/src/test/java/org/acegisecurity/adapters/jboss/MockJbossIntegrationFilter.java @@ -0,0 +1,49 @@ +/* Copyright 2004 Acegi Technology Pty Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.sf.acegisecurity.adapters.jboss; + +import javax.naming.Context; +import javax.naming.NamingException; + + +/** + * Provides mock of JbossIntegrationFilter, using a lookup + * Context provided in the constructor. + * + * @author Ben Alex + * @version $Id$ + */ +public class MockJbossIntegrationFilter extends JbossIntegrationFilter { + //~ Instance fields ======================================================== + + private Context context; + + //~ Constructors =========================================================== + + public MockJbossIntegrationFilter(Context context) { + this.context = context; + } + + private MockJbossIntegrationFilter() { + super(); + } + + //~ Methods ================================================================ + + protected Context getLookupContext() throws NamingException { + return this.context; + } +} diff --git a/core/src/test/java/org/acegisecurity/adapters/AbstractIntegrationFilterTests.java b/core/src/test/java/org/acegisecurity/adapters/AbstractIntegrationFilterTests.java new file mode 100644 index 0000000000..0aee382aaa --- /dev/null +++ b/core/src/test/java/org/acegisecurity/adapters/AbstractIntegrationFilterTests.java @@ -0,0 +1,260 @@ +/* Copyright 2004 Acegi Technology Pty Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.sf.acegisecurity.adapters; + +import junit.framework.TestCase; + +import net.sf.acegisecurity.Authentication; +import net.sf.acegisecurity.GrantedAuthority; +import net.sf.acegisecurity.GrantedAuthorityImpl; +import net.sf.acegisecurity.context.Context; +import net.sf.acegisecurity.context.ContextHolder; +import net.sf.acegisecurity.context.SecureContext; +import net.sf.acegisecurity.context.SecureContextImpl; + +import java.io.IOException; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; + + +/** + * Tests {@link AbstractIntegrationFilter}. + * + * @author Ben Alex + * @version $Id$ + */ +public class AbstractIntegrationFilterTests extends TestCase { + //~ Constructors =========================================================== + + public AbstractIntegrationFilterTests() { + super(); + } + + public AbstractIntegrationFilterTests(String arg0) { + super(arg0); + } + + //~ Methods ================================================================ + + public final void setUp() throws Exception { + super.setUp(); + } + + public static void main(String[] args) { + junit.textui.TestRunner.run(AbstractIntegrationFilterTests.class); + } + + public void testContextHolderContentsPreserved() throws Exception { + PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key", + "someone", "password", + new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")}); + MockAbstractIntegrationFilterImpl filter = new MockAbstractIntegrationFilterImpl(principal); + MockFilterChain chain = new MockFilterChain(true, principal); + + MockSecureContextImpl secureContext = new MockSecureContextImpl( + "FOO_BAR"); + ContextHolder.setContext(secureContext); + assertEquals(secureContext, ContextHolder.getContext()); + + executeFilterInContainerSimulator(filter, null, null, chain); + + MockSecureContextImpl after = (MockSecureContextImpl) ContextHolder + .getContext(); + assertEquals(secureContext.getInfo(), after.getInfo()); + ContextHolder.setContext(null); + } + + public void testContextHolderHasAuthenticationRemoved() + throws Exception { + PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key", + "someone", "password", + new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")}); + MockAbstractIntegrationFilterImpl filter = new MockAbstractIntegrationFilterImpl(principal); + MockFilterChain chain = new MockFilterChain(true, principal); + + SecureContext secureContext = new SecureContextImpl(); + secureContext.setAuthentication(principal); + ContextHolder.setContext(secureContext); + assertEquals(secureContext, ContextHolder.getContext()); + + executeFilterInContainerSimulator(filter, null, null, chain); + + SecureContext after = (SecureContext) ContextHolder.getContext(); + assertEquals(null, after.getAuthentication()); + ContextHolder.setContext(null); + } + + public void testIgnoredWhenConcreteClassReturnsANonAuthenticationObject() + throws Exception { + MockPrincipal principal = new MockPrincipal(); + MockAbstractIntegrationFilterImpl filter = new MockAbstractIntegrationFilterImpl(principal); + MockFilterChain chain = new MockFilterChain(false, null); + + Context before = ContextHolder.getContext(); + + if (before != null) { + if (before instanceof SecureContext) { + assertEquals(null, ((SecureContext) before).getAuthentication()); + } + } + + executeFilterInContainerSimulator(filter, null, null, chain); + + Context after = ContextHolder.getContext(); + + if (after != null) { + if (after instanceof SecureContext) { + assertEquals(null, ((SecureContext) after).getAuthentication()); + } + } + } + + public void testIgnoredWhenConcreteClassReturnsNullAuthenticationObject() + throws Exception { + MockAbstractIntegrationFilterImpl filter = new MockAbstractIntegrationFilterImpl(null); + MockFilterChain chain = new MockFilterChain(false, null); + + Context before = ContextHolder.getContext(); + + if (before != null) { + if (before instanceof SecureContext) { + assertEquals(null, ((SecureContext) before).getAuthentication()); + } + } + + executeFilterInContainerSimulator(filter, null, null, chain); + + Context after = ContextHolder.getContext(); + + if (after != null) { + if (after instanceof SecureContext) { + assertEquals(null, ((SecureContext) after).getAuthentication()); + } + } + } + + public void testSuccessWhenConcreteClassReturnsValidAuthenticationObject() + throws Exception { + PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key", + "someone", "password", + new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")}); + MockAbstractIntegrationFilterImpl filter = new MockAbstractIntegrationFilterImpl(principal); + MockFilterChain chain = new MockFilterChain(true, principal); + + Context before = ContextHolder.getContext(); + + if (before != null) { + if (before instanceof SecureContext) { + assertEquals(null, ((SecureContext) before).getAuthentication()); + } + } + + executeFilterInContainerSimulator(filter, null, null, chain); + + Context after = ContextHolder.getContext(); + + if (after != null) { + if (after instanceof SecureContext) { + assertEquals(null, ((SecureContext) after).getAuthentication()); + } + } + } + + private void executeFilterInContainerSimulator(Filter filter, + ServletRequest request, ServletResponse response, + FilterChain filterChain) throws ServletException, IOException { + filter.init(null); + filter.doFilter(request, response, filterChain); + filter.destroy(); + } + + //~ Inner Classes ========================================================== + + private class MockAbstractIntegrationFilterImpl + extends AbstractIntegrationFilter { + private Object extractFromContainerResult; + + public MockAbstractIntegrationFilterImpl( + Object extractFromContainerResult) { + this.extractFromContainerResult = extractFromContainerResult; + } + + private MockAbstractIntegrationFilterImpl() { + super(); + } + + public Object extractFromContainer(ServletRequest request) { + return this.extractFromContainerResult; + } + } + + private class MockFilterChain implements FilterChain { + private Authentication expectedAuthenticationObjectInContextHolder; + private boolean expectContextHolderContainSecureContext = false; + + public MockFilterChain( + boolean expectContextHolderContainSecureContext, + Authentication expectedAuthenticationObjectInContextHolder) { + if ((expectedAuthenticationObjectInContextHolder != null) + && !expectContextHolderContainSecureContext) { + throw new IllegalArgumentException( + "If an Authentication object is expected, the ContextHolder should contain a SecureContext"); + } + + this.expectContextHolderContainSecureContext = expectContextHolderContainSecureContext; + this.expectedAuthenticationObjectInContextHolder = expectedAuthenticationObjectInContextHolder; + } + + private MockFilterChain() { + super(); + } + + public void doFilter(ServletRequest request, ServletResponse response) + throws IOException, ServletException { + if (expectContextHolderContainSecureContext) { + Context context = ContextHolder.getContext(); + + if (!(context instanceof SecureContext)) { + fail("ContextHolder should have contained SecureContext"); + } + } else { + if (ContextHolder.getContext() != null) { + fail("ContextHolder should have been null but wasn't"); + } + } + } + } + + private class MockSecureContextImpl extends SecureContextImpl { + private String info; + + public MockSecureContextImpl(String info) { + this.info = info; + } + + private MockSecureContextImpl() { + super(); + } + + public String getInfo() { + return this.info; + } + } +} diff --git a/core/src/test/java/org/acegisecurity/adapters/AutoIntegrationFilterTests.java b/core/src/test/java/org/acegisecurity/adapters/AutoIntegrationFilterTests.java new file mode 100644 index 0000000000..87d0ba841e --- /dev/null +++ b/core/src/test/java/org/acegisecurity/adapters/AutoIntegrationFilterTests.java @@ -0,0 +1,149 @@ +/* Copyright 2004 Acegi Technology Pty Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.sf.acegisecurity.adapters; + +import junit.framework.TestCase; + +import net.sf.acegisecurity.GrantedAuthority; +import net.sf.acegisecurity.GrantedAuthorityImpl; +import net.sf.acegisecurity.adapters.jboss.JbossIntegrationFilter; +import net.sf.acegisecurity.adapters.jboss.MockInitialContext; +import net.sf.acegisecurity.adapters.jboss.MockJbossIntegrationFilter; + +import org.jboss.security.SimplePrincipal; + +import java.security.Principal; + +import java.util.HashSet; +import java.util.Set; + +import javax.naming.Context; + +import javax.security.auth.Subject; + + +/** + * Tests {@link AutoIntegrationFilter}. + * + * @author Ben Alex + * @version $Id$ + */ +public class AutoIntegrationFilterTests extends TestCase { + //~ Constructors =========================================================== + + public AutoIntegrationFilterTests() { + super(); + } + + public AutoIntegrationFilterTests(String arg0) { + super(arg0); + } + + //~ Methods ================================================================ + + public final void setUp() throws Exception { + super.setUp(); + } + + public static void main(String[] args) { + junit.textui.TestRunner.run(AutoIntegrationFilterTests.class); + } + + public void testDetectsAuthenticationObjectInHttpRequest() { + AutoIntegrationFilter filter = new AutoIntegrationFilter(); + PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key", + "someone", "password", + new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")}); + Object result = filter.extractFromContainer(new MockHttpServletRequest( + principal)); + + if (!(result instanceof PrincipalAcegiUserToken)) { + fail("Should have returned PrincipalAcegiUserToken"); + } + + PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) result; + assertEquals(principal, result); + } + + public void testDetectsAuthenticationObjectInJboss() { + // Prepare a mock Jboss environment reflecting completed authentication + PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key", + "someone", "password", + new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")}); + Context context = new MockInitialContext(makeIntoSubject(principal)); + JbossIntegrationFilter jbossFilter = new MockJbossIntegrationFilter(context); + + // Create a SimplePrincipal, which is what JBoss places into HttpRequest + SimplePrincipal simplePrincipal = new SimplePrincipal("TEST"); + + // Now try to extract authentication information via our mock AutoIntegrationFilter + AutoIntegrationFilter filter = new MockAutoIntegrationFilterJboss(jbossFilter); + Object result = filter.extractFromContainer(new MockHttpServletRequest( + simplePrincipal)); + + System.out.println(result); + + if (!(result instanceof PrincipalAcegiUserToken)) { + fail("Should have returned PrincipalAcegiUserToken"); + } + + PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) result; + assertEquals(principal, result); + } + + public void testHandlesIfHttpRequestIsNullForSomeReason() { + AutoIntegrationFilter filter = new AutoIntegrationFilter(); + assertEquals(null, filter.extractFromContainer(null)); + } + + public void testHandlesIfThereIsNoPrincipal() { + AutoIntegrationFilter filter = new AutoIntegrationFilter(); + assertEquals(null, + filter.extractFromContainer(new MockHttpServletRequest(null))); + } + + public void testReturnsNullIfNonAuthenticationObjectInHttpRequest() { + AutoIntegrationFilter filter = new AutoIntegrationFilter(); + assertEquals(null, + filter.extractFromContainer( + new MockHttpServletRequest(new MockPrincipal()))); + } + + private Subject makeIntoSubject(Principal principal) { + Set principals = new HashSet(); + principals.add(principal); + + return new Subject(false, principals, new HashSet(), new HashSet()); + } + + //~ Inner Classes ========================================================== + + private class MockAutoIntegrationFilterJboss extends AutoIntegrationFilter { + private JbossIntegrationFilter filter; + + public MockAutoIntegrationFilterJboss(JbossIntegrationFilter filter) { + this.filter = filter; + } + + private MockAutoIntegrationFilterJboss() { + super(); + } + + protected JbossIntegrationFilter getJbossIntegrationFilter() { + return this.filter; + } + } +} diff --git a/core/src/test/java/org/acegisecurity/adapters/HttpRequestIntegrationFilterTests.java b/core/src/test/java/org/acegisecurity/adapters/HttpRequestIntegrationFilterTests.java new file mode 100644 index 0000000000..4a2abe5f64 --- /dev/null +++ b/core/src/test/java/org/acegisecurity/adapters/HttpRequestIntegrationFilterTests.java @@ -0,0 +1,77 @@ +/* Copyright 2004 Acegi Technology Pty Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.sf.acegisecurity.adapters; + +import junit.framework.TestCase; + +import net.sf.acegisecurity.GrantedAuthority; +import net.sf.acegisecurity.GrantedAuthorityImpl; + + +/** + * Tests {@link HttpRequestIntegrationFilter}. + * + * @author Ben Alex + * @version $Id$ + */ +public class HttpRequestIntegrationFilterTests extends TestCase { + //~ Constructors =========================================================== + + public HttpRequestIntegrationFilterTests() { + super(); + } + + public HttpRequestIntegrationFilterTests(String arg0) { + super(arg0); + } + + //~ Methods ================================================================ + + public final void setUp() throws Exception { + super.setUp(); + } + + public static void main(String[] args) { + junit.textui.TestRunner.run(HttpRequestIntegrationFilterTests.class); + } + + public void testCorrectOperation() { + HttpRequestIntegrationFilter filter = new HttpRequestIntegrationFilter(); + PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key", + "someone", "password", + new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")}); + Object result = filter.extractFromContainer(new MockHttpServletRequest( + principal)); + + if (!(result instanceof PrincipalAcegiUserToken)) { + fail("Should have returned PrincipalAcegiUserToken"); + } + + PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) result; + assertEquals(principal, result); + } + + public void testHandlesIfHttpRequestIsNullForSomeReason() { + HttpRequestIntegrationFilter filter = new HttpRequestIntegrationFilter(); + assertEquals(null, filter.extractFromContainer(null)); + } + + public void testHandlesIfThereIsNoPrincipal() { + HttpRequestIntegrationFilter filter = new HttpRequestIntegrationFilter(); + assertEquals(null, + filter.extractFromContainer(new MockHttpServletRequest(null))); + } +} diff --git a/core/src/test/java/org/acegisecurity/adapters/MockHttpServletRequest.java b/core/src/test/java/org/acegisecurity/adapters/MockHttpServletRequest.java new file mode 100644 index 0000000000..bdff24276e --- /dev/null +++ b/core/src/test/java/org/acegisecurity/adapters/MockHttpServletRequest.java @@ -0,0 +1,257 @@ +/* Copyright 2004 Acegi Technology Pty Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.sf.acegisecurity.adapters; + +import java.io.BufferedReader; +import java.io.IOException; + +import java.security.Principal; + +import java.util.Enumeration; +import java.util.Locale; +import java.util.Map; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletInputStream; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + + +/** + * Mocks a HttpServletRequest and provides the + * getUserPrincipal() method. + * + * @author Ben Alex + * @version $Id$ + */ +public class MockHttpServletRequest implements HttpServletRequest { + //~ Instance fields ======================================================== + + private Principal principal; + + //~ Constructors =========================================================== + + public MockHttpServletRequest(Principal principal) { + this.principal = principal; + } + + private MockHttpServletRequest() { + super(); + } + + //~ Methods ================================================================ + + public void setAttribute(String arg0, Object arg1) { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public Object getAttribute(String arg0) { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public Enumeration getAttributeNames() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getAuthType() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public void setCharacterEncoding(String arg0) { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getCharacterEncoding() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public int getContentLength() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getContentType() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getContextPath() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public Cookie[] getCookies() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public long getDateHeader(String arg0) { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getHeader(String arg0) { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public Enumeration getHeaderNames() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public Enumeration getHeaders(String arg0) { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public ServletInputStream getInputStream() throws IOException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public int getIntHeader(String arg0) { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public Locale getLocale() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public Enumeration getLocales() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getMethod() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getParameter(String arg0) { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public Map getParameterMap() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public Enumeration getParameterNames() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String[] getParameterValues(String arg0) { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getPathInfo() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getPathTranslated() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getProtocol() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getQueryString() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public BufferedReader getReader() throws IOException { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getRealPath(String arg0) { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getRemoteAddr() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getRemoteHost() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getRemoteUser() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public RequestDispatcher getRequestDispatcher(String arg0) { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getRequestURI() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public StringBuffer getRequestURL() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getRequestedSessionId() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public boolean isRequestedSessionIdFromCookie() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public boolean isRequestedSessionIdFromURL() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public boolean isRequestedSessionIdFromUrl() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public boolean isRequestedSessionIdValid() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getScheme() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public boolean isSecure() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getServerName() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public int getServerPort() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public String getServletPath() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public HttpSession getSession(boolean arg0) { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public HttpSession getSession() { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public boolean isUserInRole(String arg0) { + throw new UnsupportedOperationException("mock method not implemented"); + } + + public Principal getUserPrincipal() { + return this.principal; + } + + public void removeAttribute(String arg0) { + throw new UnsupportedOperationException("mock method not implemented"); + } +} diff --git a/core/src/test/java/org/acegisecurity/adapters/MockPrincipal.java b/core/src/test/java/org/acegisecurity/adapters/MockPrincipal.java new file mode 100644 index 0000000000..566a810a9d --- /dev/null +++ b/core/src/test/java/org/acegisecurity/adapters/MockPrincipal.java @@ -0,0 +1,33 @@ +/* Copyright 2004 Acegi Technology Pty Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.sf.acegisecurity.adapters; + +import java.security.Principal; + + +/** + * Mocks a Principal. + * + * @author Ben Alex + * @version $Id$ + */ +public class MockPrincipal implements Principal { + //~ Methods ================================================================ + + public String getName() { + return "MockPrincipal"; + } +}