KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > ldap > UserLDAPTest


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.ext.ldap;
66
67 import com.jcorporate.expresso.services.test.ExpressoTestCase;
68 import com.jcorporate.expresso.services.test.TestSystemInitializer;
69 import junit.framework.Test;
70 import junit.framework.TestSuite;
71
72 import javax.naming.NamingEnumeration JavaDoc;
73 import javax.naming.NamingException JavaDoc;
74 import javax.naming.directory.Attribute JavaDoc;
75 import javax.naming.directory.Attributes JavaDoc;
76 import javax.naming.directory.SearchControls JavaDoc;
77 import javax.naming.directory.SearchResult JavaDoc;
78 import java.util.Enumeration JavaDoc;
79
80 /**
81  * Basic test suite for LDAP based users
82  *
83  * @author Michael Rimov
84  */

85 public class UserLDAPTest
86         extends ExpressoTestCase {
87     public UserLDAPTest(String JavaDoc testName)
88             throws Exception JavaDoc {
89         super(testName);
90     }
91
92     public static void main(String JavaDoc[] args)
93             throws Exception JavaDoc {
94
95         //Set the system properties we need
96
junit.textui.TestRunner.run(suite());
97     }
98
99     /**
100      * Define the suite of tests that verify each function of the cache
101      *
102      * @return
103      */

104     public static Test suite()
105             throws Exception JavaDoc {
106         return new TestSuite(UserLDAPTest.class);
107     } /* suite() */
108
109
110     public void testLDAP()
111             throws Exception JavaDoc {
112         UserLDAP user = new UserLDAP();
113         String JavaDoc testUser = "MNash2";
114         SearchControls JavaDoc constraints = new SearchControls JavaDoc();
115         constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
116         System.out.println("LDAP Test: ");
117
118         try {
119             user.setDBName(TestSystemInitializer.getTestContext());
120
121             NamingEnumeration JavaDoc results = user.getContext().search("o=" + user.getRoot(),
122                     "(uid=" + testUser + ")",
123                     constraints);
124             assertTrue("No results for search of LDAP server", results != null);
125
126             while (results.hasMore()) {
127                 SearchResult JavaDoc sr = (SearchResult JavaDoc) results.next();
128
129                 //result.get ln fn cn mail
130
Attributes JavaDoc a = sr.getAttributes();
131                 Attribute JavaDoc attr = a.get("mail");
132
133                 if (attr != null) {
134                     System.out.println("Email address:");
135
136                     for (NamingEnumeration JavaDoc vals = attr.getAll();
137                          vals.hasMoreElements();
138                          System.out.println("\t" + vals.nextElement())) {
139                         ;
140                     }
141                 }
142
143                 attr = a.get("sn");
144
145                 if (attr != null) {
146                     System.out.println("Surname:");
147
148                     for (NamingEnumeration JavaDoc vals = attr.getAll();
149                          vals.hasMoreElements();
150                          System.out.println("\t" + vals.nextElement())) {
151                         ;
152                     }
153                 }
154
155                 attr = a.get("fn");
156
157                 if (attr != null) {
158                     System.out.println("First name:");
159
160                     for (NamingEnumeration JavaDoc vals = attr.getAll();
161                          vals.hasMoreElements();
162                          System.out.println("\t" + vals.nextElement())) {
163                         ;
164                     }
165                 }
166
167                 attr = a.get("cn");
168
169                 if (attr != null) {
170                     System.out.println("Full name:");
171
172                     for (NamingEnumeration JavaDoc vals = attr.getAll();
173                          vals.hasMoreElements();
174                          System.out.println("\t" + vals.nextElement())) {
175                         ;
176                     }
177                 }
178                 for (NamingEnumeration JavaDoc ns = a.getAll(); ns.hasMore();) {
179                     Attribute JavaDoc x = (Attribute JavaDoc) ns.next();
180                     System.out.println("Additional Attribute " + x.getID());
181
182                     for (NamingEnumeration JavaDoc vals = x.getAll();
183                          vals.hasMoreElements();
184                          System.out.println("\t\t" + vals.nextElement())) {
185                         ;
186                     }
187                 }
188             }
189
190             System.out.println("No (more) elements");
191         } catch (NamingException JavaDoc ne) {
192             ne.printStackTrace(System.out);
193             fail("Naming Exception : " + ne.getMessage());
194         }
195
196         System.out.println("LDAP test 2 begins");
197         user.setUserName(testUser);
198         user.setDBName(TestSystemInitializer.getTestContext());
199         user.retrieve();
200         assertTrue("Proper Password", user.passwordEquals("xyx"));
201         System.out.println("LDAP test 3 begins");
202
203         try {
204             user.setDBName(TestSystemInitializer.getTestContext());
205
206             NamingEnumeration JavaDoc results = user.getContext().search("o=" + user.getRoot(),
207                     "(objectclass=groupofuniquenames)",
208                     constraints);
209
210             if (results == null) {
211                 fail("No results for search of LDAP server for groups");
212             } else {
213                 System.out.println("Got results");
214             }
215             while (results.hasMore()) {
216                 SearchResult JavaDoc sr = (SearchResult JavaDoc) results.next();
217                 System.out.println("Found " + sr.getName());
218
219                 Attributes JavaDoc a = sr.getAttributes();
220
221                 for (NamingEnumeration JavaDoc ns = a.getAll(); ns.hasMore();) {
222                     Attribute JavaDoc x = (Attribute JavaDoc) ns.next();
223                     System.out.println("Additional Attribute " + x.getID());
224
225                     for (NamingEnumeration JavaDoc vals = x.getAll();
226                          vals.hasMoreElements();
227                          System.out.println("\t\t" + vals.nextElement())) {
228                         ;
229                     }
230                 }
231             }
232
233             System.out.println("No (more) groups");
234         } catch (NamingException JavaDoc ne) {
235             ne.printStackTrace();
236             fail("Failed LDAP Test #3 " + ne.getMessage());
237         }
238
239         System.out.println("LDAP test 4 begins");
240
241         try {
242             user.setDBName(TestSystemInitializer.getTestContext());
243
244             /**
245              * @todo: This was name where I've replaced user.getLoginName()
246              * What should it be
247              */

248             NamingEnumeration JavaDoc results = user.getContext().search("o=" + user.getRoot(),
249                     "(uniquemember=" +
250                     user.getLoginName() + ", o=" +
251                     user.getRoot() + ")",
252                     constraints);
253
254             if (results == null) {
255                 fail("LDAP Test #3 No results for search of LDAP server for groups");
256             } else {
257                 System.out.println("Got results");
258             }
259             while (results.hasMore()) {
260                 SearchResult JavaDoc sr = (SearchResult JavaDoc) results.next();
261                 System.out.println("Found " + sr.getName());
262
263                 Attributes JavaDoc a = sr.getAttributes();
264
265                 for (NamingEnumeration JavaDoc ns = a.getAll(); ns.hasMore();) {
266                     Attribute JavaDoc x = (Attribute JavaDoc) ns.next();
267                     System.out.println("Additional Attribute " + x.getID());
268
269                     for (NamingEnumeration JavaDoc vals = x.getAll();
270                          vals.hasMoreElements();
271                          System.out.println("\t\t" + vals.nextElement())) {
272                         ;
273                     }
274                 }
275             }
276
277             System.out.println("No (more) groups");
278         } catch (NamingException JavaDoc ne) {
279             ne.printStackTrace();
280             fail("caught exception in LDAP Test #4 " + ne.getMessage());
281         }
282
283         System.out.println("LDAP test 5 begins");
284
285         for (Enumeration JavaDoc e = user.getGroups().elements(); e.hasMoreElements();) {
286             System.out.println("Test user belong to group '" +
287                     (String JavaDoc) e.nextElement() + "'");
288         }
289     }
290 }
Popular Tags