KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > security > B_AccessControl


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id:
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.security;
27
28
29 import org.objectweb.jonas.jtests.beans.secured.BaseS;
30 import org.objectweb.jonas.jtests.util.JTestCase;
31 import org.objectweb.security.context.SecurityContext;
32 import org.objectweb.security.context.SecurityCurrent;
33
34
35 /**
36  * Security Management common tests for all type of beans (Entity/Session)
37  *
38  * @author Ph.Coq, Ph.Durieux
39  *
40  */

41
42 public abstract class B_AccessControl extends JTestCase {
43
44     protected static String JavaDoc PRINCIPAL1_NAME = "principal1";
45     protected static String JavaDoc PRINCIPAL3_NAME = "principal3";
46     protected static String JavaDoc ROLE1_NAME = "baserole1";
47     protected static String JavaDoc ROLE2_NAME = "baserole2";
48
49     protected static SecurityCurrent current = null;
50     protected static SecurityContext principal1 = null;
51     protected static SecurityContext principal2 = null;
52     protected static SecurityContext principal3 = null;
53     protected static SecurityContext principal4 = null;
54
55
56     public B_AccessControl(String JavaDoc name) {
57         super(name);
58     }
59
60     /**
61      * init environment:
62      * - load beans
63      */

64     protected void setUp() {
65         super.setUp();
66         if (current == null) {
67             current = SecurityCurrent.getCurrent();
68             principal1 = new SecurityContext("principal1");
69             principal2 = new SecurityContext("principal2");
70             String JavaDoc[] roles3 = new String JavaDoc[]{"role1", "role3"};
71             principal3 = new SecurityContext(PRINCIPAL3_NAME, roles3);
72             String JavaDoc[] roles4 = new String JavaDoc[]{"role2"};
73             principal4 = new SecurityContext("principal4", roles4);
74         }
75     }
76
77     public abstract BaseS getBaseS(String JavaDoc name) throws Exception JavaDoc;
78
79     /**
80      * test getCallerPrincipal.
81      * The Principal must be propagated.
82      */

83     public void testGetCallerPrincipal() throws Exception JavaDoc {
84         current.setSecurityContext(principal1);
85         BaseS sl = getBaseS("un");
86         assertEquals(PRINCIPAL1_NAME, sl.getPrincipalName());
87         sl.remove();
88     }
89
90     /**
91      * test isCallerInRole.
92      * principal1 = role1
93      * principal2 = role2
94      */

95     public void testIsCallerInRole() throws Exception JavaDoc {
96         current.setSecurityContext(principal1);
97         BaseS sl = getBaseS("deux");
98         assertTrue(sl.isCallerInRole(ROLE1_NAME) == true);
99         assertTrue(sl.isCallerInRole(ROLE2_NAME) == false);
100         sl.remove();
101     }
102
103
104  
105
106     /**
107      * test basic method accept
108      */

109     public void testBasicMethodAccept() throws Exception JavaDoc {
110         current.setSecurityContext(principal2);
111         BaseS sl = getBaseS("quatre");
112         sl.simpleMethod();
113         sl.remove();
114     }
115
116   
117    
118
119     /**
120      * test principal propagation from bean to bean
121      */

122     public void testBeanToBeanPropagation() throws Exception JavaDoc {
123         current.setSecurityContext(principal1);
124         BaseS sl = getBaseS("sept");
125         assertEquals(PRINCIPAL1_NAME, sl.getPrincipalNameOfAnotherBean());
126         sl.remove();
127     }
128
129  
130   
131     
132 }
133
Popular Tags