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/08/23 07:11:45 UTC

svn commit: rev 36753 - in incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder: . bind

Author: akarasulu
Date: Sun Aug 22 22:11:44 2004
New Revision: 36753

Added:
   incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/AbstractEncoderTestCase.java
Modified:
   incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/bind/BindResponseEncoderTest.java
Log:
Extracted base functionality from BindResponseEncoder test to create an 
abstract base class for all encoder unit tests.  Then retrofitted the 
BindResponseEncoderTest to derive from the base class and use the convenience
methods therein.


Added: incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/AbstractEncoderTestCase.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/AbstractEncoderTestCase.java	Sun Aug 22 22:11:44 2004
@@ -0,0 +1,95 @@
+/*
+ *   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.encoder;
+
+
+import junit.framework.TestCase;
+
+import org.apache.commons.codec.stateful.EncoderCallback;
+import org.apache.commons.codec.stateful.StatefulEncoder;
+
+import org.apache.ldap.common.message.Message;
+import org.apache.ldap.common.message.MessageDecoder;
+import org.apache.ldap.common.message.spi.Provider;
+import org.apache.snickers.ber.DefaultMutableTupleNode;
+import org.apache.snickers.ber.DeterminateLengthVisitor;
+import org.apache.snickers.ber.TupleEncodingVisitor;
+
+import java.nio.ByteBuffer;
+import java.util.Properties;
+import java.io.ByteArrayInputStream;
+
+
+/**
+ * A testcase base class used for encoder test cases.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory
+ *         Project</a> $Rev$
+ */
+public class AbstractEncoderTestCase extends TestCase
+        implements EncoderCallback
+{
+    /** collects/accumulates the chunks emitted from the encoder */
+    ByteBuffer accumulator = ByteBuffer.wrap( new byte[128] );
+
+
+    /**
+     * 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.put( ( ByteBuffer ) encoded );
+    }
+
+
+    /**
+     * Decodes the accumulated bytes emitted from the encoder being tested using
+     * the Snacc4J decoder.
+     *
+     * @return the decoded LDAP Message envelope
+     */
+    public Message decode()
+    {
+        Properties env = new Properties();
+        env.setProperty( Provider.BERLIB_PROVIDER,
+                "org.apache.ldap.common.berlib.snacc.SnaccProvider" );
+        MessageDecoder decoder = new MessageDecoder( env );
+        ByteArrayInputStream in = new ByteArrayInputStream( accumulator.array(),
+                0, accumulator.position() );
+        return decoder.decode( null, in );
+    }
+
+
+    /**
+     * Encodes a tuple tree into the accumulator's byte buffer using the
+     * determinate length form.
+     *
+     * @param node the node to be encoded.
+     */
+    public void encode( DefaultMutableTupleNode node )
+    {
+        DeterminateLengthVisitor visitor = new DeterminateLengthVisitor();
+        node.accept( visitor );
+
+        TupleEncodingVisitor encoder = new TupleEncodingVisitor();
+        encoder.setCallback( this );
+        node.accept( encoder );
+    }
+}

Modified: incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/bind/BindResponseEncoderTest.java
==============================================================================
--- incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/bind/BindResponseEncoderTest.java	(original)
+++ incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/bind/BindResponseEncoderTest.java	Sun Aug 22 22:11:44 2004
@@ -16,19 +16,15 @@
  */
 package org.apache.snickers.ldap.encoder.bind;
 
-import junit.framework.TestCase;
-import org.apache.ldap.common.message.*;
-import org.apache.ldap.common.message.spi.Provider;
+
 import org.apache.snickers.ber.TupleNode;
-import org.apache.snickers.ber.DeterminateLengthVisitor;
-import org.apache.snickers.ber.TupleEncodingVisitor;
-import org.apache.commons.codec.stateful.CallbackHistory;
-import org.apache.commons.codec.stateful.EncoderCallback;
-import org.apache.commons.codec.stateful.StatefulEncoder;
-
-import java.nio.ByteBuffer;
-import java.util.Properties;
-import java.io.ByteArrayInputStream;
+import org.apache.snickers.ber.DefaultMutableTupleNode;
+import org.apache.snickers.ldap.encoder.AbstractEncoderTestCase;
+
+import org.apache.ldap.common.message.ReferralImpl;
+import org.apache.ldap.common.message.ResultCodeEnum;
+import org.apache.ldap.common.message.LdapResultImpl;
+import org.apache.ldap.common.message.BindResponseImpl;
 
 
 /**
@@ -37,47 +33,27 @@
  * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory
  *         Project</a> $Rev$
  */
-public class BindResponseEncoderTest extends TestCase
-        implements EncoderCallback
+public class BindResponseEncoderTest extends AbstractEncoderTestCase
 {
-    ByteBuffer accumulator = ByteBuffer.wrap( new byte[128] );
-
-
-    /**
-     * 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.put( ( ByteBuffer ) encoded );
-    }
-
-
     public void testEncode()
     {
+        // Construct the bind response to test with results and referrals
         BindResponseImpl response = new BindResponseImpl( 5 );
         LdapResultImpl result = new LdapResultImpl( response );
         response.setLdapResult( result );
         result.setMatchedDn( "dc=example,dc=com" );
         result.setResultCode( ResultCodeEnum.SUCCESS );
+        ReferralImpl refs = new ReferralImpl( result );
+        refs.addLdapUrl( "ldap://someserver.com" );
+        refs.addLdapUrl( "ldap://apache.org" );
+        refs.addLdapUrl( "ldap://another.net" );
+        result.setReferral( refs );
+
+        // Encode stub into tuple tree then into the accumulator
         TupleNode node = BindResponseEncoder.INSTANCE.encode( response );
-        DeterminateLengthVisitor visitor = new DeterminateLengthVisitor();
-        node.accept( visitor );
+        encode( ( DefaultMutableTupleNode ) node );
 
-        TupleEncodingVisitor encoder = new TupleEncodingVisitor();
-        encoder.setCallback( this );
-        node.accept( encoder );
-
-        Properties env = new Properties();
-        env.setProperty( Provider.BERLIB_PROVIDER,
-                "org.apache.ldap.common.berlib.snacc.SnaccProvider" );
-        MessageDecoder decoder = new MessageDecoder( env );
-
-        ByteArrayInputStream in = new ByteArrayInputStream( accumulator.array(),
-                0, accumulator.position() );
-        Message msg = decoder.decode( null, in );
-        BindResponse decodedResponse = ( BindResponse ) msg;
+        // Test to see if original stub equals the round trip generated stub
+        assertTrue( response.equals( decode() ) );
     }
 }