KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > dsmlv2 > addResponse > AddResponseTest


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.addResponse;
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.shared.ldap.codec.Control;
31 import org.apache.directory.shared.ldap.codec.LdapResult;
32 import org.apache.directory.shared.ldap.codec.add.AddResponse;
33 import org.apache.directory.shared.ldap.message.ResultCodeEnum;
34 import org.apache.directory.shared.ldap.util.StringTools;
35
36 import com.sun.jndi.ldap.LdapURL;
37
38
39 /**
40  * Tests for the Add Response parsing
41  *
42  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
43  * @version $Rev$, $Date$
44  */

45 public class AddResponseTest extends AbstractResponseTest
46 {
47
48     /**
49      * Test parsing of a Response with the (optional) requestID attribute
50      */

51     public void testResponseWithRequestId()
52     {
53         Dsmlv2ResponseParser parser = null;
54         try
55         {
56             parser = new Dsmlv2ResponseParser();
57
58             parser
59                 .setInputFile( AddResponseTest.class.getResource( "response_with_requestID_attribute.xml" ).getFile() );
60
61             parser.parse();
62         }
63         catch ( Exception JavaDoc e )
64         {
65             fail( e.getMessage() );
66         }
67
68         AddResponse addResponse = ( AddResponse ) parser.getBatchResponse().getCurrentResponse();
69
70         assertEquals( 456, addResponse.getMessageId() );
71     }
72     
73     /**
74      * Test parsing of a Response with the (optional) requestID attribute equals 0
75      */

76     public void testResponseWithRequestIdEquals0()
77     {
78         testParsingFail( AddResponseTest.class, "response_with_requestID_equals_0.xml" );
79     }
80
81
82     /**
83      * Test parsing of a response with a (optional) Control element
84      */

85     public void testResponseWith1Control()
86     {
87         Dsmlv2ResponseParser parser = null;
88         try
89         {
90             parser = new Dsmlv2ResponseParser();
91
92             parser.setInputFile( AddResponseTest.class.getResource( "response_with_1_control.xml" ).getFile() );
93
94             parser.parse();
95         }
96         catch ( Exception JavaDoc e )
97         {
98             fail( e.getMessage() );
99         }
100
101         AddResponse addResponse = ( AddResponse ) parser.getBatchResponse().getCurrentResponse();
102
103         assertEquals( 1, addResponse.getControls().size() );
104
105         Control control = addResponse.getCurrentControl();
106
107         assertTrue( control.getCriticality() );
108
109         assertEquals( "1.2.840.113556.1.4.643", control.getControlType() );
110
111         assertEquals( "Some text", StringTools.utf8ToString( ( byte[] ) control.getControlValue() ) );
112     }
113
114     /**
115      * Test parsing of a response with a (optional) Control element with emptyValue
116      */

117     public void testResponseWith1ControlEmptyValue()
118     {
119         Dsmlv2ResponseParser parser = null;
120         try
121         {
122             parser = new Dsmlv2ResponseParser();
123
124             parser.setInputFile( AddResponseTest.class.getResource( "response_with_1_control_empty_value.xml" ).getFile() );
125
126             parser.parse();
127         }
128         catch ( Exception JavaDoc e )
129         {
130             fail( e.getMessage() );
131         }
132
133         AddResponse addResponse = ( AddResponse ) parser.getBatchResponse().getCurrentResponse();
134         Control control = addResponse.getCurrentControl();
135         
136         assertEquals( 1, addResponse.getControls().size() );
137         assertTrue( control.getCriticality() );
138         assertEquals( "1.2.840.113556.1.4.643", control.getControlType() );
139         assertEquals( StringTools.EMPTY_BYTES, ( byte[] ) control.getControlValue() );
140     }
141
142     /**
143      * Test parsing of a response with 2 (optional) Control elements
144      */

145     public void testResponseWith2Controls()
146     {
147         Dsmlv2ResponseParser parser = null;
148         try
149         {
150             parser = new Dsmlv2ResponseParser();
151
152             parser.setInputFile( AddResponseTest.class.getResource( "response_with_2_controls.xml" ).getFile() );
153
154             parser.parse();
155         }
156         catch ( Exception JavaDoc e )
157         {
158             fail( e.getMessage() );
159         }
160
161         AddResponse addResponse = ( AddResponse ) parser.getBatchResponse().getCurrentResponse();
162
163         assertEquals( 2, addResponse.getControls().size() );
164
165         Control control = addResponse.getCurrentControl();
166
167         assertFalse( control.getCriticality() );
168
169         assertEquals( "1.2.840.113556.1.4.789", control.getControlType() );
170
171         assertEquals( "Some other text", StringTools.utf8ToString( ( byte[] ) control.getControlValue() ) );
172     }
173
174
175     /**
176      * Test parsing of a response with 3 (optional) Control elements without value
177      */

178     public void testResponseWith3ControlsWithoutValue()
179     {
180         Dsmlv2ResponseParser parser = null;
181         try
182         {
183             parser = new Dsmlv2ResponseParser();
184
185             parser.setInputFile( AddResponseTest.class.getResource( "response_with_3_controls_without_value.xml" )
186                 .getFile() );
187
188             parser.parse();
189         }
190         catch ( Exception JavaDoc e )
191         {
192             fail( e.getMessage() );
193         }
194
195         AddResponse addResponse = ( AddResponse ) parser.getBatchResponse().getCurrentResponse();
196
197         assertEquals( 3, addResponse.getControls().size() );
198
199         Control control = addResponse.getCurrentControl();
200
201         assertTrue( control.getCriticality() );
202
203         assertEquals( "1.2.840.113556.1.4.456", control.getControlType() );
204
205         assertEquals( StringTools.EMPTY_BYTES, control.getControlValue() );
206     }
207
208
209     /**
210      * Test parsing of a response without Result Code element
211      */

212     public void testResponseWithoutResultCode()
213     {
214         testParsingFail( AddResponseTest.class, "response_without_result_code.xml" );
215     }
216
217
218     /**
219      * Test parsing of a response with Result Code element but a not integer value
220      */

221     public void testResponseWithResultCodeNotInteger()
222     {
223         testParsingFail( AddResponseTest.class, "response_with_result_code_not_integer.xml" );
224     }
225
226
227     /**
228      * Test parsing of a response with Result Code
229      */

230     public void testResponseWithResultCode()
231     {
232         Dsmlv2ResponseParser parser = null;
233         try
234         {
235             parser = new Dsmlv2ResponseParser();
236
237             parser.setInputFile( AddResponseTest.class.getResource( "response_with_result_code.xml" ).getFile() );
238
239             parser.parse();
240         }
241         catch ( Exception JavaDoc e )
242         {
243             fail( e.getMessage() );
244         }
245
246         AddResponse addResponse = ( AddResponse ) parser.getBatchResponse().getCurrentResponse();
247
248         LdapResult ldapResult = addResponse.getLdapResult();
249
250         assertEquals( ResultCodeEnum.PROTOCOL_ERROR, ldapResult.getResultCode() );
251     }
252
253
254     /**
255      * Test parsing of a response with Error Message
256      */

257     public void testResponseWithErrorMessage()
258     {
259         Dsmlv2ResponseParser parser = null;
260         try
261         {
262             parser = new Dsmlv2ResponseParser();
263
264             parser.setInputFile( AddResponseTest.class.getResource( "response_with_error_message.xml" ).getFile() );
265
266             parser.parse();
267         }
268         catch ( Exception JavaDoc e )
269         {
270             fail( e.getMessage() );
271         }
272
273         AddResponse addResponse = ( AddResponse ) parser.getBatchResponse().getCurrentResponse();
274
275         LdapResult ldapResult = addResponse.getLdapResult();
276
277         assertEquals( "Unrecognized extended operation EXTENSION_OID: 1.2.6.1.4.1.18060.1.1.1.100.2", ldapResult
278             .getErrorMessage() );
279     }
280     
281     /**
282      * Test parsing of a response with empty Error Message
283      */

284     public void testResponseWithEmptyErrorMessage()
285     {
286         Dsmlv2ResponseParser parser = null;
287         try
288         {
289             parser = new Dsmlv2ResponseParser();
290
291             parser.setInputFile( AddResponseTest.class.getResource( "response_with_empty_error_message.xml" ).getFile() );
292
293             parser.parse();
294         }
295         catch ( Exception JavaDoc e )
296         {
297             fail( e.getMessage() );
298         }
299
300         AddResponse addResponse = ( AddResponse ) parser.getBatchResponse().getCurrentResponse();
301
302         LdapResult ldapResult = addResponse.getLdapResult();
303
304         assertNull( ldapResult.getErrorMessage() );
305     }
306
307
308     /**
309      * Test parsing of a response with a Referral
310      */

311     public void testResponseWith1Referral()
312     {
313         Dsmlv2ResponseParser parser = null;
314         try
315         {
316             parser = new Dsmlv2ResponseParser();
317
318             parser.setInputFile( AddResponseTest.class.getResource( "response_with_1_referral.xml" ).getFile() );
319
320             parser.parse();
321         }
322         catch ( Exception JavaDoc e )
323         {
324             fail( e.getMessage() );
325         }
326
327         AddResponse addResponse = ( AddResponse ) parser.getBatchResponse().getCurrentResponse();
328
329         LdapResult ldapResult = addResponse.getLdapResult();
330
331         List JavaDoc referrals = ldapResult.getReferrals();
332
333         assertEquals( 1, referrals.size() );
334
335         Object JavaDoc referral = referrals.get( 0 );
336
337         try
338         {
339             assertEquals( new LdapURL( "ldap://www.apache.org/" ).toString(), referral.toString() );
340         }
341         catch ( NamingException JavaDoc e )
342         {
343             fail();
344         }
345     }
346     
347     /**
348      * Test parsing of a response with an empty Referral
349      */

350     public void testResponseWith1EmptyReferral()
351     {
352         Dsmlv2ResponseParser parser = null;
353         try
354         {
355             parser = new Dsmlv2ResponseParser();
356
357             parser.setInputFile( AddResponseTest.class.getResource( "response_with_1_empty_referral.xml" ).getFile() );
358
359             parser.parse();
360         }
361         catch ( Exception JavaDoc e )
362         {
363             fail( e.getMessage() );
364         }
365
366         AddResponse addResponse = ( AddResponse ) parser.getBatchResponse().getCurrentResponse();
367
368         LdapResult ldapResult = addResponse.getLdapResult();
369
370         List JavaDoc referrals = ldapResult.getReferrals();
371
372         assertEquals( 0, referrals.size() );
373     }
374
375
376     /**
377      * Test parsing of a response with 2 Referral elements
378      */

379     public void testResponseWith2Referrals()
380     {
381         Dsmlv2ResponseParser parser = null;
382         try
383         {
384             parser = new Dsmlv2ResponseParser();
385
386             parser.setInputFile( AddResponseTest.class.getResource( "response_with_2_referrals.xml" ).getFile() );
387
388             parser.parse();
389         }
390         catch ( Exception JavaDoc e )
391         {
392             fail( e.getMessage() );
393         }
394
395         AddResponse addResponse = ( AddResponse ) parser.getBatchResponse().getCurrentResponse();
396
397         LdapResult ldapResult = addResponse.getLdapResult();
398
399         List JavaDoc referrals = ldapResult.getReferrals();
400
401         assertEquals( 2, referrals.size() );
402
403         Object JavaDoc referral = referrals.get( 0 );
404
405         try
406         {
407             assertEquals( new LdapURL( "ldap://www.apache.org/" ).toString(), referral.toString() );
408         }
409         catch ( NamingException JavaDoc e )
410         {
411             fail();
412         }
413
414         Object JavaDoc referral2 = referrals.get( 1 );
415
416         try
417         {
418             assertEquals( new LdapURL( "ldap://www.apple.com/" ).toString(), referral2.toString() );
419         }
420         catch ( NamingException JavaDoc e )
421         {
422             fail();
423         }
424     }
425
426
427     /**
428      * Test parsing of a response with a Referral and an Error Message
429      */

430     public void testResponseWith1ReferralAndAnErrorMessage()
431     {
432         Dsmlv2ResponseParser parser = null;
433         try
434         {
435             parser = new Dsmlv2ResponseParser();
436
437             parser.setInputFile( AddResponseTest.class.getResource( "response_with_1_referral_and_error_message.xml" )
438                 .getFile() );
439
440             parser.parse();
441         }
442         catch ( Exception JavaDoc e )
443         {
444             fail( e.getMessage() );
445         }
446
447         AddResponse addResponse = ( AddResponse ) parser.getBatchResponse().getCurrentResponse();
448
449         LdapResult ldapResult = addResponse.getLdapResult();
450
451         List JavaDoc referrals = ldapResult.getReferrals();
452
453         assertEquals( 1, referrals.size() );
454
455         Object JavaDoc referral = referrals.get( 0 );
456
457         try
458         {
459             assertEquals( new LdapURL( "ldap://www.apache.org/" ).toString(), referral.toString() );
460         }
461         catch ( NamingException JavaDoc e )
462         {
463             fail();
464         }
465     }
466
467
468     /**
469      * Test parsing of a response with MatchedDN attribute
470      */

471     public void testResponseWithMatchedDNAttribute()
472     {
473         Dsmlv2ResponseParser parser = null;
474         try
475         {
476             parser = new Dsmlv2ResponseParser();
477
478             parser
479                 .setInputFile( AddResponseTest.class.getResource( "response_with_matchedDN_attribute.xml" ).getFile() );
480
481             parser.parse();
482         }
483         catch ( Exception JavaDoc e )
484         {
485             fail( e.getMessage() );
486         }
487
488         AddResponse addResponse = ( AddResponse ) parser.getBatchResponse().getCurrentResponse();
489
490         LdapResult ldapResult = addResponse.getLdapResult();
491
492         assertEquals( "cn=Bob Rush,ou=Dev,dc=Example,dc=COM", ldapResult.getMatchedDN() );
493     }
494
495
496     /**
497      * Test parsing of a response with wrong matched DN
498      */

499     public void testResponseWithWrongMatchedDN()
500     {
501         testParsingFail( AddResponseTest.class, "response_with_wrong_matchedDN_attribute.xml" );
502     }
503
504
505     /**
506      * Test parsing of a response with wrong Descr attribute
507      */

508     public void testResponseWithWrongDescr()
509     {
510         testParsingFail( AddResponseTest.class, "response_with_wrong_descr.xml" );
511     }
512 }
513
Popular Tags