1 16 package org.apache.naming; 17 18 import java.util.Hashtable ; 19 20 import javax.naming.Context ; 21 22 import junit.framework.Test; 23 import junit.framework.TestSuite; 24 import junit.textui.TestRunner; 25 import junit.framework.TestCase; 26 27 28 33 public class ContextAccessControllerTest extends TestCase { 34 35 public ContextAccessControllerTest(String name) { 36 super(name); 37 } 38 39 public static void main(String [] 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 testContext = null; 50 protected Context testSubContext = null; 51 protected String testToken1 = "test1"; 52 protected String testToken2 = "test2"; 53 protected String contextName = "context"; 54 protected String subName = "sub"; 55 56 public void setUp() throws Exception { 57 testContext = new SelectorContext(new Hashtable (), true); 58 testSubContext = testContext.createSubcontext("env:comp"); 59 } 60 61 public void tearDown() throws Exception { 62 testContext.destroySubcontext("env:comp"); 63 testContext = null; 64 } 65 66 public void testAccessControl() throws Exception { 67 assertTrue(ContextAccessController.checkSecurityToken(contextName, testToken1)); 69 70 ContextAccessController.setSecurityToken(contextName, testToken1); 72 assertTrue(ContextAccessController.checkSecurityToken(contextName, testToken1)); 73 assertFalse(ContextAccessController.checkSecurityToken(contextName, testToken2)); 74 75 assertTrue(ContextAccessController.checkSecurityToken(subName, testToken1)); 77 78 ContextAccessController.setSecurityToken(contextName, null); 80 assertTrue(ContextAccessController.checkSecurityToken(subName, testToken1)); 81 82 ContextAccessController.setSecurityToken(subName, testToken2); 84 assertFalse(ContextAccessController.checkSecurityToken(subName, testToken1)); 86 assertTrue(ContextAccessController.checkSecurityToken(subName, testToken2)); 87 88 ContextAccessController.unsetSecurityToken(contextName, testToken2); 90 assertFalse(ContextAccessController.checkSecurityToken(contextName, testToken2)); 91 ContextAccessController.unsetSecurityToken(contextName, null); 92 assertFalse(ContextAccessController.checkSecurityToken(contextName, testToken2)); 93 94 ContextAccessController.unsetSecurityToken(contextName, testToken1); 96 assertTrue(ContextAccessController.checkSecurityToken(contextName, testToken2)); 97 assertTrue(ContextAccessController.checkSecurityToken(contextName, null)); 98 } 99 100 public void testMutationControl() throws Exception { 101 assertTrue(ContextAccessController.isWritable(contextName)); 103 assertTrue(ContextAccessController.isWritable(subName)); 104 105 ContextAccessController.setReadOnly(contextName); 107 assertFalse(ContextAccessController.isWritable(contextName)); 108 assertTrue(ContextAccessController.isWritable(subName)); 110 ContextAccessController.setWritable(contextName, null); 112 assertTrue(ContextAccessController.isWritable(contextName)); 113 114 ContextAccessController.setSecurityToken(contextName, testToken1); 116 ContextAccessController.setReadOnly(contextName); 117 ContextAccessController.setWritable(contextName, testToken2); 119 assertFalse(ContextAccessController.isWritable(contextName)); 120 ContextAccessController.setWritable(contextName, testToken1); 122 assertTrue(ContextAccessController.isWritable(contextName)); 123 } 124 125 } 126 | Popular Tags |