KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > ContextAccessControllerTest


1 /*
2  * Copyright 2004 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 package org.apache.naming;
17
18 import java.util.Hashtable JavaDoc;
19
20 import javax.naming.Context JavaDoc;
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24 import junit.textui.TestRunner;
25 import junit.framework.TestCase;
26
27
28 /**
29  * Unit tests for {@link ContextAccessController}.
30  *
31  * @version $Revision: 125387 $ $Date: 2003/11/30 05:36:07 $
32  */

33 public class ContextAccessControllerTest extends TestCase {
34     
35     public ContextAccessControllerTest(String JavaDoc name) {
36         super(name);
37     }
38
39     public static void main(String JavaDoc[] args) {
40         TestRunner.run(suite());
41     }
42
43     public static Test suite() {
44         TestSuite suite = new TestSuite(ContextAccessControllerTest.class);
45         suite.setName("ContextAccessController Tests");
46         return suite;
47     }
48     
49     protected Context JavaDoc testContext = null;
50     protected Context JavaDoc testSubContext = null;
51     protected String JavaDoc testToken1 = "test1";
52     protected String JavaDoc testToken2 = "test2";
53     protected String JavaDoc contextName = "context";
54     protected String JavaDoc subName = "sub";
55     
56     public void setUp() throws Exception JavaDoc {
57         testContext = new SelectorContext(new Hashtable JavaDoc(), true);
58         testSubContext = testContext.createSubcontext("env:comp");
59     }
60     
61     public void tearDown() throws Exception JavaDoc {
62         testContext.destroySubcontext("env:comp");
63         testContext = null;
64     }
65     
66    public void testAccessControl() throws Exception JavaDoc {
67        // no token set, check should return true for any token
68
assertTrue(ContextAccessController.checkSecurityToken(contextName, testToken1));
69        
70        // set token and check access
71
ContextAccessController.setSecurityToken(contextName, testToken1);
72        assertTrue(ContextAccessController.checkSecurityToken(contextName, testToken1));
73        assertFalse(ContextAccessController.checkSecurityToken(contextName, testToken2));
74       
75        // no token set, return true
76
assertTrue(ContextAccessController.checkSecurityToken(subName, testToken1));
77        
78        // set null token, should have no effect
79
ContextAccessController.setSecurityToken(contextName, null);
80        assertTrue(ContextAccessController.checkSecurityToken(subName, testToken1));
81        
82        // set valid token
83
ContextAccessController.setSecurityToken(subName, testToken2);
84        // parent's token should not work
85
assertFalse(ContextAccessController.checkSecurityToken(subName, testToken1));
86        assertTrue(ContextAccessController.checkSecurityToken(subName, testToken2));
87        
88        // try to unset using wrong token, should have no effect
89
ContextAccessController.unsetSecurityToken(contextName, testToken2);
90        assertFalse(ContextAccessController.checkSecurityToken(contextName, testToken2));
91        ContextAccessController.unsetSecurityToken(contextName, null);
92        assertFalse(ContextAccessController.checkSecurityToken(contextName, testToken2));
93        
94        // unset token
95
ContextAccessController.unsetSecurityToken(contextName, testToken1);
96        assertTrue(ContextAccessController.checkSecurityToken(contextName, testToken2));
97        assertTrue(ContextAccessController.checkSecurityToken(contextName, null));
98    }
99    
100    public void testMutationControl() throws Exception JavaDoc {
101        // verify that contexts are writable by default
102
assertTrue(ContextAccessController.isWritable(contextName));
103        assertTrue(ContextAccessController.isWritable(subName));
104        
105        // make context read only without setting token
106
ContextAccessController.setReadOnly(contextName);
107        assertFalse(ContextAccessController.isWritable(contextName));
108        // property is not inherited, however, by subcontext
109
assertTrue(ContextAccessController.isWritable(subName));
110        // can use any token value to make writable
111
ContextAccessController.setWritable(contextName, null);
112        assertTrue(ContextAccessController.isWritable(contextName));
113        
114        // set token, so setWritable will need correct token
115
ContextAccessController.setSecurityToken(contextName, testToken1);
116        ContextAccessController.setReadOnly(contextName);
117        // wrong token will not work
118
ContextAccessController.setWritable(contextName, testToken2);
119        assertFalse(ContextAccessController.isWritable(contextName));
120        // now here's the ticket...
121
ContextAccessController.setWritable(contextName, testToken1);
122        assertTrue(ContextAccessController.isWritable(contextName));
123    }
124     
125 }
126
Popular Tags