View Javadoc

1   /*
2    * Copyright 2004-2006 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.myfaces.shared.util;
19  
20  import junit.framework.Test;
21  import org.apache.myfaces.test.base.AbstractJsfTestCase;
22  
23  import javax.crypto.SecretKey;
24  
25  public class SecretKeyCacheTest extends AbstractJsfTestCase
26  {
27  
28      public SecretKeyCacheTest(String name)
29      {
30          super(name);
31      }
32  
33      // No longer necessary using junit 4 to run tests
34      //public static Test suite() {
35      //    return null; // keep this method or maven won't run it
36      //}
37      
38      public void setUp() throws Exception
39      {
40          super.setUp();
41          
42          servletContext.addInitParameter(StateUtils.INIT_SECRET, 
43                  AbstractStateUtilsTest.BASE64_KEY_SIZE_8);
44          servletContext.addInitParameter(StateUtils.INIT_MAC_SECRET, AbstractStateUtilsTest.BASE64_KEY_SIZE_8);
45      }
46  
47      public void testDefaultAlgorithmUse(){
48          
49          StateUtils.initSecret(servletContext);
50          
51          SecretKey secretKey = (SecretKey) servletContext.getAttribute(StateUtils.INIT_SECRET_KEY_CACHE);
52          
53          assertTrue("Making sure MyFaces uses the " +
54                  "default algorithm when one is not specified",
55                  StateUtils.DEFAULT_ALGORITHM.equals(secretKey.getAlgorithm()));
56          
57      }
58      
59      public void testInitFacesWithoutCache(){
60  
61          servletContext.addInitParameter(StateUtils.INIT_SECRET_KEY_CACHE, "false");
62          
63          StateUtils.initSecret(servletContext);
64  
65          Object object = servletContext.getAttribute(StateUtils.INIT_SECRET_KEY_CACHE);
66          
67          assertNull("Making sure StateUtils.initSecret does not create a SecretKey", object);
68          
69      }
70      
71      public void testInitFacesWithCache(){
72          
73          StateUtils.initSecret(servletContext);
74          
75          Object object = servletContext.getAttribute(StateUtils.INIT_SECRET_KEY_CACHE);
76          
77          assertFalse("Making sure StateUtils.initSecret() puts an object in application scope", 
78                  object == null);
79          
80          assertTrue("Making sure StateUtils.initSecret() is creating a SecretKey", 
81                  object instanceof SecretKey);
82          
83      }
84      
85  }