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.digester ;
18  
19  
20  import junit.framework.TestCase ;
21  
22  import java.util.List ;
23  
24  import org.apache.commons.collections.primitives.IntStack;
25  import org.apache.asn1.ber.digester.AbstractRule;
26  import org.apache.asn1.ber.digester.BERDigester;
27  import org.apache.asn1.ber.digester.RulesBase;
28  
29  
30  /***
31   * A test case for the RulesBase.
32   *
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   * @version $Rev: 157644 $
35   */
36  public class RulesBaseTest extends TestCase
37  {
38      int[] p0 = { 1, 2, 3 } ;
39      IntStack s0 = new IntStack( p0 ) ;
40      int[] p1 = { 1, 6, 7, 8 } ;
41      IntStack s1 = new IntStack( p1 ) ;
42      int[] p2 = { 4, 5, 6 } ;
43      IntStack s2 = new IntStack( p2 ) ;
44      MockRule r0 = new MockRule() ;
45      MockRule r1 = new MockRule() ;
46      MockRule r2 = new MockRule() ;
47      RulesBase rulesBase ;
48  
49  
50      /***
51       * Sets up the decoder rulesBase system.
52       *
53       * @throws Exception
54       */
55      public void setUp() throws Exception
56      {
57          super.setUp() ;
58  
59          rulesBase = new RulesBase() ;
60      }
61  
62  
63      /***
64       * Clears and nulls out the rulesBase.
65       *
66       * @throws Exception
67       */
68      public void tearDown() throws Exception
69      {
70          super.tearDown() ;
71  
72          rulesBase.clear() ;
73          rulesBase = null ;
74      }
75  
76  
77      /***
78       * Tests the RulesBase.add(int[],Rule) method.
79       */
80      public void testAdd()
81      {
82  
83          assertTrue( "Should be empty on creation", rulesBase.rules().isEmpty() ) ;
84  
85          rulesBase.add( p0, r0 ) ;
86          assertFalse( rulesBase.rules().isEmpty() ) ;
87          assertEquals( "Should have 1 rule  after 1st add", 1, rulesBase.rules().size() ) ;
88          assertSame( "1st rule should be r0", rulesBase.rules().get( 0 ), r0 ) ;
89  
90          rulesBase.add( p1, r1 ) ;
91          assertFalse( rulesBase.rules().isEmpty() ) ;
92          assertEquals( "Should have 2 rules after 2nd add", 2, rulesBase.rules().size() ) ;
93          assertSame( "2nd rule should be r1", rulesBase.rules().get( 1 ), r1 ) ;
94  
95          rulesBase.add( p2, r2 ) ;
96          assertFalse( rulesBase.rules().isEmpty() ) ;
97          assertEquals( "Should have 3 rules after 3rd add", 3, rulesBase.rules().size() ) ;
98          assertSame( "3rd rule should be r2", rulesBase.rules().get( 2 ), r2 ) ;
99      }
100 
101 
102     /***
103      * Tests the RulesBase.match(int[]) method.
104      */
105     public void testMatchint()
106     {
107         List matched = null ;
108 
109         matched = rulesBase.match( p0 ) ;
110         assertTrue( "match on p0 should not return any rules", matched.isEmpty() ) ;
111         rulesBase.add( p0, r0 ) ;
112         matched = rulesBase.match( p0 ) ;
113         assertSame( "match on p0 should return r0 only", matched.get( 0 ), r0 ) ;
114         assertEquals( "match on p0 should only match for one rule", 1, matched.size() ) ;
115 
116         matched = rulesBase.match( p1 ) ;
117         assertTrue( "match on p1 should not return any rules", matched.isEmpty() ) ;
118         rulesBase.add( p1, r1 ) ;
119         matched = rulesBase.match( p1 ) ;
120         assertSame( "match on p1 should return r1 only", matched.get( 0 ), r1 ) ;
121         assertEquals( "match on p1 should only match for one rule", 1, matched.size() ) ;
122 
123         matched = rulesBase.match( p2 ) ;
124         assertTrue( "match on p2 should not return any rules", matched.isEmpty() ) ;
125         rulesBase.add( p2, r2 ) ;
126         matched = rulesBase.match( p2 ) ;
127         assertSame( "match on p2 should return r2 only", matched.get( 0 ), r2 ) ;
128         assertEquals( "match on p2 should only match for one rule", 1, matched.size() ) ;
129     }
130 
131 
132     /***
133      * Tests the RulesBase.match(int[]) method.
134      */
135     public void testMatchIntStack()
136     {
137         List matched = null ;
138 
139         matched = rulesBase.match( s0 ) ;
140         assertTrue( "match on s0 should not return any rules", matched.isEmpty() ) ;
141         rulesBase.add( p0, r0 ) ;
142         matched = rulesBase.match( s0 ) ;
143         assertSame( "match on s0 should return r0 only", matched.get( 0 ), r0 ) ;
144         assertEquals( "match on s0 should only match for one rule", 1, matched.size() ) ;
145 
146         matched = rulesBase.match( s1 ) ;
147         assertTrue( "match on s1 should not return any rules", matched.isEmpty() ) ;
148         rulesBase.add( p1, r1 ) ;
149         matched = rulesBase.match( s1 ) ;
150         assertSame( "match on s1 should return r1 only", matched.get( 0 ), r1 ) ;
151         assertEquals( "match on s1 should only match for one rule", 1, matched.size() ) ;
152 
153         matched = rulesBase.match( s2 ) ;
154         assertTrue( "match on s2 should not return any rules", matched.isEmpty() ) ;
155         rulesBase.add( p2, r2 ) ;
156         matched = rulesBase.match( s2 ) ;
157         assertSame( "match on s2 should return r2 only", matched.get( 0 ), r2 ) ;
158         assertEquals( "match on s2 should only match for one rule", 1, matched.size() ) ;
159     }
160 
161 
162     public void testGetDigester()
163     {
164         assertNull( rulesBase.getDigester() ) ;
165     }
166 
167 
168     public void testSetDigester()
169     {
170         assertNull( rulesBase.getDigester() ) ;
171         BERDigester digester = new BERDigester() ;
172         rulesBase.setDigester( digester ) ;
173         assertSame( digester, rulesBase.getDigester() ) ;
174     }
175 
176 
177     class MockRule extends AbstractRule {}
178 }