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 2016/05/28 08:51:35 UTC

svn commit: r1745854 - in /directory/shared/branches/shared-value: integ/src/test/java/org/apache/directory/api/ldap/model/name/ ldap/model/src/main/antlr/

Author: elecharny
Date: Sat May 28 08:51:34 2016
New Revision: 1745854

URL: http://svn.apache.org/viewvc?rev=1745854&view=rev
Log:
o Fixed the normName computation for Rdns
o Added a test for Ava serialization
o Added some tests for Dn and Rdn

Added:
    directory/shared/branches/shared-value/integ/src/test/java/org/apache/directory/api/ldap/model/name/AvaSerializationTest.java
Modified:
    directory/shared/branches/shared-value/integ/src/test/java/org/apache/directory/api/ldap/model/name/DnTest.java
    directory/shared/branches/shared-value/integ/src/test/java/org/apache/directory/api/ldap/model/name/RdnTest.java
    directory/shared/branches/shared-value/ldap/model/src/main/antlr/distinguishedName.g

Added: directory/shared/branches/shared-value/integ/src/test/java/org/apache/directory/api/ldap/model/name/AvaSerializationTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-value/integ/src/test/java/org/apache/directory/api/ldap/model/name/AvaSerializationTest.java?rev=1745854&view=auto
==============================================================================
--- directory/shared/branches/shared-value/integ/src/test/java/org/apache/directory/api/ldap/model/name/AvaSerializationTest.java (added)
+++ directory/shared/branches/shared-value/integ/src/test/java/org/apache/directory/api/ldap/model/name/AvaSerializationTest.java Sat May 28 08:51:34 2016
@@ -0,0 +1,393 @@
+/*
+ *  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.api.ldap.model.name;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.name.Ava;
+import org.apache.directory.api.ldap.model.schema.SchemaManager;
+import org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager;
+import org.apache.directory.api.util.Strings;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import com.mycila.junit.concurrent.Concurrency;
+import com.mycila.junit.concurrent.ConcurrentJunitRunner;
+
+
+/**
+ * Test the AttributeTypeAndValue class serialization
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(ConcurrentJunitRunner.class)
+@Concurrency()
+public class AvaSerializationTest
+{
+    /** A null schemaManager used in tests */
+    private static SchemaManager schemaManager;
+
+
+    @BeforeClass
+    public static void setup() throws Exception
+    {
+        schemaManager = new DefaultSchemaManager();
+    }
+
+
+    /**
+     * Test serialization of a simple ATAV
+     */
+    @Test
+    public void testStringAtavSerialization() throws LdapException, IOException, ClassNotFoundException
+    {
+        Ava atav = new Ava( schemaManager, "CN", "Test" );
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        atav.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        Ava atav2 = new Ava( schemaManager );
+        atav2.readExternal( in );
+
+        assertEquals( atav, atav2 );
+    }
+
+
+    @Test
+    public void testBinaryAtavSerialization() throws LdapException, IOException, ClassNotFoundException
+    {
+        byte[] upValue = Strings.getBytesUtf8( "  Test  " );
+
+        Ava atav = new Ava( schemaManager, "CN", upValue );
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        atav.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        Ava atav2 = new Ava( schemaManager );
+        atav2.readExternal( in );
+
+        assertEquals( atav, atav2 );
+    }
+
+
+    /**
+     * Test serialization of a simple ATAV
+     */
+    @Test
+    public void testNullAtavSerialization() throws LdapException, IOException, ClassNotFoundException
+    {
+        Ava atav = new Ava();
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        try
+        {
+            atav.writeExternal( out );
+            fail();
+        }
+        catch ( IOException ioe )
+        {
+            assertTrue( true );
+        }
+    }
+
+
+    @Test
+    public void testNullNormValueSerialization() throws LdapException, IOException, ClassNotFoundException
+    {
+        Ava atav = new Ava( schemaManager, "DC", ( String ) null );
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        try
+        {
+            atav.writeExternal( out );
+            fail();
+        }
+        catch ( IOException ioe )
+        {
+            String message = ioe.getMessage();
+            assertEquals( "Cannot serialize a wrong ATAV, the value should not be null", message );
+        }
+    }
+
+
+    @Test
+    public void testNullUpValueSerialization() throws LdapException, IOException, ClassNotFoundException
+    {
+        Ava atav = new Ava( schemaManager, "DC", ( String ) null );
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        try
+        {
+            atav.writeExternal( out );
+            fail();
+        }
+        catch ( IOException ioe )
+        {
+            String message = ioe.getMessage();
+            assertEquals( "Cannot serialize a wrong ATAV, the value should not be null", message );
+        }
+    }
+
+
+    @Test
+    public void testEmptyNormValueSerialization() throws LdapException, IOException, ClassNotFoundException
+    {
+        Ava atav = new Ava( schemaManager, "CN", "Test" );
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        atav.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        Ava atav2 = new Ava( schemaManager );
+        atav2.readExternal( in );
+
+        assertEquals( atav, atav2 );
+        assertEquals( "CN=Test", atav2.getName() );
+        assertEquals( "2.5.4.3", atav2.getNormType() );
+        assertEquals( " test ", atav2.getValue().getNormalized() );
+    }
+
+
+    @Test
+    public void testEmptyUpValueSerialization() throws LdapException, IOException, ClassNotFoundException
+    {
+        Ava atav = new Ava( schemaManager, "dc", "" );
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        atav.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        Ava atav2 = new Ava( schemaManager );
+        atav2.readExternal( in );
+
+        assertEquals( atav, atav2 );
+    }
+
+
+    /**
+     * Test serialization of a simple ATAV
+     */
+    @Test
+    public void testStringAtavStaticSerialization() throws LdapException, IOException, ClassNotFoundException
+    {
+        Ava atav = new Ava( schemaManager, "CN", "Test" );
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        atav.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        Ava atav2 = new Ava( schemaManager );
+        atav2.readExternal( in );
+
+        assertEquals( atav, atav2 );
+    }
+
+
+    @Test
+    public void testBinaryAtavStaticSerialization() throws LdapException, IOException, ClassNotFoundException
+    {
+        byte[] upValue = Strings.getBytesUtf8( "  Test  " );
+
+        Ava atav = new Ava( schemaManager, "CN", upValue );
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        atav.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        Ava atav2 = new Ava( schemaManager );
+        atav2.readExternal( in );
+
+        assertEquals( atav, atav2 );
+    }
+
+
+    /**
+     * Test static serialization of a simple ATAV
+     */
+    @Test
+    public void testNullAtavStaticSerialization() throws LdapException, IOException, ClassNotFoundException
+    {
+        Ava atav = new Ava();
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        try
+        {
+            atav.writeExternal( out );
+            fail();
+        }
+        catch ( IOException ioe )
+        {
+            assertTrue( true );
+        }
+    }
+
+
+    @Test
+    public void testNullUpValueStaticSerialization() throws LdapException, IOException, ClassNotFoundException
+    {
+        Ava atav = new Ava( schemaManager, "DC", ( String ) null );
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        try
+        {
+            atav.writeExternal( out );
+            fail();
+        }
+        catch ( IOException ioe )
+        {
+            String message = ioe.getMessage();
+            assertEquals( "Cannot serialize a wrong ATAV, the value should not be null", message );
+        }
+    }
+
+
+    @Test
+    public void testEmptyNormValueStaticSerialization() throws LdapException, IOException, ClassNotFoundException
+    {
+        Ava atav = new Ava( schemaManager, "DC", "" );
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        atav.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        Ava atav2 = new Ava( schemaManager );
+        atav2.readExternal( in );
+
+        assertEquals( atav, atav2 );
+    }
+
+
+    @Test
+    public void testEmptyUpValueStaticSerialization() throws LdapException, IOException, ClassNotFoundException
+    {
+        Ava atav = new Ava( schemaManager, "DC", "" );
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        atav.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        Ava atav2 = new Ava( schemaManager );
+        atav2.readExternal( in );
+
+        assertEquals( atav, atav2 );
+    }
+
+
+    @Test
+    @Ignore
+    public void testSerializationPerf() throws LdapException, IOException, ClassNotFoundException
+    {
+        Ava atav = new Ava( schemaManager, "cn", "This is a serialization test" );
+
+        long t0 = System.currentTimeMillis();
+        
+        for ( int j = 0; j < 1000; j++ )
+        {
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            ObjectOutputStream out = new ObjectOutputStream( baos );
+            
+            for ( int i = 0; i < 100000; i++ )
+            {
+        
+                atav.writeExternal( out );
+                
+                out.flush();
+            }
+        
+            out.close();
+            baos.close();
+        }
+        
+        long t1 = System.currentTimeMillis();
+        System.out.println( "delta AVA new serialization : " + ( t1 - t0 ) );
+    }
+}

