You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2011/03/15 19:49:20 UTC

svn commit: r1081902 - in /directory/shared/trunk: integ/src/test/java/org/apache/directory/shared/ldap/entry/ ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/ ldap/model/src/test/java/org/apache/directory/shared/ldap/model/entry/

Author: elecharny
Date: Tue Mar 15 18:49:20 2011
New Revision: 1081902

URL: http://svn.apache.org/viewvc?rev=1081902&view=rev
Log:
o Fixed the Entry serialization tests
o Some refactoring in DefaultEntry
o EntrySerializer methods are now using teh Entry.readExternal/writeExternal methods

Modified:
    directory/shared/trunk/integ/src/test/java/org/apache/directory/shared/ldap/entry/SchemaAwareEntrySerializerTest.java
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/DefaultEntry.java
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/EntrySerializer.java
    directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/shared/ldap/model/entry/EntrySerializerTest.java

Modified: directory/shared/trunk/integ/src/test/java/org/apache/directory/shared/ldap/entry/SchemaAwareEntrySerializerTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/integ/src/test/java/org/apache/directory/shared/ldap/entry/SchemaAwareEntrySerializerTest.java?rev=1081902&r1=1081901&r2=1081902&view=diff
==============================================================================
--- directory/shared/trunk/integ/src/test/java/org/apache/directory/shared/ldap/entry/SchemaAwareEntrySerializerTest.java (original)
+++ directory/shared/trunk/integ/src/test/java/org/apache/directory/shared/ldap/entry/SchemaAwareEntrySerializerTest.java Tue Mar 15 18:49:20 2011
@@ -28,8 +28,8 @@ import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 
+import org.apache.directory.shared.ldap.model.entry.DefaultEntry;
 import org.apache.directory.shared.ldap.model.entry.Entry;
-import org.apache.directory.shared.ldap.model.entry.EntrySerializer;
 import org.apache.directory.shared.ldap.model.exception.LdapException;
 import org.apache.directory.shared.ldap.model.ldif.LdifUtils;
 import org.apache.directory.shared.ldap.model.schema.SchemaManager;
@@ -63,7 +63,7 @@ public class SchemaAwareEntrySerializerT
     
     
     @Test
