KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > dsmlv2 > searchResponse > searchResultEntry > SearchResultEntryTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.dsmlv2.searchResponse.searchResultEntry;
22
23
24 import javax.naming.NamingEnumeration JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26 import javax.naming.directory.Attribute JavaDoc;
27 import javax.naming.directory.Attributes JavaDoc;
28
29 import org.apache.directory.ldapstudio.dsmlv2.AbstractResponseTest;
30 import org.apache.directory.ldapstudio.dsmlv2.Dsmlv2ResponseParser;
31 import org.apache.directory.ldapstudio.dsmlv2.reponse.SearchResponse;
32 import org.apache.directory.shared.ldap.codec.Control;
33 import org.apache.directory.shared.ldap.codec.search.SearchResultEntry;
34 import org.apache.directory.shared.ldap.util.StringTools;
35
36
37 /**
38  * Tests for the Search Result Entry Response parsing
39  *
40  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
41  * @version $Rev$, $Date$
42  */

43 public class SearchResultEntryTest extends AbstractResponseTest
44 {
45     /**
46      * Test parsing of a response with a (optional) Control element
47      */

48     public void testResponseWith1Control()
49     {
50         Dsmlv2ResponseParser parser = null;
51         try
52         {
53             parser = new Dsmlv2ResponseParser();
54
55             parser.setInputFile( SearchResultEntryTest.class.getResource( "response_with_1_control.xml" ).getFile() );
56
57             parser.parse();
58         }
59         catch ( Exception JavaDoc e )
60         {
61             fail( e.getMessage() );
62         }
63
64         SearchResultEntry searchResultEntry = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
65             .getCurrentSearchResultEntry();
66
67         assertEquals( 1, searchResultEntry.getControls().size() );
68
69         Control control = searchResultEntry.getCurrentControl();
70
71         assertTrue( control.getCriticality() );
72
73         assertEquals( "1.2.840.113556.1.4.643", control.getControlType() );
74
75         assertEquals( "Some text", StringTools.utf8ToString( ( byte[] ) control.getControlValue() ) );
76     }
77     
78     /**
79      * Test parsing of a response with a (optional) Control element with empty value
80      */

81     public void testResponseWith1ControlEmptyValue()
82     {
83         Dsmlv2ResponseParser parser = null;
84         try
85         {
86             parser = new Dsmlv2ResponseParser();
87
88             parser.setInputFile( SearchResultEntryTest.class.getResource( "response_with_1_control_empty_value.xml" ).getFile() );
89
90             parser.parse();
91         }
92         catch ( Exception JavaDoc e )
93         {
94             fail( e.getMessage() );
95         }
96
97         SearchResultEntry searchResultEntry = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
98             .getCurrentSearchResultEntry();
99         Control control = searchResultEntry.getCurrentControl();
100         
101         assertEquals( 1, searchResultEntry.getControls().size() );
102         assertTrue( control.getCriticality() );
103         assertEquals( "1.2.840.113556.1.4.643", control.getControlType() );
104         assertEquals( StringTools.EMPTY_BYTES, ( byte[] ) control.getControlValue() );
105     }
106
107
108     /**
109      * Test parsing of a response with 2 (optional) Control elements
110      */

111     public void testResponseWith2Controls()
112     {
113         Dsmlv2ResponseParser parser = null;
114         try
115         {
116             parser = new Dsmlv2ResponseParser();
117
118             parser.setInputFile( SearchResultEntryTest.class.getResource( "response_with_2_controls.xml" ).getFile() );
119
120             parser.parse();
121         }
122         catch ( Exception JavaDoc e )
123         {
124             fail( e.getMessage() );
125         }
126
127         SearchResultEntry searchResultEntry = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
128             .getCurrentSearchResultEntry();
129
130         assertEquals( 2, searchResultEntry.getControls().size() );
131
132         Control control = searchResultEntry.getCurrentControl();
133
134         assertFalse( control.getCriticality() );
135
136         assertEquals( "1.2.840.113556.1.4.789", control.getControlType() );
137
138         assertEquals( "Some other text", StringTools.utf8ToString( ( byte[] ) control.getControlValue() ) );
139     }
140
141
142     /**
143      * Test parsing of a response with 3 (optional) Control elements without value
144      */

145     public void testResponseWith3ControlsWithoutValue()
146     {
147         Dsmlv2ResponseParser parser = null;
148         try
149         {
150             parser = new Dsmlv2ResponseParser();
151
152             parser.setInputFile( SearchResultEntryTest.class.getResource( "response_with_3_controls_without_value.xml" )
153                 .getFile() );
154
155             parser.parse();
156         }
157         catch ( Exception JavaDoc e )
158         {
159             fail( e.getMessage() );
160         }
161
162         SearchResultEntry searchResultEntry = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
163             .getCurrentSearchResultEntry();
164
165         assertEquals( 3, searchResultEntry.getControls().size() );
166
167         Control control = searchResultEntry.getCurrentControl();
168
169         assertTrue( control.getCriticality() );
170
171         assertEquals( "1.2.840.113556.1.4.456", control.getControlType() );
172
173         assertEquals( StringTools.EMPTY_BYTES, control.getControlValue() );
174     }
175
176
177     /**
178      * Test parsing of a response without dn Attribute
179      */

180     public void testResponseWithoutDnAttribute()
181     {
182         testParsingFail( SearchResultEntryTest.class, "response_without_dn_attribute.xml" );
183     }
184
185
186     /**
187      * Test parsing of a response with wrong dn Attribute
188      */

189     public void testResponseWithWrongDnAttribute()
190     {
191         testParsingFail( SearchResultEntryTest.class, "response_with_wrong_dn_attribute.xml" );
192     }
193
194
195     /**
196      * Test parsing of a response with dn Attribute
197      */

198     public void testResponseWithDnAttribute()
199     {
200         Dsmlv2ResponseParser parser = null;
201         try
202         {
203             parser = new Dsmlv2ResponseParser();
204
205             parser.setInputFile( SearchResultEntryTest.class.getResource( "response_with_dn_attribute.xml" ).getFile() );
206
207             parser.parse();
208         }
209         catch ( Exception JavaDoc e )
210         {
211             fail( e.getMessage() );
212         }
213
214         SearchResultEntry searchResultEntry = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
215             .getCurrentSearchResultEntry();
216
217         assertEquals( "dc=example,dc=com", searchResultEntry.getObjectName().toString() );
218     }
219
220
221     /**
222      * Test parsing of a Response with the (optional) requestID attribute
223      */

224     public void testResponseWithRequestId()
225     {
226         Dsmlv2ResponseParser parser = null;
227         try
228         {
229             parser = new Dsmlv2ResponseParser();
230
231             parser.setInputFile( SearchResultEntryTest.class.getResource( "response_with_requestID_attribute.xml" )
232                 .getFile() );
233
234             parser.parse();
235         }
236         catch ( Exception JavaDoc e )
237         {
238             fail( e.getMessage() );
239         }
240
241         SearchResultEntry searchResultEntry = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
242             .getCurrentSearchResultEntry();
243
244         assertEquals( 456, searchResultEntry.getMessageId() );
245     }
246     
247     
248     /**
249      * Test parsing of a Response with the (optional) requestID attribute equals 0
250      */

251     public void testResponseWithRequestIdEquals0()
252     {
253         testParsingFail( SearchResultEntryTest.class, "response_with_requestID_equals_0.xml" );
254     }
255
256
257     /**
258      * Test parsing of a response with 0 Attr
259      */

260     public void testResponseWith0Attr()
261     {
262         Dsmlv2ResponseParser parser = null;
263         try
264         {
265             parser = new Dsmlv2ResponseParser();
266
267             parser.setInputFile( SearchResultEntryTest.class.getResource( "response_with_0_attr.xml" ).getFile() );
268
269             parser.parse();
270         }
271         catch ( Exception JavaDoc e )
272         {
273             fail( e.getMessage() );
274         }
275
276         assertTrue( true );
277     }
278
279
280     /**
281      * Test parsing of a response with 1 Attr 0 Value
282      */

283     public void testResponseWith1Attr0Value()
284     {
285         Dsmlv2ResponseParser parser = null;
286         try
287         {
288             parser = new Dsmlv2ResponseParser();
289
290             parser.setInputFile( SearchResultEntryTest.class.getResource( "response_with_1_attr_0_value.xml" )
291                 .getFile() );
292
293             parser.parse();
294         }
295         catch ( Exception JavaDoc e )
296         {
297             fail( e.getMessage() );
298         }
299
300         SearchResultEntry searchResultEntry = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
301             .getCurrentSearchResultEntry();
302
303         Attributes JavaDoc attributes = searchResultEntry.getPartialAttributeList();
304
305         assertEquals( 1, attributes.size() );
306
307         NamingEnumeration JavaDoc ne = attributes.getAll();
308
309         Attribute JavaDoc attribute = ( Attribute JavaDoc ) ne.nextElement();
310
311         assertEquals( "dc", attribute.getID() );
312     }
313
314
315     /**
316      * Test parsing of a response with 1 Attr 1 Value
317      */

318     public void testResponseWith1Attr1Value()
319     {
320         Dsmlv2ResponseParser parser = null;
321         try
322         {
323             parser = new Dsmlv2ResponseParser();
324
325             parser.setInputFile( SearchResultEntryTest.class.getResource( "response_with_1_attr_1_value.xml" )
326                 .getFile() );
327
328             parser.parse();
329         }
330         catch ( Exception JavaDoc e )
331         {
332             fail( e.getMessage() );
333         }
334
335         SearchResultEntry searchResultEntry = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
336             .getCurrentSearchResultEntry();
337
338         Attributes JavaDoc attributes = searchResultEntry.getPartialAttributeList();
339
340         assertEquals( 1, attributes.size() );
341
342         NamingEnumeration JavaDoc ne = attributes.getAll();
343
344         Attribute JavaDoc attribute = ( Attribute JavaDoc ) ne.nextElement();
345
346         assertEquals( "dc", attribute.getID() );
347
348         assertEquals( 1, attribute.size() );
349
350         NamingEnumeration JavaDoc ne2 = null;
351         try
352         {
353             ne2 = attribute.getAll();
354         }
355         catch ( NamingException JavaDoc e )
356         {
357             fail();
358         }
359
360         String JavaDoc value = ( String JavaDoc ) ne2.nextElement();
361
362         assertEquals( "example", value );
363     }
364     
365     /**
366      * Test parsing of a response with 1 Attr 1 Base64 Value
367      */

368     public void testResponseWith1Attr1Base64Value()
369     {
370         Dsmlv2ResponseParser parser = null;
371         try
372         {
373             parser = new Dsmlv2ResponseParser();
374
375             parser.setInputFile( SearchResultEntryTest.class.getResource( "response_with_1_attr_1_base64_value.xml" )
376                 .getFile() );
377
378             parser.parse();
379         }
380         catch ( Exception JavaDoc e )
381         {
382             fail( e.getMessage() );
383         }
384
385         SearchResultEntry searchResultEntry = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
386             .getCurrentSearchResultEntry();
387
388         Attributes JavaDoc attributes = searchResultEntry.getPartialAttributeList();
389
390         assertEquals( 1, attributes.size() );
391
392         NamingEnumeration JavaDoc ne = attributes.getAll();
393
394         Attribute JavaDoc attribute = ( Attribute JavaDoc ) ne.nextElement();
395
396         assertEquals( "cn", attribute.getID() );
397
398         assertEquals( 1, attribute.size() );
399
400         NamingEnumeration JavaDoc ne2 = null;
401         try
402         {
403             ne2 = attribute.getAll();
404         }
405         catch ( NamingException JavaDoc e )
406         {
407             fail();
408         }
409
410         Object JavaDoc value = ne2.nextElement();
411         assertEquals( "Emmanuel Lécharny", new String JavaDoc( (byte[]) value ) );
412     }
413     
414     
415     /**
416      * Test parsing of a response with 1 Attr 1 empty Value
417      */

418     public void testResponseWith1Attr1EmptyValue()
419     {
420         Dsmlv2ResponseParser parser = null;
421         try
422         {
423             parser = new Dsmlv2ResponseParser();
424
425             parser.setInputFile( SearchResultEntryTest.class.getResource( "response_with_1_attr_1_empty_value.xml" )
426                 .getFile() );
427
428             parser.parse();
429         }
430         catch ( Exception JavaDoc e )
431         {
432             fail( e.getMessage() );
433         }
434
435         SearchResultEntry searchResultEntry = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
436             .getCurrentSearchResultEntry();
437
438         Attributes JavaDoc attributes = searchResultEntry.getPartialAttributeList();
439
440         assertEquals( 1, attributes.size() );
441
442         NamingEnumeration JavaDoc ne = attributes.getAll();
443
444         Attribute JavaDoc attribute = ( Attribute JavaDoc ) ne.nextElement();
445
446         assertEquals( "dc", attribute.getID() );
447
448         assertEquals( 1, attribute.size() );
449
450         NamingEnumeration JavaDoc ne2 = null;
451         try
452         {
453             ne2 = attribute.getAll();
454         }
455         catch ( NamingException JavaDoc e )
456         {
457             fail();
458         }
459
460         String JavaDoc value = ( String JavaDoc ) ne2.nextElement();
461
462         assertEquals( "", value );
463     }
464
465
466     /**
467      * Test parsing of a response with 1 Attr 2 Value
468      */

469     public void testResponseWith1Attr2Value()
470     {
471         Dsmlv2ResponseParser parser = null;
472         try
473         {
474             parser = new Dsmlv2ResponseParser();
475
476             parser.setInputFile( SearchResultEntryTest.class.getResource( "response_with_1_attr_2_value.xml" )
477                 .getFile() );
478
479             parser.parse();
480         }
481         catch ( Exception JavaDoc e )
482         {
483             fail( e.getMessage() );
484         }
485
486         SearchResultEntry searchResultEntry = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
487             .getCurrentSearchResultEntry();
488
489         Attributes JavaDoc attributes = searchResultEntry.getPartialAttributeList();
490
491         assertEquals( 1, attributes.size() );
492
493         NamingEnumeration JavaDoc ne = attributes.getAll();
494
495         Attribute JavaDoc attribute = ( Attribute JavaDoc ) ne.nextElement();
496
497         assertEquals( "objectclass", attribute.getID() );
498
499         assertEquals( 2, attribute.size() );
500
501         NamingEnumeration JavaDoc ne2 = null;
502         try
503         {
504             ne2 = attribute.getAll();
505         }
506         catch ( NamingException JavaDoc e )
507         {
508             fail();
509         }
510
511         String JavaDoc value = ( String JavaDoc ) ne2.nextElement();
512
513         assertEquals( "top", value );
514
515         value = ( String JavaDoc ) ne2.nextElement();
516
517         assertEquals( "domain", value );
518     }
519
520
521     /**
522      * Test parsing of a response with 2 Attr 1 Value
523      */

524     public void testResponseWith2Attr1Value()
525     {
526         Dsmlv2ResponseParser parser = null;
527         try
528         {
529             parser = new Dsmlv2ResponseParser();
530
531             parser.setInputFile( SearchResultEntryTest.class.getResource( "response_with_2_attr_1_value.xml" )
532                 .getFile() );
533
534             parser.parse();
535         }
536         catch ( Exception JavaDoc e )
537         {
538             fail( e.getMessage() );
539         }
540
541         SearchResultEntry searchResultEntry = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
542             .getCurrentSearchResultEntry();
543
544         Attributes JavaDoc attributes = searchResultEntry.getPartialAttributeList();
545
546         assertEquals( 2, attributes.size() );
547
548         NamingEnumeration JavaDoc ne = attributes.getAll();
549
550         Attribute JavaDoc attribute = ( Attribute JavaDoc ) ne.nextElement();
551
552         assertEquals( "objectclass", attribute.getID() );
553
554         assertEquals( 1, attribute.size() );
555
556         NamingEnumeration JavaDoc ne2 = null;
557         try
558         {
559             ne2 = attribute.getAll();
560         }
561         catch ( NamingException JavaDoc e )
562         {
563             fail();
564         }
565
566         String JavaDoc value = ( String JavaDoc ) ne2.nextElement();
567
568         assertEquals( "top", value );
569
570         attribute = ( Attribute JavaDoc ) ne.nextElement();
571
572         assertEquals( "dc", attribute.getID() );
573
574         assertEquals( 1, attribute.size() );
575
576         ne2 = null;
577         try
578         {
579             ne2 = attribute.getAll();
580         }
581         catch ( NamingException JavaDoc e )
582         {
583             fail();
584         }
585
586         value = ( String JavaDoc ) ne2.nextElement();
587
588         assertEquals( "example", value );
589     }
590
591
592     /**
593      * Test parsing of a response with 1 Attr without name Attribute
594      */

595     public void testResponseWith1AttrWithoutNameAttribute()
596     {
597         testParsingFail( SearchResultEntryTest.class, "response_with_1_attr_without_name_attribute.xml" );
598     }
599 }
600
Popular Tags