You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2005/09/12 17:40:13 UTC
svn commit: r280356 - in /directory:
apacheds/trunk/core/src/main/java/org/apache/ldap/server/exception/
apacheds/trunk/main/src/test/org/apache/ldap/server/
shared/ldap/trunk/common/src/java/org/apache/ldap/common/exception/
Author: akarasulu
Date: Mon Sep 12 08:40:02 2005
New Revision: 280356
URL: http://svn.apache.org/viewcvs?rev=280356&view=rev
Log:
changes ...
o worked on and fixed issue posted by Stefan Zoerner: DIREVE-241 here
http://issues.apache.org/jira/browse/DIREVE-241
o fixed another bug with the result code assosciated with the LDAP version of
the AttributeInUseException within ldap-common
o added code to the ExceptionService to check whether or not an attribute being
added in a modify operation already exists - if it does an exception is
raised
Added:
directory/apacheds/trunk/main/src/test/org/apache/ldap/server/ModifyAddTest.java (with props)
Modified:
directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/exception/ExceptionService.java
directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/exception/LdapAttributeInUseException.java
Modified: directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/exception/ExceptionService.java
URL: http://svn.apache.org/viewcvs/directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/exception/ExceptionService.java?rev=280356&r1=280355&r2=280356&view=diff
==============================================================================
--- directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/exception/ExceptionService.java (original)
+++ directory/apacheds/trunk/core/src/main/java/org/apache/ldap/server/exception/ExceptionService.java Mon Sep 12 08:40:02 2005
@@ -22,15 +22,9 @@
import javax.naming.Name;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
-import javax.naming.directory.Attribute;
-import javax.naming.directory.Attributes;
-import javax.naming.directory.ModificationItem;
-import javax.naming.directory.SearchControls;
-
-import org.apache.ldap.common.exception.LdapContextNotEmptyException;
-import org.apache.ldap.common.exception.LdapNameAlreadyBoundException;
-import org.apache.ldap.common.exception.LdapNameNotFoundException;
-import org.apache.ldap.common.exception.LdapNamingException;
+import javax.naming.directory.*;
+
+import org.apache.ldap.common.exception.*;
import org.apache.ldap.common.filter.ExprNode;
import org.apache.ldap.common.message.ResultCodeEnum;
import org.apache.ldap.common.name.LdapName;
@@ -39,6 +33,7 @@
import org.apache.ldap.server.interceptor.NextInterceptor;
import org.apache.ldap.server.jndi.ContextFactoryConfiguration;
import org.apache.ldap.server.partition.ContextPartition;
+import org.apache.ldap.server.partition.ContextPartitionNexus;
/**
@@ -52,6 +47,8 @@
*/
public class ExceptionService extends BaseInterceptor
{
+ private ContextPartitionNexus nexus;
+
/**
* Creates an interceptor that is also the exception handling service.
*/
@@ -62,6 +59,7 @@
public void init( ContextFactoryConfiguration factoryCfg, InterceptorConfiguration cfg )
{
+ nexus = factoryCfg.getPartitionNexus();
}
@@ -183,6 +181,29 @@
String msg = "Attempt to modify non-existant entry: ";
assertHasEntry( nextInterceptor, msg, name );
+ Attributes entry = nexus.lookup( name );
+ NamingEnumeration attrIds = attrs.getIDs();
+ while ( attrIds.hasMore() )
+ {
+ String attrId = ( String ) attrIds.next();
+ Attribute modAttr = attrs.get( attrId );
+ Attribute entryAttr = entry.get( attrId );
+
+ if ( modOp == DirContext.ADD_ATTRIBUTE )
+ {
+ if ( entryAttr != null )
+ {
+ for ( int ii = 0; ii < modAttr.size(); ii++ )
+ {
+ if ( entryAttr.contains( modAttr.get( ii ) ) )
+ {
+ throw new LdapAttributeInUseException( "Trying to add existing value '"
+ + modAttr.get( ii ) + "' to attribute " + attrId );
+ }
+ }
+ }
+ }
+ }
nextInterceptor.modify( name, modOp, attrs );
}
@@ -196,6 +217,27 @@
String msg = "Attempt to modify non-existant entry: ";
assertHasEntry( nextInterceptor, msg, name );
+ Attributes entry = nexus.lookup( name );
+ for ( int ii = 0; ii < items.length; ii++ )
+ {
+ if ( items[ii].getModificationOp() == DirContext.ADD_ATTRIBUTE )
+ {
+ Attribute modAttr = items[ii].getAttribute();
+ Attribute entryAttr = entry.get( modAttr.getID() );
+
+ if ( entryAttr != null )
+ {
+ for ( int jj = 0; jj < modAttr.size(); jj++ )
+ {
+ if ( entryAttr.contains( modAttr.get( jj ) ) )
+ {
+ throw new LdapAttributeInUseException( "Trying to add existing value '"
+ + modAttr.get( ii ) + "' to attribute " + modAttr.getID() );
+ }
+ }
+ }
+ }
+ }
nextInterceptor.modify( name, items );
}
Added: directory/apacheds/trunk/main/src/test/org/apache/ldap/server/ModifyAddTest.java
URL: http://svn.apache.org/viewcvs/directory/apacheds/trunk/main/src/test/org/apache/ldap/server/ModifyAddTest.java?rev=280356&view=auto
==============================================================================
--- directory/apacheds/trunk/main/src/test/org/apache/ldap/server/ModifyAddTest.java (added)
+++ directory/apacheds/trunk/main/src/test/org/apache/ldap/server/ModifyAddTest.java Mon Sep 12 08:40:02 2005
@@ -0,0 +1,203 @@
+/*
+ * Copyright (c) 2004 Solarsis Group LLC.
+ *
+ * Licensed under the Open Software License, Version 2.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://opensource.org/licenses/osl-2.1.php
+ *
+ * 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.ldap.server;
+
+import java.util.Hashtable;
+
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.AttributeInUseException;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.BasicAttribute;
+import javax.naming.directory.BasicAttributes;
+import javax.naming.directory.DirContext;
+import javax.naming.ldap.InitialLdapContext;
+import javax.naming.ldap.LdapContext;
+
+/**
+ * Testcase with different modify operations on a person entry. Each includes a
+ * single add op only. Created to demonstrate DIREVE-241 ("Adding an already
+ * existing attribute value with a modify operation does not cause an error.").
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class ModifyAddTest extends AbstractServerTest
+{
+ private LdapContext ctx = null;
+
+ public static final String RDN = "cn=Tori Amos";
+
+ public static final String PERSON_DESCRIPTION = "an American singer-songwriter";
+
+ /**
+ * Creation of required attributes of a person entry.
+ */
+ protected Attributes getPersonAttributes(String sn, String cn)
+ {
+ Attributes attributes = new BasicAttributes();
+ Attribute attribute = new BasicAttribute("objectClass");
+ attribute.add("top");
+ attribute.add("person");
+ attributes.put(attribute);
+ attributes.put("cn", cn);
+ attributes.put("sn", sn);
+
+ return attributes;
+ }
+
+ /**
+ * Create context and a person entry.
+ */
+ public void setUp() throws Exception
+ {
+ super.setUp();
+
+ Hashtable env = new Hashtable();
+ env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
+ env.put("java.naming.provider.url", "ldap://localhost:" + port + "/ou=system");
+ env.put("java.naming.security.principal", "uid=admin,ou=system");
+ env.put("java.naming.security.credentials", "secret");
+ env.put("java.naming.security.authentication", "simple");
+
+ ctx = new InitialLdapContext(env, null);
+ assertNotNull(ctx);
+
+ // Create a person with description
+ Attributes attributes = this.getPersonAttributes("Amos", "Tori Amos");
+ attributes.put("description", "an American singer-songwriter");
+ ctx.createSubcontext(RDN, attributes);
+
+ }
+
+ /**
+ * Remove person entry and close context.
+ */
+ public void tearDown() throws Exception
+ {
+ ctx.unbind(RDN);
+ ctx.close();
+
+ ctx.close();
+ ctx = null;
+
+ super.tearDown();
+ }
+
+ /**
+ * Add a new attribute to a person entry.
+ *
+ * @throws NamingException
+ */
+ public void testAddNewAttributeValue() throws NamingException
+ {
+
+ // Add telephoneNumber attribute
+ String newValue = "1234567890";
+ Attributes attrs = new BasicAttributes("telephoneNumber", newValue);
+ ctx.modifyAttributes(RDN, DirContext.ADD_ATTRIBUTE, attrs);
+
+ // Verify, that attribute value is added
+ attrs = ctx.getAttributes(RDN);
+ Attribute attr = attrs.get("telephoneNumber");
+ assertNotNull(attr);
+ assertTrue(attr.contains(newValue));
+ assertEquals(1, attr.size());
+ }
+
+ /**
+ * Add a new attribute with two values.
+ *
+ * @throws NamingException
+ */
+ public void testAddNewAttributeValues() throws NamingException
+ {
+
+ // Add telephoneNumber attribute
+ String[] newValues = { "1234567890", "999999999" };
+ Attribute attr = new BasicAttribute("telephoneNumber");
+ attr.add(newValues[0]);
+ attr.add(newValues[1]);
+ Attributes attrs = new BasicAttributes();
+ attrs.put(attr);
+ ctx.modifyAttributes(RDN, DirContext.ADD_ATTRIBUTE, attrs);
+
+ // Verify, that attribute values are present
+ attrs = ctx.getAttributes(RDN);
+ attr = attrs.get("telephoneNumber");
+ assertNotNull(attr);
+ assertTrue(attr.contains(newValues[0]));
+ assertTrue(attr.contains(newValues[1]));
+ assertEquals(newValues.length, attr.size());
+ }
+
+ /**
+ * Add an additional value.
+ *
+ * @throws NamingException
+ */
+ public void testAddAdditionalAttributeValue() throws NamingException
+ {
+
+ // A new description attribute value
+ String newValue = "A new description for this person";
+ assertFalse(newValue.equals(PERSON_DESCRIPTION));
+ Attributes attrs = new BasicAttributes("description", newValue);
+
+ ctx.modifyAttributes(RDN, DirContext.ADD_ATTRIBUTE, attrs);
+
+ // Verify, that attribute value is added
+ attrs = ctx.getAttributes(RDN);
+ Attribute attr = attrs.get("description");
+ assertNotNull(attr);
+ assertTrue(attr.contains(newValue));
+ assertTrue(attr.contains(PERSON_DESCRIPTION));
+ assertEquals(2, attr.size());
+ }
+
+ /**
+ * Try to add an already existing attribute value.
+ *
+ * Expected behaviour: Modify operation fails with an
+ * AttributeInUseException. Original LDAP Error code: 20 (Indicates that the
+ * attribute value specified in a modify or add operation already exists as
+ * a value for that attribute).
+ *
+ * @throws NamingException
+ */
+ public void testAddExistingAttributeValue() throws NamingException
+ {
+
+ // Change description attribute
+ Attributes attrs = new BasicAttributes("description", PERSON_DESCRIPTION);
+ try
+ {
+ ctx.modifyAttributes(RDN, DirContext.ADD_ATTRIBUTE, attrs);
+ fail("Adding an already existing atribute value should fail.");
+ }
+ catch (AttributeInUseException e)
+ {
+ // expected behaviour
+ }
+
+ // Verify, that attribute is still there, and is the only one
+ attrs = ctx.getAttributes(RDN);
+ Attribute attr = attrs.get("description");
+ assertNotNull(attr);
+ assertTrue(attr.contains(PERSON_DESCRIPTION));
+ assertEquals(1, attr.size());
+ }
+}
Propchange: directory/apacheds/trunk/main/src/test/org/apache/ldap/server/ModifyAddTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/exception/LdapAttributeInUseException.java
URL: http://svn.apache.org/viewcvs/directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/exception/LdapAttributeInUseException.java?rev=280356&r1=280355&r2=280356&view=diff
==============================================================================
--- directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/exception/LdapAttributeInUseException.java (original)
+++ directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/exception/LdapAttributeInUseException.java Mon Sep 12 08:40:02 2005
@@ -59,6 +59,6 @@
*/
public ResultCodeEnum getResultCode()
{
- return ResultCodeEnum.ENTRYALREADYEXISTS;
+ return ResultCodeEnum.ATTRIBUTEORVALUEEXISTS;
}
}