-    public void testEntryFullSerialization() throws IOException, LdapException
+    public void testEntryFullSerialization() throws IOException, LdapException, ClassNotFoundException
     {
         Entry entry1 = LdifUtils.createEntry( 
             schemaManager,
@@ -76,22 +76,23 @@ public class SchemaAwareEntrySerializerT
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ObjectOutputStream out = new ObjectOutputStream( baos );
 
-        EntrySerializer.serialize( entry1, out );
+        entry1.writeExternal( out );
         
         ObjectInputStream in = null;
 
         byte[] data = baos.toByteArray();
         in = new ObjectInputStream( new ByteArrayInputStream( data ) );
 
-        Entry entry2 = EntrySerializer.deserialize( schemaManager, in );
-
+        Entry entry2 = new DefaultEntry( schemaManager );
+        entry2.readExternal( in );
+        
         assertEquals( entry1, entry2 );
         assertTrue( entry2.contains( "2.5.4.0", "top", "domain" ) );
     }
     
     
     @Test
-    public void testEntryNoDnSerialization() throws IOException, LdapException
+    public void testEntryNoDnSerialization() throws IOException, LdapException, ClassNotFoundException
     {
         Entry entry1 = LdifUtils.createEntry( 
             schemaManager,
@@ -104,14 +105,16 @@ public class SchemaAwareEntrySerializerT
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ObjectOutputStream out = new ObjectOutputStream( baos );
 
-        EntrySerializer.serialize( entry1, out );
+        entry1.writeExternal( out );
         
         ObjectInputStream in = null;
 
         byte[] data = baos.toByteArray();
         in = new ObjectInputStream( new ByteArrayInputStream( data ) );
 
-        Entry entry2 = EntrySerializer.deserialize( schemaManager, in );
+        Entry entry2 = new DefaultEntry( schemaManager );
+        entry2.readExternal( in );
+        
 
         assertEquals( entry1, entry2 );
         assertTrue( entry2.contains( "ObjectClass", "top", "domain" ) );
@@ -120,21 +123,23 @@ public class SchemaAwareEntrySerializerT
 
 
     @Test
-    public void testEntryNoAttributesSerialization() throws IOException, LdapException
+    public void testEntryNoAttributesSerialization() throws IOException, LdapException, ClassNotFoundException
     {
         Entry entry1 = LdifUtils.createEntry( schemaManager, "dc=example, dc=com" ); 
         
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ObjectOutputStream out = new ObjectOutputStream( baos );
 
-        EntrySerializer.serialize( entry1, out );
+        entry1.writeExternal( out );
         
         ObjectInputStream in = null;
 
         byte[] data = baos.toByteArray();
         in = new ObjectInputStream( new ByteArrayInputStream( data ) );
 
-        Entry entry2 = EntrySerializer.deserialize( schemaManager, in );
+        Entry entry2 = new DefaultEntry( schemaManager );
+        entry2.readExternal( in );
+        
 
         assertEquals( entry1, entry2 );
         assertEquals( 0, entry2.size() );

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/DefaultEntry.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/DefaultEntry.java?rev=1081902&r1=1081901&r2=1081902&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/DefaultEntry.java (original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/DefaultEntry.java Tue Mar 15 18:49:20 2011
@@ -47,13 +47,13 @@ import org.slf4j.LoggerFactory;
 
 /**
  * A default implementation of a ServerEntry which should suite most
- * use cases.
- *
+ * use cases.<br/>
+ * <br/>
  * This class is final, it should not be extended.
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-public class DefaultEntry implements Entry
+public final class DefaultEntry implements Entry
 {
     /** Used for serialization */
     private static final long serialVersionUID = 2L;
@@ -455,6 +455,57 @@ public class DefaultEntry implements Ent
     // Helper methods
     //-------------------------------------------------------------------------
     /**
+     * Get the trimmed and lower cased entry ID
+     */
+    private String getId( String upId )
+    {
+        String id = Strings.trim( Strings.toLowerCase( upId ) );
+
+        // If empty, throw an error
+        if ( Strings.isEmpty( id ) )
+        {
+            String message = I18n.err( I18n.ERR_04133 );
+            LOG.error( message );
+            throw new IllegalArgumentException( message );
+        }
+
+        return id;
+    }
+
+
+    /**
+     * Get the UpId if it was null.
+     * 
+     * @param upId The ID
+     */
+    private String getUpId( String upId, AttributeType attributeType )
+    {
+        String normUpId = Strings.trim( upId );
+
+        if ( ( attributeType == null ) )
+        {
+            if ( Strings.isEmpty( normUpId ) )
+            {
+                String message = I18n.err( I18n.ERR_04458 );
+                LOG.error( message );
+                throw new IllegalArgumentException( message );
+            }
+        }
+        else if ( Strings.isEmpty( normUpId ) )
+        {
+            upId = attributeType.getName();
+
+            if ( Strings.isEmpty( upId ) )
+            {
+                upId = attributeType.getOid();
+            }
+        }
+
+        return upId;
+    }
+
+
+    /**
      * This method is used to initialize the OBJECT_CLASS_AT attributeType.
      *
      * We want to do it only once, so it's a synchronized method. Note that
@@ -484,54 +535,53 @@ public class DefaultEntry implements Ent
     }
 
 
-    private String getId( String upId )
+    /**
+     * normalizes the given Dn if it was not already normalized
+     *
+     * @param dn the Dn to be normalized
+     */
+    private void normalizeDN( Dn dn )
     {
-        String id = Strings.trim(Strings.toLowerCase(upId));
-
-        // If empty, throw an error
-        if ( ( id == null ) || ( id.length() == 0 ) )
+        if ( !dn.isNormalized() )
         {
-            String message = I18n.err( I18n.ERR_04133 );
-            LOG.error( message );
-            throw new IllegalArgumentException( message );
+            try
+            {
+                // The dn must be normalized
+                dn.normalize( schemaManager );
+            }
+            catch ( LdapException ne )
+            {
+                LOG.warn( "The Dn '{}' cannot be normalized", dn );
+            }
         }
-
-        return id;
     }
 
 
     /**
-     * Get the UpId if it was null.
-     * 
-     * @param upId The ID
+     * A helper method to recompute the hash code
      */
-    private static String getUpId( String upId, AttributeType attributeType )
+    private void rehash()
     {
-        String normUpId = Strings.trim(upId);
+        h = 37;
+        h = h * 17 + dn.hashCode();
 
-        if ( ( attributeType == null ) )
+        /*
+        // We have to sort the Attributes if we want to compare two entries
+        SortedMap<String, EntryAttribute> sortedMap = new TreeMap<String, EntryAttribute>();
+
+        for ( String id:attributes.keySet() )
         {
-            if ( Strings.isEmpty(normUpId) )
-            {
-                String message = I18n.err( I18n.ERR_04458 );
-                LOG.error( message );
-                throw new IllegalArgumentException( message );
-            }
+            sortedMap.put( id, attributes.get( id ) );
         }
-        else if ( Strings.isEmpty(normUpId) )
-        {
-            upId = attributeType.getName();
 
-            if ( Strings.isEmpty(upId) )
-            {
-                upId = attributeType.getOid();
-            }
+        for ( String id:sortedMap.keySet() )
+        {
+            h = h*17 + sortedMap.get( id ).hashCode();
         }
-
-        return upId;
+        */
     }
-
-
+    
+    
     /**
      * Add a new EntryAttribute, with its upId. If the upId is null,
      * default to the AttributeType name.
@@ -2578,31 +2628,6 @@ public class DefaultEntry implements Ent
 
 
     /**
-     * A helper method to recompute the hash code
-     */
-    private void rehash()
-    {
-        h = 37;
-        h = h * 17 + dn.hashCode();
-
-        /*
-        // We have to sort the Attributes if we want to compare two entries
-        SortedMap<String, EntryAttribute> sortedMap = new TreeMap<String, EntryAttribute>();
-
-        for ( String id:attributes.keySet() )
-        {
-            sortedMap.put( id, attributes.get( id ) );
-        }
-
-        for ( String id:sortedMap.keySet() )
-        {
-            h = h*17 + sortedMap.get( id ).hashCode();
-        }
-        */
-    }
-
-
-    /**
      * Get the hash code of this ClientEntry. The Attributes will be sorted
      * before the comparison can be done.
      *
@@ -2802,26 +2827,4 @@ public class DefaultEntry implements Ent
 
         return sb.toString();
     }
-
-
-    /**
-     * normalizes the given Dn if it was not already normalized
-     *
-     * @param dn the Dn to be normalized
-     */
-    private void normalizeDN( Dn dn )
-    {
-        if ( !dn.isNormalized() )
-        {
-            try
-            {
-                // The dn must be normalized
-                dn.normalize( schemaManager );
-            }
-            catch ( LdapException ne )
-            {
-                LOG.warn( "The Dn '{}' cannot be normalized", dn );
-            }
-        }
-    }
 }

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/EntrySerializer.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/EntrySerializer.java?rev=1081902&r1=1081901&r2=1081902&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/EntrySerializer.java (original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/EntrySerializer.java Tue Mar 15 18:49:20 2011
@@ -24,8 +24,6 @@ import java.io.ObjectInput;
 import java.io.ObjectOutput;
 
 import org.apache.directory.shared.ldap.model.exception.LdapInvalidDnException;
-import org.apache.directory.shared.ldap.model.name.Dn;
-import org.apache.directory.shared.ldap.model.name.DnSerializer;
 import org.apache.directory.shared.ldap.model.schema.SchemaManager;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -35,20 +33,12 @@ import org.slf4j.LoggerFactory;
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-public class EntrySerializer
+public final class EntrySerializer
 {
     /** The LoggerFactory used by this class */
     protected static final Logger LOG = LoggerFactory.getLogger( EntrySerializer.class );
 
     /**
-     * Private constructor.
-     */
-    private EntrySerializer()
-    {
-    }
-
-    
-    /**
      * Serializes a Entry instance.
      * 
      * @param principal The Entry instance to serialize
@@ -57,22 +47,7 @@ public class EntrySerializer
      */
     public static void serialize( Entry entry, ObjectOutput out ) throws IOException
     {
-        // First, the Dn
-        DnSerializer.serialize( entry.getDn(), out );
-
-        // Then the attributes.
-        int nbAttributes =  entry.size();
-        out.writeInt( nbAttributes );
-
-        // Iterate through the attributes
-        if ( nbAttributes > 0 )
-        {
-            for ( EntryAttribute attribute : entry )
-            {
-                EntryAttributeSerializer.serialize( attribute, out );
-            }
-        }
-        
+        entry.writeExternal( out );
         out.flush();
     }
     
@@ -87,26 +62,17 @@ public class EntrySerializer
      */
     public static Entry deserialize( SchemaManager schemaManager, ObjectInput in ) throws IOException, LdapInvalidDnException
     {
-        // The Dn
-        Dn dn = DnSerializer.deserialize( schemaManager, in );
-
-        // The attributes
-        int nbAttributes = in.readInt();
+        // The entry
+        Entry entry = new DefaultEntry( schemaManager );
         
-        EntryAttribute[] attributes = null;
-        
-        if ( nbAttributes > 0 )
+        try
         {
-            attributes = new EntryAttribute[ nbAttributes ];
-            
-            for ( int i = 0; i < nbAttributes; i++ )
-            {
-                EntryAttribute attribute = EntryAttributeSerializer.deserialize( schemaManager, in );
-                attributes[i] = attribute;
-            }
+            entry.readExternal( in );
+        }
+        catch ( ClassNotFoundException cnfe )
+        {
+            throw new IOException( cnfe.getMessage() );
         }
-        
-        Entry entry = new DefaultEntry( schemaManager, dn, attributes );
 
         return entry;
     }

Modified: directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/shared/ldap/model/entry/EntrySerializerTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/shared/ldap/model/entry/EntrySerializerTest.java?rev=1081902&r1=1081901&r2=1081902&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/shared/ldap/model/entry/EntrySerializerTest.java (original)
+++ directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/shared/ldap/model/entry/EntrySerializerTest.java Tue Mar 15 18:49:20 2011
@@ -46,7 +46,7 @@ import com.mycila.junit.concurrent.Concu
 public class EntrySerializerTest
 {
     @Test
-    public void testEntryFullSerialization() throws IOException, LdapException
+    public void testEntryFullSerialization() throws IOException, LdapException, ClassNotFoundException
     {
         Entry entry1 = LdifUtils.createEntry( 
             "dc=example, dc=com", 
@@ -58,14 +58,15 @@ public class EntrySerializerTest
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ObjectOutputStream out = new ObjectOutputStream( baos );
 
-        EntrySerializer.serialize( entry1, out );
+        entry1.writeExternal( out );
         
         ObjectInputStream in = null;
 
         byte[] data = baos.toByteArray();
         in = new ObjectInputStream( new ByteArrayInputStream( data ) );
 
-        Entry entry2 = EntrySerializer.deserialize( null, in );
+        Entry entry2 = new DefaultEntry();
+        entry2.readExternal( in );
 
         assertEquals( entry1, entry2 );
         assertTrue( entry2.contains( "ObjectClass", "top", "domain" ) );
@@ -73,7 +74,7 @@ public class EntrySerializerTest
     
     
     @Test
-    public void testEntryNoDnSerialization() throws IOException, LdapException
+    public void testEntryNoDnSerialization() throws IOException, LdapException, ClassNotFoundException
     {
         Entry entry1 = LdifUtils.createEntry( 
             "", 
@@ -85,14 +86,15 @@ public class EntrySerializerTest
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ObjectOutputStream out = new ObjectOutputStream( baos );
 
-        EntrySerializer.serialize( entry1, out );
+        entry1.writeExternal( out );
         
         ObjectInputStream in = null;
 
         byte[] data = baos.toByteArray();
         in = new ObjectInputStream( new ByteArrayInputStream( data ) );
 
-        Entry entry2 = EntrySerializer.deserialize( null, in );
+        Entry entry2 = new DefaultEntry();
+        entry2.readExternal( in );
 
         assertEquals( entry1, entry2 );
         assertTrue( entry2.contains( "ObjectClass", "top", "domain" ) );
@@ -101,21 +103,22 @@ public class EntrySerializerTest
 
 
     @Test
-    public void testEntryNoAttributesSerialization() throws IOException, LdapException
+    public void testEntryNoAttributesSerialization() throws IOException, LdapException, ClassNotFoundException
     {
         Entry entry1 = LdifUtils.createEntry( "dc=example, dc=com" ); 
         
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ObjectOutputStream out = new ObjectOutputStream( baos );
 
-        EntrySerializer.serialize( entry1, out );
+        entry1.writeExternal( out );
         
         ObjectInputStream in = null;
 
         byte[] data = baos.toByteArray();
         in = new ObjectInputStream( new ByteArrayInputStream( data ) );
 
-        Entry entry2 = EntrySerializer.deserialize( null, in );
+        Entry entry2 = new DefaultEntry();
+        entry2.readExternal( in );
 
         assertEquals( entry1, entry2 );
         assertEquals( 0, entry2.size() );