directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject svn commit: rev 21096 - in incubator/directory/snickers/branches/ber-decoder/ber-codec/src: java/org/apache/snickers/ber test/org/apache/snickers/ber
Date Fri, 11 Jun 2004 14:03:05 GMT
Author: adc
Date: Fri Jun 11 07:03:04 2004
New Revision: 21096

Added:
   incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/AbstractDecoderTestCase.java
   incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/BERTestDecoder.java
Modified:
   incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/BERDecoder.java
Log:
Moved accessor methods from BERDecoder to BERTestDecoder.  People should not have access to
these internal fields for every day use.

Modified: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/BERDecoder.java
==============================================================================
--- incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/BERDecoder.java
(original)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/java/org/apache/snickers/ber/BERDecoder.java
Fri Jun 11 07:03:04 2004
@@ -17,7 +17,6 @@
 package org.apache.snickers.ber;
 
 import java.nio.ByteBuffer;
-import java.util.EmptyStackException;
 
 import org.apache.commons.codec.DecoderException;
 import org.apache.commons.codec.stateful.DecoderCallback;
@@ -68,9 +67,6 @@
      */
     Tuple tlv;
 
-    Tag tag = new Tag();
-    Length length = new Length();
-
     /**
      * stack of nested/constructed TLV tuples
      */
@@ -91,29 +87,16 @@
     }
 
     /**
-     * Obtain the current incomplete tuple that is on the top of the stack.
+     * Obtain the current state of the decoder.<p/>
      *
-     * @return the current incomplete tuple that is on the top of the stack
-     */
-    public Tuple getCurrentTuple()
-    {
-        return tlv;
-//        try
-//        {
-//            return stack.peek();
-//        }
-//        catch ( EmptyStackException ese )
-//        {
-//            return tlv;
-//        }
-    }
-
-    /**
-     * Obtain the current state of the decoder.
+     * This accessor is protected and is intended to be used by testing classes
+     * that will subclass off of this decoder.  All other uses are strongly
+     * discouraged.
      *
      * @return the current state of the decoder
+     * @deprecated
      */
-    public State getState()
+    protected State getState()
     {
         return state;
     }
