From commits-return-14870-apmail-directory-commits-archive=directory.apache.org@directory.apache.org Sun Aug 12 23:43:32 2007 Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 6207 invoked from network); 12 Aug 2007 23:43:31 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 12 Aug 2007 23:43:31 -0000 Received: (qmail 52386 invoked by uid 500); 12 Aug 2007 23:43:29 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 52355 invoked by uid 500); 12 Aug 2007 23:43:29 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 52335 invoked by uid 99); 12 Aug 2007 23:43:28 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 12 Aug 2007 16:43:28 -0700 X-ASF-Spam-Status: No, hits=-98.8 required=10.0 tests=ALL_TRUSTED,FS_REPLICA X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 12 Aug 2007 23:43:28 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 1F1B11A981D; Sun, 12 Aug 2007 16:43:08 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r565206 - in /directory/apacheds/trunk/mitosis/src/test/java/org/apache/directory/mitosis/service: AbstractReplicationServiceTestCase.java ReplicationServiceITest.java Date: Sun, 12 Aug 2007 23:43:07 -0000 To: commits@directory.apache.org From: malderson@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070812234308.1F1B11A981D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 Apache Directory Project + * @version $Rev$, $Date$ + */ +public abstract class AbstractReplicationServiceTestCase extends TestCase +{ + protected Map contexts = new HashMap(); + protected Map replicationServices = new HashMap(); + + 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 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 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 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 contexts = new HashMap(); - private Map replicationServices = new HashMap(); - 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 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 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 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(); - } } }