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  
22  
23  /***
24   * Tests the 
25   *
26   * @author <a href="mailto:dev@directory.apache.org">
27   * Apache Directory Project</a>
28   * @version $Rev: 289141 $
29   */
30  public class BERDecoderStateTest extends TestCase
31  {
32  
33      public static void main(String[] args)
34      {
35          junit.textui.TestRunner.run(BERDecoderStateTest.class);
36      }
37  
38      /*
39       * @see TestCase#setUp()
40       */
41      protected void setUp() throws Exception
42      {
43          super.setUp();
44      }
45  
46      /*
47       * @see TestCase#tearDown()
48       */
49      protected void tearDown() throws Exception
50      {
51          super.tearDown();
52      }
53  
54      /***
55       * Constructor for BERDecoderStateTest.
56       * @param arg0
57       */
58      public BERDecoderStateTest(String arg0)
59      {
60          super(arg0);
61      }
62  
63      public void testGetNext()
64      {
65          BERDecoderState state = BERDecoderState.getStartState() ;
66          
67          assertEquals( BERDecoderState.TAG, state ) ;
68          state = state.getNext( true ) ;
69          assertEquals( BERDecoderState.LENGTH, state ) ;
70          state = state.getNext( true ) ;
71          assertEquals( BERDecoderState.VALUE, state ) ;
72  
73          state = state.getNext( true ) ;
74          assertEquals( BERDecoderState.TAG, state ) ;
75          state = state.getNext( true ) ;
76          assertEquals( BERDecoderState.LENGTH, state ) ;
77          state = state.getNext( false ) ;
78          assertEquals( BERDecoderState.TAG, state ) ;
79      }
80  
81      public void testIsEndState()
82      {
83          assertFalse( BERDecoderState.TAG.isEndState( true ) ) ;
84          assertFalse( BERDecoderState.TAG.isEndState( false ) ) ;
85          assertFalse( BERDecoderState.LENGTH.isEndState( true ) ) ;
86          assertTrue( BERDecoderState.LENGTH.isEndState( false ) ) ;
87          assertTrue( BERDecoderState.VALUE.isEndState( true ) ) ;
88          assertTrue( BERDecoderState.VALUE.isEndState( false ) ) ;
89      }
90  
91      public void testGetStartState()
92      {
93          assertEquals( BERDecoderState.TAG, BERDecoderState.getStartState() ) ;
94      }
95  
96      /*
97       * Class to test for BERDecoderState getTypeClass(String)
98       */
99      public void testGetStateString()
100     {
101         assertEquals( BERDecoderState.LENGTH, 
102                 BERDecoderState.getState(BERDecoderState.LENGTH.getName()) ) ;
103         assertEquals( BERDecoderState.TAG, 
104                 BERDecoderState.getState(BERDecoderState.TAG.getName()) ) ;
105         assertEquals( BERDecoderState.VALUE, 
106                 BERDecoderState.getState(BERDecoderState.VALUE.getName()) ) ;
107         
108         assertEquals( BERDecoderState.LENGTH, 
109                 BERDecoderState.getState("length") ) ;
110         assertEquals( BERDecoderState.TAG, 
111                 BERDecoderState.getState("TAG") ) ;
112         assertEquals( BERDecoderState.TAG, 
113                 BERDecoderState.getState("Tag") ) ;
114         assertEquals( BERDecoderState.VALUE, 
115                 BERDecoderState.getState("value") ) ;
116         
117         try
118         {
119             BERDecoderState.getState("asdf") ;
120             fail( "should not be reached due to thrown exception" ) ;
121         }
122         catch ( Throwable t )
123         {
124             assertNotNull( t ) ;
125         }
126     }
127 
128     /*
129      * Class to test for BERDecoderState getTypeClass(int)
130      */
131     public void testGetStateint()
132     {
133         assertEquals( BERDecoderState.LENGTH, 
134                 BERDecoderState.getState(BERDecoderState.LENGTH_VAL) ) ;
135         assertEquals( BERDecoderState.TAG, 
136                 BERDecoderState.getState(BERDecoderState.TAG_VAL) ) ;
137         assertEquals( BERDecoderState.VALUE, 
138                 BERDecoderState.getState(BERDecoderState.VALUE_VAL) ) ;
139         
140         try
141         {
142             BERDecoderState.getState( 293847 ) ;
143             fail( "should not be reached due to thrown exception" ) ;
144         }
145         catch ( Throwable t )
146         {
147             assertNotNull( t ) ;
148         }
149     }
150 }