directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1423968 [5/27] - in /directory/shared/branches/shared-refact: dsml/engine/src/main/java/org/apache/directory/shared/dsmlv2/engine/ dsml/parser/src/main/java/org/apache/directory/shared/dsmlv2/ dsml/parser/src/main/java/org/apache/directory...
Date Wed, 19 Dec 2012 17:24:42 GMT
Added: directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/SchemaAwareEntryTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/SchemaAwareEntryTest.java?rev=1423968&view=auto
==============================================================================
--- directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/SchemaAwareEntryTest.java (added)
+++ directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/SchemaAwareEntryTest.java Wed Dec 19 17:24:08 2012
@@ -0,0 +1,1268 @@
+/*
+ * 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.entry;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertNull;
+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 java.util.Arrays;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.directory.api.ldap.model.entry.Attribute;
+import org.apache.directory.api.ldap.model.entry.BinaryValue;
+import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
+import org.apache.directory.api.ldap.model.entry.DefaultEntry;
+import org.apache.directory.api.ldap.model.entry.Entry;
+import org.apache.directory.api.ldap.model.entry.StringValue;
+import org.apache.directory.api.ldap.model.entry.Value;
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.name.Dn;
+import org.apache.directory.api.ldap.model.schema.SchemaManager;
+import org.apache.directory.api.util.Strings;
+import org.apache.directory.shared.ldap.schemamanager.impl.DefaultSchemaManager;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import com.mycila.junit.concurrent.Concurrency;
+import com.mycila.junit.concurrent.ConcurrentJunitRunner;
+
+
+/**
+ * A test class for the DefaultEntry class
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(ConcurrentJunitRunner.class)
+@Concurrency()
+public class SchemaAwareEntryTest
+{
+    private static Dn EXAMPLE_DN;
+    private static final byte[] BYTES1 = new byte[]
+        { 'a', 'b' };
+    private static final byte[] BYTES2 = new byte[]
+        { 'b' };
+    private static final byte[] BYTES3 = new byte[]
+        { 'c' };
+
+    private static SchemaManager schemaManager;
+
+
+    /**
+     * Helper method which creates an entry with 4 attributes.
+     */
+    private Entry createEntry()
+    {
+        try
+        {
+            Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+            Attribute attrOC = new DefaultAttribute( "objectClass", "top", "person" );
+            Attribute attrCN = new DefaultAttribute( "cn", "test1", "test2" );
+            Attribute attrSN = new DefaultAttribute( "sn", "Test1", "Test2" );
+            Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, BYTES2 );
+
+            entry.put( attrOC, attrCN, attrSN, attrPWD );
+
+            return entry;
+        }
+        catch ( LdapException ne )
+        {
+            // Do nothing
+            return null;
+        }
+    }
+
+
+    /**
+     * Serialize a ClientEntry
+     */
+    private ByteArrayOutputStream serializeValue( Entry value ) throws IOException
+    {
+        ObjectOutputStream oOut = null;
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+        try
+        {
+            oOut = new ObjectOutputStream( out );
+            oOut.writeObject( value );
+        }
+        catch ( IOException ioe )
+        {
+            throw ioe;
+        }
+        finally
+        {
+            try
+            {
+                if ( oOut != null )
+                {
+                    oOut.flush();
+                    oOut.close();
+                }
+            }
+            catch ( IOException ioe )
+            {
+                throw ioe;
+            }
+        }
+
+        return out;
+    }
+
+
+    /**
+     * Deserialize a ClientEntry
+     */
+    private Entry deserializeValue( ByteArrayOutputStream out ) throws IOException, ClassNotFoundException
+    {
+        ObjectInputStream oIn = null;
+        ByteArrayInputStream in = new ByteArrayInputStream( out.toByteArray() );
+
+        try
+        {
+            oIn = new ObjectInputStream( in );
+
+            Entry value = ( Entry ) oIn.readObject();
+
+            return value;
+        }
+        catch ( IOException ioe )
+        {
+            throw ioe;
+        }
+        finally
+        {
+            try
+            {
+                if ( oIn != null )
+                {
+                    oIn.close();
+                }
+            }
+            catch ( IOException ioe )
+            {
+                throw ioe;
+            }
+        }
+    }
+
+
+    /**
+     * @throws java.lang.Exception
+     */
+    @BeforeClass
+    public static void setUpBeforeClass() throws Exception
+    {
+        EXAMPLE_DN = new Dn( "dc=example,dc=com" );
+        schemaManager = new DefaultSchemaManager();
+    }
+
+
+    /**
+     * Test method for DefaultEntry()
+     */
+    @Test
+    public void testDefaultClientEntry()
+    {
+        Entry entry = new DefaultEntry();
+
+        assertNotNull( entry );
+        assertEquals( Dn.EMPTY_DN, entry.getDn() );
+        assertEquals( 0, entry.size() );
+    }
+
+
+    /**
+     * Test method for DefaultEntry()
+     */
+    @Test
+    public void testDefaultClientEntryLdif() throws Exception
+    {
+        Entry entry = new DefaultEntry(
+            "ou=example, dc=com",
+            "ObjectClass: top",
+            "ObjectClass: person",
+            "cn: test",
+            "sn: test" );
+
+        assertNotNull( entry );
+        assertEquals( "ou=example, dc=com", entry.getDn().toString() );
+        assertEquals( 3, entry.size() );
+        assertTrue( entry.contains( "objectClass", "top", "person" ) );
+        assertTrue( entry.contains( "cn", "test" ) );
+        assertTrue( entry.contains( "sn", "test" ) );
+    }
+
+
+    /**
+     * Test method for DefaultEntry( Dn )
+     */
+    @Test
+    public void testDefaultClientEntryLdapDN()
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        assertNotNull( entry );
+        assertNotNull( entry.getDn() );
+        assertEquals( EXAMPLE_DN, entry.getDn() );
+        assertEquals( 0, entry.size() );
+    }
+
+
+    /**
+     * Test method for add( EntryAttribute... )
+     */
+    @Test
+    public void testAddEntryAttributeArray() throws LdapException
+    {
+        Entry entry = createEntry();
+
+        assertEquals( 4, entry.size() );
+        assertTrue( entry.containsAttribute( "ObjectClass" ) );
+        assertTrue( entry.containsAttribute( "CN" ) );
+        assertTrue( entry.containsAttribute( "  sn  " ) );
+        assertTrue( entry.containsAttribute( "userPassword" ) );
+
+        Attribute attr = entry.get( "objectclass" );
+        assertEquals( 2, attr.size() );
+
+        Attribute attrCN2 = new DefaultAttribute( "cn", "test1", "test3" );
+        entry.add( attrCN2 );
+        assertEquals( 4, entry.size() );
+        attr = entry.get( "cn" );
+        assertEquals( 3, attr.size() );
+        assertTrue( attr.contains( "test1", "test2", "test3" ) );
+
+        // Check adding some byte[] values (they will not be transformed to Strings)
+        attrCN2.clear();
+        attrCN2.add( BYTES1, BYTES2 );
+        entry.add( attrCN2 );
+        assertEquals( 4, entry.size() );
+        attr = entry.get( "cn" );
+        assertEquals( 3, attr.size() );
+        assertTrue( attr.contains( "test1", "test2", "test3" ) );
+        assertFalse( attr.contains( "ab", "b" ) );
+    }
+
+
+    /**
+     * Test method for add( String, byte[]... )
+     */
+    @Test
+    public void testAddStringByteArrayArray() throws LdapException
+    {
+        Entry entry = new DefaultEntry();
+
+        entry.add( "userPassword", ( byte[] ) null );
+        assertEquals( 1, entry.size() );
+        Attribute attributePWD = entry.get( "userPassword" );
+        assertEquals( 1, attributePWD.size() );
+        assertNotNull( attributePWD.get() );
+        assertNull( attributePWD.get().getValue() );
+
+        entry.add( "jpegPhoto", BYTES1, BYTES1, BYTES2 );
+        assertEquals( 2, entry.size() );
+        Attribute attributeJPG = entry.get( "jpegPhoto" );
+        assertEquals( 2, attributeJPG.size() );
+        assertNotNull( attributeJPG.get() );
+        assertTrue( attributeJPG.contains( BYTES1 ) );
+        assertTrue( attributeJPG.contains( BYTES2 ) );
+    }
+
+
+    /**
+     * Test method for add( String, String... )
+     */
+    @Test
+    public void testAddStringStringArray() throws LdapException
+    {
+        Entry entry = new DefaultEntry();
+
+        entry.add( "cn", ( String ) null );
+        assertEquals( 1, entry.size() );
+        Attribute attributeCN = entry.get( "cn" );
+        assertEquals( 1, attributeCN.size() );
+        assertNotNull( attributeCN.get() );
+        assertNull( attributeCN.get().getValue() );
+
+        entry.add( "sn", "test", "test", "TEST" );
+        assertEquals( 2, entry.size() );
+        Attribute attributeSN = entry.get( "sn" );
+        assertEquals( 2, attributeSN.size() );
+        assertNotNull( attributeSN.get() );
+        assertTrue( attributeSN.contains( "test" ) );
+        assertTrue( attributeSN.contains( "TEST" ) );
+    }
+
+
+    /**
+     * Test method for add( String, Value<?>... )
+     */
+    @Test
+    public void testAddStringValueArray() throws LdapException
+    {
+        Entry entry = new DefaultEntry();
+
+        Value<String> value = new StringValue( ( String ) null );
+
+        entry.add( "cn", value );
+        assertEquals( 1, entry.size() );
+        Attribute attributeCN = entry.get( "cn" );
+        assertEquals( 1, attributeCN.size() );
+        assertNotNull( attributeCN.get() );
+        assertNull( attributeCN.get().getValue() );
+
+        Value<String> value1 = new StringValue( "test1" );
+        Value<String> value2 = new StringValue( "test2" );
+        Value<String> value3 = new StringValue( "test1" );
+
+        entry.add( "sn", value1, value2, value3 );
+        assertEquals( 2, entry.size() );
+        Attribute attributeSN = entry.get( "sn" );
+        assertEquals( 2, attributeSN.size() );
+        assertNotNull( attributeSN.get() );
+        assertTrue( attributeSN.contains( value1 ) );
+        assertTrue( attributeSN.contains( value2 ) );
+
+        Value<byte[]> value4 = new BinaryValue( BYTES1 );
+        entry.add( "l", value1, value4 );
+        assertEquals( 3, entry.size() );
+        Attribute attributeL = entry.get( "l" );
+        assertEquals( 2, attributeL.size() );
+        assertNotNull( attributeL.get() );
+        assertTrue( attributeL.contains( value1 ) );
+
+        // The byte[] value must have been transformed to a String
+        assertTrue( attributeL.contains( "ab" ) );
+    }
+
+
+    /**
+     * Test method for clear()
+     */
+    @Test
+    public void testClear() throws LdapException
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        assertEquals( 0, entry.size() );
+        assertNull( entry.get( "ObjectClass" ) );
+        entry.clear();
+        assertEquals( 0, entry.size() );
+        assertNull( entry.get( "ObjectClass" ) );
+
+        entry.add( "ObjectClass", "top", "person" );
+        assertEquals( 1, entry.size() );
+        assertNotNull( entry.get( "ObjectClass" ) );
+
+        entry.clear();
+        assertEquals( 0, entry.size() );
+        assertNull( entry.get( "ObjectClass" ) );
+    }
+
+
+    /**
+     * Test method for clone()
+     */
+    @Test
+    public void testClone() throws LdapException
+    {
+        Entry entry1 = new DefaultEntry();
+
+        Entry entry2 = entry1.clone();
+
+        assertEquals( entry1, entry2 );
+        entry2.setDn( EXAMPLE_DN );
+
+        assertEquals( Dn.EMPTY_DN, entry1.getDn() );
+
+        entry1.setDn( EXAMPLE_DN );
+        entry2 = entry1.clone();
+        assertEquals( entry1, entry2 );
+
+        entry1.add( "objectClass", "top", "person" );
+        entry1.add( "cn", "test1", "test2" );
+
+        entry2 = entry1.clone();
+        assertEquals( entry1, entry2 );
+
+        entry1.add( "cn", "test3" );
+        assertEquals( 2, entry2.get( "cn" ).size() );
+        assertFalse( entry2.contains( "cn", "test3" ) );
+
+        entry1.add( "sn", ( String ) null );
+        assertFalse( entry2.containsAttribute( "sn" ) );
+    }
+
+
+    /**
+     * Test method for contains( EntryAttribute... )
+     */
+    @Test
+    public void testContainsEntryAttributeArray() throws LdapException
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        Attribute attrOC = new DefaultAttribute( "objectClass", "top", "person" );
+        Attribute attrCN = new DefaultAttribute( "cn", "test1", "test2" );
+        Attribute attrSN = new DefaultAttribute( "sn", "Test1", "Test2" );
+        Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, BYTES2 );
+
+        assertFalse( entry.contains( attrOC, attrCN ) );
+
+        entry.add( attrOC, attrCN );
+
+        assertTrue( entry.contains( attrOC, attrCN ) );
+        assertFalse( entry.contains( attrOC, attrCN, attrSN ) );
+
+        entry.add( attrSN, attrPWD );
+
+        assertTrue( entry.contains( attrSN, attrPWD ) );
+    }
+
+
+    /**
+     * Test method for contains( String, byte[]... )
+     */
+    @Test
+    public void testContainsStringByteArray() throws LdapException
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        assertFalse( entry.containsAttribute( "objectClass" ) );
+
+        Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, ( byte[] ) null, BYTES2 );
+
+        entry.add( attrPWD );
+
+        assertTrue( entry.contains( "  userPASSWORD  ", BYTES1, BYTES2 ) );
+        assertTrue( entry.contains( "  userPASSWORD  ", ( byte[] ) null ) );
+
+        // We can search for byte[] using Strings. the strings will be converted to byte[]
+        assertTrue( entry.contains( "  userPASSWORD  ", "ab", "b" ) );
+
+        assertFalse( entry.contains( "  userPASSWORD  ", "ab", "b", "d" ) );
+    }
+
+
+    /**
+     * Test method for contains( String, String... )
+     */
+    @Test
+    public void testContainsStringStringArray() throws LdapException
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        assertFalse( entry.containsAttribute( "objectClass" ) );
+
+        Attribute attrOC = new DefaultAttribute( "objectClass", "top", "person" );
+        Attribute attrCN = new DefaultAttribute( "cn", "test1", "test2" );
+        Attribute attrSN = new DefaultAttribute( "sn", "Test1", "Test2", ( String ) null );
+        Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, BYTES2 );
+
+        entry.add( attrOC, attrCN, attrSN, attrPWD );
+
+        assertTrue( entry.contains( "OBJECTCLASS", "top", "person" ) );
+        assertTrue( entry.contains( " cn ", "test1", "test2" ) );
+        assertTrue( entry.contains( "Sn", "Test1", "Test2", ( String ) null ) );
+        assertTrue( entry.contains( "  userPASSWORD  ", "ab", "b" ) );
+
+        assertFalse( entry.contains( "OBJECTCLASS", "PERSON" ) );
+        assertFalse( entry.contains( " cn ", "test1", "test3" ) );
+        assertFalse( entry.contains( "Sn", "Test" ) );
+        assertFalse( entry.contains( "  userPASSWORD  ", ( String ) null ) );
+    }
+
+
+    /**
+     * Test method for contains( Sring, Value<?>... )
+     */
+    @Test
+    public void testContainsStringValueArray() throws LdapException
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        assertFalse( entry.containsAttribute( "objectClass" ) );
+
+        Attribute attrCN = new DefaultAttribute( "cn", "test1", "test2", ( String ) null );
+        Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, BYTES2, ( byte[] ) null );
+
+        entry.add( attrCN, attrPWD );
+
+        Value<String> strValue1 = new StringValue( "test1" );
+        Value<String> strValue2 = new StringValue( "test2" );
+        Value<String> strValue3 = new StringValue( "test3" );
+        Value<String> strNullValue = new StringValue( ( String ) null );
+
+        Value<byte[]> binValue1 = new BinaryValue( BYTES1 );
+        Value<byte[]> binValue2 = new BinaryValue( BYTES2 );
+        Value<byte[]> binValue3 = new BinaryValue( BYTES3 );
+        Value<byte[]> binNullValue = new BinaryValue( ( byte[] ) null );
+
+        assertTrue( entry.contains( "CN", strValue1, strValue2, strNullValue ) );
+        assertTrue( entry.contains( "userpassword", binValue1, binValue2, binNullValue ) );
+
+        assertFalse( entry.contains( "cn", strValue3 ) );
+        assertFalse( entry.contains( "UserPassword", binValue3 ) );
+    }
+
+
+    /**
+     * Test method for containsAttribute( String )
+     */
+    @Test
+    public void testContainsAttribute() throws LdapException
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        assertFalse( entry.containsAttribute( "objectClass" ) );
+
+        Attribute attrOC = new DefaultAttribute( "objectClass", "top", "person" );
+        Attribute attrCN = new DefaultAttribute( "cn", "test1", "test2" );
+        Attribute attrSN = new DefaultAttribute( "sn", "Test1", "Test2" );
+        Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, BYTES2 );
+
+        entry.add( attrOC, attrCN, attrSN, attrPWD );
+
+        assertTrue( entry.containsAttribute( "OBJECTCLASS" ) );
+        assertTrue( entry.containsAttribute( " cn " ) );
+        assertTrue( entry.containsAttribute( "Sn" ) );
+        assertTrue( entry.containsAttribute( "  userPASSWORD  " ) );
+
+        entry.clear();
+
+        assertFalse( entry.containsAttribute( "OBJECTCLASS" ) );
+        assertFalse( entry.containsAttribute( " cn " ) );
+        assertFalse( entry.containsAttribute( "Sn" ) );
+        assertFalse( entry.containsAttribute( "  userPASSWORD  " ) );
+    }
+
+
+    /**
+     * Test method for equals()
+     */
+    @Test
+    public void testEqualsObject() throws LdapException
+    {
+        Entry entry1 = new DefaultEntry();
+        Entry entry2 = new DefaultEntry();
+
+        assertEquals( entry1, entry2 );
+
+        entry1.setDn( EXAMPLE_DN );
+        assertNotSame( entry1, entry2 );
+
+        entry2.setDn( EXAMPLE_DN );
+        assertEquals( entry1, entry2 );
+
+        Attribute attrOC = new DefaultAttribute( "objectClass", "top", "person" );
+        Attribute attrCN = new DefaultAttribute( "cn", "test1", "test2" );
+        Attribute attrSN = new DefaultAttribute( "sn", "Test1", "Test2" );
+        Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, BYTES2 );
+
+        entry1.put( attrOC, attrCN, attrSN, attrPWD );
+        entry2.put( attrOC, attrCN, attrSN );
+        assertNotSame( entry1, entry2 );
+
+        entry2.put( attrPWD );
+        assertEquals( entry1, entry2 );
+
+        Attribute attrL1 = new DefaultAttribute( "l", "Paris", "New-York" );
+        Attribute attrL2 = new DefaultAttribute( "l", "Paris", "Tokyo" );
+
+        entry1.put( attrL1 );
+        entry2.put( attrL1 );
+        assertEquals( entry1, entry2 );
+
+        entry1.add( "l", "London" );
+        assertNotSame( entry1, entry2 );
+
+        entry2.add( attrL2 );
+        assertNotSame( entry1, entry2 );
+
+        entry1.clear();
+        entry2.clear();
+        assertEquals( entry1, entry2 );
+    }
+
+
+    /**
+     * Test method for get( String )
+     */
+    @Test
+    public void testGet() throws LdapException
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        assertNull( entry.get( "objectClass" ) );
+
+        Attribute attrOC = new DefaultAttribute( "objectClass", "top", "person" );
+        Attribute attrCN = new DefaultAttribute( "cn", "test1", "test2" );
+        Attribute attrSN = new DefaultAttribute( "sn", "Test1", "Test2" );
+        Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, BYTES2 );
+
+        entry.add( attrOC, attrCN, attrSN, attrPWD );
+
+        assertNotNull( entry.get( "  CN  " ) );
+        Attribute attribute = entry.get( "cN" );
+
+        assertEquals( attribute, attrCN );
+
+        assertNull( entry.get( ( String ) null ) );
+        assertNull( entry.get( "  " ) );
+        assertNull( entry.get( "l" ) );
+    }
+
+
+    /**
+     * Test method for getDN()
+     */
+    @Test
+    public void testGetDn() throws LdapException
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        assertEquals( EXAMPLE_DN, entry.getDn() );
+
+        Dn testDn = new Dn( "cn=test" );
+        entry.setDn( testDn );
+
+        assertEquals( testDn, entry.getDn() );
+    }
+
+
+    /**
+     * Test method for hashcode()
+     */
+    @Test
+    public void testHashCode() throws LdapException, LdapException
+    {
+        Entry entry1 = new DefaultEntry( EXAMPLE_DN );
+        Entry entry2 = new DefaultEntry( EXAMPLE_DN );
+
+        assertEquals( entry1.hashCode(), entry2.hashCode() );
+
+        entry2.setDn( new Dn( "ou=system,dc=com" ) );
+        assertNotSame( entry1.hashCode(), entry2.hashCode() );
+
+        entry2.setDn( EXAMPLE_DN );
+        assertEquals( entry1.hashCode(), entry2.hashCode() );
+
+        Attribute attrOC = new DefaultAttribute( "objectClass", "top", "person" );
+        Attribute attrCN = new DefaultAttribute( "cn", "test1", "test2" );
+        Attribute attrSN = new DefaultAttribute( "sn", "Test1", "Test2" );
+        Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, BYTES2 );
+
+        entry1.add( attrOC, attrCN, attrSN, attrPWD );
+        entry2.add( attrOC, attrCN, attrSN, attrPWD );
+
+        assertEquals( entry1.hashCode(), entry2.hashCode() );
+
+        Entry entry3 = new DefaultEntry( EXAMPLE_DN );
+        entry3.add( attrOC, attrSN, attrCN, attrPWD );
+
+        assertEquals( entry1.hashCode(), entry3.hashCode() );
+    }
+
+
+    /**
+     * Test method for hasObjectClass( String )
+     */
+    @Test
+    public void testHasObjectClass() throws LdapException
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        assertFalse( entry.containsAttribute( "objectClass" ) );
+        assertFalse( entry.hasObjectClass( "top" ) );
+
+        entry.add( new DefaultAttribute( "objectClass", "top", "person" ) );
+
+        assertTrue( entry.hasObjectClass( "top" ) );
+        assertTrue( entry.hasObjectClass( "person" ) );
+        assertFalse( entry.hasObjectClass( "inetorgperson" ) );
+        assertFalse( entry.hasObjectClass( ( String ) null ) );
+        assertFalse( entry.hasObjectClass( "" ) );
+    }
+
+
+    /**
+     * Test method for Iterator()
+     */
+    @Test
+    public void testIterator() throws LdapException
+    {
+        Entry entry = createEntry();
+
+        Iterator<Attribute> iterator = entry.iterator();
+
+        assertTrue( iterator.hasNext() );
+
+        Set<String> expectedIds = new HashSet<String>();
+        expectedIds.add( "objectclass" );
+        expectedIds.add( "cn" );
+        expectedIds.add( "sn" );
+        expectedIds.add( "userpassword" );
+
+        while ( iterator.hasNext() )
+        {
+            Attribute attribute = iterator.next();
+
+            String id = attribute.getId();
+            assertTrue( expectedIds.contains( id ) );
+            expectedIds.remove( id );
+        }
+
+        assertEquals( 0, expectedIds.size() );
+    }
+
+
+    /**
+     * Test method for put( EntryAttribute... )
+     */
+    @Test
+    public void testPutEntryAttributeArray() throws LdapException
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        Attribute attrOC = new DefaultAttribute( "objectClass", "top", "person" );
+        Attribute attrCN = new DefaultAttribute( "cn", "test1", "test2" );
+        Attribute attrSN = new DefaultAttribute( "sn", "Test1", "Test2" );
+        Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, BYTES2 );
+
+        List<Attribute> removed = entry.put( attrOC, attrCN, attrSN, attrPWD );
+
+        assertEquals( 4, entry.size() );
+        assertEquals( 0, removed.size() );
+        assertTrue( entry.containsAttribute( "ObjectClass" ) );
+        assertTrue( entry.containsAttribute( "CN" ) );
+        assertTrue( entry.containsAttribute( "  sn  " ) );
+        assertTrue( entry.containsAttribute( "userPassword" ) );
+
+        Attribute attrCN2 = new DefaultAttribute( "cn", "test3", "test4" );
+        removed = entry.put( attrCN2 );
+        assertEquals( 4, entry.size() );
+        assertEquals( 1, removed.size() );
+        assertTrue( entry.containsAttribute( "CN" ) );
+        assertTrue( entry.contains( "cn", "test3", "test4" ) );
+    }
+
+
+    /**
+     * Test method for put( String, byte[]... )
+     */
+    @Test
+    public void testPutStringByteArrayArray()
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        try
+        {
+            entry.put( ( String ) null, BYTES1 );
+            fail();
+        }
+        catch ( IllegalArgumentException iae )
+        {
+            assertTrue( true );
+        }
+
+        try
+        {
+            entry.put( "   ", BYTES1 );
+            fail();
+        }
+        catch ( IllegalArgumentException iae )
+        {
+            assertTrue( true );
+        }
+
+        entry.put( "userPassword", ( byte[] ) null );
+        assertEquals( 1, entry.size() );
+        assertNotNull( entry.get( "userPassword" ) );
+        assertEquals( 1, entry.get( "userPassword" ).size() );
+        assertNull( entry.get( "userPassword" ).get().getValue() );
+
+        entry.put( "jpegPhoto", BYTES1, BYTES2, BYTES1 );
+        assertEquals( 2, entry.size() );
+        assertNotNull( entry.get( "jpegPhoto" ) );
+        assertEquals( 2, entry.get( "JPEGPhoto" ).size() );
+        Attribute attribute = entry.get( "jpegPhoto" );
+        assertTrue( attribute.contains( BYTES1 ) );
+        assertTrue( attribute.contains( BYTES2 ) );
+        assertEquals( "jpegphoto", attribute.getId() );
+        assertEquals( "jpegPhoto", attribute.getUpId() );
+    }
+
+
+    /**
+     * Test method for put( String, String... )
+     */
+    @Test
+    public void testPutStringStringArray()
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        try
+        {
+            entry.put( ( String ) null, "a" );
+            fail();
+        }
+        catch ( IllegalArgumentException iae )
+        {
+            assertTrue( true );
+        }
+
+        try
+        {
+            entry.put( "   ", "a" );
+            fail();
+        }
+        catch ( IllegalArgumentException iae )
+        {
+            assertTrue( true );
+        }
+
+        entry.put( "sn", ( String ) null );
+        assertEquals( 1, entry.size() );
+        assertNotNull( "sn", entry.get( "sn" ) );
+        assertEquals( 1, entry.get( "sn" ).size() );
+        assertNull( entry.get( "sn" ).get().getValue() );
+
+        entry.put( "ObjectClass", "top", "person", "top" );
+        assertEquals( 2, entry.size() );
+        assertNotNull( "objectclass", entry.get( "sn" ) );
+        assertEquals( 2, entry.get( "OBJECTCLASS" ).size() );
+        Attribute attribute = entry.get( "objectClass" );
+        assertTrue( attribute.contains( "top" ) );
+        assertTrue( attribute.contains( "person" ) );
+        assertEquals( "objectclass", attribute.getId() );
+        assertEquals( "ObjectClass", attribute.getUpId() );
+    }
+
+
+    /**
+     * Test method for pu( String, Value<?>... )
+     */
+    @Test
+    public void testPutStringValueArray()
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        Value<String> strValueTop = new StringValue( "top" );
+        Value<String> strValuePerson = new StringValue( "person" );
+        Value<String> strValueTop2 = new StringValue( "top" );
+        Value<String> strNullValue = new StringValue( ( String ) null );
+
+        Value<byte[]> binValue1 = new BinaryValue( BYTES1 );
+        Value<byte[]> binValue2 = new BinaryValue( BYTES2 );
+        Value<byte[]> binValue3 = new BinaryValue( BYTES1 );
+        Value<byte[]> binNullValue = new BinaryValue( ( byte[] ) null );
+
+        try
+        {
+            entry.put( ( String ) null, strValueTop );
+            fail();
+        }
+        catch ( IllegalArgumentException iae )
+        {
+            assertTrue( true );
+        }
+
+        try
+        {
+            entry.put( "   ", strValueTop );
+            fail();
+        }
+        catch ( IllegalArgumentException iae )
+        {
+            assertTrue( true );
+        }
+
+        entry.put( "sn", strNullValue );
+        assertEquals( 1, entry.size() );
+        assertNotNull( "sn", entry.get( "sn" ) );
+        assertEquals( 1, entry.get( "sn" ).size() );
+        assertNull( entry.get( "sn" ).get().getValue() );
+
+        entry.clear();
+
+        entry.put( "ObjectClass", strValueTop, strValuePerson, strValueTop2, strNullValue );
+        assertEquals( 1, entry.size() );
+        assertNotNull( "objectclass", entry.get( "objectclass" ) );
+        assertEquals( 3, entry.get( "OBJECTCLASS" ).size() );
+        Attribute attribute = entry.get( "objectClass" );
+        assertTrue( attribute.contains( "top" ) );
+        assertTrue( attribute.contains( "person" ) );
+        assertTrue( attribute.contains( ( String ) null ) );
+        assertEquals( "objectclass", attribute.getId() );
+        assertEquals( "ObjectClass", attribute.getUpId() );
+
+        entry.clear();
+
+        entry.put( "userpassword", strNullValue );
+        assertEquals( 1, entry.size() );
+        assertNotNull( "userpassword", entry.get( "userpassword" ) );
+        assertEquals( 1, entry.get( "userpassword" ).size() );
+        assertNull( entry.get( "userpassword" ).get().getValue() );
+
+        entry.clear();
+
+        entry.put( "userPassword", binValue1, binValue2, binValue3, binNullValue );
+        assertEquals( 1, entry.size() );
+        assertNotNull( "userpassword", entry.get( "userpassword" ) );
+        assertEquals( 3, entry.get( "userpassword" ).size() );
+        attribute = entry.get( "userpassword" );
+        assertTrue( attribute.contains( BYTES1 ) );
+        assertTrue( attribute.contains( BYTES2 ) );
+        assertTrue( attribute.contains( ( byte[] ) null ) );
+        assertEquals( "userpassword", attribute.getId() );
+        assertEquals( "userPassword", attribute.getUpId() );
+    }
+
+
+    /**
+     * Test method for removeAttributes( String... )
+     */
+    @Test
+    public void testRemoveAttributesStringArray() throws LdapException
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        Attribute attrOC = new DefaultAttribute( "objectClass", "top", "person" );
+        Attribute attrCN = new DefaultAttribute( "cn", "test1", "test2" );
+        Attribute attrSN = new DefaultAttribute( "sn", "Test1", "Test2" );
+        Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, BYTES2 );
+
+        entry.put( attrOC, attrCN, attrSN, attrPWD );
+
+        entry.removeAttributes( "CN", "SN" );
+
+        assertFalse( entry.containsAttribute( "cn", "sn" ) );
+        assertTrue( entry.containsAttribute( "objectclass", "userpassword" ) );
+
+        entry.removeAttributes( "badId" );
+
+        entry.removeAttributes( ( String ) null );
+    }
+
+
+    /**
+     * Test method for remove( EntryAttribute... )
+     */
+    @Test
+    public void testRemoveEntryAttributeArray() throws LdapException
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        Attribute attrOC = new DefaultAttribute( "objectClass", "top", "person" );
+        Attribute attrCN = new DefaultAttribute( "cn", "test1", "test2" );
+        Attribute attrSN = new DefaultAttribute( "sn", "Test1", "Test2" );
+        Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, BYTES2 );
+
+        entry.put( attrOC, attrCN, attrSN, attrPWD );
+
+        List<Attribute> removed = entry.remove( attrSN, attrPWD );
+
+        assertEquals( 2, removed.size() );
+        assertEquals( 2, entry.size() );
+        assertTrue( removed.contains( attrSN ) );
+        assertTrue( removed.contains( attrPWD ) );
+        assertTrue( entry.contains( "objectClass", "top", "person" ) );
+        assertTrue( entry.contains( "cn", "test1", "test2" ) );
+        assertFalse( entry.containsAttribute( "sn" ) );
+        assertFalse( entry.containsAttribute( "userPassword" ) );
+
+        removed = entry.remove( attrSN, attrPWD );
+
+        assertEquals( 0, removed.size() );
+    }
+
+
+    /**
+     * Test method for remove(String, byte[]... )
+     */
+    @Test
+    public void testRemoveStringByteArrayArray() throws LdapException
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, ( byte[] ) null, BYTES2 );
+
+        entry.put( attrPWD );
+        assertTrue( entry.remove( "userPassword", ( byte[] ) null ) );
+        assertTrue( entry.remove( "userPassword", BYTES1, BYTES2 ) );
+        assertFalse( entry.containsAttribute( "userPassword" ) );
+
+        entry.add( "userPassword", BYTES1, ( byte[] ) null, BYTES2 );
+        assertTrue( entry.remove( "userPassword", ( byte[] ) null ) );
+        assertEquals( 2, entry.get( "userPassword" ).size() );
+        assertTrue( entry.remove( "userPassword", BYTES1, BYTES3 ) );
+        assertEquals( 1, entry.get( "userPassword" ).size() );
+        assertTrue( Arrays.equals( BYTES2, entry.get( "userPassword" ).getBytes() ) );
+
+        assertFalse( entry.remove( "userPassword", BYTES3 ) );
+        assertFalse( entry.remove( "void", "whatever" ) );
+    }
+
+
+    /**
+     * Test method for remove( String, String... )
+     */
+    @Test
+    public void testRemoveStringStringArray() throws LdapException
+    {
+        Entry entry = createEntry();
+
+        assertTrue( entry.remove( "cn", "test1" ) );
+        assertTrue( entry.remove( "cn", "test2" ) );
+        assertFalse( entry.containsAttribute( "cn" ) );
+
+        entry.add( "cn", "test1", ( String ) null, "test2" );
+        assertTrue( entry.remove( "cn", ( String ) null ) );
+        assertEquals( 2, entry.get( "cn" ).size() );
+        assertTrue( entry.remove( "cn", "test1", "test3" ) );
+        assertEquals( 1, entry.get( "cn" ).size() );
+        assertEquals( "test2", entry.get( "cn" ).get().getString() );
+
+        assertFalse( entry.remove( "cn", "test3" ) );
+        assertFalse( entry.remove( "void", "whatever" ) );
+    }
+
+
+    /**
+     * Test method for remove(String, Value<?>... )
+     */
+    @Test
+    public void testRemoveStringValueArray() throws LdapException
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        Attribute attrCN = new DefaultAttribute( "cn", "test1", "test2", ( String ) null );
+        Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, BYTES2, ( byte[] ) null );
+
+        entry.add( attrCN, attrPWD );
+
+        Value<String> strValue1 = new StringValue( "test1" );
+        Value<String> strValue2 = new StringValue( "test2" );
+        Value<String> strValue3 = new StringValue( "test3" );
+        Value<String> strNullValue = new StringValue( ( String ) null );
+
+        Value<byte[]> binValue1 = new BinaryValue( BYTES1 );
+        Value<byte[]> binValue2 = new BinaryValue( BYTES2 );
+        Value<byte[]> binValue3 = new BinaryValue( BYTES3 );
+        Value<byte[]> binNullValue = new BinaryValue( ( byte[] ) null );
+
+        assertTrue( entry.remove( "cn", strValue1, strNullValue ) );
+        assertTrue( entry.contains( "cn", strValue2 ) );
+        assertFalse( entry.remove( "cn", strValue3 ) );
+        assertTrue( entry.remove( "cn", strValue2 ) );
+        assertFalse( entry.containsAttribute( "cn" ) );
+
+        entry.add( attrCN, attrPWD );
+
+        assertTrue( entry.remove( "userpassword", binValue1, binNullValue ) );
+        assertTrue( entry.contains( "userpassword", binValue2 ) );
+        assertFalse( entry.remove( "userpassword", binValue3 ) );
+        assertTrue( entry.remove( "userpassword", binValue2 ) );
+        assertFalse( entry.containsAttribute( "userpassword" ) );
+    }
+
+
+    /**
+     * Test method for setDN( Dn )
+     */
+    @Test
+    public void testSetDn()
+    {
+        Entry entry = new DefaultEntry();
+
+        assertEquals( Dn.EMPTY_DN, entry.getDn() );
+
+        entry.setDn( EXAMPLE_DN );
+        assertEquals( EXAMPLE_DN, entry.getDn() );
+    }
+
+
+    /**
+     * Test method for size()
+     */
+    @Test
+    public void testSize() throws LdapException
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        assertEquals( 0, entry.size() );
+        entry.add( "ObjectClass", "top", "person" );
+        entry.add( "cn", "test" );
+        entry.add( "sn", "Test" );
+
+        assertEquals( 3, entry.size() );
+
+        entry.clear();
+        assertEquals( 0, entry.size() );
+    }
+
+
+    /**
+     * Test method for for {@link org.apache.directory.api.ldap.model.entry.DefaultEntry#toString()}.
+     */
+    @Test
+    public void testToString()
+    {
+        Entry entry = new DefaultEntry( EXAMPLE_DN );
+
+        assertEquals( "Entry\n    dn: dc=example,dc=com\n", entry.toString() );
+
+        Value<String> strValueTop = new StringValue( "top" );
+        Value<String> strValuePerson = new StringValue( "person" );
+        Value<String> strNullValue = new StringValue( ( String ) null );
+
+        Value<byte[]> binValue1 = new BinaryValue( BYTES1 );
+        Value<byte[]> binValue2 = new BinaryValue( BYTES2 );
+        Value<byte[]> binNullValue = new BinaryValue( ( byte[] ) null );
+
+        entry.put( "ObjectClass", strValueTop, strValuePerson, strNullValue );
+        entry.put( "UserPassword", binValue1, binValue2, binNullValue );
+
+        String expected =
+            "Entry\n" +
+                "    dn: dc=example,dc=com\n" +
+                "    ObjectClass: top\n" +
+                "    ObjectClass: person\n" +
+                "    ObjectClass: ''\n" +
+                "    UserPassword: '0x61 0x62 '\n" +
+                "    UserPassword: '0x62 '\n" +
+                "    UserPassword: ''\n";
+
+        assertEquals( expected, entry.toString() );
+    }
+
+
+    /**
+     * Test the serialization of a complete entry
+     */
+    @Test
+    public void testSerializeCompleteEntry() throws LdapException, IOException, ClassNotFoundException
+    {
+        Dn dn = new Dn( "ou=system" );
+
+        dn.apply( schemaManager );
+
+        byte[] password = Strings.getBytesUtf8( "secret" );
+        Entry entry = new DefaultEntry( dn );
+        entry.add( "ObjectClass", "top", "person" );
+        entry.add( "cn", "test1" );
+        entry.add( "userPassword", password );
+
+        Entry entrySer = deserializeValue( serializeValue( entry ) );
+
+        assertEquals( entry, entrySer );
+    }
+
+
+    /**
+     * Test the serialization of an entry with no Dn
+     */
+    @Test
+    public void testSerializeEntryWithNoDN() throws LdapException, IOException, ClassNotFoundException
+    {
+        byte[] password = Strings.getBytesUtf8( "secret" );
+        Entry entry = new DefaultEntry();
+        entry.add( "ObjectClass", "top", "person" );
+        entry.add( "cn", "test1" );
+        entry.add( "userPassword", password );
+
+        Entry entrySer = deserializeValue( serializeValue( entry ) );
+
+        assertEquals( entry, entrySer );
+    }
+
+
+    /**
+     * Test the serialization of an entry with no attribute and no Dn
+     */
+    @Test
+    public void testSerializeEntryWithNoDNNoAttribute() throws LdapException, IOException, ClassNotFoundException
+    {
+        Entry entry = new DefaultEntry();
+
+        Entry entrySer = deserializeValue( serializeValue( entry ) );
+
+        assertEquals( entry, entrySer );
+    }
+
+
+    /**
+     * Test the serialization of an entry with no attribute
+     */
+    @Test
+    public void testSerializeEntryWithNoAttribute() throws LdapException, IOException, ClassNotFoundException
+    {
+        Dn dn = new Dn( "ou=system" );
+
+        dn.apply( schemaManager );
+
+        Entry entry = new DefaultEntry( dn );
+
+        Entry entrySer = deserializeValue( serializeValue( entry ) );
+
+        assertEquals( entry, entrySer );
+    }
+
+
+    /**
+     * Test method for userCertificate;binary AT
+     */
+    @Test
+    public void testUserCertificateBinary() throws LdapException
+    {
+        Entry entry = new DefaultEntry( schemaManager );
+        entry.add( "objectClass", "top", "person", "inetorgPerson" );
+        entry.add( "cn", "test1", "test2" );
+        entry.add( "sn", "Test1", "Test2" );
+        entry.add( "userPassword", BYTES1, BYTES2 );
+
+        entry.add( "userCertificate;binary", Strings.getBytesUtf8( "secret" ) );
+        assertTrue( entry.containsAttribute( "userCertificate;binary" ) );
+        assertTrue( entry.containsAttribute( "userCertificate" ) );
+
+        entry.removeAttributes( "userCertificate;binary" );
+        assertFalse( entry.containsAttribute( "userCertificate;binary" ) );
+        assertFalse( entry.containsAttribute( "userCertificate" ) );
+
+        entry.add( "userCertificate", Strings.getBytesUtf8( "secret" ) );
+        assertTrue( entry.containsAttribute( "userCertificate;binary" ) );
+        assertTrue( entry.containsAttribute( "userCertificate" ) );
+    }
+}

