1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.asn1.ber ;
18  
19  
20  import junit.framework.TestCase;
21  import org.apache.asn1.codec.stateful.DecoderCallback;
22  import org.apache.asn1.codec.stateful.DecoderMonitorAdapter;
23  import org.apache.asn1.codec.stateful.StatefulDecoder;
24  import org.apache.commons.lang.time.StopWatch;
25  
26  import java.nio.ByteBuffer;
27  import java.util.Collections;
28  
29  
30  /***
31   * Tests the TupleTreeDecoder.
32   *
33   * @author <a href="mailto:dev@directory.apache.org">
34   * Apache Directory Project</a>
35   * @version $Rev: 158144 $
36   */
37  public class TupleTreeDecoderTest extends TestCase implements DecoderCallback
38  {
39      DefaultMutableTupleNode root = null ;
40      
41      public static void main( String[] args)
42      {
43          TupleTreeDecoderTest test = new TupleTreeDecoderTest() ;
44          
45          try { test.testTTD() ; } catch ( Exception e ) { e.printStackTrace() ; } 
46      }
47      
48      public void testSetMonitor()
49      {
50          TupleTreeDecoder decoder = new TupleTreeDecoder() ;
51          decoder.setDecoderMonitor( null ) ;
52          decoder.setDecoderMonitor( new DecoderMonitorAdapter() ) ;
53      }
54      
55  
56  
57      public void testTTD2() throws Exception
58      {
59          TupleTreeDecoder decoder = new TupleTreeDecoder() ;
60          Tuple t = new Tuple( 1, 0, true, TypeClass.APPLICATION ) ;
61          ByteBuffer encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
62          decoder.decode( encoded ) ;
63          
64          t = new Tuple( 1, 0, true, TypeClass.APPLICATION ) ;
65          encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
66          decoder.decode( encoded ) ;
67      }    
68      
69  
70      public void testTTD4() throws Exception
71      {
72          Tuple t = new Tuple( 1, 0, true, TypeClass.APPLICATION ) ;
73          ByteBuffer encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
74  
75          ByteBuffer shorter = ByteBuffer.allocate( encoded.capacity() - 1 ) ;
76          shorter.put( ( ByteBuffer ) encoded.limit( shorter.limit() - 1 ) ) ;
77          assertNull( TupleTreeDecoder.treeDecode( shorter ) ) ;
78      }    
79      
80  
81      public void testTTD() throws Exception
82      {
83          // Setup the bind request
84          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};
85  
86          StopWatch watch = new StopWatch() ;
87          watch.start() ;
88          TupleTreeDecoder decoder = new TupleTreeDecoder() ;
89          decoder.setCallback( this ) ;
90          decoder.decode( ByteBuffer.wrap( pdu ) ) ;
91          watch.stop() ;
92  
93          StringBuffer buf = new StringBuffer() ;
94          root.printDepthFirst( buf, 0 ) ;
95      }
96      
97      
98      /* (non-Javadoc)
99       * @see org.apache.asn1.codec.stateful.DecoderCallback#
100      * decodeOccurred(org.apache.asn1.codec.stateful.StatefulDecoder,
101      * java.lang.Object)
102      */
103     public void decodeOccurred( StatefulDecoder decoder, Object decoded )
104     {
105         root = ( DefaultMutableTupleNode ) decoded ;
106     }
107 }