You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by lu...@apache.org on 2014/05/10 23:00:44 UTC
svn commit: r1593735 - in /directory/apacheds/trunk:
ldap-client-test/src/test/java/org/apache/directory/ldap/
ldap-client-test/src/test/java/org/apache/directory/ldap/client/
ldap-client-test/src/test/java/org/apache/directory/ldap/client/template/
ld...
Author: lucastheisen
Date: Sat May 10 21:00:43 2014
New Revision: 1593735
URL: http://svn.apache.org/r1593735
Log:
DIRAPI-163: Unit tests and @TestRule's for testing LdapConnectionTemplate.
Added:
directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/ldap/
directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/ldap/client/
directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/ldap/client/template/
directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/ldap/client/template/LdapConnectionTemplateTest.java (with props)
directory/apacheds/trunk/ldap-client-test/src/test/resources/ldif/
directory/apacheds/trunk/ldap-client-test/src/test/resources/ldif/muppets.ldif (with props)
directory/apacheds/trunk/server-annotations/src/main/java/org/apache/directory/server/annotations/CreateLdapConnectionPool.java (with props)
directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateLdapConnectionPoolRule.java (with props)
directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateLdapConnectionPoolRule.java (with props)
Modified:
directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateLdapServerRule.java
directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateDsRule.java
directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateLdapServerRule.java
Added: directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/ldap/client/template/LdapConnectionTemplateTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/ldap/client/template/LdapConnectionTemplateTest.java?rev=1593735&view=auto
==============================================================================
--- directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/ldap/client/template/LdapConnectionTemplateTest.java (added)
+++ directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/ldap/client/template/LdapConnectionTemplateTest.java Sat May 10 21:00:43 2014
@@ -0,0 +1,300 @@
+/*
+ * 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.ldap.client.template;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import org.apache.directory.api.ldap.model.entry.Entry;
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.message.AddResponse;
+import org.apache.directory.api.ldap.model.message.DeleteResponse;
+import org.apache.directory.api.ldap.model.message.ModifyRequest;
+import org.apache.directory.api.ldap.model.message.ModifyResponse;
+import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
+import org.apache.directory.api.ldap.model.name.Dn;
+import org.apache.directory.ldap.client.template.EntryMapper;
+import org.apache.directory.ldap.client.template.LdapConnectionTemplate;
+import org.apache.directory.ldap.client.template.RequestBuilder;
+import org.apache.directory.server.annotations.CreateLdapConnectionPool;
+import org.apache.directory.server.annotations.CreateLdapServer;
+import org.apache.directory.server.annotations.CreateTransport;
+import org.apache.directory.server.core.annotations.ApplyLdifFiles;
+import org.apache.directory.server.core.annotations.ContextEntry;
+import org.apache.directory.server.core.annotations.CreateDS;
+import org.apache.directory.server.core.annotations.CreateIndex;
+import org.apache.directory.server.core.annotations.CreatePartition;
+import org.apache.directory.server.core.integ.CreateLdapConnectionPoolRule;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
+
+
+/**
+ * A test class for LdapConnectionTemplate.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@CreateLdapServer(
+ transports =
+ {
+ @CreateTransport(protocol = "LDAP")
+ }
+ )
+@CreateDS(name = "classDS",
+ partitions =
+ {
+ @CreatePartition(
+ name = "example",
+ suffix = "dc=example,dc=com",
+ contextEntry = @ContextEntry(
+ entryLdif =
+ "dn: dc=example, dc=com\n" +
+ "objectClass: domain\n" +
+ "objectClass: top\n" +
+ "dc: example\n\n"
+ ),
+ indexes =
+ {
+ @CreateIndex(attribute = "objectClass"),
+ @CreateIndex(attribute = "dc"),
+ @CreateIndex(attribute = "ou")
+ }
+ )
+ }
+)
+@ApplyLdifFiles( {
+ "ldif/muppets.ldif"
+ }
+)
+@CreateLdapConnectionPool
+public class LdapConnectionTemplateTest
+{
+ @ClassRule
+ public static CreateLdapConnectionPoolRule classCreateLdapConnectionPoolRule =
+ new CreateLdapConnectionPoolRule();
+
+ @Rule
+ public CreateLdapConnectionPoolRule createLdapConnectionPoolRule =
+ new CreateLdapConnectionPoolRule( classCreateLdapConnectionPoolRule );
+
+ private LdapConnectionTemplate ldapConnectionTemplate;
+
+
+ @Before
+ public void before() {
+ ldapConnectionTemplate = createLdapConnectionPoolRule.getLdapConnectionTemplate();
+ }
+
+
+ @Test
+ public void testAdd()
+ {
+ String uid = "newmuppet";
+ Dn dn = ldapConnectionTemplate.newDn( "uid=" + uid + ",ou=people,dc=example,dc=com" );
+ assertNotNull( dn );
+
+ AddResponse response = ldapConnectionTemplate.add( dn,
+ ldapConnectionTemplate.newAttribute( "objectClass",
+ "top", "person", "organizationalPerson", "inetOrgPerson" ),
+ ldapConnectionTemplate.newAttribute( "cn", "New Muppet" ),
+ ldapConnectionTemplate.newAttribute( "sn", "Muppet" ),
+ ldapConnectionTemplate.newAttribute( "uid", "newmuppet" ) );
+ assertEquals( ResultCodeEnum.SUCCESS, response.getLdapResult().getResultCode() );
+
+ String foundUid = ldapConnectionTemplate.lookup( dn,
+ new EntryMapper<String>()
+ {
+ @Override
+ public String map( Entry entry ) throws LdapException
+ {
+ return entry.get( "uid" ).getString();
+ }
+ });
+ assertNotNull( foundUid );
+ assertEquals( uid, foundUid );
+ }
+
+
+ @Test
+ public void testDelete()
+ {
+ String uid = "kermitthefrog";
+ Dn dn = ldapConnectionTemplate.newDn( "uid=" + uid + ", ou=people, dc=example, dc=com" );
+ DeleteResponse response = ldapConnectionTemplate.delete( dn );
+ assertEquals( ResultCodeEnum.SUCCESS, response.getLdapResult().getResultCode() );
+
+ String foundUid = ldapConnectionTemplate.lookup( dn,
+ new EntryMapper<String>()
+ {
+ @Override
+ public String map( Entry entry ) throws LdapException
+ {
+ return entry.get( "uid" ).getString();
+ }
+ });
+ assertNull( foundUid );
+ }
+
+
+ @Test
+ public void testLookup()
+ {
+ String uid = "kermitthefrog";
+ Dn dn = ldapConnectionTemplate.newDn( "uid=" + uid + ", ou=people, dc=example, dc=com" );
+ String foundUid = ldapConnectionTemplate.lookup( dn,
+ new EntryMapper<String>()
+ {
+ @Override
+ public String map( Entry entry ) throws LdapException
+ {
+ return entry.get( "uid" ).getString();
+ }
+ });
+ assertNotNull( foundUid );
+ assertEquals( uid, foundUid );
+ }
+
+
+ @Test
+ public void testModify()
+ {
+ String uid = "misspiggy";
+ Dn dn = ldapConnectionTemplate.newDn( "uid=" + uid + ",ou=people,dc=example,dc=com" );
+ assertNotNull( dn );
+
+ ModifyResponse response = ldapConnectionTemplate.modify( dn,
+ new RequestBuilder<ModifyRequest>()
+ {
+ @Override
+ public void buildRequest( ModifyRequest request ) throws LdapException
+ {
+ request.replace( "sn", "The Frog" );
+ request.replace( "cn", "Miss The Frog" );
+ }
+ } );
+ assertEquals( ResultCodeEnum.SUCCESS, response.getLdapResult().getResultCode() );
+
+ Muppet found = ldapConnectionTemplate.lookup( dn, Muppet.getEntryMapper() );
+ assertNotNull( found );
+ assertEquals( uid, found.getUid() );
+ assertEquals( "The Frog", found.getSn() );
+ assertEquals( "Miss The Frog", found.getCn() );
+ }
+
+
+ public static class Muppet
+ {
+ private static final EntryMapper<Muppet> entryMapper =
+ new EntryMapper<LdapConnectionTemplateTest.Muppet>()
+ {
+ @Override
+ public Muppet map( Entry entry ) throws LdapException
+ {
+ return new Muppet()
+ .setCn( entry.get( "cn" ).getString() )
+ .setGivenName( entry.get( "givenName" ).getString() )
+ .setMail( entry.get( "mail" ).getString() )
+ .setSn( entry.get( "sn" ).getString() )
+ .setUid( entry.get( "uid" ).getString() );
+ }
+ };
+
+ private String cn;
+ private String givenName;
+ private String mail;
+ private String sn;
+ private String uid;
+
+
+ public String getCn()
+ {
+ return cn;
+ }
+
+
+ public static EntryMapper<Muppet> getEntryMapper() {
+ return entryMapper;
+ }
+
+
+ public String getGivenName()
+ {
+ return givenName;
+ }
+
+
+ public String getMail()
+ {
+ return mail;
+ }
+
+
+ public String getSn()
+ {
+ return sn;
+ }
+
+
+ public String getUid()
+ {
+ return uid;
+ }
+
+
+ public Muppet setCn( String cn )
+ {
+ this.cn = cn;
+ return this;
+ }
+
+
+ public Muppet setGivenName( String givenName )
+ {
+ this.givenName = givenName;
+ return this;
+ }
+
+
+ public Muppet setMail( String mail )
+ {
+ this.mail = mail;
+ return this;
+ }
+
+
+ public Muppet setSn( String sn )
+ {
+ this.sn = sn;
+ return this;
+ }
+
+
+ public Muppet setUid( String uid )
+ {
+ this.uid = uid;
+ return this;
+ }
+ }
+}
Propchange: directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/ldap/client/template/LdapConnectionTemplateTest.java
------------------------------------------------------------------------------
svn:executable = *
Added: directory/apacheds/trunk/ldap-client-test/src/test/resources/ldif/muppets.ldif
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/ldap-client-test/src/test/resources/ldif/muppets.ldif?rev=1593735&view=auto
==============================================================================
--- directory/apacheds/trunk/ldap-client-test/src/test/resources/ldif/muppets.ldif (added)
+++ directory/apacheds/trunk/ldap-client-test/src/test/resources/ldif/muppets.ldif Sat May 10 21:00:43 2014
@@ -0,0 +1,103 @@
+#
+# 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.
+#
+# EXAMPLE.COM is reserved for testing according to this RFC:
+#
+# http://www.rfc-editor.org/rfc/rfc2606.txt
+#
+
+version: 1
+
+## people
+dn: ou=people, dc=example, dc=com
+objectclass: organizationalUnit
+objectclass: top
+ou: people
+
+dn: uid=kermitthefrog, ou=people, dc=example, dc=com
+objectClass: organizationalPerson
+objectClass: person
+objectClass: inetOrgPerson
+objectClass: top
+cn: Kermit The Frog
+sn: The Frog
+givenName: Kermit
+mail: kermitthefrog@muppets.com
+uid: kermitthefrog
+userPassword: set4now
+
+dn: uid=misspiggy, ou=people, dc=example, dc=com
+objectClass: organizationalPerson
+objectClass: person
+objectClass: inetOrgPerson
+objectClass: top
+cn: Miss Piggy
+sn: Piggy
+givenName: Miss
+mail: misspiggy@muppets.com
+uid: misspiggy
+userPassword: set4now
+
+dn: uid=fozziebear, ou=people, dc=example, dc=com
+objectClass: organizationalPerson
+objectClass: person
+objectClass: inetOrgPerson
+objectClass: top
+cn: Fozzie Bear
+sn: Bear
+givenName: Fozzie
+mail: fozziebear@muppets.com
+uid: fozziebear
+userPassword: set4now
+
+dn: uid=gonzothegreat, ou=people, dc=example, dc=com
+objectClass: organizationalPerson
+objectClass: person
+objectClass: inetOrgPerson
+objectClass: top
+cn: Gonzo The Great
+sn: The Great
+givenName: Gonzo
+mail: gonzothegreat@muppets.com
+uid: gonzothegreat
+userPassword: set4now
+
+dn: uid=animal, ou=people, dc=example, dc=com
+objectClass: organizationalPerson
+objectClass: person
+objectClass: inetOrgPerson
+objectClass: top
+cn: Animal
+sn: Just Animal
+givenName: Animal
+mail: animal@muppets.com
+uid: animal
+userPassword: set4now
+
+dn: uid=walter, ou=people, dc=example, dc=com
+objectClass: organizationalPerson
+objectClass: person
+objectClass: inetOrgPerson
+objectClass: top
+cn: Walter
+sn: No Name
+givenName: Walter
+mail: walter@muppets.com
+uid: walter
+userPassword: set4now
+
Propchange: directory/apacheds/trunk/ldap-client-test/src/test/resources/ldif/muppets.ldif
------------------------------------------------------------------------------
svn:executable = *
Added: directory/apacheds/trunk/server-annotations/src/main/java/org/apache/directory/server/annotations/CreateLdapConnectionPool.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/server-annotations/src/main/java/org/apache/directory/server/annotations/CreateLdapConnectionPool.java?rev=1593735&view=auto
==============================================================================
--- directory/apacheds/trunk/server-annotations/src/main/java/org/apache/directory/server/annotations/CreateLdapConnectionPool.java (added)
+++ directory/apacheds/trunk/server-annotations/src/main/java/org/apache/directory/server/annotations/CreateLdapConnectionPool.java Sat May 10 21:00:43 2014
@@ -0,0 +1,107 @@
+/*
+ * 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.server.annotations;
+
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+
+/**
+ * A annotation used to define a LdapConnection configuration.
+ * Many elements can be configured :
+ *
+ * <ul>
+ * <li> The connection timeout</li>
+ * <li> A list of attributes to be interpreted as binary (in addition to the defaults)</li>
+ * </ul>
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@Documented
+@Inherited
+@Retention(RetentionPolicy.RUNTIME)
+@Target(
+ { ElementType.METHOD, ElementType.TYPE })
+public @interface CreateLdapConnectionPool
+{
+ /** The connection timeout in millis, default 30000 */
+ long timeout() default 30000L;
+
+
+ /** Attributes names to be added to the list of default binary attributes */
+ String[] additionalBinaryAttributes() default {};
+
+
+ /** Connections borrowed in LIFO order, default true */
+ boolean lifo() default true;
+
+
+ /** The maximum number of active connections, default 8 */
+ int maxActive() default 8;
+
+
+ /** The maximum number of idle connections, default 8 */
+ int maxIdle() default 8;
+
+
+ /** The maximum amount of time to wait for a connection to be returned in millis, default -1 */
+ long maxWait() default -1L;
+
+
+ /** The minimum idle time before evicting a connection in millis, default 1000*60*30 */
+ long minEvictableIdleTimeMillis() default 1000L * 60L * 30L;
+
+
+ /** The minumum number of idle instances before evictor spawns new object, default 0 */
+ int minIdle() default 0;
+
+
+ /** The number of objects to test per eviction run, default 3 */
+ int numTestsPerEvictionRun() default 3;
+
+
+ /** Same as minEvictableIdleTimeMillis with extra condition that minIdle objects remain in pool, default -1 */
+ long softMinEvictableIdleTimeMillis() default -1L;
+
+
+ /** If true, connection will be tested on borrow, default false */
+ boolean testOnBorrow() default false;
+
+
+ /** If true, connection will be tested on return, default false */
+ boolean testOnReturn() default false;
+
+
+ /** If true, connection will be tested on while idle, default false */
+ boolean testWhileIdle() default false;
+
+
+ /** The time, in millis, between eviction runs, default -1 (forever) */
+ long timeBetweenEvictionRunsMillis() default -1L;
+
+
+ /** The default action when connections are exhausted, default 1 (block) */
+ byte whenExhaustedAction() default 1;
+}
\ No newline at end of file
Propchange: directory/apacheds/trunk/server-annotations/src/main/java/org/apache/directory/server/annotations/CreateLdapConnectionPool.java
------------------------------------------------------------------------------
svn:executable = *
Added: directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateLdapConnectionPoolRule.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateLdapConnectionPoolRule.java?rev=1593735&view=auto
==============================================================================
--- directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateLdapConnectionPoolRule.java (added)
+++ directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateLdapConnectionPoolRule.java Sat May 10 21:00:43 2014
@@ -0,0 +1,204 @@
+/*
+ * 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.server.core.integ;
+
+
+import org.apache.commons.pool.impl.GenericObjectPool.Config;
+import org.apache.directory.api.ldap.codec.api.DefaultConfigurableBinaryAttributeDetector;
+import org.apache.directory.ldap.client.api.DefaultLdapConnectionFactory;
+import org.apache.directory.ldap.client.api.LdapConnectionConfig;
+import org.apache.directory.ldap.client.api.LdapConnectionPool;
+import org.apache.directory.ldap.client.api.PoolableLdapConnectionFactory;
+import org.apache.directory.ldap.client.template.LdapConnectionTemplate;
+import org.apache.directory.server.annotations.CreateLdapConnectionPool;
+import org.apache.directory.server.ldap.LdapServer;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * A {@link TestRule} for creating connection pools.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class CreateLdapConnectionPoolRule extends CreateLdapServerRule
+{
+ private static Logger LOG = LoggerFactory.getLogger( CreateLdapConnectionPoolRule.class );
+ private CreateLdapConnectionPoolRule classCreateLdapConnectionPoolRule;
+ private CreateLdapConnectionPool createLdapConnectionPool;
+ private LdapConnectionPool ldapConnectionPool;
+ private LdapConnectionTemplate ldapConnectionTemplate;
+
+
+ public CreateLdapConnectionPoolRule()
+ {
+ this( null );
+ }
+
+
+ public CreateLdapConnectionPoolRule(
+ CreateLdapConnectionPoolRule classCreateLdapConnectionPoolRule )
+ {
+ super( classCreateLdapConnectionPoolRule );
+ this.classCreateLdapConnectionPoolRule = classCreateLdapConnectionPoolRule;
+ }
+
+
+ @Override
+ public Statement apply( final Statement base, final Description description )
+ {
+ return super.apply( buildStatement( base, description ), description );
+ }
+
+
+ private Statement buildStatement( final Statement base, final Description description )
+ {
+ createLdapConnectionPool = description.getAnnotation( CreateLdapConnectionPool.class );
+ if ( createLdapConnectionPool == null )
+ {
+ return new Statement()
+ {
+ @Override
+ public void evaluate() throws Throwable
+ {
+ LdapServer ldapServer = getLdapServer();
+ if ( classCreateLdapConnectionPoolRule != null
+ && classCreateLdapConnectionPoolRule.getLdapServer() != ldapServer )
+ {
+ LOG.trace( "Creating connection pool to new ldap server" );
+
+ LdapConnectionPool oldLdapConnectionPool = ldapConnectionPool;
+ LdapConnectionTemplate oldLdapConnectionTemplate = ldapConnectionTemplate;
+
+ ldapConnectionPool = classCreateLdapConnectionPoolRule
+ .createLdapConnectionPool( ldapServer );
+ ldapConnectionTemplate = new LdapConnectionTemplate( ldapConnectionPool );
+
+ try
+ {
+ base.evaluate();
+ }
+ finally
+ {
+ LOG.trace( "Reverting to old connection pool" );
+ ldapConnectionPool = oldLdapConnectionPool;
+ ldapConnectionTemplate = oldLdapConnectionTemplate;
+ }
+ }
+ else
+ {
+ LOG.trace( "no @CreateLdapConnectionPool on: {}", description );
+ base.evaluate();
+ }
+ }
+ };
+ }
+ else
+ {
+ return new Statement()
+ {
+ @Override
+ public void evaluate() throws Throwable
+ {
+ LOG.trace( "Creating ldap connection pool" );
+ ldapConnectionPool = createLdapConnectionPool( getLdapServer() );
+ ldapConnectionTemplate = new LdapConnectionTemplate( ldapConnectionPool );
+
+ try
+ {
+ base.evaluate();
+ }
+ finally
+ {
+ LOG.trace( "Closing ldap connection pool" );
+ ldapConnectionPool.close();
+ ldapConnectionTemplate = null;
+ }
+ }
+ };
+ }
+ }
+
+
+ private LdapConnectionPool createLdapConnectionPool( LdapServer ldapServer )
+ {
+ LdapConnectionConfig config = new LdapConnectionConfig();
+ config.setLdapHost( "localhost" );
+ config.setLdapPort( ldapServer.getPort() );
+ config.setName( "uid=admin,ou=system" );
+ config.setCredentials( "secret" );
+ if ( createLdapConnectionPool.additionalBinaryAttributes() != null
+ && createLdapConnectionPool.additionalBinaryAttributes().length > 0 )
+ {
+ DefaultConfigurableBinaryAttributeDetector binaryAttributeDetector =
+ new DefaultConfigurableBinaryAttributeDetector();
+ binaryAttributeDetector.addBinaryAttribute(
+ createLdapConnectionPool.additionalBinaryAttributes() );
+ config.setBinaryAttributeDetector( binaryAttributeDetector );
+ }
+
+ DefaultLdapConnectionFactory factory = new DefaultLdapConnectionFactory( config );
+ factory.setTimeOut( createLdapConnectionPool.timeout() );
+
+ Config poolConfig = new Config();
+ poolConfig.lifo = createLdapConnectionPool.lifo();
+ poolConfig.maxActive = createLdapConnectionPool.maxActive();
+ poolConfig.maxIdle = createLdapConnectionPool.maxIdle();
+ poolConfig.maxWait = createLdapConnectionPool.maxWait();
+ poolConfig.minEvictableIdleTimeMillis = createLdapConnectionPool
+ .minEvictableIdleTimeMillis();
+ poolConfig.minIdle = createLdapConnectionPool.minIdle();
+ poolConfig.numTestsPerEvictionRun = createLdapConnectionPool
+ .numTestsPerEvictionRun();
+ poolConfig.softMinEvictableIdleTimeMillis = createLdapConnectionPool
+ .softMinEvictableIdleTimeMillis();
+ poolConfig.testOnBorrow = createLdapConnectionPool.testOnBorrow();
+ poolConfig.testOnReturn = createLdapConnectionPool.testOnReturn();
+ poolConfig.testWhileIdle = createLdapConnectionPool.testWhileIdle();
+ poolConfig.timeBetweenEvictionRunsMillis = createLdapConnectionPool
+ .timeBetweenEvictionRunsMillis();
+ poolConfig.whenExhaustedAction = createLdapConnectionPool
+ .whenExhaustedAction();
+
+ return new LdapConnectionPool(
+ new PoolableLdapConnectionFactory( factory ), poolConfig );
+ }
+
+
+ public LdapConnectionPool getLdapConnectionPool()
+ {
+ return ldapConnectionPool == null
+ ? ( classCreateLdapConnectionPoolRule == null
+ ? null
+ : classCreateLdapConnectionPoolRule.getLdapConnectionPool() )
+ : ldapConnectionPool;
+ }
+
+ public LdapConnectionTemplate getLdapConnectionTemplate()
+ {
+ return ldapConnectionTemplate == null
+ ? ( classCreateLdapConnectionPoolRule == null
+ ? null
+ : classCreateLdapConnectionPoolRule.getLdapConnectionTemplate() )
+ : ldapConnectionTemplate;
+ }
+}
Propchange: directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateLdapConnectionPoolRule.java
------------------------------------------------------------------------------
svn:executable = *
Modified: directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateLdapServerRule.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateLdapServerRule.java?rev=1593735&r1=1593734&r2=1593735&view=diff
==============================================================================
--- directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateLdapServerRule.java (original)
+++ directory/apacheds/trunk/test-framework/src/main/java/org/apache/directory/server/core/integ/CreateLdapServerRule.java Sat May 10 21:00:43 2014
@@ -36,50 +36,48 @@ import org.slf4j.LoggerFactory;
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
*/
-public class CreateLdapServerRule implements TestRule
+public class CreateLdapServerRule extends CreateDsRule
{
private static Logger LOG = LoggerFactory.getLogger( CreateLdapServerRule.class );
- private CreateDsRule createDsRule;
+ private CreateLdapServerRule classCreateLdapServerRule;
private LdapServer ldapServer;
- private CreateLdapServerRule outerCreateLdapServerRule;
- public CreateLdapServerRule( CreateDsRule createDsRule )
+ public CreateLdapServerRule()
{
- this( createDsRule, null );
+ this( null );
}
- public CreateLdapServerRule( CreateDsRule createDsRule, CreateLdapServerRule createLdapServerRule )
+ public CreateLdapServerRule( CreateLdapServerRule classCreateLdapServerRule )
{
- this.createDsRule = createDsRule;
- this.outerCreateLdapServerRule = createLdapServerRule;
+ super( classCreateLdapServerRule );
+ this.classCreateLdapServerRule = classCreateLdapServerRule;
}
- public LdapServer getLdapServer()
+ @Override
+ public Statement apply( final Statement base, final Description description )
{
- return ldapServer == null
- ? ( outerCreateLdapServerRule == null ? null : outerCreateLdapServerRule.getLdapServer() )
- : ldapServer;
+ return super.apply( buildStatement( base, description ), description );
}
- @Override
- public Statement apply( final Statement base, final Description description )
+ private Statement buildStatement( final Statement base, final Description description )
{
final CreateLdapServer createLdapServer = description.getAnnotation( CreateLdapServer.class );
if ( createLdapServer == null )
{
- return new Statement()
+ return new Statement()
{
@Override
public void evaluate() throws Throwable
{
LdapServer ldapServer = getLdapServer();
- DirectoryService directoryService = createDsRule.getDirectoryService();
- if ( ldapServer != null && directoryService != ldapServer.getDirectoryService() ) {
+ DirectoryService directoryService = getDirectoryService();
+ if ( ldapServer != null && directoryService != ldapServer.getDirectoryService() )
+ {
LOG.trace( "Changing to new directory service" );
DirectoryService oldDirectoryService = ldapServer.getDirectoryService();
ldapServer.setDirectoryService( directoryService );
@@ -93,9 +91,10 @@ public class CreateLdapServerRule implem
LOG.trace( "Reverting to old directory service" );
ldapServer.setDirectoryService( oldDirectoryService );
}
-
+
}
- else {
+ else
+ {
LOG.trace( "no @CreateLdapServer on: {}", description );
base.evaluate();
}
@@ -110,8 +109,8 @@ public class CreateLdapServerRule implem
public void evaluate() throws Throwable
{
LOG.trace( "Creating ldap server" );
- ldapServer = ServerAnnotationProcessor.createLdapServer( description,
- createDsRule.getDirectoryService() );
+ ldapServer = ServerAnnotationProcessor.createLdapServer( description,
+ getDirectoryService() );
try
{
@@ -126,4 +125,12 @@ public class CreateLdapServerRule implem
};
}
}
+
+
+ public LdapServer getLdapServer()
+ {
+ return ldapServer == null
+ ? ( classCreateLdapServerRule == null ? null : classCreateLdapServerRule.getLdapServer() )
+ : ldapServer;
+ }
}
Modified: directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateDsRule.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateDsRule.java?rev=1593735&r1=1593734&r2=1593735&view=diff
==============================================================================
--- directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateDsRule.java (original)
+++ directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateDsRule.java Sat May 10 21:00:43 2014
@@ -41,6 +41,11 @@ import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
+/**
+ * Tests the CreateDsRule.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
@CreateDS(name = "classDS",
enableChangeLog = true,
partitions =
Added: directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateLdapConnectionPoolRule.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateLdapConnectionPoolRule.java?rev=1593735&view=auto
==============================================================================
--- directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateLdapConnectionPoolRule.java (added)
+++ directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateLdapConnectionPoolRule.java Sat May 10 21:00:43 2014
@@ -0,0 +1,152 @@
+/*
+ * 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.server.core.integ;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+import org.apache.directory.api.ldap.model.entry.Entry;
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.name.Dn;
+import org.apache.directory.ldap.client.api.LdapConnection;
+import org.apache.directory.ldap.client.template.ConnectionCallback;
+import org.apache.directory.ldap.client.template.LdapConnectionTemplate;
+import org.apache.directory.server.annotations.CreateLdapConnectionPool;
+import org.apache.directory.server.annotations.CreateLdapServer;
+import org.apache.directory.server.annotations.CreateTransport;
+import org.apache.directory.server.core.annotations.ApplyLdifs;
+import org.apache.directory.server.core.annotations.ContextEntry;
+import org.apache.directory.server.core.annotations.CreateDS;
+import org.apache.directory.server.core.annotations.CreateIndex;
+import org.apache.directory.server.core.annotations.CreatePartition;
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Tests the CreateLdapConnectionPoolRule.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@CreateLdapServer(
+ transports = {
+ @CreateTransport(protocol = "LDAP")
+ })
+@CreateDS(name = "classDS",
+ enableChangeLog = true,
+ partitions = {
+ @CreatePartition(
+ name = "example",
+ suffix = "dc=example,dc=com",
+ contextEntry = @ContextEntry(
+ entryLdif =
+ "dn: dc=example,dc=com\n" +
+ "objectClass: domain\n" +
+ "objectClass: top\n" +
+ "dc: example\n\n"
+ ),
+ indexes = {
+ @CreateIndex(attribute = "objectClass"),
+ @CreateIndex(attribute = "dc"),
+ @CreateIndex(attribute = "ou")
+ })
+ })
+@ApplyLdifs({
+ "dn: cn=class,ou=system",
+ "objectClass: person",
+ "cn: class",
+ "sn: sn_class"
+ })
+@CreateLdapConnectionPool
+public class TestCreateLdapConnectionPoolRule
+{
+ private static Logger LOG = LoggerFactory.getLogger( TestCreateLdapConnectionPoolRule.class );
+ @ClassRule
+ public static CreateLdapConnectionPoolRule classCreateLdapConnectionPoolRule =
+ new CreateLdapConnectionPoolRule();
+
+ @Rule
+ public CreateLdapConnectionPoolRule createLdapConnectionPoolRule =
+ new CreateLdapConnectionPoolRule( classCreateLdapConnectionPoolRule );
+
+
+ @Test
+ public void testLdapConnectionTemplate()
+ {
+ LOG.trace( "checking ldap connection template" );
+ LdapConnectionTemplate ldapConnectionTemplate =
+ createLdapConnectionPoolRule.getLdapConnectionTemplate();
+ assertNotNull( ldapConnectionTemplate );
+
+ ldapConnectionTemplate.execute(
+ new ConnectionCallback<Object>()
+ {
+ @Override
+ public Object doWithConnection( LdapConnection connection ) throws LdapException
+ {
+ assertNotNull( connection );
+ return null;
+ }
+ });
+ }
+
+
+ @Test
+ public void testClassLdapConnectionPool()
+ {
+ assertEquals( createLdapConnectionPoolRule.getLdapConnectionPool(),
+ classCreateLdapConnectionPoolRule.getLdapConnectionPool() );
+
+ LdapConnection ldapConnection = null;
+ try
+ {
+ ldapConnection = createLdapConnectionPoolRule.getLdapConnectionPool()
+ .getConnection();
+
+ Dn dn = new Dn( "cn=class,ou=system" );
+ Entry entry = ldapConnection.lookup( dn );
+ assertNotNull( entry );
+ assertEquals( "class", entry.get( "cn" ).get().getValue() );
+ }
+ catch ( LdapException e )
+ {
+ fail( e.getMessage() );
+ }
+ finally
+ {
+ if ( ldapConnection != null )
+ {
+ try
+ {
+ createLdapConnectionPoolRule.getLdapConnectionPool()
+ .releaseConnection( ldapConnection );
+ }
+ catch ( LdapException e )
+ {
+ // Who cares!
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
Propchange: directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateLdapConnectionPoolRule.java
------------------------------------------------------------------------------
svn:executable = *
Modified: directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateLdapServerRule.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateLdapServerRule.java?rev=1593735&r1=1593734&r2=1593735&view=diff
==============================================================================
--- directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateLdapServerRule.java (original)
+++ directory/apacheds/trunk/test-framework/src/test/java/org/apache/directory/server/core/integ/TestCreateLdapServerRule.java Sat May 10 21:00:43 2014
@@ -45,12 +45,15 @@ import org.apache.directory.server.ldap.
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.RuleChain;
-import org.junit.rules.TestRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+/**
+ * Tests the CreateLdapServerRule.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
@CreateDS(name = "classDS",
enableChangeLog = true,
partitions =
@@ -88,19 +91,12 @@ import org.slf4j.LoggerFactory;
public class TestCreateLdapServerRule
{
private static Logger LOG = LoggerFactory.getLogger( TestCreateLdapServerRule.class );
- public static CreateDsRule classCreateDsRule = new CreateDsRule();
- public static CreateLdapServerRule classCreateLdapServerRule = new CreateLdapServerRule(
- classCreateDsRule );
@ClassRule
- public static TestRule classRuleChain = RuleChain.outerRule( classCreateDsRule )
- .around( classCreateLdapServerRule );
+ public static CreateLdapServerRule classCreateLdapServerRule = new CreateLdapServerRule();
- public CreateDsRule createDsRule = new CreateDsRule( classCreateDsRule );
- public CreateLdapServerRule createLdapServerRule = new CreateLdapServerRule(
- createDsRule, classCreateLdapServerRule );
@Rule
- public TestRule ruleChain = RuleChain.outerRule( createDsRule )
- .around( createLdapServerRule );
+ public CreateLdapServerRule createLdapServerRule = new CreateLdapServerRule(
+ classCreateLdapServerRule );
@Test
@@ -213,11 +209,11 @@ public class TestCreateLdapServerRule
@Test
public void testNetworkConnection()
{
- assertEquals( classCreateDsRule.getDirectoryService(), createDsRule.getDirectoryService() );
+ assertEquals( classCreateLdapServerRule.getDirectoryService(), createLdapServerRule.getDirectoryService() );
assertEquals( classCreateLdapServerRule.getLdapServer(), createLdapServerRule.getLdapServer() );
LdapServer ldapServer = createLdapServerRule.getLdapServer();
DirectoryService directoryService = ldapServer.getDirectoryService();
- assertEquals( classCreateDsRule.getDirectoryService(), directoryService );
+ assertEquals( classCreateLdapServerRule.getDirectoryService(), directoryService );
LdapConnection ldapConnection = null;
try