1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.security;
20  
21  import javax.faces.el.EvaluationException;
22  
23  import junit.framework.Test;
24  import junit.framework.TestSuite;
25  
26  import org.apache.shale.test.base.AbstractJsfTestCase;
27  
28  /**
29   * @author cagatay (latest modification by $Author: lu4242 $)
30   * @version $Revision: 1099917 $ $Date: 2011-05-05 13:57:45 -0500 (Thu, 05 May 2011) $
31   */
32  public class SecurityContextVariableResolverTest extends AbstractJsfTestCase{
33  
34      protected SecurityContextVariableResolver resolver;
35      
36      public SecurityContextVariableResolverTest(String testName) {
37          super(testName);
38      }
39      
40      public void setUp() throws Exception{
41          super.setUp();
42          resolver = new SecurityContextVariableResolver(null);
43      }
44      
45      public void tearDown() throws Exception{
46          super.tearDown();
47          resolver = null;
48      }
49      
50      public static Test suite() {
51          return new TestSuite(SecurityContextVariableResolverTest.class);
52      }
53      
54      public void testSecurityContextImplIsDefaultImplWhenNoCustomImplIsConfigured() {
55          SecurityContext securityContext = (SecurityContext)resolver.resolveVariable(facesContext, "securityContext");
56          assertTrue(securityContext instanceof SecurityContextImpl);
57      }
58      
59      public void testSecurityContextImplIsCustomWhenCustomImplIsConfigured() {
60          servletContext.addInitParameter("org.apache.myfaces.SECURITY_CONTEXT", "org.apache.myfaces.custom.security.CustomSecurityContextImpl");
61          SecurityContext securityContext = (SecurityContext)resolver.resolveVariable(facesContext, "securityContext");
62          assertFalse(securityContext instanceof SecurityContextImpl);
63          assertTrue(securityContext instanceof CustomSecurityContextImpl);
64      }
65      
66      public void testExceptionMustBeThrownWhenAnInvalidImplIsConfigured() {
67          servletContext.addInitParameter("org.apache.myfaces.SECURITY_CONTEXT", "this.class.does.not.exist");
68          try {
69              resolver.resolveVariable(facesContext, "securityContext");
70          }catch(EvaluationException evaluationException) {
71              return;
72          }
73          fail();
74          
75      }
76  }