directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r498739 - in /directory/shared/trunk/ldap/src: main/java/org/apache/directory/shared/ldap/message/ test/java/org/apache/directory/shared/ldap/message/
Date Mon, 22 Jan 2007 18:23:26 GMT
Author: elecharny
Date: Mon Jan 22 10:23:24 2007
New Revision: 498739

URL: http://svn.apache.org/viewvc?view=rev&rev=498739
Log:
Added two constructors in Attribute(s)Impl : they take an Attribute argument
nd copy it into the new Attribute(s)Impl created.
Added a test for cthis copy
Removed a useless import 

Modified:
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/AttributeImpl.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/AttributesImpl.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/BindResponseImpl.java
    directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/message/AttributeImplTest.java

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/AttributeImpl.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/AttributeImpl.java?view=diff&rev=498739&r1=498738&r2=498739
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/AttributeImpl.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/AttributeImpl.java
Mon Jan 22 10:23:24 2007
@@ -116,6 +116,45 @@
         size = 1;
     }
 
+    /**
+     * Create a copy of an Attribute, be it an AttributeImpl
+     * instance of a BasicAttribute instance
+     * 
+     * @param attribute the Attribute instace to copy
+     * @throws
+     */
+    public AttributeImpl( Attribute attribute ) throws NamingException
+    {
+        if ( attribute == null )
+        {
+            throw new NamingException( "Null attribute is not allowed" );
+        }
+        else if ( attribute instanceof AttributeImpl )
+        {
+            AttributeImpl clone = (AttributeImpl)attribute.clone();
+            
+            upId  = clone.upId;
+            list = clone.list;
+            size = clone.size;
+            value = clone.value;
+        }
+        else if ( attribute instanceof AttributeImpl )
+        {
+            upId = attribute.getID();
+            
+            NamingEnumeration values = attribute.getAll();
+            
+            while ( values.hasMoreElements() )
+            {
+                add( values.nextElement() );
+            }
+        }
+        else
+        {
+            throw new NamingException( "Attribute must be an instance of BasicAttribute or
AttributeImpl" );
+        }
+    }
+
     // ------------------------------------------------------------------------
     // javax.naming.directory.Attribute Interface Method Implementations
     // ------------------------------------------------------------------------

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/AttributesImpl.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/AttributesImpl.java?view=diff&rev=498739&r1=498738&r2=498739
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/AttributesImpl.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/AttributesImpl.java
Mon Jan 22 10:23:24 2007
@@ -30,6 +30,7 @@
 import javax.naming.directory.Attribute;
 import javax.naming.directory.Attributes;
 import javax.naming.directory.BasicAttribute;
+import javax.naming.directory.BasicAttributes;
 
 import org.apache.directory.shared.ldap.util.AttributeUtils;
 import org.apache.directory.shared.ldap.util.StringTools;
@@ -227,6 +228,60 @@
         put( id, value );
         this.ignoreCase = ignoreCase;
     }
+
+    /**
+     * Copies an Attributes
+     */
+    public AttributesImpl( Attributes attributes ) throws NamingException
+    {
+        if ( attributes == null )
+        {
+            throw new NamingException( "Cannot  create a copy of a null element" );
+        }
+        else if ( attributes instanceof BasicAttributes )
+        {
+            ignoreCase = attributes.isCaseIgnored();
+            
+            NamingEnumeration attrs = attributes.getAll();
+            
+            while ( attrs.hasMoreElements() )
+            {
+                Attribute attribute = new AttributeImpl( (Attribute)attrs.nextElement() );
+                
+                put( attribute );
+            }
+        }
+        else if ( attributes instanceof AttributesImpl )
+        {
+            try
+            {
+                AttributesImpl clone = (AttributesImpl)attributes.clone();
+                
+                keyMap = new HashMap<String, Holder>( clone.keyMap.size() );
+                
+                Iterator keys = clone.keyMap.keySet().iterator();
+        
+                while ( keys.hasNext() )
+                {
+                    String key = (String)keys.next();
+                    Holder holder = clone.keyMap.get( key );
+                    keyMap.put( key, (Holder)holder.clone() );
+                }
+                
+                ignoreCase = clone.ignoreCase;
+            }
+            catch ( CloneNotSupportedException cnse )
+            {
+                throw new NamingException( "Cannot copy a value inro the new atributes element"
);
+            }
+        }
+        else
+        {
+            throw new NamingException( "Cannot create a copy of a object which is not an"
+ 
+                " instance of AttributesImpl or of BasicAttributes" );
+        }
+    }
+
 
     // ------------------------------------------------------------------------
     // Serialization methods

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/BindResponseImpl.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/BindResponseImpl.java?view=diff&rev=498739&r1=498738&r2=498739
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/BindResponseImpl.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/message/BindResponseImpl.java
Mon Jan 22 10:23:24 2007
@@ -22,7 +22,6 @@
 
 import java.util.Arrays;
 
