Source Code Cross Referenced for DefaultInitialDirContextFactoryTests.java in  » Security » acegi-security » org » acegisecurity » 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 » Security » acegi security » org.acegisecurity.ldap 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002:         *
003:         * Licensed under the Apache License, Version 2.0 (the "License");
004:         * you may not use this file except in compliance with the License.
005:         * You may obtain a copy of the License at
006:         *
007:         *     http://www.apache.org/licenses/LICENSE-2.0
008:         *
009:         * Unless required by applicable law or agreed to in writing, software
010:         * distributed under the License is distributed on an "AS IS" BASIS,
011:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012:         * See the License for the specific language governing permissions and
013:         * limitations under the License.
014:         */
015:
016:        package org.acegisecurity.ldap;
017:
018:        import org.acegisecurity.AcegiMessageSource;
019:        import org.acegisecurity.BadCredentialsException;
020:
021:        import java.util.Hashtable;
022:
023:        import javax.naming.Context;
024:        import javax.naming.directory.DirContext;
025:
026:        /**
027:         * Tests {@link org.acegisecurity.ldap.DefaultInitialDirContextFactory}.
028:         *
029:         * @author Luke Taylor
030:         * @version $Id: DefaultInitialDirContextFactoryTests.java 1496 2006-05-23 13:38:33Z benalex $
031:         */
032:        public class DefaultInitialDirContextFactoryTests extends
033:                AbstractLdapServerTestCase {
034:            //~ Instance fields ================================================================================================
035:
036:            DefaultInitialDirContextFactory idf;
037:
038:            //~ Methods ========================================================================================================
039:
040:            public void onSetUp() {
041:                idf = getInitialCtxFactory();
042:                idf.setMessageSource(new AcegiMessageSource());
043:            }
044:
045:            public void testAnonymousBindSucceeds() throws Exception {
046:                DirContext ctx = idf.newInitialDirContext();
047:                // Connection pooling should be set by default for anon users.
048:                // Can't rely on this property being there with embedded server
049:                // assertEquals("true",ctx.getEnvironment().get("com.sun.jndi.ldap.connect.pool"));
050:                ctx.close();
051:            }
052:
053:            public void testBaseDnIsParsedFromCorrectlyFromUrl() {
054:                idf = new DefaultInitialDirContextFactory(
055:                        "ldap://acegisecurity.org/dc=acegisecurity,dc=org");
056:                assertEquals("dc=acegisecurity,dc=org", idf.getRootDn());
057:
058:                // Check with an empty root
059:                idf = new DefaultInitialDirContextFactory(
060:                        "ldap://acegisecurity.org/");
061:                assertEquals("", idf.getRootDn());
062:
063:                // Empty root without trailing slash
064:                idf = new DefaultInitialDirContextFactory(
065:                        "ldap://acegisecurity.org");
066:                assertEquals("", idf.getRootDn());
067:            }
068:
069:            public void testBindAsManagerFailsIfNoPasswordSet()
070:                    throws Exception {
071:                idf.setManagerDn(MANAGER_USER);
072:
073:                DirContext ctx = null;
074:
075:                try {
076:                    ctx = idf.newInitialDirContext();
077:                    fail("Binding with no manager password should fail.");
078:
079:                    // Can't rely on this property being there with embedded server
080:                    //        assertEquals("true",ctx.getEnvironment().get("com.sun.jndi.ldap.connect.pool"));
081:                } catch (BadCredentialsException expected) {
082:                }
083:
084:                LdapUtils.closeContext(ctx);
085:            }
086:
087:            public void testBindAsManagerSucceeds() throws Exception {
088:                idf.setManagerPassword(MANAGER_PASSWORD);
089:                idf.setManagerDn(MANAGER_USER);
090:
091:                DirContext ctx = idf.newInitialDirContext();
092:                // Can't rely on this property being there with embedded server
093:                //        assertEquals("true",ctx.getEnvironment().get("com.sun.jndi.ldap.connect.pool"));
094:                ctx.close();
095:            }
096:
097:            public void testConnectionAsSpecificUserSucceeds() throws Exception {
098:                DirContext ctx = idf.newInitialDirContext(
099:                        "uid=Bob,ou=people,dc=acegisecurity,dc=org",
100:                        "bobspassword");
101:                // We don't want pooling for specific users.
102:                // assertNull(ctx.getEnvironment().get("com.sun.jndi.ldap.connect.pool"));
103:                //        com.sun.jndi.ldap.LdapPoolManager.showStats(System.out);
104:                ctx.close();
105:            }
106:
107:            public void testConnectionFailure() throws Exception {
108:                // Use the wrong port
109:                idf = new DefaultInitialDirContextFactory(
110:                        "ldap://localhost:60389");
111:                idf
112:                        .setInitialContextFactory("com.sun.jndi.ldap.LdapCtxFactory");
113:
114:                Hashtable env = new Hashtable();
115:                env.put("com.sun.jndi.ldap.connect.timeout", "200");
116:                idf.setExtraEnvVars(env);
117:                idf.setUseConnectionPool(false); // coverage purposes only
118:
119:                try {
120:                    idf.newInitialDirContext();
121:                    fail("Connection succeeded unexpectedly");
122:                } catch (LdapDataAccessException expected) {
123:                }
124:            }
125:
126:            public void testEnvironment() {
127:                idf = new DefaultInitialDirContextFactory(
128:                        "ldap://acegisecurity.org/");
129:
130:                // check basic env
131:                Hashtable env = idf.getEnvironment();
132:                //assertEquals("com.sun.jndi.ldap.LdapCtxFactory", env.get(Context.INITIAL_CONTEXT_FACTORY));
133:                assertEquals("ldap://acegisecurity.org/", env
134:                        .get(Context.PROVIDER_URL));
135:                assertEquals("simple", env.get(Context.SECURITY_AUTHENTICATION));
136:                assertNull(env.get(Context.SECURITY_PRINCIPAL));
137:                assertNull(env.get(Context.SECURITY_CREDENTIALS));
138:
139:                // Ctx factory.
140:                idf
141:                        .setInitialContextFactory("org.acegisecurity.NonExistentCtxFactory");
142:                env = idf.getEnvironment();
143:                assertEquals("org.acegisecurity.NonExistentCtxFactory", env
144:                        .get(Context.INITIAL_CONTEXT_FACTORY));
145:
146:                // Auth type
147:                idf.setAuthenticationType("myauthtype");
148:                env = idf.getEnvironment();
149:                assertEquals("myauthtype", env
150:                        .get(Context.SECURITY_AUTHENTICATION));
151:
152:                // Check extra vars
153:                Hashtable extraVars = new Hashtable();
154:                extraVars.put("extravar", "extravarvalue");
155:                idf.setExtraEnvVars(extraVars);
156:                env = idf.getEnvironment();
157:                assertEquals("extravarvalue", env.get("extravar"));
158:            }
159:
160:            public void testInvalidPasswordCausesBadCredentialsException()
161:                    throws Exception {
162:                idf.setManagerDn(MANAGER_USER);
163:                idf.setManagerPassword("wrongpassword");
164:
165:                DirContext ctx = null;
166:
167:                try {
168:                    ctx = idf.newInitialDirContext();
169:                    fail("Binding with wrong credentials should fail.");
170:                } catch (BadCredentialsException expected) {
171:                }
172:
173:                LdapUtils.closeContext(ctx);
174:            }
175:
176:            public void testMultipleProviderUrlsAreAccepted() {
177:                idf = new DefaultInitialDirContextFactory(
178:                        "ldaps://acegisecurity.org/dc=acegisecurity,dc=org "
179:                                + "ldap://monkeymachine.co.uk/dc=acegisecurity,dc=org");
180:            }
181:
182:            public void testMultipleProviderUrlsWithDifferentRootsAreRejected() {
183:                try {
184:                    idf = new DefaultInitialDirContextFactory(
185:                            "ldap://acegisecurity.org/dc=acegisecurity,dc=org "
186:                                    + "ldap://monkeymachine.co.uk/dc=someotherplace,dc=org");
187:                    fail("Different root DNs should cause an exception");
188:                } catch (IllegalArgumentException expected) {
189:                }
190:            }
191:
192:            public void testSecureLdapUrlIsSupported() {
193:                idf = new DefaultInitialDirContextFactory(
194:                        "ldaps://localhost/dc=acegisecurity,dc=org");
195:                assertEquals("dc=acegisecurity,dc=org", idf.getRootDn());
196:            }
197:
198:            //    public void testNonLdapUrlIsRejected() throws Exception {
199:            //        DefaultInitialDirContextFactory idf = new DefaultInitialDirContextFactory();
200:            //
201:            //        idf.setUrl("http://acegisecurity.org/dc=acegisecurity,dc=org");
202:            //        idf.setInitialContextFactory(CoreContextFactory.class.getName());
203:            //
204:            //        try {
205:            //            idf.afterPropertiesSet();
206:            //            fail("Expected exception for non 'ldap://' URL");
207:            //        } catch(IllegalArgumentException expected) {
208:            //        }
209:            //    }
210:            public void testServiceLocationUrlIsSupported() {
211:                idf = new DefaultInitialDirContextFactory(
212:                        "ldap:///dc=acegisecurity,dc=org");
213:                assertEquals("dc=acegisecurity,dc=org", idf.getRootDn());
214:            }
215:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.