You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2004/11/07 04:55:00 UTC

svn commit: rev 56804 - in incubator/directory/ldap/trunk/common/src: antlr java/org/apache/ldap/common/filter test/org/apache/ldap/common/filter

Author: akarasulu
Date: Sat Nov  6 19:55:00 2004
New Revision: 56804

Modified:
   incubator/directory/ldap/trunk/common/src/antlr/filter-value-parser.g
   incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/filter/FilterParserImpl.java
   incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/filter/FilterParserImplTest.java
Log:
Changes ...

 o added more tests for substrings
 o caught a few bugs namely where null and empty strings are concerned
 o added code to the parser to filter out zero length strings
 o spaces cannot be matched unless they are escaped - must confirm this



Modified: incubator/directory/ldap/trunk/common/src/antlr/filter-value-parser.g
==============================================================================
--- incubator/directory/ldap/trunk/common/src/antlr/filter-value-parser.g	(original)
+++ incubator/directory/ldap/trunk/common/src/antlr/filter-value-parser.g	Sat Nov  6 19:55:00 2004
@@ -187,7 +187,7 @@
         // A L T E R N A T I V E   2:    (*any) *final
         ( ASTERISK alt2:VALUEENCODING
         {
-            if ( fin != null )
+            if ( fin != null && fin.length() > 0 )
             {
                 any.add( fin );
             }
@@ -203,7 +203,7 @@
         }
         ( ASTERISK alt3t1:VALUEENCODING
         {
-            if ( fin != null )
+            if ( fin != null && fin.length() > 0 )
             {
                 any.add( fin );
             }
@@ -259,6 +259,16 @@
         }
         else
         {
+            if ( initial != null && initial.length() == 0 )
+            {
+                initial = null;
+            }
+
+            if ( fin != null && fin.length() == 0 )
+            {
+                fin = null;
+            }
+
             node = new SubstringNode( any, attribute, initial, fin );
         }
     }

Modified: incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/filter/FilterParserImpl.java
==============================================================================
--- incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/filter/FilterParserImpl.java	(original)
+++ incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/filter/FilterParserImpl.java	Sat Nov  6 19:55:00 2004
@@ -92,8 +92,7 @@
 
         if ( filter == null || filter.trim().equals( "" ) )
         {
-            throw new ParseException( "The filter string is either null or is "
-                + "the empty String!", 0 ) ;
+            return null;
         }
 
         this.parserPipe.write( filter.getBytes() ) ;

Modified: incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/filter/FilterParserImplTest.java
==============================================================================
--- incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/filter/FilterParserImplTest.java	(original)
+++ incubator/directory/ldap/trunk/common/src/test/org/apache/ldap/common/filter/FilterParserImplTest.java	Sat Nov  6 19:55:00 2004
@@ -26,7 +26,6 @@
 /**
  * Tests the FilterParserImpl class.
  *
- * @todo add some substring expressions there are none here
  * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
  * @version $Rev$
  */
@@ -108,21 +107,27 @@
     {
         PresenceNode node = ( PresenceNode ) parser.parse( "( ou =*)" );
         assertEquals( "ou", node.getAttribute() );
+        assertEquals( AbstractExprNode.PRESENCE, node.getAssertionType() );
 
         node = ( PresenceNode ) parser.parse( "( ou =* )" );
         assertEquals( "ou", node.getAttribute() );
+        assertEquals( AbstractExprNode.PRESENCE, node.getAssertionType() );
 
         node = ( PresenceNode ) parser.parse( "( ou =  * )" );
         assertEquals( "ou", node.getAttribute() );
+        assertEquals( AbstractExprNode.PRESENCE, node.getAssertionType() );
 
         node = ( PresenceNode ) parser.parse( "(  ou = *)" );
         assertEquals( "ou", node.getAttribute() );
+        assertEquals( AbstractExprNode.PRESENCE, node.getAssertionType() );
 
         node = ( PresenceNode ) parser.parse( "( ou =* ) " );
         assertEquals( "ou", node.getAttribute() );
+        assertEquals( AbstractExprNode.PRESENCE, node.getAssertionType() );
 
         node = ( PresenceNode ) parser.parse( "( ou =*)" );
         assertEquals( "ou", node.getAttribute() );
+        assertEquals( AbstractExprNode.PRESENCE, node.getAssertionType() );
     }
 
 
@@ -130,18 +135,23 @@
     {
         PresenceNode node = ( PresenceNode ) parser.parse( "( 1.2.3.4 = * )" );
         assertEquals( "1.2.3.4", node.getAttribute() );
+        assertEquals( AbstractExprNode.PRESENCE, node.getAssertionType() );
 
         node = ( PresenceNode ) parser.parse( "( 1.2.3.4 =  * )" );
         assertEquals( "1.2.3.4", node.getAttribute() );
+        assertEquals( AbstractExprNode.PRESENCE, node.getAssertionType() );
 
         node = ( PresenceNode ) parser.parse( "(  1.2.3.4 = *)" );
         assertEquals( "1.2.3.4", node.getAttribute() );
+        assertEquals( AbstractExprNode.PRESENCE, node.getAssertionType() );
 
         node = ( PresenceNode ) parser.parse( "( 1.2.3.4 =* ) " );
         assertEquals( "1.2.3.4", node.getAttribute() );
+        assertEquals( AbstractExprNode.PRESENCE, node.getAssertionType() );
 
         node = ( PresenceNode ) parser.parse( "( 1.2.3.4 =*)" );
         assertEquals( "1.2.3.4", node.getAttribute() );
+        assertEquals( AbstractExprNode.PRESENCE, node.getAssertionType() );
     }
 
 
@@ -388,4 +398,181 @@
         parser.parse( "( :1.3434.23.2 := dummyAssertion\\23\\ac )" );
         parser.parse( "( := dummyAssertion\\23\\ac )" );
     }