-import org.apache.directory.shared.ldap.util.ArrayUtils;
 import org.apache.directory.shared.ldap.util.StringTools;
 
 

Modified: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/message/AttributeImplTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/message/AttributeImplTest.java?view=diff&rev=498739&r1=498738&r2=498739
==============================================================================
--- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/message/AttributeImplTest.java
(original)
+++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/message/AttributeImplTest.java
Mon Jan 22 10:23:24 2007
@@ -185,7 +185,77 @@
     }
     
     // Test the clone operation
-    public void testCloneAttribute() throws NamingException
+    public void testCloneAttribute1Value() throws NamingException
+    {
+        Attribute attr = new AttributeImpl( "test" );
+
+        byte[] zero = StringTools.getBytesUtf8( "zero" );
+        attr.add( zero );
+
+        Object[] allValues = new Object[] { zero };
+
+        Attribute clone = (Attribute)attr.clone();
+        
+        // Test the atomic elements
+        assertTrue( clone instanceof AttributeImpl );
+        assertEquals( 1, clone.size() );
+        assertEquals( "test", clone.getID() );
+        
+        // Now test the values
+        NamingEnumeration values = clone.getAll();
+        
+        int i = 0;
+        
+        while ( values.hasMoreElements() )
+        {
+            Object value = values.next();
+            
+            if ( value instanceof String )
+            {
+                assertEquals( allValues[i++], value );
+            }
+            else
+            {
+                byte[] v = (byte[])value;
+                
+                // The content should be equal
+                assertTrue( ArrayUtils.isEquals( allValues[i], v ) );
+                
+                // but not the container
+                assertNotSame( allValues[i++], value );
+            }
+        }
+        
+        // Check that if we change the content, the cloned attribute
+        // is still the same.
+        zero[1] = 'o';
+        attr.set( 0, zero );
+        
+        // The initial attribute should be modified
+        Object attrZero = attr.get( 0 );
+        assertNotSame( zero, attrZero );
+        assertTrue( ArrayUtils.isEquals( zero, attrZero ) );
+        
+        // but the cloned attribute should remain the same
+        Object clonedZero = clone.get( 0 );
+        assertNotSame( zero, clonedZero );
+        assertFalse( ArrayUtils.isEquals( zero, clonedZero ) );
+        
+        // Remove a value from the original attribute
+        zero = (byte[])attr.remove( 0 );
+        
+        // Check that it does not have modified the cloned attribute
+        assertEquals( 1, clone.size() );
+        
+        // The content should be equal
+        assertTrue( ArrayUtils.isEquals( StringTools.getBytesUtf8( "zero" ), clone.get( 0
) ) );
+        
+        // but not the container
+        assertNotSame( zero, clone.get( 0 ) );
+    }
+    
+    // Test the clone operation
+    public void testCloneAttributeNValues() throws NamingException
     {
         Attribute attr = new AttributeImpl( "test" );
 
@@ -263,6 +333,155 @@
         assertNotSame( three, clone.get( 3 ) );
     }
     
