KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > ldap > TestLDAPService


1 /**
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5
6 /**
7  * Created by The eXo Platform SARL
8  * Author : Daniel Summer
9  * danny_xcz@users.sourceforge.net
10  * Date: 25/5/2004
11  */

12 package org.exoplatform.services.ldap;
13
14 import netscape.ldap.LDAPAttribute;
15 import netscape.ldap.LDAPAttributeSet;
16 import netscape.ldap.LDAPEntry;
17 import netscape.ldap.LDAPSearchResults;
18 import netscape.ldap.LDAPv2;
19 import org.exoplatform.container.PortalContainer;
20 import org.exoplatform.services.log.LogService;
21 import org.exoplatform.test.BasicTestCase;
22
23 public class TestLDAPService extends BasicTestCase {
24
25     private LDAPServiceContainer ldapServiceContainer;
26     private LDAPService ldapService;
27     private LogService logService;
28
29     public TestLDAPService(String JavaDoc s) {
30         super(s);
31     }
32
33     protected String JavaDoc getDescription() {
34         return "test ldap service";
35     }
36
37     public void setUp() throws Exception JavaDoc {
38
39         if (ldapServiceContainer == null) {
40             PortalContainer pcontainer = PortalContainer.getInstance();
41             ldapServiceContainer =
42                 (LDAPServiceContainer) pcontainer.getComponentInstanceOfType(
43                     LDAPServiceContainer.class);
44             //assertNotNull(ldapServiceContainer);
45
ldapService = ldapServiceContainer.createLDAPService();
46         }
47     }
48
49     public void testLDAPAddEntry() throws Exception JavaDoc {
50
51         String JavaDoc objectclasses[] = { "exouser" };
52         LDAPAttribute attr1 = new LDAPAttribute("objectclass", objectclasses);
53         LDAPAttribute attr2 = new LDAPAttribute("uid", "danny_xcz");
54         LDAPAttribute attr3 = new LDAPAttribute("userPassword", "pass");
55         LDAPAttributeSet attrSet = new LDAPAttributeSet();
56         attrSet.add(attr1);
57         attrSet.add(attr2);
58         attrSet.add(attr3);
59         String JavaDoc dn = "uid=danny_xcz,ou=users,dc=exoplatform,dc=com";
60         LDAPEntry newEntry = new LDAPEntry(dn, attrSet);
61         ldapService.add(newEntry);
62     }
63
64     public void testLDAPSearch() throws Exception JavaDoc {
65         /* Specify the search criteria. */
66         String JavaDoc baseDN = "dc=exoplatform,dc=com";
67         int searchScope = LDAPv2.SCOPE_SUB;
68         String JavaDoc searchFilter = "(uid=danny_xcz)";
69         String JavaDoc getAttrs[] = { "userPassword", "mail" };
70         /* Send the search request. */
71         LDAPSearchResults res =
72             ldapService.search(
73                 baseDN,
74                 searchScope,
75                 searchFilter,
76                 getAttrs,
77                 false);
78
79         LDAPEntry nextEntry = res.next();
80         LDAPAttribute passwordAttr = nextEntry.getAttribute("userPassword");
81         assertEquals(1, passwordAttr.size());
82     }
83
84 }
85
Popular Tags