1 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 ; 73 import javax.naming.NamingException ; 74 import javax.naming.directory.Attribute ; 75 import javax.naming.directory.Attributes ; 76 import javax.naming.directory.SearchControls ; 77 import javax.naming.directory.SearchResult ; 78 import java.util.Enumeration ; 79 80 85 public class UserLDAPTest 86 extends ExpressoTestCase { 87 public UserLDAPTest(String testName) 88 throws Exception { 89 super(testName); 90 } 91 92 public static void main(String [] args) 93 throws Exception { 94 95 junit.textui.TestRunner.run(suite()); 97 } 98 99 104 public static Test suite() 105 throws Exception { 106 return new TestSuite(UserLDAPTest.class); 107 } 108 109 110 public void testLDAP() 111 throws Exception { 112 UserLDAP user = new UserLDAP(); 113 String testUser = "MNash2"; 114 SearchControls constraints = new SearchControls (); 115 constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); 116 System.out.println("LDAP Test: "); 117 118 try { 119 user.setDBName(TestSystemInitializer.getTestContext()); 120 121 NamingEnumeration 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 sr = (SearchResult ) results.next(); 128 129 Attributes a = sr.getAttributes(); 131 Attribute attr = a.get("mail"); 132 133 if (attr != null) { 134 System.out.println("Email address:"); 135 136 for (NamingEnumeration 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 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 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 vals = attr.getAll(); 173 vals.hasMoreElements(); 174 System.out.println("\t" + vals.nextElement())) { 175 ; 176 } 177 } 178 for (NamingEnumeration ns = a.getAll(); ns.hasMore();) { 179 Attribute x = (Attribute ) ns.next(); 180 System.out.println("Additional Attribute " + x.getID()); 181 182 for (NamingEnumeration 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 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 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 sr = (SearchResult ) results.next(); 217 System.out.println("Found " + sr.getName()); 218 219 Attributes a = sr.getAttributes(); 220 221 for (NamingEnumeration ns = a.getAll(); ns.hasMore();) { 222 Attribute x = (Attribute ) ns.next(); 223 System.out.println("Additional Attribute " + x.getID()); 224 225 for (NamingEnumeration 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 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 248 NamingEnumeration 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 sr = (SearchResult ) results.next(); 261 System.out.println("Found " + sr.getName()); 262 263 Attributes a = sr.getAttributes(); 264 265 for (NamingEnumeration ns = a.getAll(); ns.hasMore();) { 266 Attribute x = (Attribute ) ns.next(); 267 System.out.println("Additional Attribute " + x.getID()); 268 269 for (NamingEnumeration 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 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 e = user.getGroups().elements(); e.hasMoreElements();) { 286 System.out.println("Test user belong to group '" + 287 (String ) e.nextElement() + "'"); 288 } 289 } 290 } | Popular Tags |