directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: rev 9867 - in incubator/directory/snickers/trunk: . ber/src/java/org/apache/snickers/ber/digester ber/src/test/org/apache/snickers/ber/digester
Date Mon, 05 Apr 2004 04:14:50 GMT
Author: akarasulu
Date: Sun Apr  4 21:14:48 2004
New Revision: 9867

Modified:
   incubator/directory/snickers/trunk/   (props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/BERDigester.java
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/digester/BERDigesterTest.java
   incubator/directory/snickers/trunk/maven.xml
   incubator/directory/snickers/trunk/project.xml
Log:
o cleaned up build
o got 100% test coverage on digester


Modified: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/BERDigester.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/BERDigester.java
(original)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/BERDigester.java
Sun Apr  4 21:14:48 2004
@@ -19,10 +19,7 @@
 
 import java.nio.ByteBuffer ;
 
-import java.util.HashSet;
-import java.util.List ;
-import java.util.Iterator ;
-import java.util.EmptyStackException ;
+import java.util.*;
 
 import org.apache.snickers.ber.Tuple ;
 import org.apache.snickers.ber.IntStack ;
@@ -31,6 +28,8 @@
 import org.apache.snickers.ber.BERDecoderCallback ;
 
 import org.apache.commons.codec.stateful.StatefulDecoder ;
+import org.apache.commons.codec.stateful.AbstractStatefulDecoder;
+import org.apache.commons.codec.DecoderException;
 
 import org.apache.commons.collections.ArrayStack ;
 
@@ -43,8 +42,17 @@
  * Apache Directory Project</a>
  * @version $Rev$
  */
-public class BERDigester implements BERDecoderCallback
+public class BERDigester extends AbstractStatefulDecoder
 {
+    /**
+     * For now this corresponds to a tag of class Universal with an id
+     * of 15.  This has been reserved for future use but to my knowledge
+     * currently it is not being used for anything specifically.  Hence
+     * we use it as the return value instead of throwing an empty stack
+     * exception when the top of the tag stack is requested.
+     */
+    public static final int NO_TOP_TAG = 0x0f000000 ;
+
     /** the underlying decoder used by this rulesBase */
     private BERDecoder decoder ;
     /** the object stack where rules push and pop ASN.1 POJO stubs */
@@ -84,55 +92,72 @@
         this.tagStack = new IntStack() ;
         this.objectStack = new ArrayStack() ;
         this.decoder = new BERDecoder() ;
-        this.decoder.setCallback( this ) ;
+        this.decoder.setCallback( new DigesterCallback() ) ;
     }
     
     
     // ------------------------------------------------------------------------
-    // BERDecoderCallback methods 
+    // StatefulDecoder implementation
     // ------------------------------------------------------------------------
 
-    
-    /* (non-Javadoc)
-     * @see org.apache.snickers.ber.BERDecoderCallback#tagDecoded(
-     * org.apache.snickers.ber.Tuple)
-     */
-    public void tagDecoded( Tuple tlv )
-    {
-        tagStack.push( tlv.getRawPrimitiveTag() ) ;
-        matched = rules.match( tagStack ) ;
-        fireTagEvent( tlv.getId(), tlv.isPrimitive(), tlv.getTypeClass() ) ;
-    }
 
-    
     /* (non-Javadoc)
-     * @see org.apache.snickers.ber.BERDecoderCallback#lengthDecoded(
-     * org.apache.snickers.ber.Tuple)
+     * @see org.apache.commons.codec.stateful.StatefulDecoder
+     * #decode(java.lang.Object)
      */
-    public void lengthDecoded( Tuple tlv )
+    public void decode( Object encoded ) throws DecoderException
     {
-        fireLengthEvent( tlv.getLength() ) ;
+        decoder.decode( encoded ) ;
     }
 
-    
-    /* (non-Javadoc)
-     * @see org.apache.snickers.ber.BERDecoderCallback#partialValueDecoded(
-     * org.apache.snickers.ber.Tuple)
-     */
-    public void partialValueDecoded( Tuple tlv )
-    {
-        fireValueEvent( tlv.getLastValueChunk() ) ;
-    }
+
+    // ------------------------------------------------------------------------
+    // BERDecoderCallback implementation
+    // ------------------------------------------------------------------------
 
 
-    /* (non-Javadoc)
-     * @see org.apache.commons.codec.stateful.DecoderCallback#decodeOccurred(
-     * org.apache.commons.codec.stateful.StatefulDecoder, java.lang.Object)
-     */
-    public void decodeOccurred( StatefulDecoder decoder, Object decoded )
+    class DigesterCallback implements BERDecoderCallback
     {
-        fireFinishEvent() ;
-        tagStack.pop() ;
+        /* (non-Javadoc)
+         * @see org.apache.snickers.ber.BERDecoderCallback#tagDecoded(
+         * org.apache.snickers.ber.Tuple)
+         */
+        public void tagDecoded( Tuple tlv )
+        {
+            tagStack.push( tlv.getRawPrimitiveTag() ) ;
+            matched = rules.match( tagStack ) ;
+            fireTagEvent( tlv.getId(), tlv.isPrimitive(), tlv.getTypeClass() ) ;
+        }
+
+
+        /* (non-Javadoc)
+         * @see org.apache.snickers.ber.BERDecoderCallback#lengthDecoded(
+         * org.apache.snickers.ber.Tuple)
+         */
+        public void lengthDecoded( Tuple tlv )
+        {
+            fireLengthEvent( tlv.getLength() ) ;
+        }
+
+
+        /* (non-Javadoc)
+         * @see org.apache.snickers.ber.BERDecoderCallback#partialValueDecoded(
+         * org.apache.snickers.ber.Tuple)
+         */
+        public void partialValueDecoded( Tuple tlv )
+        {
+            fireValueEvent( tlv.getLastValueChunk() ) ;
+        }
+
+
+        /* (non-Javadoc)
+         * @see org.apache.commons.codec.stateful.DecoderCallback#decodeOccurred(
+         * org.apache.commons.codec.stateful.StatefulDecoder, java.lang.Object)
+         */
+        public void decodeOccurred( StatefulDecoder decoder, Object decoded )
+        {
+            fireFinishEvent() ;
+        }
     }
 
 
@@ -196,15 +221,12 @@
      */
     public Object peek() 
     {
-        try 
-        {
-            return ( objectStack.peek() ) ;
-        } 
-        catch ( EmptyStackException e ) 
+        if ( objectStack.size() == 0 )
         {
-            //log.warn("Empty stack (returning null)") ;
-            return ( null ) ;
+            return null ;
         }
+
+        return ( objectStack.peek() ) ;
     }
     
     
@@ -217,15 +239,12 @@
      */
     public Object peek( int n )
     {
-        try 
+        if ( objectStack.size() < n || objectStack.size() <= 0 )
         {
-            return ( objectStack.peek() ) ;
-        } 
-        catch ( EmptyStackException e ) 
-        {
-            //log.warn("Empty stack (returning null)") ;
-            return ( null ) ;
+            return null ;
         }
+
+        return ( objectStack.peek( n ) ) ;
     }
               
     
@@ -236,15 +255,12 @@
      */
     public Object pop()
     {
-        try 
+        if ( objectStack.size() == 0 )
         {
-            return ( objectStack.pop() ) ;
-        } 
-        catch ( EmptyStackException e ) 
-        {
-            // log.warn("Empty stack (returning null)") ;
-            return ( null ) ;
+            return null ;
         }
+
+        return ( objectStack.pop() ) ;
     }
               
     
@@ -291,14 +307,21 @@
     
     
     /**
-     * Gets the raw int for the tag of the TLV currently being processed.  The
-     * tag int has the primitive flag dubbed out so it appears to be a primitive
-     * even when constructed.
+     * Gets the raw int for the tag of the TLV currently being processed hence
+     * the tag on the top of the stack.  The tag's int has the primitive flag
+     * dubbed out so it appears to represent a primitive TLV even when the TLV
+     * may be constructed.
      * 
-     * @return the raw int for the tag of the TLV currently being processed
+     * @return the raw int for the tag of the TLV currently being processed, or
+     * NO_TOP_TAG if there is no TLV currently being processed.
      */
-    public int getCurrentRawPrimitiveTag()
+    public int getTopTag()
     {
+        if ( tagStack.size() <= 0 )
+        {
+            return NO_TOP_TAG ;
+        }
+
         return tagStack.peek() ;
     }
 
@@ -396,12 +419,18 @@
     
     void fireTagEvent( int id, boolean isPrimitive, TypeClass typeClass ) 
     {
-        pop() ;
-        
-        // Fire "tag" events for all relevant rules
-        Iterator rules = matched.iterator() ;
-        
-        while ( rules.hasNext() ) 
+        Iterator rules = null ;
+
+        if ( matched == null )
+        {
+            rules = Collections.EMPTY_LIST.iterator() ;
+        }
+        else
+        {
+            rules = matched.iterator() ;
+        }
+
+        while ( rules.hasNext() )
         {
             Rule rule = ( Rule ) rules.next() ;
             
@@ -409,11 +438,11 @@
             {
                 rule.tag( id, isPrimitive, typeClass ) ;
             } 
-            catch ( Exception e ) 
+            catch ( RuntimeException e ) 
             {
                 // log.error( "Tag event threw exception", e ) ;
-                // throw createSAXException(e);
-            } 
+                throw e ;
+            }
             catch ( Error e ) 
             {
                 // log.error( "Tag event threw error", e ) ;
@@ -425,12 +454,18 @@
     
     void fireLengthEvent( int length )
     {
-        pop() ;
-        
-        // Fire "length" events for all relavent rules
-        Iterator rules = matched.iterator() ;
-        
-        while ( rules.hasNext() ) 
+        Iterator rules = null ;
+
+        if ( matched == null )
+        {
+            rules = Collections.EMPTY_LIST.iterator() ;
+        }
+        else
+        {
+            rules = matched.iterator() ;
+        }
+
+        while ( rules.hasNext() )
         {
             Rule rule = ( Rule ) rules.next() ;
             
@@ -438,11 +473,11 @@
             {
                 rule.length( length ) ;
             } 
-            catch ( Exception e ) 
+            catch ( RuntimeException e )
             {
                 // log.error( "Length event threw exception", e ) ;
-                // throw createSAXException(e);
-            } 
+                throw e ;
+            }
             catch ( Error e ) 
             {
                 // log.error( "Length event threw error", e ) ;
@@ -454,12 +489,18 @@
     
     void fireValueEvent( ByteBuffer buf )
     {
-        pop() ;
-        
-        // Fire "value" events for all relavent rules
-        Iterator rules = matched.iterator() ;
-        
-        while ( rules.hasNext() ) 
+        Iterator rules = null ;
+
+        if ( matched == null )
+        {
+            rules = Collections.EMPTY_LIST.iterator() ;
+        }
+        else
+        {
+            rules = matched.iterator() ;
+        }
+
+        while ( rules.hasNext() )
         {
             Rule rule = ( Rule ) rules.next() ;
             
@@ -467,11 +508,11 @@
             {
                 rule.value( buf ) ;
             } 
-            catch ( Exception e ) 
+            catch ( RuntimeException e )
             {
                 // log.error( "Value event threw exception", e ) ;
-                // throw createSAXException(e);
-            } 
+                throw e ;
+            }
             catch ( Error e ) 
             {
                 // log.error( "Value event threw error", e ) ;
@@ -483,13 +524,10 @@
     
     void fireFinishEvent()
     {
+        HashSet seen = null ;
         pop() ;
 
-        
-        HashSet seen = null ;
-        
-        // Fire "finish" events for all relevant rules in reverse order
-        if ( matched != null ) 
+        if ( matched != null )
         {
             seen = new HashSet() ;
             
@@ -508,13 +546,14 @@
                     */
                     
                     rule.finish() ;
+                    tagStack.pop() ;
                     seen.add( rule ) ;
                 } 
-                catch ( Exception e ) 
+                catch ( RuntimeException e )
                 {
                     //log.error("End event threw exception", e);
-                    //throw createSAXException(e);
-                } 
+                    throw e ;
+                }
                 catch ( Error e ) 
                 {
                     //log.error("End event threw error", e);

Modified: incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/digester/BERDigesterTest.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/digester/BERDigesterTest.java
(original)
+++ incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/digester/BERDigesterTest.java
Sun Apr  4 21:14:48 2004
@@ -19,6 +19,12 @@
 
 import junit.framework.* ;
 
+import java.nio.ByteBuffer;
+import java.net.URLClassLoader;
+import java.net.URL;
+
+import org.apache.snickers.ber.TypeClass;
+
 
 /**
  * A test case for the BERDigester.
@@ -58,8 +64,891 @@
     }
 
 
-    public void testTagStack()
+    /**
+     * Tests the BERDigester.addRule(int[],Rule) method.
+     */
+    public void testAddRule() throws Exception
+    {
+        int[] pat0 = { 1, 2, 3 } ;
+        MockRule rule0 = new MockRule() ;
+
+        digester.addRule( pat0, rule0 ) ;
+    }
+
+
+    /**
+     * Tests the BERDigester.setRules(Rules) method.
+     */
+    public void testSetRules() throws Exception
+    {
+        RulesBase rules = new RulesBase() ;
+        digester.setRules( rules ) ;
+        assertSame( rules.getDigester(), digester ) ;
+    }
+
+
+    /**
+     * Tests the BERDigester.getRules() method.
+     */
+    public void testGetRules() throws Exception
+    {
+        Rules rules = digester.getRules() ;
+        assertNotNull( rules ) ;
+        assertTrue( rules.rules().isEmpty() ) ;
+    }
+
+
+    /**
+     * Tests the BERDigester.getCount() method.
+     */
+    public void testGetCount() throws Exception
+    {
+        assertEquals( 0, digester.getCount() ) ;
+    }
+
+
+    /**
+     * Tests the BERDigester.peek() method.
+     */
+    public void testPeek() throws Exception
+    {
+        assertNull( digester.peek() ) ;
+    }
+
+
+    /**
+     * Tests the BERDigester.peek(int) method.
+     */
+    public void testPeekint() throws Exception
+    {
+        assertNull( digester.peek( 0 ) ) ;
+        assertNull( digester.peek( -1 ) ) ;
+        assertNull( digester.peek( Integer.MAX_VALUE ) ) ;
+        assertNull( digester.peek( Integer.MIN_VALUE ) ) ;
+    }
+
+
+    /**
+     * Tests the BERDigester.pop() method.
+     */
+    public void testPop() throws Exception
+    {
+        assertNull( digester.pop() ) ;
+    }
+
+
+    /**
+     * Tests the BERDigester.push(Object) method.
+     */
+    public void testPush() throws Exception
+    {
+        Object o0 = new Object() ;
+        Object o1 = new Object() ;
+        Object o2 = new Object() ;
+
+        assertNull( digester.pop() ) ;
+
+        digester.push( o0 ) ;
+        assertSame( o0, digester.peek() ) ;
+        assertSame( o0, digester.peek( 0 ) ) ;
+
+        digester.push( o1 ) ;
+        assertSame( o1, digester.peek() ) ;
+        assertSame( o1, digester.peek( 0 ) ) ;
+        assertSame( o0, digester.peek( 1 ) ) ;
+
+        digester.push( o2 ) ;
+        assertSame( o2, digester.peek() ) ;
+        assertSame( o2, digester.peek( 0 ) ) ;
+        assertSame( o1, digester.peek( 1 ) ) ;
+        assertSame( o0, digester.peek( 2 ) ) ;
+
+        assertSame( o2, digester.pop() ) ;
+        assertSame( o1, digester.pop() ) ;
+        assertSame( o0, digester.pop() ) ;
+
+        assertNull( digester.pop() ) ;
+    }
+
+
+    /**
+     * Tests the BERDigester.GetRoot() method.
+     */
+    public void testGetRoot() throws Exception
+    {
+        Object o0 = new Object() ;
+        Object o1 = new Object() ;
+        Object o2 = new Object() ;
+
+        assertNull( digester.getRoot() ) ;
+
+        digester.push( o0 ) ;
+        assertSame( o0, digester.getRoot() ) ;
+
+        digester.push( o1 ) ;
+        assertSame( o0, digester.getRoot() ) ;
+
+        digester.push( o2 ) ;
+
+        assertSame( o2, digester.pop() ) ;
+        assertSame( o1, digester.pop() ) ;
+        assertSame( o0, digester.pop() ) ;
+
+        assertNull( digester.pop() ) ;
+
+        assertSame( o0, digester.getRoot() ) ;
+    }
+
+
+    /**
+     * Tests the BERDigester.getTopTag() method.
+     */
+    public void testGetTopTag() throws Exception
+    {
+        assertEquals( BERDigester.NO_TOP_TAG, digester.getTopTag() ) ;
+        digester.decode( ByteBuffer.allocate( 1 ) ) ;
+        assertEquals( 0, digester.getTopTag() ) ;
+    }
+
+
+    /**
+     * Tests the BERDigester.getClassLoader() method.
+     */
+    public void testGetClassLoader() throws Exception
+    {
+        assertSame( BERDigester.class.getClassLoader(), digester.getClassLoader() ) ;
+    }
+
+
+    /**
+     * Tests the BERDigester.setClassLoader() method.
+     */
+    public void testSetClassLoader() throws Exception
+    {
+        assertSame( BERDigester.class.getClassLoader(), digester.getClassLoader() ) ;
+        URL[] urls = { new URL( "file:///." ) } ;
+        ClassLoader cl = new URLClassLoader( urls ) ;
+        digester.setClassLoader( cl ) ;
+        assertSame( cl, digester.getClassLoader() ) ;
+    }
+
+
+    /**
+     * Tests the BERDigester.getUseContextClassLoader() method.
+     */
+    public void testGetUseContextClassLoader() throws Exception
+    {
+        assertSame( BERDigester.class.getClassLoader(), digester.getClassLoader() ) ;
+        assertFalse( digester.getUseContextClassLoader() ) ;
+    }
+
+
+    /**
+     * Tests the BERDigester.setUseContextClassLoader() method.
+     */
+    public void testSetUseContextClassLoader() throws Exception
+    {
+        digester.setUseContextClassLoader( true ) ;
+        assertTrue( digester.getUseContextClassLoader() ) ;
+        assertSame( Thread.currentThread().getContextClassLoader(),
+                digester.getClassLoader() ) ;
+        Thread.currentThread().setContextClassLoader( null ) ;
+        assertSame( BERDigester.class.getClassLoader(), digester.getClassLoader() ) ;
+    }
+
+
+    // ------------------------------------------------------------------------
+    // test the event fireing routines that trigger rules
+    // ------------------------------------------------------------------------
+
+
+    /**
+     * Tests the BERDigester.fireTagEvent(int,boolean,TypeClass) method.
+     */
+    public void testFireTagEvent() throws Exception
+    {
+        digester.fireTagEvent( 0, true, TypeClass.UNIVERSAL ) ;
+
+        int[] pat0 = { 0x10000000 } ;
+        CollectorRule rule0 = new CollectorRule() ;
+        digester.addRule( pat0, rule0 ) ;
+        ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+        buf.put( ( byte ) 0x10 ) ;
+        buf.flip() ;
+
+        // Nothing should have fired yet
+        assertFalse( rule0.tagFired ) ;
+        assertFalse( rule0.lengthFired ) ;
+        assertFalse( rule0.valueFired ) ;
+        assertFalse( rule0.hasFinished ) ;
+
+        digester.decode( buf ) ;
+        assertTrue( rule0.tagFired ) ;
+        assertEquals( 16, rule0.id ) ;
+        assertEquals( TypeClass.UNIVERSAL, rule0.typeClass ) ;
+        assertEquals( true, rule0.isPrimitive ) ;
+        assertFalse( rule0.lengthFired ) ;
+        assertFalse( rule0.valueFired ) ;
+        assertFalse( rule0.hasFinished ) ;
+        assertEquals( 0x10000000, digester.getTopTag() ) ;
+    }
+
+
+    /**
+     * Tests the BERDigester.fireLengthEvent(int) method.
+     */
+    public void testFireLengthEvent() throws Exception
+    {
+        digester.fireLengthEvent( 0 ) ;
+
+        int[] pat0 = { 0x10000000 } ;
+        CollectorRule rule0 = new CollectorRule() ;
+        digester.addRule( pat0, rule0 ) ;
+        ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+        buf.put( ( byte ) 0x10 ) ;
+        buf.flip() ;
+
+        // Nothing should have fired yet
+        assertFalse( rule0.tagFired ) ;
+        assertFalse( rule0.lengthFired ) ;
+        assertFalse( rule0.valueFired ) ;
+        assertFalse( rule0.hasFinished ) ;
+
+        digester.decode( buf ) ;
+        assertTrue( rule0.tagFired ) ;
+        assertEquals( 16, rule0.id ) ;
+        assertEquals( TypeClass.UNIVERSAL, rule0.typeClass ) ;
+        assertEquals( true, rule0.isPrimitive ) ;
+        assertFalse( rule0.lengthFired ) ;
+        assertFalse( rule0.valueFired ) ;
+        assertFalse( rule0.hasFinished ) ;
+        assertEquals( 0x10000000, digester.getTopTag() ) ;
+    }
+
+
+    /**
+     * Tests the BERDigester.fireValueEvent(ByteBuffer) method.
+     */
+    public void testFireValueEvent() throws Exception
     {
-        
+        digester.fireValueEvent( ByteBuffer.allocate( 0 ) ) ;
+
+        int[] pat0 = { 0x10000000 } ;
+        CollectorRule rule0 = new CollectorRule() ;
+        digester.addRule( pat0, rule0 ) ;
+        ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+        buf.put( ( byte ) 0x10 ) ;
+        buf.flip() ;
+
+        // Nothing should have fired yet
+        assertFalse( rule0.tagFired ) ;
+        assertFalse( rule0.lengthFired ) ;
+        assertFalse( rule0.valueFired ) ;
+        assertFalse( rule0.hasFinished ) ;
+
+        digester.decode( buf ) ;
+        assertTrue( rule0.tagFired ) ;
+        assertEquals( 16, rule0.id ) ;
+        assertEquals( TypeClass.UNIVERSAL, rule0.typeClass ) ;
+        assertEquals( true, rule0.isPrimitive ) ;
+        assertFalse( rule0.lengthFired ) ;
+        assertFalse( rule0.valueFired ) ;
+        assertFalse( rule0.hasFinished ) ;
+        assertEquals( 0x10000000, digester.getTopTag() ) ;
+
+        buf.rewind() ;
+        buf.put( ( byte ) 0x02 ) ;
+        buf.flip() ;
+        digester.decode( buf ) ;
+        assertTrue( rule0.tagFired ) ;
+        assertEquals( 2, rule0.length ) ;
+        assertTrue( rule0.lengthFired ) ;
+        assertFalse( rule0.valueFired ) ;
+        assertFalse( rule0.hasFinished ) ;
+        assertEquals( 0x10000000, digester.getTopTag() ) ;
+
+        buf.rewind() ;
+        buf.put( ( byte ) 0x07 ) ;
+        buf.flip() ;
+        digester.decode( buf ) ;
+        assertTrue( rule0.tagFired ) ;
+        assertEquals( 2, rule0.length ) ;
+        assertTrue( rule0.lengthFired ) ;
+        assertTrue( rule0.valueFired ) ;
+        assertFalse( rule0.hasFinished ) ;
+        assertEquals( 0x10000000, digester.getTopTag() ) ;
+        assertEquals( buf.get(0), rule0.buf.get(0) ) ;
+    }
+
+
+    /**
+     * Tests the BERDecoder.fireFinishEvent() methods.
+     */
+    public void testFireFinishEvent() throws Exception
+    {
+        digester.fireFinishEvent() ;
+
+        int[] pat0 = { 0x10000000 } ;
+        CollectorRule rule0 = new CollectorRule() ;
+        digester.addRule( pat0, rule0 ) ;
+        ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+        buf.put( ( byte ) 0x10 ) ;
+        buf.flip() ;
+
+        // Nothing should have fired yet
+        assertFalse( rule0.tagFired ) ;
+        assertFalse( rule0.lengthFired ) ;
+        assertFalse( rule0.valueFired ) ;
+        assertFalse( rule0.hasFinished ) ;
+
+        digester.decode( buf ) ;
+        assertTrue( rule0.tagFired ) ;
+        assertEquals( 16, rule0.id ) ;
+        assertEquals( TypeClass.UNIVERSAL, rule0.typeClass ) ;
+        assertEquals( true, rule0.isPrimitive ) ;
+        assertFalse( rule0.lengthFired ) ;
+        assertFalse( rule0.valueFired ) ;
+        assertFalse( rule0.hasFinished ) ;
+        assertEquals( 0x10000000, digester.getTopTag() ) ;
+
+        buf.rewind() ;
+        buf.put( ( byte ) 0x02 ) ;
+        buf.flip() ;
+        digester.decode( buf ) ;
+        assertTrue( rule0.tagFired ) ;
+        assertEquals( 2, rule0.length ) ;
+        assertTrue( rule0.lengthFired ) ;
+        assertFalse( rule0.valueFired ) ;
+        assertFalse( rule0.hasFinished ) ;
+        assertEquals( 0x10000000, digester.getTopTag() ) ;
+
+        buf.rewind() ;
+        buf.put( ( byte ) 0x07 ) ;
+        buf.flip() ;
+        digester.decode( buf ) ;
+        assertTrue( rule0.tagFired ) ;
+        assertEquals( 2, rule0.length ) ;
+        assertTrue( rule0.lengthFired ) ;
+        assertTrue( rule0.valueFired ) ;
+        assertFalse( rule0.hasFinished ) ;
+        assertEquals( 0x10000000, digester.getTopTag() ) ;
+        assertEquals( buf.get(0), rule0.buf.get(0) ) ;
+
+        buf.rewind() ;
+        buf.put( ( byte ) 0x09 ) ;
+        buf.flip() ;
+        digester.decode( buf ) ;
+        assertTrue( rule0.tagFired ) ;
+        assertEquals( 2, rule0.length ) ;
+        assertTrue( rule0.lengthFired ) ;
+        assertTrue( rule0.valueFired ) ;
+        assertTrue( rule0.hasFinished ) ;
+        assertEquals( BERDigester.NO_TOP_TAG, digester.getTopTag() ) ;
+        assertEquals( buf.get(0), rule0.buf.get(0) ) ;
+    }
+
+
+    /**
+     * Tests the when rules throw errors.
+     */
+    public void testErrorOnTag() throws Exception
+    {
+        digester.fireFinishEvent() ;
+
+        int[] pat0 = { 0x10000000 } ;
+        ErrorRule rule0 = new ErrorRule() ;
+        digester.addRule( pat0, rule0 ) ;
+        rule0.throwErrors() ;
+
+        ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+        buf.put( ( byte ) 0x10 ) ;
+        buf.flip() ;
+
+        try
+        {
+            digester.decode( buf ) ;
+        }
+        catch( Error e )
+        {
+            assertNotNull( e ) ;
+            return ;
+        }
+
+        fail( "should never get here" ) ;
+    }
+
+
+    public void testErrorOnLength() throws Exception
+    {
+        digester.fireFinishEvent() ;
+
+        int[] pat0 = { 0x10000000 } ;
+        ErrorRule rule0 = new ErrorRule() ;
+        digester.addRule( pat0, rule0 ) ;
+
+        ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+        buf.put( ( byte ) 0x10 ) ;
+        buf.flip() ;
+
+        digester.decode( buf ) ;
+
+        buf.rewind() ;
+        buf.put( ( byte ) 0x02 ) ;
+        buf.flip() ;
+
+        rule0.throwErrors() ;
+        try
+        {
+            digester.decode( buf ) ;
+        }
+        catch( Error e )
+        {
+            assertNotNull( e ) ;
+            return ;
+        }
+
+        fail( "should never get here" ) ;
+    }
+
+
+    public void testErrorOnValue() throws Exception
+    {
+        digester.fireFinishEvent() ;
+
+        int[] pat0 = { 0x10000000 } ;
+        ErrorRule rule0 = new ErrorRule() ;
+        digester.addRule( pat0, rule0 ) ;
+
+        ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+        buf.put( ( byte ) 0x10 ) ;
+        buf.flip() ;
+
+        digester.decode( buf ) ;
+
+        buf.rewind() ;
+        buf.put( ( byte ) 0x02 ) ;
+        buf.flip() ;
+
+        digester.decode( buf ) ;
+
+        buf.rewind() ;
+        buf.put( ( byte ) 0x02 ) ;
+        buf.flip() ;
+
+        rule0.throwErrors() ;
+        try
+        {
+            digester.decode( buf ) ;
+        }
+        catch( Error e )
+        {
+            assertNotNull( e ) ;
+            return ;
+        }
+
+        fail( "should never get here" ) ;
+    }
+
+
+    public void testErrorOnFinish() throws Exception
+    {
+        digester.fireFinishEvent() ;
+
+        int[] pat0 = { 0x10000000 } ;
+        ErrorRule rule0 = new ErrorRule() ;
+        digester.addRule( pat0, rule0 ) ;
+
+        ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+        buf.put( ( byte ) 0x10 ) ;
+        buf.flip() ;
+
+        digester.decode( buf ) ;
+
+        buf.rewind() ;
+        buf.put( ( byte ) 0x02 ) ;
+        buf.flip() ;
+
+        digester.decode( buf ) ;
+
+        buf.rewind() ;
+        buf.put( ( byte ) 0x07 ) ;
+        buf.flip() ;
+
+        digester.decode( buf ) ;
+
+        buf.rewind() ;
+        buf.put( ( byte ) 0x09 ) ;
+        buf.flip() ;
+
+        rule0.throwErrorOnFinish() ;
+        try
+        {
+            digester.decode( buf ) ;
+        }
+        catch( Error e )
+        {
+            assertNotNull( e ) ;
+            return ;
+        }
+
+        fail( "should never get here" ) ;
+    }
+
+
+    /**
+     * Tests the when rules throw errors.
+     */
+    public void testExceptionOnTag() throws Exception
+    {
+        digester.fireFinishEvent() ;
+
+        int[] pat0 = { 0x10000000 } ;
+        ExceptionRule rule0 = new ExceptionRule() ;
+        digester.addRule( pat0, rule0 ) ;
+        rule0.throwErrors() ;
+
+        ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+        buf.put( ( byte ) 0x10 ) ;
+        buf.flip() ;
+
+        try
+        {
+            digester.decode( buf ) ;
+        }
+        catch( RuntimeException e )
+        {
+            assertNotNull( e ) ;
+            return ;
+        }
+
+        fail( "should never get here" ) ;
+    }
+
+
+    public void testExceptionOnLength() throws Exception
+    {
+        digester.fireFinishEvent() ;
+
+        int[] pat0 = { 0x10000000 } ;
+        ExceptionRule rule0 = new ExceptionRule() ;
+        digester.addRule( pat0, rule0 ) ;
+
+        ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+        buf.put( ( byte ) 0x10 ) ;
+        buf.flip() ;
+
+        digester.decode( buf ) ;
+
+        buf.rewind() ;
+        buf.put( ( byte ) 0x02 ) ;
+        buf.flip() ;
+
+        rule0.throwErrors() ;
+        try
+        {
+            digester.decode( buf ) ;
+        }
+        catch( RuntimeException e )
+        {
+            assertNotNull( e ) ;
+            return ;
+        }
+
+        fail( "should never get here" ) ;
+    }
+
+
+    public void testExceptionOnValue() throws Exception
+    {
+        digester.fireFinishEvent() ;
+
+        int[] pat0 = { 0x10000000 } ;
+        ExceptionRule rule0 = new ExceptionRule() ;
+        digester.addRule( pat0, rule0 ) ;
+
+        ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+        buf.put( ( byte ) 0x10 ) ;
+        buf.flip() ;
+
+        digester.decode( buf ) ;
+
+        buf.rewind() ;
+        buf.put( ( byte ) 0x02 ) ;
+        buf.flip() ;
+
+        digester.decode( buf ) ;
+
+        buf.rewind() ;
+        buf.put( ( byte ) 0x02 ) ;
+        buf.flip() ;
+
+        rule0.throwErrors() ;
+        try
+        {
+            digester.decode( buf ) ;
+        }
+        catch( RuntimeException e )
+        {
+            assertNotNull( e ) ;
+            return ;
+        }
+
+        fail( "should never get here" ) ;
+    }
+
+
+    public void testExceptionOnFinish() throws Exception
+    {
+        digester.fireFinishEvent() ;
+
+        int[] pat0 = { 0x10000000 } ;
+        ExceptionRule rule0 = new ExceptionRule() ;
+        digester.addRule( pat0, rule0 ) ;
+
+        ByteBuffer buf = ByteBuffer.allocate( 1 ) ;
+        buf.put( ( byte ) 0x10 ) ;
+        buf.flip() ;
+
+        digester.decode( buf ) ;
+
+        buf.rewind() ;
+        buf.put( ( byte ) 0x02 ) ;
+        buf.flip() ;
+
+        digester.decode( buf ) ;
+
+        buf.rewind() ;
+        buf.put( ( byte ) 0x07 ) ;
+        buf.flip() ;
+
+        digester.decode( buf ) ;
+
+        buf.rewind() ;
+        buf.put( ( byte ) 0x09 ) ;
+        buf.flip() ;
+
+        rule0.throwErrorOnFinish() ;
+        try
+        {
+            digester.decode( buf ) ;
+        }
+        catch( RuntimeException e )
+        {
+            assertNotNull( e ) ;
+            return ;
+        }
+
+        fail( "should never get here" ) ;
+    }
+
+
+    public void testFullTlv() throws Exception
+    {
+        digester.fireFinishEvent() ;
+
+        int[] pat0 = { 0x10000000 } ;
+        int[] pat1 = { 0x11000000 } ;
+        CollectorRule rule0 = new CollectorRule() ;
+        CollectorRule rule1 = new CollectorRule() ;
+        digester.addRule( pat0, rule0 ) ;
+        digester.addRule( pat1, rule1 ) ;
+        byte[] bites = { 0x10, 0x02, 0x07, 0x09 } ;
+        ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+        digester.decode( buf ) ;
+    }
+
+
+    public void testNestedTlvs() throws Exception
+    {
+        digester.fireFinishEvent() ;
+
+        int[] pat0 = { 0x14000000, 0x10000000 } ;
+        int[] pat1 = { 0x11000000 } ;
+        CollectorRule rule0 = new CollectorRule() ;
+        CollectorRule rule1 = new CollectorRule() ;
+        digester.addRule( pat0, rule0 ) ;
+        digester.addRule( pat1, rule1 ) ;
+        byte[] bites = {
+            0x34, 0x04,                 // constructed id = 20 w/ 4 byte value
+            0x10, 0x02, 0x07, 0x09      // primitive id = 16 w/ 2 byte value
+        } ;
+        ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+        digester.decode( buf ) ;
+    }
+
+
+    public void testNestedTlvsWithErrors() throws Exception
+    {
+        digester.fireFinishEvent() ;
+
+        int[] pat0 = { 0x14000000, 0x10000000 } ;
+        int[] pat1 = { 0x11000000 } ;
+        ErrorRule rule0 = new ErrorRule() ;
+        ErrorRule rule1 = new ErrorRule() ;
+        digester.addRule( pat0, rule0 ) ;
+        digester.addRule( pat1, rule1 ) ;
+        byte[] bites = {
+            0x34, 0x04,                 // constructed id = 20 w/ 4 byte value
+            0x10, 0x02, 0x07            // primitive id = 16 w/ 2 byte value
+            // last byte missing
+        } ;
+        ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+        digester.decode( buf ) ;
+
+        rule1.throwErrorOnFinish() ;
+        buf = ByteBuffer.allocate( 1 ) ;
+        buf.put( 0, ( byte ) 0x09 ) ;
+
+        try
+        {
+            digester.decode( buf ) ;
+        }
+        catch( Error e )
+        {
+            assertNotNull( e ) ;
+            return ;
+        }
+
+        fail( "should never get here" ) ;
+    }
+
+
+    class MockRule extends AbstractRule {}
+
+
+    class CollectorRule extends AbstractRule
+    {
+        boolean tagFired = false ;
+        boolean lengthFired = false ;
+        boolean valueFired = false ;
+        int id = -1 ;
+        boolean isPrimitive = false ;
+        TypeClass typeClass = null ;
+        int length = -1 ;
+        ByteBuffer buf = null ;
+        boolean hasFinished = false ;
+
+        public void tag( int id, boolean isPrimitive, TypeClass typeClass )
+        {
+            this.id = id ;
+            this.typeClass = typeClass ;
+            this.isPrimitive = isPrimitive ;
+            this.tagFired = true ;
+        }
+
+        public void length( int length )
+        {
+            this.length = length ;
+            this.lengthFired = true ;
+        }
+
+        public void value( ByteBuffer buf )
+        {
+            this.buf = buf ;
+            this.valueFired = true ;
+        }
+
+        public void finish()
+        {
+            this.hasFinished = true ;
+        }
+    }
+
+
+    class ErrorRule extends AbstractRule
+    {
+        private boolean doThrow = false ;
+        private boolean doThrowOnFinish = false ;
+
+        public void throwErrors()
+        {
+            doThrow = true ;
+        }
+
+        public void throwErrorOnFinish()
+        {
+            doThrowOnFinish = true ;
+        }
+
+        public void tag( int id, boolean isPrimitive, TypeClass typeClass )
+        {
+            if ( doThrow )
+            {
+                throw new Error() ;
+            }
+        }
+
+        public void length( int length )
+        {
+            if ( doThrow )
+            {
+                throw new Error() ;
+            }
+        }
+
+        public void value( ByteBuffer buf )
+        {
+            if ( doThrow )
+            {
+                throw new Error() ;
+            }
+        }
+
+        public void finish()
+        {
+            if ( doThrowOnFinish )
+            {
+                throw new Error() ;
+            }
+        }
+    }
+
+
+    class ExceptionRule extends AbstractRule
+    {
+        private boolean doThrow = false ;
+        private boolean doThrowOnFinish = false ;
+
+        public void throwErrors()
+        {
+            doThrow = true ;
+        }
+
+        public void throwErrorOnFinish()
+        {
+            doThrowOnFinish = true ;
+        }
+
+        public void tag( int id, boolean isPrimitive, TypeClass typeClass )
+        {
+            if ( doThrow )
+            {
+                throw new RuntimeException() ;
+            }
+        }
+
+        public void length( int length )
+        {
+            if ( doThrow )
+            {
+                throw new RuntimeException() ;
+            }
+        }
+
+        public void value( ByteBuffer buf )
+        {
+            if ( doThrow )
+            {
+                throw new RuntimeException() ;
+            }
+        }
+
+        public void finish()
+        {
+            if ( doThrowOnFinish )
+            {
+                throw new RuntimeException() ;
+            }
+        }
     }
 }

Modified: incubator/directory/snickers/trunk/maven.xml
==============================================================================
--- incubator/directory/snickers/trunk/maven.xml	(original)
+++ incubator/directory/snickers/trunk/maven.xml	Sun Apr  4 21:14:48 2004
@@ -40,7 +40,7 @@
   <goal name="buildall" description="Build All Jar Files And Intall">
     <ant:delete dir="${basedir}/target/classes"/>
     <maven:reactor basedir="${basedir}" postProcessing="true" 
-      includes="**/project.xml" excludes="./project.xml" goals="jar:install" 
+      includes="**/project.xml" excludes="project.xml" goals="jar:install" 
       banner="Building" ignoreFailures="false"/>
   </goal>
   

Modified: incubator/directory/snickers/trunk/project.xml
==============================================================================
--- incubator/directory/snickers/trunk/project.xml	(original)
+++ incubator/directory/snickers/trunk/project.xml	Sun Apr  4 21:14:48 2004
@@ -2,7 +2,7 @@
 
 <project>
     <groupId>incubator-directory</groupId>
-    <id>null</id>
+    <id>snickers</id>
     <name>Snickers ASN.1 Tools</name>
     <package>org.apache.snickers</package>
 

Mime
View raw message