DnParser { public static String substring(final String dn, final int beginIndex) { if (beginIndex < 0) { throw new IndexOutOfBoundsException("beginIndex cannot be negative"); } final List<LdapAttribute> attrs = convertDnToAttributes(dn); if (beginIndex >= attrs.size()) { throw new IndexOutOfBoundsException("beginIndex cannot be larger than the number of RDNs"); } final StringBuilder sb = new StringBuilder(); for (int i = 0; i < attrs.size(); i++) { if (i >= beginIndex) { final LdapAttribute la = attrs.get(i); sb.append(la.getName()).append("=").append(la.getStringValue()).append(","); } } if (sb.length() > 0 && sb.charAt(sb.length() - 1) == ',') { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); } private  DnParser(); static Collection<String> getValues(final String dn, final String name); static String getValue(final String dn, final String name); static String substring(final String dn, final int beginIndex); static String substring(final String dn, final int beginIndex, final int endIndex); static List<LdapAttribute> convertDnToAttributes(final String dn);  }
@Test(groups = "dnParser") public void testSubstring() throws Exception { final String dn = "CN=Jim Smith,UID=jsmith,DC=ldaptive,DC=org"; try { DnParser.substring(dn, -1); Assert.fail("Should have thrown IndexOutOfBoundsException"); } catch (Exception e) { Assert.assertEquals(e.getClass(), IndexOutOfBoundsException.class); } Assert.assertEquals(DnParser.substring(dn, 0), "CN=Jim Smith,UID=jsmith,DC=ldaptive,DC=org"); Assert.assertEquals(DnParser.substring(dn, 1), "UID=jsmith,DC=ldaptive,DC=org"); Assert.assertEquals(DnParser.substring(dn, 2), "DC=ldaptive,DC=org"); Assert.assertEquals(DnParser.substring(dn, 3), "DC=org"); try { DnParser.substring(dn, 4); Assert.fail("Should have thrown IndexOutOfBoundsException"); } catch (Exception e) { Assert.assertEquals(e.getClass(), IndexOutOfBoundsException.class); } }
@Test(groups = "dnParser") public void testSubstringEndIndex() throws Exception { final String dn = "CN=Jim Smith,UID=jsmith,DC=ldaptive,DC=org"; try { DnParser.substring(dn, -1, 0); Assert.fail("Should have thrown IndexOutOfBoundsException"); } catch (Exception e) { Assert.assertEquals(e.getClass(), IndexOutOfBoundsException.class); } try { DnParser.substring(dn, 2, 1); Assert.fail("Should have thrown IndexOutOfBoundsException"); } catch (Exception e) { Assert.assertEquals(e.getClass(), IndexOutOfBoundsException.class); } Assert.assertEquals(DnParser.substring(dn, 0, 4), "CN=Jim Smith,UID=jsmith,DC=ldaptive,DC=org"); Assert.assertEquals(DnParser.substring(dn, 1, 4), "UID=jsmith,DC=ldaptive,DC=org"); Assert.assertEquals(DnParser.substring(dn, 2, 4), "DC=ldaptive,DC=org"); Assert.assertEquals(DnParser.substring(dn, 3, 4), "DC=org"); Assert.assertEquals(DnParser.substring(dn, 0, 1), "CN=Jim Smith"); Assert.assertEquals(DnParser.substring(dn, 1, 2), "UID=jsmith"); Assert.assertEquals(DnParser.substring(dn, 2, 3), "DC=ldaptive"); Assert.assertEquals(DnParser.substring(dn, 3, 4), "DC=org"); Assert.assertEquals(DnParser.substring(dn, 0, 2), "CN=Jim Smith,UID=jsmith"); Assert.assertEquals(DnParser.substring(dn, 2, 4), "DC=ldaptive,DC=org"); try { DnParser.substring(dn, 1, 5); Assert.fail("Should have thrown IndexOutOfBoundsException"); } catch (Exception e) { Assert.assertEquals(e.getClass(), IndexOutOfBoundsException.class); } }