Source Code Cross Referenced for LdapContextImplTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » jndi » provider » ldap » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.jndi.provider.ldap 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* 
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more 
003:         *  contributor license agreements.  See the NOTICE file distributed with 
004:         *  this work for additional information regarding copyright ownership. 
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0 
006:         *  (the "License"); you may not use this file except in compliance with 
007:         *  the License.  You may obtain a copy of the License at 
008:         * 
009:         *     http://www.apache.org/licenses/LICENSE-2.0 
010:         * 
011:         *  Unless required by applicable law or agreed to in writing, software 
012:         *  distributed under the License is distributed on an "AS IS" BASIS, 
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014:         *  See the License for the specific language governing permissions and 
015:         *  limitations under the License. 
016:         */
017:
018:        package org.apache.harmony.jndi.provider.ldap;
019:
020:        import java.util.HashMap;
021:        import java.util.HashSet;
022:        import java.util.Hashtable;
023:        import java.util.Iterator;
024:        import java.util.List;
025:        import java.util.Map;
026:
027:        import javax.naming.CompositeName;
028:        import javax.naming.Context;
029:        import javax.naming.InvalidNameException;
030:        import javax.naming.Name;
031:        import javax.naming.NamingEnumeration;
032:        import javax.naming.NamingException;
033:        import javax.naming.directory.Attribute;
034:        import javax.naming.directory.Attributes;
035:        import javax.naming.directory.BasicAttribute;
036:        import javax.naming.directory.BasicAttributes;
037:        import javax.naming.directory.DirContext;
038:        import javax.naming.directory.InvalidSearchFilterException;
039:        import javax.naming.directory.SearchControls;
040:        import javax.naming.event.EventContext;
041:        import javax.naming.event.NamespaceChangeListener;
042:        import javax.naming.event.NamingEvent;
043:        import javax.naming.event.NamingExceptionEvent;
044:        import javax.naming.event.NamingListener;
045:        import javax.naming.ldap.BasicControl;
046:        import javax.naming.ldap.Control;
047:        import javax.naming.ldap.LdapName;
048:        import javax.naming.ldap.PagedResultsControl;
049:        import javax.naming.ldap.SortControl;
050:
051:        import junit.framework.TestCase;
052:
053:        import org.apache.harmony.jndi.internal.parser.AttributeTypeAndValuePair;
054:        import org.apache.harmony.jndi.provider.ldap.event.PersistentSearchResult;
055:
056:        public class LdapContextImplTest extends TestCase {
057:            private LdapContextImpl context;
058:
059:            public void test_getSchema() throws NamingException {
060:                context = new LdapContextImpl(new MockLdapClient(), null, "");
061:                try {
062:                    context.getSchema((Name) null);
063:                    fail("Should throw NullPointerException");
064:                } catch (NullPointerException e) {
065:                    // expected
066:                }
067:
068:                try {
069:                    context.getSchema((String) null);
070:                    fail("Should throw NullPointerException");
071:                } catch (NullPointerException e) {
072:                    // expected
073:                }
074:            }
075:
076:            public void test_getSchemaClassDefinition() throws NamingException {
077:                context = new LdapContextImpl(new MockLdapClient(), null, "");
078:                try {
079:                    context.getSchemaClassDefinition((Name) null);
080:                    fail("Should throw NullPointerException");
081:                } catch (NullPointerException e) {
082:                    // expected
083:                }
084:
085:                try {
086:                    context.getSchemaClassDefinition((String) null);
087:                    fail("Should throw NullPointerException");
088:                } catch (NullPointerException e) {
089:                    // expected
090:                }
091:            }
092:
093:            public void test_composeName_LNameLName() throws Exception {
094:                context = new LdapContextImpl(new MockLdapClient(), null, "");
095:                Name name = new LdapName("cn=happy,dc=test");
096:                Name prefix = new LdapName("o=harmony");
097:                Name result = context.composeName(name, prefix);
098:                assertTrue(result instanceof  LdapName);
099:                assertEquals("cn=happy,dc=test,o=harmony", result.toString());
100:
101:                try {
102:                    context.composeName(null, prefix);
103:                    fail("Should throws NPE");
104:                } catch (NullPointerException e) {
105:                    // expected
106:                }
107:
108:                try {
109:                    context.composeName(name, null);
110:                    fail("Should throws NPE");
111:                } catch (NullPointerException e) {
112:                    // expected
113:                }
114:
115:                CompositeName compositeName = new CompositeName("usr/bin");
116:                result = context.composeName(compositeName, prefix);
117:                assertTrue(result instanceof  CompositeName);
118:                assertEquals("o=harmony/usr/bin", result.toString());
119:
120:                result = context.composeName(name, compositeName);
121:                assertTrue(result instanceof  CompositeName);
122:                assertEquals("usr/bin/cn=happy,dc=test", result.toString());
123:
124:                compositeName = new CompositeName("usr");
125:                CompositeName cName = new CompositeName("bin/cn=ok");
126:                result = context.composeName(compositeName, cName);
127:                assertTrue(result instanceof  CompositeName);
128:                assertEquals("bin/cn=ok/usr", result.toString());
129:            }
130:
131:            public void test_list_LName() throws Exception {
132:                MockLdapClient client = new MockLdapClient();
133:                context = new LdapContextImpl(client,
134:                        new Hashtable<Object, Object>(), "");
135:
136:                Name name = new LdapName("cn=test,o=harmony");
137:                context.list(name);
138:
139:                SearchOp op = (SearchOp) client.getRequest();
140:                assertEquals(name.toString(), op.getBaseObject());
141:                assertFalse(op.isTypesOnly());
142:                SearchControls controls = op.getControls();
143:                assertEquals(SearchControls.ONELEVEL_SCOPE, controls
144:                        .getSearchScope());
145:                Filter filter = op.getFilter();
146:                assertEquals(Filter.PRESENT_FILTER, filter.getType());
147:                assertEquals("objectClass", (String) filter.getValue());
148:
149:                name = new CompositeName("usr/bin");
150:                try {
151:                    context.list(name);
152:                    fail("should throws InvalidNameException");
153:                } catch (InvalidNameException e) {
154:                    // expected
155:                }
156:            }
157:
158:            public void test_createSubcontext_LName() throws Exception {
159:                MockLdapClient client = new MockLdapClient();
160:                context = new LdapContextImpl(client,
161:                        new Hashtable<Object, Object>(), "cn=test");
162:
163:                Name name = new LdapName("cn=add+dc=hello,o=harmony");
164:                context.createSubcontext(name);
165:
166:                AddOp op = (AddOp) client.getRequest();
167:                assertEquals("cn=add+dc=hello,o=harmony,cn=test", op.getEntry());
168:                List<LdapAttribute> list = op.getAttributeList();
169:
170:                assertEquals(3, list.size());
171:                Map<String, LdapAttribute> map = new HashMap<String, LdapAttribute>();
172:                for (LdapAttribute attribute : list) {
173:                    map.put(attribute.getID(), attribute);
174:                }
175:
176:                assertTrue(map.containsKey("objectClass"));
177:                Attribute attr = map.get("objectClass");
178:                assertEquals(2, attr.size());
179:                assertTrue(attr.get(0) instanceof  String);
180:                assertTrue(attr.get(1) instanceof  String);
181:                assertTrue(attr.get(0).equals("top")
182:                        || attr.get(0).equals("javaContainer"));
183:                assertTrue(attr.get(1).equals("top")
184:                        || attr.get(1).equals("javaContainer"));
185:
186:                assertTrue(map.containsKey("cn"));
187:                attr = map.get("cn");
188:                assertEquals(1, attr.size());
189:                assertEquals("add", attr.get(0));
190:
191:                assertTrue(map.containsKey("dc"));
192:                attr = map.get("dc");
193:                assertEquals(1, attr.size());
194:                assertEquals("hello", attr.get(0));
195:            }
196:
197:            public void test_createSubcontext_LNameLAttributes()
198:                    throws Exception {
199:                MockLdapClient client = new MockLdapClient();
200:                context = new LdapContextImpl(client,
201:                        new Hashtable<Object, Object>(), "cn=test");
202:
203:                Name name = new LdapName("cn=add+dc=hello,o=harmony");
204:                Attributes attrs = new BasicAttributes();
205:                Attribute attr = new BasicAttribute("ou");
206:                attr.add("harmony");
207:                attr.add("apache");
208:                attrs.put(attr);
209:
210:                context.createSubcontext(name, attrs);
211:
212:                AddOp op = (AddOp) client.getRequest();
213:                assertEquals("cn=add+dc=hello,o=harmony,cn=test", op.getEntry());
214:                List<LdapAttribute> list = op.getAttributeList();
215:
216:                assertEquals(3, list.size());
217:                Map<String, LdapAttribute> map = new HashMap<String, LdapAttribute>();
218:                for (LdapAttribute attribute : list) {
219:                    map.put(attribute.getID(), attribute);
220:                }
221:
222:                assertTrue(map.containsKey("ou"));
223:                attr = map.get("ou");
224:                assertEquals(2, attr.size());
225:                assertTrue(attr.get(0) instanceof  String);
226:                assertTrue(attr.get(1) instanceof  String);
227:                assertTrue(attr.get(0).equals("harmony")
228:                        || attr.get(0).equals("apache"));
229:                assertTrue(attr.get(1).equals("harmony")
230:                        || attr.get(1).equals("apache"));
231:
232:                assertTrue(map.containsKey("cn"));
233:                attr = map.get("cn");
234:                assertEquals(1, attr.size());
235:                assertEquals("add", attr.get(0));
236:
237:                assertTrue(map.containsKey("dc"));
238:                attr = map.get("dc");
239:                assertEquals(1, attr.size());
240:                assertEquals("hello", attr.get(0));
241:            }
242:
243:            public void test_getAttributes() throws Exception {
244:                MockLdapClient client = new MockLdapClient();
245:                context = new LdapContextImpl(client,
246:                        new Hashtable<Object, Object>(), "cn=test");
247:
248:                Attributes attrs = context.getAttributes("o=apache");
249:                SearchOp op = (SearchOp) client.getRequest();
250:                assertEquals("o=apache,cn=test", op.getBaseObject());
251:                SearchControls controls = op.getControls();
252:                assertEquals(SearchControls.OBJECT_SCOPE, controls
253:                        .getSearchScope());
254:                assertNull(controls.getReturningAttributes());
255:
256:                Filter filter = op.getFilter();
257:                assertEquals(Filter.PRESENT_FILTER, filter.getType());
258:                assertEquals("objectClass", filter.getValue());
259:
260:                attrs = context.getAttributes("o=apache", new String[0]);
261:                op = (SearchOp) client.getRequest();
262:                assertEquals("o=apache,cn=test", op.getBaseObject());
263:                controls = op.getControls();
264:                assertEquals(SearchControls.OBJECT_SCOPE, controls
265:                        .getSearchScope());
266:                assertEquals(1, controls.getReturningAttributes().length);
267:                assertEquals("1.1", controls.getReturningAttributes()[0]);
268:                filter = op.getFilter();
269:                assertEquals(Filter.PRESENT_FILTER, filter.getType());
270:                assertEquals("objectClass", filter.getValue());
271:            }
272:
273:            public void test_modifyAttributes() throws Exception {
274:                MockLdapClient client = new MockLdapClient();
275:                context = new LdapContextImpl(client,
276:                        new Hashtable<Object, Object>(), "cn=test");
277:
278:                Attributes attributes = new BasicAttributes();
279:                Attribute attr = new BasicAttribute("cn");
280:                attr.add("hello");
281:                attr.add("world");
282:                attributes.put(attr);
283:
284:                context.modifyAttributes("o=apache", DirContext.ADD_ATTRIBUTE,
285:                        attributes);
286:                try {
287:                    context.modifyAttributes("", null);
288:                    fail("Should throw NullPointerException");
289:                } catch (NullPointerException e) {
290:                    // expected
291:                }
292:
293:                try {
294:                    context.modifyAttributes("", -1, attributes);
295:                    fail("Should throw IllegalArgumentException");
296:                } catch (IllegalArgumentException e) {
297:                    // expected
298:                }
299:            }
300:
301:            // TODO: add test for names not in same namespace
302:            public void test_rename() throws Exception {
303:                MockLdapClient client = new MockLdapClient();
304:                context = new LdapContextImpl(client,
305:                        new Hashtable<Object, Object>(), "cn=test");
306:
307:                context.rename("cn=what", "cn=how");
308:
309:                ModifyDNOp op = (ModifyDNOp) client.getRequest();
310:                assertEquals("cn=what,cn=test", op.getEntry());
311:                assertEquals("cn=how", op.getNewrdn());
312:                assertEquals("cn=test", op.getNewSuperior());
313:                assertEquals(true, op.isDeleteoldrdn());
314:
315:                Hashtable<Object, Object> env = new Hashtable<Object, Object>();
316:                env.put("java.naming.ldap.deleteRDN", "false");
317:                context = new LdapContextImpl(client, env, "cn=test");
318:
319:                context.rename("cn=what,o=harmony", "o=apache");
320:
321:                op = (ModifyDNOp) client.getRequest();
322:                assertEquals("cn=what,o=harmony,cn=test", op.getEntry());
323:                assertEquals("o=apache", op.getNewrdn());
324:                assertEquals("cn=test", op.getNewSuperior());
325:                assertEquals(false, op.isDeleteoldrdn());
326:
327:                try {
328:                    context.rename(null, "cn=hello");
329:                    fail("should throw NullPointerException");
330:                } catch (NullPointerException e) {
331:                    // expected
332:                }
333:
334:                try {
335:                    context.rename("cn=hello", null);
336:                    fail("should throw NullPointerException");
337:                } catch (NullPointerException e) {
338:                    // expected
339:                }
340:            }
341:
342:            public void test_destroySubcontext() throws Exception {
343:                MockLdapClient client = new MockLdapClient();
344:                context = new LdapContextImpl(client,
345:                        new Hashtable<Object, Object>(), "cn=test");
346:                context.destroySubcontext("cn=bad");
347:
348:                DeleteOp op = (DeleteOp) client.getRequest();
349:                assertEquals("cn=bad,cn=test", op.getDn());
350:
351:                try {
352:                    String name = null;
353:                    context.destroySubcontext(name);
354:                    fail("Should throw NullPointerException");
355:                } catch (NullPointerException e) {
356:                    // expected
357:                }
358:            }
359:
360:            public void test_setRequestControls() throws Exception {
361:                MockLdapClient client = new MockLdapClient();
362:                Hashtable<Object, Object> env = new Hashtable<Object, Object>();
363:                env.put(Context.REFERRAL, "follow");
364:                context = new LdapContextImpl(client, env, "cn=test");
365:
366:                context.setRequestControls(null);
367:                assertNull(context.getRequestControls());
368:
369:                Control[] controls = new Control[] { new BasicControl("0"),
370:                        new BasicControl("1"), new BasicControl("2"),
371:                        new BasicControl("3") };
372:
373:                context.setRequestControls(controls);
374:
375:                Control[] actual = context.getRequestControls();
376:
377:                assertEquals(controls.length, actual.length);
378:                assertNotSame(controls, actual);
379:
380:                // Context.REFERRAL is 'ignore' add ManageDsaIT Control
381:                env.put(Context.REFERRAL, "ignore");
382:                context = new LdapContextImpl(client, env, "cn=test");
383:                context.setRequestControls(null);
384:                actual = context.getRequestControls();
385:                assertEquals(1, actual.length);
386:                assertEquals("2.16.840.1.113730.3.4.2", actual[0].getID());
387:                assertNull(actual[0].getEncodedValue());
388:                assertFalse(actual[0].isCritical());
389:
390:                context.setRequestControls(controls);
391:                actual = context.getRequestControls();
392:                assertEquals(controls.length + 1, actual.length);
393:
394:                // Context.REFERRAL is 'ignore', add ManageDsaIT Control
395:                context = new LdapContextImpl(client,
396:                        new Hashtable<Object, Object>(), "cn=test");
397:                context.setRequestControls(null);
398:                actual = context.getRequestControls();
399:                assertEquals(1, actual.length);
400:                assertEquals("2.16.840.1.113730.3.4.2", actual[0].getID());
401:                assertNull(actual[0].getEncodedValue());
402:                assertFalse(actual[0].isCritical());
403:
404:                context.setRequestControls(controls);
405:                actual = context.getRequestControls();
406:                assertEquals(controls.length + 1, actual.length);
407:            }
408:
409:            public void test_search_LAttribute() throws Exception {
410:                MockLdapClient client = new MockLdapClient();
411:                context = new LdapContextImpl(client,
412:                        new Hashtable<Object, Object>(), "cn=test");
413:
414:                Attributes attrs = new BasicAttributes();
415:                attrs.put("cn", "hello");
416:                Attribute attr = new BasicAttribute("o");
417:                attr.add("harmony");
418:                attr.add("apache");
419:                attrs.put(attr);
420:
421:                context.search("cn=hello", attrs);
422:
423:                SearchOp op = (SearchOp) client.getRequest();
424:                assertEquals("cn=hello,cn=test", op.getBaseObject());
425:                SearchControls controls = op.getControls();
426:                assertEquals(SearchControls.ONELEVEL_SCOPE, controls
427:                        .getSearchScope());
428:                assertEquals(0, controls.getCountLimit());
429:                assertEquals(false, controls.getDerefLinkFlag());
430:                assertEquals(false, controls.getReturningObjFlag());
431:                assertEquals(null, controls.getReturningAttributes());
432:
433:                Filter filter = op.getFilter();
434:                assertEquals(Filter.AND_FILTER, filter.getType());
435:                List<Filter> children = filter.getChildren();
436:
437:                for (Filter f : children) {
438:                    assertEquals(Filter.EQUALITY_MATCH_FILTER, f.getType());
439:                    AttributeTypeAndValuePair pair = (AttributeTypeAndValuePair) f
440:                            .getValue();
441:                    if (pair.getType().equals("cn")) {
442:                        assertEquals("hello", pair.getValue());
443:                    } else {
444:                        assertEquals("o", pair.getType());
445:                        assertTrue(pair.getValue().equals("harmony")
446:                                || pair.getValue().equals("apache"));
447:                    }
448:                }
449:
450:                String[] rtAttributes = new String[] { "cn", "o" };
451:                context.search("cn=hello", attrs, rtAttributes);
452:                op = (SearchOp) client.getRequest();
453:                assertEquals("cn=hello,cn=test", op.getBaseObject());
454:                controls = op.getControls();
455:                String[] actual = controls.getReturningAttributes();
456:
457:                for (String attribute : rtAttributes) {
458:                    boolean found = false;
459:                    for (String s : actual) {
460:                        if (s.equals(attribute)) {
461:                            found = true;
462:                            break;
463:                        }
464:                    }
465:                    if (!found) {
466:                        fail("Attribute " + attribute + " not found.");
467:                    }
468:                }
469:            }
470:
471:            public void test_search_with_filter() throws Exception {
472:                MockLdapClient client = new MockLdapClient();
473:                context = new LdapContextImpl(client,
474:                        new Hashtable<Object, Object>(), "cn=test");
475:                SearchControls controls = new SearchControls();
476:                controls.setCountLimit(100);
477:                controls.setDerefLinkFlag(true);
478:                controls.setReturningObjFlag(true);
479:                controls.setTimeLimit(5);
480:
481:                context.search("test=search", "(objectClass=*)", controls);
482:
483:                SearchOp op = (SearchOp) client.getRequest();
484:                assertEquals("test=search,cn=test", op.getBaseObject());
485:                Filter filter = op.getFilter();
486:                assertEquals(Filter.PRESENT_FILTER, filter.getType());
487:                assertEquals("objectClass", filter.getValue());
488:                assertEquals(controls.getCountLimit(), op.getControls()
489:                        .getCountLimit());
490:                assertEquals(controls.getDerefLinkFlag(), op.getControls()
491:                        .getDerefLinkFlag());
492:                assertEquals(controls.getReturningObjFlag(), op.getControls()
493:                        .getReturningObjFlag());
494:                assertEquals(controls.getSearchScope(), op.getControls()
495:                        .getSearchScope());
496:                assertEquals(controls.getTimeLimit(), op.getControls()
497:                        .getTimeLimit());
498:
499:                try {
500:                    context.search("test=search", "objectClass=*", controls);
501:                    fail("Should throw InvalidSearchFilterException");
502:                } catch (InvalidSearchFilterException e) {
503:                    // expected
504:                }
505:
506:                context.search("test=search", "(objectClass=*)", null);
507:                op = (SearchOp) client.getRequest();
508:                assertEquals(0, op.getControls().getCountLimit());
509:                assertEquals(false, op.getControls().getDerefLinkFlag());
510:                assertEquals(false, op.getControls().getReturningObjFlag());
511:                assertEquals(SearchControls.ONELEVEL_SCOPE, op.getControls()
512:                        .getSearchScope());
513:                assertEquals(0, op.getControls().getTimeLimit());
514:                assertNull(controls.getReturningAttributes());
515:            }
516:
517:            public void test_search_invalid_properties() throws Exception {
518:                MockLdapClient client = new MockLdapClient();
519:
520:                Hashtable<Object, Object> env = new Hashtable<Object, Object>();
521:                env.put("java.naming.ldap.derefAliases", "error");
522:                context = new LdapContextImpl(client, env, "cn=test");
523:                try {
524:                    context.search("", null);
525:                    fail("Should throws IllegalArgumentException");
526:                } catch (IllegalArgumentException e) {
527:                    // expected
528:                }
529:
530:                env = new Hashtable<Object, Object>();
531:                env.put("java.naming.ldap.typesOnly", "error");
532:                context = new LdapContextImpl(client, env, "cn=test");
533:                try {
534:                    context.getAttributes("", null);
535:                    fail("Should throws IllegalArgumentException");
536:                } catch (IllegalArgumentException e) {
537:                    // expected
538:                }
539:            }
540:
541:            public void test_addToEnvironment() throws Exception {
542:                MockLdapClient client = new MockLdapClient();
543:                Hashtable<Object, Object> env = new Hashtable<Object, Object>();
544:
545:                context = new LdapContextImpl(client, env, "cn=test");
546:
547:                Object preValue = context.addToEnvironment(Context.REFERRAL,
548:                        "ignore");
549:                assertNull(preValue);
550:                Hashtable<Object, Object> returnedEnv = (Hashtable<Object, Object>) context
551:                        .getEnvironment();
552:                assertTrue(returnedEnv.containsKey(Context.REFERRAL));
553:                assertEquals("ignore", returnedEnv.get(Context.REFERRAL));
554:
555:                preValue = context.addToEnvironment(Context.REFERRAL, "follow");
556:                assertEquals("ignore", preValue);
557:                returnedEnv = (Hashtable<Object, Object>) context
558:                        .getEnvironment();
559:                assertTrue(returnedEnv.containsKey(Context.REFERRAL));
560:                assertEquals("follow", returnedEnv.get(Context.REFERRAL));
561:
562:            }
563:
564:            public void test_removeFromEnvironment() throws Exception {
565:                MockLdapClient client = new MockLdapClient();
566:                Hashtable<Object, Object> env = new Hashtable<Object, Object>();
567:
568:                context = new LdapContextImpl(client, env, "cn=test");
569:
570:                Object preValue = context
571:                        .removeFromEnvironment(Context.REFERRAL);
572:                assertNull(preValue);
573:                Hashtable<Object, Object> returnedEnv = (Hashtable<Object, Object>) context
574:                        .getEnvironment();
575:                assertFalse(returnedEnv.containsKey(Context.REFERRAL));
576:
577:                env.clear();
578:                env.put(Context.REFERRAL, "ignore");
579:                context = new LdapContextImpl(client, env, "cn=test");
580:                preValue = context.removeFromEnvironment(Context.REFERRAL);
581:                assertEquals("ignore", preValue);
582:                returnedEnv = (Hashtable<Object, Object>) context
583:                        .getEnvironment();
584:                assertFalse(returnedEnv.containsKey(Context.REFERRAL));
585:            }
586:
587:            public void test_bind() throws Exception {
588:                MockLdapClient client = new MockLdapClient();
589:                Hashtable<Object, Object> env = new Hashtable<Object, Object>();
590:
591:                context = new LdapContextImpl(client, env, "cn=test");
592:                context.bind("cn=bind", "it's bind");
593:
594:                AddOp op = (AddOp) client.getRequest();
595:                assertEquals("cn=bind,cn=test", op.getEntry());
596:                List<LdapAttribute> attrList = op.getAttributeList();
597:                // has attribute: objectClass, javaClassNames, javaClassName,
598:                // javaSerializedData, cn
599:                assertEquals(5, attrList.size());
600:                Map<String, LdapAttribute> map = new HashMap<String, LdapAttribute>();
601:                for (Iterator iter = attrList.iterator(); iter.hasNext();) {
602:                    LdapAttribute attr = (LdapAttribute) iter.next();
603:                    map.put(attr.getID(), attr);
604:                }
605:
606:                assertTrue(map.containsKey("objectClass"));
607:                Attribute attribute = map.get("objectClass");
608:                NamingEnumeration<?> enu = attribute.getAll();
609:                HashSet<Object> valueSet = new HashSet<Object>();
610:                while (enu.hasMore()) {
611:                    valueSet.add(enu.next());
612:                }
613:                // objectClass has values: top, javaContainer, javaObject,
614:                // javaSerializedObject
615:                assertEquals(4, valueSet.size());
616:                assertTrue(valueSet.contains("top"));
617:                assertTrue(valueSet.contains("javaContainer"));
618:                assertTrue(valueSet.contains("javaObject"));
619:                assertTrue(valueSet.contains("javaSerializedObject"));
620:
621:                assertTrue(map.containsKey("javaClassNames"));
622:                attribute = map.get("javaClassNames");
623:                enu = attribute.getAll();
624:                valueSet = new HashSet<Object>();
625:                while (enu.hasMore()) {
626:                    valueSet.add(enu.next());
627:                }
628:
629:                assertEquals(5, valueSet.size());
630:                assertTrue(valueSet.contains("java.io.Serializable"));
631:                assertTrue(valueSet.contains("java.lang.CharSequence"));
632:                assertTrue(valueSet.contains("java.lang.Comparable"));
633:                assertTrue(valueSet.contains("java.lang.Object"));
634:                assertTrue(valueSet.contains("java.lang.String"));
635:
636:                assertTrue(map.containsKey("javaClassName"));
637:                attribute = map.get("javaClassName");
638:                assertEquals(1, attribute.size());
639:                assertEquals(String.class.getName(), attribute.get(0));
640:
641:                assertTrue(map.containsKey("javaSerializedData"));
642:                assertEquals(1, attribute.size());
643:                attribute = map.get("javaSerializedData");
644:
645:                assertTrue(map.containsKey("cn"));
646:                attribute = map.get("cn");
647:                assertEquals(1, attribute.size());
648:                assertEquals("bind", attribute.get(0));
649:            }
650:
651:            public void test_lookup() throws Exception {
652:                MockLdapClient client = new MockLdapClient();
653:                Hashtable<Object, Object> env = new Hashtable<Object, Object>();
654:
655:                context = new LdapContextImpl(client, env, "cn=test");
656:                context.lookup("cn=lookup");
657:
658:                SearchOp op = (SearchOp) client.getRequest();
659:                assertEquals("cn=lookup,cn=test", op.getBaseObject());
660:                SearchControls controls = op.getControls();
661:                assertEquals(SearchControls.OBJECT_SCOPE, controls
662:                        .getSearchScope());
663:                assertNull(controls.getReturningAttributes());
664:
665:                Filter filter = op.getFilter();
666:                assertEquals(Filter.PRESENT_FILTER, filter.getType());
667:                assertEquals("objectClass", filter.getValue());
668:            }
669:
670:            public void test_listBinding() throws Exception {
671:                MockLdapClient client = new MockLdapClient();
672:                context = new LdapContextImpl(client,
673:                        new Hashtable<Object, Object>(), "cn=test");
674:
675:                context.list("cn=listBinding");
676:
677:                SearchOp op = (SearchOp) client.getRequest();
678:                assertEquals("cn=listBinding,cn=test", op.getBaseObject());
679:                assertFalse(op.isTypesOnly());
680:                SearchControls controls = op.getControls();
681:                assertEquals(SearchControls.ONELEVEL_SCOPE, controls
682:                        .getSearchScope());
683:                Filter filter = op.getFilter();
684:                assertEquals(Filter.PRESENT_FILTER, filter.getType());
685:                assertEquals("objectClass", (String) filter.getValue());
686:
687:                Name name = new CompositeName("usr/bin");
688:                try {
689:                    context.list(name);
690:                    fail("should throws InvalidNameException");
691:                } catch (InvalidNameException e) {
692:                    // expected
693:                }
694:            }
695:
696:            public void test_addNamingListener() throws Exception {
697:                MockLdapClient client = new MockLdapClient();
698:                Hashtable<Object, Object> env = new Hashtable<Object, Object>();
699:
700:                context = new LdapContextImpl(client, env, "cn=test");
701:
702:                context.addNamingListener("", EventContext.OBJECT_SCOPE,
703:                        new TestNamingListener());
704:
705:                SearchOp op = (SearchOp) client.getRequest();
706:                assertEquals("cn=test", op.getBaseObject());
707:                SearchControls controls = op.getControls();
708:                assertEquals(SearchControls.OBJECT_SCOPE, controls
709:                        .getSearchScope());
710:                assertEquals(0, controls.getCountLimit());
711:                assertEquals(false, controls.getDerefLinkFlag());
712:                assertEquals(false, controls.getReturningObjFlag());
713:                assertEquals(null, controls.getReturningAttributes());
714:
715:                Filter filter = op.getFilter();
716:                assertEquals(Filter.PRESENT_FILTER, filter.getType());
717:                assertEquals("objectClass", filter.getValue());
718:                assertTrue(op.getSearchResult() instanceof  PersistentSearchResult);
719:
720:                try {
721:                    context
722:                            .addNamingListener("cn=addlistener/use/bin",
723:                                    EventContext.OBJECT_SCOPE,
724:                                    new TestNamingListener());
725:                    fail("Should throw InvalidNameException");
726:                } catch (InvalidNameException e) {
727:                    // expected
728:                }
729:
730:                client = new MockLdapClient();
731:                context = new LdapContextImpl(client, env, "cn=test");
732:                // listener is null, do nothing
733:                context.addNamingListener("", EventContext.OBJECT_SCOPE, null);
734:                assertFalse(client.getRequest() instanceof  SearchOp);
735:
736:                try {
737:                    context
738:                            .addNamingListener("", 100,
739:                                    new TestNamingListener());
740:                    fail("Should throw IllegalArgumentException");
741:                } catch (IllegalArgumentException e) {
742:                    // expected
743:                }
744:            }
745:
746:            public void test_addNamingListener_with_filter() throws Exception {
747:                MockLdapClient client = new MockLdapClient();
748:                Hashtable<Object, Object> env = new Hashtable<Object, Object>();
749:
750:                context = new LdapContextImpl(client, env, "cn=test");
751:
752:                SearchControls controls = new SearchControls();
753:                controls.setCountLimit(100);
754:                controls.setDerefLinkFlag(true);
755:                controls.setReturningObjFlag(true);
756:                controls.setTimeLimit(5);
757:                context.addNamingListener("test=addlistener",
758:                        "(objectClass=*)", controls, new TestNamingListener());
759:
760:                SearchOp op = (SearchOp) client.getRequest();
761:                assertEquals("test=addlistener,cn=test", op.getBaseObject());
762:                Filter filter = op.getFilter();
763:                assertEquals(Filter.PRESENT_FILTER, filter.getType());
764:                assertEquals("objectClass", filter.getValue());
765:                assertEquals(controls.getCountLimit(), op.getControls()
766:                        .getCountLimit());
767:                assertEquals(controls.getDerefLinkFlag(), op.getControls()
768:                        .getDerefLinkFlag());
769:                assertEquals(controls.getReturningObjFlag(), op.getControls()
770:                        .getReturningObjFlag());
771:                assertEquals(controls.getSearchScope(), op.getControls()
772:                        .getSearchScope());
773:                assertEquals(controls.getTimeLimit(), op.getControls()
774:                        .getTimeLimit());
775:                assertTrue(op.getSearchResult() instanceof  PersistentSearchResult);
776:
777:                try {
778:                    context.search("test=addlistener", "objectClass=*",
779:                            controls);
780:                    fail("Should throw InvalidSearchFilterException");
781:                } catch (InvalidSearchFilterException e) {
782:                    // expected
783:                }
784:
785:                context.addNamingListener("test=addlistener",
786:                        "(objectClass=*)", null, new TestNamingListener());
787:
788:                op = (SearchOp) client.getRequest();
789:                assertEquals(0, op.getControls().getCountLimit());
790:                assertEquals(false, op.getControls().getDerefLinkFlag());
791:                assertEquals(false, op.getControls().getReturningObjFlag());
792:                assertEquals(SearchControls.ONELEVEL_SCOPE, op.getControls()
793:                        .getSearchScope());
794:                assertEquals(0, op.getControls().getTimeLimit());
795:                assertNull(controls.getReturningAttributes());
796:                assertTrue(op.getSearchResult() instanceof  PersistentSearchResult);
797:            }
798:
799:            public void test_removeNamingListener() throws Exception {
800:                MockLdapClient client = new MockLdapClient();
801:                Hashtable<Object, Object> env = new Hashtable<Object, Object>();
802:
803:                context = new LdapContextImpl(client, env, "cn=test");
804:                // remove not registered listener, do nothing
805:                context.removeNamingListener(new TestNamingListener());
806:
807:                NamingListener listener = new TestNamingListener();
808:
809:                context.addNamingListener("test=listener",
810:                        EventContext.OBJECT_SCOPE, listener);
811:                context.removeNamingListener(listener);
812:
813:                listener = new TestNamingListener();
814:                context.addNamingListener("test=listener",
815:                        EventContext.OBJECT_SCOPE, listener);
816:                context.addNamingListener("test=listener",
817:                        EventContext.ONELEVEL_SCOPE, listener);
818:                context.removeNamingListener(listener);
819:            }
820:
821:            public void test_reconnect() throws Exception {
822:                Hashtable<Object, Object> env = new Hashtable<Object, Object>();
823:                env.put("java.naming.ldap.control.connect",
824:                        new Control[] { new PagedResultsControl(10,
825:                                Control.NONCRITICAL) });
826:
827:                MockLdapClient client = new MockLdapClient();
828:                context = new LdapContextImpl(client, env, "cn=test");
829:
830:                Control[] controls = context.getConnectControls();
831:                assertNotNull(controls);
832:                Control c = controls[0];
833:                assertTrue(c instanceof  PagedResultsControl);
834:                assertEquals(Control.NONCRITICAL, ((PagedResultsControl) c)
835:                        .isCritical());
836:
837:                context.reconnect(new Control[] { new SortControl("",
838:                        Control.NONCRITICAL) });
839:
840:                controls = context.getConnectControls();
841:                assertNotNull(controls);
842:                assertEquals(1, controls.length);
843:                c = controls[0];
844:                assertTrue(c instanceof  SortControl);
845:                assertEquals(Control.NONCRITICAL, ((SortControl) c)
846:                        .isCritical());
847:
848:                context.reconnect(null);
849:
850:                assertNull(context.getConnectControls());
851:            }
852:
853:            public static class TestNamingListener implements 
854:                    NamespaceChangeListener {
855:
856:                private NamingEvent event;
857:
858:                private NamingExceptionEvent ex;
859:
860:                public void objectAdded(NamingEvent namingevent) {
861:                    this .event = namingevent;
862:                }
863:
864:                public void objectRemoved(NamingEvent namingevent) {
865:                    this .event = namingevent;
866:                }
867:
868:                public NamingEvent getEvent() {
869:                    return event;
870:                }
871:
872:                public NamingExceptionEvent getExceptionEvent() {
873:                    return ex;
874:                }
875:
876:                public void objectRenamed(NamingEvent namingevent) {
877:                    this .event = namingevent;
878:                }
879:
880:                public void namingExceptionThrown(
881:                        NamingExceptionEvent namingExceptionEvent) {
882:                    this.ex = namingExceptionEvent;
883:
884:                }
885:
886:            }
887:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.