Java Code Examples for org.apache.directory.api.ldap.model.entry.Attribute#clear()

The following examples show how to use org.apache.directory.api.ldap.model.entry.Attribute#clear() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test method clear()
 */
@Test
public void testClear() throws LdapException
{
    Attribute attr = new DefaultAttribute( "email", atEMail );

    assertEquals( 0, attr.size() );

    attr.add( ( String ) null, "a", "b" );
    assertEquals( 3, attr.size() );

    attr.clear();
    assertTrue( attr.isHumanReadable() );
    assertEquals( 0, attr.size() );
    assertEquals( atEMail, attr.getAttributeType() );
}
 
Example 2
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test method toString
 */
@Test
public void testToString() throws LdapException
{
    Attribute attr = new DefaultAttribute( atEMail );

    assertEquals( "email: (null)", attr.toString() );

    attr.setUpId( "EMail" );
    assertEquals( "EMail: (null)", attr.toString() );

    attr.add( ( String ) null );
    assertEquals( "EMail: ''", attr.toString() );

    attr.clear();
    attr.add( "a", "b" );
    assertEquals( "EMail: a\nEMail: b", attr.toString() );
}
 
Example 3
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test method isValid()
 */
@Test
public void testIsValid() throws Exception
{
    Attribute attr = new DefaultAttribute( atCN );

    // No value, this should not be valid
    assertFalse( attr.isValid( atCN ) );

    attr.add( "test", "test2", "A123\\;" );
    assertTrue( attr.isValid( atCN ) );

    // If we try to add a wrong value, it will not be added. The
    // attribute remains valid.
    assertEquals( 0, attr.add( new byte[]
        { 0x01 } ) );
    assertTrue( attr.isValid( atCN ) );

    // test a SINGLE-VALUE attribute. CountryName is SINGLE-VALUE
    attr.clear();
    attr.apply( atC );
    attr.add( "FR" );
    assertTrue( attr.isValid( atC ) );
    assertEquals( 0, attr.add( "US" ) );
    assertFalse( attr.contains( "US" ) );
    assertTrue( attr.isValid( atC ) );
}
 
Example 4
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test method getAll()
 */
@Test
public void testIterator2() throws LdapException
{
    Attribute attr = new DefaultAttribute( atEMail );

    Iterator<Value> iterator = attr.iterator();
    assertFalse( iterator.hasNext() );

    attr.add( nullStringValue );
    iterator = attr.iterator();
    assertTrue( iterator.hasNext() );

    Value value = iterator.next();
    assertEquals( nullStringValue, value );

    attr.clear();
    iterator = attr.iterator();
    assertFalse( iterator.hasNext() );

    attr.add( "a", "b", "c" );
    iterator = attr.iterator();
    assertTrue( iterator.hasNext() );
    assertEquals( "a", iterator.next().getString() );
    assertEquals( "b", iterator.next().getString() );
    assertEquals( "c", iterator.next().getString() );
    assertFalse( iterator.hasNext() );
}
 
Example 5
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test method size()
 */
@Test
public void testSize() throws Exception
{
    Attribute attr1 = new DefaultAttribute( atDC );

    assertEquals( 0, attr1.size() );

    attr1.add( ( String ) null );
    assertEquals( 1, attr1.size() );

    Attribute attr2 = new DefaultAttribute( atCN );

    attr2.add( "a", "b" );
    assertEquals( 2, attr2.size() );

    attr2.clear();
    assertEquals( 0, attr2.size() );

    Attribute attr3 = new DefaultAttribute( atC );

    attr3.add( "US" );
    assertEquals( 1, attr3.size() );

    // TODO : forbid addition of more than 1 value for SINGLE-VALUE attributes
    attr3.add( "FR" );
    assertEquals( 1, attr3.size() );
}
 
Example 6
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test method remove( byte... )
 */
@Test
public void testRemoveByteArray() throws Exception
{
    Attribute attr1 = new DefaultAttribute( atPwd );

    assertFalse( attr1.remove( BYTES1 ) );

    attr1.add( BYTES1, BYTES2, BYTES3 );
    assertTrue( attr1.remove( BYTES1 ) );
    assertEquals( 2, attr1.size() );

    assertTrue( attr1.remove( BYTES2, BYTES3 ) );
    assertEquals( 0, attr1.size() );

    assertFalse( attr1.remove( BYTES4 ) );

    attr1.clear();
    attr1.add( BYTES1, BYTES2, BYTES3 );
    assertFalse( attr1.remove( BYTES3, BYTES4 ) );
    assertEquals( 2, attr1.size() );

    attr1.clear();
    attr1.add( BYTES1, ( byte[] ) null, BYTES2 );
    assertTrue( attr1.remove( ( byte[] ) null, BYTES1 ) );
    assertEquals( 1, attr1.size() );
}
 
