directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r617816 [1/2] - in /directory: apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/jndi/ apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/operational/ shared/branches/bigbang/ldap/src/m...
Date Sat, 02 Feb 2008 15:43:13 GMT
Author: elecharny
Date: Sat Feb  2 07:43:12 2008
New Revision: 617816

URL: http://svn.apache.org/viewvc?rev=617816&view=rev
Log:
o Added serializer for Dn, Rdn and atavs
o Renamed some fields to normXXX for normalized values
o Switched Ldap Shared name tests to Junit 4

Added:
    directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/AtavSerializer.java
    directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/DnSerializer.java
    directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/RdnSerializer.java
Modified:
    directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/jndi/ServerContext.java
    directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/jndi/ServerDirContext.java
    directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/operational/OperationalAttributeInterceptor.java
    directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/entry/Entry.java
    directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/AttributeTypeAndValue.java
    directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java
    directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java
    directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/name/AttributeTypeAndValueTest.java
    directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java
    directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/name/RdnTest.java

Modified: directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/jndi/ServerContext.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/jndi/ServerContext.java?rev=617816&r1=617815&r2=617816&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/jndi/ServerContext.java (original)
+++ directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/jndi/ServerContext.java Sat Feb  2 07:43:12 2008
@@ -696,7 +696,7 @@
             for ( Iterator<AttributeTypeAndValue> ii = rdn.iterator(); ii.hasNext(); /**/ )
             {
                 AttributeTypeAndValue atav = ( AttributeTypeAndValue ) ii.next();
-                attributes.put( atav.getUpType(), atav.getValue() );
+                attributes.put( atav.getUpType(), atav.getNormValue() );
             }
         }
     }

Modified: directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/jndi/ServerDirContext.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/jndi/ServerDirContext.java?rev=617816&r1=617815&r2=617816&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/jndi/ServerDirContext.java (original)
+++ directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/jndi/ServerDirContext.java Sat Feb  2 07:43:12 2008
@@ -436,11 +436,11 @@
                 doRdnPut = doRdnPut || attributes.get( atav.getNormType() ).size() == 0;
                 
                 // TODO Fix DIRSERVER-832
-                doRdnPut = doRdnPut || !attributes.get( atav.getNormType() ).contains( atav.getValue() );
+                doRdnPut = doRdnPut || !attributes.get( atav.getNormType() ).contains( atav.getNormValue() );
         
                 if ( doRdnPut )
                 {
-                    attributes.put( atav.getNormType(), atav.getValue() );
+                    attributes.put( atav.getNormType(), atav.getNormValue() );
                 }
             }
         }

Modified: directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/operational/OperationalAttributeInterceptor.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/operational/OperationalAttributeInterceptor.java?rev=617816&r1=617815&r2=617816&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/operational/OperationalAttributeInterceptor.java (original)
+++ directory/apacheds/branches/bigbang/core/src/main/java/org/apache/directory/server/core/operational/OperationalAttributeInterceptor.java Sat Feb  2 07:43:12 2008
@@ -457,7 +457,7 @@
             else if ( rdn.size() == 1 )
             {
                 String name = atRegistry.lookup( rdn.getNormType() ).getName();
-                String value = (String)rdn.getAtav().getValue(); 
+                String value = (String)rdn.getAtav().getNormValue(); 
                 newDn.add( new Rdn( name, name, value, value ) );
                 continue;
             }