Propchange: directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/SchemaAwareEntryTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/SchemaAwareModificationSerializationTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/SchemaAwareModificationSerializationTest.java?rev=1423968&view=auto
==============================================================================
--- directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/SchemaAwareModificationSerializationTest.java (added)
+++ directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/SchemaAwareModificationSerializationTest.java Wed Dec 19 17:24:08 2012
@@ -0,0 +1,238 @@
+/*
+ *  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.entry;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+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.entry.Attribute;
+import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
+import org.apache.directory.api.ldap.model.entry.DefaultModification;
+import org.apache.directory.api.ldap.model.entry.Modification;
+import org.apache.directory.api.ldap.model.entry.ModificationOperation;
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
+import org.apache.directory.api.ldap.model.schema.AttributeType;
+import org.apache.directory.api.ldap.model.schema.SchemaManager;
+import org.apache.directory.shared.ldap.schemamanager.impl.DefaultSchemaManager;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import com.mycila.junit.concurrent.Concurrency;
+import com.mycila.junit.concurrent.ConcurrentJunitRunner;
+
+
+/**
+ * Test the DefaultModification class
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(ConcurrentJunitRunner.class)
+@Concurrency()
+public class SchemaAwareModificationSerializationTest
+{
+    private static SchemaManager schemaManager;
+    private static AttributeType CN_AT;
+
+
+    /**
+     * Initialize OIDs maps for normalization
+     */
+    @BeforeClass
+    public static void setup() throws Exception
+    {
+        schemaManager = new DefaultSchemaManager();
+        CN_AT = schemaManager.getAttributeType( "cn" );
+    }
+
+
+    /**
+     * Serialize a DefaultModification
+     */
+    private ByteArrayOutputStream serializeValue( Modification modification ) throws IOException
+    {
+        ObjectOutputStream oOut = null;
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+        try
+        {
+            oOut = new ObjectOutputStream( out );
+
+            modification.writeExternal( oOut );
+        }
+        catch ( IOException ioe )
+        {
+            throw ioe;
+        }
+        finally
+        {
+            try
+            {
+                if ( oOut != null )
+                {
+                    oOut.flush();
+                    oOut.close();
+                }
+            }
+            catch ( IOException ioe )
+            {
+                throw ioe;
+            }
+        }
+
+        return out;
+    }
+
+
+    /**
+     * Deserialize a DefaultModification
+     */
+    private Modification deserializeValue( ByteArrayOutputStream out ) throws IOException, ClassNotFoundException,
+        LdapInvalidAttributeValueException
+    {
+        ObjectInputStream oIn = null;
+        ByteArrayInputStream in = new ByteArrayInputStream( out.toByteArray() );
+
+        try
+        {
+            oIn = new ObjectInputStream( in );
+            Modification modification = new DefaultModification();
+            modification.readExternal( oIn );
+
+            Attribute attribute = modification.getAttribute();
+
+            if ( ( attribute != null ) && ( schemaManager != null ) )
+            {
+                AttributeType attributeType = schemaManager.getAttributeType( attribute.getId() );
+
+                modification.apply( attributeType );
+            }
+
+            return modification;
+        }
+        catch ( IOException ioe )
+        {
+            throw ioe;
+        }
+        finally
+        {
+            try
+            {
+                if ( oIn != null )
+                {
+                    oIn.close();
+                }
+            }
+            catch ( IOException ioe )
+            {
+                throw ioe;
+            }
+        }
+    }
+
+
+    @Test
+    public void testCreateServerModification() throws LdapException
+    {
+        Attribute attribute = new DefaultAttribute( "cn", CN_AT );
+        attribute.add( "test1", "test2" );
+
+        Modification mod = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attribute );
+        Modification clone = mod.clone();
+
+        attribute.remove( "test2" );
+
+        Attribute clonedAttribute = clone.getAttribute();
+
+        assertEquals( 1, mod.getAttribute().size() );
+        assertTrue( mod.getAttribute().contains( "TEST1" ) );
+
+        assertEquals( 2, clonedAttribute.size() );
+        assertTrue( clone.getAttribute().contains( "test1" ) );
+        assertTrue( clone.getAttribute().contains( "test2" ) );
+    }
+
+
+    @Test
+    public void testSerializationModificationADD() throws ClassNotFoundException, IOException,
+        LdapInvalidAttributeValueException
+    {
+        Attribute attribute = new DefaultAttribute( "cn", CN_AT );
+        attribute.add( "test1", "test2" );
+
+        DefaultModification mod = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attribute );
+
+        Modification modSer = deserializeValue( serializeValue( mod ) );
+
+        assertEquals( mod, modSer );
+    }
+
+
+    @Test
+    public void testSerializationModificationREPLACE() throws ClassNotFoundException, IOException,
+        LdapInvalidAttributeValueException
+    {
+        Attribute attribute = new DefaultAttribute( "cn", CN_AT );
+        attribute.add( "test1", "test2" );
+
+        DefaultModification mod = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, attribute );
+
+        Modification modSer = deserializeValue( serializeValue( mod ) );
+
+        assertEquals( mod, modSer );
+    }
+
+
+    @Test
+    public void testSerializationModificationREMOVE() throws ClassNotFoundException, IOException,
+        LdapInvalidAttributeValueException
+    {
+        Attribute attribute = new DefaultAttribute( "cn", CN_AT );
+        attribute.add( "test1", "test2" );
+
+        DefaultModification mod = new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, attribute );
+
+        Modification modSer = deserializeValue( serializeValue( mod ) );
+
+        assertEquals( mod, modSer );
+    }
+
+
+    @Test
+    public void testSerializationModificationNoAttribute() throws ClassNotFoundException, IOException,
+        LdapInvalidAttributeValueException
+    {
+        DefaultModification mod = new DefaultModification();
+
+        mod.setOperation( ModificationOperation.ADD_ATTRIBUTE );
+
+        Modification modSer = deserializeValue( serializeValue( mod ) );
+
+        assertEquals( mod, modSer );
+    }
+}

