KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > security > AuthorizationMapTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.security;
19
20 import org.apache.activemq.command.ActiveMQQueue;
21 import org.apache.activemq.command.ActiveMQTempQueue;
22 import org.apache.activemq.jaas.GroupPrincipal;
23
24 import java.util.*;
25
26 import junit.framework.TestCase;
27
28 /**
29  *
30  * @version $Revision: 480575 $
31  */

32 public class AuthorizationMapTest extends TestCase {
33     static final GroupPrincipal guests = new GroupPrincipal("guests");
34     static final GroupPrincipal users = new GroupPrincipal("users");
35     static final GroupPrincipal admins = new GroupPrincipal("admins");
36     static final GroupPrincipal tempDestinationAdmins = new GroupPrincipal("tempDestAdmins");
37
38     public void testAuthorizationMap() {
39         AuthorizationMap map = createAuthorizationMap();
40
41         Set readACLs = map.getReadACLs(new ActiveMQQueue("USERS.FOO.BAR"));
42         assertEquals("set size: " + readACLs, 2, readACLs.size());
43         assertTrue("Contains users group", readACLs.contains(admins));
44         assertTrue("Contains users group", readACLs.contains(users));
45         
46     }
47
48     public void testAuthorizationMapWithTempDest() {
49         AuthorizationMap map = createAuthorizationMapWithTempDest();
50
51         Set readACLs = map.getReadACLs(new ActiveMQQueue("USERS.FOO.BAR"));
52         assertEquals("set size: " + readACLs, 2, readACLs.size());
53         assertTrue("Contains users group", readACLs.contains(admins));
54         assertTrue("Contains users group", readACLs.contains(users));
55         
56         Set tempAdminACLs = map.getTempDestinationAdminACLs();
57         assertEquals("set size: " + tempAdminACLs, 1, tempAdminACLs.size());
58         assertTrue("Contains users group", tempAdminACLs.contains(tempDestinationAdmins));
59               
60     }
61     
62     protected AuthorizationMap createAuthorizationMap() {
63         DefaultAuthorizationMap answer = new DefaultAuthorizationMap();
64
65         List entries = new ArrayList();
66
67         AuthorizationEntry entry = new AuthorizationEntry();
68         entry.setGroupClass("org.apache.activemq.jaas.GroupPrincipal");
69         entry.setQueue(">");
70         try {
71             entry.setRead("admins");
72         } catch (Exception JavaDoc e) {
73             fail(e.toString());
74         }
75         
76         
77         entries.add(entry);
78         // entry using default org.apache.activemq.jaas.GroupPrincipal class
79
entry = new AuthorizationEntry();
80         entry.setQueue("USERS.>");
81         try {
82             entry.setRead("users");
83         } catch (Exception JavaDoc e) {
84             fail(e.toString());
85         }
86         entries.add(entry);
87
88         answer.setAuthorizationEntries(entries);
89
90         return answer;
91     }
92     
93     protected AuthorizationMap createAuthorizationMapWithTempDest() {
94         DefaultAuthorizationMap answer = new DefaultAuthorizationMap();
95
96         List entries = new ArrayList();
97
98         AuthorizationEntry entry = new AuthorizationEntry();
99         entry.setQueue(">");
100         try {
101             entry.setRead("admins");
102         } catch (Exception JavaDoc e) {
103             fail(e.toString());
104         }
105         entries.add(entry);
106
107         entry = new AuthorizationEntry();
108         entry.setQueue("USERS.>");
109         try {
110             entry.setRead("users");
111         } catch (Exception JavaDoc e) {
112             fail(e.toString());
113         }
114         entries.add(entry);
115
116         answer.setAuthorizationEntries(entries);
117         
118         //create entry for temporary queue
119
TempDestinationAuthorizationEntry tEntry = new TempDestinationAuthorizationEntry();
120         try {
121             tEntry.setAdmin("tempDestAdmins");
122         } catch (Exception JavaDoc e) {
123             fail(e.toString());
124         }
125         
126         answer.setTempDestinationAuthorizationEntry(tEntry);
127
128         return answer;
129     }
130
131 }
132
Popular Tags