Modified: directory/shared/branches/shared-value/integ/src/test/java/org/apache/directory/api/ldap/model/name/DnTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-value/integ/src/test/java/org/apache/directory/api/ldap/model/name/DnTest.java?rev=1745854&r1=1745853&r2=1745854&view=diff
==============================================================================
--- directory/shared/branches/shared-value/integ/src/test/java/org/apache/directory/api/ldap/model/name/DnTest.java (original)
+++ directory/shared/branches/shared-value/integ/src/test/java/org/apache/directory/api/ldap/model/name/DnTest.java Sat May 28 08:51:34 2016
@@ -377,6 +377,7 @@ public class DnTest
 
         assertEquals( "cn=Exa\\+mple  one", dn2.getEscaped() );
         assertEquals( "cn = Exa\\+mple  one ", dn2.getName() );
+        assertEquals( "2.5.4.3= exa+mple  one ", dn2.getNormName() );
     }
 
 
@@ -1954,12 +1955,14 @@ public class DnTest
     {
         Dn name = new Dn( "ou= Some   People   ", "dc = eXample", "dc= cOm" );
 
-        assertTrue( name.getName().equals( "ou= Some   People   ,dc = eXample,dc= cOm" ) );
+        assertEquals( "ou= Some   People   ,dc = eXample,dc= cOm", name.getName() );
 
         Dn result = new Dn( schemaManager, name );
 
         assertEquals( "ou=Some   People,dc=eXample,dc=cOm",
             result.getEscaped() );
+        assertEquals( "2.5.4.11= some  people ,0.9.2342.19200300.100.1.25= example ,0.9.2342.19200300.100.1.25= com ", 
+            result.getNormName() );
     }
 
 
