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.ldap.common.exception.LdapConfigurationException; 23 import org.apache.ldap.common.ldif.LdifIterator; 24 import org.apache.ldap.common.ldif.LdifParser; 25 import org.apache.ldap.common.ldif.LdifParserImpl; 26 import org.apache.ldap.common.message.LockableAttributesImpl; 27 import org.apache.ldap.common.name.LdapName; 28 import org.apache.ldap.server.jndi.EnvKeys; 29 import org.apache.mina.util.AvailablePortFinder; 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 43 44 50 public abstract class AbstractServerTest extends TestCase 51 { 52 53 protected LdapContext sysRoot; 54 55 56 protected boolean doDelete = true; 57 58 59 protected Hashtable extras = new Hashtable (); 60 61 62 protected Hashtable overrides = new Hashtable (); 63 64 protected int port = -1; 65 66 72 protected void setUp() throws Exception 73 { 74 super.setUp(); 75 76 if ( overrides.containsKey( EnvKeys.WKDIR ) ) 77 { 78 doDelete( new File ( ( String ) overrides.get( EnvKeys.WKDIR ) ) ); 79 } 80 else 81 { 82 doDelete( new File ( "target" + File.separator + "apacheds" ) ); 83 } 84 85 port = AvailablePortFinder.getNextAvailable( 1024 ); 86 87 extras.put( EnvKeys.LDAP_PORT, String.valueOf( port ) ); 88 89 setSysRoot( "uid=admin,ou=system", "secret" ); 90 } 91 92 93 96 protected void doDelete( File wkdir ) throws IOException 97 { 98 if ( doDelete ) 99 { 100 if ( wkdir.exists() ) 101 { 102 FileUtils.deleteDirectory( wkdir ); 103 } 104 if ( wkdir.exists() ) 105 { 106 throw new IOException ( "Failed to delete: " + wkdir ); 107 } 108 } 109 } 110 111 112 122 protected LdapContext setSysRoot( String user, String passwd ) throws NamingException 123 { 124 Hashtable env = new Hashtable (); 125 126 env.put( Context.SECURITY_PRINCIPAL, user ); 127 128 env.put( Context.SECURITY_CREDENTIALS, passwd ); 129 130 return setSysRoot( env ); 131 } 132 133 134 143 protected LdapContext setSysRoot( Hashtable env ) throws NamingException 144 { 145 Hashtable envFinal = new Hashtable (); 146 147 envFinal.putAll( extras ); 148 149 envFinal.putAll( env ); 150 151 envFinal.put( Context.PROVIDER_URL, "ou=system" ); 152 153 envFinal.put( EnvKeys.WKDIR, "target" + File.separator + "apacheds" ); 154 155 envFinal.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.ServerContextFactory" ); 156 157 envFinal.putAll( overrides ); 158 159 return sysRoot = new InitialLdapContext ( envFinal, null ); 160 } 161 162 163 164 169 protected void tearDown() throws Exception 170 { 171 super.tearDown(); 172 173 Hashtable env = new Hashtable (); 174 175 env.put( Context.PROVIDER_URL, "ou=system" ); 176 177 env.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.ldap.server.jndi.ServerContextFactory" ); 178 179 env.put( EnvKeys.SHUTDOWN, "" ); 180 181 env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" ); 182 183 env.put( Context.SECURITY_CREDENTIALS, "secret" ); 184 185 try { new InitialContext ( env ); } catch( Exception e ) {} 186 187 sysRoot = null; 188 189 Runtime.getRuntime().gc(); 190 } 191 192 193 202 protected void importLdif( InputStream in ) throws NamingException 203 { 204 Hashtable env = new Hashtable (); 205 206 env.putAll( sysRoot.getEnvironment() ); 207 208 LdapContext ctx = new InitialLdapContext ( env, null ); 209 210 LdifParser parser = new LdifParserImpl(); 211 212 try 213 { 214 LdifIterator iterator = new LdifIterator( in ); 215 216 while ( iterator.hasNext() ) 217 { 218 Attributes attributes = new LockableAttributesImpl(); 219 220 String ldif = ( String ) iterator.next(); 221 222 parser.parse( attributes, ldif ); 223 224 Name dn = new LdapName( ( String ) attributes.remove( "dn" ).get() ); 225 226 dn.remove( 0 ); 227 228 ctx.createSubcontext( dn, attributes ); 229 } 230 } 231 catch ( Exception e ) 232 { 233 String msg = "failed while trying to parse system ldif file"; 234 235 NamingException ne = new LdapConfigurationException( msg ); 236 237 ne.setRootCause( e ); 238 239 throw ne; 240 } 241 } 242 } 243 | Popular Tags |