From 50ef62b427894e02161742009f6ea1a28f0a6269 Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Thu, 30 Oct 2014 09:14:39 +0000
Subject: [PATCH] Improvements to DN class.
---
opendj-core/src/test/java/org/forgerock/opendj/ldap/DNTestCase.java | 75 ++++++++++++++++++++++++++++++-------
opendj-core/src/main/java/org/forgerock/opendj/ldap/DN.java | 18 +++++----
2 files changed, 71 insertions(+), 22 deletions(-)
diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldap/DN.java b/opendj-core/src/main/java/org/forgerock/opendj/ldap/DN.java
index 629256b..c9a7729 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldap/DN.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/ldap/DN.java
@@ -36,6 +36,7 @@
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.LocalizedIllegalArgumentException;
+import org.forgerock.opendj.ldap.schema.AttributeType;
import org.forgerock.opendj.ldap.schema.MatchingRule;
import org.forgerock.opendj.ldap.schema.Schema;
import org.forgerock.opendj.ldap.schema.Syntax;
@@ -512,9 +513,8 @@
public int hashCode() {
if (size == 0) {
return 0;
- } else {
- return 31 * parent.hashCode() + rdn.hashCode();
}
+ return 31 * parent.hashCode() + rdn.hashCode();
}
/**
@@ -928,7 +928,7 @@
return ByteString.empty();
}
- final StringBuilder builder = new StringBuilder(size());
+ final StringBuilder builder = new StringBuilder();
int i = size() - 1;
normalizeRDN(builder, parent(i).rdn());
for (i--; i >= 0; i--) {
@@ -1087,8 +1087,10 @@
* @return The normalized string representation of the provided AVA.
*/
private static StringBuilder normalizeAVA(final StringBuilder builder, final AVA ava) {
+ final AttributeType attributeType = ava.getAttributeType();
+
ByteString value = ava.getAttributeValue();
- final MatchingRule matchingRule = ava.getAttributeType().getEqualityMatchingRule();
+ final MatchingRule matchingRule = attributeType.getEqualityMatchingRule();
if (matchingRule != null) {
try {
value = matchingRule.normalizeAttributeValue(ava.getAttributeValue());
@@ -1097,18 +1099,18 @@
}
}
- if (!ava.getAttributeType().getNames().iterator().hasNext()) {
- builder.append(ava.getAttributeType().getOID());
+ if (attributeType.getNames().isEmpty()) {
+ builder.append(attributeType.getOID());
builder.append("=#");
builder.append(value.toHexString());
} else {
- final String name = ava.getAttributeType().getNameOrOID();
+ final String name = attributeType.getNameOrOID();
// Normalizing.
StaticUtils.toLowerCase(name, builder);
builder.append("=");
- final Syntax syntax = ava.getAttributeType().getSyntax();
+ final Syntax syntax = attributeType.getSyntax();
if (!syntax.isHumanReadable()) {
builder.append("#");
builder.append(value.toHexString());
diff --git a/opendj-core/src/test/java/org/forgerock/opendj/ldap/DNTestCase.java b/opendj-core/src/test/java/org/forgerock/opendj/ldap/DNTestCase.java
index 7df63f2..d7f2c9e 100644
--- a/opendj-core/src/test/java/org/forgerock/opendj/ldap/DNTestCase.java
+++ b/opendj-core/src/test/java/org/forgerock/opendj/ldap/DNTestCase.java
@@ -22,20 +22,19 @@
*
*
* Copyright 2010 Sun Microsystems, Inc.
- * Portions copyright 2011-2012 ForgeRock AS.
+ * Portions copyright 2011-2014 ForgeRock AS.
*/
-
package org.forgerock.opendj.ldap;
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
-import static org.testng.Assert.fail;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
import org.forgerock.i18n.LocalizedIllegalArgumentException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
+import static org.testng.Assert.*;
+
/**
* This class defines a set of tests for the org.forgerock.opendj.ldap.DN class.
*/
@@ -976,8 +975,8 @@
@Test(dataProvider = "createRenameTestData")
public void testRename(final String dn, final String fromDN, final String toDN,
final String expectedDN) {
- assertEquals(DN.valueOf(dn).rename(DN.valueOf(fromDN), DN.valueOf(toDN)), DN
- .valueOf(expectedDN));
+ DN actual = DN.valueOf(dn).rename(DN.valueOf(fromDN), DN.valueOf(toDN));
+ assertEquals(actual, DN.valueOf(expectedDN));
}
/**
@@ -991,9 +990,7 @@
assertEquals(actual.toString(), "deviceId=123,uid=bjensen,dc=test");
}
- /**
- * Tests the {@link DN#format(String, Object...)} method.
- */
+ /** Tests the {@link DN#format(String, Object...)} method. */
@Test
public void testFormatEscape() {
DN actual = DN.format("uid=%s,dc=test", "#cn=foo+sn=bar");
@@ -1002,12 +999,62 @@
assertEquals(actual.toString(), "uid=\\#cn\\=foo\\+sn\\=bar,dc=test");
}
- /**
- * Tests the {@link DN#escapeAttributeValue(Object)} method.
- */
+ /** Tests the {@link DN#escapeAttributeValue(Object)} method. */
@Test
public void testEscapeAttributeValue() {
String actual = DN.escapeAttributeValue("#cn=foo+sn=bar");
assertEquals(actual, "\\#cn\\=foo\\+sn\\=bar");
}
+
+ /** Tests the {@link DN#toIrreversibleNormalizedByteString()} method. */
+ @Test
+ public void testToIrreversibleNormalizedByteStringWithRootDN() {
+ ByteString actual = DN.rootDN().toIrreversibleNormalizedByteString();
+ assertEquals(actual, ByteString.empty());
+ }
+
+ /** Tests the {@link DN#iterator()} method. */
+ @Test
+ public void testIterator() {
+ final String childRdn = "dc=example";
+ final String parentRdn = "dc=com";
+ final Iterator<RDN> it = DN.valueOf(childRdn + "," + parentRdn).iterator();
+ assertTrue(it.hasNext());
+ assertEquals(it.next(), RDN.valueOf(childRdn));
+ assertTrue(it.hasNext());
+ assertEquals(it.next(), RDN.valueOf(parentRdn));
+ assertFalse(it.hasNext());
+ try {
+ it.next();
+ fail("Expected NoSuchElementException to be thrown");
+ } catch (NoSuchElementException expected) {
+ // do nothing
+ }
+ try {
+ it.remove();
+ fail("Expected UnsupportedOperationException to be thrown");
+ } catch (UnsupportedOperationException expected) {
+ // do nothing
+ }
+ }
+
+ @DataProvider
+ public Object[][] toIrreversibleNormalizedByteStringDataProvider() {
+ // @formatter:off
+ return new Object[][] {
+ { "dc=com", "dc=com" },
+ { "dc=example,dc=com", "dc=example,dc=com" },
+ { "dc=example,dc=com", "dc = example, dc = com" },
+ { "dc=example+cn=test,dc=com", "cn=test+dc=example,dc=com" },
+ };
+ // @formatter:on
+ }
+
+ /** Tests the {@link DN#toIrreversibleNormalizedByteString()} method. */
+ @Test(dataProvider = "toIrreversibleNormalizedByteStringDataProvider")
+ public void testToIrreversibleNormalizedByteString(String actualStr, String expectedStr) {
+ DN actual = DN.valueOf(actualStr);
+ DN expected = DN.valueOf(expectedStr);
+ assertEquals(actual.toIrreversibleNormalizedByteString(), expected.toIrreversibleNormalizedByteString());
+ }
}
--
Gitblit v1.10.0