1 20 21 package org.apache.directory.ldapstudio.dsmlv2.searchResponse.searchResultEntry; 22 23 24 import javax.naming.NamingEnumeration ; 25 import javax.naming.NamingException ; 26 import javax.naming.directory.Attribute ; 27 import javax.naming.directory.Attributes ; 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 43 public class SearchResultEntryTest extends AbstractResponseTest 44 { 45 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 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 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 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 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 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 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 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 180 public void testResponseWithoutDnAttribute() 181 { 182 testParsingFail( SearchResultEntryTest.class, "response_without_dn_attribute.xml" ); 183 } 184 185 186 189 public void testResponseWithWrongDnAttribute() 190 { 191 testParsingFail( SearchResultEntryTest.class, "response_with_wrong_dn_attribute.xml" ); 192 } 193 194 195 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 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 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 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 251 public void testResponseWithRequestIdEquals0() 252 { 253 testParsingFail( SearchResultEntryTest.class, "response_with_requestID_equals_0.xml" ); 254 } 255 256 257 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 e ) 272 { 273 fail( e.getMessage() ); 274 } 275 276 assertTrue( true ); 277 } 278 279 280 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 e ) 296 { 297 fail( e.getMessage() ); 298 } 299 300 SearchResultEntry searchResultEntry = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() ) 301 .getCurrentSearchResultEntry(); 302 303 Attributes attributes = searchResultEntry.getPartialAttributeList(); 304 305 assertEquals( 1, attributes.size() ); 306 307 NamingEnumeration ne = attributes.getAll(); 308 309 Attribute attribute = ( Attribute ) ne.nextElement(); 310 311 assertEquals( "dc", attribute.getID() ); 312 } 313 314 315 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 e ) 331 { 332 fail( e.getMessage() ); 333 } 334 335 SearchResultEntry searchResultEntry = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() ) 336 .getCurrentSearchResultEntry(); 337 338 Attributes attributes = searchResultEntry.getPartialAttributeList(); 339 340 assertEquals( 1, attributes.size() ); 341 342 NamingEnumeration ne = attributes.getAll(); 343 344 Attribute attribute = ( Attribute ) ne.nextElement(); 345 346 assertEquals( "dc", attribute.getID() ); 347 348 assertEquals( 1, attribute.size() ); 349 350 NamingEnumeration ne2 = null; 351 try 352 { 353 ne2 = attribute.getAll(); 354 } 355 catch ( NamingException e ) 356 { 357 fail(); 358 } 359 360 String value = ( String ) ne2.nextElement(); 361 362 assertEquals( "example", value ); 363 } 364 365 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 e ) 381 { 382 fail( e.getMessage() ); 383 } 384 385 SearchResultEntry searchResultEntry = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() ) 386 .getCurrentSearchResultEntry(); 387 388 Attributes attributes = searchResultEntry.getPartialAttributeList(); 389 390 assertEquals( 1, attributes.size() ); 391 392 NamingEnumeration ne = attributes.getAll(); 393 394 Attribute attribute = ( Attribute ) ne.nextElement(); 395 396 assertEquals( "cn", attribute.getID() ); 397 398 assertEquals( 1, attribute.size() ); 399 400 NamingEnumeration ne2 = null; 401 try 402 { 403 ne2 = attribute.getAll(); 404 } 405 catch ( NamingException e ) 406 { 407 fail(); 408 } 409 410 Object value = ne2.nextElement(); 411 assertEquals( "Emmanuel Lécharny", new String ( (byte[]) value ) ); 412 } 413 414 415 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 e ) 431 { 432 fail( e.getMessage() ); 433 } 434 435 SearchResultEntry searchResultEntry = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() ) 436 .getCurrentSearchResultEntry(); 437 438 Attributes attributes = searchResultEntry.getPartialAttributeList(); 439 440 assertEquals( 1, attributes.size() ); 441 442 NamingEnumeration ne = attributes.getAll(); 443 444 Attribute attribute = ( Attribute ) ne.nextElement(); 445 446 assertEquals( "dc", attribute.getID() ); 447 448 assertEquals( 1, attribute.size() ); 449 450 NamingEnumeration ne2 = null; 451 try 452 { 453 ne2 = attribute.getAll(); 454 } 455 catch ( NamingException e ) 456 { 457 fail(); 458 } 459 460 String value = ( String ) ne2.nextElement(); 461 462 assertEquals( "", value ); 463 } 464 465 466 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 e ) 482 { 483 fail( e.getMessage() ); 484 } 485 486 SearchResultEntry searchResultEntry = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() ) 487 .getCurrentSearchResultEntry(); 488 489 Attributes attributes = searchResultEntry.getPartialAttributeList(); 490 491 assertEquals( 1, attributes.size() ); 492 493 NamingEnumeration ne = attributes.getAll(); 494 495 Attribute attribute = ( Attribute ) ne.nextElement(); 496 497 assertEquals( "objectclass", attribute.getID() ); 498 499 assertEquals( 2, attribute.size() ); 500 501 NamingEnumeration ne2 = null; 502 try 503 { 504 ne2 = attribute.getAll(); 505 } 506 catch ( NamingException e ) 507 { 508 fail(); 509 } 510 511 String value = ( String ) ne2.nextElement(); 512 513 assertEquals( "top", value ); 514 515 value = ( String ) ne2.nextElement(); 516 517 assertEquals( "domain", value ); 518 } 519 520 521 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 e ) 537 { 538 fail( e.getMessage() ); 539 } 540 541 SearchResultEntry searchResultEntry = ( ( SearchResponse ) parser.getBatchResponse().getCurrentResponse() ) 542 .getCurrentSearchResultEntry(); 543 544 Attributes attributes = searchResultEntry.getPartialAttributeList(); 545 546 assertEquals( 2, attributes.size() ); 547 548 NamingEnumeration ne = attributes.getAll(); 549 550 Attribute attribute = ( Attribute ) ne.nextElement(); 551 552 assertEquals( "objectclass", attribute.getID() ); 553 554 assertEquals( 1, attribute.size() ); 555 556 NamingEnumeration ne2 = null; 557 try 558 { 559 ne2 = attribute.getAll(); 560 } 561 catch ( NamingException e ) 562 { 563 fail(); 564 } 565 566 String value = ( String ) ne2.nextElement(); 567 568 assertEquals( "top", value ); 569 570 attribute = ( Attribute ) 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 e ) 582 { 583 fail(); 584 } 585 586 value = ( String ) ne2.nextElement(); 587 588 assertEquals( "example", value ); 589 } 590 591 592 595 public void testResponseWith1AttrWithoutNameAttribute() 596 { 597 testParsingFail( SearchResultEntryTest.class, "response_with_1_attr_without_name_attribute.xml" ); 598 } 599 } 600 | Popular Tags |