Propchange: directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/SchemaAwareModificationSerializationTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/SchemaAwareValueSerializationTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/SchemaAwareValueSerializationTest.java?rev=1423968&view=auto
==============================================================================
--- directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/SchemaAwareValueSerializationTest.java (added)
+++ directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/SchemaAwareValueSerializationTest.java Wed Dec 19 17:24:08 2012
@@ -0,0 +1,340 @@
+/*
+ *  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.entry;
+
+
+import static org.junit.Assert.assertEquals;
+
+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.entry.BinaryValue;
+import org.apache.directory.api.ldap.model.entry.StringValue;
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.schema.AttributeType;
+import org.apache.directory.api.ldap.model.schema.SchemaManager;
+import org.apache.directory.api.util.StringConstants;
+import org.apache.directory.shared.ldap.schemamanager.impl.DefaultSchemaManager;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import com.mycila.junit.concurrent.Concurrency;
+import com.mycila.junit.concurrent.ConcurrentJunitRunner;
+
+
+/**
+ * Test the Value Serialization
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(ConcurrentJunitRunner.class)
+@Concurrency()
+public class SchemaAwareValueSerializationTest
+{
+    private static byte[] data = new byte[]
+        { 0x01, 0x02, 0x03, 0x04 };
+    private static BinaryValue bv1;
+    private static BinaryValue bv2;
+    private static BinaryValue bv3;
+    private static BinaryValue bv1n;
+    private static BinaryValue bv2n;
+    private static BinaryValue bv3n;
+    private static StringValue sv1;
+    private static StringValue sv2;
+    private static StringValue sv3;
+    private static StringValue sv1n;
+    private static StringValue sv2n;
+    private static StringValue sv3n;
+
+    private static SchemaManager schemaManager;
+    private static AttributeType cn = null;
+    private static AttributeType dc = null;
+    private static AttributeType userCertificate = null;
+
+
+    /**
+     * Initialize OIDs maps for normalization
+     */
+    @BeforeClass
+    public static void setup() throws Exception
+    {
+        schemaManager = new DefaultSchemaManager();
+        cn = schemaManager.getAttributeType( "cn" );
+        dc = schemaManager.getAttributeType( "dc" );
+        userCertificate = schemaManager.getAttributeType( "userCertificate" );
+
+        bv1 = new BinaryValue( userCertificate, data );
+        bv2 = new BinaryValue( userCertificate, StringConstants.EMPTY_BYTES );
+        bv3 = new BinaryValue( userCertificate, null );
+        bv1n = new BinaryValue( userCertificate, data );
+        bv2n = new BinaryValue( userCertificate, StringConstants.EMPTY_BYTES );
+        bv3n = new BinaryValue( userCertificate, null );
+        sv1 = new StringValue( cn, "test" );
+        sv2 = new StringValue( dc, "" );
+        sv3 = new StringValue( dc, ( String ) null );
+        sv1n = new StringValue( cn, "test" );
+        sv2n = new StringValue( dc, "" );
+        sv3n = new StringValue( dc, ( String ) null );
+    }
+
+
+    @Test
+    public void testBinaryValueWithDataSerialization() throws IOException, ClassNotFoundException
+    {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        bv1.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        BinaryValue bvDeser = BinaryValue.deserialize( in );
+
+        assertEquals( bv1, bvDeser );
+    }
+
+
+    @Test
+    public void testBinaryValueWithEmptyDataSerialization() throws IOException, ClassNotFoundException
+    {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        bv2.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        BinaryValue bvDeser = BinaryValue.deserialize( in );
+
+        assertEquals( bv2, bvDeser );
+    }
+
+
+    @Test
+    public void testBinaryValueNoDataSerialization() throws IOException, ClassNotFoundException
+    {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        bv3.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        BinaryValue bvDeser = BinaryValue.deserialize( in );
+
+        assertEquals( bv3, bvDeser );
+    }
+
+
+    @Test
+    public void testStringValueWithDataSerialization() throws IOException, ClassNotFoundException
+    {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        sv1.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        StringValue svDeser = StringValue.deserialize( in );
+
+        assertEquals( sv1, svDeser );
+    }
+
+
+    @Test
+    public void testStringValueWithEmptyDataSerialization() throws IOException, ClassNotFoundException
+    {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        sv2.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        StringValue svDeser = StringValue.deserialize( in );
+
+        assertEquals( sv2, svDeser );
+    }
+
+
+    @Test
+    public void testStringValueNoDataSerialization() throws IOException, ClassNotFoundException
+    {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+
+        sv3.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        StringValue svDeser = StringValue.deserialize( in );
+
+        assertEquals( sv3, svDeser );
+    }
+
+
+    @Test
+    public void testBinaryValueWithDataNormalizedSerialization() throws IOException, LdapException,
+        ClassNotFoundException
+    {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+        BinaryValue value = new BinaryValue( userCertificate, bv1n.getBytes() );
+
+        value.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        BinaryValue bvDeser = BinaryValue.deserialize( userCertificate, in );
+
+        assertEquals( value, bvDeser );
+    }
+
+
+    @Test
+    public void testBinaryValueWithEmptyDataNormalizedSerialization() throws IOException, LdapException,
+        ClassNotFoundException
+    {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+        BinaryValue value = new BinaryValue( userCertificate, bv2n.getBytes() );
+
+        value.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        BinaryValue bvDeser = BinaryValue.deserialize( userCertificate, in );
+
+        assertEquals( value, bvDeser );
+    }
+
+
+    @Test
+    public void testBinaryValueNoDataNormalizedSerialization() throws IOException, LdapException,
+        ClassNotFoundException
+    {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+        BinaryValue value = new BinaryValue( userCertificate, bv3n.getBytes() );
+
+        value.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        BinaryValue bvDeser = BinaryValue.deserialize( userCertificate, in );
+
+        assertEquals( value, bvDeser );
+    }
+
+
+    @Test
+    public void testStringValueWithDataNormalizedSerialization() throws IOException, LdapException,
+        ClassNotFoundException
+    {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+        StringValue value = new StringValue( cn, sv1n.getString() );
+
+        value.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        StringValue svDeser = StringValue.deserialize( cn, in );
+
+        assertEquals( value, svDeser );
+    }
+
+
+    @Test
+    public void testStringValueWithEmptyDataNormalizedSerialization() throws IOException, LdapException,
+        ClassNotFoundException
+    {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+        StringValue value = new StringValue( dc, sv2n.getString() );
+
+        value.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        StringValue svDeser = StringValue.deserialize( dc, in );
+
+        assertEquals( value, svDeser );
+    }
+
+
+    @Test
+    public void testStringValueNoDataNormalizedSerialization() throws IOException, LdapException,
+        ClassNotFoundException
+    {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream( baos );
+        StringValue value = new StringValue( dc, sv3n.getString() );
+
+        value.writeExternal( out );
+
+        ObjectInputStream in = null;
+
+        byte[] data = baos.toByteArray();
+        in = new ObjectInputStream( new ByteArrayInputStream( data ) );
+
+        StringValue svDeser = StringValue.deserialize( dc, in );
+
+        assertEquals( value, svDeser );
+    }
+}