@@ -164,7 +147,6 @@
 
     public abstract class State
     {
-
         /**
          * Decodes a peice of encoded data.  The nature of this call, synchronous
          * verses asynchonous, with respect to driving the actual decoding of the
@@ -180,6 +162,8 @@
 
     protected class TagState extends State
     {
+        Tag tag = new Tag();
+
         /**
          * Decodes a peice of encoded data.  The nature of this call, synchronous
          * verses asynchonous, with respect to driving the actual decoding of the
@@ -219,6 +203,8 @@
 
     protected class LengthState extends State
     {
+        Length length = new Length();
+
         /**
          * Decodes a peice of encoded data.  The nature of this call, synchronous
          * verses asynchonous, with respect to driving the actual decoding of the

Added: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/AbstractDecoderTestCase.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/AbstractDecoderTestCase.java
Fri Jun 11 07:03:04 2004
@@ -0,0 +1,468 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed 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.snickers.ber ;
+
+
+import java.nio.ByteBuffer ;
+import java.util.ArrayList ;
+
+import org.apache.commons.codec.binary.Binary ;
+import org.apache.commons.codec.DecoderException ;
+import org.apache.commons.codec.stateful.DecoderMonitor ;
+import org.apache.commons.codec.stateful.DecoderCallback ;
+import org.apache.commons.codec.stateful.StatefulDecoder ;
+
+import org.apache.commons.lang.exception.ExceptionUtils ;
+
+import junit.framework.TestCase ;
+
+
+/**
+ * Tests the BER decoder.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 9766 $
+ */
+public abstract class AbstractDecoderTestCase extends TestCase 
+    implements BERDecoderCallback, DecoderMonitor
+{
+    /** list of encountered TLV's as we recieve completed decode callbacks */
+    protected ArrayList tlvList = new ArrayList() ;
+    /** the decoder that is constructed every time */
+    protected BERTestDecoder decoder = null ;
+    /** value accumulator */
+    ByteBuffer buffer = ByteBuffer.allocate(11111) ;
+    /** stack counter */
+    int level;
+    /** expected state */
+    BERDecoder.State expectedState;
+
+    /*
+     * @see TestCase#setUp()
+     */
+    protected void setUp() throws Exception
+    {
+        super.setUp() ;
+        decoder = new BERTestDecoder() ;
+        decoder.setCallback( this ) ;
+        decoder.setDecoderMonitor( this ) ;
+        level = 0;
+        buffer.clear();
+    }
+
+    
+    /*
+     * @see TestCase#tearDown()
+     */
+    protected void tearDown() throws Exception
+    {
+        super.tearDown() ;
+        tlvList.clear() ;
+        decoder = null ;
+    }
+
+    
+    /**
+     * Constructor for BERDecoderTest.
+     * @param arg0
+     */
+    public AbstractDecoderTestCase( String arg0 )
+    {
+        super( arg0 ) ;
+    }
+
+    /**
+     * Set the expected state when tuple is decoded
+     * @param expectedState the xpected state when tuple is decoded
+     */
+    public void setExpectedState( BERDecoder.State expectedState )
+    {
+        this.expectedState = expectedState;
+    }
+
+    /**
+     * Fragments an array of bytes into multiple arrays 'attempting' to keep 
+     * them the same size however the last fragment will be an array 
+     * bites.length%size which may or may not be of the requested fragment size.
+     * However it will never be greater.  Use this method to break appart TLV 
+     * byte arrays to feed them into the decoder for testing.
+     * 
+     * @param bites the bites to fragment
+     * @param size the maximum size of a fragment
+     * @return the array of byte[] fragments
+     */
+    public byte[][] fragment( byte[] bites, int size )
+    {
+        byte[][] fragments = null ;
+        
+        if ( size <= 0 )
+        {    
+            throw new IllegalArgumentException( 
+                    "fragment size should be 1 or more but was " + size ) ;
+        }
+        
+        int wholeFrags = bites.length/size ;
+        int partialFragSize = bites.length % size ; 
+
+        /*
+         * Allocate what we need depending on the size of our remainder
+         */
+        if ( partialFragSize == 0 ) 
+        {
+            fragments = new byte[wholeFrags][] ;
+        }
+        else
+        {
+            fragments = new byte[wholeFrags+1][] ;
+            fragments[wholeFrags] = new byte[partialFragSize] ;
+        }
+        
+        for ( int ii = 0; ii < wholeFrags; ii++ )
+        {
+            fragments[ii] = new byte[size] ;
+            System.arraycopy( bites, ii * size, fragments[ii], 0, size ) ;
+        }
+        
+        if ( partialFragSize != 0 )
+        {
+            int srcPos = wholeFrags * size ;
+            byte[] src = fragments[wholeFrags] ;
+            System.arraycopy( bites, srcPos, src, 0, partialFragSize ) ;
+        }
+        
+        return fragments ;
+    }
+    
+    
+    /**
+     * Fragments a byte buffer into multiple buffer 'attempting' to keep 
+     * them the same size however the last fragment will be an array 
+     * bites.length%size which may or may not be of the requested fragment size.
+     * However it will never be greater.  Use this method to break appart TLV 
+     * bytes to feed them into the decoder for testing.
+     * 
+     * @param bites the bites to fragment
+     * @param size the maximum size of a fragment
+     * @return the buffer fragment
+     */
+    public ByteBuffer[] fragment( ByteBuffer bites, int size )
+    {
+        bites = bites.duplicate() ;
+        ByteBuffer[] fragments = null ;
+        
+        if ( size <= 0 )
+        {    
+            throw new IllegalArgumentException( 
+                    "fragment size should be 1 or more but was " + size ) ;
+        }
+        
+        int wholeFrags = bites.remaining()/size ;
+        int partialFragSize = bites.remaining() % size ; 
+
+        /*
+         * Allocate what we need depending on the size of our remainder
+         */
+        if ( partialFragSize == 0 ) 
+        {
+            fragments = new ByteBuffer[wholeFrags] ;
+        }
+        else
+        {
+            fragments = new ByteBuffer[wholeFrags+1] ;
+            fragments[wholeFrags] = ByteBuffer.allocate( partialFragSize ) ;
+        }
+        
+        for ( int ii = 0; ii < wholeFrags; ii++ )
+        {
+            fragments[ii] = ( ByteBuffer ) bites.slice().limit( size ) ;
+            bites.position( bites.position() + size ) ;
+        }
+        
+        if ( partialFragSize != 0 )
+        {
+            fragments[wholeFrags].put( bites ) ;
+            fragments[wholeFrags].flip() ;
+        }
+        
+        return fragments ;
+    }
+    
+    
+    /**
+     * BER decodes a string of 0's and 1's.
+     * 
+     * @param bitString a string of ascii 0's and 1's
+     * @return a copy of the decoded tuple or the partially decoded current tlv
+     * @throws DecoderException if there are errors while decoding.
+     */
+    public Tuple decode( String bitString ) throws DecoderException
+    {
+        byte [] bites = Binary.fromAscii( bitString.getBytes() ) ;
+        ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+        int lastSize = tlvList.size() ;
+        decoder.decode( buf ) ;
+        
+        if ( tlvList.isEmpty() || tlvList.size() == lastSize )
+        {
+            return decoder.getCurrentTuple() ;
+        }
+        
+        return ( Tuple ) tlvList.get( tlvList.size() - 1 ) ;
+    }
+
+    
+    /**
+     * BER decodes a single byte.
+     * 
+     * @param bite a single byte to decode
+     * @return a copy of the decoded tuple or the partially decoded current tlv
+     * @throws DecoderException if there are errors while decoding.
+     */
+    public Tuple decode( byte bite ) throws DecoderException
+    {
+        byte [] bites = { bite } ;
+        ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+        int lastSize = tlvList.size() ;
+        decoder.decode( buf ) ;
+        
+        if ( tlvList.isEmpty() || tlvList.size() == lastSize )
+        {
+            return decoder.getCurrentTuple() ;
+        }
+        
+        return ( Tuple ) tlvList.get( tlvList.size() - 1 ) ;
+    }
+
+    
+    /**
+     * BER decodes a byte array.
+     * 
+     * @param bites a byte array to decode
+     * @return a copy of the decoded tuple or the partially decoded current tlv
+     * @throws DecoderException if there are errors while decoding.
+     */
+    public Tuple decode( byte[] bites ) throws DecoderException
+    {
+        ByteBuffer buf = ByteBuffer.wrap( bites ) ;
+        int lastSize = tlvList.size() ;
+        decoder.decode( buf ) ;
+        
+        if ( tlvList.isEmpty() || tlvList.size() == lastSize )
+        {
+            return decoder.getCurrentTuple() ;
+        }
+        
+        return ( Tuple ) tlvList.get( tlvList.size() - 1 ) ;
+    }
+
+    
+    /**
+     * BER decodes a byte buffer.
+     * 
+     * @param bites a byte buffer to decode
+     * @return a copy of the decoded tuple or the partially decoded current tlv
+     * @throws DecoderException if there are errors while decoding.
+     */
+    public Tuple decode( ByteBuffer bites ) throws DecoderException
+    {
+        int lastSize = tlvList.size() ;
+        decoder.decode( bites ) ;
+        
+        if ( tlvList.isEmpty() || tlvList.size() == lastSize )
+        {
+            return decoder.getCurrentTuple() ;
+        }
+        
+        return ( Tuple ) tlvList.get( tlvList.size() - 1 ) ;
+    }
+
+    
+    /**
+     * First BER encodes then BER decodes a Tuple.
+     * 
+     * @param tlv a tuple to encode then decode
+     * @return a copy of the decoded tuple or the partially decoded current tlv
+     * @throws DecoderException if there are errors while decoding
+     */
+    public Tuple decode( Tuple tlv, ByteBuffer value ) throws DecoderException
+    {
+        ArrayList list = new ArrayList() ;
+        list.add( value ) ;
+        ByteBuffer buf = tlv.toEncodedBuffer( list ) ;
+        int lastSize = tlvList.size() ;
+        decoder.decode( buf ) ;
+        
+        if ( tlvList.isEmpty() || tlvList.size() == lastSize )
+        {
+            return decoder.getCurrentTuple() ;
+        }
+        
+        return ( Tuple ) tlvList.get( tlvList.size() - 1 ) ;
+    }
+
+    
+    /**
+     * First BER encodes then BER decodes a train of Tuples.
+     * 
+     * @param tlvs a tuple array to encode then decode
+     * @return a copy of the decoded tuple or the partially decoded current tlv
+     * @throws DecoderException if there are errors while decoding
+     */
+    public Tuple decode( Tuple[] tlvs, ByteBuffer[] values ) 
+        throws DecoderException
+    {
+        int lastSize = tlvList.size() ;
+
+        for ( int ii = 0; ii < tlvs.length; ii++ )
+        {
+            decode( tlvs[ii], values[ii] ) ;
+        }
+        
+        if ( tlvList.isEmpty() || tlvList.size() == lastSize )
+        {
+            return decoder.getCurrentTuple() ;
+        }
+        
+        return ( Tuple ) tlvList.get( tlvList.size() - 1 ) ;
+    }
+    
+    /* (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 )
+    {
+        tlvList.add( decoded ) ;
+        assertEquals( expectedState, this.decoder.getState() ) ;
+        buffer.flip() ;
+    }
+    
+    
+    /* (non-Javadoc)
+     * @see org.apache.commons.codec.stateful.DecoderMonitor#callbackOccured(
+     * org.apache.commons.codec.stateful.StatefulDecoder, 
+     * org.apache.commons.codec.stateful.DecoderCallback, java.lang.Object)
+     */
+    public void callbackOccured( StatefulDecoder decoder, DecoderCallback cb,
+								 Object decoded )
+    {
+        assertEquals( this, cb ) ;
+        assertEquals( this.decoder, decoder ) ;
+        Tuple t = ( Tuple ) decoded ;
+        assertNotNull( t ) ;
+        if ( t.isPrimitive )
+        {    
+            assertTrue( this.decoder.getCurrentTuple().equals( decoded ) ) ;
+        }
+    }
+
+    
+    /* (non-Javadoc)
+     * @see org.apache.commons.codec.stateful.DecoderMonitor#callbackSet(
+     * org.apache.commons.codec.stateful.StatefulDecoder, 
+     * org.apache.commons.codec.stateful.DecoderCallback, 
+     * org.apache.commons.codec.stateful.DecoderCallback)
+     */
+    public void callbackSet( StatefulDecoder decoder, DecoderCallback oldcb,
+							 DecoderCallback newcb )
+    {
+        assertEquals( this, newcb ) ;
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.commons.codec.stateful.DecoderMonitor#error(
+     * org.apache.commons.codec.stateful.StatefulDecoder, java.lang.Exception)
+     */
+    public void error( StatefulDecoder decoder, Exception exception )
+    {
+        fail( ExceptionUtils.getFullStackTrace( exception ) ) ;
+    }
+
+    
+    /* (non-Javadoc)
+     * @see org.apache.commons.codec.stateful.DecoderMonitor#fatalError(
+     * org.apache.commons.codec.stateful.StatefulDecoder, java.lang.Exception)
+     */
+    public void fatalError( StatefulDecoder decoder, Exception exception )
+    {
+        fail( ExceptionUtils.getFullStackTrace( exception ) ) ;
+    }
+
+    
+    /* (non-Javadoc)
+     * @see org.apache.commons.codec.stateful.DecoderMonitor#monitorSet(
+     * org.apache.commons.codec.stateful.StatefulDecoder, 
+     * org.apache.commons.codec.stateful.DecoderMonitor)
+     */
+    public void monitorSet( StatefulDecoder decoder, DecoderMonitor oldmon )
+    {
+        assertEquals( this, oldmon ) ;
+    }
+
+    
+    /* (non-Javadoc)
+     * @see org.apache.commons.codec.stateful.DecoderMonitor#warning(
+     * org.apache.commons.codec.stateful.StatefulDecoder, java.lang.Exception)
+     */
+    public void warning( StatefulDecoder decoder, Exception exception )
+    {
+        assertNotNull( exception ) ;
+    }
+
+    /**
+     * Method used to receive notification that a TLV tuple has begun to be
+     * read.  The following tag properties of the TLV tuple are valid at this
+     * point:
+     * <ul>
+     * <li>id</li>
+     * <li>isPrimitive</li>
+     * <li>typeClass</li>
+     * <li>length</li>
+     * </ul>
+     *
+     * @param tlv the TLV tuple
+     */
+    public void start( Tuple tlv )
+    {
+        assertNotNull( tlv );
+        level++;
+        buffer.clear();
+    }
+
+    /**
+     * Method used to provide the possibly partial contents of a TLV value.
+     *
+     * @param buf the possibly partial contents of a TLV value
+     */
+    public void contents( ByteBuffer buf )
+    {
+        assertNotNull( buf );
+        assertTrue( level > 0 );
+        buffer.put( buf );
+    }
+
+    /**
+     * Method used to provide notification that a TLV tuple has been read.
+     */
+    public void end()
+    {
+        assertTrue( level > 0 );
+        level--;
+    }
+}

Added: incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/BERTestDecoder.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/branches/ber-decoder/ber-codec/src/test/org/apache/snickers/ber/BERTestDecoder.java
Fri Jun 11 07:03:04 2004
@@ -0,0 +1,51 @@
+/**
+ *
+ * Copyright 2004 The Apache Software Foundation
+ *
+ *  Licensed 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.snickers.ber;
+
+/**
+ * @version $Revision: $ $Date: $
+ */
+public class BERTestDecoder extends BERDecoder
+{
+    /**
+     * Obtain the current state of the decoder.
+     *
+     * @return the current state of the decoder
+     */
+    public State getState()
+    {
+        return super.getState();
+    }
+
+    /**
+     * Obtain the current incomplete tuple that is on the top of the stack.
+     *
+     * @return the current incomplete tuple that is on the top of the stack
+     */
+    public Tuple getCurrentTuple()
+    {
+        return tlv;
+//        try
+//        {
+//            return stack.peek();
+//        }
+//        catch ( EmptyStackException ese )
+//        {
+//            return tlv;
+//        }
+    }
+}

Mime
View raw message