KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > dsmlv2 > searchResponse > searchResultReference > SearchResultReferenceTest


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.searchResultReference;
22
23
24 import java.util.List JavaDoc;
25
26 import org.apache.directory.ldapstudio.dsmlv2.AbstractResponseTest;
27 import org.apache.directory.ldapstudio.dsmlv2.Dsmlv2ResponseParser;
28 import org.apache.directory.ldapstudio.dsmlv2.reponse.SearchResponse;
29 import org.apache.directory.shared.ldap.codec.Control;
30 import org.apache.directory.shared.ldap.codec.search.SearchResultReference;
31 import org.apache.directory.shared.ldap.codec.util.LdapURL;
32 import org.apache.directory.shared.ldap.codec.util.LdapURLEncodingException;
33 import org.apache.directory.shared.ldap.util.StringTools;
34
35
36 /**
37  * Tests for the Search Result Reference Response parsing
38  *
39  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
40  * @version $Rev$, $Date$
41  */

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

47     public void testResponseWith1Control()
48     {
49         Dsmlv2ResponseParser parser = null;
50         try
51         {
52             parser = new Dsmlv2ResponseParser();
53
54             parser
55                 .setInputFile( SearchResultReferenceTest.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         SearchResultReference searchResultReference = ( ( SearchResponse ) parser.getBatchResponse()
65             .getCurrentResponse() ).getCurrentSearchResultReference();
66
67         assertEquals( 1, searchResultReference.getControls().size() );
68
69         Control control = searchResultReference.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
89                 .setInputFile( SearchResultReferenceTest.class.getResource( "response_with_1_control_empty_value.xml" ).getFile() );
90
91             parser.parse();
92         }
93         catch ( Exception JavaDoc e )
94         {
95             fail( e.getMessage() );
96         }
97
98         SearchResultReference searchResultReference = ( ( SearchResponse ) parser.getBatchResponse()
99             .getCurrentResponse() ).getCurrentSearchResultReference();
100
101         assertEquals( 1, searchResultReference.getControls().size() );
102
103         Control control = searchResultReference.getCurrentControl();
104
105         assertTrue( control.getCriticality() );
106
107         assertEquals( "1.2.840.113556.1.4.643", control.getControlType() );
108
109         assertEquals( StringTools.EMPTY_BYTES, ( byte[] ) control.getControlValue() );
110     }
111
112
113     /**
114      * Test parsing of a response with 2 (optional) Control elements
115      */

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

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

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

212     public void testResponseWithRequestIdEquals0()
213     {
214         testParsingFail( SearchResultReferenceTest.class, "response_with_requestID_equals_0.xml" );
215     }
216
217
218     /**
219      * Test parsing of a response with 0 Ref
220      */

221     public void testResponseWith0Ref()
222     {
223         testParsingFail( SearchResultReferenceTest.class, "response_with_0_ref.xml" );
224     }
225
226
227     /**
228      * Test parsing of a Response with 1 Ref
229      */

230     public void testResponseWith1Ref()
231     {
232         Dsmlv2ResponseParser parser = null;
233         try
234         {
235             parser = new Dsmlv2ResponseParser();
236
237             parser.setInputFile( SearchResultReferenceTest.class.getResource( "response_with_1_ref.xml" ).getFile() );
238
239             parser.parse();
240         }
241         catch ( Exception JavaDoc e )
242         {
243             fail( e.getMessage() );
244         }
245
246         SearchResultReference searchResultReference = ( ( SearchResponse ) parser.getBatchResponse()
247             .getCurrentResponse() ).getCurrentSearchResultReference();
248
249         List JavaDoc references = searchResultReference.getSearchResultReferences();
250
251         assertEquals( 1, references.size() );
252
253         try
254         {
255             assertEquals( new LdapURL( "ldap://localhost" ).toString(), references.get( 0 ).toString() );
256         }
257         catch ( LdapURLEncodingException e )
258         {
259             fail();
260         }
261     }
262     
263     /**
264      * Test parsing of a Response with 1 Ref
265      */

266     public void testResponseWith1EmptyRef()
267     {
268         Dsmlv2ResponseParser parser = null;
269         try
270         {
271             parser = new Dsmlv2ResponseParser();
272
273             parser.setInputFile( SearchResultReferenceTest.class.getResource( "response_with_1_empty_ref.xml" ).getFile() );
274
275             parser.parse();
276         }
277         catch ( Exception JavaDoc e )
278         {
279             fail( e.getMessage() );
280         }
281
282         SearchResultReference searchResultReference = ( ( SearchResponse ) parser.getBatchResponse()
283             .getCurrentResponse() ).getCurrentSearchResultReference();
284
285         List JavaDoc references = searchResultReference.getSearchResultReferences();
286
287         assertEquals( 0, references.size() );
288     }
289
290
291
292     /**
293      * Test parsing of a Response with 2 Ref
294      */

295     public void testResponseWith2Ref()
296     {
297         Dsmlv2ResponseParser parser = null;
298         try
299         {
300             parser = new Dsmlv2ResponseParser();
301
302             parser.setInputFile( SearchResultReferenceTest.class.getResource( "response_with_2_ref.xml" ).getFile() );
303
304             parser.parse();
305         }
306         catch ( Exception JavaDoc e )
307         {
308             fail( e.getMessage() );
309         }
310
311         SearchResultReference searchResultReference = ( ( SearchResponse ) parser.getBatchResponse()
312             .getCurrentResponse() ).getCurrentSearchResultReference();
313
314         List JavaDoc references = searchResultReference.getSearchResultReferences();
315
316         assertEquals( 2, references.size() );
317
318         try
319         {
320             assertEquals( new LdapURL( "ldap://localhost" ).toString(), references.get( 0 ).toString() );
321         }
322         catch ( LdapURLEncodingException e )
323         {
324             fail();
325         }
326
327         try
328         {
329             assertEquals( new LdapURL( "ldap://www.apache.org" ).toString(), references.get( 1 ).toString() );
330         }
331         catch ( LdapURLEncodingException e )
332         {
333             fail();
334         }
335     }
336
337
338     /**
339      * Test parsing of a response with 1 wrong Ref
340      */

341     public void testResponseWith1WrongRef()
342     {
343         testParsingFail( SearchResultReferenceTest.class, "response_with_1_wrong_ref.xml" );
344     }
345 }
346
Popular Tags