Example 7
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test method remove( String... )
 */
@Test
public void testRemoveStringArray() throws Exception
{
    Attribute attr1 = new DefaultAttribute( atEMail );

    assertFalse( attr1.remove( "a" ) );

    attr1.add( "a", "b", "c" );
    assertTrue( attr1.remove( "a" ) );
    assertEquals( 2, attr1.size() );

    assertTrue( attr1.remove( "b", "c" ) );
    assertEquals( 0, attr1.size() );

    assertFalse( attr1.remove( "d" ) );

    attr1.clear();
    attr1.add( "a", "b", "c" );
    assertFalse( attr1.remove( "b", "e" ) );
    assertEquals( 2, attr1.size() );

    attr1.clear();
    attr1.add( "a", ( String ) null, "b" );
    assertTrue( attr1.remove( ( String ) null, "a" ) );
    assertEquals( 1, attr1.size() );

    Attribute attr2 = new DefaultAttribute( "test" );

    attr2.add( BYTES1, BYTES2, BYTES3 );

    assertFalse( attr2.remove( ( String ) null ) );
    assertTrue( attr2.remove( "ab", "c" ) );
    assertFalse( attr2.remove( "d" ) );
}
 
Example 8
Source File: SchemaAwareEntryTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * 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" ) );
}
 
Example 9
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test method hashCode()
 */
@Test
public void testHashCode() throws LdapException
{
    Attribute attr1 = new DefaultAttribute( atDC );
    Attribute attr2 = new DefaultAttribute( atSN );
    assertNotSame( attr1.hashCode(), attr2.hashCode() );

    attr2.apply( atDC );
    assertEquals( attr1.hashCode(), attr2.hashCode() );

    attr1.add( ( String ) null );
    assertNotSame( attr1.hashCode(), attr2.hashCode() );

    attr1.clear();
    assertEquals( attr1.hashCode(), attr2.hashCode() );

    attr1.add( "a", "b" );
    assertNotSame( attr1.hashCode(), attr2.hashCode() );

    attr2.add( "a", "b" );
    assertEquals( attr1.hashCode(), attr2.hashCode() );

    // Order matters
    attr2.clear();
    attr2.add( "b", "a" );
    assertNotSame( attr1.hashCode(), attr2.hashCode() );

    Attribute attr3 = new DefaultAttribute( atPwd );
    Attribute attr4 = new DefaultAttribute( atPwd );
    assertNotSame( attr3.hashCode(), attr4.hashCode() );

    attr3.add( ( byte[] ) null );
    assertNotSame( attr3.hashCode(), attr4.hashCode() );

    attr3.clear();
    assertEquals( attr3.hashCode(), attr4.hashCode() );

    attr3.add( new byte[]
        { 0x01, 0x02 }, new byte[]
        { 0x03, 0x04 } );
    assertNotSame( attr1.hashCode(), attr2.hashCode() );

    attr4.add( new byte[]
        { 0x01, 0x02 }, new byte[]
        { 0x03, 0x04 } );
    assertNotSame( attr1.hashCode(), attr2.hashCode() );

    // Order matters
    attr4.clear();
    attr4.add( new byte[]
        { 0x03, 0x04 }, new byte[]
        { 0x01, 0x02 } );
    assertNotSame( attr1.hashCode(), attr2.hashCode() );
}
 
Example 10
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test method put( byte[]... )
 */
