1 17 package org.apache.ldap.server; 18 19 20 import junit.framework.TestCase; 21 import org.apache.commons.io.FileUtils; 22 import org.apache.commons.lang.exception.NestableRuntimeException; 23 import org.apache.ldap.common.exception.LdapConfigurationException; 24 import org.apache.ldap.common.ldif.LdifIterator; 25 import org.apache.ldap.common.ldif.LdifParser; 26 import org.apache.ldap.common.ldif.LdifParserImpl; 27 import org.apache.ldap.common.message.LockableAttributesImpl; 28 import org.apache.ldap.common.name.LdapName; 29 import org.apache.ldap.server.jndi.EnvKeys; 30 31 import javax.naming.Context ; 32 import javax.naming.InitialContext ; 33 import javax.naming.Name ; 34 import javax.naming.NamingException ; 35 import javax.naming.directory.Attributes ; 36 import javax.naming.ldap.InitialLdapContext ; 37 import javax.naming.ldap.LdapContext ; 38 import java.io.File ; 39 import java.io.IOException ; 40 import java.io.InputStream ; 41 import java.util.Hashtable ; 42 import java.util.ArrayList ; 43 44 45 51 public abstract class AbstractCoreTest extends TestCase 52 { 53 public static final String LDIF = "dn: uid=akarasulu,ou=users,ou=system\n" + 54 "cn: Alex Karasulu\n" + 55 "sn: Karasulu\n" + 56 "givenname: Alex\n" + 57 "objectclass: top\n" + 58 "objectclass: person\n" + 59 "objectclass: organizationalPerson\n" + 60 "objectclass: inetOrgPerson\n" + 61 "ou: Engineering\n" + 62 "ou: People\n" + 63 "l: Bogusville\n" + 64 "uid: akarasulu\n" + 65 "mail: akarasulu@apache.org\n" + 66 "telephonenumber: +1 408 555 4798\n" + 67 "facsimiletelephonenumber: +1 408 555 9751\n" + 68 "roomnumber: 4612\n" + 69 "userpassword: test\n"; 70 71 72 protected LdapContext sysRoot; 73 74 75 protected boolean doDelete = true; 76 77 78 protected Hashtable extras = new Hashtable (); 79 80 81 protected Hashtable overrides = new Hashtable (); 82 83 84 private ArrayList list = null; 85 86 87 public AbstractCoreTest() 88 { 89 list = new ArrayList (); 90 91 Attributes attributes = new LockableAttributesImpl(); 92 93 LdifParserImpl parser = new LdifParserImpl(); 94 95 try 96 { 97 parser.parse( attributes, LDIF ); 98 } 99 catch ( NamingException e ) 100 { 101 e.printStackTrace(); 102 103 throw new NestableRuntimeException( e ); 104 } 105 106 list.add( attributes ); 107 } 108 109 110 116 protected void setUp() throws Exception 117 { 118 super.setUp(); 119 120 extras.put( EnvKeys.TEST_ENTRIES, list ); 121 122 if ( overrides.containsKey( EnvKeys.WKDIR ) ) 123 { 124 doDelete( new File ( ( String ) overrides.get( EnvKeys.WKDIR ) ) ); 125 } 126 else 127 { 128 doDelete( new File ( "target" + File.separator + "apacheds" ) ); 129 } 130 131 setSysRoot( "uid=admin,ou=system", "secret" ); 132 } 133 134 135 138 protected void doDelete( File wkdir ) throws IOException 139 { 140 if ( doDelete ) 141 { 142 if ( wkdir.exists() ) 143 { 144 FileUtils.deleteDirectory( wkdir ); 145 } 146 if ( wkdir.exists() ) 147 { 148 throw new IOException ( "Failed to delete: " + wkdir ); 149 } 150 } 151 } 152 153 154 164 protected LdapContext setSysRoot( String user, String passwd ) throws NamingException 165 { 166 Hashtable env = new Hashtable (); 167 168 env.put( Context.SECURITY_PRINCIPAL, user ); 169 170 env.put( Context.SECURITY_CREDENTIALS, passwd ); 171 172 return setSysRoot( env ); 173 } 174 175 176 185 protected LdapContext setSysRoot( Hashtable env ) throws NamingException 186 { 187 Hashtable envFinal = new Hashtable (); 188 189 envFinal.putAll( extras ); 190 191 envFinal.putAll( env ); 192 193 envFinal.put( Context.PROVIDER_URL, "ou=system" ); 194 195 envFinal.put( EnvKeys.WKDIR, "target" + File.separator + "apacheds" ); 196 197 envFinal.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.CoreContextFactory" ); 198 199 envFinal.putAll( overrides ); 200 201 return sysRoot = new InitialLdapContext ( envFinal, null ); 202 } 203 204 205 206 211 protected void tearDown() throws Exception 212 { 213 super.tearDown(); 214 215 Hashtable env = new Hashtable (); 216 217 env.put( Context.PROVIDER_URL, "ou=system" ); 218 219 env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.CoreContextFactory" ); 220 221 env.put( EnvKeys.SHUTDOWN, "" ); 222 223 env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" ); 224 225 env.put( Context.SECURITY_CREDENTIALS, "secret" ); 226 227 try { new InitialContext ( env ); } catch( Exception e ) {} 228 229 sysRoot = null; 230 231 Runtime.getRuntime().gc(); 232 } 233 234 235 244 protected void importLdif( InputStream in ) throws NamingException 245 { 246 Hashtable env = new Hashtable (); 247 248 env.putAll( sysRoot.getEnvironment() ); 249 250 LdapContext ctx = new InitialLdapContext ( env, null ); 251 252 LdifParser parser = new LdifParserImpl(); 253 254 try 255 { 256 LdifIterator iterator = new LdifIterator( in ); 257 258 while ( iterator.hasNext() ) 259 { 260 Attributes attributes = new LockableAttributesImpl(); 261 262 String ldif = ( String ) iterator.next(); 263 264 parser.parse( attributes, ldif ); 265 266 Name dn = new LdapName( ( String ) attributes.remove( "dn" ).get() ); 267 268 dn.remove( 0 ); 269 270 ctx.createSubcontext( dn, attributes ); 271 } 272 } 273 catch ( Exception e ) 274 { 275 String msg = "failed while trying to parse system ldif file"; 276 277 NamingException ne = new LdapConfigurationException( msg ); 278 279 ne.setRootCause( e ); 280 281 throw ne; 282 } 283 } 284 } 285 | Popular Tags |