directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: rev 30884 - in incubator/directory/snickers/trunk: ber-codec/src/java/org/apache/snickers/ber/digester/rules ldap-ber-provider/src/java/org/apache/snickers/ldap ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder ldap-ber-provider/src/test/org/apache/snickers/ldap
Date Wed, 28 Jul 2004 23:15:48 GMT
Author: akarasulu
Date: Wed Jul 28 16:15:47 2004
New Revision: 30884

Added:
   incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/SnickersDecoder.java
  (contents, props changed)
   incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/SnickersEncoder.java
  (contents, props changed)
   incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/SnickersProvider.java
  (contents, props changed)
   incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/SnickersTransformer.java
  (contents, props changed)
   incubator/directory/snickers/trunk/ldap-ber-provider/src/test/org/apache/snickers/ldap/SnickersDecoderTest.java
  (contents, props changed)
   incubator/directory/snickers/trunk/ldap-ber-provider/src/test/org/apache/snickers/ldap/SnickersEncoderTest.java
  (contents, props changed)
Modified:
   incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/digester/rules/ByteAccumulator.java
   incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/SnickersLdapEncoder.java
Log:
Commit changes ...

 Generally we're preparing to now insert the Snickers stuff 
 as a provider back into the server.  The whole provider is 
 not yet complete but we are close.  We will add PDU support
 one by one until everything is ready.

 o made ByteAccumulator constructor public we needed it as a
   utility class but only temporarily
 o added SnickersProvider implementation (nothing to test 
   really)
 o added SnickersTransformer implementation (nothing to test)
   this class is only a passthro transformer that simply returns
   the inputs.
 o added SnickersEncoder and tested implementation
 o added SnickersDecoder and tested implementation



Modified: incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/digester/rules/ByteAccumulator.java
==============================================================================
--- incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/digester/rules/ByteAccumulator.java
(original)
+++ incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/digester/rules/ByteAccumulator.java
Wed Jul 28 16:15:47 2004
@@ -53,7 +53,7 @@
      * Creates a ByteAccumulator used to gather bytes from various sources
      * with a default initial size and a default growth increment.
      */