@@ -2712,14 +2715,19 @@ public class DnTest
     public void testNormalizeBackSlash() throws Exception
     {
         Dn dn = new Dn( "cn=A\\,b,dc=com" );
-        new Dn( schemaManager, dn );
-        
-        System.out.println( dn.toString() );
-        System.out.println( dn );
-        System.out.println( dn.getName() );
-        System.out.println( dn.getEscaped() );
+        Dn newDn = new Dn( schemaManager, dn );
         
-        System.out.println( dn.getRdn().getAva().getValue().getValue() );
+        // The original DN
+        assertEquals( "cn=A\\,b,dc=com", dn.toString() );
+        assertEquals( "cn=A\\,b,dc=com", dn.getName() );
+        assertEquals( "cn=A,b,dc=com", dn.getNormName() );
+        assertEquals( "cn=A\\,b,dc=com", dn.getEscaped() );
+
+        // The new DN
+        assertEquals( "cn=A\\,b,dc=com", newDn.toString() );
+        assertEquals( "cn=A\\,b,dc=com", newDn.getName() );
+        assertEquals( "2.5.4.3= a,b ,0.9.2342.19200300.100.1.25= com ", newDn.getNormName() );
+        assertEquals( "cn=A\\,b,dc=com", newDn.getEscaped() );
     }
 
 
