1 16 package scriptella.driver.ldap; 17 18 import scriptella.AbstractTestCase; 19 import scriptella.driver.ldap.ldif.Entry; 20 import scriptella.driver.ldap.ldif.LdifReader; 21 import scriptella.spi.MockParametersCallbacks; 22 import scriptella.util.ProxyAdapter; 23 24 import javax.naming.CompoundName ; 25 import javax.naming.InvalidNameException ; 26 import javax.naming.Name ; 27 import javax.naming.NamingException ; 28 import javax.naming.directory.Attributes ; 29 import javax.naming.directory.BasicAttribute ; 30 import javax.naming.directory.BasicAttributes ; 31 import javax.naming.directory.DirContext ; 32 import javax.naming.directory.ModificationItem ; 33 import java.io.Reader ; 34 import java.io.StringReader ; 35 36 42 public class LdifScriptTest extends AbstractTestCase { 43 private boolean modified; 44 45 protected void setUp() throws Exception { 46 super.setUp(); 47 modified=false; 48 } 49 50 public void testGetRelativeDN() throws NamingException { 51 String rootDn = "dc=airius, dc=com"; 52 String rDn = "ou=PD Accountants, ou=Product Development, dc=airius, dc=com"; 53 Name actual = LdifScript.getRelativeDN(rootDn, rDn); 54 Name expected = newName("ou=PD Accountants, ou=Product Development"); 55 assertEquals(expected, actual); 56 rootDn = ""; 58 rDn = "dc=com"; 59 actual = LdifScript.getRelativeDN(rootDn, rDn); 60 expected = newName("dc=com"); 61 assertEquals(expected, actual); 62 rootDn = "dc=com"; 64 rDn = "ou=test"; 65 try { 66 LdifScript.getRelativeDN(rootDn, rDn); 67 fail("getRelativeDN works only when rootDn is a part of rDn"); 68 } catch (NamingException e) { 69 } 71 } 72 73 76 public void testModifyModdn() throws NamingException { 77 final Entry e = readEntry( 78 "# Rename an entry and move all of its children to a new location in\n" + 79 "# the directory tree (only implemented by LDAPv3 servers).\n" + 80 "dn: ou=PD Accountants, ou=Product Development, dc=airius, dc=com\n" + 81 "changetype: moddn\n" + 82 "newrdn: ou=Product Development Accountants\n" + 83 "deleteoldrdn: 0\n" + 84 "newsuperior: ou=Accounting, dc=airius, dc=com\n"); 85 DirContext mock = new ProxyAdapter<DirContext >(DirContext .class) { 86 public void addToEnvironment(String p, Object v) { 88 assertEquals("java.naming.ldap.deleteRDN", p); 89 assertEquals("false", v); 90 } 91 92 public void removeFromEnvironment(String s) { 93 } 94 95 96 public String getNameInNamespace() { 97 return ""; 98 } 99 100 public void rename(Name oldName, Name newName) throws InvalidNameException { 101 assertEquals(newName("ou=PD Accountants, ou=Product Development, dc=airius, dc=com"), oldName); 102 assertEquals(newName("ou=Product Development Accountants, ou=Accounting, dc=airius, dc=com"), newName); 103 modified=true; 104 } 105 106 }.getProxy(); 107 LdifScript.modify(mock, e); 108 assertTrue("DirContext was not modified", modified); 109 } 110 111 114 public void testModifyModrdn() throws NamingException { 115 final Entry e = readEntry( 116 "# Rename an entry and move all of its children to a new location in\n" + 117 "# the directory tree (only implemented by LDAPv3 servers).\n" + 118 "dn: ou=PD Accountants, dc=com\n" + 119 "changetype: modrdn\n" + 120 "newrdn: ou=Accountants\n" + 121 "deleteoldrdn: 1\n"); 122 DirContext mock = new ProxyAdapter<DirContext >(DirContext .class) { 123 public void addToEnvironment(String p, Object v) { 125 assertEquals("java.naming.ldap.deleteRDN", p); 126 assertEquals("true", v); 127 } 128 129 public void removeFromEnvironment(String s) { 130 } 131 132 public String getNameInNamespace() { 133 return ""; 134 } 135 136 public void rename(Name oldName, Name newName) throws InvalidNameException { 137 assertEquals(newName("ou=PD Accountants, dc=com"), oldName); 138 assertEquals(newName("ou=Accountants, dc=com"), newName); 139 modified=true; 140 } 141 142 }.getProxy(); 143 LdifScript.modify(mock, e); 144 assertTrue("DirContext was not modified", modified); 145 } 146 147 151 public void testModifyAdd() throws NamingException { 152 final Entry e = readEntry( 153 "dn: cn=ldap,dc=scriptella\n" + 154 "cn: ldap\n" + 155 "objectClass: top\n" + 156 "objectClass: driver\n" + 157 "envVars:"); 158 DirContext mock = new ProxyAdapter<DirContext >(DirContext .class) { 159 public String getNameInNamespace() { 160 return "dc=scriptella"; 161 } 162 163 public DirContext createSubcontext(Name name, Attributes attrs) throws InvalidNameException { 164 assertEquals(newName("cn=ldap"), name); 165 BasicAttributes exp = new BasicAttributes (true); 166 exp.put("cn", "ldap"); 167 final BasicAttribute oc = new BasicAttribute ("objectClass"); 168 oc.add("top"); 169 oc.add("driver"); 170 exp.put(oc); 171 exp.put("envVars", null); 172 assertEquals(exp, attrs); 173 modified=true; 174 return null; 175 } 176 177 }.getProxy(); 178 LdifScript.modify(mock, e); 179 assertTrue("DirContext was not modified", modified); 180 } 181 182 185 public void testDelete() throws NamingException { 186 final Entry e = readEntry( 187 "dn: cn=ldap,dc=scriptella\n" + 188 "changetype: delete\n"); 189 DirContext mock = new ProxyAdapter<DirContext >(DirContext .class) { 190 public String getNameInNamespace() { 191 return "dc=scriptella"; 192 } 193 194 public void destroySubcontext(Name name) throws NamingException { 195 assertEquals(newName("cn=ldap"), name); 196 modified=true; 197 } 198 199 }.getProxy(); 200 LdifScript.modify(mock, e); 201 assertTrue("DirContext was not modified", modified); 202 } 203 204 205 208 public void testModify() throws NamingException { 209 final Entry e = readEntry( 210 "dn: cn=ldap, dc=scriptella\n" + 211 "changetype: modify\n" + 212 "add: postaladdress\n" + 213 "postaladdress: 123 Anystreet\n" + 214 "-\n" + 215 "delete: description\n" + 216 "-\n" + 217 "replace: phone\n" + 218 "phone: 1234\n" + 219 "phone: 5678\n" + 220 "-\n" + 221 "delete: fax\n" + 222 "fax: 1111\n"); 223 224 DirContext mock = new ProxyAdapter<DirContext >(DirContext .class) { 225 public String getNameInNamespace() { 226 return ""; 227 } 228 public void modifyAttributes(Name name, ModificationItem [] mods) throws InvalidNameException { 229 assertEquals(newName("cn=ldap, dc=scriptella"), name); 230 ModificationItem [] expected = new ModificationItem [4]; 231 expected[0]=new ModificationItem (DirContext.ADD_ATTRIBUTE, 232 new BasicAttribute ("postaladdress", "123 Anystreet")); 233 expected[1]=new ModificationItem (DirContext.REMOVE_ATTRIBUTE, 234 new BasicAttribute ("description", null)); 235 BasicAttribute phone = new BasicAttribute ("phone"); 236 phone.add("1234");phone.add("5678"); 237 expected[2]=new ModificationItem (DirContext.REPLACE_ATTRIBUTE, 238 phone); 239 expected[3]=new ModificationItem (DirContext.REMOVE_ATTRIBUTE, 240 new BasicAttribute ("fax", "1111")); 241 for (int i = 0; i < expected.length; i++) { 242 assertEquals(expected[i].getAttribute(), mods[i].getAttribute()); 243 assertEquals(expected[i].getModificationOp(), mods[i].getModificationOp()); 244 } 245 modified=true; 246 } 247 248 }.getProxy(); 249 LdifScript.modify(mock, e); 250 assertTrue("DirContext was not modified", modified); 251 } 252 253 256 public void testExecute() { 257 Reader ldif = new StringReader ( 258 "# Rename an entry and move all of its children to a new location in\n" + 259 "# the directory tree (only implemented by LDAPv3 servers).\n" + 260 "dn: ou=$test, dc=scriptella\n" + 261 "ou: $test\n"); 262 263 DirContext mock = new ProxyAdapter<DirContext >(DirContext .class) { 264 public String getNameInNamespace() { 265 return "dc=scriptella"; 266 } 267 268 public DirContext createSubcontext(Name name, Attributes attrs) throws InvalidNameException { 269 assertEquals(newName("ou=*test*"), name); 270 BasicAttributes exp = new BasicAttributes (true); 271 exp.put("ou", "*test*"); 272 assertEquals(exp, attrs); 273 modified=true; 274 return null; 275 } 276 277 }.getProxy(); 278 LdifScript ls = new LdifScript(new LdapConnection()); 279 ls.execute(ldif, mock, MockParametersCallbacks.SIMPLE); 280 281 assertTrue("DirContext was not modified", modified); 282 } 283 284 287 public void testErrorHadnling() throws NamingException { 288 String ldif = "dn: cn=ldap,dc=scriptella\n" + 289 "changetype: delete\n"; 290 Reader reader = new StringReader (ldif); 291 DirContext mock = new ProxyAdapter<DirContext >(DirContext .class) { 292 public String getNameInNamespace() { 293 return "dc=scriptella"; 294 } 295 296 public void destroySubcontext(Name name) throws NamingException { 297 throw new NamingException ("Failure"); 298 } 299 }.getProxy(); 300 try { 301 LdifScript ls = new LdifScript(new LdapConnection()); 302 ls.execute(reader, mock, MockParametersCallbacks.UNSUPPORTED); 303 304 } catch (LdapProviderException e) { 305 Throwable ne = e.getNativeException(); 306 assertEquals(NamingException .class, ne.getClass()); 307 assertEquals("Failure", ne.getMessage()); 308 assertEquals(ldif, e.getErrorStatement()); 309 } 310 } 311 312 313 317 private static Entry readEntry(String s) { 318 LdifReader lr = new LdifReader(new StringReader (s)); 319 return lr.next(); 320 } 321 322 private static Name newName(String name) throws InvalidNameException { 323 return new CompoundName (name, LdifScript.DN_SYNTAX); 324 } 325 } 326 | Popular Tags |