KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > dsmlv2 > modifyResponse > ModifyResponseTest


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.modifyResponse;
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.modify.ModifyResponse;
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 Modify Response parsing
41  *
42  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
43  * @version $Rev$, $Date$
44  */

45 public class ModifyResponseTest 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.setInputFile( ModifyResponseTest.class.getResource( "response_with_requestID_attribute.xml" )
59                 .getFile() );
60
61             parser.parse();
62         }
63         catch ( Exception JavaDoc e )
64         {
65             fail( e.getMessage() );
66         }
67
68         ModifyResponse modifyResponse = ( ModifyResponse ) parser.getBatchResponse().getCurrentResponse();
69
70         assertEquals( 456, modifyResponse.getMessageId() );
71     }
72     
73     
74     /**
75      * Test parsing of a Response with the (optional) requestID attribute equals 0
76      */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

510     public void testResponseWithWrongDescr()
511     {
512         testParsingFail( ModifyResponseTest.class, "response_with_wrong_descr.xml" );
513     }
514 }
515
Popular Tags