@@ -469,7 +469,7 @@
             {
                 AttributeTypeAndValue atav = atavs.next();
                 String type = atRegistry.lookup( rdn.getNormType() ).getName();
-                buf.append( type ).append( '=' ).append( atav.getValue() );
+                buf.append( type ).append( '=' ).append( atav.getNormValue() );
                 
                 if ( atavs.hasNext() )
                 {

Modified: directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/entry/Entry.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/entry/Entry.java?rev=617816&r1=617815&r2=617816&view=diff
==============================================================================
--- directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/entry/Entry.java (original)
+++ directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/entry/Entry.java Sat Feb  2 07:43:12 2008
@@ -74,6 +74,39 @@
 
 
     /**
+     * Add some String values to the current Entry.
+     *
+     * @param upId The user provided ID of the attribute we want to add 
+     * some values to
+     * @param values The list of String values to add
+     * @throws NamingException
+     */
+    void add( String upId, String... values ) throws NamingException;
+
+    
+    /**
+     * Add some binary values to the current Entry.
+     *
+     * @param upId The user provided ID of the attribute we want to add 
+     * some values to
+     * @param values The list of binary values to add
+     * @throws NamingException
+     */
+    void add( String upId, byte[]... values ) throws NamingException;
+
+    
+    /**
+     * Add some Values to the current Entry.
+     *
+     * @param upId The user provided ID of the attribute we want to add 
+     * some values to
+     * @param values The list of Values to add
+     * @throws NamingException
+     */
+    void add( String upId, Value<?>... values ) throws NamingException;
+    
+    
+    /**
      * Places non-null attributes in the attribute collection. If there is
      * already an attribute with the same OID as any of the new attributes, 
      * the old ones are removed from the collection and are returned by this 

Added: directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/AtavSerializer.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/AtavSerializer.java?rev=617816&view=auto
==============================================================================
--- directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/AtavSerializer.java (added)
+++ directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/AtavSerializer.java Sat Feb  2 07:43:12 2008
@@ -0,0 +1,181 @@
+/*
+ *  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.shared.ldap.name;
+
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+
+import javax.naming.InvalidNameException;
+
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A helper class which serialize and deserialize a AttributeTypeAndValue
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class AtavSerializer
+{
+    /** The LoggerFactory used by this class */
+    protected static final Logger LOG = LoggerFactory.getLogger( AtavSerializer.class );
+
+    /**
+     * Serialize an AttributeTypeAndValue object.
+     * 
+     * An AttributeTypeAndValue is composed of  a type and a value.
+     * The data are stored following the structure :
+     * 
+     * <li>upName</li> The User provided ATAV
+     * <li>start</li> The position of this ATAV in the DN
+     * <li>length</li> The ATAV length
+     * <li>upType</li> The user Provided Type
+     * <li>normType</li> The normalized AttributeType
+     * <li>isHR<li> Tells if the value is a String or not
+     * <p>
+     * if the value is a String :
+     * <li>upValue</li> The User Provided value.
+     * <li>value</li> The normalized value.
+     * <p>
+     * if the value is binary :
+     * <li>upValueLength</li>
+     * <li>upValue</li> The User Provided value.
+     * <li>valueLength</li>
+     * <li>value</li> The normalized value.
+     */
+    public static void serialize( AttributeTypeAndValue atav, ObjectOutput out ) throws IOException
+    {
+        if ( StringTools.isEmpty( atav.getUpName() ) || 
+             StringTools.isEmpty( atav.getUpType() ) ||
+             StringTools.isEmpty( atav.getNormType() ) ||
+             ( atav.getStart() < 0 ) ||
+             ( atav.getLength() < 2 ) ||             // At least a type and '='
+             ( atav.getUpValue() == null ) ||
+             ( atav.getNormValue() == null ) )
+        {
+            String message = "Cannot serialize an wrong ATAV, ";
+            
+            if ( StringTools.isEmpty( atav.getUpName() ) )
+            {
+                message += "the upName should not be null or empty";
+            }
+            else if ( StringTools.isEmpty( atav.getUpType() ) )
+            {
+                message += "the upType should not be null or empty";
+            }
+            else if ( StringTools.isEmpty( atav.getNormType() ) )
+            {
+                message += "the normType should not be null or empty";
+            }
+            else if ( atav.getStart() < 0 )
+            {
+                message += "the start should not be < 0";
+            }
+            else if ( atav.getLength() < 2 )
+            {
+                message += "the length should not be < 2";
+            }
+            else if ( atav.getUpValue() == null )
+            {
+                message += "the upValue should not be null";
+            }
+            else if ( atav.getNormValue() == null )
+            {
+                message += "the value should not be null";
+            }
+                
+            LOG.error( message );
+            throw new IOException( message );
+        }
+        
+        out.writeUTF( atav.getUpName() );
+        out.writeInt( atav.getStart() );
+        out.writeInt( atav.getLength() );
+        out.writeUTF( atav.getUpType() );
+        out.writeUTF( atav.getNormType() );
+        
+        boolean isHR = ( atav.getNormValue() instanceof String );
+        
+        out.writeBoolean( isHR );
+        
+        if ( isHR )
+        {
+            out.writeUTF( (String)atav.getUpValue() );
+            out.writeUTF( (String)atav.getNormValue() );
+        }
+        else
+        {
+            out.writeInt( ((byte[])atav.getUpValue()).length );
+            out.write( (byte[])atav.getUpValue() );
+            out.writeInt( ((byte[])atav.getNormValue()).length );
+            out.write( (byte[])atav.getNormValue() );
+        }
+        
+        out.flush();
+    }
+    
+    
+    /**
+     * @see Externalizable#readExternal(ObjectInput)
+     * 
+     * We read back the data to create a new ATAV. The structure 
+     * read is exposed in the {@link AttributeTypeAndValue#writeExternal(ObjectOutput)} 
+     * method<p>
+     */
+    public static AttributeTypeAndValue deserialize( ObjectInput in ) throws IOException, ClassNotFoundException
+    {
+        String upName = in.readUTF();
+        int start = in.readInt();
+        int length = in.readInt();
+        String upType = in.readUTF();
+        String normType = in.readUTF();
+        
+        boolean isHR = in.readBoolean();
+        
+        if ( isHR )
+        {
+            String upValue = in.readUTF();
+            String normValue = in.readUTF();
+            AttributeTypeAndValue atav = 
+                new AttributeTypeAndValue( upType, normType, upValue, normValue, start, length, upName );
+            
+            return atav;
+        }
+        else
+        {
+            int upValueLength = in.readInt();
+            byte[] upValue = new byte[upValueLength];
+            in.readFully( (byte[])upValue );
+
+            int valueLength = in.readInt();
+            byte[] normValue = new byte[valueLength];
+            in.readFully( (byte[])normValue );
+
+            AttributeTypeAndValue atav = 
+                new AttributeTypeAndValue( upType, normType, upValue, normValue, start, length, upName );
+            
+            return atav;
+        }
+    }
+}

Modified: directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/AttributeTypeAndValue.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/AttributeTypeAndValue.java?rev=617816&r1=617815&r2=617816&view=diff
==============================================================================
--- directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/AttributeTypeAndValue.java (original)
+++ directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/AttributeTypeAndValue.java Sat Feb  2 07:43:12 2008
@@ -69,7 +69,7 @@
     private String upType;
 
     /** The name value. It can be a String or a byte array */
-    private Object value;
+    private Object normValue;
 
     /** The name user provided value. It can be a String or a byte array */
     private Object upValue;
@@ -97,7 +97,7 @@
     {
         normType = null;
         upType = null;
-        value = null;
+        normValue = null;
         upValue = null;
         upName = "";
         start = -1;
@@ -156,11 +156,11 @@
         {
             if ( normValue instanceof String )
             {
-                this.value = StringTools.isEmpty( ( String ) normValue ) ? "" : normValue;
+                this.normValue = StringTools.isEmpty( ( String ) normValue ) ? "" : normValue;
             }
             else
             {
-                this.value = normValue;
+                this.normValue = normValue;
             }
 
             if ( upValue instanceof String )
@@ -179,11 +179,11 @@
     
             if ( normValue instanceof String )
             {
-                this.value = StringTools.isEmpty( ( String ) normValue ) ? "" : normValue;
+                this.normValue = StringTools.isEmpty( ( String ) normValue ) ? "" : normValue;
             }
             else
             {
-                this.value = normValue;
+                this.normValue = normValue;
             }
         }
 
@@ -194,6 +194,38 @@
 
 
     /**
+     * Construct an AttributeTypeAndValue. The type and value are normalized :
+     * <li> the type is trimmed and lowercased </li>
+     * <li> the value is trimmed </li>
+     * <p>
+     * Note that the upValue should <b>not</b> be null or empty, or resolved
+     * to an empty string after having trimmed it. 
+     *
+     * @param upType The Usrr Provided type
+     * @param normType The normalized type
+     * @param upValue The User Provided value
+     * @param normValue The normalized value
+     */
+    /**No protection*/ AttributeTypeAndValue( 
+                            String upType, 
+                            String normType, 
+                            Object upValue, 
+                            Object normValue,
+                            int start, 
+                            int length, 
+                            String upName )
+    {
+        this.upType = upType;
+        this.normType = normType;
+        this.upValue = upValue;
+        this.normValue = normValue;
+        this.start = start;
+        this.length = length;
+        this.upName = upName;
+    }
+
+    
+    /**
      * Get the normalized type of a AttributeTypeAndValue
      *
      * @return The normalized type
@@ -283,9 +315,9 @@
      *
      * @return The value
      */
-    public Object getValue()
+    public Object getNormValue()
     {
-        return value;
+        return normValue;
     }
 
     /**
@@ -315,15 +347,15 @@
      * @param value
      *            The value of the AttributeTypeAndValue
      */
-    public void setValue( Object upValue, Object value )
+    public void setValue( Object upValue, Object normValue )
     {
-        if ( value instanceof String )
+        if ( normValue instanceof String )
         {
-            this.value = StringTools.isEmpty( ( String ) value ) ? "" : ( String ) value;
+            this.normValue = StringTools.isEmpty( ( String ) normValue ) ? "" : ( String ) normValue;
         }
         else
         {
-            this.value = value;
+            this.normValue = normValue;
         }
 
         this.upValue = upValue;
@@ -378,11 +410,11 @@
 
         if ( StringTools.isEmpty( newValue ) )
         {
-            this.value = "";
+            this.normValue = "";
         }
         else
         {
-            this.value = newValue;
+            this.normValue = newValue;
         }
 
         upName = upName.substring( 0, upName.indexOf( '=' ) + 1 ) + value;
@@ -410,8 +442,9 @@
 
 
     /**
-     * Compares two NameComponents. They are equals if : - types are equals,
-     * case insensitive, - values are equals, case sensitive
+     * Compares two NameComponents. They are equals if : 
+     * - types are equals, case insensitive, 
+     * - values are equals, case sensitive
      *
      * @param object
      * @return 0 if both NC are equals, otherwise a positive value if the
@@ -432,7 +465,7 @@
             }
             else
             {
-                return compareValue( value, nc.value, CASE_SENSITIVE );
+                return compareValue( normValue, nc.normValue, CASE_SENSITIVE );
             }
         }
         else
@@ -443,8 +476,9 @@
 
 
     /**
-     * Compares two NameComponents. They are equals if : - types are equals,
-     * case insensitive, - values are equals, case insensitive
+     * Compares two NameComponents. They are equals if : 
+     * - types are equals, case insensitive, 
+     * - values are equals, case insensitive
      *
      * @param object
      * @return 0 if both NC are equals, otherwise a positive value if the
@@ -465,7 +499,7 @@
             }
             else
             {
-                return compareValue( value, nc.value, CASE_INSENSITIVE );
+                return compareValue( normValue, nc.normValue, CASE_INSENSITIVE );
             }
         }
         else
@@ -572,7 +606,7 @@
      */
     public String normalize()
     {
-        if ( value instanceof String )
+        if ( normValue instanceof String )
         {
         	// The result will be gathered in a stringBuilder
             StringBuilder sb = new StringBuilder();
@@ -580,7 +614,7 @@
             // First, store the type and the '=' char
             sb.append( normType ).append( '=' );
             
-            String normalizedValue =  ( String ) value;
+            String normalizedValue =  ( String ) normValue;
             int valueLength = normalizedValue.length();
             boolean escaped = false;
             
@@ -680,7 +714,7 @@
         else
         {
             return normType + "=#"
-                + StringTools.dumpHexPairs( ( byte[] ) value );
+                + StringTools.dumpHexPairs( ( byte[] ) normValue );
         }
     }
 
@@ -695,7 +729,7 @@
         int result = 37;
 
         result = result*17 + ( normType != null ? normType.hashCode() : 0 );
-        result = result*17 + ( value != null ? value.hashCode() : 0 );
+        result = result*17 + ( normValue != null ? normValue.hashCode() : 0 );
 
         return result;
     }
@@ -734,26 +768,26 @@
         }
             
         // Compare the values
-        if ( value == null )
+        if ( normValue == null )
         {
-            return instance.value == null;
+            return instance.normValue == null;
         }
-        else if ( value instanceof String )
+        else if ( normValue instanceof String )
         {
-            if ( instance.value instanceof String )
+            if ( instance.normValue instanceof String )
             {
-                return value.equals( instance.value );
+                return normValue.equals( instance.normValue );
             }
             else
             {
                 return false;
             }
         }
-        else if ( value instanceof byte[] )
+        else if ( normValue instanceof byte[] )
         {
-            if ( instance.value instanceof byte[] )
+            if ( instance.normValue instanceof byte[] )
             {
-                return Arrays.equals( (byte[])value, (byte[])instance.value );
+                return Arrays.equals( (byte[])normValue, (byte[])instance.normValue );
             }
             else
             {
@@ -798,7 +832,7 @@
              ( start < 0 ) ||
              ( length < 2 ) ||             // At least a type and '='
              ( upValue == null ) ||
-             ( value == null ) )
+             ( normValue == null ) )
         {
             String message = "Cannot serialize an wrong ATAV, ";
             
@@ -826,7 +860,7 @@
             {
                 message += "the upValue should not be null";
             }
-            else if ( value == null )
+            else if ( normValue == null )
             {
                 message += "the value should not be null";
             }
@@ -841,21 +875,21 @@
         out.writeUTF( upType );
         out.writeUTF( normType );
         
-        boolean isHR = ( value instanceof String );
+        boolean isHR = ( normValue instanceof String );
         
         out.writeBoolean( isHR );
         
         if ( isHR )
         {
             out.writeUTF( (String)upValue );
-            out.writeUTF( (String)value );
+            out.writeUTF( (String)normValue );
         }
         else
         {
             out.writeInt( ((byte[])upValue).length );
             out.write( (byte[])upValue );
-            out.writeInt( ((byte[])value).length );
-            out.write( (byte[])value );
+            out.writeInt( ((byte[])normValue).length );
+            out.write( (byte[])normValue );
         }
         
         out.flush();
@@ -882,7 +916,7 @@
         if ( isHR )
         {
             upValue = in.readUTF();
-            value = in.readUTF();
+            normValue = in.readUTF();
         }
         else
         {
@@ -891,8 +925,8 @@
             in.readFully( (byte[])upValue );
 
             int valueLength = in.readInt();
-            value = new byte[valueLength];
-            in.readFully( (byte[])value );
+            normValue = new byte[valueLength];
+            in.readFully( (byte[])normValue );
         }
     }
     
@@ -913,9 +947,9 @@
 
         sb.append( normType ).append( "=" );
 
-        if ( value != null )
+        if ( normValue != null )
         {
-            sb.append( value );
+            sb.append( normValue );
         }
 
         return sb.toString();

Added: directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/DnSerializer.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/DnSerializer.java?rev=617816&view=auto
==============================================================================
--- directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/DnSerializer.java (added)
+++ directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/DnSerializer.java Sat Feb  2 07:43:12 2008
@@ -0,0 +1,146 @@
+/*
+ *  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.shared.ldap.name;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A helper class which serialize and deserialize a DN
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class DnSerializer
+{
+    /** The LoggerFactory used by this class */
+    protected static final Logger LOG = LoggerFactory.getLogger( DnSerializer.class );
+
+    /**
+     * We have to store a DN data efficiently. Here is the structure :
+     * 
+     * <li>upName</li> The User provided DN<p>
+     * <li>normName</li> May be null if the normName is equaivalent to 
+     * the upName<p>
+     * <li>rdns</li> The rdn's List.<p>
+     * 
+     * for each rdn :
+     * <li>call the RDN write method</li>
+     */
+    public static void serialize( LdapDN dn, ObjectOutput out ) throws IOException
+    {
+        if ( dn.getUpName() == null )
+        {
+            String message = "Cannot serialize a NULL DN";
+            LOG.error( message );
+            throw new IOException( message );
+        }
+        
+        // Write the UPName
+        out.writeUTF( dn.getUpName() );
+        
+        // Write the NormName if different
+        if ( dn.isNormalized() )
+        {
+            if ( dn.getUpName().equals( dn.getNormName() ) )
+            {
+                out.writeUTF( "" );
+            }
+            else
+            {
+                out.writeUTF( dn.getNormName() );
+            }
+        }
+        else
+        {
+            String message = "The DN should have been normalized before being serialized";
+            LOG.error( message );
+            throw new IOException( message );
+        }
+        
+        // Should we store the byte[] ???
+        
+        // Write the RDNs. Is it's null, the number will be -1. 
+        if ( dn.getRdns() == null )
+        {
+            out.writeInt( -1 );
+        }
+        else if ( dn.size() == 0 )
+        {
+            out.writeInt( 0 );
+        }
+        else
+        {
+            out.writeInt( dn.size() );
+            
+            // Loop on the RDNs
+            for ( Rdn rdn:dn.getRdns() )
+            {
+                RdnSerializer.serialize( rdn, out );
+            }
+        }
+        
+        out.flush();
+    }
+
+
+    /**
+     * We read back the data to create a new LdapDN. The structure 
+     * read is exposed in the {@link DnSerializer#serialize(LdapDN, ObjectOutput)} 
+     * method<p>
+     */
+    public static LdapDN deserialize( ObjectInput in ) throws IOException, ClassNotFoundException
+    {
+        // Read the UPName
+        String upName = in.readUTF();
+        
+        // Read the NormName
+        String normName = in.readUTF();
+        
+        if ( normName.length() == 0 )
+        {
+            // As the normName is equal to the upName,
+            // we didn't saved the nbnormName on disk.
+            // restore it by copying the upName.
+            normName = upName;
+        }
+        
+        // Should we read the byte[] ???
+        byte[] bytes = StringTools.getBytesUtf8( upName );
+        
+        // Read the RDNs. Is it's null, the number will be -1.
+        int nbRdns = in.readInt();
+        
+        LdapDN dn = new LdapDN( upName, normName, bytes );
+        
+        for ( int i = 0; i < nbRdns; i++ )
+        {
+            Rdn rdn = RdnSerializer.deserialize( in );
+            dn.add( 0, rdn );
+        }
+    
+        return dn;
+    }
+}

Modified: directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java?rev=617816&r1=617815&r2=617816&view=diff
==============================================================================
--- directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java (original)
+++ directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java Sat Feb  2 07:43:12 2008
@@ -214,6 +214,18 @@
 
 
     /**
+     * Create a DN when deserializing it.
+     */
+    /* No protection */ LdapDN( String upName, String normName, byte[] bytes )
+    {
+        normalized = true;
+        this.upName = upName;
+        this.normName = normName;
+        this.bytes = bytes;
+    }
+
+
+    /**
      * Static factory which creates a normalized DN from a String and a Map of OIDs.
      *
      * @param name The DN as a String
@@ -295,7 +307,7 @@
      */
     public String toNormName()
     {
-        if ( ( rdns == null ) || ( rdns.size() == 0 ) )
+        if ( rdns.size() == 0 )
         {
             bytes = null;
             return "";
@@ -351,7 +363,7 @@
      */
     private String toUpName()
     {
-        if ( ( rdns == null ) || ( rdns.size() == 0 ) )
+        if ( rdns.size() == 0 )
         {
             upName = "";
         }
@@ -491,12 +503,9 @@
     {
         int result = 37;
 
-        if ( ( rdns != null ) && ( rdns.size() != 0 ) )
+        for ( Rdn rdn : rdns )
         {
-            for ( Rdn rdn : rdns )
-            {
-                result = result * 17 + rdn.hashCode();
-            }
+            result = result * 17 + rdn.hashCode();
         }
 
         return result;
@@ -526,9 +535,9 @@
 
 
     /**
-     * Get the number of NameComponent conatained in this LdapDN
+     * Get the number of NameComponent contained in this LdapDN
      *
-     * @return The number of NameComponent conatained in this LdapDN
+     * @return The number of NameComponent contained in this LdapDN
      */
     public int size()
     {
@@ -1172,6 +1181,25 @@
         return this;
     }
 
+
+    /**
+     * Adds a single RDN to a specific position.
+     *
+     * @param newRdn the RDN to add
+     * @param pos The position where we want to add the Rdn
+     * @return the updated name (not a new one)
+     */
+    public Name add( int pos, Rdn newRdn )
+    {
+        rdns.add( newRdn );
+        
+        normalizeInternal();
+        toUpName();
+
+        return this;
+    }
+
+
     /**
      * Adds a single normalized RDN to the (leaf) end of this name.
      *
@@ -1423,7 +1451,7 @@
                 {
                     return new AttributeTypeAndValue( atav.getUpType(), oidNormalizer.getAttributeTypeOid(), 
                     		atav.getUpValue(),
-                    		oidNormalizer.getNormalizer().normalize( atav.getValue() ) );
+                    		oidNormalizer.getNormalizer().normalize( atav.getNormValue() ) );
 
                 }
                 else
@@ -1487,7 +1515,7 @@
             {
             	AttributeTypeAndValue val = atavs.next();
                 AttributeTypeAndValue newAtav = atavOidToName( val, oidsMap );
-                rdn.addAttributeTypeAndValue( val.getUpType(), newAtav.getNormType(), val.getUpValue(), newAtav.getValue() );
+                rdn.addAttributeTypeAndValue( val.getUpType(), newAtav.getNormType(), val.getUpValue(), newAtav.getNormValue() );
             }
 
         }
@@ -1708,23 +1736,12 @@
         // Should we store the byte[] ???
         
         // Write the RDNs. Is it's null, the number will be -1. 
-        if ( rdns == null )
-        {
-            out.writeInt( -1 );
-        }
-        else if ( rdns.size() == 0 )
-        {
-            out.writeInt( 0 );
-        }
-        else
+        out.writeInt( rdns.size() );
+
+        // Loop on the RDNs
+        for ( Rdn rdn:rdns )
         {
-            out.writeInt( rdns.size() );
-            
-            // Loop on the RDNs
-            for ( Rdn rdn:rdns )
-            {
-                out.writeObject( rdn );
-            }
+            out.writeObject( rdn );
         }
         
         out.flush();
@@ -1762,28 +1779,12 @@
         
         // Read the RDNs. Is it's null, the number will be -1.
         int nbRdns = in.readInt();
+        rdns = new ArrayList<Rdn>( nbRdns );
         
-        switch ( nbRdns )
+        for ( int i = 0; i < nbRdns; i++ )
         {
-            case -1 :
-                // No RDN at all... 
-                rdns = null;
-                break;
-                
-            case 0 :
-                // No RDN, but we have to initialize the list
-                // Note : this may not be a different case than -1
-                rdns = new ArrayList<Rdn>();
-                break;
-                
-            default :
-                for ( int i = 0; i < nbRdns; i++ )
-                {
-                    Rdn rdn = (Rdn)in.readObject();
-                    rdns.add( rdn );
-                }
-            
-                break;
+            Rdn rdn = (Rdn)in.readObject();
+            rdns.add( rdn );
         }
     }
 }

Modified: directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java?rev=617816&r1=617815&r2=617816&view=diff
==============================================================================
--- directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java (original)
+++ directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/Rdn.java Sat Feb  2 07:43:12 2008
@@ -107,7 +107,7 @@
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-public class Rdn implements Cloneable, Comparable, Serializable
+public class Rdn implements Cloneable, Comparable, Serializable, Iterable<AttributeTypeAndValue>
 {
     /** The LoggerFactory used by this class */
     protected static final Logger LOG = LoggerFactory.getLogger( Rdn.class );
@@ -235,13 +235,13 @@
     * @throws InvalidNameException
     *             If the RDN is invalid
     */
-   public Rdn( String upType, String type, String upValue, String value ) throws InvalidNameException
+   public Rdn( String upType, String normType, String upValue, String normValue ) throws InvalidNameException
    {
        super();
 
-       addAttributeTypeAndValue( upType, type, upValue, value );
+       addAttributeTypeAndValue( upType, normType, upValue, normValue );
 
-       upName = type + '=' + value;
+       upName = upType + '=' + upValue;
        start = 0;
        length = upName.length();
        // create the internal normalized form
@@ -250,6 +250,28 @@
 
 
    /**
+    * A constructor that constructs a RDN from a type and a value. Constructs
+    * an Rdn from the given attribute type and value. The string attribute
+    * values are not interpretted as RFC 2253 formatted RDN strings. That is,
+    * the values are used literally (not parsed) and assumed to be unescaped.
+    *
+    * @param type
+    *            The type of the RDN
+    * @param value
+    *            The value of the RDN
+    * @throws InvalidNameException
+    *             If the RDN is invalid
+    */
+   /* No protection */ Rdn( int start, int length, String upName, String normName )
+   {
+       this.start = 0;
+       this.length = length;
+       this.upName = upName;
+       this.normName = normName;
+   }
+
+   
+   /**
     * Constructs an Rdn from the given rdn. The contents of the rdn are simply
     * copied into the newly created
     *
@@ -312,13 +334,13 @@
            case 1:
                // We have a single AttributeTypeAndValue
                // We will trim and lowercase type and value.
-               if ( atav.getValue() instanceof String )
+               if ( atav.getNormValue() instanceof String )
                {
                    normName = atav.getNormalizedValue();
                }
                else
                {
-                   normName = atav.getNormType() + "=#" + StringTools.dumpHexPairs( (byte[])atav.getValue() );
+                   normName = atav.getNormType() + "=#" + StringTools.dumpHexPairs( (byte[])atav.getNormValue() );
                }
 
                break;
@@ -408,6 +430,61 @@
 
 
    /**
+    * Add a AttributeTypeAndValue to the current RDN
+    *
+    * @param type
+    *            The type of the added RDN.
+    * @param value
+    *            The value of the added RDN
+    * @throws InvalidNameException
+    *             If the RDN is invalid
+    */
+   // WARNING : The protection level is left unspecified intentionnaly.
+   // We need this method to be visible from the DnParser class, but not
+   // from outside this package.
+   @SuppressWarnings({"unchecked"})
+   /* Unspecified protection */void addAttributeTypeAndValue( AttributeTypeAndValue atav )
+   {
+       String normalizedType = atav.getNormType();
+
+       switch ( nbAtavs )
+       {
+           case 0:
+               // This is the first AttributeTypeAndValue. Just stores it.
+               this.atav = atav;
+               nbAtavs = 1;
+               atavType = normalizedType;
+               return;
+
+           case 1:
+               // We already have an atav. We have to put it in the HashMap
+               // before adding a new one.
+               // First, create the HashMap,
+               atavs = new TreeSet<AttributeTypeAndValue>();
+
+               // and store the existing AttributeTypeAndValue into it.
+               atavs.add( this.atav );
+               atavTypes = new MultiHashMap();
+               atavTypes.put( atavType, this.atav );
+
+               this.atav = null;
+
+           // Now, fall down to the commmon case
+           // NO BREAK !!!
+
+           default:
+               // add a new AttributeTypeAndValue
+               atavs.add( atav );
+               atavTypes.put( normalizedType, atav );
+
+               nbAtavs++;
+               break;
+
+       }
+   }
+
+   
+   /**
     * Clear the RDN, removing all the AttributeTypeAndValues.
     */
    public void clear()
@@ -445,7 +522,7 @@
            case 1:
                if ( StringTools.equals( atav.getNormType(), normalizedType ) )
                {
-                   return atav.getValue();
+                   return atav.getNormValue();
                }
                else
                {
@@ -459,7 +536,7 @@
 
                    if ( obj instanceof AttributeTypeAndValue )
                    {
-                       return ( ( AttributeTypeAndValue ) obj ).getValue();
+                       return ( ( AttributeTypeAndValue ) obj ).getNormValue();
                    }
                    else if ( obj instanceof List )
                    {
@@ -479,7 +556,7 @@
                                sb.append( ',' );
                            }
 
-                           sb.append( elem.getValue() );
+                           sb.append( elem.getNormValue() );
                        }
 
                        return sb.toString();
@@ -496,8 +573,29 @@
        }
    }
 
+   
+   /** 
+    * Get the start position
+    *
+    * @return The start position in the DN
+    */
+   public int getStart()
+   {
+       return start;
+   }
+   
 
    /**
+    * Get the Rdn length
+    *
+    * @return The Rdn length
+    */
+   public int getLength()
+   {
+       return length;
+   }
+   
+   /**
     * Get the AttributeTypeAndValue which type is given as an argument. If we
     * have more than one value associated with the type, we will return only
     * the first one.
@@ -761,6 +859,15 @@
 
 
    /**
+    * Returns The normalized name
+    */
+   public String getNormName()
+   {
+       return normName == null ? "" : normName;
+   }
+
+
+   /**
     * Set the User Provided Name
     */
    public void setUpName( String upName )
@@ -853,10 +960,10 @@
                return null;
 
            case 1:
-               return atav.getValue();
+               return atav.getNormValue();
 
            default:
-               return ( ( AttributeTypeAndValue )((TreeSet)atavs).first() ).getValue();
+               return ( ( AttributeTypeAndValue )((TreeSet)atavs).first() ).getNormValue();
        }
    }
 
@@ -935,7 +1042,7 @@
 
            case 1 :
                attribute = new AttributeImpl( atavType, true );
-               attribute.add( atav.getValue() );
+               attribute.add( atav.getNormValue() );
                attributes.put( attribute );
                break;
 
@@ -955,7 +1062,7 @@
                    {
                        AttributeTypeAndValue value = iterValues.next();
 
-                       attribute.add( value.getValue() );
+                       attribute.add( value.getNormValue() );
                    }
 
                    attributes.put( attribute );

Added: directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/RdnSerializer.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/RdnSerializer.java?rev=617816&view=auto
==============================================================================
--- directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/RdnSerializer.java (added)
+++ directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/name/RdnSerializer.java Sat Feb  2 07:43:12 2008
@@ -0,0 +1,141 @@
+/*
+ *  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.shared.ldap.name;
+
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+
+import org.apache.directory.shared.ldap.util.StringTools;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A helper class which serialize and deserialize a RDN
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class RdnSerializer
+{
+    /** The LoggerFactory used by this class */
+    protected static final Logger LOG = LoggerFactory.getLogger( RdnSerializer.class );
+
+    /**
+     * @see Externalizable#readExternal(ObjectInput)<p>
+     * 
+     * A RDN is composed of on to many ATAVs (AttributeType And Value).
+     * We should write all those ATAVs sequencially, following the 
+     * structure :
+     * 
+     * <li>nbAtavs</li> The number of ATAVs to write. Can't be 0.
+     * <li>upName</li> The User provided RDN
+     * <li>normName</li> The normalized RDN. It can be empty if the normalized
+     * name equals the upName.
+     * <li>atavs</li>
+     * <p>
+     * For each ATAV :<p>
+     * <li>start</li> The position of this ATAV in the upName string
+     * <li>length</li> The ATAV user provided length
+     * <li>Call the ATAV write method</li> The ATAV itself
+     *  
+     */
+    public static void serialize( Rdn rdn, ObjectOutput out ) throws IOException
+    {
+        out.writeInt( rdn.getNbAtavs() );
+        out.writeUTF( rdn.getUpName() );
+        out.writeUTF( rdn.getNormName() );
+        out.writeInt( rdn.getStart() );
+        out.writeInt( rdn.getLength() );
+        
+        switch ( rdn.getNbAtavs() )
+        {
+            case 0 :
+                break;
+
+            case 1 :
+                AtavSerializer.serialize( rdn.getAtav(), out );
+                break;
+                
+            default :
+                for ( AttributeTypeAndValue atav:rdn )
+                {
+                    AtavSerializer.serialize( atav, out );
+                }
+            
+                break;
+        }
+        
+        out.flush();
+    }
+    
+    /**
+     * @see Externalizable#readExternal(ObjectInput)
+     * 
+     * We read back the data to create a new RDB. The structure 
+     * read is exposed in the {@link Rdn#writeExternal(ObjectOutput)} 
+     * method<p>
+     */
+    public static Rdn deserialize( ObjectInput in ) throws IOException, ClassNotFoundException
+    {
+        // Read the ATAV number
+        int nbAtavs = in.readInt();
+        
+        // Read the UPName
+        String upName = in.readUTF();
+        
+        // Read the normName
+        String normName = in.readUTF();
+        
+        if ( StringTools.isEmpty( normName ) )
+        {
+            normName = upName;
+        }
+        
+        int start = in.readInt();
+        int length = in.readInt();
+        
+        Rdn rdn = new Rdn( length, start, upName, normName );
+
+        
+        switch ( nbAtavs )
+        {
+            case 0 :
+                return rdn;
+                
+            case 1 :
+                AttributeTypeAndValue atav = AtavSerializer.deserialize( in );
+                
+                rdn.addAttributeTypeAndValue( atav );
+
+                return rdn;
+                
+            default :
+                for ( int i = 0; i < nbAtavs; i++  )
+                {
+                    atav = AtavSerializer.deserialize( in );
+                    rdn.addAttributeTypeAndValue( atav );
+                }
+            
+                return rdn;
+        }
+    }
+}

Modified: directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/name/AttributeTypeAndValueTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/name/AttributeTypeAndValueTest.java?rev=617816&r1=617815&r2=617816&view=diff
==============================================================================
--- directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/name/AttributeTypeAndValueTest.java (original)
+++ directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/name/AttributeTypeAndValueTest.java Sat Feb  2 07:43:12 2008
@@ -32,9 +32,9 @@
 import org.apache.directory.shared.ldap.name.AttributeTypeAndValue;
 import org.apache.directory.shared.ldap.util.StringTools;
 import org.junit.Test;
-
-import junit.framework.Assert;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+import static org.junit.Assert.assertTrue;
 
 
 /**
@@ -42,13 +42,14 @@
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-public class AttributeTypeAndValueTest extends TestCase
+public class AttributeTypeAndValueTest
 {
     // ~ Methods
     // ------------------------------------------------------------------------------------
     /**
      * Test a null AttributeTypeAndValue
      */
+    @Test
     public void testAttributeTypeAndValueNull()
     {
         AttributeTypeAndValue atav = new AttributeTypeAndValue();
@@ -62,6 +63,7 @@
     /**
      * Test a null type for an AttributeTypeAndValue
      */
+    @Test
     public void testAttributeTypeAndValueNullType() throws InvalidNameException
     {
         try
@@ -79,6 +81,7 @@
     /**
      * Test an invalid type for an AttributeTypeAndValue
      */
+    @Test
     public void testAttributeTypeAndValueInvalidType() throws InvalidNameException
     {
         try
@@ -96,6 +99,7 @@
     /**
      * Test a valid type for an AttributeTypeAndValue
      */
+    @Test
     public void testAttributeTypeAndValueValidType() throws InvalidNameException
     {
         AttributeTypeAndValue atav = new AttributeTypeAndValue( "A", "a", null, null );
@@ -119,12 +123,13 @@
     /**
      * test an empty AttributeTypeAndValue
      */
+    @Test
     public void testLdapRDNEmpty()
     {
         try
         {
             new AttributeTypeAndValue( "", "", "", "" );
-            Assert.fail( "Should not occurs ... " );
+            fail( "Should not occurs ... " );
         }
         catch ( InvalidNameException ine )
         {
@@ -136,6 +141,7 @@
     /**
      * test a simple AttributeTypeAndValue : a = b
      */
+    @Test
     public void testLdapRDNSimple() throws InvalidNameException
     {
         AttributeTypeAndValue atav = new AttributeTypeAndValue( "a", "a", "b", "b" );
@@ -149,6 +155,7 @@
     /**
      * Compares two equals atavs
      */
+    @Test
     public void testCompareToEquals() throws InvalidNameException
     {
         AttributeTypeAndValue atav1 = new AttributeTypeAndValue( "a", "a", "b", "b" );
@@ -161,6 +168,7 @@
     /**
      * Compares two equals atavs but with a type in different case
      */
+    @Test
     public void testCompareToEqualsCase() throws InvalidNameException
     {
         AttributeTypeAndValue atav1 = new AttributeTypeAndValue( "a", "a", "b", "b" );
@@ -174,6 +182,7 @@
      * Compare two atavs : the first one is superior because its type is
      * superior
      */
+    @Test
     public void testCompareAtav1TypeSuperior() throws InvalidNameException
     {
         AttributeTypeAndValue atav1 = new AttributeTypeAndValue( "b", "b", "b", "b" );
@@ -187,6 +196,7 @@
      * Compare two atavs : the second one is superior because its type is
      * superior
      */
+    @Test
     public void testCompareAtav2TypeSuperior() throws InvalidNameException
     {
         AttributeTypeAndValue atav1 = new AttributeTypeAndValue( "a", "a", "b", "b" );
@@ -200,6 +210,7 @@
      * Compare two atavs : the first one is superior because its type is
      * superior
      */
+    @Test
     public void testCompareAtav1ValueSuperior() throws InvalidNameException
     {
         AttributeTypeAndValue atav1 = new AttributeTypeAndValue( "a", "a", "b", "b" );
@@ -213,6 +224,7 @@
      * Compare two atavs : the second one is superior because its type is
      * superior
      */
+    @Test
     public void testCompareAtav2ValueSuperior() throws InvalidNameException
     {
         AttributeTypeAndValue atav1 = new AttributeTypeAndValue( "a", "a", "a", "a" );
@@ -222,6 +234,7 @@
     }
 
 
+    @Test
     public void testNormalize() throws InvalidNameException
     {
         AttributeTypeAndValue atav = new AttributeTypeAndValue( " A ", " A ", "a", "a" );
@@ -391,6 +404,165 @@
 
         AttributeTypeAndValue atav2 = new AttributeTypeAndValue();
         atav2.readExternal( in );
+
+        assertEquals( atav, atav2 );
+    }
+
+
+    /**
+     * Test serialization of a simple ATAV
+     */
+    @Test
+    public void testStringAtavStaticSerialization() throws NamingException, IOException, ClassNotFoundException
+    {
+        AttributeTypeAndValue atav = new AttributeTypeAndValue( "cn", "CN", "test", "Test" );
+
+        atav.normalize();
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        AtavSerializer.serialize( atav, out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        AttributeTypeAndValue atav2 = AtavSerializer.deserialize( in );
+
+        assertEquals( atav, atav2 );
+    }
+
+
+    @Test
+    public void testBinaryAtavStaticSerialization() throws NamingException, IOException, ClassNotFoundException
+    {
+        byte[] upValue = StringTools.getBytesUtf8( "  Test  " );
+        byte[] normValue = StringTools.getBytesUtf8( "Test" );
+
+        AttributeTypeAndValue atav = new AttributeTypeAndValue( "cn", "CN", upValue, normValue );
+
+        atav.normalize();
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        AtavSerializer.serialize( atav, out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        AttributeTypeAndValue atav2 = AtavSerializer.deserialize( in );
+
+        assertEquals( atav, atav2 );
+    }
+
+
+    /**
+     * Test static serialization of a simple ATAV
+     */
+    @Test
+    public void testNullAtavStaticSerialization() throws NamingException, IOException, ClassNotFoundException
+    {
+        AttributeTypeAndValue atav = new AttributeTypeAndValue();
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        try
+        {
+            AtavSerializer.serialize( atav, out );
+            fail();
+        }
+        catch ( IOException ioe )
+        {
+            assertTrue( true );
+        }
+    }
+
+
+    @Test
+    public void testNullNormValueStaticSerialization() throws NamingException, IOException, ClassNotFoundException
+    {
+        AttributeTypeAndValue atav = new AttributeTypeAndValue( "CN", "cn", "test", (String)null );
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        try
+        {
+            AtavSerializer.serialize( atav, out );
+            fail();
+        }
+        catch ( IOException ioe )
+        {
+            String message = ioe.getMessage();
+            assertEquals( "Cannot serialize an wrong ATAV, the value should not be null", message );
+        }
+    }
+
+
+    @Test
+    public void testNullUpValueStaticSerialization() throws NamingException, IOException, ClassNotFoundException
+    {
+        AttributeTypeAndValue atav = new AttributeTypeAndValue( "CN", "cn", null, "test" );
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        try
+        {
+            AtavSerializer.serialize( atav, out );
+            fail();
+        }
+        catch ( IOException ioe )
+        {
+            String message = ioe.getMessage();
+            assertEquals( "Cannot serialize an wrong ATAV, the upValue should not be null", message );
+        }
+    }
+
+
+    @Test
+    public void testEmptyNormValueStaticSerialization() throws NamingException, IOException, ClassNotFoundException
+    {
+        AttributeTypeAndValue atav = new AttributeTypeAndValue( "CN", "cn", "test", "" );
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        AtavSerializer.serialize( atav, out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        AttributeTypeAndValue atav2 = AtavSerializer.deserialize( in );
+
+        assertEquals( atav, atav2 );
+    }
+
+
+    @Test
+    public void testEmptyUpValueStaticSerialization() throws NamingException, IOException, ClassNotFoundException
+    {
+        AttributeTypeAndValue atav = new AttributeTypeAndValue( "CN", "cn", "", "test" );
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        AtavSerializer.serialize( atav, out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        AttributeTypeAndValue atav2 = AtavSerializer.deserialize( in );
 
         assertEquals( atav, atav2 );
     }

Modified: directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java?rev=617816&r1=617815&r2=617816&view=diff
==============================================================================
--- directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java (original)
+++ directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java Sat Feb  2 07:43:12 2008
@@ -23,9 +23,6 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
@@ -2839,6 +2836,45 @@
     {
         LdapDN dn = LdapDN.EMPTY_LDAPDN;
         
+        assertEquals( dn, deserializeDN( serializeDN( dn ) ) );
+    }
+
+
+    /**
+     * Test the serialization of a DN
+     *
+     * @throws Exception
+     */
+    @Test public void testNameStaticSerialization() throws Exception
+    {
+        LdapDN dn = new LdapDN( "ou= Some   People   + dc=  And   Some anImAls,dc = eXample,dc= cOm" );
+        dn.normalize( oids );
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        DnSerializer.serialize( dn, out );
+        
+        byte[] data = baos.toByteArray();
+        ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        assertEquals( dn, DnSerializer.deserialize( in ) );
+    }
+
+
+    @Test public void testStaticSerializeEmptyDN() throws Exception
+    {
+        LdapDN dn = LdapDN.EMPTY_LDAPDN;
+        
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        DnSerializer.serialize( dn, out );
+        
+        byte[] data = baos.toByteArray();
+        ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        assertEquals( dn, DnSerializer.deserialize( in ) );
         assertEquals( dn, deserializeDN( serializeDN( dn ) ) );
     }
 }



Mime
View raw message