+    // Test the copy operation
+    public void testCopyAttributeImpl1Value() throws NamingException
+    {
+        Attribute attr = new AttributeImpl( "test" );
+
+        byte[] zero = StringTools.getBytesUtf8( "zero" );
+        attr.add( zero );
+
+        Object[] allValues = new Object[] { zero };
+
+        Attribute copy = new AttributeImpl( attr );
+        
+        // Test the atomic elements
+        assertTrue( copy instanceof AttributeImpl );
+        assertEquals( 1, copy.size() );
+        assertEquals( "test", copy.getID() );
+        
+        // Now test the values
+        NamingEnumeration values = copy.getAll();
+        
+        int i = 0;
+        
+        while ( values.hasMoreElements() )
+        {
+            Object value = values.next();
+            
+            if ( value instanceof String )
+            {
+                assertEquals( allValues[i++], value );
+            }
+            else
+            {
+                byte[] v = (byte[])value;
+                
+                // The content should be equal
+                assertTrue( ArrayUtils.isEquals( allValues[i], v ) );
+                
+                // but not the container
+                assertNotSame( allValues[i++], value );
+            }
+        }
+        
+        // Check that if we change the content, the copied attribute
+        // is still the same.
+        zero[1] = 'o';
+        attr.set( 0, zero );
+        
+        // The initial attribute should be modified
+        Object attrZero = attr.get( 0 );
+        assertNotSame( zero, attrZero );
+        assertTrue( ArrayUtils.isEquals( zero, attrZero ) );
+        
+        // but the copied attribute should remain the same
+        Object copied = copy.get( 0 );
+        assertNotSame( zero, copied );
+        assertFalse( ArrayUtils.isEquals( zero, copied ) );
+        
+        // Remove a value from the original attribute
+        zero = (byte[])attr.remove( 0 );
+        
+        // Check that it does not have modified the copied attribute
+        assertEquals( 1, copy.size() );
+        
+        // The content should be equal
+        assertTrue( ArrayUtils.isEquals( StringTools.getBytesUtf8( "zero" ), copy.get( 0
) ) );
+        
+        // but not the container
+        assertNotSame( zero, copy.get( 0 ) );
+    }
+    
+    // Test the copy operation with more than one value
+    public void testCopyAttributeImplNValues() throws NamingException
+    {
+        Attribute attr = new AttributeImpl( "test" );
+
+        String zero = "zero";
+        attr.add( zero );
+
+        String one = "one";
+        attr.add( one );
+        
+        byte[] two = StringTools.getBytesUtf8( "two" );
+        attr.add( two );
+
+        byte[] three = StringTools.getBytesUtf8( "three" );
+        attr.add( three );
+        
+        Object[] allValues = new Object[] { zero, one, two, three };
+
+        Attribute copy = new AttributeImpl( attr );
+        
+        // Test the atomic elements
+        assertTrue( copy instanceof AttributeImpl );
+        assertEquals( 4, copy.size() );
+        assertEquals( "test", copy.getID() );
+        
+        // Now test the values
+        NamingEnumeration values = copy.getAll();
+        
+        int i = 0;
+        
+        while ( values.hasMoreElements() )
+        {
+            Object value = values.next();
+            
+            if ( value instanceof String )
+            {
+                assertEquals( allValues[i++], value );
+            }
+            else
+            {
+                byte[] v = (byte[])value;
+                
+                // The content should be equal
+                assertTrue( ArrayUtils.isEquals( allValues[i], v ) );
+                
+                // but not the container
+                assertNotSame( allValues[i++], value );
+            }
+        }
+        
+        // Check that if we change the content, the copied attribute
+        // is still the same.
+        two[1] = 'o';
+        attr.set( 2, two );
+        
+        // The initial attribute should be modified
+        Object attrTwo = attr.get( 2 );
+        assertNotSame( two, attrTwo );
+        assertTrue( ArrayUtils.isEquals( two, attrTwo ) );
+        
+        // but the copied attribute should remain the same
+        Object copied = copy.get( 2 );
+        assertNotSame( two, copied );
+        assertFalse( ArrayUtils.isEquals( two, copied ) );
+        
+        // Remove a value from the original attribute
+        three = (byte[])attr.remove( 3 );
+        
+        // Check that it does not have modified the copied attribute
+        assertEquals( 4, copy.size() );
+        
+        // The content should be equal
+        assertTrue( ArrayUtils.isEquals( three, copy.get( 3 ) ) );
+        
+        // but not the container
+        assertNotSame( three, copy.get( 3 ) );
+    }
+
     public void testEquals()
     {
         Attribute attr = new AttributeImpl( "test" );



Mime
View raw message