KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > impl > LDAPUserTest


1 /*
2  * Copyright 1999-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  */

17
18 package org.apache.lenya.ac.impl;
19
20 import java.io.File JavaDoc;
21
22 import org.apache.avalon.framework.configuration.ConfigurationException;
23 import org.apache.lenya.ac.AccessControlException;
24 import org.apache.lenya.ac.User;
25 import org.apache.lenya.ac.UserType;
26 import org.apache.lenya.ac.file.FileAccreditableManager;
27 import org.apache.lenya.ac.file.FileGroup;
28 import org.apache.lenya.ac.file.FileRole;
29 import org.apache.lenya.ac.file.FileUserManager;
30 import org.apache.lenya.ac.ldap.LDAPUser;
31 import org.apache.lenya.cms.PublicationHelper;
32 import org.apache.lenya.cms.publication.Publication;
33 import org.apache.lenya.cms.publication.PublicationException;
34 import org.apache.lenya.cms.publication.PublicationFactory;
35
36 /**
37  * LDAP user test.
38  *
39  * @version $Id: LDAPUserTest.java 43670 2004-09-10 14:29:22Z andreas $
40  */

41 public class LDAPUserTest extends AccessControlTest {
42     /**
43      * Constructor for LDAPUserTest.
44      * @param arg0 a <code>String</code>
45      */

46     public LDAPUserTest(String JavaDoc arg0) {
47         super(arg0);
48     }
49
50     /**
51      *
52      * @param args an array of <code>String</code>
53      */

54     public static void main(String JavaDoc[] args) {
55         PublicationHelper.extractPublicationArguments(args);
56         junit.textui.TestRunner.run(LDAPUserTest.class);
57     }
58
59     /**
60      * @see junit.framework.TestCase#setUp()
61      */

62     protected void setUp() throws Exception JavaDoc {
63         super.setUp();
64     }
65
66     /**
67      * @see junit.framework.TestCase#tearDown()
68      */

69     public void tearDown() throws Exception JavaDoc {
70         super.tearDown();
71     }
72
73     /**
74      * get a publication
75      *
76      * @return a <code>Publication</code>
77      *
78      * @throws PublicationException if an error occurs
79      */

80     final public Publication getPublication() throws PublicationException {
81         String JavaDoc publicationId = "default";
82         String JavaDoc servletContextPath =
83             "/home/egli/build/jakarta-tomcat-4.1.21-LE-jdk14/webapps/lenya/";
84
85         return PublicationFactory.getPublication(
86             publicationId,
87             servletContextPath);
88     }
89
90     /**
91      * Create and save an ldap user
92      *
93      * @param userName name of the user
94      * @param email of the user
95      * @param ldapId ldap id of the user
96      * @throws AccessControlException if the creating or the saving fails
97     * @throws ConfigurationException if the creating or the saving fails
98      */

99     final public void createAndSaveUser(
100         String JavaDoc userName,
101         String JavaDoc email,
102         String JavaDoc ldapId)
103         throws AccessControlException, ConfigurationException {
104         String JavaDoc editorGroupName = "editorGroup";
105         String JavaDoc adminGroupName = "adminGroup";
106         String JavaDoc editorRoleName = "editorRole";
107         String JavaDoc adminRoleName = "adminRole";
108
109         File JavaDoc configDir = getAccreditablesDirectory();
110         FileRole editorRole = new FileRole();
111         editorRole.setName(editorRoleName);
112         editorRole.setConfigurationDirectory(configDir);
113
114         FileRole adminRole = new FileRole();
115         adminRole.setName(adminRoleName);
116         adminRole.setConfigurationDirectory(configDir);
117
118         FileGroup editorGroup = new FileGroup(configDir, editorGroupName);
119         FileGroup adminGroup = new FileGroup(configDir, adminGroupName);
120
121         LDAPUser user = new LDAPUser(configDir, userName, email, ldapId);
122
123         editorRole.save();
124         adminRole.save();
125
126         /*
127                 editorGroup.addRole(editorRole);
128                 user.addGroup(editorGroup);
129                 adminGroup.addRole(editorRole);
130                 adminGroup.addRole(adminRole);
131         */

132         editorGroup.save();
133         adminGroup.save();
134
135         adminGroup.add(user);
136         user.save();
137     }
138
139     /**
140      * Test loading an LDAPUser
141      *
142      * @param userName the name of the user
143      * @return an <code>LDAPUser</code>
144      * @throws AccessControlException of the loading fails
145      */

146     final public LDAPUser loadUser(String JavaDoc userName)
147         throws AccessControlException {
148         UserType[] userTypes = { FileAccreditableManager.getDefaultUserType() };
149         FileUserManager manager = FileUserManager.instance(getAccreditablesDirectory(), userTypes);
150
151         return (LDAPUser)manager.getUser(userName);
152     }
153
154     // final public void testGetFullName() throws AccessControlException {
155
// String userName = "felix";
156
// createAndSaveUser(userName, "felix@wyona.com", "m400032");
157
// LDAPUser user = null;
158
// user = loadUser(userName);
159
// assertNotNull(user);
160
// String fullName = user.getFullName();
161
// assertTrue(fullName.equals(" Felix Maeder - Wayona"));
162
// }
163

164     /**
165      * Test the setter of the full name
166      */

167     final public void testSetFullName() {
168         // the setFullName method is supposed to do nothing
169
}
170
171     // final public void testAuthenticate() throws AccessControlException {
172
// String userName = "felix";
173
// createAndSaveUser(userName, "felix@wyona.com", "m400032");
174
// User user = null;
175
// user = loadUser(userName);
176
// assertNotNull(user);
177
// assertTrue(user.authenticate("sekret"));
178
// }
179

180     /**
181      * Test the ldap id getter
182      *
183      * @throws AccessControlException if the test fails
184     * @throws ConfigurationException if the creating or the saving fails
185      */

186     final public void testGetLdapId()
187         throws ConfigurationException, AccessControlException {
188         String JavaDoc userName = "felix";
189         String JavaDoc ldapId = "m400032";
190         createAndSaveUser(userName, "felix@wyona.com", ldapId);
191
192         LDAPUser user = null;
193         user = loadUser(userName);
194         assertNotNull(user);
195         assertEquals(ldapId, user.getLdapId());
196     }
197
198     /**
199      * Test settinf the ldap id
200      *
201      * @throws AccessControlException if the test fails
202     * @throws ConfigurationException if the creating or the saving fails
203      */

204     final public void testSetLdapId()
205         throws ConfigurationException, AccessControlException {
206         String JavaDoc userName = "felix";
207         String JavaDoc newLdapId = "foo";
208         createAndSaveUser(userName, "felix@wyona.com", "bar");
209
210         LDAPUser user = null;
211         user = loadUser(userName);
212         assertNotNull(user);
213         user.setLdapId(newLdapId);
214         user.save();
215         user = null;
216         user = loadUser(userName);
217         assertNotNull(user);
218         assertEquals(newLdapId, user.getLdapId());
219     }
220
221     /**
222      * Test save
223      *
224      * @throws AccessControlException if the test fails
225     * @throws ConfigurationException if the creating or the saving fails
226      */

227     final public void testSave()
228         throws ConfigurationException, AccessControlException {
229         String JavaDoc userName = "felix";
230         createAndSaveUser(userName, "felix@wyona.com", "m400032");
231
232         User user = null;
233         user = loadUser(userName);
234         assertNotNull(user);
235     }
236
237     /**
238      * Test the deletion of a ldap user
239      *
240      */

241     final public void testDelete() {
242         //TODO Implement delete().
243
}
244 }
245
Popular Tags