directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r234226 [1/4] - in /directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber: ./ digester/ digester/rules/ primitives/
Date Sun, 21 Aug 2005 15:03:59 GMT
Author: elecharny
Date: Sun Aug 21 08:03:46 2005
New Revision: 234226

URL: http://svn.apache.org/viewcvs?rev=234226&view=rev
Log:
re-imported old ber tests

Added:
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/AbstractDecoderTestCaseTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BERDecoderCallbackAdapterTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BERDecoderStateTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BERDecoderTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BEREncoderTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/ConstructedTLVTests.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/DefaultMutableTupleNodeTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/EncodeDecodeTests.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/LdapMessageTests.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/LengthTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/MultiByteLengthTests.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/MultiByteTagTests.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/SimplePrimitiveTLVTests.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/SingleByteLengthTests.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/SingleByteTagTests.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TagTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TupleTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TupleTreeDecoderTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/TypeClassTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/AbstractRuleTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/BERDigesterTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/RulesBaseTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/TagNodeTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/TagTreeTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/rules/
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/rules/ByteAccumulatorTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/rules/PrimitiveIntDecodeRuleTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/digester/rules/PrimitiveOctetStringRuleTest.java
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/primitives/
    directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/primitives/PrimitiveUtilsTest.java

Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/AbstractDecoderTestCaseTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/AbstractDecoderTestCaseTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/AbstractDecoderTestCaseTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/AbstractDecoderTestCaseTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,146 @@
+/*
+ *   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.asn1.ber ;
+
+
+import java.nio.ByteBuffer;
+
+import org.apache.commons.lang.ArrayUtils ;
+import org.apache.asn1.ber.AbstractDecoderTestCase;
+
+
+/**
+ * Tests the base test class functions.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class AbstractDecoderTestCaseTest extends AbstractDecoderTestCase
+{
+    
+    public AbstractDecoderTestCaseTest()
+    {
+        super ( AbstractDecoderTestCaseTest.class.getName() ) ; 
+    }
+
+    public void testFragment()
+    {
+        byte[] all = new byte[3] ;
+        assertEquals( 1, fragment(all, 3).length) ;
+        try
+        {
+            fragment(ArrayUtils.EMPTY_BYTE_ARRAY, 0) ;
+            fail( "should have thrown exception before reaching this line" ) ;
+        }
+        catch( IllegalArgumentException e )
+        {
+            assertNotNull( e ) ;
+        }
+    }
+
+    /*
+     * Class to test for Tuple decode(byte)
+     */
+    public void testDecodebyte() throws Exception
+    {
+        decode( ( byte ) 1 ) ;
+        decode( ( byte ) 1 ) ;
+        Tuple t = decode( ( byte ) 1 ) ;
+        assertEquals( 1, t.id ) ;
+        assertEquals( 1, t.length ) ;
+    }
+
+    /*
+     * Class to test for Tuple decode(Tuple[])
+     */
+    public void testDecodeTupleArray() throws Exception
+    {
+        Tuple [] tuples = new Tuple[2] ;
+        tuples[0] = new Tuple( 1, 0 ) ;
+        tuples[1] = new Tuple( 1, 0 ) ;
+        
+        ByteBuffer[] buffers = new ByteBuffer[2] ; 
+        buffers[0] = ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ;
+        buffers[1] = ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ;
+        decode( tuples, buffers ) ;
+        decode( tuples[0], ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ) ;
+    }
+
+    public void testCallbackOccured() throws Exception
+    {
+        decoder.setDecoderMonitor( this ) ;
+        Tuple [] tuples = new Tuple[2] ;
+        tuples[0] = new Tuple(1, 0) ;
+        tuples[1] = new Tuple(1, 0) ;
+        ByteBuffer[] buffers = new ByteBuffer[2] ; 
+        buffers[0] = ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ;
+        buffers[1] = ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ;
+        decode(tuples, buffers) ;
+        callbackOccured(decoder, this, tuples[1]) ;
+    }
+
+    public void testCallbackSet()
+    {
+        decoder.setCallback(this) ;
+        callbackSet(decoder, this, this) ;
+    }
+
+    public void testError()
+    {
+        try
+        {
+            error( decoder, new Exception() ) ;
+            fail("should not get here") ;
+        }
+        catch ( Throwable e )
+        {
+            assertNotNull( e ) ;
+        }
+    }
+
+    public void testFatalError()
+    {
+        try
+        {
+            fatalError( decoder, new Exception() ) ;
+            fail("should not get here") ;
+        }
+        catch ( Throwable e )
+        {
+            assertNotNull( e ) ;
+        }
+    }
+
+    public void testMonitorSet()
+    {
+        monitorSet( decoder, this ) ;
+    }
+
+    public void testWarning()
+    {
+        try
+        {
+            warning( decoder, new Exception() ) ;
+            fail("should not get here") ;
+        }
+        catch ( Throwable e )
+        {
+            assertNotNull( e ) ;
+        }
+    }
+}

Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BERDecoderCallbackAdapterTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BERDecoderCallbackAdapterTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BERDecoderCallbackAdapterTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BERDecoderCallbackAdapterTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,88 @@
+/*
+ *   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.asn1.ber ;
+
+
+import junit.framework.TestCase ;
+import org.apache.asn1.ber.BERDecoderCallbackAdapter;
+
+
+/**
+ * Tests for the adapter to pass clover coverage.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class BERDecoderCallbackAdapterTest extends TestCase
+{
+    BERDecoderCallbackAdapter adapter = null ;
+    
+
+    public static void main(String[] args)
+    {
+        junit.textui.TestRunner.run(BERDecoderCallbackAdapterTest.class);
+    }
+
+    /*
+     * @see TestCase#setUp()
+     */
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        adapter = new BERDecoderCallbackAdapter() ;
+    }
+
+    /*
+     * @see TestCase#tearDown()
+     */
+    protected void tearDown() throws Exception
+    {
+        super.tearDown();
+        adapter = null ;
+    }
+
+    /**
+     * Constructor for BERDecoderCallbackAdapter.
+     * @param arg0
+     */
+    public BERDecoderCallbackAdapterTest(String arg0)
+    {
+        super(arg0);
+    }
+
+    public void testTagDecoded()
+    {
+        adapter.tagDecoded( null ) ;
+    }
+
+    public void testLengthDecoded()
+    {
+        adapter.lengthDecoded( null ) ;
+    }
+
+    public void testPartialValueDecoded()
+    {
+        adapter.partialValueDecoded( null ) ;
+    }
+
+    public void testDecodeOccurred()
+    {
+        adapter.decodeOccurred( null, null ) ;
+    }
+
+}

Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BERDecoderStateTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BERDecoderStateTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BERDecoderStateTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BERDecoderStateTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,174 @@
+/*
+ *   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.asn1.ber ;
+
+
+import java.util.List;
+import java.util.Map;
+
+import junit.framework.TestCase ;
+import org.apache.asn1.ber.BERDecoderState;
+
+
+/**
+ * Tests the 
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class BERDecoderStateTest extends TestCase
+{
+
+    public static void main(String[] args)
+    {
+        junit.textui.TestRunner.run(BERDecoderStateTest.class);
+    }
+
+    /*
+     * @see TestCase#setUp()
+     */
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+    }
+
+    /*
+     * @see TestCase#tearDown()
+     */
+    protected void tearDown() throws Exception
+    {
+        super.tearDown();
+    }
+
+    /**
+     * Constructor for BERDecoderStateTest.
+     * @param arg0
+     */
+    public BERDecoderStateTest(String arg0)
+    {
+        super(arg0);
+    }
+
+    public void testGetNext()
+    {
+        BERDecoderState state = BERDecoderState.getStartState() ;
+        
+        assertEquals( BERDecoderState.TAG, state ) ;
+        state = state.getNext( true ) ;
+        assertEquals( BERDecoderState.LENGTH, state ) ;
+        state = state.getNext( true ) ;
+        assertEquals( BERDecoderState.VALUE, state ) ;
+
+        state = state.getNext( true ) ;
+        assertEquals( BERDecoderState.TAG, state ) ;
+        state = state.getNext( true ) ;
+        assertEquals( BERDecoderState.LENGTH, state ) ;
+        state = state.getNext( false ) ;
+        assertEquals( BERDecoderState.TAG, state ) ;
+    }
+
+    public void testIsEndState()
+    {
+        assertFalse( BERDecoderState.TAG.isEndState( true ) ) ;
+        assertFalse( BERDecoderState.TAG.isEndState( false ) ) ;
+        assertFalse( BERDecoderState.LENGTH.isEndState( true ) ) ;
+        assertTrue( BERDecoderState.LENGTH.isEndState( false ) ) ;
+        assertTrue( BERDecoderState.VALUE.isEndState( true ) ) ;
+        assertTrue( BERDecoderState.VALUE.isEndState( false ) ) ;
+    }
+
+    public void testGetStartState()
+    {
+        assertEquals( BERDecoderState.TAG, BERDecoderState.getStartState() ) ;
+    }
+
+    /*
+     * Class to test for BERDecoderState getTypeClass(String)
+     */
+    public void testGetStateString()
+    {
+        assertEquals( BERDecoderState.LENGTH, 
+                BERDecoderState.getState(BERDecoderState.LENGTH.getName()) ) ;
+        assertEquals( BERDecoderState.TAG, 
+                BERDecoderState.getState(BERDecoderState.TAG.getName()) ) ;
+        assertEquals( BERDecoderState.VALUE, 
+                BERDecoderState.getState(BERDecoderState.VALUE.getName()) ) ;
+        
+        assertEquals( BERDecoderState.LENGTH, 
+                BERDecoderState.getState("length") ) ;
+        assertEquals( BERDecoderState.TAG, 
+                BERDecoderState.getState("TAG") ) ;
+        assertEquals( BERDecoderState.TAG, 
+                BERDecoderState.getState("Tag") ) ;
+        assertEquals( BERDecoderState.VALUE, 
+                BERDecoderState.getState("value") ) ;
+        
+        try
+        {
+            BERDecoderState.getState("asdf") ;
+            fail( "should not be reached due to thrown exception" ) ;
+        }
+        catch ( Throwable t )
+        {
+            assertNotNull( t ) ;
+        }
+    }
+
+    public void testList()
+    {
+        List list = BERDecoderState.list() ;
+        assertNotNull( list ) ;
+        assertEquals( 3, list.size() ) ;
+        assertTrue( list.contains( BERDecoderState.LENGTH ) ) ;
+        assertTrue( list.contains( BERDecoderState.TAG ) ) ;
+        assertTrue( list.contains( BERDecoderState.VALUE ) ) ;
+    }
+
+    public void testMap()
+    {
+        Map map = BERDecoderState.map() ;
+        assertNotNull( map ) ;
+        assertEquals( 3, map.size() ) ;
+        assertTrue( map.containsKey( BERDecoderState.VALUE.getName() ) ) ;
+        assertTrue( map.containsKey( BERDecoderState.LENGTH.getName() ) ) ;
+        assertTrue( map.containsKey( BERDecoderState.TAG.getName() ) ) ;
+    }
+
+    /*
+     * Class to test for BERDecoderState getTypeClass(int)
+     */
+    public void testGetStateint()
+    {
+        assertEquals( BERDecoderState.LENGTH, 
+                BERDecoderState.getState(BERDecoderState.LENGTH_VAL) ) ;
+        assertEquals( BERDecoderState.TAG, 
+                BERDecoderState.getState(BERDecoderState.TAG_VAL) ) ;
+        assertEquals( BERDecoderState.VALUE, 
+                BERDecoderState.getState(BERDecoderState.VALUE_VAL) ) ;
+        
+        try
+        {
+            BERDecoderState.getState( 293847 ) ;
+            fail( "should not be reached due to thrown exception" ) ;
+        }
+        catch ( Throwable t )
+        {
+            assertNotNull( t ) ;
+        }
+    }
+}

Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BERDecoderTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BERDecoderTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BERDecoderTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BERDecoderTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,292 @@
+/*
+ *   Copyright 2004-2005 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.asn1.ber ;
+
+
+import java.nio.ByteBuffer ;
+import java.util.ArrayList ;
+
+import org.apache.commons.lang.ArrayUtils ;
+import org.apache.commons.lang.RandomStringUtils ;
+
+import org.apache.asn1.codec.stateful.StatefulDecoder ;
+import org.apache.asn1.codec.stateful.DecoderCallback;
+import org.apache.asn1.codec.stateful.DecoderMonitorAdapter;
+import org.apache.asn1.codec.stateful.*;
+import org.apache.asn1.ber.AbstractDecoderTestCase;
+import org.apache.asn1.ber.BERDecoderMonitor;
+
+
+/**
+ * Tests the decoder using various complext TLV decoding scenarios and performs
+ * round trip encode - decode functionality.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class BERDecoderTest extends AbstractDecoderTestCase
+{
+    private static final ByteBuffer EMPTY_BUFFER =
+        ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ;
+
+
+    public BERDecoderTest()
+    {
+        super( BERDecoderTest.class.getName() ) ;
+    }
+
+
+    public void testBasisCases() throws Exception
+    {
+        decoder.setDecoderMonitor( new DecoderMonitorAdapter() ) ;
+        decoder.decode( null ) ;
+        decoder.decode( EMPTY_BUFFER ) ;
+    }
+
+
+    public void testPrimitives() throws Exception
+    {
+        Tuple decoded = null ;
+        Tuple t = new Tuple( 45, 0, true, TypeClass.APPLICATION ) ;
+        assertTrue( decode( t, EMPTY_BUFFER ).equals( t ) ) ;
+
+        t = new Tuple( 45, "Hello world!".length(), true,
+                TypeClass.APPLICATION ) ;
+        decoded = decode( t, ByteBuffer.wrap( "Hello world!".getBytes() ) ) ;
+        assertTrue( decoded.equals( t ) ) ;
+        assertEquals( "Hello world!", toString( decoded.getLastValueChunk() ) ) ;
+
+        String mesg = RandomStringUtils.randomAlphanumeric(1000) ;
+        t = new Tuple( 1234233, mesg.length(), true, TypeClass.APPLICATION ) ;
+        decoded = decode( t, ByteBuffer.wrap( mesg.getBytes() ) ) ;
+        assertTrue( decoded.equals( t ) ) ;
+        assertEquals( mesg, toString( decoded.getLastValueChunk() ) ) ;
+    }
+
+
+    String toString(ByteBuffer buf)
+    {
+        buf = buf.slice() ;
+        byte[] bites = new byte[buf.remaining()] ;
+        buf.get( bites ) ;
+        return new String( bites ) ;
+    }
+
+
+    public void testConstructedIndefinite() throws Exception
+    {
+        Tuple top = new Tuple( 1, TypeClass.APPLICATION ) ;
+        Tuple t0 = new Tuple( 2, "Hello".length(),
+                true, TypeClass.APPLICATION ) ;
+        Tuple t1 = new Tuple( 3, "World".length(),
+                true, TypeClass.APPLICATION ) ;
+        Tuple terminator = new Tuple( 0, 0, true, TypeClass.UNIVERSAL ) ;
+        assertTrue( decode( top, EMPTY_BUFFER ).equals( top ) ) ;
+
+        Tuple decoded = decode( t0, ByteBuffer.wrap( "Hello".getBytes() ) ) ;
+        assertTrue( decoded.equals( t0 ) ) ;
+        assertEquals( "Hello", toString( decoded.getLastValueChunk() ) ) ;
+
+        decoded = decode( t1, ByteBuffer.wrap( "World".getBytes() ) ) ;
+        assertTrue( decoded.equals( t1 ) ) ;
+        assertEquals( "World", toString( decoded.getLastValueChunk() ) ) ;
+
+        decoded = decode( terminator, EMPTY_BUFFER ) ;
+        assertTrue( decoded.equals( top ) ) ;
+    }
+
+
+    public void testConstructedLongLengthForm() throws Exception
+    {
+        String str0 = RandomStringUtils.randomAlphanumeric(128) ;
+        Tuple t0 = new Tuple( 2, 128, true, TypeClass.APPLICATION ) ;
+        String str1 = RandomStringUtils.randomAlphanumeric(128) ;
+        Tuple t1 = new Tuple( 3, 128, true, TypeClass.APPLICATION ) ;
+        Tuple top = new Tuple( 1, t0.size() + t1.size() ) ;
+        assertTrue( decode( top, EMPTY_BUFFER ).equals( top ) ) ;
+
+        Tuple decoded = decode( t0, ByteBuffer.wrap( str0.getBytes() ) ) ;
+        assertTrue( decoded.equals( t0 ) ) ;
+        assertEquals( str0, toString( decoded.getLastValueChunk() ) ) ;
+
+        // automatically set to top because after t1 is delivered top is
+        decoded = decode( t1, ByteBuffer.wrap( str1.getBytes() ) ) ;
+        assertTrue( decoded.equals( top ) ) ;
+    }
+
+
+    public void testConstructedShortLengthForm() throws Exception
+    {
+        Tuple t0 = new Tuple( 2, "Hello".length(), true,
+                TypeClass.APPLICATION ) ;
+        Tuple t1 = new Tuple( 3, "World".length(), true,
+                TypeClass.APPLICATION ) ;
+        Tuple top = new Tuple( 1, t0.size() + t1.size() ) ;
+        assertTrue( decode( top, EMPTY_BUFFER ).equals( top ) ) ;
+
+        Tuple decoded = decode( t0, ByteBuffer.wrap( "Hello".getBytes() ) ) ;
+        assertTrue( decoded.equals( t0 ) ) ;
+        assertEquals( "Hello", toString( decoded.getLastValueChunk() ) ) ;
+
+        // automatically set to top because after t1 is delivered top is
+        decoded = decode( t1, ByteBuffer.wrap( "World".getBytes() ) ) ;
+        assertTrue( decoded.equals( top ) ) ;
+    }
+
+
+    public void testFragmentedValue() throws Exception
+    {
+        String str0 = RandomStringUtils.randomAlphanumeric(20) ;
+        Tuple t0 = new Tuple( 2, str0.length(), true, TypeClass.APPLICATION ) ;
+        String str1 = RandomStringUtils.randomAlphanumeric(20) ;
+        Tuple t1 = new Tuple( 3, str1.length(), true, TypeClass.APPLICATION ) ;
+        Tuple top = new Tuple( 1, t0.size() + t1.size() ) ;
+        assertTrue( decode( top, EMPTY_BUFFER ).equals( top ) ) ;
+
+        ArrayList list = new ArrayList() ;
+        list.add( ByteBuffer.wrap( str0.getBytes() ) ) ;
+        ByteBuffer all = t0.toEncodedBuffer( list ) ;
+        ByteBuffer[] fragments = fragment( all, 10 ) ;
+        Tuple decoded = null ;
+
+        for ( int ii = 0; ii < fragments.length; ii++ )
+        {
+            decoded = decode( fragments[ii] ) ;
+        }
+
+        assertTrue( decoded.equals( t0 ) ) ;
+        assertEquals( str0, toString( buf ) ) ;
+
+        // automatically set to top because after t1 is delivered top is
+        decoded = decode( t1, ByteBuffer.wrap( str1.getBytes() ) ) ;
+        assertTrue( decoded.equals( top ) ) ;
+    }
+
+
+    public void testDecodeOccurred()
+    {
+        try
+        {
+            decoder.decodeOccurred( null, null ) ;
+            fail( "should never get here due to exception being thrown" ) ;
+        }
+        catch ( IllegalArgumentException e )
+        {
+            assertNotNull( e ) ;
+        }
+    }
+
+
+    public void testFireTagDecoded() throws Exception
+    {
+        decoder.setDecoderMonitor( new BERMonitor() ) ;
+        String str0 = RandomStringUtils.randomAlphanumeric(20) ;
+        Tuple t0 = new Tuple( 2, str0.length() ) ;
+        String str1 = RandomStringUtils.randomAlphanumeric(20) ;
+        Tuple t1 = new Tuple( 3, str1.length() ) ;
+        Tuple top = new Tuple( 1, t0.size() + t1.size() ) ;
+        Tuple decoded = decode( t0, ByteBuffer.wrap( str0.getBytes() ) ) ;
+        assertTrue( decoded.equals( t0 ) ) ;
+
+        // automatically set to top because after t1 is delivered top is
+        decoded = decode( t1, ByteBuffer.wrap( str1.getBytes() ) ) ;
+
+        assertTrue( decode( top, EMPTY_BUFFER ).equals( top ) ) ;
+
+        decoder.setDecoderMonitor(null) ;
+        decoded = decode( t0, ByteBuffer.wrap( str0.getBytes() ) ) ;
+        assertTrue( decoded.equals( t0 ) ) ;
+
+        tlvList.clear() ;
+        decoder.setDecoderMonitor(null) ;
+        decoder.setCallback(null) ;
+        decoded = decode( t0, ByteBuffer.wrap( str0.getBytes() ) ) ;
+        assertTrue(tlvList.isEmpty()) ;
+    }
+
+
+    public void testFireTagDecoded2() throws Exception
+    {
+        decoder.setDecoderMonitor( null ) ;
+        String str0 = RandomStringUtils.randomAlphanumeric(20) ;
+        Tuple t0 = new Tuple( 2, str0.length() ) ;
+        String str1 = RandomStringUtils.randomAlphanumeric(20) ;
+        Tuple t1 = new Tuple( 3, str1.length() ) ;
+        Tuple top = new Tuple( 1, t0.size() + t1.size() ) ;
+        Tuple decoded = decode( t0, ByteBuffer.wrap( str0.getBytes() ) ) ;
+        assertTrue( decoded.equals( t0 ) ) ;
+
+        // automatically set to top because after t1 is delivered top is
+        decoded = decode( t1, ByteBuffer.wrap( str1.getBytes() ) ) ;
+        assertTrue( decode( top, EMPTY_BUFFER ).equals( top ) ) ;
+    }
+
+
+    public void testFireTagDecoded3() throws Exception
+    {
+        decoder.setDecoderMonitor( new BERMonitor() ) ;
+        decoder.setCallback( null ) ;
+        String str0 = RandomStringUtils.randomAlphanumeric(20) ;
+        Tuple t0 = new Tuple( 2, str0.length() ) ;
+        String str1 = RandomStringUtils.randomAlphanumeric(20) ;
+        Tuple t1 = new Tuple( 3, str1.length() ) ;
+        Tuple top = new Tuple( 1, t0.size() + t1.size() ) ;
+        decode( t0, ByteBuffer.wrap( str0.getBytes() ) ) ;
+
+        // automatically set to top because after t1 is delivered top is
+        decode( t1, ByteBuffer.wrap( str1.getBytes() ) ) ;
+        assertTrue( decode( top, EMPTY_BUFFER ).equals( top ) ) ;
+    }
+
+
+    public void testFireTagDecoded4() throws Exception
+    {
+        decoder.setDecoderMonitor( null ) ;
+        decoder.setCallback( null ) ;
+        String str0 = RandomStringUtils.randomAlphanumeric(20) ;
+        Tuple t0 = new Tuple( 2, str0.length() ) ;
+        String str1 = RandomStringUtils.randomAlphanumeric(20) ;
+        Tuple t1 = new Tuple( 3, str1.length() ) ;
+        Tuple top = new Tuple( 1, t0.size() + t1.size() ) ;
+        decode( t0, ByteBuffer.wrap( str0.getBytes() ) );
+
+        // automatically set to top because after t1 is delivered top is
+        decode( t1, ByteBuffer.wrap( str1.getBytes() ) ) ;
+        assertTrue( decode( top, EMPTY_BUFFER ).equals( top ) ) ;
+    }
+
+
+    class BERMonitor implements BERDecoderMonitor
+    {
+        public void callbackOccured(StatefulDecoder decoder,
+                DecoderCallback cb, Object decoded) { }
+
+        public void error(StatefulDecoder decoder, Exception exception) { }
+
+        public void callbackSet(StatefulDecoder decoder, DecoderCallback oldcb,
+                DecoderCallback newcb) { }
+
+        public void fatalError(StatefulDecoder decoder, Exception exception){}
+
+        public void lengthDecoded( Tuple tlv ) { }
+
+        public void tagDecoded( Tuple tlv ) { }
+
+        public void warning( StatefulDecoder decoder, Exception exception ) { }
+    }
+}

Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BEREncoderTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BEREncoderTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BEREncoderTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/BEREncoderTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,276 @@
+/*
+ *   Copyright 2004-2005 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.asn1.ber;
+
+
+import junit.framework.TestCase;
+import org.apache.asn1.ber.primitives.UniversalTag;
+import org.apache.asn1.codec.stateful.EncoderCallback;
+import org.apache.asn1.codec.stateful.StatefulEncoder;
+import org.apache.asn1.codec.stateful.StatefulEncoder;
+import org.apache.asn1.ber.primitives.UniversalTag;
+import org.apache.asn1.ber.BEREncoder;
+import org.apache.commons.lang.ArrayUtils;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+
+
+/**
+ * Tests the BEREncoder for correct operation.
+ *
+ * @author <a href="mailto:dev@directory.apache.org"> Apache Directory
+ *         Project</a> $Rev: 157644 $
+ */
+public class BEREncoderTest extends TestCase implements EncoderCallback
+{
+    private BEREncoder encoder = null;
+    private ByteBuffer collector = null;
+
+
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        encoder = new BEREncoder();
+        encoder.setCallback( this );
+        collector = ByteBuffer.wrap( new byte[32] );
+    }
+
+
+    protected void tearDown() throws Exception
+    {
+        super.tearDown();
+        encoder.setCallback( null );
+        encoder = null;
+        collector = null;
+    }
+
+
+    public void encodeOccurred( StatefulEncoder encoder, Object encoded )
+    {
+        ByteBuffer buf = ( ByteBuffer ) encoded;
+        collector.put( buf );
+    }
+
+
+
+    /**
+     * Produces a primitive tuple and pumps it through the encoder while
+     * listening to the output of the encoder collecting the output bytes into
+     * one buffer.  Then the collected data is compared with the expected
+     * encoded data.
+     */
+    public void testPrimitives()
+    {
+        // prepare tlv and generate an integer tag event
+        Tuple tlv = new Tuple();
+        tlv.setTag( UniversalTag.INTEGER );
+        encoder.tag( tlv );
+
+        // generate a length event
+        tlv.setLength( 1 );
+        encoder.length( tlv );
+
+        // generate a value event
+        byte[] value = new byte[] { (byte) 10 };
+        ByteBuffer chunk = ByteBuffer.wrap( value );
+        tlv.setLastValueChunk( chunk );
+        encoder.chunkedValue( tlv, chunk );
+
+        // not really necessary but here for completeness
+        encoder.finish( tlv );
+
+        // generate the expected encoded bytes
+        ArrayList list = new ArrayList();
+        list.add( ByteBuffer.wrap( value ) );
+        ByteBuffer buf = tlv.toEncodedBuffer( list );
+        byte[] correctBytes = new byte[buf.remaining()];
+        buf.get( correctBytes );
+
+        // gather the collected encoded bytes
+        collector.flip();
+        byte[] encodedBytes = new byte[collector.remaining()];
+        collector.get( encodedBytes );
+
+        // compare the two
+        assertTrue( ArrayUtils.isEquals( correctBytes, encodedBytes ) );
+    }
+
+
+    /**
+     * Produces the tlv events for constructed TLV of definate length.
+     */
+    public void testConstructedDefinateLength1()
+    {
+        // prepare top level TLV of sequence with length of 3
+        Tuple top = new Tuple();
+        top.setTag( UniversalTag.SEQUENCE_SEQUENCE_OF, false );
+        encoder.tag( top );
+        top.setLength( 3 );
+        encoder.length( top );
+
+        // prepare single nested child tlv
+        Tuple tlv = new Tuple();
+        tlv.setTag( UniversalTag.INTEGER );
+        encoder.tag( tlv );
+        tlv.setLength( 1 );
+        encoder.length( tlv );
+        byte[] value = new byte[] { (byte) 10 };
+        ByteBuffer chunk = ByteBuffer.wrap( value );
+        tlv.setLastValueChunk( chunk );
+        encoder.chunkedValue( tlv, chunk );
+        encoder.finish( tlv );
+        encoder.finish( top );
+
+        // prepare the expected correct sequence of encoded bytes
+        ArrayList list = new ArrayList();
+        ByteBuffer all = ByteBuffer.wrap( new byte[64] ) ;
+        all.put( top.toEncodedBuffer( list ) );
+        list.add( ByteBuffer.wrap( value ) );
+        all.put( tlv.toEncodedBuffer( list ) );
+        all.flip();
+        byte[] correctBytes = new byte[all.remaining()];
+        all.get( correctBytes );
+
+        // gather the collected encoded bytes
+        collector.flip();
+        byte[] encodedBytes = new byte[collector.remaining()];
+        collector.get( encodedBytes );
+
+        // compare correct with encoded
+        assertTrue( ArrayUtils.isEquals( correctBytes, encodedBytes ) );
+    }
+
+
+    /**
+     * Produces the tlv events for constructed TLV of definate length.
+     */
+    public void testConstructedDefinateLength2()
+    {
+        // prepare top level TLV of sequence with length of 3
+        Tuple top = new Tuple();
+        top.setTag( UniversalTag.SEQUENCE_SEQUENCE_OF, false );
+        encoder.tag( top );
+        top.setLength( 8 );
+        encoder.length( top );
+
+        // prepare the expected correct sequence of encoded bytes
+        ArrayList list = new ArrayList();
+        ByteBuffer all = ByteBuffer.wrap( new byte[64] ) ;
+        all.put( top.toEncodedBuffer( list ) );
+
+        // prepare single nested child tlv
+        Tuple tlv = new Tuple();
+        tlv.setTag( UniversalTag.INTEGER );
+        encoder.tag( tlv );
+        tlv.setLength( 1 );
+        encoder.length( tlv );
+        byte[] value = new byte[] { (byte) 10 };
+        ByteBuffer chunk = ByteBuffer.wrap( value );
+        tlv.setLastValueChunk( chunk );
+        encoder.chunkedValue( tlv, chunk );
+        encoder.finish( tlv );
+        list.add( ByteBuffer.wrap( value ) );
+        all.put( tlv.toEncodedBuffer( list ) );
+
+        tlv.setTag( UniversalTag.INTEGER );
+        encoder.tag( tlv );
+        tlv.setLength( 3 );
+        encoder.length( tlv );
+        value = new byte[] { (byte) 2, 7, 12 };
+        chunk = ByteBuffer.wrap( value );
+        tlv.setLastValueChunk( chunk );
+        encoder.chunkedValue( tlv, chunk );
+        encoder.finish( tlv );
+        encoder.finish( top );
+        list.add( ByteBuffer.wrap( value ) );
+        all.put( tlv.toEncodedBuffer( list ) );
+
+        // prepare the correct buffers
+        all.flip();
+        byte[] correctBytes = new byte[all.remaining()];
+        all.get( correctBytes );
+
+        // gather the collected encoded bytes
+        collector.flip();
+        byte[] encodedBytes = new byte[collector.remaining()];
+        collector.get( encodedBytes );
+
+        // compare correct with encoded
+        assertTrue( ArrayUtils.isEquals( correctBytes, encodedBytes ) );
+    }
+
+
+    /**
+     * Produces the tlv events for constructed TLV of definate length.
+     */
+    public void testConstructedIndefiniteLength()
+    {
+        // prepare top level TLV of sequence with length of 3
+        Tuple top = new Tuple();
+        top.setTag( UniversalTag.SEQUENCE_SEQUENCE_OF, false );
+        encoder.tag( top );
+        top.setLength( Length.INDEFINITE );
+        encoder.length( top );
+
+        // prepare the expected correct sequence of encoded bytes
+        ArrayList list = new ArrayList();
+        ByteBuffer all = ByteBuffer.wrap( new byte[64] ) ;
+        all.put( top.toEncodedBuffer( list ) );
+
+        // prepare single nested child tlv
+        Tuple tlv = new Tuple();
+        tlv.setTag( UniversalTag.INTEGER );
+        encoder.tag( tlv );
+        tlv.setLength( 1 );
+        encoder.length( tlv );
+        byte[] value = new byte[] { (byte) 10 };
+        ByteBuffer chunk = ByteBuffer.wrap( value );
+        tlv.setLastValueChunk( chunk );
+        encoder.chunkedValue( tlv, chunk );
+        encoder.finish( tlv );
+        list.add( ByteBuffer.wrap( value ) );
+        all.put( tlv.toEncodedBuffer( list ) );
+
+        tlv.setTag( UniversalTag.INTEGER );
+        encoder.tag( tlv );
+        tlv.setLength( 3 );
+        encoder.length( tlv );
+        value = new byte[] { (byte) 2, 7, 12 };
+        chunk = ByteBuffer.wrap( value );
+        tlv.setLastValueChunk( chunk );
+        encoder.chunkedValue( tlv, chunk );
+        encoder.finish( tlv );
+        encoder.finish( top );
+        list.add( ByteBuffer.wrap( value ) );
+        all.put( tlv.toEncodedBuffer( list ) );
+        all.put( (byte) 0 ).put( (byte) 0 );
+
+        // prepare the correct buffers
+        all.flip();
+        byte[] correctBytes = new byte[all.remaining()];
+        all.get( correctBytes );
+
+        // gather the collected encoded bytes
+        collector.flip();
+        byte[] encodedBytes = new byte[collector.remaining()];
+        collector.get( encodedBytes );
+
+        // compare correct with encoded
+        assertTrue( ArrayUtils.isEquals( correctBytes, encodedBytes ) );
+    }
+}

Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/ConstructedTLVTests.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/ConstructedTLVTests.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/ConstructedTLVTests.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/ConstructedTLVTests.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,204 @@
+/*
+ *   Copyright 2004-2005 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.asn1.ber ;
+
+
+import org.apache.asn1.ber.AbstractDecoderTestCase;
+import org.apache.asn1.ber.BERDecoderState;
+
+
+/**
+ * Performs constructed tlv tests.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class ConstructedTLVTests extends AbstractDecoderTestCase
+{
+
+    /**
+     * @param arg0
+     */
+    public ConstructedTLVTests( String arg0 )
+    {
+        super( arg0 ) ;
+    }
+
+
+    public void testConstructedDefinateTLV() throws Exception
+    {
+        // decode tag
+        Tuple outter = decode( "01100001" ) ;
+        assertEquals( 1, outter.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( false, outter.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, outter.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+
+        // decode length
+        outter = decode( "00000011" ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+        // decode tag
+        Tuple tlv = decode( "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+
+        // decode length
+        tlv = decode( "00000001" ) ;
+        assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+        assertEquals( 1, tlv.length ) ;
+
+        // decode value
+        tlv = decode( "01010101" ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+        assertEquals( 2, tlvList.size() ) ;
+    }
+
+
+    public void testMultipleIndefiniteTLV() throws Exception
+    {
+        // --------------------------------------------------------------------
+
+        // decode tag
+        Tuple outter = decode( "01100001" ) ;
+        assertEquals( 1, outter.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( false, outter.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, outter.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+
+        // decode length
+        outter = decode( "10000000" ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+
+
+        // --------------------------------------------------------------------
+
+
+        // decode tag
+        Tuple tlv = decode( "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 0, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+
+        // decode length
+        tlv = decode( "00000001" ) ;
+        assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+        assertEquals( 1, tlv.length ) ;
+        assertEquals( 0, tlvList.size() ) ;
+
+        // decode value
+        tlv = decode( "01010101" ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+        assertEquals( 1, tlvList.size() ) ;
+        assertNotNull( tlv.valueChunk ) ;
+        assertEquals( 0x0055, 0x00ff & tlv.valueChunk.get( 0 ) ) ;
+
+
+        // --------------------------------------------------------------------
+
+
+        // decode tag
+        tlv = decode( "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 1, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+
+        // decode length
+        tlv = decode( "00000001" ) ;
+        assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+        assertEquals( 1, tlv.length ) ;
+
+        // decode value
+        tlv = decode( "01010101" ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+        assertEquals( 2, tlvList.size() ) ;
+        assertNotNull( tlv.valueChunk ) ;
+        assertEquals( 0x0055, 0x00ff & tlv.valueChunk.get( 0 ) ) ;
+
+
+        // --------------------------------------------------------------------
+
+
+        // decode tag
+        tlv = decode( "01000001" ) ;
+        assertEquals( 1, tlv.id ) ;
+        assertEquals( 2, tlvList.size() ) ;
+        assertEquals( true, tlv.isPrimitive ) ;
+        assertEquals( TypeClass.APPLICATION, tlv.typeClass ) ;
+        assertEquals( BERDecoderState.LENGTH, decoder.getState() ) ;
+
+        // decode length
+        tlv = decode( "00000001" ) ;
+        assertEquals( BERDecoderState.VALUE, decoder.getState() ) ;
+        assertEquals( 1, tlv.length ) ;
+
+        // decode value
+        tlv = decode( "01010101" ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+        assertEquals( 3, tlvList.size() ) ;
+        assertNotNull( tlv.valueChunk ) ;
+        assertEquals( 0x0055, 0x00ff & tlv.valueChunk.get( 0 ) ) ;
+
+        decode( "00000000" ) ;
+        decode( "00000000" ) ;
+
+        assertEquals( 4, tlvList.size() ) ;
+        assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+    }
+
+
+    public void testIllegalState() throws Exception
+    {
+        try
+        {
+            decode( "00000000" ) ;
+            decode( "00000000" ) ;
+            fail( "should throw an exception before getting here" ) ;
+        }
+        catch( Throwable e )
+        {
+            assertNotNull( e ) ;
+        }
+    }
+
+
+    public void testIllegalStateNoMonitor() throws Exception
+    {
+        decoder.setDecoderMonitor( null ) ;
+
+        try
+        {
+            decode( "00000000" ) ;
+            decode( "00000000" ) ;
+            fail( "should throw an exception before getting here" ) ;
+        }
+        catch( Throwable e )
+        {
+            assertNotNull( e ) ;
+        }
+    }
+}

Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/DefaultMutableTupleNodeTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/DefaultMutableTupleNodeTest.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/DefaultMutableTupleNodeTest.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/DefaultMutableTupleNodeTest.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,611 @@
+/*
+ *   Copyright 2004-2005 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.asn1.ber ;
+
+
+import java.util.Collections;
+import java.util.Iterator ;
+
+import java.nio.ByteBuffer ;
+
+import org.apache.asn1.ber.AbstractDecoderTestCase;
+import org.apache.asn1.ber.DefaultMutableTupleNode;
+
+
+/**
+ * Tests the default MutableTupleNode implementation.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 157644 $
+ */
+public class DefaultMutableTupleNodeTest extends AbstractDecoderTestCase
+{
+    public DefaultMutableTupleNodeTest()
+    {
+        super ( DefaultMutableTupleNodeTest.class.getName() ) ;
+    }
+
+
+    /*
+     * Class to test for void insert(MutableTupleNode, int)
+     */
+    public void testInsertMutableTupleNodeint()
+    {
+    }
+
+    /*
+     * Class to test for void remove(int)
+     */
+    public void testRemoveint()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        parent.insert( (MutableTupleNode) node, 0 ) ;
+        assertEquals(1,parent.getChildCount()) ;
+        parent.remove( 0 ) ;
+        assertEquals(0,parent.getChildCount()) ;
+    }
+
+    /*
+     * Class to test for void remove(MutableTupleNode)
+     */
+    public void testRemoveMutableTupleNode()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        parent.insert( (MutableTupleNode) node, 0 ) ;
+        assertEquals(1,parent.getChildCount()) ;
+        parent.remove( (MutableTupleNode) node ) ;
+        assertEquals(0,parent.getChildCount()) ;
+
+        parent.insert( node, 0 ) ;
+        assertEquals(1,parent.getChildCount()) ;
+        parent.remove( node ) ;
+        assertEquals(0,parent.getChildCount()) ;
+    }
+
+    public void testRemoveFromParent()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        parent.insert( (MutableTupleNode) node, 0 ) ;
+        node.setParent( (MutableTupleNode) parent) ;
+
+        assertEquals(1,parent.getChildCount()) ;
+        node.removeFromParent() ;
+        assertEquals(0,parent.getChildCount()) ;
+    }
+
+    /*
+     * Class to test for void setParent(MutableTupleNode)
+     */
+    public void testSetParentMutableTupleNode()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        parent.insert( (MutableTupleNode) node, 0 ) ;
+        node.setParent( (MutableTupleNode) parent) ;
+        assertEquals(parent, node.getParentTupleNode()) ;
+        node.setParent((MutableTupleNode)new DefaultMutableTupleNode()) ;
+        assertNotSame(parent, node.getParentTupleNode()) ;
+    }
+
+    /*
+     * Class to test for void setParent(MutableTupleNode)
+     */
+    public void testSetParentMutableTreeNode()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        parent.insert( node, 0 ) ;
+        node.setParent( parent) ;
+        assertEquals(parent, node.getParentTupleNode()) ;
+        node.setParent( new DefaultMutableTupleNode()) ;
+        assertNotSame( parent, node.getParentTupleNode() ) ;
+    }
+
+    public void testSetTuple()
+    {
+        Tuple t = new Tuple() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        node.setTuple( t ) ;
+        assertEquals(t, node.getTuple()) ;
+    }
+
+    public void testGetParentTupleNode()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        parent.insert( (MutableTupleNode) node, 0 ) ;
+        node.setParent( (MutableTupleNode) parent) ;
+        assertSame(parent, node.getParentTupleNode()) ;
+        assertSame(parent, node.getParentTupleNode()) ;
+    }
+
+    public void testGetChildren()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        parent.insert( (MutableTupleNode) node, 0 ) ;
+        node.setParent( (MutableTupleNode) parent) ;
+        assertSame(parent, node.getParentTupleNode()) ;
+        assertTrue( parent.getChildren().hasNext()) ;
+    }
+
+
+    public void testGetChildTupleNodeAt()
+    {
+    }
+
+    /*
+     * Class to test for int getIndex(TupleNode)
+     */
+    public void testGetIndexTupleNode()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        parent.insert( (MutableTupleNode) node, 0 ) ;
+        node.setParent( (MutableTupleNode) parent) ;
+        assertEquals( 0, parent.getIndex((MutableTupleNode)node));
+        assertEquals( 0, parent.getIndex(node));
+    }
+
+    public void testGetChildCount()
+    {
+    }
+
+    public void testSize()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        parent.insert( (MutableTupleNode) node, 0 ) ;
+        node.setParent( (MutableTupleNode) parent) ;
+
+        Tuple primitive = new Tuple( 1, 3, true, TypeClass.APPLICATION ) ;
+        Tuple constructed = new Tuple ( 2, TypeClass.APPLICATION ) ;
+        parent.setTuple( constructed ) ;
+        node.setTuple( primitive ) ;
+
+        // fact that we use the indefinite form we automatically add two to
+        // the contained size
+        assertEquals(9, parent.size()) ;
+    }
+
+    public void testSize2()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        parent.insert( (MutableTupleNode) node, 0 ) ;
+        node.setParent( (MutableTupleNode) parent) ;
+
+        Tuple primitive = new Tuple( 1, 3, true, TypeClass.APPLICATION ) ;
+        Tuple constructed = new Tuple ( 2, 5, TypeClass.APPLICATION ) ;
+        parent.setTuple( constructed ) ;
+        node.setTuple( primitive ) ;
+
+        assertEquals(7, parent.size()) ;
+    }
+
+    public void testSize3()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        Tuple constructed = new Tuple ( 2, TypeClass.APPLICATION ) ;
+        parent.setTuple( constructed ) ;
+
+        assertEquals(4, parent.size()) ;
+    }
+
+    public void testEncode3() throws Exception
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        Tuple constructed = new Tuple ( 2, TypeClass.APPLICATION ) ;
+        parent.setTuple( constructed ) ;
+        assertEquals(4, parent.size()) ;
+        ByteBuffer buf = ByteBuffer.allocate( 4 ) ;
+        parent.encode( buf ) ;
+
+
+        /*
+        final ArrayList list = new ArrayList() ;
+        TupleTreeDecoder decoder = new TupleTreeDecoder() ;
+        decoder.setCallback( new DecoderCallback(){
+            public void decodeOccurred(StatefulDecoder arg0, Object arg1)
+            {
+                list.add( arg1 ) ;
+            }}) ;
+
+        decoder.decode(buf.flip()) ;
+        DefaultMutableTupleNode decoded = ( DefaultMutableTupleNode )
+            list.get(0);
+        assertEquals( decoded.getTuple(), parent.getTuple()) ;
+        */
+    }
+
+    public void testGetTuple()
+    {
+        Tuple t = new Tuple() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        node.setTuple(t) ;
+        assertSame(t, node.getTuple()) ;
+    }
+
+    public void testEncode0() throws Exception
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+
+        DefaultMutableTupleNode end = new DefaultMutableTupleNode() ;
+        parent.insert( (MutableTupleNode) end, 0 ) ;
+        end.setParent( (MutableTupleNode) parent) ;
+        parent.insert( (MutableTupleNode) node, 0 ) ;
+        node.setParent( (MutableTupleNode) parent) ;
+
+        Tuple primitive = new Tuple( 1, 3, true, TypeClass.APPLICATION ) ;
+        Tuple constructed = new Tuple ( 2, TypeClass.APPLICATION ) ;
+        Tuple terminator = new Tuple( 0, 0, true, TypeClass.UNIVERSAL ) ;
+        terminator.typeClass = TypeClass.UNIVERSAL ;
+        parent.setTuple( constructed ) ;
+        node.setTuple( primitive ) ;
+        end.setTuple(terminator) ;
+
+        assertEquals(9, parent.size()) ;
+
+        ByteBuffer buf = ByteBuffer.allocate( 9 ) ;
+        parent.encode( buf ) ;
+
+        /**
+        final ArrayList list = new ArrayList() ;
+        TupleTreeDecoder decoder = new TupleTreeDecoder() ;
+        decoder.setCallback( new DecoderCallback(){
+            public void decodeOccurred(StatefulDecoder arg0, Object arg1)
+            {
+                list.add( arg1 ) ;
+            }}) ;
+
+        decoder.decode(buf.flip()) ;
+        DefaultMutableTupleNode decoded = ( DefaultMutableTupleNode )
+            list.get(0);
+        assertEquals( decoded.getTuple(), parent.getTuple()) ;
+        */
+    }
+
+    public void testEncode1() throws Exception
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        parent.insert( (MutableTupleNode) node, 0 ) ;
+        node.setParent( (MutableTupleNode) parent) ;
+
+        Tuple primitive = new Tuple( 1, 3, true, TypeClass.APPLICATION ) ;
+        Tuple constructed = new Tuple ( 2, TypeClass.APPLICATION ) ;
+        parent.setTuple( constructed ) ;
+        node.setTuple( primitive ) ;
+
+        // 2 extra bytes added for indefinite form
+        assertEquals(9, parent.size()) ;
+
+        ByteBuffer buf = ByteBuffer.allocate( 9 ) ;
+        parent.encode( buf ) ;
+
+        /*
+        final ArrayList list = new ArrayList() ;
+        TupleTreeDecoder decoder = new TupleTreeDecoder() ;
+        decoder.setCallback( new DecoderCallback(){
+            public void decodeOccurred(StatefulDecoder arg0, Object arg1)
+            {
+                list.add( arg1 ) ;
+            }}) ;
+
+        decoder.decode(buf.flip()) ;
+        DefaultMutableTupleNode decoded = ( DefaultMutableTupleNode )
+            list.get(0);
+        assertEquals( decoded.getTuple(), parent.getTuple()) ;
+        */
+    }
+
+    public void testEncode2() throws Exception
+    {
+        DefaultMutableTupleNode top = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode middle = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode bottom = new DefaultMutableTupleNode() ;
+        top.insert( (MutableTupleNode) middle, 0 ) ;
+        middle.setParent( (MutableTupleNode) top) ;
+        middle.insert( (MutableTupleNode) bottom, 0 ) ;
+        bottom.setParent( (MutableTupleNode) middle) ;
+
+        Tuple middleTuple = new Tuple( 1, 3, TypeClass.APPLICATION  ) ;
+        Tuple topTuple = new Tuple ( 2, 5, TypeClass.APPLICATION ) ;
+        Tuple bottomTuple = new Tuple( 3, 1, true, TypeClass.APPLICATION ) ;
+
+        bottomTuple.typeClass = TypeClass.UNIVERSAL ;
+        top.setTuple( topTuple ) ;
+        middle.setTuple( middleTuple ) ;
+        bottom.setTuple( bottomTuple ) ;
+
+        assertEquals(7, top.size()) ;
+
+        ByteBuffer buf = ByteBuffer.allocate( 7 ) ;
+        top.encode( buf ) ;
+
+        /*
+        final ArrayList list = new ArrayList() ;
+        TupleTreeDecoder decoder = new TupleTreeDecoder() ;
+        decoder.setCallback( new DecoderCallback(){
+            public void decodeOccurred(StatefulDecoder arg0, Object arg1)
+            {
+                list.add( arg1 ) ;
+            }}) ;
+
+        decoder.decode(buf.flip()) ;
+        DefaultMutableTupleNode decoded = ( DefaultMutableTupleNode )
+            list.get(0);
+        assertEquals( decoded.getTuple(), top.getTuple()) ;
+        */
+    }
+
+
+    public void testEquals()
+    {
+        DefaultMutableTupleNode top = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode middle = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode bottom = new DefaultMutableTupleNode() ;
+        top.insert( (MutableTupleNode) middle, 0 ) ;
+        middle.setParent( (MutableTupleNode) top) ;
+        middle.insert( (MutableTupleNode) bottom, 0 ) ;
+        bottom.setParent( (MutableTupleNode) middle) ;
+
+        Tuple middleTuple = new Tuple( 1, 3, TypeClass.APPLICATION  ) ;
+        Tuple topTuple = new Tuple ( 2, 5, TypeClass.APPLICATION ) ;
+        Tuple bottomTuple = new Tuple( 3, 1, true, TypeClass.APPLICATION ) ;
+
+        bottomTuple.typeClass = TypeClass.UNIVERSAL ;
+        top.setTuple( topTuple ) ;
+        middle.setTuple( middleTuple ) ;
+        bottom.setTuple( bottomTuple ) ;
+
+        assertTrue( top.equals( top ) ) ;
+
+        DefaultMutableTupleNode topClone = new DefaultMutableTupleNode() ;
+        topClone.setTuple( topTuple ) ;
+        assertFalse( top.equals(topClone)) ;
+
+        topClone = new DefaultMutableTupleNode() ;
+        topClone.setTuple( bottomTuple ) ;
+        assertFalse( top.equals(topClone)) ;
+
+        topClone = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode middleClone = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode bottomClone = new DefaultMutableTupleNode() ;
+        topClone.insert( (MutableTupleNode) middleClone, 0 ) ;
+        middleClone.setParent( (MutableTupleNode) topClone) ;
+        middleClone.insert( (MutableTupleNode) bottomClone, 0 ) ;
+        bottomClone.setParent( (MutableTupleNode) middleClone) ;
+
+        Tuple middleTupleClone = new Tuple( 1, 3, TypeClass.APPLICATION  ) ;
+        Tuple topTupleClone = new Tuple ( 2, 5, TypeClass.APPLICATION ) ;
+        Tuple bottomTupleClone= new Tuple( 3, 1, true, TypeClass.APPLICATION ) ;
+
+        bottomTupleClone.typeClass = TypeClass.UNIVERSAL ;
+        topClone.setTuple( topTupleClone ) ;
+        middleClone.setTuple( middleTupleClone ) ;
+        bottomClone.setTuple( bottomTupleClone ) ;
+
+        assertTrue( bottom.equals( bottomClone ) ) ;
+        assertTrue( middle.equals( middleClone ) ) ;
+        assertTrue( top.equals( topClone ) ) ;
+     }
+
+
+    public void testEquals2()
+    {
+        DefaultMutableTupleNode top = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode middle = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode bottom = new DefaultMutableTupleNode() ;
+        top.insert( (MutableTupleNode) middle, 0 ) ;
+        middle.setParent( (MutableTupleNode) top) ;
+        middle.insert( (MutableTupleNode) bottom, 0 ) ;
+        bottom.setParent( (MutableTupleNode) middle) ;
+
+        Tuple middleTuple = new Tuple( 1, 3, TypeClass.APPLICATION  ) ;
+        Tuple topTuple = new Tuple ( 2, 5, TypeClass.APPLICATION ) ;
+        Tuple bottomTuple = new Tuple( 3, 1, true, TypeClass.APPLICATION ) ;
+
+        bottomTuple.typeClass = TypeClass.UNIVERSAL ;
+        top.setTuple( topTuple ) ;
+        middle.setTuple( middleTuple ) ;
+        bottom.setTuple( bottomTuple ) ;
+
+        assertTrue( top.equals( top ) ) ;
+
+        top.printDepthFirst(new StringBuffer(), 0 ) ;
+
+        DefaultMutableTupleNode topClone = new DefaultMutableTupleNode() ;
+        topClone.setTuple( topTuple ) ;
+        assertFalse( top.equals(topClone)) ;
+
+        topClone = new DefaultMutableTupleNode() ;
+        topClone.setTuple( bottomTuple ) ;
+        assertFalse( top.equals(topClone)) ;
+
+        topClone = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode middleClone = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode bottomClone = new DefaultMutableTupleNode() ;
+        topClone.insert( (MutableTupleNode) middleClone, 0 ) ;
+        middleClone.setParent( (MutableTupleNode) topClone) ;
+        middleClone.insert( (MutableTupleNode) bottomClone, 0 ) ;
+        bottomClone.setParent( (MutableTupleNode) middleClone) ;
+
+        Tuple middleTupleClone = new Tuple( 1, 3, TypeClass.APPLICATION  ) ;
+        Tuple topTupleClone = new Tuple ( 2, 5, TypeClass.APPLICATION ) ;
+        Tuple bottomTupleClone= new Tuple( 3, 1, true, TypeClass.APPLICATION ) ;
+
+        bottomTupleClone.typeClass = TypeClass.UNIVERSAL ;
+        topClone.setTuple( topTupleClone ) ;
+        middleClone.setTuple( bottomTupleClone ) ;
+        bottomClone.setTuple( middleTupleClone ) ;
+
+        assertFalse( bottom.equals( bottomClone ) ) ;
+        assertFalse( middle.equals( middleClone ) ) ;
+        assertFalse( top.equals( topClone ) ) ;
+        assertFalse( top.equals( new Object() ) ) ;
+        assertFalse( top.equals( null ) ) ;
+     }
+
+
+    /*
+     * Class to test for String toString()
+     */
+    public void testToString()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode end = new DefaultMutableTupleNode() ;
+        parent.insert( end, 0 ) ;
+        end.setParent( parent) ;
+        parent.insert( node, 0 ) ;
+        node.setParent( parent) ;
+
+        Tuple primitive = new Tuple( 1, 3, true, TypeClass.APPLICATION ) ;
+        Tuple constructed = new Tuple ( 2, TypeClass.APPLICATION ) ;
+        Tuple terminator = new Tuple( 0, 0, true, TypeClass.UNIVERSAL ) ;
+        terminator.typeClass = TypeClass.UNIVERSAL ;
+        parent.setTuple( constructed ) ;
+        node.setTuple( primitive ) ;
+        end.setTuple( terminator ) ;
+
+        assertNotNull( parent.toString() ) ;
+    }
+
+    public void testSetTupleTupleList()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode end = new DefaultMutableTupleNode() ;
+        parent.insert( end, 0 ) ;
+        end.setParent( parent) ;
+        parent.insert( node, 0 ) ;
+        node.setParent( parent) ;
+
+        Tuple primitive = new Tuple( 1, 3, true, TypeClass.APPLICATION ) ;
+        Tuple constructed = new Tuple ( 2, TypeClass.APPLICATION ) ;
+        Tuple terminator = new Tuple( 0, 0, true, TypeClass.UNIVERSAL ) ;
+        terminator.typeClass = TypeClass.UNIVERSAL ;
+        parent.setTuple( constructed ) ;
+        node.setTuple( primitive, Collections.EMPTY_LIST ) ;
+        end.setTuple( terminator ) ;
+
+        assertTrue( node.getValueChunks().isEmpty() ) ;
+
+
+        node.addValueChunk( ByteBuffer.allocate( 3 ) ) ;
+        assertFalse( node.getValueChunks().isEmpty() ) ;
+    }
+
+    public void testChildren()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode end = new DefaultMutableTupleNode() ;
+        parent.insert( end, 0 ) ;
+        end.setParent( parent) ;
+        parent.insert( node, 0 ) ;
+        node.setParent( parent) ;
+
+        Tuple primitive = new Tuple( 1, 3, true, TypeClass.APPLICATION ) ;
+        Tuple constructed = new Tuple ( 2, TypeClass.APPLICATION ) ;
+        Tuple terminator = new Tuple( 0, 0, true, TypeClass.UNIVERSAL ) ;
+        terminator.typeClass = TypeClass.UNIVERSAL ;
+        parent.setTuple( constructed ) ;
+        node.setTuple( primitive ) ;
+        end.setTuple( terminator ) ;
+
+        Iterator list = parent.getChildren() ;
+
+        assertEquals( node, list.next() ) ;
+        assertEquals( end, list.next() ) ;
+
+        try
+        {
+            list.next() ;
+            fail( "should never get here due to thrown exception" ) ;
+        }
+        catch( Throwable t )
+        {
+            assertNotNull( t ) ;
+        }
+    }
+
+    public void testGetAllowsChildren()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode end = new DefaultMutableTupleNode() ;
+        parent.insert( end, 0 ) ;
+        end.setParent( parent) ;
+        parent.insert( node, 0 ) ;
+        node.setParent( parent) ;
+
+        Tuple primitive = new Tuple( 1, 3, true, TypeClass.APPLICATION ) ;
+        Tuple constructed = new Tuple ( 2, TypeClass.APPLICATION ) ;
+        Tuple terminator = new Tuple( 0, 0, true, TypeClass.UNIVERSAL ) ;
+        terminator.typeClass = TypeClass.UNIVERSAL ;
+        parent.setTuple( constructed ) ;
+        node.setTuple( primitive ) ;
+        end.setTuple( terminator ) ;
+    }
+
+    public void testAddFirst()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode end = new DefaultMutableTupleNode() ;
+        parent.addFront( end ) ;
+        end.setParent( parent) ;
+        parent.addFront( node ) ;
+        node.setParent( parent) ;
+
+        Tuple primitive = new Tuple( 1, 3, true, TypeClass.APPLICATION ) ;
+        Tuple constructed = new Tuple ( 2, TypeClass.APPLICATION ) ;
+        Tuple terminator = new Tuple( 0, 0, true, TypeClass.UNIVERSAL ) ;
+        terminator.typeClass = TypeClass.UNIVERSAL ;
+        parent.setTuple( constructed ) ;
+        node.setTuple( primitive ) ;
+        end.setTuple( terminator ) ;
+        parent.toDepthFirstString() ;
+    }
+
+
+    public void testAddLast()
+    {
+        DefaultMutableTupleNode parent = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode node = new DefaultMutableTupleNode() ;
+        DefaultMutableTupleNode end = new DefaultMutableTupleNode() ;
+        parent.addFront( node ) ;
+        node.setParent( (MutableTupleNode) parent) ;
+        parent.addLast( end ) ;
+        end.setParent( (MutableTupleNode) parent) ;
+
+        Tuple primitive = new Tuple( 1, 3, true, TypeClass.APPLICATION ) ;
+        Tuple constructed = new Tuple ( 2, TypeClass.APPLICATION ) ;
+        Tuple terminator = new Tuple( 0, 0, true, TypeClass.UNIVERSAL ) ;
+        terminator.typeClass = TypeClass.UNIVERSAL ;
+        parent.setTuple( constructed ) ;
+        node.setTuple( primitive ) ;
+        end.setTuple(terminator) ;
+    }
+}

Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/EncodeDecodeTests.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/EncodeDecodeTests.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/EncodeDecodeTests.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/EncodeDecodeTests.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,67 @@
+/*
+ *   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.asn1.ber ;
+
+
+import junit.framework.TestCase;
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+
+/**
+ * Testing out round trip encode decode.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 158144 $
+ */
+public class EncodeDecodeTests extends TestCase
+{
+    public void testAbandonRequest() throws Exception
+    {
+        byte[] pdu = {0x30, 0x06, 0x02, 0x01, 0x01, 0x50, 0x01, 0x03};
+
+        DefaultMutableTupleNode root = ( DefaultMutableTupleNode )
+            TupleTreeDecoder.treeDecode( ByteBuffer.wrap( pdu ) ) ;
+        
+        ByteBuffer buf = ByteBuffer.allocate( root.size() ) ;
+        root.encode( buf ) ;
+        buf.flip();
+        byte[] actual = new byte[buf.remaining()] ;
+        buf.get( actual ) ;
+
+        assertTrue( Arrays.equals( pdu, actual ) );
+    }
+
+
+    public void testBindRequest() throws Exception
+    {
+        byte[] pdu = {0x30, 0x33, 0x02, 0x01, 0x01, 0x60, 0x2E, 0x02, 0x01, 0x03, 0x04, 0x1F, 0x75, 0x69, 0x64, 0x3D, 0x61, 0x6B, 0x61, 0x72, 0x61, 0x73, 0x75, 0x6C, 0x75, 0x2C, 0x64, 0x63, 0x3D, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2C, 0x64, 0x63, 0x3D, 0x63, 0x6F, 0x6D, 0xFFFFFF80, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64};
+
+         DefaultMutableTupleNode root =
+                 (DefaultMutableTupleNode) TupleTreeDecoder.treeDecode( ByteBuffer.wrap( pdu ) );
+
+        ByteBuffer buf = ByteBuffer.allocate( root.size() ) ;
+        root.encode( buf ) ;
+        buf.flip() ;
+        byte[] actual = new byte[buf.remaining()] ;
+        buf.get( actual ) ;
+
+        assertTrue( Arrays.equals( pdu, actual ) );
+    }
+}