-    ByteAccumulator()
+    public ByteAccumulator()
     {
         bs = ArrayUtils.EMPTY_BYTE_ARRAY ;
         pos = 0 ;

Added: incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/SnickersDecoder.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/SnickersDecoder.java
Wed Jul 28 16:15:47 2004
@@ -0,0 +1,203 @@
+/*
+ *   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.ldap;
+
+
+import org.apache.ldap.common.message.spi.DecoderSpi;
+import org.apache.ldap.common.message.spi.ProviderException;
+import org.apache.ldap.common.message.spi.Provider;
+import org.apache.ldap.common.message.Message;
+
+import org.apache.snickers.ber.digester.BERDigester;
+import org.apache.commons.codec.stateful.DecoderCallback;
+import org.apache.commons.codec.stateful.StatefulDecoder;
+import org.apache.snickers.ldap.decoder.LdapDigesterFactory;
+
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+
+
+/**
+ * A Snickers based LDAP PDU decoder.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory
+ *         Project</a> $Rev$
+ */
+public class SnickersDecoder implements DecoderSpi
+{
+    private static final int BUFSZ = 128;
+
+    private final Provider provider;
+    private final BERDigester digester;
+    private final DigesterCallback cb;
+
+
+    // ------------------------------------------------------------------------
+    // Constructors
+    // ------------------------------------------------------------------------
+
+
+    /**
+     * Creates an instance of a Snickers DecoderSpi implementation.
+     *
+     * @param provider the owning provider.
+     */
+    public SnickersDecoder( Provider provider )
+    {
+        this.provider = provider;
+        LdapDigesterFactory factory = LdapDigesterFactory.getSingleton();
+        digester = factory.create();
+        cb = new DigesterCallback();
+        digester.setCallback( cb );
+    }
+
+
+    /**
+     * Gets the Provider that this DecoderSpi implementation is part of.
+     *
+     * @return the owning provider.
+     */
+    public Provider getProvider()
+    {
+        return provider ;
+    }
+
+
+    // ------------------------------------------------------------------------
+    // DecoderSpi Interface Method Implementations
+    // ------------------------------------------------------------------------
+
+
+    /**
+     * Decodes a PDU from an input stream into a Snickers compiler generated
+     * stub envelope.
+     *
+     * @param lock lock object used to exclusively read from the input stream
+     * @param in the input stream to read and decode PDU bytes from
+     * @return org.apache.ldap.common.berlib.snacc.ldap_v3.LDAPMessage snacc stub instance
+     */
+    public Object decode( Object lock, InputStream in )
+        throws ProviderException
+    {
+        if( lock == null )
+        {
+            digest( in );
+            return cb.getMessage() ;
+        }
+
+		try
+        {
+			// Synchronize on the input lock object to prevent concurrent reads
+			synchronized ( lock )
+            {
+				digest( in );
+
+				// Notify/awaken threads waiting to read from input stream
+				lock.notifyAll() ;
+			}
+		}
+        catch( Exception e )
+        {
+			//getLogger().debug("Decoder failure: ", e) ;
+			ProviderException pe =  new ProviderException( provider,
+                "Snickers decoder failure!" ) ;
+            pe.addThrowable( e ) ;
+            throw pe ;
+		}
+
+        return cb.getMessage() ;
+    }
+
+
+    /**
+     * Feeds the bytes within the input stream to the digester to generate the
+     * resultant decoded Message.
+     *
+     * @param in
+     * @throws ProviderException
+     */
+    private void digest( InputStream in ) throws ProviderException
+    {
+        byte[] buf = null;
+
+        try
+        {
+            int amount = -1;
+            do
+            {
+                buf = new byte[BUFSZ];
+                amount = in.read( buf );
+
+                if ( amount == -1 )
+                {
+                    break;
+                }
+
+                digester.decode( ByteBuffer.wrap( buf ) );
+            } while( amount >= 0 );
+        }
+        catch( Exception e )
+        {
+            //getLogger().debug("Decoder failure: ", e) ;
+            ProviderException pe =  new ProviderException( provider,
+                "Snickers decoder failure!" ) ;
+            pe.addThrowable( e ) ;
+            throw pe ;
+        }
+    }
+
+
+    class DigesterCallback implements DecoderCallback
+    {
+        /** the message we recieved via the callback */
+        private Message msg;
+
+        /**
+         * Callback to deliver a fully decoded object.
+         *
+         * @param decoder the stateful decoder driving the callback
+         * @param decoded the object that was decoded
+         */
+        public void decodeOccurred( StatefulDecoder decoder, Object decoded )
+        {
+            msg = ( Message ) decoded;
+        }
+
+
+        /**
+         * Gets and clears the message reference to the most recently delivered
+         * (decoded) message.  If no Message was decoded and the message is null
+         * then an exception is raised.
+         *
+         * @return the last decoded Message
+         * @throws ProviderException if nothin was decoded since the last call
+         */
+        Message getMessage() throws ProviderException
+        {
+            if ( msg == null )
+            {
+                throw new ProviderException( provider,
+                        "Callback did not receive a message as expected from " +
+                        "the Snickers BERDigester" );
+            }
+
+            Message tmp = msg;
+            msg = null;
+            return tmp;
+        }
+    }
+}

Added: incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/SnickersEncoder.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/SnickersEncoder.java
Wed Jul 28 16:15:47 2004
@@ -0,0 +1,172 @@
+/*
+ *   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.ldap;
+
+import org.apache.ldap.common.message.spi.EncoderSpi;
+import org.apache.ldap.common.message.spi.ProviderException;
+import org.apache.ldap.common.message.spi.Provider;
+import org.apache.commons.codec.stateful.EncoderCallback;
+import org.apache.commons.codec.stateful.StatefulEncoder;
+import org.apache.commons.codec.EncoderException;
+import org.apache.snickers.ldap.encoder.SnickersLdapEncoder;
+import org.apache.snickers.ber.digester.rules.ByteAccumulator;
+
+import java.io.OutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.WritableByteChannel;
+import java.nio.channels.Channels;
+
+
+/**
+ * Snickers LDAP BER provider's encoder.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory
+ *         Project</a> $Rev$
+ */
+public class SnickersEncoder implements EncoderSpi
+{
+    private final Provider provider;
+    private final OutputCallback outCb;
+    private final AccumulatorCallback accCb;
+    private final SnickersLdapEncoder encoder;
+
+
+    public SnickersEncoder( Provider provider )
+    {
+        this.provider = provider;
+        outCb = new OutputCallback();
+        accCb = new AccumulatorCallback();
+        encoder = new SnickersLdapEncoder();
+        encoder.setCallback( outCb );
+    }
+
+
+    public void encode( Object lock, OutputStream out, Object obj )
+            throws ProviderException
+    {
+        synchronized( encoder )
+        {
+            outCb.attach( out );
+            encoder.setCallback( outCb );
+
+            try
+            {
+                encoder.encode( obj );
+            }
+            catch ( EncoderException e )
+            {
+                ProviderException pe = new ProviderException( provider,
+                        "Snickers encoder failed to encode object: " + obj );
+                throw pe;
+            }
+        }
+    }
+
+
+    public byte[] encode( Object obj ) throws ProviderException
+    {
+        synchronized( encoder )
+        {
+            encoder.setCallback( accCb );
+
+            try
+            {
+                encoder.encode( obj );
+            }
+            catch ( EncoderException e )
+            {
+                ProviderException pe = new ProviderException( provider,
+                        "Snickers encoder failed to encode object: " + obj );
+                throw pe;
+            }
+
+            return BufferUtils.getArray( accCb.getEncoded() );
+        }
+    }
+
+
+    /**
+     * Gets the Provider associated with this SPI implementation object.
+     *
+     * @return Provider.
+     */
+    public Provider getProvider()
+    {
+        return provider;
+    }
+
+
+    class AccumulatorCallback implements EncoderCallback
+    {
+        ByteAccumulator accumulator = new ByteAccumulator();
+
+        /**
+         * Callback to deliver a fully encoded object.
+         *
+         * @param encoder the stateful encoder driving the callback
+         * @param encoded the object that was encoded
+         */
+        public void encodeOccurred( StatefulEncoder encoder, Object encoded )
+        {
+             accumulator.fill( ( ByteBuffer ) encoded );
+        }
+
+
+        ByteBuffer getEncoded()
+        {
+            return accumulator.drain();
+        }
+    }
+
+
+    class OutputCallback implements EncoderCallback
+    {
+        private WritableByteChannel channel = null;
+
+
+        /**
+         * Callback to deliver a fully encoded object.
+         *
+         * @param encoder the stateful encoder driving the callback
+         * @param encoded the object that was encoded
+         */
+        public void encodeOccurred( StatefulEncoder encoder, Object encoded )
+        {
+            try
+            {
+                channel.write( ( ByteBuffer ) encoded );
+            }
+            catch ( IOException e )
+            {
+                e.printStackTrace();
+            }
+        }
+
+
+        void attach( WritableByteChannel channel )
+        {
+            this.channel = channel;
+        }
+
+
+        void attach( OutputStream out )
+        {
+            this.channel = Channels.newChannel( out ) ;
+        }
+    }
+}

Added: incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/SnickersProvider.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/SnickersProvider.java
Wed Jul 28 16:15:47 2004
@@ -0,0 +1,84 @@
+/*
+ *   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.ldap;
+
+import org.apache.ldap.common.message.spi.*;
+
+
+/**
+ * The Snickers specific BER provider for LDAP.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory
+ *         Project</a> $Rev$
+ */
+public class SnickersProvider extends Provider
+{
+    private final SnickersDecoder decoder;
+    private final SnickersEncoder encoder;
+    private final SnickersTransformer transformer;
+
+
+    /**
+     * Creates an instance of a Snickers based LDAP BER Provider.
+     */
+    public SnickersProvider()
+    {
+        super( "Snickers LDAP BER Provider", "Apache Directory Project" );
+        decoder = new SnickersDecoder( this );
+        encoder = new SnickersEncoder( this );
+        transformer = new SnickersTransformer( this );
+    }
+
+
+    /**
+     * Gets the encoder associated with this provider.
+     *
+     * @return the provider's encoder.
+     * @throws org.apache.ldap.common.message.spi.ProviderException
+     *          if the provider or its encoder cannot be found
+     */
+    public EncoderSpi getEncoder() throws ProviderException
+    {
+        return encoder;
+    }
+
+
+    /**
+     * Gets the decoder associated with this provider.
+     *
+     * @return the provider's decoder.
+     * @throws org.apache.ldap.common.message.spi.ProviderException
+     *          if the provider or its decoder cannot be found
+     */
+    public DecoderSpi getDecoder() throws ProviderException
+    {
+        return decoder;
+    }
+
+
+    /**
+     * Gets the transformer associated with this provider.
+     *
+     * @return the provider's transformer.
+     * @throws org.apache.ldap.common.message.spi.ProviderException
+     *          if the provider or its transformer cannot be found
+     */
+    public TransformerSpi getTransformer() throws ProviderException
+    {
+        return transformer;
+    }
+}

Added: incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/SnickersTransformer.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/SnickersTransformer.java
Wed Jul 28 16:15:47 2004
@@ -0,0 +1,85 @@
+/*
+ *   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.ldap;
+
+
+import org.apache.ldap.common.message.Message;
+import org.apache.ldap.common.message.spi.Provider;
+import org.apache.ldap.common.message.spi.TransformerSpi;
+
+
+/**
+ * A do nothing Message transformer.  When the Snickers provider is used there
+ * is no need to transform Messages to and from a provider stub.  The provider
+ * stubs are messages themselves.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory
+ *         Project</a> $Rev$
+ */
+public class SnickersTransformer implements TransformerSpi
+{
+    /** the provider this transformer is part of */
+    private final SnickersProvider provider;
+
+
+    /**
+     * Creates a passthrough transformer that really does nothing at all.
+     *
+     * @param provider the povider for this transformer
+     */
+    public SnickersTransformer( SnickersProvider provider )
+    {
+        this.provider = provider;
+    }
+
+
+    /**
+     * Gets the Provider associated with this SPI implementation object.
+     *
+     * @return Provider.
+     */
+    public Provider getProvider()
+    {
+        return provider;
+    }
+
+
+    /**
+     * Returns the object passed in as is.  There is no need for a
+     * transformation when Snickers is used.
+     *
+     * @param obj the object to transform
+     * @return the object cast to a Message without any transformation at all
+     */
+    public Message transform( Object obj )
+    {
+        return ( Message ) obj;
+    }
+
+
+    /**
+     * Returns the Message passed in as is.  There is no need for a
+     * transformation when Snickers is used.
+     *
+     * @param msg the message to transform
+     * @return the same msg arg untouched
+     */
+    public Object transform( Message msg )
+    {
+        return msg;
+    }
+}

Modified: incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/SnickersLdapEncoder.java
==============================================================================
--- incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/SnickersLdapEncoder.java
(original)
+++ incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/SnickersLdapEncoder.java
Wed Jul 28 16:15:47 2004
@@ -21,10 +21,13 @@
 import org.apache.commons.codec.stateful.StatefulEncoder;
 import org.apache.commons.codec.stateful.EncoderCallback;
 import org.apache.commons.codec.stateful.EncoderMonitor;
-import org.apache.ldap.common.message.Message;
-import org.apache.ldap.common.message.MessageTypeEnum;
-import org.apache.ldap.common.message.AbandonRequest;
+import org.apache.ldap.common.message.*;
 import org.apache.snickers.ldap.encoder.abandon.AbandonRequestEncoder;
+import org.apache.snickers.ldap.encoder.bind.BindRequestEncoder;
+import org.apache.snickers.ldap.encoder.bind.BindResponseEncoder;
+import org.apache.snickers.ldap.encoder.search.SearchRequestEncoder;
+import org.apache.snickers.ldap.encoder.search.SearchResponseDoneEncoder;
+import org.apache.snickers.ldap.encoder.search.SearchResponseEntryEncoder;
 import org.apache.snickers.ber.BEREncoder;
 
 
@@ -38,7 +41,13 @@
  */
 public class SnickersLdapEncoder implements StatefulEncoder
 {
-    AbandonRequestEncoder abandonReqEncoder = new AbandonRequestEncoder();
+    AbandonRequestEncoder abandonReqEncoder = null;
+    BindRequestEncoder bindReqEncoder = null;
+    BindResponseEncoder bindRespEncoder = null;
+    SearchRequestEncoder searchReqEncoder = null;
+    SearchResponseDoneEncoder searchRespDoneEncoder = null;
+    SearchResponseEntryEncoder searchRespEntryEncoder = null;
+
     EncoderMonitor monitor;
     BEREncoder encoder = new BEREncoder();
 
@@ -51,12 +60,62 @@
         switch( msg.getType().getValue() )
         {
             case( MessageTypeEnum.ABANDONREQUEST_VAL ):
+                if ( abandonReqEncoder == null )
+                {
+                    abandonReqEncoder = new AbandonRequestEncoder();
+                }
                 abandonReqEncoder.attach( encoder );
                 abandonReqEncoder.encode( ( AbandonRequest ) obj );
                 break;
+
+            case( MessageTypeEnum.BINDREQUEST_VAL ):
+                if ( bindReqEncoder == null )
+                {
+                    bindReqEncoder = new BindRequestEncoder();
+                }
+                bindReqEncoder.attach( encoder );
+                bindReqEncoder.encode( ( BindRequest ) obj );
+                break;
+
+            case( MessageTypeEnum.BINDRESPONSE_VAL ):
+                if ( bindRespEncoder == null )
+                {
+                    bindRespEncoder = new BindResponseEncoder();
+                }
+                bindRespEncoder.attach( encoder );
+                bindRespEncoder.encode( ( BindResponse ) obj );
+                break;
+
+            case( MessageTypeEnum.SEARCHREQUEST_VAL ):
+                if ( searchReqEncoder == null )
+                {
+                    searchReqEncoder = new SearchRequestEncoder();
+                }
+                searchReqEncoder.attach( encoder );
+                searchReqEncoder.encode( ( SearchRequest ) obj );
+                break;
+
+            case( MessageTypeEnum.SEARCHRESDONE_VAL ):
+                if ( searchRespDoneEncoder == null )
+                {
+                    searchRespDoneEncoder = new SearchResponseDoneEncoder();
+                }
+                searchRespDoneEncoder.attach( encoder );
+                searchRespDoneEncoder.encode( ( SearchResponseDone ) obj );
+                break;
+
+            case( MessageTypeEnum.SEARCHRESENTRY_VAL ):
+                if ( searchRespEntryEncoder == null )
+                {
+                    searchRespEntryEncoder = new SearchResponseEntryEncoder();
+                }
+                searchRespEntryEncoder.attach( encoder );
+                searchRespEntryEncoder.encode( ( SearchResponseEntry ) obj );
+                break;
+
             default:
                 IllegalArgumentException e = new IllegalArgumentException(
-                        "Cannot encode " + obj ) ;
+                        "Unable to encode unrecognized object: " + obj ) ;
                 monitor.error( this, e );
                 throw e;
         }
@@ -67,7 +126,7 @@
     {
         encoder.setCallback( cb );
 
-        if ( monitor == null )
+        if ( monitor != null )
         {
             monitor.callbackSet( this, null, cb );
             return;

Added: incubator/directory/snickers/trunk/ldap-ber-provider/src/test/org/apache/snickers/ldap/SnickersDecoderTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ldap-ber-provider/src/test/org/apache/snickers/ldap/SnickersDecoderTest.java
Wed Jul 28 16:15:47 2004
@@ -0,0 +1,84 @@
+/*
+ *   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.ldap;
+
+import junit.framework.TestCase;
+
+import java.io.InputStream;
+import java.io.ByteArrayInputStream;
+
+import org.apache.ldap.common.message.Message;
+import org.apache.ldap.common.message.MessageEncoder;
+import org.apache.ldap.common.message.BindRequestImpl;
+import org.apache.ldap.common.message.BindRequest;
+import org.apache.commons.lang.ArrayUtils;
+
+
+/**
+ * Test cases for the SnickersDecoder class.
+ *
+ * @todo test with all PDU types - only BindRequest has been tested up to now
+ * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory
+ *         Project</a> $Rev$
+ */
+public class SnickersDecoderTest extends TestCase
+{
+    SnickersDecoder decoder = null;
+
+
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+
+        SnickersProvider provider = new SnickersProvider();
+        decoder = ( SnickersDecoder ) provider.getDecoder();
+    }
+
+
+    protected void tearDown() throws Exception
+    {
+        super.tearDown();
+        decoder = null;
+    }
+
+
+    public void testDecoderOnBindRequest()
+    {
+        BindRequestImpl req = new BindRequestImpl( 27 );
+        req.setName( "cn=admin,dc=example,dc=com" );
+        req.setSimple( true );
+        req.setVersion3( true );
+        req.setCredentials( "passwd".getBytes() );
+
+        BindRequest decoded = ( BindRequest )
+                decoder.decode( null, encode( req ) );
+
+        assertEquals( req.getMessageId(), decoded.getMessageId() );
+        assertEquals( req.getName(), decoded.getName() );
+        assertTrue( ArrayUtils.isEquals( req.getCredentials(),
+                decoded.getCredentials() ) );
+        assertEquals( req.getSimple(), decoded.getSimple() );
+        assertEquals( req.getVersion3(), decoded.getVersion3() );
+    }
+
+
+    private InputStream encode( Message msg )
+    {
+        MessageEncoder encoder = new MessageEncoder();
+        return new ByteArrayInputStream( encoder.encode( msg ) );
+    }
+}

Added: incubator/directory/snickers/trunk/ldap-ber-provider/src/test/org/apache/snickers/ldap/SnickersEncoderTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ldap-ber-provider/src/test/org/apache/snickers/ldap/SnickersEncoderTest.java
Wed Jul 28 16:15:47 2004
@@ -0,0 +1,81 @@
+/*
+ *   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.ldap;
+
+import junit.framework.TestCase;
+
+import java.io.InputStream;
+import java.io.ByteArrayInputStream;
+
+import org.apache.ldap.common.message.*;
+import org.apache.commons.lang.ArrayUtils;
+
+
+/**
+ * Test cases for the SnickersEncoder class.
+ *
+ * @todo test with all PDU types - only BindRequest has been tested up to now
+ * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory
+ *         Project</a> $Rev$
+ */
+public class SnickersEncoderTest extends TestCase
+{
+    SnickersEncoder encoder = null;
+
+
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+
+        SnickersProvider provider = new SnickersProvider();
+        encoder = ( SnickersEncoder ) provider.getEncoder();
+    }
+
+
+    protected void tearDown() throws Exception
+    {
+        super.tearDown();
+        encoder = null;
+    }
+
+
+    public void testEncoderOnBindRequest()
+    {
+        BindRequestImpl req = new BindRequestImpl( 27 );
+        req.setName( "cn=admin,dc=example,dc=com" );
+        req.setSimple( true );
+        req.setVersion3( true );
+        req.setCredentials( "passwd".getBytes() );
+
+        BindRequest encoded = ( BindRequest ) decode ( encoder.encode( req ) ) ;
+
+        assertEquals( req.getMessageId(), encoded.getMessageId() );
+        assertEquals( req.getName(), encoded.getName() );
+        assertTrue( ArrayUtils.isEquals( req.getCredentials(),
+                encoded.getCredentials() ) );
+        assertEquals( req.getSimple(), encoded.getSimple() );
+        assertEquals( req.getVersion3(), encoded.getVersion3() );
+    }
+
+
+    private Message decode( byte[] bites )
+    {
+        MessageDecoder decoder = new MessageDecoder();
+        ByteArrayInputStream in = new ByteArrayInputStream( bites );
+        return ( Message ) decoder.decode( null, in );
+    }
+}

Mime
View raw message