You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by ka...@apache.org on 2011/07/18 13:10:55 UTC
svn commit: r1147810 [2/2] - in /shiro/trunk: ./ all/ support/
support/guice/ support/guice/src/ support/guice/src/main/
support/guice/src/main/java/ support/guice/src/main/java/org/
support/guice/src/main/java/org/apache/ support/guice/src/main/java/o...
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/GuiceEnvironmentTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/GuiceEnvironmentTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/GuiceEnvironmentTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/GuiceEnvironmentTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice;
+
+import com.google.inject.spi.InjectionPoint;
+import org.apache.shiro.mgt.SecurityManager;
+import org.junit.Test;
+
+import static org.easymock.EasyMock.createMock;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
+
+public class GuiceEnvironmentTest {
+ @Test
+ public void testGetSecurityManager() throws Exception {
+ SecurityManager securityManager = createMock(SecurityManager.class);
+
+ GuiceEnvironment underTest = new GuiceEnvironment(securityManager);
+ assertSame(securityManager, underTest.getSecurityManager());
+ }
+
+ @Test
+ public void ensureInjectable() {
+ try {
+ InjectionPoint ip = InjectionPoint.forConstructorOf(GuiceEnvironment.class);
+ } catch (Exception e) {
+ fail("Could not create constructor injection point.");
+ }
+ }
+}
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/InitializableInjectionListenerTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/InitializableInjectionListenerTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/InitializableInjectionListenerTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/InitializableInjectionListenerTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice;
+
+import org.apache.shiro.util.Initializable;
+import org.junit.Test;
+
+import static org.easymock.EasyMock.*;
+
+public class InitializableInjectionListenerTest {
+ @Test
+ public void testAfterInjection() throws Exception {
+ Initializable initializable = createMock(Initializable.class);
+
+ initializable.init();
+
+ replay(initializable);
+
+ InitializableInjectionListener underTest = new InitializableInjectionListener();
+ underTest.afterInjection(initializable);
+
+ verify(initializable);
+ }
+}
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/LifecycleTypeListenerTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/LifecycleTypeListenerTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/LifecycleTypeListenerTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/LifecycleTypeListenerTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice;
+
+import com.google.inject.TypeLiteral;
+import com.google.inject.spi.TypeEncounter;
+import org.apache.shiro.ShiroException;
+import org.apache.shiro.util.Destroyable;
+import org.apache.shiro.util.Initializable;
+import org.junit.Test;
+
+import static org.easymock.EasyMock.*;
+
+public class LifecycleTypeListenerTest {
+ @Test
+ public void testHearInitializable() throws Exception {
+ TypeEncounter encounter = createMock(TypeEncounter.class);
+
+ encounter.register(anyObject(InitializableInjectionListener.class));
+
+ replay(encounter);
+
+ LifecycleTypeListener underTest = new LifecycleTypeListener(null);
+
+ underTest.hear(TypeLiteral.get(MyInitializable.class), encounter);
+
+ verify(encounter);
+ }
+
+ @Test
+ public void testHearDestroyable() throws Exception {
+ TypeEncounter encounter = createMock(TypeEncounter.class);
+
+ encounter.register(anyObject(DestroyableInjectionListener.class));
+
+ replay(encounter);
+
+ LifecycleTypeListener underTest = new LifecycleTypeListener(null);
+
+ underTest.hear(TypeLiteral.get(MyDestroyable.class), encounter);
+
+ verify(encounter);
+ }
+
+ static class MyInitializable implements Initializable {
+
+ public void init() throws ShiroException {
+ // do nothing
+ }
+ }
+
+ static class MyDestroyable implements Destroyable {
+ public void destroy() throws Exception {
+ // do nothing
+ }
+ }
+}
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/ShiroMatchersTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/ShiroMatchersTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/ShiroMatchersTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/ShiroMatchersTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice;
+
+import com.google.inject.TypeLiteral;
+import com.google.inject.matcher.Matcher;
+import org.junit.Test;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class ShiroMatchersTest {
+ @Test
+ public void testTypeLiteral() throws Exception {
+ Matcher<Class> classMatcher = createMock(Matcher.class);
+ expect(classMatcher.matches(MatchingClass.class)).andReturn(true);
+ expect(classMatcher.matches(NotMatchingClass.class)).andReturn(false);
+
+ replay(classMatcher);
+
+ Matcher<TypeLiteral> underTest = ShiroMatchers.typeLiteral(classMatcher);
+
+ assertTrue(underTest.matches(TypeLiteral.get(MatchingClass.class)));
+ assertFalse(underTest.matches(TypeLiteral.get(NotMatchingClass.class)));
+
+ verify(classMatcher);
+ }
+
+
+ static class MatchingClass {
+ }
+
+ static class NotMatchingClass {
+ }
+}
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/ShiroModuleTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/ShiroModuleTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/ShiroModuleTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/ShiroModuleTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,230 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice;
+
+import com.google.inject.Guice;
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import com.google.inject.Provides;
+import com.google.inject.binder.AnnotatedBindingBuilder;
+import org.apache.shiro.SecurityUtils;
+import org.apache.shiro.authc.AuthenticationInfo;
+import org.apache.shiro.authc.AuthenticationToken;
+import org.apache.shiro.authc.SimpleAuthenticationInfo;
+import org.apache.shiro.env.Environment;
+import org.apache.shiro.mgt.DefaultSecurityManager;
+import org.apache.shiro.mgt.SecurityManager;
+import org.apache.shiro.realm.Realm;
+import org.apache.shiro.session.mgt.DefaultSessionManager;
+import org.apache.shiro.session.mgt.SessionManager;
+import org.apache.shiro.subject.Subject;
+import org.apache.shiro.util.Destroyable;
+import org.junit.Test;
+
+import java.util.Collection;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+public class ShiroModuleTest {
+
+ @Test
+ public void basicInstantiation() {
+
+ final MockRealm mockRealm = createMock(MockRealm.class);
+
+ Injector injector = Guice.createInjector(new ShiroModule() {
+ @Override
+ protected void configureShiro() {
+ bindRealm().to(MockRealm.class);
+ }
+
+ @Provides
+ public MockRealm createRealm() {
+ return mockRealm;
+ }
+ });
+ SecurityManager securityManager = injector.getInstance(SecurityManager.class);
+ assertNotNull(securityManager);
+ }
+
+ @Test
+ public void testConfigure() {
+ final MockRealm mockRealm = createMock(MockRealm.class);
+ AuthenticationToken authToken = createMock(AuthenticationToken.class);
+ AuthenticationInfo info = new SimpleAuthenticationInfo("mockUser", "password", "mockRealm");
+
+ expect(mockRealm.supports(authToken)).andReturn(true);
+ expect(mockRealm.getAuthenticationInfo(authToken)).andReturn(info);
+
+ replay(mockRealm);
+
+ Injector injector = Guice.createInjector(new ShiroModule() {
+ @Override
+ protected void configureShiro() {
+ bindRealm().to(MockRealm.class);
+ }
+
+ @Provides
+ public MockRealm createRealm() {
+ return mockRealm;
+ }
+ });
+ SecurityManager securityManager = injector.getInstance(SecurityManager.class);
+ assertNotNull(securityManager);
+ SecurityUtils.setSecurityManager(securityManager);
+
+ final Subject subject = new Subject.Builder(securityManager).buildSubject();
+ securityManager.login(subject, authToken);
+
+ verify(mockRealm);
+ }
+
+ @Test
+ public void testBindSecurityManager() {
+ final MockRealm mockRealm = createMock(MockRealm.class);
+
+ Injector injector = Guice.createInjector(new ShiroModule() {
+ @Override
+ protected void configureShiro() {
+ bindRealm().to(MockRealm.class);
+ }
+
+ @Provides
+ public MockRealm createRealm() {
+ return mockRealm;
+ }
+
+ @Override
+ protected void bindSecurityManager(AnnotatedBindingBuilder<? super SecurityManager> bind) {
+ bind.to(MyDefaultSecurityManager.class);
+ }
+ });
+ SecurityManager securityManager = injector.getInstance(SecurityManager.class);
+ assertNotNull(securityManager);
+ assertTrue(securityManager instanceof MyDefaultSecurityManager);
+ }
+
+ @Test
+ public void testBindSessionManager() {
+ final MockRealm mockRealm = createMock(MockRealm.class);
+
+ Injector injector = Guice.createInjector(new ShiroModule() {
+ @Override
+ protected void configureShiro() {
+ bindRealm().to(MockRealm.class);
+ }
+
+ @Provides
+ public MockRealm createRealm() {
+ return mockRealm;
+ }
+
+ @Override
+ protected void bindSessionManager(AnnotatedBindingBuilder<SessionManager> bind) {
+ bind.to(MyDefaultSessionManager.class);
+ }
+ });
+ DefaultSecurityManager securityManager = (DefaultSecurityManager) injector.getInstance(SecurityManager.class);
+ assertNotNull(securityManager);
+ assertNotNull(securityManager.getSessionManager());
+ assertTrue(securityManager.getSessionManager() instanceof MyDefaultSessionManager);
+ }
+
+ @Test
+ public void testBindEnvironment() {
+ final MockRealm mockRealm = createMock(MockRealm.class);
+
+ Injector injector = Guice.createInjector(new ShiroModule() {
+ @Override
+ protected void configureShiro() {
+ bindRealm().to(MockRealm.class);
+ expose(Environment.class);
+ }
+
+ @Provides
+ public MockRealm createRealm() {
+ return mockRealm;
+ }
+
+ @Override
+ protected void bindEnvironment(AnnotatedBindingBuilder<Environment> bind) {
+ bind.to(MyEnvironment.class);
+ }
+ });
+ Environment environment = injector.getInstance(Environment.class);
+ assertNotNull(environment);
+ assertTrue(environment instanceof MyEnvironment);
+ }
+
+ @Test
+ public void testDestroy() throws Exception {
+ final MockRealm mockRealm = createMock(MockRealm.class);
+ final MyDestroyable myDestroyable = createMock(MyDestroyable.class);
+
+ myDestroyable.destroy();
+
+ replay(myDestroyable);
+
+ final ShiroModule shiroModule = new ShiroModule() {
+ @Override
+ protected void configureShiro() {
+ bindRealm().to(MockRealm.class);
+ bind(MyDestroyable.class).toInstance(myDestroyable);
+ expose(MyDestroyable.class);
+ }
+
+ @Provides
+ public MockRealm createRealm() {
+ return mockRealm;
+ }
+
+ };
+ Injector injector = Guice.createInjector(shiroModule);
+ injector.getInstance(MyDestroyable.class);
+ shiroModule.destroy();
+
+ verify(myDestroyable);
+ }
+
+ public static interface MockRealm extends Realm {
+
+ }
+
+ public static class MyDefaultSecurityManager extends DefaultSecurityManager {
+ @Inject
+ public MyDefaultSecurityManager(Collection<Realm> realms) {
+ super(realms);
+ }
+ }
+
+ public static class MyDefaultSessionManager extends DefaultSessionManager {
+ }
+
+ public static class MyEnvironment extends GuiceEnvironment {
+ @Inject
+ public MyEnvironment(SecurityManager securityManager) {
+ super(securityManager);
+ }
+ }
+
+ public static interface MyDestroyable extends Destroyable {
+ }
+}
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/ShiroSessionScopeTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/ShiroSessionScopeTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/ShiroSessionScopeTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/ShiroSessionScopeTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice;
+
+import com.google.inject.Key;
+import com.google.inject.OutOfScopeException;
+import com.google.inject.Provider;
+import org.apache.shiro.session.Session;
+import org.apache.shiro.subject.Subject;
+import org.apache.shiro.util.ThreadContext;
+import org.junit.Test;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.assertSame;
+
+public class ShiroSessionScopeTest {
+ @Test
+ public void testScope() throws Exception {
+ Subject subject = createMock(Subject.class);
+ try {
+ ThreadContext.bind(subject);
+
+ final Key<SomeClass> key = Key.get(SomeClass.class);
+ Provider<SomeClass> mockProvider = createMock(Provider.class);
+ Session session = createMock(Session.class);
+
+ SomeClass retuned = new SomeClass();
+
+ expect(subject.getSession()).andReturn(session);
+ expect(session.getAttribute(key)).andReturn(null);
+ expect(mockProvider.get()).andReturn(retuned);
+
+ expect(subject.getSession()).andReturn(session);
+ expect(session.getAttribute(key)).andReturn(retuned);
+
+
+ replay(subject, mockProvider, session);
+
+ ShiroSessionScope underTest = new ShiroSessionScope();
+
+ // first time the session doesn't contain it, we expect the provider to be invoked
+ assertSame(retuned, underTest.scope(key, mockProvider).get());
+ // second time the session does contain it, we expect the provider to not be invoked
+ assertSame(retuned, underTest.scope(key, mockProvider).get());
+
+ verify(subject, mockProvider, session);
+ } finally {
+ ThreadContext.unbindSubject();
+ }
+
+ }
+
+ @Test(expected = OutOfScopeException.class)
+ public void testOutOfScope() throws Exception {
+ ShiroSessionScope underTest = new ShiroSessionScope();
+
+ Provider<SomeClass> mockProvider = createMock(Provider.class);
+
+ replay(mockProvider);
+
+ underTest.scope(Key.get(SomeClass.class), mockProvider).get();
+ }
+
+
+ static class SomeClass {
+
+ }
+}
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/aop/AopAllianceMethodInterceptorAdapterTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/aop/AopAllianceMethodInterceptorAdapterTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/aop/AopAllianceMethodInterceptorAdapterTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/aop/AopAllianceMethodInterceptorAdapterTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice.aop;
+
+import org.aopalliance.intercept.MethodInvocation;
+import org.apache.shiro.aop.MethodInterceptor;
+import org.easymock.IAnswer;
+import org.junit.Test;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.assertSame;
+
+
+public class AopAllianceMethodInterceptorAdapterTest {
+ @Test
+ public void testInvoke() throws Throwable {
+ MethodInvocation allianceInvocation = createMock(MethodInvocation.class);
+ MethodInterceptor mockShiroInterceptor = createMock(MethodInterceptor.class);
+ expect(mockShiroInterceptor.invoke(anyObject(AopAllianceMethodInvocationAdapter.class))).andAnswer(new IAnswer<Object>() {
+ public Object answer() throws Throwable {
+ return getCurrentArguments()[0];
+ }
+ });
+ final Object expectedValue = new Object();
+ expect(allianceInvocation.proceed()).andReturn(expectedValue);
+
+ replay(mockShiroInterceptor, allianceInvocation);
+
+ AopAllianceMethodInterceptorAdapter underTest = new AopAllianceMethodInterceptorAdapter(mockShiroInterceptor);
+ Object invocation = underTest.invoke(allianceInvocation);
+ Object value = ((AopAllianceMethodInvocationAdapter) invocation).proceed();
+
+ assertSame("Adapter invocation returned a different value.", expectedValue, value);
+
+ verify(mockShiroInterceptor, allianceInvocation);
+ }
+}
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/aop/AopAllianceMethodInvocationAdapterTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/aop/AopAllianceMethodInvocationAdapterTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/aop/AopAllianceMethodInvocationAdapterTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/aop/AopAllianceMethodInvocationAdapterTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice.aop;
+
+import org.aopalliance.intercept.MethodInvocation;
+import org.junit.Test;
+
+import java.lang.reflect.Method;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.assertSame;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: jbunting
+ * Date: 6/18/11
+ * Time: 5:02 PM
+ * To change this template use File | Settings | File Templates.
+ */
+public class AopAllianceMethodInvocationAdapterTest {
+ @Test
+ public void testGetMethod() throws Exception {
+ MethodInvocation mock = createMock(MethodInvocation.class);
+ Method method = AopAllianceMethodInvocationAdapterTest.class.getMethod("testGetMethod");
+ expect(mock.getMethod()).andReturn(method);
+ AopAllianceMethodInvocationAdapter underTest = new AopAllianceMethodInvocationAdapter(mock);
+
+ replay(mock);
+
+ assertSame(method, underTest.getMethod());
+
+ verify(mock);
+ }
+
+ @Test
+ public void testGetArguments() throws Exception {
+ MethodInvocation mock = createMock(MethodInvocation.class);
+ Object[] args = new Object[0];
+ expect(mock.getArguments()).andReturn(args);
+ AopAllianceMethodInvocationAdapter underTest = new AopAllianceMethodInvocationAdapter(mock);
+
+ replay(mock);
+
+ assertSame(args, underTest.getArguments());
+
+ verify(mock);
+ }
+
+ @Test
+ public void testProceed() throws Throwable {
+ MethodInvocation mock = createMock(MethodInvocation.class);
+ Object value = new Object();
+ expect(mock.proceed()).andReturn(value);
+ AopAllianceMethodInvocationAdapter underTest = new AopAllianceMethodInvocationAdapter(mock);
+
+ replay(mock);
+
+ assertSame(value, underTest.proceed());
+
+ verify(mock);
+ }
+
+ @Test
+ public void testGetThis() throws Exception {
+ MethodInvocation mock = createMock(MethodInvocation.class);
+ Object value = new Object();
+ expect(mock.getThis()).andReturn(value);
+ AopAllianceMethodInvocationAdapter underTest = new AopAllianceMethodInvocationAdapter(mock);
+
+ replay(mock);
+
+ assertSame(value, underTest.getThis());
+
+ verify(mock);
+ }
+}
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/aop/ShiroAopModuleTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/aop/ShiroAopModuleTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/aop/ShiroAopModuleTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/aop/ShiroAopModuleTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,213 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice.aop;
+
+import com.google.inject.Binding;
+import com.google.inject.Key;
+import com.google.inject.name.Named;
+import com.google.inject.name.Names;
+import com.google.inject.spi.Element;
+import com.google.inject.spi.Elements;
+import com.google.inject.spi.InterceptorBinding;
+import org.aopalliance.intercept.MethodInterceptor;
+import org.apache.shiro.aop.*;
+import org.apache.shiro.authz.annotation.*;
+import org.apache.shiro.authz.aop.*;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.lang.annotation.*;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.*;
+
+public class ShiroAopModuleTest {
+ @Test
+ public void testGetAnnotationResolver() {
+
+ final AnnotationResolver annotationResolver = new DefaultAnnotationResolver();
+
+ ShiroAopModule underTest = new ShiroAopModule() {
+
+ @Override
+ protected AnnotationResolver createAnnotationResolver() {
+ return annotationResolver;
+ }
+
+ @Override
+ protected void configureDefaultInterceptors(AnnotationResolver resolver) {
+ assertSame(annotationResolver, resolver);
+ bind(Object.class).annotatedWith(Names.named("configureDefaultInterceptors"));
+ }
+
+ @Override
+ protected void configureInterceptors(AnnotationResolver resolver) {
+ assertSame(annotationResolver, resolver);
+ bind(Object.class).annotatedWith(Names.named("configureInterceptors"));
+ }
+ };
+
+ boolean calledDefault = false;
+ boolean calledCustom = false;
+
+ for (Element e : Elements.getElements(underTest)) {
+ if (e instanceof Binding) {
+ Key key = ((Binding) e).getKey();
+ if (Named.class.isAssignableFrom(key.getAnnotation().annotationType())
+ && "configureInterceptors".equals(((Named) key.getAnnotation()).value())
+ && key.getTypeLiteral().getRawType().equals(Object.class)) {
+ calledCustom = true;
+ }
+ if (Named.class.isAssignableFrom(key.getAnnotation().annotationType())
+ && "configureDefaultInterceptors".equals(((Named) key.getAnnotation()).value())
+ && key.getTypeLiteral().getRawType().equals(Object.class)) {
+ calledDefault = true;
+ }
+ }
+ }
+ }
+
+ @Test
+ public void testBindShiroInterceptor() {
+
+
+ ShiroAopModule underTest = new ShiroAopModule() {
+ @Override
+ protected void configureInterceptors(AnnotationResolver resolver) {
+ bindShiroInterceptor(new MyAnnotationMethodInterceptor());
+ }
+ };
+
+ List<Element> elements = Elements.getElements(underTest);
+
+ for (Element element : elements) {
+ if (element instanceof InterceptorBinding) {
+ InterceptorBinding binding = (InterceptorBinding) element;
+ assertTrue(binding.getClassMatcher().matches(getClass()));
+ Method method = null;
+ Class<? extends Annotation> theAnnotation = null;
+
+ for (Class<? extends Annotation> annotation : protectedMethods.keySet()) {
+ if (binding.getMethodMatcher().matches(protectedMethods.get(annotation))) {
+ method = protectedMethods.get(annotation);
+ theAnnotation = annotation;
+ protectedMethods.remove(annotation);
+ break;
+ }
+ }
+
+ if (method == null) {
+ fail("Did not expect interceptor binding " + binding.getInterceptors());
+ }
+
+ List<MethodInterceptor> interceptors = binding.getInterceptors();
+ assertEquals(1, interceptors.size());
+ assertTrue(interceptors.get(0) instanceof AopAllianceMethodInterceptorAdapter);
+ assertTrue(interceptorTypes.get(theAnnotation).isInstance(((AopAllianceMethodInterceptorAdapter) interceptors.get(0)).shiroInterceptor));
+
+ }
+ }
+
+ assertTrue("Not all interceptors were bound.", protectedMethods.isEmpty());
+ }
+
+ @Target({ElementType.TYPE, ElementType.METHOD})
+ @Retention(RetentionPolicy.RUNTIME)
+ private static @interface MyTestAnnotation {
+ }
+
+ private static class MyAnnotationHandler extends AnnotationHandler {
+
+ /**
+ * Constructs an <code>AnnotationHandler</code> who processes annotations of the
+ * specified type. Immediately calls {@link #setAnnotationClass(Class)}.
+ *
+ * @param annotationClass the type of annotation this handler will process.
+ */
+ public MyAnnotationHandler(Class<? extends Annotation> annotationClass) {
+ super(annotationClass);
+ }
+ }
+
+ private static class MyAnnotationMethodInterceptor extends AnnotationMethodInterceptor {
+ public MyAnnotationMethodInterceptor() {
+ super(new MyAnnotationHandler(MyTestAnnotation.class));
+ }
+
+ public Object invoke(MethodInvocation methodInvocation) throws Throwable {
+ return null;
+ }
+ }
+
+
+ @RequiresRoles("role")
+ public void roleProtected() {
+
+ }
+
+ @RequiresPermissions("permission")
+ public void permissionProtected() {
+
+ }
+
+ @RequiresAuthentication
+ public void authProtected() {
+
+ }
+
+ @RequiresUser
+ public void userProtected() {
+
+ }
+
+ @RequiresGuest
+ public void guestProtected() {
+
+ }
+
+ @ShiroAopModuleTest.MyTestAnnotation
+ public void myTestProtected() {
+
+ }
+
+ private Map<Class<? extends Annotation>, Method> protectedMethods;
+ private Map<Class<? extends Annotation>, Class<? extends AnnotationMethodInterceptor>> interceptorTypes;
+
+ @Before
+ public void setup() throws NoSuchMethodException {
+ protectedMethods = new HashMap<Class<? extends Annotation>, Method>();
+ protectedMethods.put(RequiresRoles.class, getClass().getMethod("roleProtected"));
+ protectedMethods.put(RequiresPermissions.class, getClass().getMethod("permissionProtected"));
+ protectedMethods.put(RequiresAuthentication.class, getClass().getMethod("authProtected"));
+ protectedMethods.put(RequiresUser.class, getClass().getMethod("userProtected"));
+ protectedMethods.put(RequiresGuest.class, getClass().getMethod("guestProtected"));
+ protectedMethods.put(MyTestAnnotation.class, getClass().getMethod("myTestProtected"));
+
+ interceptorTypes = new HashMap<Class<? extends Annotation>, Class<? extends AnnotationMethodInterceptor>>();
+ interceptorTypes.put(RequiresRoles.class, RoleAnnotationMethodInterceptor.class);
+ interceptorTypes.put(RequiresPermissions.class, PermissionAnnotationMethodInterceptor.class);
+ interceptorTypes.put(RequiresAuthentication.class, AuthenticatedAnnotationMethodInterceptor.class);
+ interceptorTypes.put(RequiresUser.class, UserAnnotationMethodInterceptor.class);
+ interceptorTypes.put(RequiresGuest.class, GuestAnnotationMethodInterceptor.class);
+ interceptorTypes.put(MyTestAnnotation.class, MyAnnotationMethodInterceptor.class);
+ }
+}
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/AbstractInjectionProviderTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/AbstractInjectionProviderTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/AbstractInjectionProviderTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/AbstractInjectionProviderTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice.web;
+
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import com.google.inject.Key;
+import com.google.inject.name.Named;
+import com.google.inject.name.Names;
+import com.google.inject.spi.Dependency;
+import org.junit.Test;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.*;
+
+public class AbstractInjectionProviderTest {
+
+ @Test
+ public void testGet() throws Exception {
+ Injector mockInjector = createMock(Injector.class);
+
+ Object c1 = new Object();
+ Object c2 = new Object();
+ final AtomicBoolean postProcessCalled = new AtomicBoolean(false);
+
+ expect(mockInjector.getInstance(keyC1)).andReturn(c1);
+ expect(mockInjector.getInstance(keyC2)).andReturn(c2);
+ mockInjector.injectMembers(anyObject(SomeInjectedClass.class));
+
+ replay(mockInjector);
+
+ AbstractInjectionProvider<SomeInjectedClass> underTest =
+ new AbstractInjectionProvider<SomeInjectedClass>(Key.get(SomeInjectedClass.class)) {
+ @Override
+ protected SomeInjectedClass postProcess(SomeInjectedClass someInjectedClass) {
+ postProcessCalled.set(true);
+ return super.postProcess(someInjectedClass);
+ }
+ };
+
+ underTest.injector = mockInjector;
+
+ SomeInjectedClass got = underTest.get();
+
+ assertEquals("Wrong parameter passed to constructor (index 0).", c1, got.c1);
+ assertEquals("Wrong parameter passed to constructor (index 1).", c2, got.c2);
+
+ assertTrue("postProcess method was not called.", postProcessCalled.get());
+
+ verify(mockInjector);
+ }
+
+ @Test
+ public void testGetDependencies() throws Exception {
+ AbstractInjectionProvider<SomeInjectedClass> underTest =
+ new AbstractInjectionProvider<SomeInjectedClass>(Key.get(SomeInjectedClass.class));
+
+ boolean foundC1 = false;
+ boolean foundC2 = false;
+ boolean foundV1 = false;
+ boolean foundV2 = false;
+ boolean foundF1 = false;
+
+ for (Dependency<?> dependency : underTest.getDependencies()) {
+ if (dependency.getInjectionPoint().getMember() instanceof Constructor) {
+ if (dependency.getParameterIndex() == 0 && dependency.getKey().equals(keyC1)) {
+ foundC1 = true;
+ } else if (dependency.getParameterIndex() == 1 && dependency.getKey().equals(keyC2)) {
+ foundC2 = true;
+ } else {
+ fail("Did not expect constructor dependency with key " + dependency.getKey() + " at parameter index " + dependency.getParameterIndex());
+ }
+ } else if (dependency.getInjectionPoint().getMember() instanceof Method) {
+ if (dependency.getKey().equals(keyV1)) {
+ foundV1 = true;
+ } else if (dependency.getKey().equals(keyV2)) {
+ foundV2 = true;
+ } else {
+ fail("Did not expect method dependency with key " + dependency.getKey());
+ }
+ } else if (dependency.getInjectionPoint().getMember() instanceof Field) {
+ if (dependency.getKey().equals(keyF1)) {
+ foundF1 = true;
+ } else {
+ fail("Did not expect field dependency with key " + dependency.getKey());
+ }
+ } else {
+ fail("Did not expect dependency with key " + dependency.getKey());
+ }
+ }
+
+ assertTrue("Did not find dependency C1", foundC1);
+ assertTrue("Did not find dependency C2", foundC2);
+ assertTrue("Did not find dependency V1", foundV1);
+ assertTrue("Did not find dependency V2", foundV2);
+ assertTrue("Did not find dependency F1", foundF1);
+ }
+
+ static Key keyC1 = Key.get(Object.class, Names.named("constructor1"));
+ static Key keyC2 = Key.get(Object.class, Names.named("constructor2"));
+ static Key keyV1 = Key.get(Object.class, Names.named("val1"));
+ static Key keyV2 = Key.get(Object.class, Names.named("val2"));
+ static Key keyF1 = Key.get(Object.class, Names.named("field1"));
+
+
+ static class SomeInjectedClass {
+
+ @Inject
+ @Named("field1")
+ private Object field;
+ private Object c1;
+ private Object c2;
+
+ @Inject
+ public SomeInjectedClass(@Named("constructor1") Object c1, @Named("constructor2") Object c2) {
+
+ this.c1 = c1;
+ this.c2 = c2;
+ }
+
+ @Inject
+ public void setVal1(@Named("val1") Object v1) {
+
+ }
+
+ @Inject
+ public void setVal2(@Named("val2") Object v2) {
+
+ }
+ }
+}
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/DefaultFiltersTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/DefaultFiltersTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/DefaultFiltersTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/DefaultFiltersTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice.web;
+
+import com.google.inject.Key;
+import org.apache.shiro.web.filter.mgt.DefaultFilter;
+import org.junit.Test;
+
+import javax.servlet.Filter;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.EnumSet;
+
+import static org.junit.Assert.fail;
+
+public class DefaultFiltersTest {
+ @Test
+ public void checkDefaultFilters() throws Exception {
+ EnumSet<DefaultFilter> defaultFilters = EnumSet.allOf(DefaultFilter.class);
+ for(Field field: ShiroWebModule.class.getFields()) {
+ if(Modifier.isStatic(field.getModifiers()) && Key.class.isAssignableFrom(field.getType())) {
+ Class<? extends Filter> filterType = ((Key)field.get(null)).getTypeLiteral().getRawType();
+ boolean found = false;
+ for(DefaultFilter filter: defaultFilters) {
+ if(filterType.equals(filter.getFilterClass())) {
+ found = true;
+ defaultFilters.remove(filter);
+ break;
+ }
+ }
+ if(!found) {
+ fail("Guice ShiroWebModule containts a default filter that Shiro proper does not. (" + filterType.getName() + ")");
+ }
+ }
+ }
+ if(!defaultFilters.isEmpty()) {
+ fail("Guice ShiroWebModule is missing one or more filters. " + defaultFilters);
+ }
+ }
+
+}
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/FilterChainResolverProviderTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/FilterChainResolverProviderTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/FilterChainResolverProviderTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/FilterChainResolverProviderTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice.web;
+
+import com.google.inject.Injector;
+import com.google.inject.Key;
+import com.google.inject.name.Names;
+import com.google.inject.spi.Dependency;
+import org.apache.shiro.util.PatternMatcher;
+import org.apache.shiro.web.filter.mgt.FilterChainResolver;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.servlet.Filter;
+import java.lang.reflect.Field;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+import static org.easymock.EasyMock.createMock;
+import static org.junit.Assert.*;
+
+/**
+ * This test relies on the internal structure of FilterChainResolver in order to check that it got created correctly.
+ */
+public class FilterChainResolverProviderTest {
+
+ private Map<String, Key<? extends Filter>[]> chains;
+ private Key<? extends Filter> key1a;
+ private Key<? extends Filter> key1b;
+ private Key<? extends Filter> key1c;
+ private Key<? extends Filter> key2a;
+ private FilterChainResolverProvider underTest;
+
+ @Before
+ public void setup() {
+ chains = new LinkedHashMap<String, Key<? extends Filter>[]>();
+
+ key1a = Key.get(Filter.class, Names.named("key1a"));
+ key1b = Key.get(Filter.class, Names.named("key1b"));
+ key1c = Key.get(Filter.class, Names.named("key1c"));
+ key2a = Key.get(Filter.class, Names.named("key2a"));
+
+ chains.put("one", new Key[]{key1a, key1b, key1c});
+ chains.put("two", new Key[]{key2a});
+
+ underTest = new FilterChainResolverProvider(chains);
+ }
+
+ @Test
+ public void testGetDependencies() throws Exception {
+
+ Set<Dependency<?>> dependencySet = underTest.getDependencies();
+ assertEquals(4, dependencySet.size());
+
+ assertTrue("Dependency set doesn't contain key1a.", dependencySet.contains(Dependency.get(key1a)));
+ assertTrue("Dependency set doesn't contain key1b.", dependencySet.contains(Dependency.get(key1b)));
+ assertTrue("Dependency set doesn't contain key1c.", dependencySet.contains(Dependency.get(key1c)));
+ assertTrue("Dependency set doesn't contain key2a.", dependencySet.contains(Dependency.get(key2a)));
+ }
+
+
+ @Test
+ public void testGet() throws Exception {
+
+ Injector injector = createMock(Injector.class);
+ PatternMatcher patternMatcher = createMock(PatternMatcher.class);
+
+ underTest.injector = injector;
+ underTest.setPatternMatcher(patternMatcher);
+
+ FilterChainResolver resolver = underTest.get();
+
+ Field chainsField = SimpleFilterChainResolver.class.getDeclaredField("chains");
+ chainsField.setAccessible(true);
+ Field injectorField = SimpleFilterChainResolver.class.getDeclaredField("injector");
+ injectorField.setAccessible(true);
+ Field patternMatcherField = SimpleFilterChainResolver.class.getDeclaredField("patternMatcher");
+ patternMatcherField.setAccessible(true);
+
+ assertSame(chains, chainsField.get(resolver));
+ assertSame(injector, injectorField.get(resolver));
+ assertSame(patternMatcher, patternMatcherField.get(resolver));
+ }
+}
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/FilterConfigTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/FilterConfigTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/FilterConfigTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/FilterConfigTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice.web;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.Provides;
+import org.apache.shiro.guice.ShiroModuleTest;
+import org.apache.shiro.web.filter.mgt.FilterChainResolver;
+import org.apache.shiro.web.util.WebUtils;
+import org.junit.Test;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.assertNotNull;
+
+public class FilterConfigTest {
+ private FilterChainResolver setupResolver() {
+ final ShiroModuleTest.MockRealm mockRealm = createMock(ShiroModuleTest.MockRealm.class);
+ ServletContext servletContext = createMock(ServletContext.class);
+
+ Injector injector = Guice.createInjector(new ShiroWebModule(servletContext) {
+ @Override
+ protected void configureShiroWeb() {
+ bindRealm().to(ShiroModuleTest.MockRealm.class);
+
+ addFilterChain("/index.html", AUTHC_BASIC);
+ addFilterChain("/index2.html", config(PERMS, "permission"));
+ }
+
+ @Provides
+ public ShiroModuleTest.MockRealm createRealm() {
+ return mockRealm;
+ }
+ });
+ GuiceShiroFilter filter = injector.getInstance(GuiceShiroFilter.class);
+ return filter.getFilterChainResolver();
+ }
+
+ @Test
+ public void testSimple() throws Exception {
+ FilterChainResolver resolver = setupResolver();
+ HttpServletResponse response = createNiceMock(HttpServletResponse.class);
+ FilterChain chain = createNiceMock(FilterChain.class);
+ HttpServletRequest request = createMockRequest("/index.html");
+
+ FilterChain resolved = resolver.getChain(request, response, chain);
+ assertNotNull(resolved);
+ verify(request);
+ }
+
+ @Test
+ public void testWithConfig() throws Exception {
+ FilterChainResolver resolver = setupResolver();
+ HttpServletResponse response = createNiceMock(HttpServletResponse.class);
+ FilterChain chain = createNiceMock(FilterChain.class);
+ HttpServletRequest request = createMockRequest("/index2.html");
+
+ FilterChain resolved = resolver.getChain(request, response, chain);
+ assertNotNull(resolved);
+ verify(request);
+ }
+
+ private HttpServletRequest createMockRequest(String path) {
+ HttpServletRequest request = createNiceMock(HttpServletRequest.class);
+
+ expect(request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)).andReturn(null).anyTimes();
+ expect(request.getContextPath()).andReturn("");
+ expect(request.getRequestURI()).andReturn(path);
+ replay(request);
+ return request;
+ }
+
+}
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/GuiceShiroFilterTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/GuiceShiroFilterTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/GuiceShiroFilterTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/GuiceShiroFilterTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice.web;
+
+import com.google.inject.spi.InjectionPoint;
+import org.apache.shiro.web.filter.mgt.FilterChainResolver;
+import org.apache.shiro.web.mgt.WebSecurityManager;
+import org.junit.Test;
+
+import static org.easymock.EasyMock.createMock;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
+
+public class GuiceShiroFilterTest {
+
+ @Test
+ public void ensureInjectable() {
+ try {
+ InjectionPoint ip = InjectionPoint.forConstructorOf(GuiceShiroFilter.class);
+ } catch (Exception e) {
+ fail("Could not create constructor injection point.");
+ }
+ }
+
+ @Test
+ public void testConstructor() {
+ WebSecurityManager securityManager = createMock(WebSecurityManager.class);
+ FilterChainResolver filterChainResolver = createMock(FilterChainResolver.class);
+
+ GuiceShiroFilter underTest = new GuiceShiroFilter(securityManager, filterChainResolver);
+
+ assertSame(securityManager, underTest.getSecurityManager());
+ assertSame(filterChainResolver, underTest.getFilterChainResolver());
+ }
+}
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/PathMatchingFilterProviderTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/PathMatchingFilterProviderTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/PathMatchingFilterProviderTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/PathMatchingFilterProviderTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice.web;
+
+import com.google.inject.Key;
+import org.apache.shiro.web.filter.PathMatchingFilter;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.easymock.EasyMock.*;
+
+public class PathMatchingFilterProviderTest {
+ @Test
+ public void testPostProcess() {
+ PathMatchingFilter filter = createMock(PathMatchingFilter.class);
+
+ expect(filter.processPathConfig("/1", "first")).andReturn(filter);
+ expect(filter.processPathConfig("/2", "second")).andReturn(filter);
+
+ replay(filter);
+
+ Map<String, String> pathConfigMap = new HashMap<String, String>();
+ pathConfigMap.put("/1", "first");
+ pathConfigMap.put("/2", "second");
+
+ PathMatchingFilterProvider underTest = new PathMatchingFilterProvider(Key.get(PathMatchingFilter.class), pathConfigMap);
+
+ underTest.postProcess(filter);
+
+ verify(filter);
+ }
+
+
+}
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/ShiroWebModuleTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/ShiroWebModuleTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/ShiroWebModuleTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/ShiroWebModuleTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice.web;
+
+import com.google.inject.Guice;
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import com.google.inject.Provides;
+import com.google.inject.binder.AnnotatedBindingBuilder;
+import org.apache.shiro.guice.ShiroModuleTest;
+import org.apache.shiro.env.Environment;
+import org.apache.shiro.mgt.SecurityManager;
+import org.apache.shiro.realm.Realm;
+import org.apache.shiro.session.mgt.SessionManager;
+import org.apache.shiro.web.env.WebEnvironment;
+import org.apache.shiro.web.filter.mgt.FilterChainResolver;
+import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
+import org.apache.shiro.web.mgt.WebSecurityManager;
+import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
+import org.junit.Test;
+
+import javax.inject.Named;
+import javax.servlet.ServletContext;
+import java.util.Collection;
+
+import static org.easymock.EasyMock.createMock;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+public class ShiroWebModuleTest {
+
+
+ @Test
+ public void basicInstantiation() {
+ final ShiroModuleTest.MockRealm mockRealm = createMock(ShiroModuleTest.MockRealm.class);
+ ServletContext servletContext = createMock(ServletContext.class);
+
+ Injector injector = Guice.createInjector(new ShiroWebModule(servletContext) {
+ @Override
+ protected void configureShiroWeb() {
+ bindRealm().to(ShiroModuleTest.MockRealm.class);
+ expose(SessionManager.class);
+ }
+
+ @Provides
+ public ShiroModuleTest.MockRealm createRealm() {
+ return mockRealm;
+ }
+
+ });
+ // we're not getting a WebSecurityManager here b/c it's not exposed. There didn't seem to be a good reason to
+ // expose it outside of the Shiro module.
+ SecurityManager securityManager = injector.getInstance(SecurityManager.class);
+ assertNotNull(securityManager);
+ assertTrue(securityManager instanceof WebSecurityManager);
+ SessionManager sessionManager = injector.getInstance(SessionManager.class);
+ assertNotNull(sessionManager);
+ assertTrue(sessionManager instanceof DefaultWebSessionManager);
+ assertTrue(((DefaultWebSecurityManager)securityManager).getSessionManager() instanceof DefaultWebSessionManager);
+ }
+
+ @Test
+ public void testBindGuiceFilter() throws Exception {
+
+ }
+
+ @Test
+ public void testBindWebSecurityManager() throws Exception {
+ final ShiroModuleTest.MockRealm mockRealm = createMock(ShiroModuleTest.MockRealm.class);
+ ServletContext servletContext = createMock(ServletContext.class);
+
+ Injector injector = Guice.createInjector(new ShiroWebModule(servletContext) {
+ @Override
+ protected void configureShiroWeb() {
+ bindRealm().to(ShiroModuleTest.MockRealm.class);
+ expose(WebSecurityManager.class);
+ }
+
+ @Provides
+ public ShiroModuleTest.MockRealm createRealm() {
+ return mockRealm;
+ }
+
+ @Override
+ protected void bindWebSecurityManager(AnnotatedBindingBuilder<? super WebSecurityManager> bind) {
+ bind.to(MyDefaultWebSecurityManager.class);
+ }
+ });
+ SecurityManager securityManager = injector.getInstance(SecurityManager.class);
+ assertNotNull(securityManager);
+ assertTrue(securityManager instanceof MyDefaultWebSecurityManager);
+ WebSecurityManager webSecurityManager = injector.getInstance(WebSecurityManager.class);
+ assertNotNull(webSecurityManager);
+ assertTrue(webSecurityManager instanceof MyDefaultWebSecurityManager);
+
+ }
+
+ @Test
+ public void testBindWebEnvironment() throws Exception {
+ final ShiroModuleTest.MockRealm mockRealm = createMock(ShiroModuleTest.MockRealm.class);
+ ServletContext servletContext = createMock(ServletContext.class);
+
+ Injector injector = Guice.createInjector(new ShiroWebModule(servletContext) {
+ @Override
+ protected void configureShiroWeb() {
+ bindRealm().to(ShiroModuleTest.MockRealm.class);
+ expose(WebEnvironment.class);
+ expose(Environment.class);
+ }
+
+ @Provides
+ public ShiroModuleTest.MockRealm createRealm() {
+ return mockRealm;
+ }
+
+ @Override
+ protected void bindWebEnvironment(AnnotatedBindingBuilder<? super WebEnvironment> bind) {
+ bind.to(MyWebEnvironment.class);
+ }
+ });
+ Environment environment = injector.getInstance(Environment.class);
+ assertNotNull(environment);
+ assertTrue(environment instanceof MyWebEnvironment);
+ WebEnvironment webEnvironment = injector.getInstance(WebEnvironment.class);
+ assertNotNull(webEnvironment);
+ assertTrue(webEnvironment instanceof MyWebEnvironment);
+ }
+
+ public static class MyDefaultWebSecurityManager extends DefaultWebSecurityManager {
+ @Inject
+ public MyDefaultWebSecurityManager(Collection<Realm> realms) {
+ super(realms);
+ }
+ }
+
+ public static class MyDefaultWebSessionManager extends DefaultWebSessionManager {
+ }
+
+ public static class MyWebEnvironment extends WebGuiceEnvironment {
+ @Inject
+ MyWebEnvironment(FilterChainResolver filterChainResolver, @Named(ShiroWebModule.NAME) ServletContext servletContext, WebSecurityManager securityManager) {
+ super(filterChainResolver, servletContext, securityManager);
+ }
+ }
+}
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/SimpleFilterChainResolverTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/SimpleFilterChainResolverTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/SimpleFilterChainResolverTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/SimpleFilterChainResolverTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice.web;
+
+import com.google.inject.Injector;
+import com.google.inject.Key;
+import com.google.inject.name.Names;
+import org.apache.shiro.util.PatternMatcher;
+import org.apache.shiro.web.util.WebUtils;
+import org.easymock.IMocksControl;
+import org.junit.Test;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.assertNull;
+
+
+/**
+ * Note that this test is highly dependent on the implementation of SimpleFilterChain. There's really no way around that
+ * that I can see. We determine that the resolver has created it correctly by observing it's behavior.
+ */
+public class SimpleFilterChainResolverTest {
+ @Test
+ public void testGetChain() throws Exception {
+ // test that it uses the pattern matcher - check
+ // test that the FIRST chain found is the one that gets returned - check
+ // test that the chain returned actually contains the filters returned by the injector - check
+ // test that the keys specified for the chain are requested from the injector - check
+ // test that filters are looked up lazily - check
+
+ IMocksControl ctrl = createStrictControl();
+
+ Injector injector = ctrl.createMock(Injector.class);
+ Map<String, Key<? extends Filter>[]> chainMap = new LinkedHashMap<String, Key<? extends Filter>[]>();
+
+ final String chainOne = "one";
+ final String chainTwo = "two";
+ final String chainThree = "three";
+
+ final Key<? extends Filter> key1a = Key.get(Filter.class, Names.named("key1a"));
+ final Key<? extends Filter> key1b = Key.get(Filter.class, Names.named("key1b"));
+ final Key<? extends Filter> key2a = Key.get(Filter.class, Names.named("key2a"));
+ final Key<? extends Filter> key2b = Key.get(Filter.class, Names.named("key2b"));
+ final Key<? extends Filter> key3a = Key.get(Filter.class, Names.named("key3a"));
+ final Key<? extends Filter> key3b = Key.get(Filter.class, Names.named("key3b"));
+
+ chainMap.put(chainOne, new Key[]{key1a, key1b});
+ chainMap.put(chainTwo, new Key[]{key2a, key2b});
+ chainMap.put(chainThree, new Key[]{key3a, key3b});
+
+ PatternMatcher patternMatcher = ctrl.createMock(PatternMatcher.class);
+ ServletRequest request = ctrl.createMock(HttpServletRequest.class);
+ ServletResponse response = ctrl.createMock(HttpServletResponse.class);
+ FilterChain originalChain = ctrl.createMock(FilterChain.class);
+
+ expect(request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)).andReturn("/context");
+ expect(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).andReturn("/mychain");
+
+ expect(request.getCharacterEncoding()).andStubReturn(null);
+
+ expect(patternMatcher.matches(chainOne, "/mychain")).andReturn(false);
+ expect(patternMatcher.matches(chainTwo, "/mychain")).andReturn(true);
+
+ Filter filter2a = ctrl.createMock(Filter.class);
+ Filter filter2b = ctrl.createMock(Filter.class);
+
+ expect(injector.getInstance(key2a)).andReturn(filter2a);
+ filter2a.doFilter(same(request), same(response), anyObject(FilterChain.class));
+ expect(injector.getInstance(key2b)).andReturn(filter2b);
+ filter2b.doFilter(same(request), same(response), anyObject(FilterChain.class));
+ originalChain.doFilter(request, response);
+
+ ctrl.replay();
+
+ SimpleFilterChainResolver underTest = new SimpleFilterChainResolver(chainMap, injector, patternMatcher);
+
+ FilterChain got = underTest.getChain(request, response, originalChain);
+
+ got.doFilter(request, response);
+ got.doFilter(request, response);
+ got.doFilter(request, response);
+
+ ctrl.verify();
+
+ ctrl.reset();
+
+ expect(request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)).andReturn("/context");
+ expect(request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE)).andReturn("/nochain");
+
+ expect(request.getCharacterEncoding()).andStubReturn(null);
+
+ expect(patternMatcher.matches(chainOne, "/nochain")).andReturn(false);
+ expect(patternMatcher.matches(chainTwo, "/nochain")).andReturn(false);
+ expect(patternMatcher.matches(chainThree, "/nochain")).andReturn(false);
+
+ ctrl.replay();
+
+ assertNull("Expected no chain to match, did not get a null value in return.", underTest.getChain(request, response, originalChain));
+
+ ctrl.verify();
+ }
+}
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/SimpleFilterChainTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/SimpleFilterChainTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/SimpleFilterChainTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/SimpleFilterChainTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice.web;
+
+import com.google.common.collect.Iterators;
+import org.easymock.Capture;
+import org.easymock.IMocksControl;
+import org.junit.Test;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import static org.easymock.EasyMock.*;
+
+public class SimpleFilterChainTest {
+ @Test
+ public void testDoFilter() throws Exception {
+ IMocksControl ctrl = createStrictControl();
+
+ FilterChain originalChain = ctrl.createMock(FilterChain.class);
+ Filter filter1 = ctrl.createMock("filter1", Filter.class);
+ Filter filter2 = ctrl.createMock("filter2", Filter.class);
+
+ ServletRequest request = ctrl.createMock(ServletRequest.class);
+ ServletResponse response = ctrl.createMock(ServletResponse.class);
+
+ Capture<FilterChain> fc1 = new Capture<FilterChain>();
+ Capture<FilterChain> fc2 = new Capture<FilterChain>();
+ filter1.doFilter(same(request), same(response), and(anyObject(FilterChain.class), capture(fc1)));
+ filter2.doFilter(same(request), same(response), and(anyObject(FilterChain.class), capture(fc2)));
+ originalChain.doFilter(request, response);
+
+ ctrl.replay();
+
+ SimpleFilterChain underTest = new SimpleFilterChain(originalChain, Iterators.forArray(filter1, filter2));
+
+ // all we actually care about is that, if we keep calling the filter chain, everything is called in the right
+ // order - we don't care what fc actually contains
+ underTest.doFilter(request, response);
+ fc1.getValue().doFilter(request, response);
+ fc2.getValue().doFilter(request, response);
+
+ ctrl.verify();
+ }
+}
Added: shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/WebGuiceEnvironmentTest.java
URL: http://svn.apache.org/viewvc/shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/WebGuiceEnvironmentTest.java?rev=1147810&view=auto
==============================================================================
--- shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/WebGuiceEnvironmentTest.java (added)
+++ shiro/trunk/support/guice/src/test/java/org/apache/shiro/guice/web/WebGuiceEnvironmentTest.java Mon Jul 18 11:10:51 2011
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shiro.guice.web;
+
+import com.google.inject.spi.InjectionPoint;
+import org.apache.shiro.web.env.EnvironmentLoaderListener;
+import org.apache.shiro.web.filter.mgt.FilterChainResolver;
+import org.apache.shiro.web.mgt.WebSecurityManager;
+import org.easymock.Capture;
+import org.junit.Test;
+
+import javax.servlet.ServletContext;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
+
+public class WebGuiceEnvironmentTest {
+
+ @Test
+ public void ensureInjectable() {
+ try {
+ InjectionPoint ip = InjectionPoint.forConstructorOf(WebGuiceEnvironment.class);
+ } catch (Exception e) {
+ fail("Could not create constructor injection point.");
+ }
+ }
+
+ @Test
+ public void testConstructor() {
+ WebSecurityManager securityManager = createMock(WebSecurityManager.class);
+ FilterChainResolver filterChainResolver = createMock(FilterChainResolver.class);
+ ServletContext servletContext = createMock(ServletContext.class);
+
+ Capture<WebGuiceEnvironment> capture = new Capture<WebGuiceEnvironment>();
+ servletContext.setAttribute(eq(EnvironmentLoaderListener.ENVIRONMENT_ATTRIBUTE_KEY), and(anyObject(WebGuiceEnvironment.class), capture(capture)));
+
+ replay(servletContext, securityManager, filterChainResolver);
+
+ WebGuiceEnvironment underTest = new WebGuiceEnvironment(filterChainResolver, servletContext, securityManager);
+
+ assertSame(securityManager, underTest.getSecurityManager());
+ assertSame(filterChainResolver, underTest.getFilterChainResolver());
+ assertSame(securityManager, underTest.getWebSecurityManager());
+ assertSame(servletContext, underTest.getServletContext());
+
+ assertSame(underTest, capture.getValue());
+
+ verify(servletContext);
+ }
+}
Modified: shiro/trunk/support/pom.xml
URL: http://svn.apache.org/viewvc/shiro/trunk/support/pom.xml?rev=1147810&r1=1147809&r2=1147810&view=diff
==============================================================================
--- shiro/trunk/support/pom.xml (original)
+++ shiro/trunk/support/pom.xml Mon Jul 18 11:10:51 2011
@@ -36,6 +36,7 @@
<module>ehcache</module>
<module>quartz</module>
<module>spring</module>
+ <module>guice</module>
<module>openid4j</module>
<module>features</module>
</modules>