+
+
+    public void testNullOrEmptyString() throws IOException, ParseException
+    {
+        assertNull( parser.parse( null ) );
+        assertNull( parser.parse( "" ) );
+    }
+
+
+    public void testSubstringNoAnyNoFinal() throws IOException, ParseException
+    {
+        SubstringNode node = ( SubstringNode ) parser.parse( "( ou = foo* )" );
+        assertEquals( "ou", node.getAttribute() );
+        assertEquals( AbstractExprNode.SUBSTRING, node.getAssertionType() );
+
+        assertEquals( 0, node.getAny().size() );
+        assertFalse( node.getAny().contains( "" ) );
+        assertEquals( "foo", node.getInitial() );
+        assertEquals( null, node.getFinal() );
+    }
+
+
+    public void testSubstringNoAny() throws IOException, ParseException
+    {
+        SubstringNode node = ( SubstringNode ) parser.parse( "( ou = foo*bar )" );
+        assertEquals( "ou", node.getAttribute() );
+        assertEquals( AbstractExprNode.SUBSTRING, node.getAssertionType() );
+
+        assertEquals( 0, node.getAny().size() );
+        assertFalse( node.getAny().contains( "" ) );
+        assertEquals( "foo", node.getInitial() );
+        assertEquals( "bar", node.getFinal() );
+    }
+
+
+    public void testSubstringNoAnyNoIni() throws IOException, ParseException
+    {
+        SubstringNode node = ( SubstringNode ) parser.parse( "( ou = *bar )" );
+        assertEquals( "ou", node.getAttribute() );
+        assertEquals( AbstractExprNode.SUBSTRING, node.getAssertionType() );
+
+        assertEquals( 0, node.getAny().size() );
+        assertFalse( node.getAny().contains( "" ) );
+        assertEquals( null, node.getInitial() );
+        assertEquals( "bar", node.getFinal() );
+    }
+
+
+    public void testSubstringOneAny() throws IOException, ParseException
+    {
+        SubstringNode node = ( SubstringNode ) parser.parse( "( ou = foo*guy*bar )" );
+        assertEquals( "ou", node.getAttribute() );
+        assertEquals( AbstractExprNode.SUBSTRING, node.getAssertionType() );
+
+        assertEquals( 1, node.getAny().size() );
+        assertFalse( node.getAny().contains( "" ) );
+        assertTrue( node.getAny().contains( "guy" ) );
+        assertEquals( "foo", node.getInitial() );
+        assertEquals( "bar", node.getFinal() );
+    }
+
+
+    public void testSubstringManyAny() throws IOException, ParseException
+    {
+        SubstringNode node = ( SubstringNode ) parser.parse( "( ou =a*b*c*d*e*f )" );
+        assertEquals( "ou", node.getAttribute() );
+        assertEquals( AbstractExprNode.SUBSTRING, node.getAssertionType() );
+
+        assertEquals( 4, node.getAny().size() );
+        assertFalse( node.getAny().contains( "" ) );
+        assertTrue( node.getAny().contains( "b" ) );
+        assertTrue( node.getAny().contains( "c" ) );
+        assertTrue( node.getAny().contains( "d" ) );
+        assertTrue( node.getAny().contains( "e" ) );
+        assertEquals( "a", node.getInitial() );
+        assertEquals( "f", node.getFinal() );
+    }
+
+
+    public void testSubstringNoIniManyAny() throws IOException, ParseException
+    {
+        SubstringNode node = ( SubstringNode ) parser.parse( "( ou =*b*c*d*e*f )" );
+        assertEquals( "ou", node.getAttribute() );
+        assertEquals( AbstractExprNode.SUBSTRING, node.getAssertionType() );
+
+        assertEquals( 4, node.getAny().size() );
+        assertFalse( node.getAny().contains( "" ) );
+        assertTrue( node.getAny().contains( "e" ) );
+        assertTrue( node.getAny().contains( "b" ) );
+        assertTrue( node.getAny().contains( "c" ) );
+        assertTrue( node.getAny().contains( "d" ) );
+        assertEquals( null, node.getInitial() );
+        assertEquals( "f", node.getFinal() );
+    }
+
+
+    public void testSubstringManyAnyNoFinal() throws IOException, ParseException
+    {
+        SubstringNode node = ( SubstringNode ) parser.parse( "( ou =a*b*c*d*e* )" );
+        assertEquals( "ou", node.getAttribute() );
+        assertEquals( AbstractExprNode.SUBSTRING, node.getAssertionType() );
+
+        assertEquals( 4, node.getAny().size() );
+        assertFalse( node.getAny().contains( "" ) );
+        assertTrue( node.getAny().contains( "e" ) );
+        assertTrue( node.getAny().contains( "b" ) );
+        assertTrue( node.getAny().contains( "c" ) );
+        assertTrue( node.getAny().contains( "d" ) );
+        assertEquals( "a", node.getInitial() );
+        assertEquals( null, node.getFinal() );
+    }
+
+
+    public void testSubstringNoIniManyAnyNoFinal() throws IOException, ParseException
+    {
+        SubstringNode node = ( SubstringNode ) parser.parse( "( ou =*b*c*d*e* )" );
+        assertEquals( "ou", node.getAttribute() );
+        assertEquals( AbstractExprNode.SUBSTRING, node.getAssertionType() );
+
+        assertEquals( 4, node.getAny().size() );
+        assertFalse( node.getAny().contains( "" ) );
+        assertTrue( node.getAny().contains( "e" ) );
+        assertTrue( node.getAny().contains( "b" ) );
+        assertTrue( node.getAny().contains( "c" ) );
+        assertTrue( node.getAny().contains( "d" ) );
+        assertEquals( null, node.getInitial() );
+        assertEquals( null, node.getFinal() );
+    }
+
+
+    public void testSubstringNoAnyDoubleSpaceStar() throws IOException, ParseException
+    {
+        SubstringNode node = ( SubstringNode ) parser.parse( "( ou = foo* *bar )" );
+        assertEquals( "ou", node.getAttribute() );
+        assertEquals( AbstractExprNode.SUBSTRING, node.getAssertionType() );
+
+        assertEquals( 0, node.getAny().size() );
+        assertFalse( node.getAny().contains( "" ) );
+        assertFalse( node.getAny().contains( " " ) );
+        assertEquals( "foo", node.getInitial() );
+        assertEquals( "bar", node.getFinal() );
+    }
+
+
+    public void testSubstringAnyDoubleSpaceStar() throws IOException, ParseException
+    {
+        SubstringNode node = ( SubstringNode ) parser.parse( "( ou = foo* a *bar )" );
+        assertEquals( "ou", node.getAttribute() );
+        assertEquals( AbstractExprNode.SUBSTRING, node.getAssertionType() );
+
+        assertEquals( 1, node.getAny().size() );
+        assertFalse( node.getAny().contains( "" ) );
+        assertTrue( node.getAny().contains( "a" ) );
+        assertEquals( "foo", node.getInitial() );
+        assertEquals( "bar", node.getFinal() );
+    }
+
+
+    /* @todo look at custom error handlers for the parser */
+    /////// Causes parser to hang rather than really bombing out.  Looks like
+    /////// we may need to implement a custom error handler for this parser.
+//    public void testSubstringNoAnyDoubleStar() throws IOException, ParseException
+//    {
+//        SubstringNode node = null;
+//
+//        try
+//        {
+//            node = ( SubstringNode ) parser.parse( "( ou = foo**bar )" );
+//            fail("should not get here");
+//        }
+//        catch( Exception e )
+//        {
+//        }
+//
+//        assertNull( node );
+//    }
+
 }