KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Properties JavaDoc;
23 import java.util.Set JavaDoc;
24
25 import javax.naming.Context JavaDoc;
26 import javax.naming.NameClassPair JavaDoc;
27 import javax.naming.NamingEnumeration JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import javax.naming.directory.DirContext JavaDoc;
30 import javax.naming.directory.InitialDirContext JavaDoc;
31
32 import org.apache.activemq.command.ActiveMQDestination;
33 import org.apache.activemq.command.ActiveMQQueue;
34 import org.apache.activemq.command.ActiveMQTopic;
35 import org.apache.activemq.jaas.GroupPrincipal;
36 import org.apache.directory.server.core.configuration.StartupConfiguration;
37 import org.apache.directory.server.core.jndi.CoreContextFactory;
38 import org.springframework.context.ApplicationContext;
39 import org.springframework.context.support.ClassPathXmlApplicationContext;
40
41 import junit.framework.TestCase;
42
43 /**
44  * This test assumes setup like in file 'AMQauth.ldif'. Contents of this file is
45  * attached below in comments.
46  *
47  * @author ngcutura
48  *
49  */

50 public class LDAPAuthorizationMapTest extends TestCase {
51     private HashMap JavaDoc options;
52     private LDAPAuthorizationMap authMap;
53
54     protected void setUp() throws Exception JavaDoc {
55         super.setUp();
56
57         startLdapServer();
58
59         authMap = new LDAPAuthorizationMap();
60     }
61
62     protected void startLdapServer() throws Exception JavaDoc {
63         ApplicationContext factory = new ClassPathXmlApplicationContext("org/apache/activemq/security/ldap-spring.xml");
64         StartupConfiguration cfg = (StartupConfiguration) factory.getBean("configuration");
65         Properties JavaDoc env = (Properties JavaDoc) factory.getBean("environment");
66
67         env.setProperty(Context.PROVIDER_URL, "");
68         env.setProperty(Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class.getName());
69         env.putAll(cfg.toJndiEnvironment());
70
71         new InitialDirContext JavaDoc(env);
72     }
73
74     protected void tearDown() throws Exception JavaDoc {
75         super.tearDown();
76     }
77
78     public void testOpen() throws Exception JavaDoc {
79         DirContext JavaDoc ctx = authMap.open();
80         HashSet JavaDoc set = new HashSet JavaDoc();
81         NamingEnumeration JavaDoc list = ctx.list("ou=destinations,o=ActiveMQ,dc=example,dc=com");
82         while (list.hasMore()) {
83             NameClassPair JavaDoc ncp = (NameClassPair JavaDoc) list.next();
84             set.add(ncp.getName());
85         }
86         assertTrue(set.contains("ou=topics"));
87         assertTrue(set.contains("ou=queues"));
88     }
89
90     /*
91      * Test method for
92      * 'org.apache.activemq.security.LDAPAuthorizationMap.getAdminACLs(ActiveMQDestination)'
93      */

94     public void testGetAdminACLs() {
95         ActiveMQDestination q1 = new ActiveMQQueue("queue1");
96         Set JavaDoc aclsq1 = authMap.getAdminACLs(q1);
97         assertEquals(1, aclsq1.size());
98         assertTrue(aclsq1.contains(new GroupPrincipal("role1")));
99
100         ActiveMQDestination t1 = new ActiveMQTopic("topic1");
101         Set JavaDoc aclst1 = authMap.getAdminACLs(t1);
102         assertEquals(1, aclst1.size());
103         assertTrue(aclst1.contains(new GroupPrincipal("role1")));
104     }
105
106     /*
107      * Test method for
108      * 'org.apache.activemq.security.LDAPAuthorizationMap.getReadACLs(ActiveMQDestination)'
109      */

110     public void testGetReadACLs() {
111         ActiveMQDestination q1 = new ActiveMQQueue("queue1");
112         Set JavaDoc aclsq1 = authMap.getReadACLs(q1);
113         assertEquals(1, aclsq1.size());
114         assertTrue(aclsq1.contains(new GroupPrincipal("role1")));
115
116         ActiveMQDestination t1 = new ActiveMQTopic("topic1");
117         Set JavaDoc aclst1 = authMap.getReadACLs(t1);
118         assertEquals(1, aclst1.size());
119         assertTrue(aclst1.contains(new GroupPrincipal("role2")));
120     }
121
122     /*
123      * Test method for
124      * 'org.apache.activemq.security.LDAPAuthorizationMap.getWriteACLs(ActiveMQDestination)'
125      */

126     public void testGetWriteACLs() {
127         ActiveMQDestination q1 = new ActiveMQQueue("queue1");
128         Set JavaDoc aclsq1 = authMap.getWriteACLs(q1);
129         assertEquals(2, aclsq1.size());
130         assertTrue(aclsq1.contains(new GroupPrincipal("role1")));
131         assertTrue(aclsq1.contains(new GroupPrincipal("role2")));
132
133         ActiveMQDestination t1 = new ActiveMQTopic("topic1");
134         Set JavaDoc aclst1 = authMap.getWriteACLs(t1);
135         assertEquals(1, aclst1.size());
136         assertTrue(aclst1.contains(new GroupPrincipal("role3")));
137     }
138
139 }
140
Popular Tags