@Test
public void testPutByteArray() throws LdapException, Exception
{
    Attribute attr1 = new DefaultAttribute( atPwd );

    int nbAdded = attr1.add( ( byte[] ) null );
    assertEquals( 1, nbAdded );
    assertFalse( attr1.isHumanReadable() );
    assertTrue( Arrays.equals( nullBinaryValue.getBytes(), attr1.getBytes() ) );

    Attribute attr2 = new DefaultAttribute( atPwd );

    nbAdded = attr2.add( Strings.EMPTY_BYTES );
    assertEquals( 1, nbAdded );
    assertFalse( attr2.isHumanReadable() );
    assertTrue( Arrays.equals( Strings.EMPTY_BYTES, attr2.getBytes() ) );

    Attribute attr3 = new DefaultAttribute( atPwd );

    nbAdded = attr3.add( BYTES1 );
    assertEquals( 1, nbAdded );
    assertFalse( attr3.isHumanReadable() );
    assertTrue( Arrays.equals( BYTES1, attr3.getBytes() ) );

    Attribute attr4 = new DefaultAttribute( atPwd );

    nbAdded = attr4.add( BYTES1, BYTES2 );
    assertEquals( 2, nbAdded );
    assertFalse( attr4.isHumanReadable() );
    assertTrue( attr4.contains( BYTES1 ) );
    assertTrue( attr4.contains( BYTES2 ) );

    attr4.clear();
    nbAdded = attr4.add( BYTES3, BYTES4 );
    assertEquals( 2, nbAdded );
    assertFalse( attr4.isHumanReadable() );
    assertTrue( attr4.contains( BYTES3 ) );
    assertTrue( attr4.contains( BYTES4 ) );

    Attribute attr5 = new DefaultAttribute( atPwd );

    nbAdded = attr5.add( BYTES1, BYTES2, ( byte[] ) null, BYTES3 );
    assertEquals( 4, nbAdded );
    assertFalse( attr5.isHumanReadable() );
    assertTrue( attr5.contains( BYTES1 ) );
    assertTrue( attr5.contains( BYTES2 ) );
    assertTrue( attr5.contains( ( byte[] ) null ) );
    assertTrue( attr5.contains( BYTES3 ) );

    Attribute attr6 = new DefaultAttribute( atPwd );

    assertFalse( attr6.isHumanReadable() );
    nbAdded = attr6.add( BYTES1, ( byte[] ) null );
    assertEquals( 2, nbAdded );
    assertTrue( attr6.contains( BYTES1 ) );
    assertTrue( attr6.contains( ( byte[] ) null ) );
}
 
Example 11
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test method put( String... )
 */
@Test
public void testPutStringArray() throws LdapInvalidAttributeValueException
{
    Attribute attr1 = new DefaultAttribute( atDC );

    int nbAdded = attr1.add( ( String ) null );
    assertEquals( 1, nbAdded );
    assertTrue( attr1.isHumanReadable() );
    assertEquals( nullStringValue, attr1.get() );

    Attribute attr2 = new DefaultAttribute( atDC );

    nbAdded = attr2.add( "" );
    assertEquals( 1, nbAdded );
    assertTrue( attr2.isHumanReadable() );
    assertEquals( "", attr2.getString() );

    Attribute attr3 = new DefaultAttribute( atDC );

    nbAdded = attr3.add( "t" );
    assertEquals( 1, nbAdded );
    assertTrue( attr3.isHumanReadable() );
    assertEquals( "t", attr3.getString() );

    Attribute attr4 = new DefaultAttribute( atEMail );

    nbAdded = attr4.add( "a", "b", "c", "d" );
    assertEquals( 4, nbAdded );
    assertTrue( attr4.isHumanReadable() );
    assertEquals( "a", attr4.getString() );
    assertTrue( attr4.contains( "a" ) );
    assertTrue( attr4.contains( "b" ) );
    assertTrue( attr4.contains( "c" ) );
    assertTrue( attr4.contains( "d" ) );

    attr4.clear();
    nbAdded = attr4.add( "e" );
    assertEquals( 1, nbAdded );
    assertTrue( attr4.isHumanReadable() );
    assertEquals( "e", attr4.getString() );
    assertFalse( attr4.contains( "a" ) );
    assertFalse( attr4.contains( "b" ) );
    assertFalse( attr4.contains( "c" ) );
    assertFalse( attr4.contains( "d" ) );
    assertTrue( attr4.contains( "e" ) );

    attr4.clear();
    nbAdded = attr4.add( BYTES1 );
    assertEquals( 0, nbAdded );
    assertTrue( attr4.isHumanReadable() );

    Attribute attr5 = new DefaultAttribute( atEMail );

    nbAdded = attr5.add( "a", "b", ( String ) null, "d" );
    assertEquals( 4, nbAdded );
    assertTrue( attr5.isHumanReadable() );
    assertTrue( attr5.contains( "a" ) );
    assertTrue( attr5.contains( "b" ) );
    assertTrue( attr5.contains( nullStringValue ) );
    assertTrue( attr5.contains( "d" ) );

    Attribute attr6 = new DefaultAttribute( atPwd );

    nbAdded = attr6.add( "a", ( String ) null );
    assertEquals( 2, nbAdded );
    assertFalse( attr6.isHumanReadable() );
}
 
