You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ma...@apache.org on 2007/08/13 01:43:07 UTC
svn commit: r565206 - in
/directory/apacheds/trunk/mitosis/src/test/java/org/apache/directory/mitosis/service:
AbstractReplicationServiceTestCase.java ReplicationServiceITest.java
Author: malderson
Date: Sun Aug 12 16:43:07 2007
New Revision: 565206
URL: http://svn.apache.org/viewvc?view=rev&rev=565206
Log:
Refactored the ReplicationServiceITest to simplify creation of new ITests.
Added:
directory/apacheds/trunk/mitosis/src/test/java/org/apache/directory/mitosis/service/AbstractReplicationServiceTestCase.java
Modified:
directory/apacheds/trunk/mitosis/src/test/java/org/apache/directory/mitosis/service/ReplicationServiceITest.java
Added: directory/apacheds/trunk/mitosis/src/test/java/org/apache/directory/mitosis/service/AbstractReplicationServiceTestCase.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/mitosis/src/test/java/org/apache/directory/mitosis/service/AbstractReplicationServiceTestCase.java?view=auto&rev=565206
==============================================================================
--- directory/apacheds/trunk/mitosis/src/test/java/org/apache/directory/mitosis/service/AbstractReplicationServiceTestCase.java (added)
+++ directory/apacheds/trunk/mitosis/src/test/java/org/apache/directory/mitosis/service/AbstractReplicationServiceTestCase.java Sun Aug 12 16:43:07 2007
@@ -0,0 +1,209 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.directory.mitosis.service;
+
+import java.io.File;
+import java.net.InetSocketAddress;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.ldap.InitialLdapContext;
+import javax.naming.ldap.LdapContext;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.directory.mitosis.common.Replica;
+import org.apache.directory.mitosis.common.ReplicaId;
+import org.apache.directory.mitosis.configuration.MutableReplicationInterceptorConfiguration;
+import org.apache.directory.mitosis.configuration.ReplicationConfiguration;
+import org.apache.directory.server.core.DirectoryService;
+import org.apache.directory.server.core.configuration.InterceptorConfiguration;
+import org.apache.directory.server.core.configuration.MutableStartupConfiguration;
+import org.apache.directory.server.core.configuration.ShutdownConfiguration;
+import org.apache.directory.server.core.jndi.CoreContextFactory;
+import org.apache.mina.util.AvailablePortFinder;
+
+/**
+ * TODO AbstractReplicationServiceTestCase.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public abstract class AbstractReplicationServiceTestCase extends TestCase
+{
+ protected Map<String, LdapContext> contexts = new HashMap<String, LdapContext>();
+ protected Map<String, ReplicationService> replicationServices = new HashMap<String, ReplicationService>();
+
+ protected void setUp() throws Exception
+ {
+ createReplicas( new String[] { "A", "B", "C" } );
+ }
+
+ protected void tearDown() throws Exception
+ {
+ destroyAllReplicas();
+ }
+
+ @SuppressWarnings("unchecked")
+ protected void createReplicas( String[] names ) throws Exception
+ {
+ int lastAvailablePort = 1024;
+
+ Replica[] replicas = new Replica[ names.length ];
+ for( int i = 0; i < names.length; i++ )
+ {
+ int replicationPort = AvailablePortFinder
+ .getNextAvailable( lastAvailablePort );
+ lastAvailablePort = replicationPort + 1;
+
+ replicas[ i ] = new Replica( new ReplicaId( names[ i ] ),
+ new InetSocketAddress( "127.0.0.1", replicationPort ) );
+ }
+
+ Random random = new Random();
+ String homeDirectory = System.getProperty( "java.io.tmpdir" )
+ + File.separator + "mitosis-"
+ + Long.toHexString( random.nextLong() );
+
+ for( int i = 0; i < replicas.length; i++ )
+ {
+ Replica replica = replicas[ i ];
+ String replicaId = replicas[ i ].getId().getId();
+ MutableStartupConfiguration ldapCfg = new MutableStartupConfiguration(
+ replicaId );
+
+ File workDir = new File( homeDirectory + File.separator
+ + ldapCfg.getInstanceId() );
+
+ ldapCfg.setShutdownHookEnabled( false );
+ ldapCfg.setWorkingDirectory( workDir );
+
+ List<InterceptorConfiguration> interceptorCfgs = ldapCfg.getInterceptorConfigurations();
+
+ ReplicationConfiguration replicationCfg = new ReplicationConfiguration();
+ replicationCfg.setReplicaId( replica.getId() );
+ // Disable automatic replication to prevent unexpected behavior
+ replicationCfg.setReplicationInterval(0);
+ replicationCfg.setServerPort( replica.getAddress().getPort() );
+ for( int j = 0; j < replicas.length; j++ )
+ {
+ if( replicas[ j ] != replica )
+ {
+ replicationCfg.addPeerReplica( replicas[ j ] );
+ }
+ }
+
+ MutableReplicationInterceptorConfiguration interceptorCfg =
+ new MutableReplicationInterceptorConfiguration();
+ interceptorCfg.setName( "mitosis" );
+ interceptorCfg.setInterceptorClassName( ReplicationService.class.getName() );
+ interceptorCfg.setReplicationConfiguration( replicationCfg );
+ interceptorCfgs.add( interceptorCfg );
+
+ ldapCfg.setInterceptorConfigurations( interceptorCfgs );
+
+ if( workDir.exists() )
+ {
+ FileUtils.deleteDirectory( workDir );
+ }
+
+ Hashtable env = new Hashtable( ldapCfg.toJndiEnvironment() );
+ env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
+ env.put( Context.SECURITY_CREDENTIALS, "secret" );
+ env.put( Context.SECURITY_AUTHENTICATION, "simple" );
+ env.put( Context.PROVIDER_URL, "" );
+ env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class
+ .getName() );
+
+ // Initialize the server instance.
+ LdapContext context = new InitialLdapContext( env, null );
+ contexts.put( replicaId, context );
+ ReplicationService replicationService = (ReplicationService) DirectoryService.getInstance( replicaId ).getConfiguration().getInterceptorChain().get( "mitosis" );
+ replicationServices.put( replicaId, replicationService );
+ }
+
+ // Ensure all replicas have had a chance to connect to each other since the last one started.
+ for( Iterator<ReplicationService> i = replicationServices.values().iterator(); i.hasNext(); )
+ {
+ i.next().interruptConnectors();
+ }
+ Thread.sleep( 5000 );
+ }
+
+ protected LdapContext getReplicaContext( String name ) throws Exception
+ {
+ LdapContext context = contexts.get( name );
+ if( context == null )
+ {
+ throw new IllegalArgumentException( "No such replica: " + name );
+ }
+
+ return ( LdapContext ) context.lookup( "" );
+ }
+
+ @SuppressWarnings("unchecked")
+ protected void destroyAllReplicas() throws Exception
+ {
+ for( Iterator<String> i = contexts.keySet().iterator(); i.hasNext(); )
+ {
+ String replicaId = i.next();
+ File workDir = DirectoryService.getInstance( replicaId )
+ .getConfiguration().getStartupConfiguration()
+ .getWorkingDirectory();
+
+ Hashtable env = new Hashtable();
+ env.put( Context.PROVIDER_URL, "ou=system" );
+ env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class
+ .getName() );
+ env.putAll( new ShutdownConfiguration( replicaId )
+ .toJndiEnvironment() );
+ env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
+ env.put( Context.SECURITY_CREDENTIALS, "secret" );
+ try
+ {
+ new InitialContext( env );
+ }
+ catch( Exception e )
+ {
+ }
+
+ try
+ {
+ FileUtils.deleteDirectory( workDir );
+ }
+ catch( Exception e )
+ {
+ e.printStackTrace();
+ }
+
+ workDir.getParentFile().delete();
+
+ i.remove();
+ }
+ }
+}
Modified: directory/apacheds/trunk/mitosis/src/test/java/org/apache/directory/mitosis/service/ReplicationServiceITest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/mitosis/src/test/java/org/apache/directory/mitosis/service/ReplicationServiceITest.java?view=diff&rev=565206&r1=565205&r2=565206
==============================================================================
--- directory/apacheds/trunk/mitosis/src/test/java/org/apache/directory/mitosis/service/ReplicationServiceITest.java (original)
+++ directory/apacheds/trunk/mitosis/src/test/java/org/apache/directory/mitosis/service/ReplicationServiceITest.java Sun Aug 12 16:43:07 2007
@@ -19,45 +19,21 @@
*/
package org.apache.directory.mitosis.service;
-import java.io.File;
-import java.net.InetSocketAddress;
-import java.util.HashMap;
-import java.util.Hashtable;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Random;
-
-import javax.naming.Context;
-import javax.naming.InitialContext;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.ModificationItem;
-import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import junit.framework.Assert;
-import junit.framework.TestCase;
-import org.apache.commons.io.FileUtils;
-import org.apache.directory.mitosis.common.Replica;
-import org.apache.directory.mitosis.common.ReplicaId;
-import org.apache.directory.mitosis.configuration.MutableReplicationInterceptorConfiguration;
-import org.apache.directory.mitosis.configuration.ReplicationConfiguration;
-import org.apache.directory.server.core.DirectoryService;
-import org.apache.directory.server.core.configuration.InterceptorConfiguration;
-import org.apache.directory.server.core.configuration.MutableStartupConfiguration;
-import org.apache.directory.server.core.configuration.ShutdownConfiguration;
-import org.apache.directory.server.core.jndi.CoreContextFactory;
import org.apache.directory.shared.ldap.exception.LdapNameNotFoundException;
import org.apache.directory.shared.ldap.message.AttributeImpl;
import org.apache.directory.shared.ldap.message.AttributesImpl;
import org.apache.directory.shared.ldap.message.ModificationItemImpl;
import org.apache.directory.shared.ldap.name.LdapDN;
-import org.apache.mina.util.AvailablePortFinder;
/**
* A test case for {@link ReplicationServiceITest}
@@ -65,21 +41,13 @@
* @author The Apache Directory Project Team (dev@directory.apache.org)
* @version $Rev$, $Date$
*/
-public class ReplicationServiceITest extends TestCase
+public class ReplicationServiceITest extends AbstractReplicationServiceTestCase
{
- private Map<String, LdapContext> contexts = new HashMap<String, LdapContext>();
- private Map<String, ReplicationService> replicationServices = new HashMap<String, ReplicationService>();
-
protected void setUp() throws Exception
{
createReplicas( new String[] { "A", "B", "C" } );
}
- protected void tearDown() throws Exception
- {
- destroyAllReplicas();
- }
-
public void testOneWay() throws Exception
{
String dn1 = "cn=test,ou=system";
@@ -265,143 +233,5 @@
}
}
return foundValue;
- }
-
- @SuppressWarnings("unchecked")
- private void createReplicas( String[] names ) throws Exception
- {
- int lastAvailablePort = 1024;
-
- Replica[] replicas = new Replica[ names.length ];
- for( int i = 0; i < names.length; i++ )
- {
- int replicationPort = AvailablePortFinder
- .getNextAvailable( lastAvailablePort );
- lastAvailablePort = replicationPort + 1;
-
- replicas[ i ] = new Replica( new ReplicaId( names[ i ] ),
- new InetSocketAddress( "127.0.0.1", replicationPort ) );
- }
-
- Random random = new Random();
- String homeDirectory = System.getProperty( "java.io.tmpdir" )
- + File.separator + "mitosis-"
- + Long.toHexString( random.nextLong() );
-
- for( int i = 0; i < replicas.length; i++ )
- {
- Replica replica = replicas[ i ];
- String replicaId = replicas[ i ].getId().getId();
- MutableStartupConfiguration ldapCfg = new MutableStartupConfiguration(
- replicaId );
-
- File workDir = new File( homeDirectory + File.separator
- + ldapCfg.getInstanceId() );
-
- ldapCfg.setShutdownHookEnabled( false );
- ldapCfg.setWorkingDirectory( workDir );
-
- List<InterceptorConfiguration> interceptorCfgs = ldapCfg.getInterceptorConfigurations();
-
- ReplicationConfiguration replicationCfg = new ReplicationConfiguration();
- replicationCfg.setReplicaId( replica.getId() );
- // Disable automatic replication to prevent unexpected behavior
- replicationCfg.setReplicationInterval(0);
- replicationCfg.setServerPort( replica.getAddress().getPort() );
- for( int j = 0; j < replicas.length; j++ )
- {
- if( replicas[ j ] != replica )
- {
- replicationCfg.addPeerReplica( replicas[ j ] );
- }
- }
-
- MutableReplicationInterceptorConfiguration interceptorCfg =
- new MutableReplicationInterceptorConfiguration();
- interceptorCfg.setName( "mitosis" );
- interceptorCfg.setInterceptorClassName( ReplicationService.class.getName() );
- interceptorCfg.setReplicationConfiguration( replicationCfg );
- interceptorCfgs.add( interceptorCfg );
-
- ldapCfg.setInterceptorConfigurations( interceptorCfgs );
-
- if( workDir.exists() )
- {
- FileUtils.deleteDirectory( workDir );
- }
-
- Hashtable env = new Hashtable( ldapCfg.toJndiEnvironment() );
- env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
- env.put( Context.SECURITY_CREDENTIALS, "secret" );
- env.put( Context.SECURITY_AUTHENTICATION, "simple" );
- env.put( Context.PROVIDER_URL, "" );
- env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class
- .getName() );
-
- // Initialize the server instance.
- LdapContext context = new InitialLdapContext( env, null );
- contexts.put( replicaId, context );
- ReplicationService replicationService = (ReplicationService) DirectoryService.getInstance( replicaId ).getConfiguration().getInterceptorChain().get( "mitosis" );
- replicationServices.put( replicaId, replicationService );
- }
-
- // Ensure all replicas have had a chance to connect to each other since the last one started.
- for( Iterator<ReplicationService> i = replicationServices.values().iterator(); i.hasNext(); )
- {
- i.next().interruptConnectors();
- }
- Thread.sleep( 5000 );
- }
-
- private LdapContext getReplicaContext( String name ) throws Exception
- {
- LdapContext context = contexts.get( name );
- if( context == null )
- {
- throw new IllegalArgumentException( "No such replica: " + name );
- }
-
- return ( LdapContext ) context.lookup( "" );
- }
-
- @SuppressWarnings("unchecked")
- private void destroyAllReplicas() throws Exception
- {
- for( Iterator<String> i = contexts.keySet().iterator(); i.hasNext(); )
- {
- String replicaId = i.next();
- File workDir = DirectoryService.getInstance( replicaId )
- .getConfiguration().getStartupConfiguration()
- .getWorkingDirectory();
-
- Hashtable env = new Hashtable();
- env.put( Context.PROVIDER_URL, "ou=system" );
- env.put( Context.INITIAL_CONTEXT_FACTORY, CoreContextFactory.class
- .getName() );
- env.putAll( new ShutdownConfiguration( replicaId )
- .toJndiEnvironment() );
- env.put( Context.SECURITY_PRINCIPAL, "uid=admin,ou=system" );
- env.put( Context.SECURITY_CREDENTIALS, "secret" );
- try
- {
- new InitialContext( env );
- }
- catch( Exception e )
- {
- }
-
- try
- {
- FileUtils.deleteDirectory( workDir );
- }
- catch( Exception e )
- {
- e.printStackTrace();
- }
-
- workDir.getParentFile().delete();
-
- i.remove();
- }
}
}