You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2008/04/22 12:12:01 UTC
svn commit: r650435 - in /harmony/enhanced/classlib/trunk/modules/jndi/src:
main/java/org/apache/harmony/jndi/provider/ldap/
main/java/org/apache/harmony/jndi/provider/ldap/parser/
test/java/org/apache/harmony/jndi/provider/ldap/
Author: tellison
Date: Tue Apr 22 03:11:49 2008
New Revision: 650435
URL: http://svn.apache.org/viewvc?rev=650435&view=rev
Log:
Apply patch HARMONY-5716 ([classlib][jndi][ldap] - Refine implementation of LdapSchemaContextImpl)
Added:
harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/parser/SchemaParser.java (with props)
harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextTest.java (with props)
Modified:
harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImpl.java
Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java?rev=650435&r1=650434&r2=650435&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java (original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapContextImpl.java Tue Apr 22 03:11:49 2008
@@ -95,6 +95,7 @@
import org.apache.harmony.jndi.provider.ldap.parser.FilterParser;
import org.apache.harmony.jndi.provider.ldap.parser.LdapUrlParser;
import org.apache.harmony.jndi.provider.ldap.parser.ParseException;
+import org.apache.harmony.jndi.provider.ldap.parser.SchemaParser;
import org.apache.harmony.jndi.provider.ldap.sasl.SaslBind;
/**
@@ -201,7 +202,7 @@
Hashtable<Object, Object> environment, String dn)
throws NamingException {
initial(client, environment, dn);
- doBindOperation(connCtls);
+ doBindOperation(connCtls);
}
private void initial(LdapClient ldapClient,
@@ -245,19 +246,19 @@
SaslBind.AuthMech authMech = saslBind.valueAuthMech(env);
try {
- if (authMech == SaslBind.AuthMech.None) {
- BindOp bind = new BindOp("", "", null, null);
- client.doOperation(bind, connCtsl);
- result = bind.getResult();
- } else if (authMech == SaslBind.AuthMech.Simple) {
- String principal = (String) env.get(Context.SECURITY_PRINCIPAL);
- String credential = Utils.getString(env
- .get(Context.SECURITY_CREDENTIALS));
- BindOp bind = new BindOp(principal, credential, null, null);
- client.doOperation(bind, connCtsl);
- result = bind.getResult();
- } else if (authMech == SaslBind.AuthMech.SASL) {
- result = saslBind.doSaslBindOperation(env, client, connCtsl);
+ if (authMech == SaslBind.AuthMech.None) {
+ BindOp bind = new BindOp("", "", null, null);
+ client.doOperation(bind, connCtsl);
+ result = bind.getResult();
+ } else if (authMech == SaslBind.AuthMech.Simple) {
+ String principal = (String) env.get(Context.SECURITY_PRINCIPAL);
+ String credential = Utils.getString(env
+ .get(Context.SECURITY_CREDENTIALS));
+ BindOp bind = new BindOp(principal, credential, null, null);
+ client.doOperation(bind, connCtsl);
+ result = bind.getResult();
+ } else if (authMech == SaslBind.AuthMech.SASL) {
+ result = saslBind.doSaslBindOperation(env, client, connCtsl);
}
} catch (IOException e) {
CommunicationException ex = new CommunicationException();
@@ -761,14 +762,17 @@
public static Hashtable<String, Hashtable<String, Hashtable<String, Object>>> schemaTree = new Hashtable<String, Hashtable<String, Hashtable<String, Object>>>();
+ private Hashtable<String, Object> schemaTable = new Hashtable<String, Object>();
+
private LdapSchemaContextImpl ldapSchemaCtx = null;
protected String subschemasubentry = null;
public DirContext getSchema(Name name) throws NamingException {
checkName(name);
- if (null != ldapSchemaCtx)
+ if (null != ldapSchemaCtx) {
return ldapSchemaCtx;
+ }
SearchControls searchControls = new SearchControls();
SearchOp search = null;
@@ -778,9 +782,6 @@
Map<String, Attributes> names = null;
Set<String> keyset = null;
- if (name.size() != 0) {
- subschemasubentry = name.toString() + "," + contextDn.toString();
- }
if (null == subschemasubentry) {
filterParser = new FilterParser("(objectClass=*)");
try {
@@ -795,7 +796,7 @@
searchControls.setSearchScope(SearchControls.OBJECT_SCOPE);
searchControls.setReturningAttributes(new String[] {
"namingContexts", "subschemaSubentry", "altServer", });
- search = new SearchOp("", searchControls, filter);
+ search = new SearchOp(name.toString(), searchControls, filter);
try {
client.doOperation(search, requestControls);
@@ -813,6 +814,7 @@
names = sre.getEntries();
keyset = names.keySet();
+
schemaRoot: for (Iterator<String> iterator = keyset.iterator(); iterator
.hasNext();) {
String key = iterator.next();
@@ -828,6 +830,10 @@
}
}
+ if (null == subschemasubentry) {
+ return getSchema(name.getPrefix(name.size() - 1));
+ }
+
searchControls.setSearchScope(SearchControls.OBJECT_SCOPE);
searchControls.setReturningAttributes(new String[] { "objectclasses",
"attributetypes", "matchingrules", "ldapsyntaxes" });
@@ -871,17 +877,32 @@
if (!schemaTree.contains(schemaType.toLowerCase())) {
schemaTree.put(schemaType.toLowerCase(),
new Hashtable<String, Hashtable<String, Object>>());
+
+ schemaTable.put(schemaType.toLowerCase(),
+ new Hashtable<String, String>());
}
Hashtable<String, Hashtable<String, Object>> schemaDefs = schemaTree
.get(schemaType.toLowerCase());
+
+ Hashtable<String, String> schemaDef = (Hashtable<String, String>) schemaTable
+ .get(schemaType.toLowerCase());
LdapAttribute attribute = (LdapAttribute) as.get(schemaType);
+
+ String value;
+ String attrName;
for (int i = 0; i < attribute.size(); i++) {
- String value = (String) attribute.get(i);
+ value = (String) attribute.get(i);
+ attrName = SchemaParser.getName(value);
+ schemaDef.put(attrName.toLowerCase(), value);
+
parseValue(schemaType, value.toLowerCase(), schemaDefs);
}
+
}
}
- ldapSchemaCtx = new LdapSchemaContextImpl(this, env, name);
+
+ ldapSchemaCtx = new LdapSchemaContextImpl(this, env, name,
+ schemaTable, LdapSchemaContextImpl.SCHEMA_ROOT_LEVEL);
return ldapSchemaCtx;
}
@@ -900,7 +921,7 @@
*/
private static void parseValue(String schemaType, String value,
Hashtable<String, Hashtable<String, Object>> schemaDefs) {
- StringTokenizer st = new StringTokenizer(value);
+ StringTokenizer st = new StringTokenizer(value.toLowerCase());
// Skip (
st.nextToken();
@@ -917,9 +938,15 @@
if (attrName.startsWith("x-")) {
token = st.nextToken();
// remove the ending ' symbol
- token = token.substring(0, token.length() - 1);
+ token = token.substring(1, token.length() - 1);
schemaDef.put(attrName, token);
}
+ if (attrName.startsWith("auxiliary")) {
+ schemaDef.put(attrName, "true");
+ }
+ if (attrName.startsWith("structural")) {
+ schemaDef.put(attrName, "true");
+ }
if (attrName.equals("usage") || attrName.equals("equality")
|| attrName.equals("syntax") || attrName.equals("ordering")
|| attrName.equals("substr")) {
@@ -1032,6 +1059,8 @@
public DirContext getSchemaClassDefinition(Name name)
throws NamingException {
+ checkName(name);
+
if (null == ldapSchemaCtx) {
getSchema("");
}
@@ -1282,7 +1311,8 @@
sr.setNameInNamespace(dn.substring(index + 1, dn.length()));
list.add(sr);
} else {
- String relativeName = LdapUtils.convertToRelativeName(dn, baseDN);
+ String relativeName = LdapUtils.convertToRelativeName(dn,
+ baseDN);
sr = new SearchResult(relativeName, null, entries.get(dn));
sr.setNameInNamespace(dn);
}
@@ -1340,7 +1370,8 @@
sr.setNameInNamespace(dn.substring(index + 1, dn.length()));
list.add(sr);
} else {
- String relativeName = LdapUtils.convertToRelativeName(dn, baseDN);
+ String relativeName = LdapUtils.convertToRelativeName(dn,
+ baseDN);
sr = new SearchResult(relativeName, null, entries.get(dn));
sr.setNameInNamespace(dn);
}
@@ -1789,7 +1820,6 @@
.getException());
}
-
protected String getTargetDN(Name name, Name prefix)
throws NamingException, InvalidNameException {
Name target = null;
@@ -2588,36 +2618,40 @@
}
private NamingEvent constructNamingEvent(PersistentSearchResult result,
- ECNotificationControl control, String baseDN) throws InvalidNameException, NamingException {
+ ECNotificationControl control, String baseDN)
+ throws InvalidNameException, NamingException {
Binding newBinding = null;
Binding oldBinding = null;
switch (control.getChangeType()) {
case ECNotificationControl.ADD:
- String newName = LdapUtils.convertToRelativeName(result.getDn(), baseDN);
+ String newName = LdapUtils.convertToRelativeName(result.getDn(),
+ baseDN);
newBinding = new Binding(newName, null);
newBinding.setNameInNamespace(result.getDn());
break;
case ECNotificationControl.DELETE:
- String deleteName = LdapUtils.convertToRelativeName(result.getDn(), baseDN);
+ String deleteName = LdapUtils.convertToRelativeName(result.getDn(),
+ baseDN);
oldBinding = new Binding(deleteName, null);
oldBinding.setNameInNamespace(result.getDn());
break;
case ECNotificationControl.MODIFY_DN:
if (result.getDn() != null) {
- newBinding = new Binding(LdapUtils.convertToRelativeName(result.getDn(),
- baseDN), null);
+ newBinding = new Binding(LdapUtils.convertToRelativeName(result
+ .getDn(), baseDN), null);
newBinding.setNameInNamespace(result.getDn());
}
if (control.getPreviousDN() != null) {
- oldBinding = new Binding(LdapUtils.convertToRelativeName(control
- .getPreviousDN(), baseDN), null);
+ oldBinding = new Binding(LdapUtils.convertToRelativeName(
+ control.getPreviousDN(), baseDN), null);
oldBinding.setNameInNamespace(control.getPreviousDN());
}
break;
case ECNotificationControl.MODIFY:
- String relativeName = LdapUtils.convertToRelativeName(result.getDn(), baseDN);
+ String relativeName = LdapUtils.convertToRelativeName(result
+ .getDn(), baseDN);
newBinding = new Binding(relativeName, null);
newBinding.setNameInNamespace(result.getDn());
// FIXME: how to get old binding?
Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImpl.java?rev=650435&r1=650434&r2=650435&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImpl.java (original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImpl.java Tue Apr 22 03:11:49 2008
@@ -52,6 +52,7 @@
import org.apache.harmony.jndi.internal.parser.AttributeTypeAndValuePair;
import org.apache.harmony.jndi.provider.ldap.parser.FilterParser;
import org.apache.harmony.jndi.provider.ldap.parser.ParseException;
+import org.apache.harmony.jndi.provider.ldap.parser.SchemaParser;
public class LdapSchemaContextImpl extends LdapContextImpl {
@@ -71,6 +72,8 @@
public static final String MATCHING_RULES = "matchingrules";
+ public static final int SCHEMA_ROOT_LEVEL = 3;
+
protected String subschemasubentry = null;
final private static Hashtable<String, String> schemaJndi2Ldap = new Hashtable<String, String>();
@@ -91,28 +94,47 @@
private LdapContextImpl parent;
+ private Hashtable<String, Object> schemaTable;
+
private Name rdn = null;
- public LdapSchemaContextImpl(LdapContextImpl ctx, Hashtable<Object, Object> env,
- Name dn) throws InvalidNameException {
+ private int level;
+
+ public LdapSchemaContextImpl(LdapContextImpl ctx,
+ Hashtable<Object, Object> env, Name dn) throws InvalidNameException {
+ super(ctx, env, dn.getPrefix(0).toString());
+ parent = ctx;
+ rdn = dn;
+ }
+
+ public LdapSchemaContextImpl(LdapContextImpl ctx,
+ Hashtable<Object, Object> env, Name dn,
+ Hashtable<String, Object> schemaTable, int level)
+ throws InvalidNameException {
super(ctx, env, dn.getPrefix(0).toString());
parent = ctx;
rdn = dn;
+ this.schemaTable = schemaTable;
+ this.level = level;
}
+ @Override
public DirContext getSchema(Name name) throws NamingException {
throw new OperationNotSupportedException();
}
+ @Override
public DirContext getSchema(String name) throws NamingException {
throw new OperationNotSupportedException();
}
+ @Override
public DirContext getSchemaClassDefinition(Name name)
throws NamingException {
throw new OperationNotSupportedException();
}
+ @Override
public DirContext getSchemaClassDefinition(String name)
throws NamingException {
throw new OperationNotSupportedException();
@@ -132,11 +154,12 @@
throw new SchemaViolationException(Messages.getString("jndi.8D"));
}
- String schema = schemaJndi2Ldap.get(schemaType.toString().toLowerCase());
+ String schema = schemaJndi2Ldap
+ .get(schemaType.toString().toLowerCase());
if (null == schema) {
throw new SchemaViolationException(Messages.getString("jndi.8D"));
}
- if (!LdapContextImpl.schemaTree.keySet().contains(schema)) {
+ if (schemaTable.keySet().contains(schema)) {
throw new SchemaViolationException(Messages.getString("jndi.8E"));
}
String targetDN = rdn.toString() + parent.subschemasubentry;
@@ -176,7 +199,9 @@
} catch (ReferralException e) {
// TODO
}
- return new LdapSchemaContextImpl(parent, parent.env, name);
+
+ return new LdapSchemaContextImpl(parent, parent.env, name, null,
+ LdapSchemaContextImpl.SCHEMA_ROOT_LEVEL);
}
public DirContext createSubcontext(String name, Attributes attributes)
@@ -185,45 +210,55 @@
return createSubcontext(n, attributes);
}
+ /* (non-Javadoc)
+ * @see org.apache.harmony.jndi.provider.ldap.LdapContextImpl#getAttributes(javax.naming.Name)
+ */
+ @Override
public Attributes getAttributes(Name name) throws NamingException {
- Name targetDN = (rdn.size() != 0) ? name.addAll(rdn) : name;
+ int size = name.size();
- BasicAttributes schemaAttributes = new BasicAttributes(true);
+ Hashtable<String, Object> attributesTable = doLookup(name, size);
- Set<String> keyset = null;
- int size = targetDN.size();
- switch (size) {
- case 0:
- break;
+ BasicAttributes schemaAttributes = new BasicAttributes();
+
+ switch (level - size) {
case 1:
- String schemaType = schemaJndi2Ldap.get(name.get(0).toLowerCase());
- if (null == schemaType) {
- throw new NameNotFoundException(name.toString());
- }
- schemaAttributes.put(new BasicAttribute("objectclass", name.get(0)
- .toLowerCase()));
- break;
- default:
- Hashtable<String, Object> classDef = parent.findSchemaDefInfo(
- schemaJndi2Ldap.get(name.get(0).toLowerCase()), name.get(1));
- if (null == classDef) {
- throw new NameNotFoundException(name.toString());
- }
- schemaAttributes = new BasicAttributes(true);
- keyset = classDef.keySet();
+ Set<String> keyset = attributesTable.keySet();
for (Iterator<String> i = keyset.iterator(); i.hasNext();) {
String id = i.next();
- if (id.equals("orig")) {
+ if (id.equals("orig")) { //$NON-NLS-1$
continue;
}
- Object value = classDef.get(id);
- schemaAttributes
- .put(new BasicAttribute(id.toLowerCase(), value));
+ Object value = attributesTable.get(id);
+ BasicAttribute basicAttr = new BasicAttribute(id);
+
+ if (value instanceof List) {
+ List<Object> list = (List<Object>) value;
+ for (int j = 0; j < list.size(); j++) {
+ basicAttr.add(list.get(j));
+ }
+ } else {
+ basicAttr.add(value);
+ }
+ schemaAttributes.put(basicAttr);
}
+ break;
+
+ case 2:
+ BasicAttribute basicAttr = new BasicAttribute("objectclass"); //$NON-NLS-1$
+ Name allName = name.addAll(rdn);
+ basicAttr.add(allName.toString());
+ schemaAttributes.put(basicAttr);
+ break;
+
+ default:
+ // Do nothing.
}
+
return schemaAttributes;
}
+ @Override
public Attributes getAttributes(Name name, String[] as)
throws NamingException {
Attributes attrs = getAttributes(name);
@@ -238,12 +273,14 @@
return filteredAttrs;
}
+ @Override
public Attributes getAttributes(String attributeName)
throws NamingException {
Name name = new CompositeName(attributeName);
return getAttributes(name);
}
+ @Override
public Attributes getAttributes(String name, String[] as)
throws NamingException {
return getAttributes(new CompositeName(name), as);
@@ -328,14 +365,14 @@
addValue.append(attr.getID()).append(" ").append(attr.get());
}
addValue.append(" )");
- BasicAttribute newAttr = new LdapAttribute(
- new BasicAttribute(OBJECT_CLASSES, oldValue.replace(")",
- addValue.toString())), this);
+ BasicAttribute newAttr = new LdapAttribute(new BasicAttribute(
+ OBJECT_CLASSES, oldValue.replace(")", addValue.toString())),
+ this);
op.addModification(jndi2ldap[DirContext.REMOVE_ATTRIBUTE],
new LdapAttribute(oldAttr, parent));
op.addModification(jndi2ldap[DirContext.ADD_ATTRIBUTE],
new LdapAttribute(newAttr, parent));
-
+
try {
doBasicOperation(op);
} catch (Exception e) {
@@ -402,109 +439,148 @@
destroySubcontext(convertFromStringToName(name));
}
+ private String ldap2jndi(String jndiName) {
+ String ldapName = schemaLdap2Jndi.get(jndiName);
+ if (null == ldapName) {
+ ldapName = jndiName;
+ }
+
+ return ldapName.toLowerCase();
+ }
+
+ private String jndi2ldap(String ldapName) {
+ String jndiName = schemaJndi2Ldap.get(ldapName.toLowerCase());
+ if (null == jndiName) {
+ jndiName = ldapName;
+ }
+
+ return jndiName.toLowerCase();
+ }
+
+ @Override
public NamingEnumeration<NameClassPair> list(Name name)
throws NamingException {
- Name targetDN = name.addAll(rdn);
+ int size = name.size();
+
+ Hashtable<String, Object> tempSchema = doLookup(name, size);
LdapNamingEnumeration<NameClassPair> enumeration = new LdapNamingEnumeration<NameClassPair>(
null, null);
- Set<String> keyset = null;
- int size = targetDN.size();
- switch (size) {
- case 0:
- keyset = LdapContextImpl.schemaTree.keySet();
- for (Iterator<String> i = keyset.iterator(); i.hasNext();) {
- String schemaType = i.next();
- NameClassPair pair = new NameClassPair(schemaLdap2Jndi
- .get(schemaType.toLowerCase()), this.getClass()
- .getName());
- enumeration.add(pair);
- }
- break;
- case 1:
- String schemaType = schemaJndi2Ldap.get(name.get(0).toLowerCase());
- if (null == schemaType) {
- throw new NameNotFoundException(name.toString());
- }
- Hashtable<String, Hashtable<String, Object>> schemas = LdapContextImpl.schemaTree
- .get(schemaType);
- keyset = schemas.keySet();
- for (Iterator<String> i = keyset.iterator(); i.hasNext();) {
- schemaType = i.next();
- NameClassPair pair = new NameClassPair(
- schemaType.toLowerCase(), this.getClass().getName());
- enumeration.add(pair);
- }
- break;
- default:
- schemaType = schemaJndi2Ldap.get(name.getPrefix(1).toString()
- .toLowerCase());
- if (null == schemaType) {
- throw new NameNotFoundException(name.toString());
- }
- list(name.getSuffix(1));
+
+ if (size == level - 1) {
+ return enumeration;
}
+
+ Iterator<String> keys = tempSchema.keySet().iterator();
+
+ while (keys.hasNext()) {
+ enumeration.add(new NameClassPair(ldap2jndi(keys.next()), this
+ .getClass().getName()));
+ }
+
return enumeration;
}
+ @Override
+ protected Name convertFromStringToName(String s)
+ throws InvalidNameException {
+ if (s == null) {
+ // jndi.2E=The name is null
+ throw new NullPointerException(Messages.getString("jndi.2E")); //$NON-NLS-1$
+ }
+
+ CompositeName name = new CompositeName(s);
+ return name;
+ }
+
+ @Override
public NamingEnumeration<NameClassPair> list(String name)
throws NamingException {
- // TODO name supposed to be "" string, what about the situation when
- // name is not ""
return list(convertFromStringToName(name));
}
+ @Override
public NamingEnumeration<Binding> listBindings(Name name)
throws NamingException {
- Name targetDN = name.addAll(rdn);
+ int size = name.size();
+
+ Hashtable<String, Object> tempSchema = doLookup(name, size);
LdapNamingEnumeration<Binding> enumeration = new LdapNamingEnumeration<Binding>(
null, null);
- Set<String> keyset = null;
- int size = targetDN.size();
- switch (size) {
- case 0:
- keyset = LdapContextImpl.schemaTree.keySet();
- for (Iterator<String> i = keyset.iterator(); i.hasNext();) {
- String schemaType = i.next();
- Binding binding = new Binding(schemaLdap2Jndi.get(schemaType
- .toLowerCase()), this.getClass().getName(), null);
- enumeration.add(binding);
- }
- break;
- case 1:
- String schemaType = schemaJndi2Ldap.get(name.get(0).toLowerCase());
- if (null == schemaType) {
- throw new NameNotFoundException(name.toString());
- }
- Hashtable<String, Hashtable<String, Object>> schemas = LdapContextImpl.schemaTree
- .get(schemaType);
- keyset = schemas.keySet();
- for (Iterator<String> i = keyset.iterator(); i.hasNext();) {
- schemaType = i.next();
- Binding binding = new Binding(schemaType.toLowerCase(), this
- .getClass().getName(), null);
- enumeration.add(binding);
- }
- break;
- default:
- schemaType = schemaJndi2Ldap.get(name.getPrefix(1).toString()
- .toLowerCase());
- if (null == schemaType) {
- throw new NameNotFoundException(name.toString());
- }
- list(name.getSuffix(1));
+
+ if (size == level - 1) {
+ return enumeration;
}
+
+ Iterator<String> keys = tempSchema.keySet().iterator();
+
+ while (keys.hasNext()) {
+ enumeration.add(new Binding(ldap2jndi(keys.next()), this.getClass()
+ .getName()));
+ }
+
return enumeration;
}
+ @Override
public NamingEnumeration<Binding> listBindings(String name)
throws NamingException {
return listBindings(convertFromStringToName(name));
}
- public Object lookup(Name n) throws NamingException {
- return new LdapSchemaContextImpl(parent, env, n.addAll(rdn));
+ private Hashtable<Name, LdapSchemaContextImpl> cachedSubSchemas = new Hashtable<Name, LdapSchemaContextImpl>();
+
+ @Override
+ public Object lookup(Name name) throws NamingException {
+ // If cached, directly return cached one.
+ Name targetDN = name;
+ LdapSchemaContextImpl cachedSchema = cachedSubSchemas.get(targetDN);
+ if (cachedSchema != null) {
+ return cachedSchema;
+ }
+ int size = targetDN.size();
+ if (size == 0) {
+ return this;
+ }
+
+ Hashtable<String, Object> newSchemaTable = doLookup(name, size);
+
+ cachedSchema = new LdapSchemaContextImpl(parent, env, targetDN,
+ newSchemaTable, level - size);
+ cachedSubSchemas.put(targetDN, cachedSchema);
+
+ return cachedSchema;
+ }
+
+ // Find the subtree of schematree corresponding to the name.
+ private Hashtable<String, Object> doLookup(Name name, int size)
+ throws NamingException {
+ Name targetDN = name;
+ if (size >= level) {
+ throw new NameNotFoundException(name.toString());
+ }
+
+ Hashtable<String, Object> tempSchema = schemaTable;
+ Object tempValue;
+ for (int i = 0; i < size; i++) {
+ String key = targetDN.get(i);
+ tempValue = tempSchema.get(jndi2ldap(key));
+ if (tempValue == null) {
+ throw new NameNotFoundException(name.toString());
+ }
+
+ if (tempValue instanceof String) {
+ Hashtable<String, Object> attributesTable = SchemaParser
+ .parseValue(tempValue.toString());
+ tempSchema.put(jndi2ldap(key).toLowerCase(), attributesTable);
+ tempSchema = attributesTable;
+ } else {
+ tempSchema = (Hashtable<String, Object>) tempValue;
+ }
+ }
+
+ return tempSchema;
}
public Object lookup(String name) throws NamingException {
@@ -611,7 +687,8 @@
* encountered attribute value,
*/
if (attribute.contains(schemaType)) {
- BasicAttributes basicAttributes = new BasicAttributes(true);
+ BasicAttributes basicAttributes = new BasicAttributes(
+ true);
/*
* if(objectclassIndex == -1), then No name was choose,
* which means SearchResult will have empty
@@ -729,11 +806,11 @@
private boolean match(Attribute filter, Object values)
throws NamingException {
NamingEnumeration<?> attrValues = filter.getAll();
- ArrayList v = null;
+ ArrayList<Object> v = null;
if (values instanceof ArrayList) {
- v = (ArrayList) values;
+ v = (ArrayList<Object>) values;
} else {
- v = new ArrayList();
+ v = new ArrayList<Object>();
v.add(values);
}
@@ -793,8 +870,8 @@
BasicAttributes matchingAttrs) {
if (!f.isLeaf()) {
List<Filter> children = f.getChildren();
- for (Iterator iter = children.iterator(); iter.hasNext();) {
- extractMatchingAttributes((Filter) iter.next(), matchingAttrs);
+ for (Iterator<Filter> iter = children.iterator(); iter.hasNext();) {
+ extractMatchingAttributes(iter.next(), matchingAttrs);
}
} else {
Object value = f.getValue();
Added: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/parser/SchemaParser.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/parser/SchemaParser.java?rev=650435&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/parser/SchemaParser.java (added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/parser/SchemaParser.java Tue Apr 22 03:11:49 2008
@@ -0,0 +1,168 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.harmony.jndi.provider.ldap.parser;
+
+import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.StringTokenizer;
+
+public class SchemaParser {
+
+ public final static String SPACE = " "; //$NON-NLS-1$
+
+ public final static String LEFT_PARENTHESIS = "("; //$NON-NLS-1$
+
+ public final static String RIGHT_PARENTHESIS = ")"; //$NON-NLS-1$
+
+ public final static String SINGLE_QUOTE = "'"; //$NON-NLS-1$
+
+ public final static String NAME = "name"; //$NON-NLS-1$
+
+ public final static String ORIG = "orig"; //$NON-NLS-1$
+
+ public final static String NUMERICOID = "numericoid"; //$NON-NLS-1$
+
+ public final static String TRUE = "true"; //$NON-NLS-1$
+
+ public static String getName(String schemaLine) {
+ StringTokenizer st = new StringTokenizer(schemaLine);
+ st.nextToken();
+
+ String name = st.nextToken();
+ if (st.hasMoreTokens()) {
+ if (st.nextToken().equalsIgnoreCase(NAME)) {
+ name = st.nextToken();
+
+ if (name.equals(LEFT_PARENTHESIS)) {
+ name = st.nextToken();
+ }
+ name = name.substring(1, name.length() - 1);
+ }
+ }
+
+ return name;
+ }
+
+ public static Hashtable<String, Object> parseValue(String schemaLine) {
+ StringTokenizer st = new StringTokenizer(schemaLine.toLowerCase());
+ // Skip (
+ st.nextToken();
+
+ String oid = st.nextToken();
+
+ Hashtable<String, Object> schemaDef = new Hashtable<String, Object>();
+ schemaDef.put(ORIG, schemaLine);
+ schemaDef.put(NUMERICOID, oid);
+ String token = null;
+ ArrayList<String> values = null;
+ StringBuilder desc = new StringBuilder();
+ while (st.hasMoreTokens()) {
+ String attrName = st.nextToken().toLowerCase();
+ if (attrName.startsWith("x-")) { //$NON-NLS-1$
+ token = st.nextToken();
+ // remove the ending ' symbol
+ token = token.substring(1, token.length() - 1);
+ schemaDef.put(attrName, token);
+ } else if (attrName.equals("usage") || attrName.equals("equality") //$NON-NLS-1$//$NON-NLS-2$
+ || attrName.equals("syntax") || attrName.equals("ordering") //$NON-NLS-1$ //$NON-NLS-2$
+ || attrName.equals("substr")) { //$NON-NLS-1$
+ token = st.nextToken();
+ schemaDef.put(attrName, token);
+ } else if (attrName.equals("desc")) { //$NON-NLS-1$
+ token = st.nextToken();
+
+ // remove the leading ' symbol
+ if (token.startsWith(SINGLE_QUOTE)) {
+ token = token.substring(1);
+ }
+ while (token != null && !token.endsWith(SINGLE_QUOTE)) {
+ desc.append(token).append(SPACE);
+ token = st.nextToken();
+ }
+
+ if (token != null) {
+ // remove the ending ' symbol
+ desc.append(token.substring(0, token.length() - 1));
+ schemaDef.put(attrName, desc.toString());
+ desc.delete(0, desc.length());
+ }
+ } else if (attrName.equals(NAME)) {
+ token = st.nextToken();
+ values = new ArrayList<String>();
+ // Name has multiple values
+ if (token.startsWith(LEFT_PARENTHESIS)) {
+ token = st.nextToken();
+ while (!token.equals(RIGHT_PARENTHESIS)) {
+ // remove the leading ' symbol
+ if (token.startsWith(SINGLE_QUOTE)) {
+ token = token.substring(1);
+ }
+ while (!token.endsWith(SINGLE_QUOTE)) {
+ desc.append(token).append(SPACE);
+ token = st.nextToken();
+ }
+
+ // remove the ending ' symbol
+ desc.append(token.substring(0, token.length() - 1));
+ values.add(desc.toString());
+ desc.delete(0, desc.length());
+
+ token = st.nextToken();
+ }
+ } else {
+ // remove the leading ' symbol
+ if (token.startsWith(SINGLE_QUOTE)) {
+ token = token.substring(1);
+ }
+ while (token != null && !token.endsWith(SINGLE_QUOTE)) {
+ desc.append(token).append(SPACE);
+ token = st.nextToken();
+ }
+
+ if (token != null) {// remove the ending ' symbol
+ desc.append(token.substring(0, token.length() - 1));
+ values.add(desc.toString());
+ desc.delete(0, desc.length());
+ }
+ }
+ schemaDef.put(attrName, values);
+ } else if (attrName.equals("must") || attrName.equals("sup") //$NON-NLS-1$ //$NON-NLS-2$
+ || attrName.equals("may")) { //$NON-NLS-1$
+ token = st.nextToken();
+ values = new ArrayList<String>();
+ // has multiple values
+ if (token.startsWith(LEFT_PARENTHESIS)) {
+ token = st.nextToken();
+ while (!token.equals(RIGHT_PARENTHESIS)) {
+ if (!token.equals("$")) //$NON-NLS-1$
+ values.add(token);
+ token = st.nextToken();
+ }
+ } else {
+ values.add(token);
+ }
+ schemaDef.put(attrName, values);
+ } else if (attrName.equals("abstract") || attrName.equals("structural") //$NON-NLS-1$ //$NON-NLS-2$
+ || attrName.equals("auxiliary") //$NON-NLS-1$
+ || attrName.equals("single-value") //$NON-NLS-1$
+ || attrName.equals("no-user-modification")) { //$NON-NLS-1$
+ schemaDef.put(attrName, TRUE);
+ }
+ }
+ return schemaDef;
+ }
+}
Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/parser/SchemaParser.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextTest.java?rev=650435&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextTest.java (added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextTest.java Tue Apr 22 03:11:49 2008
@@ -0,0 +1,722 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.harmony.jndi.provider.ldap;
+
+import java.util.ArrayList;
+import java.util.Hashtable;
+
+import javax.naming.Binding;
+import javax.naming.CompositeName;
+import javax.naming.Name;
+import javax.naming.NameClassPair;
+import javax.naming.NameNotFoundException;
+import javax.naming.NamingEnumeration;
+import javax.naming.OperationNotSupportedException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.DirContext;
+
+import junit.framework.TestCase;
+
+public class LdapSchemaContextTest extends TestCase {
+ private LdapSchemaContextImpl schema;
+
+ @Override
+ public void setUp() throws Exception {
+ // Construct the schema table.
+ Hashtable<String, Object> schemaTable = new Hashtable<String, Object>();
+
+ Hashtable<String, Object> subSchema = new Hashtable<String, Object>();
+ subSchema
+ .put(
+ "javaclass",
+ "( 1.3.6.1.4.1.18060.0.4.1.3.8 name 'javaclass' sup top "
+ + "structural must ( fullyqualifiedjavaclassname $ javaclassbytecode ) x-schema 'apache' )");
+
+ subSchema
+ .put(
+ "extensibleobject",
+ "( 1.3.6.1.4.1.1466.101.120.111 name 'extensibleobject' "
+ + "desc 'rfc2252: extensible object' sup top auxiliary x-schema 'system' )");
+ subSchema
+ .put(
+ "prefnode",
+ "( 1.3.6.1.4.1.18060.0.4.1.3.1 name "
+ + "'prefnode' sup top structural must prefnodename x-schema 'apache' )");
+ schemaTable.put("objectclasses", subSchema);
+
+ subSchema = new Hashtable<String, Object>();
+ subSchema
+ .put(
+ "integerorderingmatch",
+ "( 2.5.13.15 name 'integerorderingmatch' syntax 1.3.6.1.4.1.1466.115.121.1.27 x-schema 'system' )");
+
+ subSchema
+ .put(
+ "caseexactmatch",
+ "( 2.5.13.5 name 'caseexactmatch' syntax 1.3.6.1.4.1.1466.115.121.1.15 x-schema 'system' )");
+ schemaTable.put("matchingrules", subSchema);
+
+ subSchema = new Hashtable<String, Object>();
+ subSchema
+ .put(
+ "1.3.6.1.4.1.1466.115.121.1.19",
+ "( 1.3.6.1.4.1.1466.115.121.1.19 x-schema 'system' x-is-human-readable 'true' )");
+ schemaTable.put("ldapsyntaxes", subSchema);
+
+ subSchema = new Hashtable<String, Object>();
+ subSchema
+ .put(
+ "dsaquality",
+ "( 0.9.2342.19200300.100.1.49 name 'dsaquality' desc 'rfc1274: dsa quality' syntax 1.3.6.1.4.1.1466.115.121.1.19 single-value usage userapplications x-schema 'cosine' )");
+ schemaTable.put("attributetypes", subSchema);
+
+ LdapContextImpl context = new LdapContextImpl(new MockLdapClient(),
+ null, "");
+ Name name = new CompositeName("");
+ schema = new LdapSchemaContextImpl(context, null, name, schemaTable,
+ LdapSchemaContextImpl.SCHEMA_ROOT_LEVEL);
+ }
+
+ public void testList_NullPara() throws Exception {
+ String nullString = null;
+ Name nullName = null;
+ try {
+ schema.list(nullString);
+ fail("Should throw NullPointerException.");
+ } catch (NullPointerException e) {
+ // Expected.
+ }
+
+ try {
+ schema.list(nullName);
+ fail("Should throw NullPointerException.");
+ } catch (NullPointerException e) {
+ // Expected.
+ }
+ }
+
+ public void testList() throws Exception {
+ // "" as parameter.
+ NamingEnumeration<NameClassPair> namingEnum = schema.list("");
+ NameClassPair pair;
+ ArrayList<String> verifyList = new ArrayList<String>();
+ verifyList.add("classdefinition");
+ verifyList.add("attributedefinition");
+ verifyList.add("matchingrule");
+ verifyList.add("syntaxdefinition");
+ while (namingEnum.hasMore()) {
+ pair = namingEnum.next();
+ assertTrue(verifyList.remove(pair.getName().toLowerCase()));
+ }
+ assertEquals(0, verifyList.size());
+
+ // "matchingrule" as parameter.
+ namingEnum = schema.list("matchingrule");
+ int count = 0;
+ while (namingEnum.hasMore()) {
+ pair = namingEnum.next();
+ count++;
+ }
+ assertEquals(2, count);
+
+ // "syntaxdefinition" as parameter.
+ namingEnum = schema.list("syntaxdefinition");
+ count = 0;
+ while (namingEnum.hasMore()) {
+ pair = namingEnum.next();
+ count++;
+ }
+ assertEquals(1, count);
+
+ // "classdefinition" as parameter.
+ namingEnum = schema.list("classdefinition");
+ count = 0;
+ while (namingEnum.hasMore()) {
+ pair = namingEnum.next();
+ count++;
+ }
+ assertEquals(3, count);
+
+ // "attributedefinition" as parameter.
+ namingEnum = schema.list("attributedefinition");
+ count = 0;
+ while (namingEnum.hasMore()) {
+ pair = namingEnum.next();
+ count++;
+ }
+ assertEquals(1, count);
+
+ // "classdefinition/javaClass" as parameter.
+ namingEnum = schema.list("classdefinition/javaClass");
+ assertFalse(namingEnum.hasMore());
+ }
+
+ public void testList_Exception() throws Exception {
+ NamingEnumeration<NameClassPair> namingEnum;
+ try {
+ namingEnum = schema.list("invalid");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ namingEnum = schema.list("invalid/invalid/invalid");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ namingEnum = schema.list("invalid/invalid");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ namingEnum = schema.list("classdefinition/invalid");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ namingEnum = schema.list("classdefinition/javaClass/name");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ namingEnum = schema.list("classdefinition/javaClass/invalid");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+ }
+
+ public void testList_Address() throws Exception {
+ NamingEnumeration<NameClassPair> namingEnum;
+ try {
+ namingEnum = schema.list("ldap://localhost:10389/");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ namingEnum = schema
+ .list("ldap://localhost:10389/dc=example,dc=com");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+ }
+
+ public void testGetSchema() throws Exception {
+ try {
+ schema.getSchema("");
+ fail("Should throw OperationNotSupportedException.");
+ } catch (OperationNotSupportedException e) {
+ // Expected.
+ }
+
+ try {
+ schema.getSchema(new CompositeName());
+ fail("Should throw OperationNotSupportedException.");
+ } catch (OperationNotSupportedException e) {
+ // Expected.
+ }
+
+ try {
+ schema.getSchema(new CompositeName("invalid"));
+ fail("Should throw OperationNotSupportedException.");
+ } catch (OperationNotSupportedException e) {
+ // Expected.
+ }
+
+ try {
+ String nullString = null;
+ schema.getSchema(nullString);
+ fail("Should throw OperationNotSupportedException.");
+ } catch (OperationNotSupportedException e) {
+ // Expected.
+ }
+
+ try {
+ Name nullName = null;
+ schema.getSchema(nullName);
+ fail("Should throw NullPointerException.");
+ } catch (OperationNotSupportedException e) {
+ // Expected.
+ }
+
+ try {
+ schema.getSchema(new CompositeName("ldap://invalid"));
+ fail("Should throw OperationNotSupportedException.");
+ } catch (OperationNotSupportedException e) {
+ // Expected.
+ }
+ }
+
+ public void testGetSchemaClassDefinition() throws Exception {
+ try {
+ schema.getSchemaClassDefinition("");
+ fail("Should throw OperationNotSupportedException.");
+ } catch (OperationNotSupportedException e) {
+ // Expected.
+ }
+
+ try {
+ schema.getSchemaClassDefinition(new CompositeName());
+ fail("Should throw OperationNotSupportedException.");
+ } catch (OperationNotSupportedException e) {
+ // Expected.
+ }
+
+ try {
+ schema.getSchemaClassDefinition(new CompositeName("invalid"));
+ fail("Should throw OperationNotSupportedException.");
+ } catch (OperationNotSupportedException e) {
+ // Expected.
+ }
+
+ try {
+ String nullString = null;
+ schema.getSchemaClassDefinition(nullString);
+ fail("Should throw OperationNotSupportedException.");
+ } catch (OperationNotSupportedException e) {
+ // Expected.
+ }
+
+ try {
+ Name nullName = null;
+ schema.getSchemaClassDefinition(nullName);
+ fail("Should throw NullPointerException.");
+ } catch (OperationNotSupportedException e) {
+ // Expected.
+ }
+
+ try {
+ schema
+ .getSchemaClassDefinition(new CompositeName(
+ "ldap://invalid"));
+ fail("Should throw OperationNotSupportedException.");
+ } catch (OperationNotSupportedException e) {
+ // Expected.
+ }
+ }
+
+ public void testGetAttributes() throws Exception {
+ // "" as parameter.
+ Attributes attrs = schema.getAttributes("");
+ NamingEnumeration<? extends Attribute> namingEnum = attrs.getAll();
+ assertFalse(namingEnum.hasMore());
+
+ attrs = schema.getAttributes("classdefinition/javaclass");
+
+ Attributes attrs2 = schema.getAttributes("classdefinition/javaclass");
+ assertNotSame(attrs, attrs2);
+ assertEquals(attrs.toString(), attrs2.toString());
+ namingEnum = attrs.getAll();
+
+ Attribute attr;
+ NamingEnumeration<?> attrEnum;
+ int count = 0;
+ while (namingEnum.hasMore()) {
+ attr = namingEnum.next();
+ attrEnum = attr.getAll();
+ while (attrEnum.hasMore()) {
+ Object o = attrEnum.next();
+ count++;
+ }
+
+ }
+ assertEquals(7, count);
+
+ // "matchingrule/integerorderingmatch" as parameter.
+ attrs = schema.getAttributes("matchingrule/integerorderingmatch");
+ namingEnum = attrs.getAll();
+
+ count = 0;
+ while (namingEnum.hasMore()) {
+ attr = namingEnum.next();
+ attrEnum = attr.getAll();
+ while (attrEnum.hasMore()) {
+ Object o = attrEnum.next();
+ count++;
+ }
+
+ }
+ assertEquals(4, count);
+
+ // "matchingrule" as parameter.
+ attrs = schema.getAttributes("matchingrule");
+ namingEnum = attrs.getAll();
+
+ count = 0;
+ while (namingEnum.hasMore()) {
+ attr = namingEnum.next();
+ attrEnum = attr.getAll();
+ while (attrEnum.hasMore()) {
+ Object o = attrEnum.next();
+ count++;
+ }
+
+ }
+ assertEquals(1, count);
+
+ // "syntaxdefinition/1.3.6.1.4.1.1466.115.121.1.19" as parameter.
+ attrs = schema
+ .getAttributes("syntaxdefinition/1.3.6.1.4.1.1466.115.121.1.19");
+ namingEnum = attrs.getAll();
+
+ count = 0;
+ while (namingEnum.hasMore()) {
+ attr = namingEnum.next();
+ attrEnum = attr.getAll();
+ while (attrEnum.hasMore()) {
+ Object o = attrEnum.next();
+ count++;
+ }
+
+ }
+ assertEquals(3, count);
+ }
+
+ public void testGetAttributes_Exception() throws Exception {
+ Attributes attrs;
+ Attribute attr;
+ NamingEnumeration<? extends Attribute> namingEnum;
+ NamingEnumeration<?> attrEnum;
+
+ try {
+ attrs = schema.getAttributes("classdefinition/rfc822localpart/may");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ // Invalid format.
+ try {
+ attrs = schema.getAttributes("classdefinition\rfc822localpart");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ attrs = schema.getAttributes("invalid");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ attrs = schema.getAttributes("invalid/invalid");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ attrs = schema.getAttributes("classdefinition/invalid");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ attrs = schema
+ .getAttributes("ldap://localhost:10389/dc=example,dc=com");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ attrs = schema.getAttributes("ldap://localhost:10389");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+ }
+
+ public void testGetAttributes_NullPara() throws Exception {
+ Attributes attrs;
+ String nullString = null;
+ Name nullName = null;
+
+ try {
+ attrs = schema.getAttributes(nullString);
+ fail("Should throw NullPointerException.");
+ } catch (NullPointerException e) {
+ // Expected.
+ }
+ try {
+ attrs = schema.getAttributes(nullName);
+ fail("Should throw NullPointerException.");
+ } catch (NullPointerException e) {
+ // Expected.
+ }
+ }
+
+ public void testListBindings() throws Exception {
+ // "" as parameter.
+ NamingEnumeration<Binding> bindings = schema.listBindings("");
+ Binding binding;
+ ArrayList<String> verifyList = new ArrayList<String>();
+ verifyList.add("classdefinition");
+ verifyList.add("attributedefinition");
+ verifyList.add("matchingrule");
+ verifyList.add("syntaxdefinition");
+ while (bindings.hasMore()) {
+ binding = bindings.next();
+ assertTrue(verifyList.remove(binding.getName().toLowerCase()));
+ }
+ assertEquals(0, verifyList.size());
+
+ // "matchingrule" as parameter.
+ bindings = schema.listBindings("matchingrule");
+ int count = 0;
+ while (bindings.hasMore()) {
+ binding = bindings.next();
+ count++;
+ }
+ assertEquals(2, count);
+
+ // "syntaxdefinition" as parameter.
+ bindings = schema.listBindings("syntaxdefinition");
+ count = 0;
+ while (bindings.hasMore()) {
+ binding = bindings.next();
+ count++;
+ }
+ assertEquals(1, count);
+
+ // "classdefinition" as parameter.
+ bindings = schema.listBindings("classdefinition");
+ count = 0;
+ while (bindings.hasMore()) {
+ binding = bindings.next();
+ count++;
+ }
+ assertEquals(3, count);
+
+ // "attributedefinition" as parameter.
+ bindings = schema.listBindings("attributedefinition");
+ count = 0;
+ while (bindings.hasMore()) {
+ binding = bindings.next();
+ count++;
+ }
+ assertEquals(1, count);
+
+ // "classdefinition/javaClass" as parameter.
+ bindings = schema.listBindings("classdefinition/javaClass");
+ assertFalse(bindings.hasMore());
+ }
+
+ public void testListBindings_Exception() throws Exception {
+ NamingEnumeration<Binding> bindings;
+ try {
+ bindings = schema.listBindings("invalid");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ bindings = schema.listBindings("invalid/invalid/invalid");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ bindings = schema.listBindings("invalid/invalid");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ bindings = schema.listBindings("classdefinition/invalid");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ bindings = schema.listBindings("classdefinition/javaClass/name");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ bindings = schema.listBindings("classdefinition/javaClass/invalid");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+ }
+
+ public void testListBindings_NullPara() throws Exception {
+ String nullString = null;
+ Name nullName = null;
+ try {
+ schema.listBindings(nullString);
+ fail("Should throw NullPointerException.");
+ } catch (NullPointerException e) {
+ // Expected.
+ }
+
+ try {
+ schema.listBindings(nullName);
+ fail("Should throw NullPointerException.");
+ } catch (NullPointerException e) {
+ // Expected.
+ }
+ }
+
+ public void testLookup() throws Exception {
+ DirContext subSchema = (DirContext) schema.lookup("");
+ assertSame(schema, subSchema);
+
+ subSchema = (DirContext) schema.lookup("classdefinition");
+ assertNotSame(schema, subSchema);
+ NamingEnumeration<NameClassPair> namingEnum = subSchema.list("");
+ NameClassPair pair;
+ int count = 0;
+ while (namingEnum.hasMore()) {
+ pair = namingEnum.next();
+ count++;
+ }
+ assertEquals(3, count);
+
+ assertSame(subSchema, schema.lookup("classdefinition"));
+
+ DirContext oldSchema = subSchema;
+ subSchema = (DirContext) schema.lookup("syntaxdefinition");
+ assertNotSame(oldSchema, subSchema);
+ namingEnum = subSchema.list("");
+ count = 0;
+ while (namingEnum.hasMore()) {
+ pair = namingEnum.next();
+ count++;
+ }
+ assertEquals(1, count);
+
+ subSchema = (DirContext) schema.lookup("classdefinition/jAvaClass");
+ namingEnum = subSchema.list("");
+ count = 0;
+ while (namingEnum.hasMore()) {
+ pair = namingEnum.next();
+ count++;
+ }
+ assertEquals(0, count);
+
+ }
+
+ public void testLookup_Exception() throws Exception {
+ try {
+ schema.lookup("invalid");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ schema.lookup("invalid/invalid/invalid");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ schema.lookup("invalid/invalid");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ schema.lookup("classdefinition/invalid");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ schema.lookup("classdefinition/javaClass/name");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+
+ try {
+ schema.lookup("classdefinition/javaClass/invalid");
+ fail("Should throw NameNotFoundException.");
+ } catch (NameNotFoundException e) {
+ // Expected.
+ }
+ }
+
+ public void testAttributes_SubSchema() throws Exception {
+ DirContext subSchema = (DirContext) schema
+ .lookup("classdefinition/javaClass");
+ Attributes attrs = subSchema.getAttributes("");
+ NamingEnumeration<? extends Attribute> namingEnum = attrs.getAll();
+
+ Attribute attr;
+ NamingEnumeration<?> attrEnum;
+ int count = 0;
+ while (namingEnum.hasMore()) {
+ attr = namingEnum.next();
+ attrEnum = attr.getAll();
+ while (attrEnum.hasMore()) {
+ Object o = attrEnum.next();
+ count++;
+ }
+
+ }
+ assertEquals(7, count);
+
+ subSchema = (DirContext) schema.lookup("classdefinition");
+ attrs = subSchema.getAttributes("");
+ namingEnum = attrs.getAll();
+
+ count = 0;
+ assertTrue(namingEnum.hasMore());
+ while (namingEnum.hasMore()) {
+ attr = namingEnum.next();
+ assertEquals("objectclass", attr.getID());
+ attrEnum = attr.getAll();
+ while (attrEnum.hasMore()) {
+ Object o = attrEnum.next();
+ assertEquals("classdefinition", o.toString().toLowerCase());
+ count++;
+ }
+
+ }
+ assertEquals(1, count);
+ }
+
+}
Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextTest.java
------------------------------------------------------------------------------
svn:eol-style = native