Example 12
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test method put( Value... )
 */
@Test
public void testPutValueArray() throws Exception
{
    Attribute attr1 = new DefaultAttribute( atDC );

    assertEquals( 0, attr1.size() );

    attr1.add( nullStringValue );
    assertEquals( 1, attr1.size() );
    assertTrue( attr1.contains( nullStringValue ) );

    attr1.clear();
    attr1.add( stringValue1, stringValue2, stringValue3 );
    assertEquals( 3, attr1.size() );
    assertTrue( attr1.contains( stringValue1 ) );
    assertTrue( attr1.contains( stringValue2 ) );
    assertTrue( attr1.contains( stringValue3 ) );

    attr1.clear();
    attr1.add( stringValue1, nullStringValue, stringValue3 );
    assertEquals( 3, attr1.size() );
    assertTrue( attr1.contains( stringValue1 ) );
    assertTrue( attr1.contains( nullStringValue ) );
    assertTrue( attr1.contains( stringValue3 ) );

    attr1.clear();
    attr1.add( stringValue1, nullStringValue, binaryValue3 );
    assertEquals( 2, attr1.size() );
    assertTrue( attr1.contains( stringValue1 ) );
    assertTrue( attr1.contains( nullStringValue ) );
    assertFalse( attr1.contains( stringValue3 ) );

    Attribute attr2 = new DefaultAttribute( atPwd );
    assertEquals( 0, attr2.size() );

    attr2.add( nullBinaryValue );
    assertEquals( 1, attr2.size() );
    assertTrue( attr2.contains( nullBinaryValue ) );

    attr2.clear();
    attr2.add( binaryValue1, binaryValue2, binaryValue3 );
    assertEquals( 3, attr2.size() );
    assertTrue( attr2.contains( binaryValue1 ) );
    assertTrue( attr2.contains( binaryValue2 ) );
    assertTrue( attr2.contains( binaryValue3 ) );

    attr2.clear();
    attr2.add( binaryValue1, nullBinaryValue, stringValue3 );
    assertEquals( 2, attr2.size() );
    assertTrue( attr2.contains( binaryValue1 ) );
    assertTrue( attr2.contains( nullBinaryValue ) );
    assertFalse( attr2.contains( binaryValue3 ) );
}
 
Example 13
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test method remove( Value... )
 */
@Test
public void testRemoveValueArray() throws Exception
{
    Attribute attr1 = new DefaultAttribute( atEMail );

    assertFalse( attr1.remove( stringValue1 ) );

    attr1.add( "a", "b", "c" );
    assertTrue( attr1.remove( stringValue1 ) );
    assertEquals( 2, attr1.size() );

    assertTrue( attr1.remove( stringValue2, stringValue3 ) );
    assertEquals( 0, attr1.size() );

    assertFalse( attr1.remove( stringValue4 ) );

    attr1.clear();
    attr1.add( "a", "b", "c" );
    assertFalse( attr1.remove( stringValue2, stringValue4 ) );
    assertEquals( 2, attr1.size() );

    attr1.clear();
    attr1.add( "a", ( String ) null, "b" );
    assertTrue( attr1.remove( nullStringValue, stringValue1 ) );
    assertEquals( 1, attr1.size() );

    attr1.clear();
    attr1.add( "a", ( String ) null, "b" );
    attr1.add( BYTES3 );
    assertFalse( attr1.remove( nullStringValue, stringValue1, binaryValue3 ) );
    assertEquals( 1, attr1.size() );

    Attribute attr2 = new DefaultAttribute( atPwd );

    assertFalse( attr2.remove( binaryValue1 ) );

    attr2.add( BYTES1, BYTES2, BYTES3 );
    assertTrue( attr2.remove( binaryValue1 ) );
    assertEquals( 2, attr2.size() );

    assertTrue( attr2.remove( binaryValue2, binaryValue3 ) );
    assertEquals( 0, attr2.size() );

    assertFalse( attr2.remove( binaryValue4 ) );

    attr2.clear();
    attr2.add( BYTES1, BYTES2, BYTES3 );
    assertFalse( attr2.remove( binaryValue2, stringValue4 ) );
    assertEquals( 2, attr2.size() );

    attr2.clear();
    attr2.add( BYTES1, ( byte[] ) null, BYTES3 );
    assertFalse( attr2.remove( nullStringValue, binaryValue1 ) );
    assertEquals( 2, attr2.size() );

    attr2.clear();
    attr2.add( BYTES1, ( byte[] ) null, BYTES2 );
    attr2.add( "c" );
    assertEquals( 4, attr2.size() );
    assertFalse( attr2.remove( nullStringValue, binaryValue1, stringValue3 ) );
    assertEquals( 3, attr2.size() );
}
 
