You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2014/08/30 13:39:03 UTC

svn commit: r1621447 - in /directory/shared/trunk/ldap/model/src: main/java/org/apache/directory/api/ldap/model/schema/normalizers/ObjectClassNormalizer.java test/java/org/apache/directory/api/ldap/model/schema/normalizers/ObjectClassNormalizerTest.java

Author: elecharny
Date: Sat Aug 30 11:39:03 2014
New Revision: 1621447

URL: http://svn.apache.org/r1621447
Log:
Added a normalizer for ObjectClass values

Added:
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/normalizers/ObjectClassNormalizer.java
    directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/normalizers/ObjectClassNormalizerTest.java

Added: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/normalizers/ObjectClassNormalizer.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/normalizers/ObjectClassNormalizer.java?rev=1621447&view=auto
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/normalizers/ObjectClassNormalizer.java (added)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/normalizers/ObjectClassNormalizer.java Sat Aug 30 11:39:03 2014
@@ -0,0 +1,128 @@
+/*
+ *  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.schema.normalizers;
+
+
+
+import org.apache.directory.api.ldap.model.constants.SchemaConstants;
+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.Normalizer;
+import org.apache.directory.api.util.Strings;
+
+
+/**
+ * Normalizer which deals with ObjectClass values. We just have to get rid of spaces 
+ * at the beginning and the end of the name, and to lowercase the value.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@SuppressWarnings("serial")
+public class ObjectClassNormalizer extends Normalizer
+{
+    /**
+     * Creates a new instance of ObjectClassNormalizer.
+     */
+    public ObjectClassNormalizer()
+    {
+        super( SchemaConstants.OBJECT_CLASS_AT_OID );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Value<?> normalize( Value<?> value ) throws LdapException
+    {
+        if ( value == null )
+        {
+            return null;
+        }
+
+        String normalized = normalizeInternal( value.getString() );
+
+        return new StringValue( normalized );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public String normalize( String value ) throws LdapException
+    {
+        if ( value == null )
+        {
+            return "";
+        }
+
+        String normalized = normalizeInternal( value );
+        
+        return normalized;
+    }
+    
+    
+    /**
+     * Normalize the ObjectClass value by removing the leading and training 
+     * spaces, and lowercasing the value.
+     */
+    private String normalizeInternal( String str )
+    {
+        if ( Strings.isEmpty( str ) )
+        {
+            return "";
+        }
+
+        char[] chars = str.toCharArray();
+        int length = chars.length;
+        int startPos = 0;
+        
+        // Skip the starting spaces
+        while ( ( startPos < length ) && ( chars[startPos] == ' ' ) )
+        {
+            startPos++;
+        }
+        
+        // We only have spaces...
+        if ( startPos == length )
+        {
+            return "";
+        }
+        
+        // trim from right
+        int endPos = length - 1;
+        
+        while ( ( endPos > 0 ) && ( chars[endPos] == ' ' ) )
+        {
+            endPos--;
+        }
+        
+        // Now lowercase the chars
+        int currPos = startPos;
+        
+        while ( currPos <= endPos )
+        {
+            chars[currPos] = Character.toLowerCase( chars[currPos] );
+            currPos++;
+        }
+        
+        return new String( chars, startPos, endPos - startPos + 1 );
+    }
+}

Added: directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/normalizers/ObjectClassNormalizerTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/normalizers/ObjectClassNormalizerTest.java?rev=1621447&view=auto
==============================================================================
--- directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/normalizers/ObjectClassNormalizerTest.java (added)
+++ directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/normalizers/ObjectClassNormalizerTest.java Sat Aug 30 11:39:03 2014
@@ -0,0 +1,119 @@
+/*
+ *  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.schema.normalizers;
+
+
+import com.mycila.junit.concurrent.Concurrency;
+import com.mycila.junit.concurrent.ConcurrentJunitRunner;
+
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.schema.Normalizer;
+import org.apache.directory.api.ldap.model.schema.normalizers.ObjectClassNormalizer;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static org.junit.Assert.assertEquals;
+
+
+/**
+ * Test the ObjectClass normalizer class
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(ConcurrentJunitRunner.class)
+@Concurrency()
+public class ObjectClassNormalizerTest
+{
+    @Test
+    public void testObjectClassNormalizerNull() throws LdapException
+    {
+        Normalizer normalizer = new ObjectClassNormalizer();
+        assertEquals( "", normalizer.normalize( ( String ) null ) );
+    }
+
+
+    @Test
+    public void testObjectClassNormalizerEmpty() throws LdapException
+    {
+        Normalizer normalizer = new ObjectClassNormalizer();
+        assertEquals( "", normalizer.normalize( "" ) );
+    }
+
+
+    @Test
+    public void testObjectClassNormalizerOneSpace() throws LdapException
+    {
+        Normalizer normalizer = new ObjectClassNormalizer();
+        assertEquals( "", normalizer.normalize( " " ) );
+    }
+
+
+    @Test
+    public void testObjectClassNormalizerTwoSpaces() throws LdapException
+    {
+        Normalizer normalizer = new ObjectClassNormalizer();
+        assertEquals( "", normalizer.normalize( "  " ) );
+    }
+
+
+    @Test
+    public void testObjectClassNormalizerNSpaces() throws LdapException
+    {
+        Normalizer normalizer = new ObjectClassNormalizer();
+        assertEquals( "", normalizer.normalize( "      " ) );
+    }
+
+
+    @Test
+    public void testOneChar() throws LdapException
+    {
+        Normalizer normalizer = new ObjectClassNormalizer();
+        assertEquals( "a", normalizer.normalize( "a" ) );
+        assertEquals( "a", normalizer.normalize( "A" ) );
+    }
+
+
+    @Test
+    public void testTwoChars() throws LdapException
+    {
+        Normalizer normalizer = new ObjectClassNormalizer();
+        assertEquals( "aa", normalizer.normalize( "Aa" ) );
+        assertEquals( "aa", normalizer.normalize( "aA" ) );
+    }
+
+
+    @Test
+    public void testNChars() throws LdapException
+    {
+        Normalizer normalizer = new ObjectClassNormalizer();
+        assertEquals( "abcdef", normalizer.normalize( "AbCdEf" ) );
+    }
+
+
+    @Test
+    public void testCharsWithSpaces() throws LdapException
+    {
+        Normalizer normalizer = new ObjectClassNormalizer();
+        assertEquals( "a", normalizer.normalize( "   A" ) );
+        assertEquals( "a", normalizer.normalize( "a   " ) );
+        assertEquals( "a", normalizer.normalize( "   A   " ) );
+        assertEquals( "top", normalizer.normalize( "  top   " ) );
+    }
+}
\ No newline at end of file