Author: akarasulu Date: Thu Oct 25 04:36:28 2007 New Revision: 588204 URL: http://svn.apache.org/viewvc?rev=588204&view=rev Log: some test cases Added: directory/apacheds/branches/bigbang/core-entry/src/test/java/org/apache/directory/server/core/entry/ServerStringValueTest.java Modified: directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerBinaryValue.java directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerStringValue.java Modified: directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerBinaryValue.java URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerBinaryValue.java?rev=588204&r1=588203&r2=588204&view=diff ============================================================================== --- directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerBinaryValue.java (original) +++ directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerBinaryValue.java Thu Oct 25 04:36:28 2007 @@ -49,6 +49,11 @@ public ServerBinaryValue( AttributeType attributeType ) throws NamingException { + if ( attributeType == null ) + { + throw new NullPointerException( "attributeType cannot be null" ); + } + if ( attributeType.getSyntax().isHumanReadable() ) { LOG.warn( "Treating a value of a human readible attribute {} as binary: ", attributeType.getName() ); @@ -60,6 +65,11 @@ public ServerBinaryValue( AttributeType attributeType, byte[] wrapped ) { + if ( attributeType == null ) + { + throw new NullPointerException( "attributeType cannot be null" ); + } + this.attributeType = attributeType; super.set( wrapped ); } Modified: directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerStringValue.java URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerStringValue.java?rev=588204&r1=588203&r2=588204&view=diff ============================================================================== --- directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerStringValue.java (original) +++ directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerStringValue.java Thu Oct 25 04:36:28 2007 @@ -54,6 +54,10 @@ public ServerStringValue( AttributeType attributeType ) { + if ( attributeType == null ) + { + throw new NullPointerException( "attributeType cannot be null" ); + } this.attributeType = attributeType; this.oid = attributeType.getOid(); } @@ -61,6 +65,10 @@ public ServerStringValue( AttributeType attributeType, String wrapped ) { + if ( attributeType == null ) + { + throw new NullPointerException( "attributeType cannot be null" ); + } this.attributeType = attributeType; this.oid = attributeType.getOid(); super.set( wrapped ); Added: directory/apacheds/branches/bigbang/core-entry/src/test/java/org/apache/directory/server/core/entry/ServerStringValueTest.java URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core-entry/src/test/java/org/apache/directory/server/core/entry/ServerStringValueTest.java?rev=588204&view=auto ============================================================================== --- directory/apacheds/branches/bigbang/core-entry/src/test/java/org/apache/directory/server/core/entry/ServerStringValueTest.java (added) +++ directory/apacheds/branches/bigbang/core-entry/src/test/java/org/apache/directory/server/core/entry/ServerStringValueTest.java Thu Oct 25 04:36:28 2007 @@ -0,0 +1,472 @@ +package org.apache.directory.server.core.entry; + + +import junit.framework.TestCase; +import org.apache.directory.shared.ldap.schema.*; +import org.apache.directory.shared.ldap.schema.syntax.AcceptAllSyntaxChecker; +import org.apache.directory.shared.ldap.schema.syntax.SyntaxChecker; + +import javax.naming.NamingException; +import javax.naming.directory.InvalidAttributeValueException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashSet; + + +/** + * Tests that the ServerStringValue class works properly as expected. + * + * Some notes while conducting tests: + * + * + * + * @author Apache Directory Project + * @version $Rev$, $Date$ + */ +public class ServerStringValueTest extends TestCase +{ + /** + * Presumes an attribute which constrains it's values to some constant + * strings: LOW, MEDUIM, HIGH. Normalization does nothing. MatchingRules + * are exact case matching. + * + * @throws Exception on errors + */ + public void testConstrainedString() throws Exception + { + S s = new S( "1.1.1.1", true ); + + s.setSyntaxChecker( new SyntaxChecker() { + public String getSyntaxOid() { return "1.1.1.1"; } + public boolean isValidSyntax( Object value ) + { + if ( value instanceof String ) + { + String strval = ( String ) value; + return strval.equals( "HIGH" ) || strval.equals( "LOW" ) || strval.equals( "MEDIUM" ); + } + return false; + } + public void assertSyntax( Object value ) throws NamingException + { if ( ! isValidSyntax( value ) ) throw new InvalidAttributeValueException(); } + }); + + final MR mr = new MR( "1.1.2.1" ); + mr.syntax = s; + mr.comparator = new Comparator() + { + public int compare( String o1, String o2 ) + { + if ( o1 == null && o2 == null ) + { + return 0; + } + + //noinspection ConstantConditions + if ( o1 == null && o2 != null ) + { + return -1; + } + + //noinspection ConstantConditions + if ( o1 != null && o2 == null ) + { + return 1; + } + + int i1 = getValue( o1 ); + int i2 = getValue( o2 ); + + if ( i1 == i2 ) return 0; + if ( i1 > i2 ) return 1; + if ( i1 < i2 ) return -1; + + throw new IllegalStateException( "should not get here at all" ); + } + + int getValue( String val ) + { + if ( val.equals( "LOW" ) ) return 0; + if ( val.equals( "MEDIUM" ) ) return 1; + if ( val.equals( "HIGH" ) ) return 2; + throw new IllegalArgumentException( "Not a valid value" ); + } + }; + mr.normalizer = new NoOpNormalizer(); + AT at = new AT( "1.1.3.1" ); + at.setEquality( mr ); + at.setSyntax( s ); + + // check that normalization and syntax checks work as expected + ServerStringValue value = new ServerStringValue( at, "HIGH" ); + assertEquals( value.get(), value.getNormalizedValue() ); + assertTrue( value.isValid() ); + value = new ServerStringValue( at, "high" ); + assertFalse( value.isValid() ); + + // create a bunch to best tested for equals and in containers + ServerStringValue v0 = new ServerStringValue( at, "LOW" ); + assertTrue( v0.isValid() ); + ServerStringValue v1 = new ServerStringValue( at, "LOW" ); + assertTrue( v1.isValid() ); + ServerStringValue v2 = new ServerStringValue( at, "MEDIUM" ); + assertTrue( v2.isValid() ); + ServerStringValue v3 = new ServerStringValue( at, "HIGH" ); + assertTrue( v3.isValid() ); + ServerStringValue v4 = new ServerStringValue( at ); + assertFalse( v4.isValid() ); + ServerStringValue v5 = new ServerStringValue( at ); + assertFalse( v5.isValid() ); + + // check equals + assertTrue( v0.equals( v1 ) ); + assertTrue( v1.equals( v0 ) ); + assertTrue( v4.equals( v5 ) ); + assertTrue( v5.equals( v4 ) ); + assertFalse( v2.equals( v3 ) ); + assertFalse( v3.equals( v2 ) ); + + // add all except v1 and v5 to a set + HashSet set = new HashSet(); + set.add( v0 ); + set.add( v2 ); + set.add( v3 ); + set.add( v4 ); + + // check contains method + assertTrue( "since v1.equals( v0 ) and v0 was added then this should be true", set.contains( v1 ) ); + assertTrue( "since v4.equals( v5 ) and v4 was added then this should be true", set.contains( v5 ) ); + + // check ordering based on the comparator + ArrayList list = new ArrayList(); + list.add( v1 ); + list.add( v3 ); + list.add( v5 ); + list.add( v0 ); + list.add( v2 ); + list.add( v4 ); + + Comparator c = new Comparator() + { + public int compare( ServerStringValue o1, ServerStringValue o2 ) + { + if ( o1 == null && o2 == null ) + { + return 0; + } + + //noinspection ConstantConditions + if ( o1 == null && o2 != null ) + { + if ( o2.get() == null ) + { + return 0; + } + return -1; + } + + //noinspection ConstantConditions + if ( o1 != null && o2 == null ) + { + if ( o1.get() == null ) + { + return 0; + } + return 1; + } + + //noinspection unchecked + return mr.comparator.compare( o1.get(), o2.get() ); + } + }; + + //noinspection unchecked + Collections.sort( list, c ); + + assertTrue( "since v4 equals v5 and has no value either could be at index 0 & 1", list.get( 0 ).equals( v4 ) ); + assertTrue( "since v4 equals v5 and has no value either could be at index 0 & 1", list.get( 0 ).equals( v5 ) ); + assertTrue( "since v4 equals v5 and has no value either could be at index 0 & 1", list.get( 1 ).equals( v4 ) ); + assertTrue( "since v4 equals v5 and has no value either could be at index 0 & 1", list.get( 1 ).equals( v5 ) ); + + assertTrue( "since v0 equals v1 either could be at index 2 & 3", list.get( 2 ).equals( v0 ) ); + assertTrue( "since v0 equals v1 either could be at index 2 & 3", list.get( 2 ).equals( v1 ) ); + assertTrue( "since v0 equals v1 either could be at index 2 & 3", list.get( 3 ).equals( v0 ) ); + assertTrue( "since v0 equals v1 either could be at index 2 & 3", list.get( 3 ).equals( v1 ) ); + + assertTrue( "since v2 \"MEDIUM\" should be at index 4", list.get( 4 ).equals( v2 ) ); + assertTrue( "since v3 \"HIGH\" should be at index 5", list.get( 5 ).equals( v3 ) ); + + assertEquals( 6, list.size() ); + } + + + /** + * Creates a string value with an attribute type that is of a syntax + * which accepts anything. Also there is no normalization since the + * value is the same as the normalized value. This makes the at technically + * a binary value however it can be dealt with as a string so this test + * is still OK. + * @throws Exception on errors + */ + public void testAcceptAllNoNormalization() throws Exception + { + S s = new S( "1.1.1.1", false ); + s.setSyntaxChecker( new AcceptAllSyntaxChecker( "1.1.1.1" ) ); + final MR mr = new MR( "1.1.2.1" ); + mr.syntax = s; + mr.comparator = new ByteArrayComparator(); + mr.normalizer = new NoOpNormalizer(); + AT at = new AT( "1.1.3.1" ); + at.setEquality( mr ); + at.setOrdering( mr ); + at.setSubstr( mr ); + at.setSyntax( s ); + + // check that normalization and syntax checks work as expected + ServerStringValue value = new ServerStringValue( at, "hello" ); + assertEquals( value.get(), value.getNormalizedValue() ); + assertTrue( value.isValid() ); + + // create a bunch to best tested for equals and in containers + ServerStringValue v0 = new ServerStringValue( at, "hello" ); + ServerStringValue v1 = new ServerStringValue( at, "hello" ); + ServerStringValue v2 = new ServerStringValue( at, "next0" ); + ServerStringValue v3 = new ServerStringValue( at, "next1" ); + ServerStringValue v4 = new ServerStringValue( at ); + ServerStringValue v5 = new ServerStringValue( at ); + + // check equals + assertTrue( v0.equals( v1 ) ); + assertTrue( v1.equals( v0 ) ); + assertTrue( v4.equals( v5 ) ); + assertTrue( v5.equals( v4 ) ); + assertFalse( v2.equals( v3 ) ); + assertFalse( v3.equals( v2 ) ); + + // add all except v1 and v5 to a set + HashSet set = new HashSet(); + set.add( v0 ); + set.add( v2 ); + set.add( v3 ); + set.add( v4 ); + + // check contains method + assertTrue( "since v1.equals( v0 ) and v0 was added then this should be true", set.contains( v1 ) ); + assertTrue( "since v4.equals( v5 ) and v4 was added then this should be true", set.contains( v5 ) ); + + // check ordering based on the comparator + ArrayList list = new ArrayList(); + list.add( v1 ); + list.add( v3 ); + list.add( v5 ); + list.add( v0 ); + list.add( v2 ); + list.add( v4 ); + + Comparator c = new Comparator() + { + public int compare( ServerStringValue o1, ServerStringValue o2 ) + { + byte[] b1 = new byte[0]; + byte[] b2 = new byte[0]; + + try + { + if ( o1 != null ) + { + String n1 = o1.getNormalizedValue(); + if ( n1 != null ) + { + b1 = n1.getBytes( "UTF-8" ); + } + } + + if ( o2 != null ) + { + String n2 = o2.getNormalizedValue(); + if ( n2 != null ) + { + b2 = o2.getNormalizedValue().getBytes( "UTF-8" ); + } + } + } + catch ( Exception e ) + { + e.printStackTrace(); + } + + try + { + //noinspection unchecked + return mr.getComparator().compare( b1, b2 ); + } + catch ( Exception e ) + { + throw new IllegalStateException( "Normalization and comparison should succeed!", e ); + } + } + }; + //noinspection unchecked + Collections.sort( list, c ); + + assertTrue( "since v4 equals v5 and has no value either could be at index 0 & 1", list.get( 0 ).equals( v4 ) ); + assertTrue( "since v4 equals v5 and has no value either could be at index 0 & 1", list.get( 0 ).equals( v5 ) ); + assertTrue( "since v4 equals v5 and has no value either could be at index 0 & 1", list.get( 1 ).equals( v4 ) ); + assertTrue( "since v4 equals v5 and has no value either could be at index 0 & 1", list.get( 1 ).equals( v5 ) ); + + assertTrue( "since v0 equals v1 either could be at index 2 & 3", list.get( 2 ).equals( v0 ) ); + assertTrue( "since v0 equals v1 either could be at index 2 & 3", list.get( 2 ).equals( v1 ) ); + assertTrue( "since v0 equals v1 either could be at index 2 & 3", list.get( 3 ).equals( v0 ) ); + assertTrue( "since v0 equals v1 either could be at index 2 & 3", list.get( 3 ).equals( v1 ) ); + + assertTrue( "since v2 \"next0\" should be at index 4", list.get( 4 ).equals( v2 ) ); + assertTrue( "since v3 \"next1\" should be at index 5", list.get( 5 ).equals( v3 ) ); + + assertEquals( 6, list.size() ); + } + + + static class AT extends AbstractAttributeType + { + AttributeType superior; + Syntax syntax; + MatchingRule equality; + MatchingRule ordering; + MatchingRule substr; + + protected AT( String oid ) + { + super( oid ); + } + + public AttributeType getSuperior() throws NamingException + { + return superior; + } + + + public Syntax getSyntax() throws NamingException + { + return syntax; + } + + + public MatchingRule getEquality() throws NamingException + { + return equality; + } + + + public MatchingRule getOrdering() throws NamingException + { + return ordering; + } + + + public MatchingRule getSubstr() throws NamingException + { + return substr; + } + + + public void setSuperior( AttributeType superior ) + { + this.superior = superior; + } + + + public void setSyntax( Syntax syntax ) + { + this.syntax = syntax; + } + + + public void setEquality( MatchingRule equality ) + { + this.equality = equality; + } + + + public void setOrdering( MatchingRule ordering ) + { + this.ordering = ordering; + } + + + public void setSubstr( MatchingRule substr ) + { + this.substr = substr; + } + } + + static class MR extends AbstractMatchingRule + { + private Syntax syntax; + private Comparator comparator; + private Normalizer normalizer; + + protected MR( String oid ) + { + super( oid ); + } + + public Syntax getSyntax() throws NamingException + { + return syntax; + } + + public Comparator getComparator() throws NamingException + { + return comparator; + } + + + public Normalizer getNormalizer() throws NamingException + { + return normalizer; + } + + + public void setSyntax( Syntax syntax ) + { + this.syntax = syntax; + } + + + public void setComparator( Comparator comparator ) + { + this.comparator = comparator; + } + + + public void setNormalizer( Normalizer normalizer ) + { + this.normalizer = normalizer; + } + } + + + static class S extends AbstractSyntax + { + SyntaxChecker checker; + + public S( String oid, boolean humanReadible ) + { + super( oid, humanReadible ); + } + + public void setSyntaxChecker( SyntaxChecker checker ) + { + this.checker = checker; + } + + public SyntaxChecker getSyntaxChecker() throws NamingException + { + return checker; + } + } +} \ No newline at end of file