Source Code Cross Referenced for LdifScriptTest.java in  » Scripting » scriptella » scriptella » driver » 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 » Scripting » scriptella » scriptella.driver.ldap 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2006-2007 The Scriptella Project Team.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package scriptella.driver.ldap;
017:
018:        import scriptella.AbstractTestCase;
019:        import scriptella.driver.ldap.ldif.Entry;
020:        import scriptella.driver.ldap.ldif.LdifReader;
021:        import scriptella.spi.MockParametersCallbacks;
022:        import scriptella.util.ProxyAdapter;
023:
024:        import javax.naming.CompoundName;
025:        import javax.naming.InvalidNameException;
026:        import javax.naming.Name;
027:        import javax.naming.NamingException;
028:        import javax.naming.directory.Attributes;
029:        import javax.naming.directory.BasicAttribute;
030:        import javax.naming.directory.BasicAttributes;
031:        import javax.naming.directory.DirContext;
032:        import javax.naming.directory.ModificationItem;
033:        import java.io.Reader;
034:        import java.io.StringReader;
035:
036:        /**
037:         * Tests for {@link LdifScript}.
038:         *
039:         * @author Fyodor Kupolov
040:         * @version 1.0
041:         */
042:        public class LdifScriptTest extends AbstractTestCase {
043:            private boolean modified;
044:
045:            protected void setUp() throws Exception {
046:                super .setUp();
047:                modified = false;
048:            }
049:
050:            public void testGetRelativeDN() throws NamingException {
051:                String rootDn = "dc=airius, dc=com";
052:                String rDn = "ou=PD Accountants, ou=Product Development, dc=airius, dc=com";
053:                Name actual = LdifScript.getRelativeDN(rootDn, rDn);
054:                Name expected = newName("ou=PD Accountants, ou=Product Development");
055:                assertEquals(expected, actual);
056:                //DirContext is boound to a root
057:                rootDn = "";
058:                rDn = "dc=com";
059:                actual = LdifScript.getRelativeDN(rootDn, rDn);
060:                expected = newName("dc=com");
061:                assertEquals(expected, actual);
062:                //rDn doesn't belong to DirContext
063:                rootDn = "dc=com";
064:                rDn = "ou=test";
065:                try {
066:                    LdifScript.getRelativeDN(rootDn, rDn);
067:                    fail("getRelativeDN works only when rootDn is a part of rDn");
068:                } catch (NamingException e) {
069:                    //OK
070:                }
071:            }
072:
073:            /**
074:             * Tests changetype: moddn
075:             */
076:            public void testModifyModdn() throws NamingException {
077:                final Entry e = readEntry("# Rename an entry and move all of its children to a new location in\n"
078:                        + "# the directory tree (only implemented by LDAPv3 servers).\n"
079:                        + "dn: ou=PD Accountants, ou=Product Development, dc=airius, dc=com\n"
080:                        + "changetype: moddn\n"
081:                        + "newrdn: ou=Product Development Accountants\n"
082:                        + "deleteoldrdn: 0\n"
083:                        + "newsuperior: ou=Accounting, dc=airius, dc=com\n");
084:                DirContext mock = new ProxyAdapter<DirContext>(DirContext.class) {
085:                    //do not delete old rdn
086:                    public void addToEnvironment(String p, Object v) {
087:                        assertEquals("java.naming.ldap.deleteRDN", p);
088:                        assertEquals("false", v);
089:                    }
090:
091:                    public void removeFromEnvironment(String s) {
092:                    }
093:
094:                    public String getNameInNamespace() {
095:                        return "";
096:                    }
097:
098:                    public void rename(Name oldName, Name newName)
099:                            throws InvalidNameException {
100:                        assertEquals(
101:                                newName("ou=PD Accountants, ou=Product Development, dc=airius, dc=com"),
102:                                oldName);
103:                        assertEquals(
104:                                newName("ou=Product Development Accountants, ou=Accounting, dc=airius, dc=com"),
105:                                newName);
106:                        modified = true;
107:                    }
108:
109:                }.getProxy();
110:                LdifScript.modify(mock, e);
111:                assertTrue("DirContext was not modified", modified);
112:            }
113:
114:            /**
115:             * Tests changetype: modrdn
116:             */
117:            public void testModifyModrdn() throws NamingException {
118:                final Entry e = readEntry("# Rename an entry and move all of its children to a new location in\n"
119:                        + "# the directory tree (only implemented by LDAPv3 servers).\n"
120:                        + "dn: ou=PD Accountants, dc=com\n"
121:                        + "changetype: modrdn\n"
122:                        + "newrdn: ou=Accountants\n"
123:                        + "deleteoldrdn: 1\n");
124:                DirContext mock = new ProxyAdapter<DirContext>(DirContext.class) {
125:                    //Delete old rdn
126:                    public void addToEnvironment(String p, Object v) {
127:                        assertEquals("java.naming.ldap.deleteRDN", p);
128:                        assertEquals("true", v);
129:                    }
130:
131:                    public void removeFromEnvironment(String s) {
132:                    }
133:
134:                    public String getNameInNamespace() {
135:                        return "";
136:                    }
137:
138:                    public void rename(Name oldName, Name newName)
139:                            throws InvalidNameException {
140:                        assertEquals(newName("ou=PD Accountants, dc=com"),
141:                                oldName);
142:                        assertEquals(newName("ou=Accountants, dc=com"), newName);
143:                        modified = true;
144:                    }
145:
146:                }.getProxy();
147:                LdifScript.modify(mock, e);
148:                assertTrue("DirContext was not modified", modified);
149:            }
150:
151:            /**
152:             * Tests add entry
153:             *
154:             */
155:            public void testModifyAdd() throws NamingException {
156:                final Entry e = readEntry("dn: cn=ldap,dc=scriptella\n"
157:                        + "cn: ldap\n" + "objectClass: top\n"
158:                        + "objectClass: driver\n" + "envVars:");
159:                DirContext mock = new ProxyAdapter<DirContext>(DirContext.class) {
160:                    public String getNameInNamespace() {
161:                        return "dc=scriptella";
162:                    }
163:
164:                    public DirContext createSubcontext(Name name,
165:                            Attributes attrs) throws InvalidNameException {
166:                        assertEquals(newName("cn=ldap"), name);
167:                        BasicAttributes exp = new BasicAttributes(true);
168:                        exp.put("cn", "ldap");
169:                        final BasicAttribute oc = new BasicAttribute(
170:                                "objectClass");
171:                        oc.add("top");
172:                        oc.add("driver");
173:                        exp.put(oc);
174:                        exp.put("envVars", null);
175:                        assertEquals(exp, attrs);
176:                        modified = true;
177:                        return null;
178:                    }
179:
180:                }.getProxy();
181:                LdifScript.modify(mock, e);
182:                assertTrue("DirContext was not modified", modified);
183:            }
184:
185:            /**
186:             * Tests entry removing
187:             */
188:            public void testDelete() throws NamingException {
189:                final Entry e = readEntry("dn: cn=ldap,dc=scriptella\n"
190:                        + "changetype: delete\n");
191:                DirContext mock = new ProxyAdapter<DirContext>(DirContext.class) {
192:                    public String getNameInNamespace() {
193:                        return "dc=scriptella";
194:                    }
195:
196:                    public void destroySubcontext(Name name)
197:                            throws NamingException {
198:                        assertEquals(newName("cn=ldap"), name);
199:                        modified = true;
200:                    }
201:
202:                }.getProxy();
203:                LdifScript.modify(mock, e);
204:                assertTrue("DirContext was not modified", modified);
205:            }
206:
207:            /**
208:             * Tests changetype: modify
209:             */
210:            public void testModify() throws NamingException {
211:                final Entry e = readEntry("dn: cn=ldap, dc=scriptella\n"
212:                        + "changetype: modify\n" + "add: postaladdress\n"
213:                        + "postaladdress: 123 Anystreet\n" + "-\n"
214:                        + "delete: description\n" + "-\n" + "replace: phone\n"
215:                        + "phone: 1234\n" + "phone: 5678\n" + "-\n"
216:                        + "delete: fax\n" + "fax: 1111\n");
217:
218:                DirContext mock = new ProxyAdapter<DirContext>(DirContext.class) {
219:                    public String getNameInNamespace() {
220:                        return "";
221:                    }
222:
223:                    public void modifyAttributes(Name name,
224:                            ModificationItem[] mods)
225:                            throws InvalidNameException {
226:                        assertEquals(newName("cn=ldap, dc=scriptella"), name);
227:                        ModificationItem[] expected = new ModificationItem[4];
228:                        expected[0] = new ModificationItem(
229:                                DirContext.ADD_ATTRIBUTE, new BasicAttribute(
230:                                        "postaladdress", "123 Anystreet"));
231:                        expected[1] = new ModificationItem(
232:                                DirContext.REMOVE_ATTRIBUTE,
233:                                new BasicAttribute("description", null));
234:                        BasicAttribute phone = new BasicAttribute("phone");
235:                        phone.add("1234");
236:                        phone.add("5678");
237:                        expected[2] = new ModificationItem(
238:                                DirContext.REPLACE_ATTRIBUTE, phone);
239:                        expected[3] = new ModificationItem(
240:                                DirContext.REMOVE_ATTRIBUTE,
241:                                new BasicAttribute("fax", "1111"));
242:                        for (int i = 0; i < expected.length; i++) {
243:                            assertEquals(expected[i].getAttribute(), mods[i]
244:                                    .getAttribute());
245:                            assertEquals(expected[i].getModificationOp(),
246:                                    mods[i].getModificationOp());
247:                        }
248:                        modified = true;
249:                    }
250:
251:                }.getProxy();
252:                LdifScript.modify(mock, e);
253:                assertTrue("DirContext was not modified", modified);
254:            }
255:
256:            /**
257:             * Tests if substituted data goes to LDAP.
258:             */
259:            public void testExecute() {
260:                Reader ldif = new StringReader(
261:                        "# Rename an entry and move all of its children to a new location in\n"
262:                                + "# the directory tree (only implemented by LDAPv3 servers).\n"
263:                                + "dn: ou=$test, dc=scriptella\n"
264:                                + "ou: $test\n");
265:
266:                DirContext mock = new ProxyAdapter<DirContext>(DirContext.class) {
267:                    public String getNameInNamespace() {
268:                        return "dc=scriptella";
269:                    }
270:
271:                    public DirContext createSubcontext(Name name,
272:                            Attributes attrs) throws InvalidNameException {
273:                        assertEquals(newName("ou=*test*"), name);
274:                        BasicAttributes exp = new BasicAttributes(true);
275:                        exp.put("ou", "*test*");
276:                        assertEquals(exp, attrs);
277:                        modified = true;
278:                        return null;
279:                    }
280:
281:                }.getProxy();
282:                LdifScript ls = new LdifScript(new LdapConnection());
283:                ls.execute(ldif, mock, MockParametersCallbacks.SIMPLE);
284:
285:                assertTrue("DirContext was not modified", modified);
286:            }
287:
288:            /**
289:             * Tests error handling
290:             */
291:            public void testErrorHadnling() throws NamingException {
292:                String ldif = "dn: cn=ldap,dc=scriptella\n"
293:                        + "changetype: delete\n";
294:                Reader reader = new StringReader(ldif);
295:                DirContext mock = new ProxyAdapter<DirContext>(DirContext.class) {
296:                    public String getNameInNamespace() {
297:                        return "dc=scriptella";
298:                    }
299:
300:                    public void destroySubcontext(Name name)
301:                            throws NamingException {
302:                        throw new NamingException("Failure");
303:                    }
304:                }.getProxy();
305:                try {
306:                    LdifScript ls = new LdifScript(new LdapConnection());
307:                    ls.execute(reader, mock,
308:                            MockParametersCallbacks.UNSUPPORTED);
309:
310:                } catch (LdapProviderException e) {
311:                    Throwable ne = e.getNativeException();
312:                    assertEquals(NamingException.class, ne.getClass());
313:                    assertEquals("Failure", ne.getMessage());
314:                    assertEquals(ldif, e.getErrorStatement());
315:                }
316:            }
317:
318:            /**
319:             * @param s ldif
320:             * @return entry from ldif
321:             */
322:            private static Entry readEntry(String s) {
323:                LdifReader lr = new LdifReader(s);
324:                return lr.next();
325:            }
326:
327:            private static Name newName(String name)
328:                    throws InvalidNameException {
329:                return new CompoundName(name, LdifScript.DN_SYNTAX);
330:            }
331:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.