use of org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer in project directory-ldap-api by apache.
the class Value method computeNormValue.
/**
* Compute the normalized value
*
* @throws LdapException If we were'nt able to normalize the value
*/
private void computeNormValue() throws LdapException {
if (upValue == null) {
return;
}
Normalizer normalizer;
// We should have a Equality MatchingRule
MatchingRule equality = attributeType.getEquality();
if (equality == null) {
// Let's try with the Substring MatchingRule
MatchingRule subString = attributeType.getSubstring();
if (subString == null) {
// last chance : ordering matching rule
MatchingRule ordering = attributeType.getOrdering();
if (ordering == null) {
// Ok, no luck. Use a NoOp normalizer
normalizer = new NoOpNormalizer();
} else {
normalizer = ordering.getNormalizer();
}
} else {
normalizer = subString.getNormalizer();
}
} else {
normalizer = equality.getNormalizer();
}
if (normalizer == null) {
throw new IllegalArgumentException(I18n.err(I18n.ERR_13220_NO_NORMALIZER));
}
// Now, normalize the upValue
normValue = normalizer.normalize(upValue);
}
use of org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer in project directory-ldap-api by apache.
the class EntryUtils method getBytesAttributeType.
/* No protection */
static AttributeType getBytesAttributeType() {
MutableAttributeType attributeType = new MutableAttributeType("1.2");
LdapSyntax syntax = new LdapSyntax("1.2.1", "", false);
syntax.setSyntaxChecker(new SyntaxChecker("1.2.1") {
public static final long serialVersionUID = 1L;
public boolean isValidSyntax(Object value) {
return (value == null) || (((byte[]) value).length < 5);
}
});
MutableMatchingRule matchingRule = new MutableMatchingRule("1.2.2");
matchingRule.setSyntax(syntax);
matchingRule.setLdapComparator(new ByteArrayComparator("1.2.2"));
matchingRule.setNormalizer(new NoOpNormalizer("1.1.1"));
attributeType.setEquality(matchingRule);
attributeType.setSyntax(syntax);
return attributeType;
}
use of org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer in project directory-ldap-api by apache.
the class StringValueAttributeTypeTest method testConstrainedString.
/**
* Presumes an attribute which constrains it's values to some constant
* strings: LOW, MEDIUM, HIGH. Normalization does nothing. MatchingRules
* are exact case matching.
*
* @throws Exception on errors
*/
@Test
public void testConstrainedString() throws LdapInvalidAttributeValueException {
s.setSyntaxChecker(new SyntaxChecker("1.1.1.1") {
public static final long serialVersionUID = 1L;
public boolean isValidSyntax(Object value) {
if (value instanceof String) {
String strval = (String) value;
return strval.equals("HIGH") || strval.equals("LOW") || strval.equals("MEDIUM");
}
return false;
}
});
mr.setSyntax(s);
mr.setNormalizer(new NoOpNormalizer(mr.getOid()));
at.setEquality(mr);
at.setSyntax(s);
// check that normalization and syntax checks work as expected
Value value = new Value(at, "HIGH");
assertEquals(value.getValue(), value.getValue());
try {
new Value(at, "high");
fail();
} catch (LdapInvalidAttributeValueException liave) {
// expected
}
// create a bunch to best tested for equals and in containers
Value v0 = new Value(at, "LOW");
Value v1 = new Value(at, "LOW");
Value v2 = new Value(at, "MEDIUM");
Value v3 = new Value(at, "HIGH");
// check equals
assertTrue(v0.equals(v1));
assertTrue(v1.equals(v0));
assertEquals(0, v0.compareTo(v1));
assertFalse(v2.equals(v3));
assertFalse(v3.equals(v2));
assertTrue(v2.compareTo(v3) > 0);
assertTrue(v3.compareTo(v2) < 0);
// add all except v1 and v5 to a set
HashSet<Value> set = new HashSet<Value>();
set.add(v0);
set.add(v2);
set.add(v3);
// check contains method
assertTrue("since v1.equals( v0 ) and v0 was added then this should be true", set.contains(v1));
// check ordering based on the comparator
List<Value> list = new ArrayList<Value>();
list.add(v1);
list.add(v3);
list.add(v0);
list.add(v2);
Collections.sort(list);
// High, low, low, medium
assertTrue("since v0 equals v1 either could be at index 0 & 1", list.get(0).equals(v3));
assertTrue("since v0 equals v1 either could be at index 0 & 1", list.get(1).equals(v0));
assertTrue("since v2 \"MEDIUM\" should be at index 2", list.get(2).equals(v1));
assertTrue("since v3 \"HIGH\" should be at index 3", list.get(3).equals(v2));
assertEquals(4, list.size());
}
Aggregations