@@ -2987,6 +2995,7 @@ public class DnTest
     public void testDnParsing() throws LdapInvalidDnException
     {
         long[] deltas = new long[10];
+        long allDeltas = 0L;
         
         for ( int j = 0; j < 10; j++ )
         {
@@ -3003,7 +3012,6 @@ public class DnTest
             System.out.println( "Iteration[" + j + "] : " + deltas[j] );
         }
         
-        long allDeltas = 0L;
         
         for ( int i = 0; i < 10; i++ )
         {

Modified: directory/shared/branches/shared-value/integ/src/test/java/org/apache/directory/api/ldap/model/name/RdnTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-value/integ/src/test/java/org/apache/directory/api/ldap/model/name/RdnTest.java?rev=1745854&r1=1745853&r2=1745854&view=diff
==============================================================================
--- directory/shared/branches/shared-value/integ/src/test/java/org/apache/directory/api/ldap/model/name/RdnTest.java (original)
+++ directory/shared/branches/shared-value/integ/src/test/java/org/apache/directory/api/ldap/model/name/RdnTest.java Sat May 28 08:51:34 2016
@@ -50,6 +50,69 @@ public class RdnTest
         schemaManager = new DefaultSchemaManager();
     }
 
+
+
+    /**
+     * Test a null Rdn
+     */
+    @Test
+    public void testRdnNull()
+    {
+        Rdn rdn = new Rdn( schemaManager );
+        assertEquals( "", rdn.toString() );
+        assertEquals( "", rdn.getName() );
+        assertEquals( "", rdn.getNormName() );
+    }
+
+
+    /**
+     * test an empty Rdn
+     * 
+     * @throws LdapException
+     */
+    @Test
+    public void testRdnEmpty() throws LdapException
+    {
+        Rdn rdn = new Rdn( schemaManager, "" );
+        assertEquals( "", rdn.toString() );
+        assertEquals( "", rdn.getName() );
+        assertEquals( "", rdn.getNormName() );
+    }
+
+
+    /**
+     * test a simple Rdn : ' cn = b    C d'
+     * 
+     * @throws LdapException
+     */
+    @Test
+    public void testRdnSimple() throws LdapException
+    {
+        Rdn rdn = new Rdn( schemaManager, " cn = b    C d" );
+        assertEquals( " cn = b    C d", rdn.toString() );
+        assertEquals( " cn = b    C d", rdn.getName() );
+        assertEquals( "2.5.4.3= b  c  d ", rdn.getNormName() );
+
+        Rdn rdn2 = new Rdn( " cn = b    C d" );
+        assertEquals( " cn = b    C d", rdn2.toString() );
+        assertEquals( " cn = b    C d", rdn2.getName() );
+        assertEquals( "cn=b    C d", rdn2.getNormName() );
+    }
+
+
+    /**
+     * test a simple Rdn with no value : ' dc = '
+     * 
+     * @throws LdapException
+     */
+    @Test
+    public void testRdnSimpleEmptyValue() throws LdapException
+    {
+        Rdn rdn = new Rdn( schemaManager, " dc = " );
+        assertEquals( " dc = ", rdn.toString() );
+        assertEquals( " dc = ", rdn.getName() );
+        assertEquals( "0.9.2342.19200300.100.1.25=  ", rdn.getNormName() );
+    }
     
     
     @Test

Modified: directory/shared/branches/shared-value/ldap/model/src/main/antlr/distinguishedName.g
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-value/ldap/model/src/main/antlr/distinguishedName.g?rev=1745854&r1=1745853&r2=1745854&view=diff
==============================================================================
--- directory/shared/branches/shared-value/ldap/model/src/main/antlr/distinguishedName.g (original)
+++ directory/shared/branches/shared-value/ldap/model/src/main/antlr/distinguishedName.g Sat May 28 08:51:34 2016
@@ -331,7 +331,7 @@ relativeDistinguishedName [SchemaManager
             }
             else
             {
-                rdnNormStr.append( tmp );
+                rdnNormStr.append( currentAva.getNormType() );
             }
             
             rdnNormStr.append( '=' );