Propchange: directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/SchemaAwareValueSerializationTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/TestEntryUtils.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/TestEntryUtils.java?rev=1423968&view=auto
==============================================================================
--- directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/TestEntryUtils.java (added)
+++ directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/TestEntryUtils.java Wed Dec 19 17:24:08 2012
@@ -0,0 +1,266 @@
+/*
+ *  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.entry;
+
+
+import org.apache.directory.api.ldap.model.entry.BinaryValue;
+import org.apache.directory.api.ldap.model.entry.StringValue;
+import org.apache.directory.api.ldap.model.entry.Value;
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.schema.AttributeType;
+import org.apache.directory.api.ldap.model.schema.LdapComparator;
+import org.apache.directory.api.ldap.model.schema.LdapSyntax;
+import org.apache.directory.api.ldap.model.schema.MatchingRule;
+import org.apache.directory.api.ldap.model.schema.MutableAttributeType;
+import org.apache.directory.api.ldap.model.schema.MutableMatchingRule;
+import org.apache.directory.api.ldap.model.schema.Normalizer;
+import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
+import org.apache.directory.api.ldap.model.schema.comparators.ByteArrayComparator;
+import org.apache.directory.api.ldap.model.schema.normalizers.DeepTrimToLowerNormalizer;
+import org.apache.directory.api.util.Strings;
+
+
+/**
+ * Some common declaration used by the serverEntry tests.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class TestEntryUtils
+{
+    /**
+     * A local Syntax class for tests
+     */
+    static class AT extends AttributeType
+    {
+        private static final long serialVersionUID = 0L;
+
+
+        protected AT( String oid )
+        {
+            super( oid );
+        }
+    }
+
+
+    public static MatchingRule matchingRuleFactory( String oid )
+    {
+        MatchingRule matchingRule = new MatchingRule( oid );
+
+        return matchingRule;
+    }
+
+    /**
+     * A local MatchingRule class for tests
+     */
+    static class MR extends MatchingRule
+    {
+        protected MR( String oid )
+        {
+            super( oid );
+        }
+    }
+
+
+    /**
+     * A local Syntax class used for the tests
+     */
+    public static LdapSyntax syntaxFactory( String oid, boolean humanReadable )
+    {
+        LdapSyntax ldapSyntax = new LdapSyntax( oid );
+
+        ldapSyntax.setHumanReadable( humanReadable );
+
+        return ldapSyntax;
+    }
+
+    static class S extends LdapSyntax
+    {
+        private static final long serialVersionUID = 0L;
+
+
+        public S( String oid, boolean humanReadible )
+        {
+            super( oid, "", humanReadible );
+        }
+    }
+
+
+    /* no protection*/
+    //This will suppress PMD.AvoidUsingHardCodedIP warnings in this class
+    @SuppressWarnings("PMD.AvoidUsingHardCodedIP")
+    static AttributeType getCaseIgnoringAttributeNoNumbersType()
+    {
+        MutableAttributeType attributeType = new MutableAttributeType( "1.1.3.1" );
+        LdapSyntax syntax = new LdapSyntax( "1.1.1.1", "", true );
+
+        syntax.setSyntaxChecker( new SyntaxChecker( "1.1.2.1" )
+        {
+            public boolean isValidSyntax( Object value )
+            {
+                if ( !( value instanceof String ) )
+                {
+                    return false;
+                }
+
+                String strval = ( String ) value;
+
+                for ( char c : strval.toCharArray() )
+                {
+                    if ( Character.isDigit( c ) )
+                    {
+                        return false;
+                    }
+                }
+                return true;
+            }
+        } );
+
+        MutableMatchingRule matchingRule = new MutableMatchingRule( "1.1.2.1" );
+        matchingRule.setSyntax( syntax );
+
+        matchingRule.setLdapComparator( new LdapComparator<String>( matchingRule.getOid() )
+        {
+            public int compare( String o1, String o2 )
+            {
+                return ( o1 == null ?
+                    ( o2 == null ? 0 : -1 ) :
+                    ( o2 == null ? 1 : o1.compareTo( o2 ) ) );
+            }
+        } );
+
+        Normalizer normalizer = new Normalizer( "1.1.1" )
+        {
+            public Value<?> normalize( Value<?> value ) throws LdapException
+            {
+                if ( value.isHumanReadable() )
+                {
+                    return new StringValue( Strings.toLowerCase( value.getString() ) );
+                }
+
+                throw new IllegalStateException();
+            }
+
+
+            public String normalize( String value ) throws LdapException
+            {
+                return Strings.toLowerCase( value );
+            }
+        };
+
+        matchingRule.setNormalizer( normalizer );
+
+        attributeType.setEquality( matchingRule );
+        attributeType.setSyntax( syntax );
+
+        return attributeType;
+    }
+
+
+    /* no protection*/static AttributeType getIA5StringAttributeType()
+    {
+        MutableAttributeType attributeType = new MutableAttributeType( "1.1" );
+        attributeType.addName( "1.1" );
+        LdapSyntax syntax = new LdapSyntax( "1.1.1", "", true );
+
+        syntax.setSyntaxChecker( new SyntaxChecker( "1.1.2" )
+        {
+            public boolean isValidSyntax( Object value )
+            {
+                return ( ( String ) value == null ) || ( ( ( String ) value ).length() < 7 );
+            }
+        } );
+
+        MutableMatchingRule matchingRule = new MutableMatchingRule( "1.1.2" );
+        matchingRule.setSyntax( syntax );
+
+        matchingRule.setLdapComparator( new LdapComparator<String>( matchingRule.getOid() )
+        {
+            public int compare( String o1, String o2 )
+            {
+                return ( ( o1 == null ) ?
+                    ( o2 == null ? 0 : -1 ) :
+                    ( o2 == null ? 1 : o1.compareTo( o2 ) ) );
+            }
+        } );
+
+        matchingRule.setNormalizer( new DeepTrimToLowerNormalizer( matchingRule.getOid() ) );
+
+        attributeType.setEquality( matchingRule );
+        attributeType.setSyntax( syntax );
+
+        return attributeType;
+    }
+
+
+    /* No protection */static AttributeType getBytesAttributeType()
+    {
+        MutableAttributeType attributeType = new MutableAttributeType( "1.2" );
+        LdapSyntax syntax = new LdapSyntax( "1.2.1", "", true );
+
+        syntax.setSyntaxChecker( new SyntaxChecker( "1.2.1" )
+        {
+            public boolean isValidSyntax( Object value )
+            {
+                return ( value == null ) || ( ( ( byte[] ) value ).length < 5 );
+            }
+        } );
+
+        MutableMatchingRule matchingRule = new MutableMatchingRule( "1.2.2" );
+        matchingRule.setSyntax( syntax );
+
+        matchingRule.setLdapComparator( new ByteArrayComparator( "1.2.2" ) );
+
+        matchingRule.setNormalizer( new Normalizer( "1.1.1" )
+        {
+            public Value<?> normalize( Value<?> value ) throws LdapException
+            {
+                if ( !value.isHumanReadable() )
+                {
+                    byte[] val = value.getBytes();
+
+                    // each byte will be changed to be > 0, and spaces will be trimmed
+                    byte[] newVal = new byte[val.length];
+
+                    int i = 0;
+
+                    for ( byte b : val )
+                    {
+                        newVal[i++] = ( byte ) ( b & 0x007F );
+                    }
+
+                    return new BinaryValue( Strings.trim( newVal ) );
+                }
+
+                throw new IllegalStateException();
+            }
+
+
+            public String normalize( String value ) throws LdapException
+            {
+                throw new IllegalStateException();
+            }
+        } );
+
+        attributeType.setEquality( matchingRule );
+        attributeType.setSyntax( syntax );
+
+        return attributeType;
+    }
+}