Added: directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/LdapMessageTests.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/LdapMessageTests.java?rev=234226&view=auto
==============================================================================
--- directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/LdapMessageTests.java (added)
+++ directory/asn1/branches/asn1-new-ber/ber/src/test/org/apache/asn1/ber/LdapMessageTests.java Sun Aug 21 08:03:46 2005
@@ -0,0 +1,268 @@
+/*
+ *   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.asn1.ber ;
+
+
+import org.apache.asn1.codec.stateful.CallbackHistory;
+
+import java.nio.ByteBuffer;
+
+
+/**
+ * $todo$ doc me
+ *
+ * @author <a href="mailto:dev@directory.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 158144 $
+ */
+public class LdapMessageTests extends AbstractDecoderTestCase
+{
+    TupleTreeDecoder ttd = null ;
+    CallbackHistory history = null ;
+    
+    
+    public LdapMessageTests()
+    {
+        super ( LdapMessageTests.class.getName() ) ;
+    }
+    
+    
+    /* (non-Javadoc)
+     * @see org.apache.asn1.ber.AbstractDecoderTestCase#setUp()
+     */
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        
+        ttd = new TupleTreeDecoder() ;
+        history = new CallbackHistory( 1 ) ;
+        ttd.setCallback( history ) ;
+    }
+    
+    
+    /* (non-Javadoc)
+     * @see org.apache.asn1.ber.AbstractDecoderTestCase#tearDown()
+     */
+    protected void tearDown() throws Exception
+    {
+        super.tearDown();
+        
+        ttd = null ;
+        history = null ;
+    }
+    
+    
+    /**
+     * Creates a tuple node tree from a message.
+     * 
+     * @param pdu the message to encode and decode
+     * @return
+     * @throws Exception
+     */
+    public DefaultMutableTupleNode roundTripTest( byte[] pdu ) throws Exception
+    {
+        DefaultMutableTupleNode node = null ;
+        ByteBuffer buf1 = ByteBuffer.wrap( pdu ) ;
+        ttd.decode( buf1 ) ;
+        node = ( DefaultMutableTupleNode ) history.getMostRecent() ;
+
+        ByteBuffer buf2 = ByteBuffer.allocate( node.size() ) ;
+        node.encode( buf2 ) ;
+        assertTrue( buf1.equals( buf2 ) ) ;
+
+        return node ;
+    }
+
+    public void testBindMessage() throws Exception
+    {
+        byte[] pdu = {0x30, 0x33, 0x02, 0x01, 0x01, 0x60, 0x2E, 0x02, 0x01, 0x03, 0x04, 0x1F, 0x75, 0x69, 0x64, 0x3D, 0x61, 0x6B, 0x61, 0x72, 0x61, 0x73, 0x75, 0x6C, 0x75, 0x2C, 0x64, 0x63, 0x3D, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2C, 0x64, 0x63, 0x3D, 0x63, 0x6F, 0x6D, 0xFFFFFF80, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64};
+
+        decode( pdu ) ;
+        roundTripTest( pdu ) ;
+        assertFalse( tlvList.isEmpty() ) ;
+    }
+
+    public void testAddMessage() throws Exception
+    {
+        byte[] pdu = {0x30, 0x6D, 0x02, 0x01, 0x01, 0x68, 0x68, 0x04, 0x1F, 0x75, 0x69, 0x64, 0x3D, 0x61, 0x6B, 0x61, 0x72, 0x61, 0x73, 0x75, 0x6C, 0x75, 0x2C, 0x64, 0x63, 0x3D, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2C, 0x64, 0x63, 0x3D, 0x63, 0x6F, 0x6D, 0x30, 0x45, 0x30, 0x0F, 0x04, 0x05, 0x61, 0x74, 0x74, 0x72, 0x31, 0x31, 0x06, 0x04, 0x04, 0x76, 0x61, 0x6C, 0x30, 0x30, 0x15, 0x04, 0x05, 0x61, 0x74, 0x74, 0x72, 0x32, 0x31, 0x0C, 0x04, 0x04, 0x76, 0x61, 0x6C, 0x31, 0x04, 0x04, 0x76, 0x61, 0x6C, 0x30, 0x30, 0x1B, 0x04, 0x05, 0x61, 0x74, 0x74, 0x72, 0x30, 0x31, 0x12, 0x04, 0x04, 0x76, 0x61, 0x6C, 0x30, 0x04, 0x04, 0x76, 0x61, 0x6C, 0x31, 0x04, 0x04, 0x76, 0x61, 0x6C, 0x32};
+
+        decode( pdu ) ;
+        roundTripTest( pdu ) ;
+        assertFalse( tlvList.isEmpty() ) ;
+    }
+
+
+    public void testDeleteMessage() throws Exception
+    {
+        byte[] pdu = {0x30, 0x24, 0x02, 0x01, 0x01, 0x4A, 0x1F, 0x75, 0x69, 0x64, 0x3D, 0x61, 0x6B, 0x61, 0x72, 0x61, 0x73, 0x75, 0x6C, 0x75, 0x2C, 0x64, 0x63, 0x3D, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2C, 0x64, 0x63, 0x3D, 0x63, 0x6F, 0x6D};
+
+        decode( pdu ) ;
+        roundTripTest( pdu ) ;
+        assertFalse( tlvList.isEmpty() ) ;
+    }
+
+
+    public void testAbandonMessage() throws Exception
+    {
+        byte[] pdu = new byte[] {0x30, 0x06, 0x02, 0x01, 0x01, 0x50, 0x01, 0x03};
+
+        decode( pdu ) ;
+        roundTripTest( pdu ) ;
+        assertFalse( tlvList.isEmpty() ) ;
+    }
+
+
+    public void testCompareMessage() throws Exception
+    {
+        byte[] pdu = {0x30, 0x3D, 0x02, 0x01, 0x01, 0x6E, 0x38, 0x04, 0x1F, 0x75, 0x69, 0x64, 0x3D, 0x61, 0x6B, 0x61, 0x72, 0x61, 0x73, 0x75, 0x6C, 0x75, 0x2C, 0x64, 0x63, 0x3D, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2C, 0x64, 0x63, 0x3D, 0x63, 0x6F, 0x6D, 0x30, 0x15, 0x04, 0x08, 0x74, 0x65, 0x73, 0x74, 0x61, 0x74, 0x74, 0x72, 0x04, 0x09, 0x74, 0x65, 0x73, 0x74, 0x76, 0x61, 0x6C, 0x75, 0x65};
+
+        decode( pdu ) ;
+        roundTripTest( pdu ) ;
+        assertFalse( tlvList.isEmpty() ) ;
+    }
+
+
+    public void testExtendedMessage() throws Exception
+    {
+        byte[] pdu = {0x30, 0x22, 0x02, 0x01, 0x01, 0x77, 0x1D, 0xFFFFFF80, 0x0E, 0x31, 0x32, 0x33, 0x34, 0x2E, 0x31, 0x32, 0x33, 0x34, 0x2E, 0x31, 0x33, 0x32, 0x34, 0xFFFFFF81, 0x0B, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64};
+
+        decode( pdu ) ;
+        roundTripTest( pdu ) ;
+        assertFalse( tlvList.isEmpty() ) ;
+    }
+
+
+    public void testModifyDnMessage() throws Exception
+    {
+        byte[] pdu = {0x30, 0x45, 0x02, 0x01, 0x01, 0x6C, 0x40, 0x04, 0x1F, 0x75, 0x69, 0x64, 0x3D, 0x61, 0x6B, 0x61, 0x72, 0x61, 0x73, 0x75, 0x6C, 0x75, 0x2C, 0x64, 0x63, 0x3D, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2C, 0x64, 0x63, 0x3D, 0x63, 0x6F, 0x6D, 0x04, 0x07, 0x75, 0x69, 0x64, 0x3D, 0x61, 0x6F, 0x6B, 0x01, 0x01, 0xFFFFFFFF, 0xFFFFFF80, 0x11, 0x64, 0x63, 0x3D, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2C, 0x64, 0x63, 0x3D, 0x63, 0x6F, 0x6D};
+
+        decode( pdu ) ;
+        roundTripTest( pdu ) ;
+        assertFalse( tlvList.isEmpty() ) ;
+    }
+
+
+    /* ------------------------------------------------------------------------
+     
+Here's what the modify message looks like:
+     
+LDAP Modify Operation without any modification items
+dn = "uid=akarasulu,dc=example,dc=com"
+message id = 17
+
+[
+0  -> 48  0x30, 0011 0000 (const) Universal id = 16 
+                Sequence and Sequence-of types
+1  -> 42  0x2a, 0100 0010 length = 42
+
+   [
+    2  -> 2   0x2,  0000 0010 (prim) Universal id = 2, Integer type
+    3  -> 1   0x1,  0000 0001 length = 1 
+    4  -> 17  0x11, 0001 0001 integer value = 17
+   ]
+
+   [
+    5  -> 102 0x66, 0110 0110 (const) Application id = 6
+                    LDAPv3 RFC 2251 defines APPLICATION 6 for ModifyRequests
+    6  -> 35  0x23, 0010 0011 length = 35
+       [
+           7  -> 4   0x4,  0000 0100  ( prim ) UNIVERSAL id = 4
+                           Octetstring type
+           8  -> 31  0x1f, 0001 1111  length = 31
+
+           Octetstring = "uid=akarasulu,dc=example,dc=com"
+
+           [117, 105, 100, 61, 97, 107, 97, 114, 97, 115, 117, 108, 117, 44, 
+            100, 99, 61, 101, 120, 97, 109, 112, 108, 101, 44, 100, 99, 61, 
+            99, 111, 109]
+
+
+            9  -> 117 0x75, 
+            10 -> 105 0x69, 
+            11 -> 100 0x64, 
+            12 -> 61  0x3d, 
+            13 -> 97  0x61, 
+            14 -> 107 0x6b, 
+            15 -> 97  0x61, 
+            16 -> 114 0x72, 
+            17 -> 97  0x61, 
+            18 -> 115 0x73, 
+            19 -> 117 0x75, 
+            20 -> 108 0x6c, 
+            21 -> 117 0x75, 
+            22 -> 44  0x2c, 
+            23 -> 100 0x64, 
+            24 -> 99  0x63, 
+            25 -> 61  0x3d, 
+            26 -> 101 0x65, 
+            27 -> 120 0x78, 
+            28 -> 97  0x61, 
+            29 -> 109 0x6d, 
+            30 -> 112 0x70, 
+            31 -> 108 0x6c, 
+            32 -> 101 0x65, 
+            33 -> 44  0x2c, 
+            34 -> 100 0x64, 
+            35 -> 99  0x63, 
+            36 -> 61  0x3d, 
+            37 -> 99  0x63, 
+            38 -> 111 0x6f, 
+            39 -> 109 0x6d,
+
+38    33    40 -> 48  0x30, 0011 0000 (const) Universal id = 16
+                            Sequence and Sequence-of types
+39    34    41 -> 0   0x0,  length = 0
+    
+        ]
+    
+    [
+40  42 -> -96 0xa0, 1010 0000 (prim) Context-specific id = 0
+                    "controls" part of the LDAP message Envelope
+                    is specified as context-specific [0] when no
+                    no type class name is explicitly given
+
+        LDAPMessage ::= SEQUENCE 
+        {
+            messageID INTEGER,
+            protocolOp CHOICE { ... },
+            controls [0] Controls OPTIONAL
+        }
+
+41  43 -> 0   0x0   length = 0
+    ]
+]
+     
+     ------------------------------------------------------------------------ */
+    public void testModifyMessage() throws Exception
+    {
+        byte[] pdu = {0x30, 0x28, 0x02, 0x01, 0x11, 0x66, 0x23, 0x04, 0x1F, 0x75, 0x69, 0x64, 0x3D, 0x61, 0x6B, 0x61, 0x72, 0x61, 0x73, 0x75, 0x6C, 0x75, 0x2C, 0x64, 0x63, 0x3D, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2C, 0x64, 0x63, 0x3D, 0x63, 0x6F, 0x6D, 0x30, 0x00};
+
+        decode( pdu ) ;
+        roundTripTest( pdu ) ;
+        assertFalse( tlvList.isEmpty() ) ;
+    }
+
+
+    public void testSearchMessage() throws Exception
+    {
+        byte[] pdu = {0x30, 0x27, 0x02, 0x01, 0x01, 0x63, 0x22, 0x04, 0x06, 0x64, 0x63, 0x3D, 0x63, 0x6F, 0x6D, 0x0A, 0x01, 0x00, 0x0A, 0x01, 0x03, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x01, 0x01, 0x00, 0xFFFFFF87, 0x07, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x30, 0x30, 0x00};
+
+        decode( pdu ) ;
+        roundTripTest( pdu ) ;
+        assertFalse( tlvList.isEmpty() ) ;
+    }
+}



Mime
View raw message