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.ldap.server.subtree;
18  
19  
20  import junit.framework.TestCase;
21  
22  import javax.naming.NamingException;
23  import javax.naming.directory.BasicAttribute;
24  
25  import org.apache.ldap.server.schema.bootstrap.*;
26  import org.apache.ldap.server.schema.GlobalRegistries;
27  import org.apache.ldap.server.schema.OidRegistry;
28  import org.apache.ldap.common.filter.SimpleNode;
29  import org.apache.ldap.common.filter.LeafNode;
30  
31  import java.util.Set;
32  import java.util.HashSet;
33  
34  
35  /***
36   * Unit test cases for testing the evaluator for refinement leaf nodes.
37   *
38   * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
39   * @version $Rev$
40   */
41  public class RefinementLeafEvaluatorTest extends TestCase
42  {
43      /*** the global registries */
44      private GlobalRegistries registries;
45      /*** the refinement leaf evaluator to test */
46      private RefinementLeafEvaluator evaluator;
47  
48  
49      /***
50       * Initializes the global registries.
51       * @throws NamingException if there is a failure loading the schema
52       */
53      private void init() throws NamingException
54      {
55          BootstrapRegistries bsRegistries = new BootstrapRegistries();
56          BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
57          Set schemas = new HashSet();
58          schemas.add( new SystemSchema() );
59          schemas.add( new ApacheSchema() );
60          schemas.add( new CoreSchema() );
61          schemas.add( new CosineSchema() );
62          schemas.add( new InetorgpersonSchema() );
63          schemas.add( new JavaSchema() );
64          loader.load( schemas, bsRegistries );
65          registries = new GlobalRegistries( bsRegistries );
66      }
67  
68  
69      /***
70       * Initializes registries and creates the leaf evalutator
71       * @throws Exception if there are schema initialization problems
72       */
73      protected void setUp() throws Exception
74      {
75          init();
76          OidRegistry registry = registries.getOidRegistry();
77          evaluator = new RefinementLeafEvaluator( registry );
78      }
79  
80  
81      /***
82       * Sets evaluator and registries to null.
83       */
84      protected void tearDown()
85      {
86          evaluator = null;
87          registries = null;
88      }
89  
90  
91      /***
92       * Test cases for various bad combinations of arguments
93       * @throws Exception if something goes wrongg
94       */
95      public void testForBadArguments() throws Exception
96      {
97          BasicAttribute objectClasses = null;
98  
99          try
100         {
101             assertFalse( evaluator.evaluate( null, null ) );
102             fail( "should never get here due to an IAE" );
103         }
104         catch ( IllegalArgumentException iae )
105         {
106         }
107 
108         try
109         {
110             assertFalse( evaluator.evaluate( new SimpleNode( "", "", LeafNode.GREATEREQ ), objectClasses ) );
111             fail( "should never get here due to an NE" );
112         }
113         catch ( NamingException ne )
114         {
115         }
116 
117         try
118         {
119             assertFalse( evaluator.evaluate( new SimpleNode( "", "", LeafNode.EQUALITY ), objectClasses ) );
120             fail( "should never get here due to an NE" );
121         }
122         catch ( NamingException ne )
123         {
124         }
125 
126         try
127         {
128             assertFalse( evaluator.evaluate( new SimpleNode( "objectClass", "", LeafNode.EQUALITY ), objectClasses ) );
129             fail( "should never get here due to an IAE" );
130         }
131         catch ( IllegalArgumentException iae )
132         {
133         }
134 
135         try
136         {
137             objectClasses = new BasicAttribute( "incorrectAttrId" );
138             assertFalse( evaluator.evaluate( new SimpleNode( "objectClass", "", LeafNode.EQUALITY ), objectClasses ) );
139             fail( "should never get here due to an IAE" );
140         }
141         catch ( IllegalArgumentException iae )
142         {
143         }
144     }
145 
146 
147     public void testMatchByName() throws Exception
148     {
149         BasicAttribute objectClasses = null;
150 
151         // positive test
152         objectClasses = new BasicAttribute( "objectClass", "person" );
153         assertTrue( evaluator.evaluate( new SimpleNode( "objectClass", "person", LeafNode.EQUALITY ),
154                 objectClasses ) );
155 
156         objectClasses = new BasicAttribute( "objectClass" );
157         objectClasses.add( "person" );
158         objectClasses.add( "blah" );
159         assertTrue( evaluator.evaluate( new SimpleNode( "objectClass", "person", LeafNode.EQUALITY ),
160                 objectClasses ) );
161 
162         // negative tests
163         objectClasses = new BasicAttribute( "objectClass", "person" );
164         assertFalse( evaluator.evaluate( new SimpleNode( "objectClass", "blah", LeafNode.EQUALITY ),
165                 objectClasses ) );
166 
167         objectClasses = new BasicAttribute( "objectClass", "blah" );
168         assertFalse( evaluator.evaluate( new SimpleNode( "objectClass", "person", LeafNode.EQUALITY ),
169                 objectClasses ) );
170     }
171 
172 
173     public void testMatchByOID() throws Exception
174     {
175         BasicAttribute objectClasses = null;
176 
177         // positive test
178         objectClasses = new BasicAttribute( "objectClass", "person" );
179         assertTrue( evaluator.evaluate( new SimpleNode( "objectClass", "2.5.6.6", LeafNode.EQUALITY ),
180                 objectClasses ) );
181 
182         objectClasses = new BasicAttribute( "objectClass" );
183         objectClasses.add( "person" );
184         objectClasses.add( "blah" );
185         assertTrue( evaluator.evaluate( new SimpleNode( "objectClass", "2.5.6.6", LeafNode.EQUALITY ),
186                 objectClasses ) );
187 
188         // negative tests
189         objectClasses = new BasicAttribute( "objectClass", "person" );
190         assertFalse( evaluator.evaluate( new SimpleNode( "objectClass", "2.5.6.5", LeafNode.EQUALITY ),
191                 objectClasses ) );
192 
193         objectClasses = new BasicAttribute( "objectClass", "blah" );
194         assertFalse( evaluator.evaluate( new SimpleNode( "objectClass", "2.5.6.5", LeafNode.EQUALITY ),
195                 objectClasses ) );
196     }
197 }