Propchange: directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/entry/TestEntryUtils.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/model/name/AvaTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/model/name/AvaTest.java?rev=1423968&view=auto
==============================================================================
--- directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/model/name/AvaTest.java (added)
+++ directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/model/name/AvaTest.java Wed Dec 19 17:24:08 2012
@@ -0,0 +1,406 @@
+/*
+ *  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.util.Arrays;
+
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
+import org.apache.directory.api.ldap.model.name.Ava;
+import org.apache.directory.api.ldap.model.schema.SchemaManager;
+import org.apache.directory.shared.ldap.schemamanager.impl.DefaultSchemaManager;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import com.mycila.junit.concurrent.Concurrency;
+import com.mycila.junit.concurrent.ConcurrentJunitRunner;
+
+
+/**
+ * Test the class AttributeTypeAndValue
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(ConcurrentJunitRunner.class)
+@Concurrency()
+public class AvaTest
+{
+    private static SchemaManager schemaManager;
+
+
+    @BeforeClass
+    public static void setup() throws Exception
+    {
+        schemaManager = new DefaultSchemaManager();
+    }
+
+
+    /**
+     * Test a null AttributeTypeAndValue
+     */
+    @Test
+    public void testAttributeTypeAndValueNull()
+    {
+        Ava atav = new Ava( schemaManager );
+        assertEquals( "", atav.toString() );
+        assertEquals( "", atav.getName() );
+    }
+
+
+    /**
+     * Test a null type for an AttributeTypeAndValue
+     */
+    @Test
+    public void testAttributeTypeAndValueNullType() throws LdapException
+    {
+        try
+        {
+            new Ava( schemaManager, null, ( String ) null );
+            fail();
+        }
+        catch ( LdapException ine )
+        {
+            assertTrue( true );
+        }
+
+    }
+
+
+    /**
+     * Test an invalid type for an AttributeTypeAndValue
+     */
+    @Test
+    public void testAttributeTypeAndValueInvalidType() throws LdapException
+    {
+        try
+        {
+            new Ava( schemaManager, "  ", ( String ) null );
+            fail();
+        }
+        catch ( LdapException ine )
+        {
+            assertTrue( true );
+        }
+    }
+
+
+    /**
+     * Test a valid type for an AttributeTypeAndValue
+     */
+    @Test
+    public void testAttributeTypeAndValueValidType() throws LdapException
+    {
+        Ava ava = new Ava( schemaManager, "CN", " " );
+        assertEquals( "CN=\\ ", ava.toString() );
+        assertEquals( "2.5.4.3=\\ ", ava.getNormName() );
+        assertEquals( "CN=\\ ", ava.getName() );
+
+        ava = new Ava( schemaManager, "  CN  ", " " );
+        assertEquals( "  CN  =\\ ", ava.toString() );
+        assertEquals( "2.5.4.3=\\ ", ava.getNormName() );
+        assertEquals( "  CN  =\\ ", ava.getName() );
+
+        ava = new Ava( schemaManager, "cn", " " );
+        assertEquals( "cn=\\ ", ava.toString() );
+        assertEquals( "2.5.4.3=\\ ", ava.getNormName() );
+        assertEquals( "cn=\\ ", ava.getName() );
+
+        ava = new Ava( schemaManager, "  cn  ", " " );
+        assertEquals( "  cn  =\\ ", ava.toString() );
+        assertEquals( "2.5.4.3=\\ ", ava.getNormName() );
+        assertEquals( "  cn  =\\ ", ava.getName() );
+    }
+
+
+    /**
+     * test an empty AttributeTypeAndValue
+     */
+    @Test
+    public void testAvaEmpty()
+    {
+        try
+        {
+            new Ava( schemaManager, "", "" );
+            fail( "Should not occurs ... " );
+        }
+        catch ( LdapException ine )
+        {
+            assertTrue( true );
+        }
+    }
+
+
+    /**
+     * test a simple AttributeTypeAndValue : a = b
+     */
+    @Test
+    public void testAvaSimple() throws LdapException
+    {
+        Ava atav = new Ava( schemaManager, "cn", "b" );
+        assertEquals( "cn=b", atav.toString() );
+        assertEquals( "2.5.4.3=b", atav.getNormName() );
+        assertEquals( "cn=b", atav.getName() );
+    }
+
+
+    /**
+     * test a simple AttributeTypeAndValue : a = b
+     */
+    @Test
+    public void testAvaSimpleNorm() throws LdapException
+    {
+        Ava atav = new Ava( schemaManager, " CommonName ", " This is    a TEST " );
+        assertEquals( " CommonName =\\ This is    a TEST\\ ", atav.toString() );
+        assertEquals( "2.5.4.3=this is a test", atav.getNormName() );
+        assertEquals( " CommonName =\\ This is    a TEST\\ ", atav.getName() );
+    }
+
+
+    /**
+     * Compares two equals atavs
+     */
+    @Test
+    public void testEqualsSameAva() throws LdapException
+    {
+        Ava atav1 = new Ava( schemaManager, "cn", "b" );
+        Ava atav2 = new Ava( schemaManager, "cn", "b" );
+
+        assertTrue( atav1.equals( atav2 ) );
+    }
+
+
+    /**
+     * Compares two equals atavs but with a type in different case
+     */
+    @Test
+    public void testEqualsUpperCaseAT() throws LdapException
+    {
+        Ava atav1 = new Ava( schemaManager, "cn", "b" );
+        Ava atav2 = new Ava( schemaManager, "CN", "b" );
+
+        assertTrue( atav1.equals( atav2 ) );
+    }
+
+
+    /**
+     * Compares two equals atavs but with a type in different case
+     */
+    @Test
+    public void testEqualsSameValues() throws LdapException
+    {
+        Ava atav1 = new Ava( schemaManager, "cn", "  B  a" );
+        Ava atav2 = new Ava( schemaManager, "CN", "b a" );
+
+        assertTrue( atav1.equals( atav2 ) );
+    }
+    
+    
+    /**
+     * Test the returned values for Ava. \u00E4 is the unicode char for "ä", encoded
+     * \C3\A4 in UTF8
+     */
+    @Test
+    public void testAvaValuesNoSchema() throws LdapException
+    {
+        String errors = null;
+        
+        Ava ava = new Ava( "OU", "Exemple + Rdn\u00E4 " );
+        
+        if ( !"ou=Exemple \\+ Rdn\\C3\\A4\\ ".equals( ava.getNormName() ) )
+        {
+            errors = "\nAva.getNormName fails '" + ava.getNormName() + "'";
+        }
+        
+        if ( !"ou".equals( ava.getNormType() ) )
+        {
+            errors += "\nAva.getNormType fails '" + ava.getNormType() + "'";
+        }
+        
+        if ( !"Exemple + Rdn\u00E4 ".equals( ava.getNormValue().getString() ) )
+        {
+            errors += "\nAva.getNormValue fails '" + ava.getNormValue().getString() + "'";
+        }
+        
+        if ( !"OU=Exemple \\+ Rdn\\C3\\A4\\ ".equals( ava.getName() ) )
+        {
+            errors += "\nAva.getUpName fails '" + ava.getName() + "'";
+        }
+        
+        if ( !"OU".equals( ava.getType() ) )
+        {
+            errors += "\nAva.getUpType fails '" + ava.getType() + "'";
+        }
+        
+        if ( !"Exemple + Rdn\u00E4 ".equals( ava.getValue().getString() ) )
+        {
+            errors += "\nAva.getUpValue fails '" + ava.getValue() .getString() + "'";
+        }
+        
+        if ( !"ou=Exemple \\+ Rdn\\C3\\A4\\ ".equals( ava.normalize() ) )
+        {
+            errors += "\nAva.normalize fails '" + ava.normalize() + "'";
+        }
+        
+        if ( !"OU=Exemple \\+ Rdn\\C3\\A4\\ ".equals( ava.toString() ) )
+        {
+            errors += "\nAva.toString fails '" + ava.toString() + "'";
+        }
+        
+        assertEquals( null, errors );
+    }
+    
+    
+    /**
+     * Test the returned values for a schema aware Ava.
+     * \u00E4 is the unicode char for "ä", encoded \C3\A4 in UTF8
+     */
+    @Test
+    public void testAvaValuesSchemaAware() throws LdapException
+    {
+        String errors = null;
+        
+        Ava ava = new Ava( schemaManager, "OU", "Exemple + Rdn\u00E4 " );
+        
+        if ( !"2.5.4.11=exemple \\+ rdn\\C3\\A4".equals( ava.getNormName() ) )
+        {
+            errors = "\nAva.getNormName fails '" + ava.getNormName() + "'";
+        }
+        
+        if ( !"2.5.4.11".equals( ava.getNormType() ) )
+        {
+            errors += "\nAva.getNormType fails '" + ava.getNormType() + "'";
+        }
+        
+        if ( !"exemple + rdn\u00E4".equals( ava.getNormValue().getString() ) )
+        {
+            errors += "\nAva.getNormValue fails '" + ava.getNormValue().getString() + "'";
+        }
+        
+        if ( !"OU=Exemple \\+ Rdn\\C3\\A4\\ ".equals( ava.getName() ) )
+        {
+            errors += "\nAva.getUpName fails '" + ava.getName() + "'";
+        }
+        
+        if ( !"OU".equals( ava.getType() ) )
+        {
+            errors += "\nAva.getUpType fails '" + ava.getType() + "'";
+        }
+        
+        if ( !"Exemple + Rdn\u00E4 ".equals( ava.getValue().getString() ) )
+        {
+            errors += "\nAva.getUpValue fails '" + ava.getValue().getString() + "'";
+        }
+        
+        if ( !"2.5.4.11=exemple \\+ rdn\\C3\\A4".equals( ava.normalize() ) )
+        {
+            errors += "\nAva.normalize fails '" + ava.normalize() + "'";
+        }
+        
+        if ( !"OU=Exemple \\+ Rdn\\C3\\A4\\ ".equals( ava.toString() ) )
+        {
+            errors += "\nAva.toString fails '" + ava.toString() + "'";
+        }
+        
+        assertEquals( null, errors );
+    }
+    
+    
+    @Test
+    public void testCompareToSameAva() throws LdapInvalidDnException
+    {
+        Ava atav1 = new Ava( schemaManager, "cn", "b" );
+        Ava atav2 = new Ava( schemaManager, "cn", "b" );
+        Ava atav3 = new Ava( schemaManager, "commonName", "b" );
+        Ava atav4 = new Ava( schemaManager, "2.5.4.3", "  B  " );
+
+        // 1 with others
+        assertEquals( 0, atav1.compareTo( atav1 ) );
+        assertEquals( 0, atav1.compareTo( atav2 ) );
+        assertEquals( 0, atav1.compareTo( atav3 ) );
+        assertEquals( 0, atav1.compareTo( atav4 ) );
+        
+        // 2 with others
+        assertEquals( 0, atav2.compareTo( atav1 ) );
+        assertEquals( 0, atav2.compareTo( atav2 ) );
+        assertEquals( 0, atav2.compareTo( atav3 ) );
+        assertEquals( 0, atav2.compareTo( atav4 ) );
+        
+        // 3 with others
+        assertEquals( 0, atav3.compareTo( atav1 ) );
+        assertEquals( 0, atav3.compareTo( atav2 ) );
+        assertEquals( 0, atav3.compareTo( atav3 ) );
+        assertEquals( 0, atav3.compareTo( atav4 ) );
+        
+        // 4 with others
+        assertEquals( 0, atav4.compareTo( atav1 ) );
+        assertEquals( 0, atav4.compareTo( atav2 ) );
+        assertEquals( 0, atav4.compareTo( atav3 ) );
+        assertEquals( 0, atav4.compareTo( atav4 ) );
+    }
+    
+    
+    @Test
+    public void testCompareAvaOrder() throws LdapInvalidDnException
+    {
+        Ava atav1 = new Ava( schemaManager, "cn", "  B  " );
+        Ava atav2 = new Ava( schemaManager, "sn", "  c" );
+        
+        // atav1 should be before atav2
+        assertEquals( -1, atav1.compareTo( atav2 ) );
+        assertEquals( 1, atav2.compareTo( atav1 ) );
+
+        Ava atav3 = new Ava( schemaManager, "2.5.4.3", "A " );
+        
+        // Atav1 shoud be after atav3
+        assertEquals( 1, atav1.compareTo( atav3 ) );
+        assertEquals( -1, atav3.compareTo( atav1 ) );
+    }
+    
+    
+    @Test
+    public void testSortAva() throws LdapInvalidDnException
+    {
+        Ava atav1 = new Ava( schemaManager, "cn", "  B  " );
+        Ava atav2 = new Ava( schemaManager, "sn", "  c" );
+        Ava atav3 = new Ava( schemaManager, "2.5.4.3", "A " );
+        Ava atav4 = new Ava( schemaManager, "2.5.4.11", " C  " );
+        Ava atav5 = new Ava( schemaManager, "ou", "B " );
+        Ava atav6 = new Ava( schemaManager, "ou", "D " );
+        Ava atav7 = new Ava( schemaManager, "CN", " " );
+
+        Ava[] avas = new Ava[] { atav1, atav2, atav3, atav4, atav5, atav6, atav7 };
+        
+        Arrays.sort( avas );
+        
+        assertEquals( atav5, avas[0] );
+        assertEquals( atav4, avas[1] );
+        assertEquals( atav6, avas[2] );
+        assertEquals( atav7, avas[3] );
+        assertEquals( atav3, avas[4] );
+        assertEquals( atav1, avas[5] );
+        assertEquals( atav2, avas[6] );
+    }
+}

Propchange: directory/shared/branches/shared-refact/integ/src/test/java/org/apache/directory/api/ldap/model/name/AvaTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message