1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.*;
29
30 import java.util.Set;
31 import java.util.HashSet;
32
33
34 /***
35 * Unit test cases for testing the evaluator for refinements.
36 *
37 * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
38 * @version $Rev$
39 */
40 public class RefinementEvaluatorTest extends TestCase
41 {
42 /*** the global registries */
43 private GlobalRegistries registries;
44 /*** the refinement evaluator to test */
45 private RefinementEvaluator evaluator;
46
47
48 /***
49 * Initializes the global registries.
50 * @throws javax.naming.NamingException if there is a failure loading the schema
51 */
52 private void init() throws NamingException
53 {
54 BootstrapRegistries bsRegistries = new BootstrapRegistries();
55 BootstrapSchemaLoader loader = new BootstrapSchemaLoader();
56 Set schemas = new HashSet();
57 schemas.add( new SystemSchema() );
58 schemas.add( new ApacheSchema() );
59 schemas.add( new CoreSchema() );
60 schemas.add( new CosineSchema() );
61 schemas.add( new InetorgpersonSchema() );
62 schemas.add( new JavaSchema() );
63 loader.load( schemas, bsRegistries );
64 registries = new GlobalRegistries( bsRegistries );
65 }
66
67
68 /***
69 * Initializes registries and creates the leaf evalutator
70 * @throws Exception if there are schema initialization problems
71 */
72 protected void setUp() throws Exception
73 {
74 init();
75 OidRegistry registry = registries.getOidRegistry();
76 RefinementLeafEvaluator leafEvaluator = new RefinementLeafEvaluator( registry );
77 evaluator = new RefinementEvaluator( leafEvaluator );
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 try
98 {
99 assertFalse( evaluator.evaluate( null, new BasicAttribute( "objectClass" ) ) );
100 fail( "should never get here due to an IAE" );
101 }
102 catch ( IllegalArgumentException iae )
103 {
104 }
105
106 try
107 {
108 assertFalse( evaluator.evaluate( new SimpleNode( "", "", LeafNode.EQUALITY ), null ) );
109 fail( "should never get here due to an IAE" );
110 }
111 catch ( IllegalArgumentException iae )
112 {
113 }
114
115 try
116 {
117 assertFalse( evaluator.evaluate( new SimpleNode( "", "", LeafNode.EQUALITY ),
118 new BasicAttribute( "blah") ) );
119 fail( "should never get here due to an IAE" );
120 }
121 catch ( IllegalArgumentException iae )
122 {
123 }
124 }
125
126
127 public void testMatchByName() throws Exception
128 {
129 BasicAttribute objectClasses = null;
130
131
132 objectClasses = new BasicAttribute( "objectClass", "person" );
133 assertTrue( evaluator.evaluate( new SimpleNode( "objectClass", "person", LeafNode.EQUALITY ),
134 objectClasses ) );
135
136 objectClasses = new BasicAttribute( "objectClass" );
137 objectClasses.add( "person" );
138 objectClasses.add( "blah" );
139 assertTrue( evaluator.evaluate( new SimpleNode( "objectClass", "person", LeafNode.EQUALITY ),
140 objectClasses ) );
141
142
143 objectClasses = new BasicAttribute( "objectClass", "person" );
144 assertFalse( evaluator.evaluate( new SimpleNode( "objectClass", "blah", LeafNode.EQUALITY ),
145 objectClasses ) );
146
147 objectClasses = new BasicAttribute( "objectClass", "blah" );
148 assertFalse( evaluator.evaluate( new SimpleNode( "objectClass", "person", LeafNode.EQUALITY ),
149 objectClasses ) );
150 }
151
152
153 public void testMatchByOID() throws Exception
154 {
155 BasicAttribute objectClasses = null;
156
157
158 objectClasses = new BasicAttribute( "objectClass", "person" );
159 assertTrue( evaluator.evaluate( new SimpleNode( "objectClass", "2.5.6.6", LeafNode.EQUALITY ),
160 objectClasses ) );
161
162 objectClasses = new BasicAttribute( "objectClass" );
163 objectClasses.add( "person" );
164 objectClasses.add( "blah" );
165 assertTrue( evaluator.evaluate( new SimpleNode( "objectClass", "2.5.6.6", LeafNode.EQUALITY ),
166 objectClasses ) );
167
168
169 objectClasses = new BasicAttribute( "objectClass", "person" );
170 assertFalse( evaluator.evaluate( new SimpleNode( "objectClass", "2.5.6.5", LeafNode.EQUALITY ),
171 objectClasses ) );
172
173 objectClasses = new BasicAttribute( "objectClass", "blah" );
174 assertFalse( evaluator.evaluate( new SimpleNode( "objectClass", "2.5.6.5", LeafNode.EQUALITY ),
175 objectClasses ) );
176 }
177
178
179 public void testComplexOrRefinement() throws Exception
180 {
181 ExprNode refinement = null;
182 BasicAttribute objectClasses = new BasicAttribute( "objectClass", "person" );
183 FilterParser parser = new FilterParserImpl();
184 String refStr = "(| (objectClass=person) (objectClass=organizationalUnit) )";
185 refinement = parser.parse( refStr );
186
187 assertTrue( evaluator.evaluate( refinement, objectClasses ) );
188 objectClasses = new BasicAttribute( "objectClass", "organizationalUnit" );
189 assertTrue( evaluator.evaluate( refinement, objectClasses ) );
190 objectClasses = new BasicAttribute( "objectClass", "domain" );
191 assertFalse( evaluator.evaluate( refinement, objectClasses ) );
192 }
193
194
195 public void testComplexAndRefinement() throws Exception
196 {
197 ExprNode refinement = null;
198 BasicAttribute objectClasses = new BasicAttribute( "objectClass", "person" );
199 objectClasses.add( "organizationalUnit" );
200 FilterParser parser = new FilterParserImpl();
201 String refStr = "(& (objectClass=person) (objectClass=organizationalUnit) )";
202 refinement = parser.parse( refStr );
203
204 assertTrue( evaluator.evaluate( refinement, objectClasses ) );
205 objectClasses = new BasicAttribute( "objectClass", "organizationalUnit" );
206 assertFalse( evaluator.evaluate( refinement, objectClasses ) );
207 objectClasses = new BasicAttribute( "objectClass", "person" );
208 assertFalse( evaluator.evaluate( refinement, objectClasses ) );
209 objectClasses = new BasicAttribute( "objectClass", "domain" );
210 assertFalse( evaluator.evaluate( refinement, objectClasses ) );
211 }
212
213
214 public void testComplexNotRefinement() throws Exception
215 {
216 ExprNode refinement = null;
217 BasicAttribute objectClasses = new BasicAttribute( "objectClass", "person" );
218 FilterParser parser = new FilterParserImpl();
219 String refStr = "(! (objectClass=person) )";
220 refinement = parser.parse( refStr );
221
222 assertFalse( evaluator.evaluate( refinement, objectClasses ) );
223 objectClasses = new BasicAttribute( "objectClass", "organizationalUnit" );
224 assertTrue( evaluator.evaluate( refinement, objectClasses ) );
225 objectClasses = new BasicAttribute( "objectClass", "domain" );
226 assertTrue( evaluator.evaluate( refinement, objectClasses ) );
227
228 try
229 {
230 assertFalse( evaluator.evaluate( new BranchNode( 1000 ),
231 new BasicAttribute( "objectClass" ) ) );
232 fail( "should never get here due to an IAE" );
233 }
234 catch ( IllegalArgumentException iae )
235 {
236 }
237
238 try
239 {
240 assertFalse( evaluator.evaluate( new BranchNode( BranchNode.NOT ),
241 new BasicAttribute( "objectClass" ) ) );
242 fail( "should never get here due to an IAE" );
243 }
244 catch ( IllegalArgumentException iae )
245 {
246 }
247 }
248 }