Search in sources :

Example 11 with ByteStringBuffer

use of com.unboundid.util.ByteStringBuffer in project ldapsdk by pingidentity.

the class LDIFDeleteChangeRecordTestCase method testConstructor1.

/**
 * Tests the first constructor.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testConstructor1() throws Exception {
    LDIFDeleteChangeRecord r = new LDIFDeleteChangeRecord("dc=example,dc=com");
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("dc=example,dc=com"));
    DeleteRequest deleteRequest = r.toDeleteRequest();
    assertEquals(deleteRequest.getDN(), "dc=example,dc=com");
    assertEquals(r.getChangeType(), ChangeType.DELETE);
    String[] ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 2);
    r.hashCode();
    ByteStringBuffer byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    StringBuilder stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    assertNotNull(r.getControls());
    assertTrue(r.getControls().isEmpty());
    r = r.duplicate();
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("dc=example,dc=com"));
    deleteRequest = r.toDeleteRequest();
    assertEquals(deleteRequest.getDN(), "dc=example,dc=com");
    assertEquals(r.getChangeType(), ChangeType.DELETE);
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 2);
    r.hashCode();
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    assertNotNull(r.getControls());
    assertTrue(r.getControls().isEmpty());
    r = r.duplicate((Control[]) null);
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("dc=example,dc=com"));
    deleteRequest = r.toDeleteRequest();
    assertEquals(deleteRequest.getDN(), "dc=example,dc=com");
    assertEquals(r.getChangeType(), ChangeType.DELETE);
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 2);
    r.hashCode();
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    assertNotNull(r.getControls());
    assertTrue(r.getControls().isEmpty());
    r = r.duplicate(new Control[0]);
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("dc=example,dc=com"));
    deleteRequest = r.toDeleteRequest();
    assertEquals(deleteRequest.getDN(), "dc=example,dc=com");
    assertEquals(r.getChangeType(), ChangeType.DELETE);
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 2);
    r.hashCode();
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    assertNotNull(r.getControls());
    assertTrue(r.getControls().isEmpty());
    r = r.duplicate(new ManageDsaITRequestControl(false));
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("dc=example,dc=com"));
    deleteRequest = r.toDeleteRequest();
    assertEquals(deleteRequest.getDN(), "dc=example,dc=com");
    assertEquals(r.getChangeType(), ChangeType.DELETE);
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 3);
    r.hashCode();
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    assertNotNull(r.getControls());
    assertFalse(r.getControls().isEmpty());
    assertEquals(r.getControls(), Collections.singletonList(new ManageDsaITRequestControl(false)));
    r = r.duplicate(new ManageDsaITRequestControl(false), new SubtreeDeleteRequestControl(false));
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("dc=example,dc=com"));
    deleteRequest = r.toDeleteRequest();
    assertEquals(deleteRequest.getDN(), "dc=example,dc=com");
    assertEquals(r.getChangeType(), ChangeType.DELETE);
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 4);
    r.hashCode();
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    assertNotNull(r.getControls());
    assertFalse(r.getControls().isEmpty());
    assertEquals(r.getControls(), Arrays.asList(new ManageDsaITRequestControl(false), new SubtreeDeleteRequestControl(false)));
}
Also used : ManageDsaITRequestControl(com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl) SubtreeDeleteRequestControl(com.unboundid.ldap.sdk.controls.SubtreeDeleteRequestControl) Control(com.unboundid.ldap.sdk.Control) DN(com.unboundid.ldap.sdk.DN) DeleteRequest(com.unboundid.ldap.sdk.DeleteRequest) ByteStringBuffer(com.unboundid.util.ByteStringBuffer) ManageDsaITRequestControl(com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl) SubtreeDeleteRequestControl(com.unboundid.ldap.sdk.controls.SubtreeDeleteRequestControl) Test(org.testng.annotations.Test)

Example 12 with ByteStringBuffer

use of com.unboundid.util.ByteStringBuffer in project ldapsdk by pingidentity.

the class LDIFModifyChangeRecordTestCase method testConstructor1SingleModification.

/**
 * Tests the first constructor with a single modification.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testConstructor1SingleModification() throws Exception {
    Modification[] mods = { new Modification(ModificationType.ADD, "description", "foo") };
    LDIFModifyChangeRecord r = new LDIFModifyChangeRecord("dc=example,dc=com", mods);
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("dc=example,dc=com"));
    assertNotNull(r.getModifications());
    assertEquals(r.getModifications().length, 1);
    ModifyRequest modifyRequest = r.toModifyRequest();
    assertEquals(modifyRequest.getDN(), "dc=example,dc=com");
    assertEquals(r.getChangeType(), ChangeType.MODIFY);
    assertTrue(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    String[] ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 5);
    ByteStringBuffer byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    StringBuilder stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    LDIFModifyChangeRecord.setAlwaysIncludeTrailingDash(false);
    assertFalse(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 4);
    LDIFModifyChangeRecord.setAlwaysIncludeTrailingDash(true);
    assertTrue(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    r.hashCode();
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    assertNotNull(r.getControls());
    assertTrue(r.getControls().isEmpty());
    r = r.duplicate();
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("dc=example,dc=com"));
    assertNotNull(r.getModifications());
    assertEquals(r.getModifications().length, 1);
    modifyRequest = r.toModifyRequest();
    assertEquals(modifyRequest.getDN(), "dc=example,dc=com");
    assertEquals(r.getChangeType(), ChangeType.MODIFY);
    assertTrue(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 5);
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    LDIFModifyChangeRecord.setAlwaysIncludeTrailingDash(false);
    assertFalse(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 4);
    LDIFModifyChangeRecord.setAlwaysIncludeTrailingDash(true);
    assertTrue(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    r.hashCode();
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    assertNotNull(r.getControls());
    assertTrue(r.getControls().isEmpty());
    r = r.duplicate((Control[]) null);
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("dc=example,dc=com"));
    assertNotNull(r.getModifications());
    assertEquals(r.getModifications().length, 1);
    modifyRequest = r.toModifyRequest();
    assertEquals(modifyRequest.getDN(), "dc=example,dc=com");
    assertEquals(r.getChangeType(), ChangeType.MODIFY);
    assertTrue(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 5);
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    LDIFModifyChangeRecord.setAlwaysIncludeTrailingDash(false);
    assertFalse(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 4);
    LDIFModifyChangeRecord.setAlwaysIncludeTrailingDash(true);
    assertTrue(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    r.hashCode();
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    assertNotNull(r.getControls());
    assertTrue(r.getControls().isEmpty());
    r = r.duplicate(new Control[0]);
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("dc=example,dc=com"));
    assertNotNull(r.getModifications());
    assertEquals(r.getModifications().length, 1);
    modifyRequest = r.toModifyRequest();
    assertEquals(modifyRequest.getDN(), "dc=example,dc=com");
    assertEquals(r.getChangeType(), ChangeType.MODIFY);
    assertTrue(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 5);
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    LDIFModifyChangeRecord.setAlwaysIncludeTrailingDash(false);
    assertFalse(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 4);
    LDIFModifyChangeRecord.setAlwaysIncludeTrailingDash(true);
    assertTrue(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    r.hashCode();
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    assertNotNull(r.getControls());
    assertTrue(r.getControls().isEmpty());
    r = r.duplicate(new ManageDsaITRequestControl(false));
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("dc=example,dc=com"));
    assertNotNull(r.getModifications());
    assertEquals(r.getModifications().length, 1);
    modifyRequest = r.toModifyRequest();
    assertEquals(modifyRequest.getDN(), "dc=example,dc=com");
    assertEquals(r.getChangeType(), ChangeType.MODIFY);
    assertTrue(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 6);
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    LDIFModifyChangeRecord.setAlwaysIncludeTrailingDash(false);
    assertFalse(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 5);
    LDIFModifyChangeRecord.setAlwaysIncludeTrailingDash(true);
    assertTrue(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    r.hashCode();
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    assertNotNull(r.getControls());
    assertFalse(r.getControls().isEmpty());
    assertEquals(r.getControls(), Collections.singletonList(new ManageDsaITRequestControl(false)));
    r = r.duplicate(new ManageDsaITRequestControl(false), new PermissiveModifyRequestControl(false));
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("dc=example,dc=com"));
    assertNotNull(r.getModifications());
    assertEquals(r.getModifications().length, 1);
    modifyRequest = r.toModifyRequest();
    assertEquals(modifyRequest.getDN(), "dc=example,dc=com");
    assertEquals(r.getChangeType(), ChangeType.MODIFY);
    assertTrue(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 7);
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    LDIFModifyChangeRecord.setAlwaysIncludeTrailingDash(false);
    assertFalse(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 6);
    LDIFModifyChangeRecord.setAlwaysIncludeTrailingDash(true);
    assertTrue(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    r.hashCode();
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    assertNotNull(r.getControls());
    assertFalse(r.getControls().isEmpty());
    assertEquals(r.getControls(), Arrays.asList(new ManageDsaITRequestControl(false), new PermissiveModifyRequestControl(false)));
}
Also used : PermissiveModifyRequestControl(com.unboundid.ldap.sdk.controls.PermissiveModifyRequestControl) Modification(com.unboundid.ldap.sdk.Modification) ManageDsaITRequestControl(com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl) Control(com.unboundid.ldap.sdk.Control) PermissiveModifyRequestControl(com.unboundid.ldap.sdk.controls.PermissiveModifyRequestControl) DN(com.unboundid.ldap.sdk.DN) ModifyRequest(com.unboundid.ldap.sdk.ModifyRequest) ByteStringBuffer(com.unboundid.util.ByteStringBuffer) ManageDsaITRequestControl(com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl) Test(org.testng.annotations.Test)

Example 13 with ByteStringBuffer

use of com.unboundid.util.ByteStringBuffer in project ldapsdk by pingidentity.

the class LDIFModifyChangeRecordTestCase method testConstructor1MultipleModifications.

/**
 * Tests the first constructor with multiple modifications.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testConstructor1MultipleModifications() throws Exception {
    Modification[] mods = { new Modification(ModificationType.REPLACE, "description", "foo", "bar"), new Modification(ModificationType.ADD, "objectClass", "extensibleObject"), new Modification(ModificationType.DELETE, "cn"), new Modification(ModificationType.INCREMENT, "intValue", "5") };
    LDIFModifyChangeRecord r = new LDIFModifyChangeRecord("dc=example,dc=com", mods);
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("dc=example,dc=com"));
    assertNotNull(r.getModifications());
    assertEquals(r.getModifications().length, 4);
    ModifyRequest modifyRequest = r.toModifyRequest();
    assertEquals(modifyRequest.getDN(), "dc=example,dc=com");
    assertEquals(r.getChangeType(), ChangeType.MODIFY);
    assertTrue(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    String[] ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 14);
    ByteStringBuffer byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    StringBuilder stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    LDIFModifyChangeRecord.setAlwaysIncludeTrailingDash(false);
    assertFalse(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 13);
    LDIFModifyChangeRecord.setAlwaysIncludeTrailingDash(true);
    assertTrue(LDIFModifyChangeRecord.alwaysIncludeTrailingDash());
    r.hashCode();
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
}
Also used : Modification(com.unboundid.ldap.sdk.Modification) DN(com.unboundid.ldap.sdk.DN) ModifyRequest(com.unboundid.ldap.sdk.ModifyRequest) ByteStringBuffer(com.unboundid.util.ByteStringBuffer) Test(org.testng.annotations.Test)

Example 14 with ByteStringBuffer

use of com.unboundid.util.ByteStringBuffer in project ldapsdk by pingidentity.

the class LDIFModifyDNChangeRecordTestCase method testConstructor1WithNewSuperior.

/**
 * Tests the first constructor with a non-{@code null} newSuperior DN.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testConstructor1WithNewSuperior() throws Exception {
    LDIFModifyDNChangeRecord r = new LDIFModifyDNChangeRecord("ou=People,dc=example,dc=com", "ou=Users", true, "o=example.com");
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "ou=People,dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("ou=People,dc=example,dc=com"));
    assertNotNull(r.getNewRDN());
    assertEquals(r.getNewRDN(), "ou=Users");
    assertEquals(r.getParsedNewRDN(), new RDN("ou=Users"));
    assertTrue(r.deleteOldRDN());
    assertNotNull(r.getNewSuperiorDN());
    assertEquals(r.getNewSuperiorDN(), "o=example.com");
    assertEquals(r.getParsedNewSuperiorDN(), new DN("o=example.com"));
    assertEquals(r.getNewDN(), new DN("ou=Users,o=example.com"));
    ModifyDNRequest modifyDNRequest = r.toModifyDNRequest();
    assertEquals(modifyDNRequest.getDN(), "ou=People,dc=example,dc=com");
    String[] ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 5);
    assertEquals(r.getChangeType(), ChangeType.MODIFY_DN);
    r.hashCode();
    ByteStringBuffer byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    StringBuilder stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    assertNotNull(r.getControls());
    assertTrue(r.getControls().isEmpty());
    r = r.duplicate();
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "ou=People,dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("ou=People,dc=example,dc=com"));
    assertNotNull(r.getNewRDN());
    assertEquals(r.getNewRDN(), "ou=Users");
    assertEquals(r.getParsedNewRDN(), new RDN("ou=Users"));
    assertTrue(r.deleteOldRDN());
    assertNotNull(r.getNewSuperiorDN());
    assertEquals(r.getNewSuperiorDN(), "o=example.com");
    assertEquals(r.getParsedNewSuperiorDN(), new DN("o=example.com"));
    assertEquals(r.getNewDN(), new DN("ou=Users,o=example.com"));
    modifyDNRequest = r.toModifyDNRequest();
    assertEquals(modifyDNRequest.getDN(), "ou=People,dc=example,dc=com");
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 5);
    assertEquals(r.getChangeType(), ChangeType.MODIFY_DN);
    r.hashCode();
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    assertNotNull(r.getControls());
    assertTrue(r.getControls().isEmpty());
    r = r.duplicate((Control[]) null);
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "ou=People,dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("ou=People,dc=example,dc=com"));
    assertNotNull(r.getNewRDN());
    assertEquals(r.getNewRDN(), "ou=Users");
    assertEquals(r.getParsedNewRDN(), new RDN("ou=Users"));
    assertTrue(r.deleteOldRDN());
    assertNotNull(r.getNewSuperiorDN());
    assertEquals(r.getNewSuperiorDN(), "o=example.com");
    assertEquals(r.getParsedNewSuperiorDN(), new DN("o=example.com"));
    assertEquals(r.getNewDN(), new DN("ou=Users,o=example.com"));
    modifyDNRequest = r.toModifyDNRequest();
    assertEquals(modifyDNRequest.getDN(), "ou=People,dc=example,dc=com");
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 5);
    assertEquals(r.getChangeType(), ChangeType.MODIFY_DN);
    r.hashCode();
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    assertNotNull(r.getControls());
    assertTrue(r.getControls().isEmpty());
    r = r.duplicate(new Control[0]);
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "ou=People,dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("ou=People,dc=example,dc=com"));
    assertNotNull(r.getNewRDN());
    assertEquals(r.getNewRDN(), "ou=Users");
    assertEquals(r.getParsedNewRDN(), new RDN("ou=Users"));
    assertTrue(r.deleteOldRDN());
    assertNotNull(r.getNewSuperiorDN());
    assertEquals(r.getNewSuperiorDN(), "o=example.com");
    assertEquals(r.getParsedNewSuperiorDN(), new DN("o=example.com"));
    assertEquals(r.getNewDN(), new DN("ou=Users,o=example.com"));
    modifyDNRequest = r.toModifyDNRequest();
    assertEquals(modifyDNRequest.getDN(), "ou=People,dc=example,dc=com");
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 5);
    assertEquals(r.getChangeType(), ChangeType.MODIFY_DN);
    r.hashCode();
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    assertNotNull(r.getControls());
    assertTrue(r.getControls().isEmpty());
    r = r.duplicate(new ManageDsaITRequestControl(false));
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "ou=People,dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("ou=People,dc=example,dc=com"));
    assertNotNull(r.getNewRDN());
    assertEquals(r.getNewRDN(), "ou=Users");
    assertEquals(r.getParsedNewRDN(), new RDN("ou=Users"));
    assertTrue(r.deleteOldRDN());
    assertNotNull(r.getNewSuperiorDN());
    assertEquals(r.getNewSuperiorDN(), "o=example.com");
    assertEquals(r.getParsedNewSuperiorDN(), new DN("o=example.com"));
    assertEquals(r.getNewDN(), new DN("ou=Users,o=example.com"));
    modifyDNRequest = r.toModifyDNRequest();
    assertEquals(modifyDNRequest.getDN(), "ou=People,dc=example,dc=com");
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 6);
    assertEquals(r.getChangeType(), ChangeType.MODIFY_DN);
    r.hashCode();
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    assertNotNull(r.getControls());
    assertFalse(r.getControls().isEmpty());
    assertEquals(r.getControls(), Collections.singletonList(new ManageDsaITRequestControl(false)));
    r = r.duplicate(new ManageDsaITRequestControl(false), new ProxiedAuthorizationV2RequestControl("u:test.user"));
    assertNotNull(r.getDN());
    assertEquals(r.getDN(), "ou=People,dc=example,dc=com");
    assertEquals(r.getParsedDN(), new DN("ou=People,dc=example,dc=com"));
    assertNotNull(r.getNewRDN());
    assertEquals(r.getNewRDN(), "ou=Users");
    assertEquals(r.getParsedNewRDN(), new RDN("ou=Users"));
    assertTrue(r.deleteOldRDN());
    assertNotNull(r.getNewSuperiorDN());
    assertEquals(r.getNewSuperiorDN(), "o=example.com");
    assertEquals(r.getParsedNewSuperiorDN(), new DN("o=example.com"));
    assertEquals(r.getNewDN(), new DN("ou=Users,o=example.com"));
    modifyDNRequest = r.toModifyDNRequest();
    assertEquals(modifyDNRequest.getDN(), "ou=People,dc=example,dc=com");
    ldifLines = r.toLDIF();
    assertNotNull(ldifLines);
    assertEquals(ldifLines.length, 7);
    assertEquals(r.getChangeType(), ChangeType.MODIFY_DN);
    r.hashCode();
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer);
    assertNotNull(byteBuffer.toString());
    byteBuffer = new ByteStringBuffer();
    r.toLDIF(byteBuffer, 10);
    assertNotNull(byteBuffer.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer);
    assertNotNull(r.toString());
    stringBuffer = new StringBuilder();
    r.toLDIFString(stringBuffer, 10);
    assertNotNull(r.toString());
    assertNotNull(r.toLDIFString());
    assertNotNull(r.toLDIFString(10));
    assertNotNull(r.toString());
    assertNotNull(r.getControls());
    assertFalse(r.getControls().isEmpty());
    assertEquals(r.getControls(), Arrays.asList(new ManageDsaITRequestControl(false), new ProxiedAuthorizationV2RequestControl("u:test.user")));
}
Also used : ModifyDNRequest(com.unboundid.ldap.sdk.ModifyDNRequest) ManageDsaITRequestControl(com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl) ProxiedAuthorizationV2RequestControl(com.unboundid.ldap.sdk.controls.ProxiedAuthorizationV2RequestControl) Control(com.unboundid.ldap.sdk.Control) ProxiedAuthorizationV2RequestControl(com.unboundid.ldap.sdk.controls.ProxiedAuthorizationV2RequestControl) RDN(com.unboundid.ldap.sdk.RDN) DN(com.unboundid.ldap.sdk.DN) RDN(com.unboundid.ldap.sdk.RDN) ByteStringBuffer(com.unboundid.util.ByteStringBuffer) ManageDsaITRequestControl(com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl) Test(org.testng.annotations.Test)

Example 15 with ByteStringBuffer

use of com.unboundid.util.ByteStringBuffer in project ldapsdk by pingidentity.

the class SplitLDIF method doToolProcessing.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
public ResultCode doToolProcessing() {
    // Get the schema to use during processing.
    final Schema schema;
    try {
        schema = getSchema();
    } catch (final LDAPException le) {
        wrapErr(0, MAX_OUTPUT_LINE_LENGTH, le.getMessage());
        return le.getResultCode();
    }
    // If an encryption passphrase file is provided, then get the passphrase
    // from it.
    String encryptionPassphrase = null;
    if (encryptionPassphraseFile.isPresent()) {
        try {
            encryptionPassphrase = ToolUtils.readEncryptionPassphraseFromFile(encryptionPassphraseFile.getValue());
        } catch (final LDAPException e) {
            Debug.debugException(e);
            wrapErr(0, MAX_OUTPUT_LINE_LENGTH, e.getMessage());
            return e.getResultCode();
        }
    }
    // Figure out which subcommand was selected, and create the appropriate
    // translator to use to perform the processing.
    final SplitLDIFTranslator translator;
    if (splitUsingHashOnRDN.isPresent()) {
        translator = new SplitLDIFRDNHashTranslator(splitBaseDN.getValue(), splitUsingHashOnRDNNumSets.getValue(), addEntriesOutsideSplitBaseDNToAllSets.isPresent(), addEntriesOutsideSplitBaseDNToDedicatedSet.isPresent());
    } else if (splitUsingHashOnAttribute.isPresent()) {
        translator = new SplitLDIFAttributeHashTranslator(splitBaseDN.getValue(), splitUsingHashOnAttributeNumSets.getValue(), splitUsingHashOnAttributeAttributeName.getValue(), splitUsingHashOnAttributeUseAllValues.isPresent(), splitUsingHashOnAttributeAssumeFlatDIT.isPresent(), addEntriesOutsideSplitBaseDNToAllSets.isPresent(), addEntriesOutsideSplitBaseDNToDedicatedSet.isPresent());
    } else if (splitUsingFewestEntries.isPresent()) {
        translator = new SplitLDIFFewestEntriesTranslator(splitBaseDN.getValue(), splitUsingFewestEntriesNumSets.getValue(), splitUsingFewestEntriesAssumeFlatDIT.isPresent(), addEntriesOutsideSplitBaseDNToAllSets.isPresent(), addEntriesOutsideSplitBaseDNToDedicatedSet.isPresent());
    } else if (splitUsingFilter.isPresent()) {
        final List<Filter> filterList = splitUsingFilterFilter.getValues();
        final LinkedHashSet<Filter> filterSet = new LinkedHashSet<>(StaticUtils.computeMapCapacity(filterList.size()));
        for (final Filter f : filterList) {
            filterSet.add(f);
        }
        translator = new SplitLDIFFilterTranslator(splitBaseDN.getValue(), schema, filterSet, splitUsingFilterAssumeFlatDIT.isPresent(), addEntriesOutsideSplitBaseDNToAllSets.isPresent(), addEntriesOutsideSplitBaseDNToDedicatedSet.isPresent());
    } else {
        // This should never happen.
        wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ERR_SPLIT_LDIF_CANNOT_DETERMINE_SPLIT_ALGORITHM.get(splitUsingHashOnRDN.getPrimaryName() + ", " + splitUsingHashOnAttribute.getPrimaryName() + ", " + splitUsingFewestEntries.getPrimaryName() + ", " + splitUsingFilter.getPrimaryName()));
        return ResultCode.PARAM_ERROR;
    }
    // Create the LDIF reader.
    final LDIFReader ldifReader;
    try {
        final InputStream inputStream;
        if (sourceLDIF.isPresent()) {
            final ObjectPair<InputStream, String> p = ToolUtils.getInputStreamForLDIFFiles(sourceLDIF.getValues(), encryptionPassphrase, getOut(), getErr());
            inputStream = p.getFirst();
            if ((encryptionPassphrase == null) && (p.getSecond() != null)) {
                encryptionPassphrase = p.getSecond();
            }
        } else {
            inputStream = System.in;
        }
        ldifReader = new LDIFReader(inputStream, numThreads.getValue(), translator);
        if (schema != null) {
            ldifReader.setSchema(schema);
        }
    } catch (final Exception e) {
        Debug.debugException(e);
        wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ERR_SPLIT_LDIF_ERROR_CREATING_LDIF_READER.get(StaticUtils.getExceptionMessage(e)));
        return ResultCode.LOCAL_ERROR;
    }
    // Iterate through and process all of the entries.
    ResultCode resultCode = ResultCode.SUCCESS;
    final LinkedHashMap<String, OutputStream> outputStreams = new LinkedHashMap<>(StaticUtils.computeMapCapacity(10));
    try {
        final AtomicLong entriesRead = new AtomicLong(0L);
        final AtomicLong entriesExcluded = new AtomicLong(0L);
        final TreeMap<String, AtomicLong> fileCounts = new TreeMap<>();
        readLoop: while (true) {
            final SplitLDIFEntry entry;
            try {
                entry = (SplitLDIFEntry) ldifReader.readEntry();
            } catch (final LDIFException le) {
                Debug.debugException(le);
                resultCode = ResultCode.LOCAL_ERROR;
                final File f = getOutputFile(SplitLDIFEntry.SET_NAME_ERRORS);
                OutputStream s = outputStreams.get(SplitLDIFEntry.SET_NAME_ERRORS);
                if (s == null) {
                    try {
                        s = new FileOutputStream(f);
                        if (encryptTarget.isPresent()) {
                            if (encryptionPassphrase == null) {
                                try {
                                    encryptionPassphrase = ToolUtils.promptForEncryptionPassphrase(false, true, getOut(), getErr());
                                } catch (final LDAPException ex) {
                                    Debug.debugException(ex);
                                    wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ex.getMessage());
                                    return ex.getResultCode();
                                }
                            }
                            s = new PassphraseEncryptedOutputStream(encryptionPassphrase, s);
                        }
                        if (compressTarget.isPresent()) {
                            s = new GZIPOutputStream(s);
                        }
                        outputStreams.put(SplitLDIFEntry.SET_NAME_ERRORS, s);
                        fileCounts.put(SplitLDIFEntry.SET_NAME_ERRORS, new AtomicLong(0L));
                    } catch (final Exception e) {
                        Debug.debugException(e);
                        resultCode = ResultCode.LOCAL_ERROR;
                        wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ERR_SPLIT_LDIF_CANNOT_OPEN_OUTPUT_FILE.get(f.getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
                        break readLoop;
                    }
                }
                final ByteStringBuffer buffer = new ByteStringBuffer();
                buffer.append("# ");
                buffer.append(le.getMessage());
                buffer.append(StaticUtils.EOL_BYTES);
                final List<String> dataLines = le.getDataLines();
                if (dataLines != null) {
                    for (final String dataLine : dataLines) {
                        buffer.append(dataLine);
                        buffer.append(StaticUtils.EOL_BYTES);
                    }
                }
                buffer.append(StaticUtils.EOL_BYTES);
                try {
                    s.write(buffer.toByteArray());
                } catch (final Exception e) {
                    Debug.debugException(e);
                    resultCode = ResultCode.LOCAL_ERROR;
                    wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ERR_SPLIT_LDIF_ERROR_WRITING_ERROR_TO_FILE.get(le.getMessage(), f.getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
                    break readLoop;
                }
                if (le.mayContinueReading()) {
                    wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ERR_SPLIT_LDIF_INVALID_LDIF_RECORD_RECOVERABLE.get(StaticUtils.getExceptionMessage(le)));
                    continue;
                } else {
                    wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ERR_SPLIT_LDIF_INVALID_LDIF_RECORD_UNRECOVERABLE.get(StaticUtils.getExceptionMessage(le)));
                    break;
                }
            } catch (final IOException ioe) {
                Debug.debugException(ioe);
                resultCode = ResultCode.LOCAL_ERROR;
                wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ERR_SPLIT_LDIF_IO_READ_ERROR.get(StaticUtils.getExceptionMessage(ioe)));
                break;
            } catch (final Exception e) {
                Debug.debugException(e);
                resultCode = ResultCode.LOCAL_ERROR;
                wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ERR_SPLIT_LDIF_UNEXPECTED_READ_ERROR.get(StaticUtils.getExceptionMessage(e)));
                break;
            }
            if (entry == null) {
                break;
            }
            final long readCount = entriesRead.incrementAndGet();
            if ((readCount % 1000L) == 0) {
                // Even though we aren't done with this entry yet, we'll go ahead and
                // log a progress message now because it's easier to do that now than
                // to ensure that it's handled properly through all possible error
                // conditions that need to be handled below.
                wrapOut(0, MAX_OUTPUT_LINE_LENGTH, INFO_SPLIT_LDIF_PROGRESS.get(readCount));
            }
            // Get the set(s) to which the entry should be written.  If this is
            // null (which could be the case as a result of a race condition when
            // using multiple threads where processing for a child completes before
            // processing for its parent, or as a result of a case in which a
            // child is included without or before its parent), then try to see if
            // we can get the sets by passing the entry through the translator.
            Set<String> sets = entry.getSets();
            byte[] ldifBytes = entry.getLDIFBytes();
            if (sets == null) {
                try {
                    sets = translator.translate(entry, 0L).getSets();
                } catch (final Exception e) {
                    Debug.debugException(e);
                }
                if (sets == null) {
                    final SplitLDIFEntry errorEntry = translator.createEntry(entry, ERR_SPLIT_LDIF_ENTRY_WITHOUT_PARENT.get(entry.getDN(), splitBaseDN.getStringValue()), Collections.singleton(SplitLDIFEntry.SET_NAME_ERRORS));
                    ldifBytes = errorEntry.getLDIFBytes();
                    sets = errorEntry.getSets();
                }
            }
            // to do anything else.
            if (sets.isEmpty()) {
                entriesExcluded.incrementAndGet();
                continue;
            }
            // files if necessary.
            for (final String set : sets) {
                if (set.equals(SplitLDIFEntry.SET_NAME_ERRORS)) {
                    // This indicates that an error was encountered during processing,
                    // so we'll update the result code to reflect that.
                    resultCode = ResultCode.LOCAL_ERROR;
                }
                final File f = getOutputFile(set);
                OutputStream s = outputStreams.get(set);
                if (s == null) {
                    try {
                        s = new FileOutputStream(f);
                        if (encryptTarget.isPresent()) {
                            if (encryptionPassphrase == null) {
                                try {
                                    encryptionPassphrase = ToolUtils.promptForEncryptionPassphrase(false, true, getOut(), getErr());
                                } catch (final LDAPException ex) {
                                    Debug.debugException(ex);
                                    wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ex.getMessage());
                                    return ex.getResultCode();
                                }
                            }
                            s = new PassphraseEncryptedOutputStream(encryptionPassphrase, s);
                        }
                        if (compressTarget.isPresent()) {
                            s = new GZIPOutputStream(s);
                        }
                        outputStreams.put(set, s);
                        fileCounts.put(set, new AtomicLong(0L));
                    } catch (final Exception e) {
                        Debug.debugException(e);
                        resultCode = ResultCode.LOCAL_ERROR;
                        wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ERR_SPLIT_LDIF_CANNOT_OPEN_OUTPUT_FILE.get(f.getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
                        break readLoop;
                    }
                }
                try {
                    s.write(ldifBytes);
                } catch (final Exception e) {
                    Debug.debugException(e);
                    resultCode = ResultCode.LOCAL_ERROR;
                    wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ERR_SPLIT_LDIF_ERROR_WRITING_TO_FILE.get(entry.getDN(), f.getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
                    break readLoop;
                }
                fileCounts.get(set).incrementAndGet();
            }
        }
        // Processing is complete.  Summarize the processing that was performed.
        final long finalReadCount = entriesRead.get();
        if (finalReadCount > 1000L) {
            out();
        }
        wrapOut(0, MAX_OUTPUT_LINE_LENGTH, INFO_SPLIT_LDIF_PROCESSING_COMPLETE.get(finalReadCount));
        final long excludedCount = entriesExcluded.get();
        if (excludedCount > 0L) {
            wrapOut(0, MAX_OUTPUT_LINE_LENGTH, INFO_SPLIT_LDIF_EXCLUDED_COUNT.get(excludedCount));
        }
        for (final Map.Entry<String, AtomicLong> e : fileCounts.entrySet()) {
            final File f = getOutputFile(e.getKey());
            wrapOut(0, MAX_OUTPUT_LINE_LENGTH, INFO_SPLIT_LDIF_COUNT_TO_FILE.get(e.getValue().get(), f.getName()));
        }
    } finally {
        try {
            ldifReader.close();
        } catch (final Exception e) {
            Debug.debugException(e);
        }
        for (final Map.Entry<String, OutputStream> e : outputStreams.entrySet()) {
            try {
                e.getValue().close();
            } catch (final Exception ex) {
                Debug.debugException(ex);
                resultCode = ResultCode.LOCAL_ERROR;
                wrapErr(0, MAX_OUTPUT_LINE_LENGTH, ERR_SPLIT_LDIF_ERROR_CLOSING_FILE.get(getOutputFile(e.getKey()), StaticUtils.getExceptionMessage(ex)));
            }
        }
    }
    return resultCode;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Schema(com.unboundid.ldap.sdk.schema.Schema) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) LinkedHashMap(java.util.LinkedHashMap) GZIPOutputStream(java.util.zip.GZIPOutputStream) LDIFException(com.unboundid.ldif.LDIFException) ArrayList(java.util.ArrayList) List(java.util.List) ByteStringBuffer(com.unboundid.util.ByteStringBuffer) InputStream(java.io.InputStream) IOException(java.io.IOException) TreeMap(java.util.TreeMap) LDIFException(com.unboundid.ldif.LDIFException) ArgumentException(com.unboundid.util.args.ArgumentException) LDAPException(com.unboundid.ldap.sdk.LDAPException) IOException(java.io.IOException) AtomicLong(java.util.concurrent.atomic.AtomicLong) LDAPException(com.unboundid.ldap.sdk.LDAPException) Filter(com.unboundid.ldap.sdk.Filter) LDIFReader(com.unboundid.ldif.LDIFReader) FileOutputStream(java.io.FileOutputStream) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) ResultCode(com.unboundid.ldap.sdk.ResultCode) NotNull(com.unboundid.util.NotNull)

Aggregations

ByteStringBuffer (com.unboundid.util.ByteStringBuffer)142 Test (org.testng.annotations.Test)99 NotNull (com.unboundid.util.NotNull)22 DN (com.unboundid.ldap.sdk.DN)20 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)11 Entry (com.unboundid.ldap.sdk.Entry)10 ResultCode (com.unboundid.ldap.sdk.ResultCode)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 InputStream (java.io.InputStream)8 Modification (com.unboundid.ldap.sdk.Modification)7 File (java.io.File)6 ModifyRequest (com.unboundid.ldap.sdk.ModifyRequest)5 RDN (com.unboundid.ldap.sdk.RDN)5 FileOutputStream (java.io.FileOutputStream)5 Map (java.util.Map)5 Control (com.unboundid.ldap.sdk.Control)4 LDAPException (com.unboundid.ldap.sdk.LDAPException)4 ManageDsaITRequestControl (com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl)4 ArgumentException (com.unboundid.util.args.ArgumentException)4