KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > dsmlv2 > searchResponse > searchResultDone > SearchResultDoneTest


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.searchResultDone;
22
23
24 import java.util.List JavaDoc;
25
26 import javax.naming.NamingException JavaDoc;
27
28 import org.apache.directory.ldapstudio.dsmlv2.AbstractResponseTest;
29 import org.apache.directory.ldapstudio.dsmlv2.Dsmlv2ResponseParser;
30 import org.apache.directory.ldapstudio.dsmlv2.reponse.SearchResponse;
31 import org.apache.directory.shared.ldap.codec.Control;
32 import org.apache.directory.shared.ldap.codec.LdapResult;
33 import org.apache.directory.shared.ldap.codec.search.SearchResultDone;
34 import org.apache.directory.shared.ldap.message.ResultCodeEnum;
35 import org.apache.directory.shared.ldap.util.StringTools;
36
37 import com.sun.jndi.ldap.LdapURL;
38
39
40 /**
41  * Tests for the Search Result Done Response parsing
42  *
43  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
44  * @version $Rev$, $Date$
45  */

46 public class SearchResultDoneTest extends AbstractResponseTest
47 {
48     /**
49      * Test parsing of a response with a (optional) Control element
50      */

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

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

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

148     public void testResponseWith3ControlsWithoutValue()
149     {
150         Dsmlv2ResponseParser parser = null;
151         try
152         {
153             parser = new Dsmlv2ResponseParser();
154
155             parser.setInputFile( SearchResultDoneTest.class.getResource( "response_with_3_controls_without_value.xml" )
156                 .getFile() );
157
158             parser.parse();
159         }
160         catch ( Exception JavaDoc e )
161         {
162             fail( e.getMessage() );
163         }
164
165         SearchResultDone searchResultDone = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
166             .getSearchResultDone();
167
168         assertEquals( 3, searchResultDone.getControls().size() );
169
170         Control control = searchResultDone.getCurrentControl();
171
172         assertTrue( control.getCriticality() );
173
174         assertEquals( "1.2.840.113556.1.4.456", control.getControlType() );
175
176         assertEquals( StringTools.EMPTY_BYTES, control.getControlValue() );
177     }
178
179
180     /**
181      * Test parsing of a Response with the (optional) requestID attribute
182      */

183     public void testResponseWithRequestId()
184     {
185         Dsmlv2ResponseParser parser = null;
186         try
187         {
188             parser = new Dsmlv2ResponseParser();
189
190             parser.setInputFile( SearchResultDoneTest.class.getResource( "response_with_requestID_attribute.xml" )
191                 .getFile() );
192
193             parser.parse();
194         }
195         catch ( Exception JavaDoc e )
196         {
197             fail( e.getMessage() );
198         }
199
200         SearchResultDone searchResultDone = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
201             .getSearchResultDone();
202
203         assertEquals( 456, searchResultDone.getMessageId() );
204     }
205     
206     
207     /**
208      * Test parsing of a Response with the (optional) requestID attribute equals 0
209      */

210     public void testResponseWithRequestIdEquals0()
211     {
212         testParsingFail( SearchResultDoneTest.class, "response_with_requestID_equals_0.xml" );
213     }
214
215
216     /**
217      * Test parsing of a response without Result Code element
218      */

219     public void testResponseWithoutResultCode()
220     {
221         testParsingFail( SearchResultDoneTest.class, "response_without_result_code.xml" );
222     }
223
224
225     /**
226      * Test parsing of a response with Result Code element but a not integer value
227      */

228     public void testResponseWithResultCodeNotInteger()
229     {
230         testParsingFail( SearchResultDoneTest.class, "response_with_result_code_not_integer.xml" );
231     }
232
233
234     /**
235      * Test parsing of a response with Result Code
236      */

237     public void testResponseWithResultCode()
238     {
239         Dsmlv2ResponseParser parser = null;
240         try
241         {
242             parser = new Dsmlv2ResponseParser();
243
244             parser.setInputFile( SearchResultDoneTest.class.getResource( "response_with_result_code.xml" ).getFile() );
245
246             parser.parse();
247         }
248         catch ( Exception JavaDoc e )
249         {
250             fail( e.getMessage() );
251         }
252
253         SearchResultDone searchResultDone = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
254             .getSearchResultDone();
255
256         LdapResult ldapResult = searchResultDone.getLdapResult();
257
258         assertEquals( ResultCodeEnum.PROTOCOL_ERROR, ldapResult.getResultCode() );
259     }
260
261
262     /**
263      * Test parsing of a response with Error Message
264      */

265     public void testResponseWithErrorMessage()
266     {
267         Dsmlv2ResponseParser parser = null;
268         try
269         {
270             parser = new Dsmlv2ResponseParser();
271
272             parser.setInputFile( SearchResultDoneTest.class.getResource( "response_with_error_message.xml" ).getFile() );
273
274             parser.parse();
275         }
276         catch ( Exception JavaDoc e )
277         {
278             fail( e.getMessage() );
279         }
280
281         SearchResultDone searchResultDone = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
282             .getSearchResultDone();
283
284         LdapResult ldapResult = searchResultDone.getLdapResult();
285
286         assertEquals( "Unrecognized extended operation EXTENSION_OID: 1.2.6.1.4.1.18060.1.1.1.100.2", ldapResult
287             .getErrorMessage() );
288     }
289     
290     /**
291      * Test parsing of a response with empty Error Message
292      */

293     public void testResponseWithEmptyErrorMessage()
294     {
295         Dsmlv2ResponseParser parser = null;
296         try
297         {
298             parser = new Dsmlv2ResponseParser();
299
300             parser.setInputFile( SearchResultDoneTest.class.getResource( "response_with_empty_error_message.xml" ).getFile() );
301
302             parser.parse();
303         }
304         catch ( Exception JavaDoc e )
305         {
306             fail( e.getMessage() );
307         }
308
309         SearchResultDone searchResultDone = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
310             .getSearchResultDone();
311
312         LdapResult ldapResult = searchResultDone.getLdapResult();
313
314         assertNull( ldapResult.getErrorMessage() );
315     }
316
317
318     /**
319      * Test parsing of a response with a Referral
320      */

321     public void testResponseWith1Referral()
322     {
323         Dsmlv2ResponseParser parser = null;
324         try
325         {
326             parser = new Dsmlv2ResponseParser();
327
328             parser.setInputFile( SearchResultDoneTest.class.getResource( "response_with_1_referral.xml" ).getFile() );
329
330             parser.parse();
331         }
332         catch ( Exception JavaDoc e )
333         {
334             fail( e.getMessage() );
335         }
336
337         SearchResultDone searchResultDone = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
338             .getSearchResultDone();
339
340         LdapResult ldapResult = searchResultDone.getLdapResult();
341
342         List JavaDoc referrals = ldapResult.getReferrals();
343
344         assertEquals( 1, referrals.size() );
345
346         Object JavaDoc referral = referrals.get( 0 );
347
348         try
349         {
350             assertEquals( new LdapURL( "ldap://www.apache.org/" ).toString(), referral.toString() );
351         }
352         catch ( NamingException JavaDoc e )
353         {
354             fail();
355         }
356     }
357     
358     /**
359      * Test parsing of a response with an empty Referral
360      */

361     public void testResponseWith1EmptyReferral()
362     {
363         Dsmlv2ResponseParser parser = null;
364         try
365         {
366             parser = new Dsmlv2ResponseParser();
367
368             parser.setInputFile( SearchResultDoneTest.class.getResource( "response_with_1_empty_referral.xml" ).getFile() );
369
370             parser.parse();
371         }
372         catch ( Exception JavaDoc e )
373         {
374             fail( e.getMessage() );
375         }
376
377         SearchResultDone searchResultDone = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
378             .getSearchResultDone();
379
380         LdapResult ldapResult = searchResultDone.getLdapResult();
381
382         List JavaDoc referrals = ldapResult.getReferrals();
383
384         assertEquals( 0, referrals.size() );
385     }
386
387
388     /**
389      * Test parsing of a response with 2 Referral elements
390      */

391     public void testResponseWith2Referrals()
392     {
393         Dsmlv2ResponseParser parser = null;
394         try
395         {
396             parser = new Dsmlv2ResponseParser();
397
398             parser.setInputFile( SearchResultDoneTest.class.getResource( "response_with_2_referrals.xml" ).getFile() );
399
400             parser.parse();
401         }
402         catch ( Exception JavaDoc e )
403         {
404             fail( e.getMessage() );
405         }
406
407         SearchResultDone searchResultDone = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
408             .getSearchResultDone();
409
410         LdapResult ldapResult = searchResultDone.getLdapResult();
411
412         List JavaDoc referrals = ldapResult.getReferrals();
413
414         assertEquals( 2, referrals.size() );
415
416         Object JavaDoc referral = referrals.get( 0 );
417
418         try
419         {
420             assertEquals( new LdapURL( "ldap://www.apache.org/" ).toString(), referral.toString() );
421         }
422         catch ( NamingException JavaDoc e )
423         {
424             fail();
425         }
426
427         Object JavaDoc referral2 = referrals.get( 1 );
428
429         try
430         {
431             assertEquals( new LdapURL( "ldap://www.apple.com/" ).toString(), referral2.toString() );
432         }
433         catch ( NamingException JavaDoc e )
434         {
435             fail();
436         }
437     }
438
439
440     /**
441      * Test parsing of a response with a Referral and an Error Message
442      */

443     public void testResponseWith1ReferralAndAnErrorMessage()
444     {
445         Dsmlv2ResponseParser parser = null;
446         try
447         {
448             parser = new Dsmlv2ResponseParser();
449
450             parser.setInputFile( SearchResultDoneTest.class.getResource(
451                 "response_with_1_referral_and_error_message.xml" ).getFile() );
452
453             parser.parse();
454         }
455         catch ( Exception JavaDoc e )
456         {
457             fail( e.getMessage() );
458         }
459
460         SearchResultDone searchResultDone = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
461             .getSearchResultDone();
462
463         LdapResult ldapResult = searchResultDone.getLdapResult();
464
465         List JavaDoc referrals = ldapResult.getReferrals();
466
467         assertEquals( 1, referrals.size() );
468
469         Object JavaDoc referral = referrals.get( 0 );
470
471         try
472         {
473             assertEquals( new LdapURL( "ldap://www.apache.org/" ).toString(), referral.toString() );
474         }
475         catch ( NamingException JavaDoc e )
476         {
477             fail();
478         }
479     }
480
481
482     /**
483      * Test parsing of a response with MatchedDN attribute
484      */

485     public void testResponseWithMatchedDNAttribute()
486     {
487         Dsmlv2ResponseParser parser = null;
488         try
489         {
490             parser = new Dsmlv2ResponseParser();
491
492             parser.setInputFile( SearchResultDoneTest.class.getResource( "response_with_matchedDN_attribute.xml" )
493                 .getFile() );
494
495             parser.parse();
496         }
497         catch ( Exception JavaDoc e )
498         {
499             fail( e.getMessage() );
500         }
501
502         SearchResultDone searchResultDone = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() )
503             .getSearchResultDone();
504
505         LdapResult ldapResult = searchResultDone.getLdapResult();
506
507         assertEquals( "cn=Bob Rush,ou=Dev,dc=Example,dc=COM", ldapResult.getMatchedDN() );
508     }
509
510
511     /**
512      * Test parsing of a response with wrong matched DN
513      */

514     public void testResponseWithWrongMatchedDN()
515     {
516         testParsingFail( SearchResultDoneTest.class, "response_with_wrong_matchedDN_attribute.xml" );
517     }
518
519
520     /**
521      * Test parsing of a response with wrong Descr attribute
522      */

523     public void testResponseWithWrongDescr()
524     {
525         testParsingFail( SearchResultDoneTest.class, "response_with_wrong_descr.xml" );
526     }
527 }
528
Popular Tags