1 17 package org.apache.ldap.server.jndi; 18 19 20 import org.apache.ldap.server.AbstractCoreTest; 21 22 import javax.naming.Context ; 23 import javax.naming.Name ; 24 import javax.naming.NamingException ; 25 import javax.naming.directory.*; 26 import javax.naming.spi.DirObjectFactory ; 27 import javax.naming.spi.DirStateFactory ; 28 import java.util.Hashtable ; 29 30 31 37 public class ObjStateFactoryTest extends AbstractCoreTest 38 { 39 public void testObjectFactory() throws NamingException 40 { 41 super.sysRoot.addToEnvironment( Context.OBJECT_FACTORIES, PersonObjectFactory.class.getName() ); 42 43 Object obj = super.sysRoot.lookup( "uid=akarasulu, ou=users" ); 44 45 Attributes attrs = super.sysRoot.getAttributes( "uid=akarasulu, ou=users" ); 46 47 assertEquals( Person.class, obj.getClass() ); 48 49 Person me = ( Person ) obj; 50 51 assertEquals( attrs.get( "sn" ).get(), me.getLastname() ); 52 53 assertEquals( attrs.get( "cn" ).get(), me.getCn() ); 54 55 assertEquals( attrs.get( "userPassword" ).get(), me.getPassword() ); 56 57 assertEquals( attrs.get( "telephonenumber" ).get(), me.getTelephoneNumber() ); 58 59 assertNull( me.getSeealso() ); 60 61 assertNull( me.getDescription() ); 62 } 63 64 65 public void testStateFactory() throws NamingException 66 { 67 super.sysRoot.addToEnvironment( Context.STATE_FACTORIES, PersonStateFactory.class.getName() ); 68 69 Person p = new Person( "Rodriguez", "Mr. Kerberos", "noices", "555-1212", "erodriguez", "committer" ); 70 71 super.sysRoot.bind( "uid=erodriguez, ou=users", p ); 72 73 Attributes attrs = super.sysRoot.getAttributes( "uid=erodriguez, ou=users" ); 74 75 assertEquals( "Rodriguez", attrs.get( "sn" ).get() ); 76 77 assertEquals( "Mr. Kerberos", attrs.get( "cn" ).get() ); 78 79 assertEquals( "noices", attrs.get( "userPassword" ).get() ); 80 81 assertEquals( "555-1212", attrs.get( "telephonenumber" ).get() ); 82 83 assertEquals( "erodriguez", attrs.get( "seealso" ).get() ); 84 85 assertEquals( "committer", attrs.get( "description" ).get() ); 86 87 } 88 89 90 public static class PersonStateFactory implements DirStateFactory 91 { 92 public Result getStateToBind( Object obj, Name name, Context nameCtx, Hashtable environment, Attributes inAttrs ) throws NamingException 93 { 94 if ( obj instanceof Person ) 96 { 97 98 Attributes outAttrs; 99 100 if ( inAttrs == null ) 101 { 102 outAttrs = new BasicAttributes(true); 103 } 104 else 105 { 106 outAttrs = ( Attributes ) inAttrs.clone(); 107 } 108 109 if ( outAttrs.get( "objectclass" ) == null ) 111 { 112 BasicAttribute oc = new BasicAttribute( "objectclass", "person" ); 113 114 oc.add( "top" ); 115 116 outAttrs.put( oc ); 117 } 118 119 Person per = ( Person ) obj; 120 121 if ( per.getLastname() != null ) 123 { 124 outAttrs.put( "sn", per.getLastname() ); 125 } 126 else 127 { 128 throw new SchemaViolationException( "Person must have surname" ); 129 } 130 131 if ( per.getCn() != null ) 132 { 133 outAttrs.put( "cn", per.getCn() ); 134 } 135 else 136 { 137 throw new SchemaViolationException( "Person must have common name" ); 138 } 139 140 if ( per.getPassword() != null ) 142 { 143 outAttrs.put( "userPassword", per.getPassword() ); 144 } 145 if ( per.getTelephoneNumber() != null ) 146 { 147 outAttrs.put( "telephoneNumber", per.getTelephoneNumber() ); 148 } 149 if ( per.getSeealso() != null ) 150 { 151 outAttrs.put( "seeAlso", per.getSeealso() ); 152 } 153 if ( per.getDescription() != null ) 154 { 155 outAttrs.put( "description", per.getDescription() ); 156 } 157 158 return new DirStateFactory.Result ( null, outAttrs ); 159 } 160 161 return null; 162 } 163 164 165 public Object getStateToBind( Object obj, Name name, Context nameCtx, Hashtable environment ) throws NamingException 166 { 167 throw new UnsupportedOperationException ( "Please use directory support overload with Attributes argument." ); 168 } 169 } 170 171 public static class PersonObjectFactory implements DirObjectFactory 172 { 173 public Object getObjectInstance( Object obj, Name name, Context nameCtx, Hashtable environment, Attributes attrs ) throws Exception 174 { 175 Attribute oc = (attrs != null ? attrs.get("objectclass") : null); 178 if (oc != null && oc.contains("person")) { 179 Attribute attr; 180 String passwd = null; 181 182 attr = attrs.get("userPassword"); 184 if (attr != null) { 185 Object pw = attr.get(); 186 187 if ( pw instanceof String ) 188 passwd = ( String ) pw; 189 else 190 passwd = new String ((byte[]) pw); 191 } 192 Person per = new Person( 193 (String )attrs.get("sn").get(), 194 (String )attrs.get("cn").get(), 195 passwd, 196 (attr=attrs.get("telephoneNumber")) != null ? (String )attr.get() : null, 197 (attr=attrs.get("seealso")) != null ? (String )attr.get() : null, 198 (attr=attrs.get("description")) != null ? (String )attr.get() : null); 199 return per; 200 } 201 return null; 202 } 203 204 205 public Object getObjectInstance( Object obj, Name name, Context nameCtx, Hashtable environment ) throws Exception 206 { 207 throw new UnsupportedOperationException ( "Please use directory support overload with Attributes argument." ); 208 } 209 } 210 211 212 public static class Person 213 { 214 private String sn, cn, pwd, tele, seealso, desc; 215 216 public Person( String sn, String cn, String pwd, String tele, String seealso, String desc ) 217 { 218 this.sn = sn; 219 this.cn = cn; 220 this.pwd = pwd; 221 this.tele = tele; 222 this.seealso = seealso; 223 this.desc = desc; 224 } 225 226 227 public String getLastname() 228 { 229 return sn; 230 } 231 232 233 public String getCn() 234 { 235 return cn; 236 } 237 238 239 public String getPassword() 240 { 241 return pwd; 242 } 243 244 245 public String getTelephoneNumber() 246 { 247 return tele; 248 } 249 250 251 public String getSeealso() 252 { 253 return seealso; 254 } 255 256 257 public String getDescription() 258 { 259 return desc; 260 } 261 } 262 } 263 | Popular Tags |