Example 14
Source File: SchemaInterceptor.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * Given the objectClasses for an entry, this method adds missing ancestors
 * in the hierarchy except for top which it removes.  This is used for this
 * solution to DIREVE-276.  More information about this solution can be found
 * <a href="http://docs.safehaus.org:8080/x/kBE">here</a>.
 *
 * @param objectClassAttr the objectClass attribute to modify
 * @throws Exception if there are problems
 */
private void alterObjectClasses( Attribute objectClassAttr ) throws LdapException
{
    Set<String> objectClasses = new HashSet<String>();
    Set<String> objectClassesUP = new HashSet<String>();

    // Init the objectClass list with 'top'
    objectClasses.add( SchemaConstants.TOP_OC );
    objectClassesUP.add( SchemaConstants.TOP_OC );

    // Construct the new list of ObjectClasses
    for ( Value<?> ocValue : objectClassAttr )
    {
        String ocName = ocValue.getString();

        if ( !ocName.equalsIgnoreCase( SchemaConstants.TOP_OC ) )
        {
            String ocLowerName = Strings.toLowerCase( ocName );

            ObjectClass objectClass = schemaManager.lookupObjectClassRegistry( ocLowerName );

            if ( !objectClasses.contains( ocLowerName ) )
            {
                objectClasses.add( ocLowerName );
                objectClassesUP.add( ocName );
            }

            List<ObjectClass> ocSuperiors = superiors.get( objectClass.getOid() );

            if ( ocSuperiors != null )
            {
                for ( ObjectClass oc : ocSuperiors )
                {
                    if ( !objectClasses.contains( Strings.toLowerCase( oc.getName() ) ) )
                    {
                        objectClasses.add( oc.getName() );
                        objectClassesUP.add( oc.getName() );
                    }
                }
            }
        }
    }

    // Now, reset the ObjectClass attribute and put the new list into it
    objectClassAttr.clear();

    for ( String attribute : objectClassesUP )
    {
        objectClassAttr.add( attribute );
    }
}
 
Example 15
Source File: SchemaInterceptor.java    From MyVirtualDirectory with Apache License 2.0 4 votes vote down vote up
/**
 * Given the objectClasses for an entry, this method adds missing ancestors
 * in the hierarchy except for top which it removes.  This is used for this
 * solution to DIREVE-276.  More information about this solution can be found
 * <a href="http://docs.safehaus.org:8080/x/kBE">here</a>.
 *
 * @param objectClassAttr the objectClass attribute to modify
 * @throws Exception if there are problems
 */
private void alterObjectClasses( Attribute objectClassAttr ) throws LdapException
{
    Set<String> objectClasses = new HashSet<String>();
    Set<String> objectClassesUP = new HashSet<String>();

    // Init the objectClass list with 'top'
    objectClasses.add( SchemaConstants.TOP_OC );
    objectClassesUP.add( SchemaConstants.TOP_OC );

    // Construct the new list of ObjectClasses
    for ( Value<?> ocValue : objectClassAttr )
    {
        String ocName = ocValue.getString();

        if ( !ocName.equalsIgnoreCase( SchemaConstants.TOP_OC ) )
        {
            String ocLowerName = Strings.toLowerCase( ocName );

            ObjectClass objectClass = schemaManager.lookupObjectClassRegistry( ocLowerName );

            if ( !objectClasses.contains( ocLowerName ) )
            {
                objectClasses.add( ocLowerName );
                objectClassesUP.add( ocName );
            }

            List<ObjectClass> ocSuperiors = superiors.get( objectClass.getOid() );

            if ( ocSuperiors != null )
            {
                for ( ObjectClass oc : ocSuperiors )
                {
                    if ( !objectClasses.contains( Strings.toLowerCase( oc.getName() ) ) )
                    {
                        objectClasses.add( oc.getName() );
                        objectClassesUP.add( oc.getName() );
                    }
                }
            }
        }
    }

    // Now, reset the ObjectClass attribute and put the new list into it
    objectClassAttr.clear();

    for ( String attribute : objectClassesUP )
    {
        objectClassAttr.add( attribute );
    }
}