harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lvj...@apache.org
Subject svn commit: r598603 - in /harmony/enhanced/classlib/trunk/modules/jndi/src: main/java/org/apache/harmony/jndi/provider/ldap/ test/java/org/apache/harmony/jndi/provider/ldap/
Date Tue, 27 Nov 2007 11:32:30 GMT
Author: lvjing
Date: Tue Nov 27 03:32:17 2007
New Revision: 598603

URL: http://svn.apache.org/viewvc?rev=598603&view=rev
Log:
Apply patch for HARMONY-5183 ([BTI][ldap] Add scenario tests for ldap schema retrieval methods,
such as search, lookup, list etc.)

Modified:
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/ExtendedOp.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImpl.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImplTest.java

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/ExtendedOp.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/ExtendedOp.java?rev=598603&r1=598602&r2=598603&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/ExtendedOp.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/ExtendedOp.java
Tue Nov 27 03:32:17 2007
@@ -34,6 +34,8 @@
     private ExtendedResponse response;
 
     private LdapResult result;
+    
+    private Object[] responseValues;
 
     public ExtendedOp(ExtendedRequest request) {
         this.request = request;
@@ -60,6 +62,26 @@
     }
 
     public ExtendedResponse getExtendedResponse() {
+
+        if (result != null && result.getResultCode() == 0
+                && responseValues != null) {
+            String id = null;
+            if (responseValues[4] != null) {
+                id = Utils.getString((byte[]) responseValues[4]);
+            }
+            byte[] value = (byte[]) responseValues[5];
+            int length = 0;
+            if (value != null) {
+                length = value.length;
+            }
+            
+            try {
+                response = request.createExtendedResponse(id, value, 0, length);
+            } catch (NamingException e) {
+                // FIXME: how to deal with this exception
+                e.printStackTrace();
+            }
+        }
         return response;
     }
 
@@ -74,16 +96,7 @@
 
     public void decodeValues(Object[] values) {
         result = new LdapResult();
-        result.decodeValues(values);
-        if (values[4] != null) {
-            String id = Utils.getString((byte[]) values[4]);
-            byte[] value = (byte[]) values[5];
-            try {
-                response = request.createExtendedResponse(id, value, 0,
-                        value.length);
-            } catch (NamingException e) {
-                // FIXME: how to deal with this exception
-            }
-        }
+        result.decodeValues(values);       
+        this.responseValues = values;
     }
 }

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=598603&r1=598602&r2=598603&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 Nov 27 03:32:17 2007
@@ -18,7 +18,6 @@
 
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -27,6 +26,7 @@
 import java.util.TreeSet;
 
 import javax.naming.Binding;
+import javax.naming.CompositeName;
 import javax.naming.ConfigurationException;
 import javax.naming.Context;
 import javax.naming.InvalidNameException;
@@ -93,10 +93,8 @@
 
     private Name rdn = null;
 
-    private BasicAttributes schemaAttributes;
-
-    public LdapSchemaContextImpl(LdapContextImpl ctx,
-            Hashtable<Object, Object> env, Name dn) throws InvalidNameException {
+    public LdapSchemaContextImpl(LdapContextImpl ctx, Hashtable<Object, Object> env,
+            Name dn) throws InvalidNameException {
         super(ctx, env, dn.getPrefix(0).toString());
         parent = ctx;
         rdn = dn;
@@ -134,8 +132,11 @@
             throw new SchemaViolationException(Messages.getString("jndi.8D"));
         }
 
-        if (!LdapContextImpl.schemaTree.keySet().contains(
-                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)) {
             throw new SchemaViolationException(Messages.getString("jndi.8E"));
         }
         String targetDN = rdn.toString() + parent.subschemasubentry;
@@ -166,8 +167,7 @@
 
         ModifyOp op = new ModifyOp(targetDN);
         BasicAttribute schemaEntry = new LdapAttribute(new BasicAttribute(
-                schemaJndi2Ldap.get(schemaType.toString().toLowerCase()),
-                builder.toString()), parent);
+                schema, builder.toString()), parent);
         op.addModification(jndi2ldap[DirContext.ADD_ATTRIBUTE],
                 new LdapAttribute(schemaEntry, parent));
 
@@ -185,32 +185,40 @@
     }
 
     public Attributes getAttributes(Name name) throws NamingException {
-        if (null != schemaAttributes) {
-            return schemaAttributes;
-        }
-        Name schemaName = name;
-        if (schemaName.size() == 0) {
-            schemaName = rdn;
-        }
-        if (schemaName.size() != 2) {
-            throw new NameNotFoundException(name.toString());
-        }
-        Hashtable<String, Object> schemaDef = parent.findSchemaDefInfo(
-                schemaJndi2Ldap.get(schemaName.get(0).toLowerCase()),
-                schemaName.get(1).toLowerCase());
-        if (null == schemaDef) {
-            throw new NameNotFoundException(name.toString());
-        }
-        schemaAttributes = new BasicAttributes();
-        Enumeration<String> keys = schemaDef.keys();
-        while (keys.hasMoreElements()) {
-            String id = keys.nextElement();
-            if (id.equals("orig")) {
-                continue;
-            }
-            Object value = schemaDef.get(id);
-            schemaAttributes.put(new LdapAttribute(
-                    new BasicAttribute(id, value), parent));
+        Name targetDN = (rdn.size() != 0) ? name.addAll(rdn) : name;
+
+        BasicAttributes schemaAttributes = new BasicAttributes();
+
+        Set<String> keyset = null;
+        int size = targetDN.size();
+        switch (size) {
+        case 0:
+            break;
+        case 1:
+            String schemaType = schemaJndi2Ldap.get(name.get(0).toLowerCase());
+            if (null == schemaType) {
+                throw new NameNotFoundException(name.toString());
+            }
+            schemaAttributes.put(new LdapAttribute(new BasicAttribute(
+                    "objectclass", name.get(0).toLowerCase()), parent));
+            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();
+            keyset = classDef.keySet();
+            for (Iterator<String> i = keyset.iterator(); i.hasNext();) {
+                String id = i.next();
+                if (id.equals("orig")) {
+                    continue;
+                }
+                Object value = classDef.get(id);
+                schemaAttributes.put(new LdapAttribute(new BasicAttribute(id
+                        .toLowerCase(), value), parent));
+            }
         }
         return schemaAttributes;
     }
@@ -231,13 +239,13 @@
 
     public Attributes getAttributes(String attributeName)
             throws NamingException {
-        Name name = convertFromStringToName(attributeName);
+        Name name = new CompositeName(attributeName);
         return getAttributes(name);
     }
 
     public Attributes getAttributes(String name, String[] as)
             throws NamingException {
-        return getAttributes(convertFromStringToName(name), as);
+        return getAttributes(new CompositeName(name), as);
     }
 
     private void checkName(Name name) {
@@ -282,20 +290,35 @@
             throws NamingException {
         checkName(name);
 
-        if (modificationItems == null) {
-            // FIXME: spec say ModificationItem may not be null, but ri
-            // silence in this case
-            throw new NullPointerException(Messages.getString("ldap.27")); //$NON-NLS-1$
-        }
+        Name targetDN = (rdn.size() != 0) ? name.addAll(rdn) : name;
+        int size = targetDN.size();
+        ModifyOp op = new ModifyOp(targetDN.toString());
+        String oldValue = "(objectclass)";
+        switch (size) {
+        case 0:
+            break;
+        case 1:
+            String schemaType = schemaJndi2Ldap.get(name.get(0).toLowerCase());
+            if (null == schemaType) {
+                throw new NameNotFoundException(name.toString());
+            }
+            if (modificationItems == null) {
+                throw new NullPointerException(Messages.getString("ldap.27")); //$NON-NLS-1$
+            }
+            break;
+        default:
+            Hashtable<String, Object> classDef = parent
+                    .findSchemaDefInfo(schemaJndi2Ldap.get(name.get(0)
+                            .toLowerCase()), name.get(1));
+            if (modificationItems == null) {
+                throw new NullPointerException(Messages.getString("ldap.27")); //$NON-NLS-1$
+            }
+            if (null == classDef) {
+                throw new NameNotFoundException(name.toString());
+            }
 
-        String targetDN = rdn.toString() + parent.subschemasubentry;
-        ModifyOp op = new ModifyOp(targetDN);
-        Hashtable<String, Object> classDef = parent.findSchemaDefInfo(
-                schemaJndi2Ldap.get(name.get(0).toLowerCase()), name.get(1));
-        if (null == classDef) {
-            throw new NameNotFoundException(name.toString());
+            oldValue = (String) classDef.get("orig");
         }
-        String oldValue = (String) classDef.get("orig");
         BasicAttribute oldAttr = new LdapAttribute(new BasicAttribute(
                 OBJECT_CLASSES, oldValue), parent);
         StringBuilder addValue = new StringBuilder();
@@ -304,9 +327,9 @@
             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],
@@ -374,15 +397,47 @@
         destroySubcontext(convertFromStringToName(name));
     }
 
-    public NamingEnumeration<NameClassPair> list(Name n) throws NamingException {
-        Set<String> keyset = LdapContextImpl.schemaTree.keySet();
+    public NamingEnumeration<NameClassPair> list(Name name)
+            throws NamingException {
+        Name targetDN = name.addAll(rdn);
+
         LdapNamingEnumeration<NameClassPair> enumeration = new LdapNamingEnumeration<NameClassPair>(
                 null, null);
-        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);
+        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));
         }
         return enumeration;
     }
@@ -407,24 +462,6 @@
     }
 
     public Object lookup(Name n) throws NamingException {
-        if (n.size() == 0) {
-            return this;
-        }
-        if (n.size() == 1) {
-            throw new NameNotFoundException(n.toString());
-        }
-        Hashtable<String, Object> schemaDef = parent.findSchemaDefInfo(
-        // mapping jndi schema type name to ldap native name
-                schemaJndi2Ldap.get(
-                // get [0, 1) as the jndi schema type name
-                        n.getPrefix(1).toString().toLowerCase()),
-                // schema name
-                n.getSuffix(1).toString().toLowerCase()); // find SchemaDef
-        // Info.
-
-        if (null == schemaDef) {
-            throw new NameNotFoundException(n.toString());
-        }
         return new LdapSchemaContextImpl(parent, env, n.addAll(rdn));
     }
 
@@ -438,6 +475,11 @@
 
     public void rename(String sOld, String sNew) throws NamingException {
         throw new SchemaViolationException(Messages.getString("jndi.err.01"));
+    }
+
+    public NamingEnumeration<SearchResult> search(Name name,
+            Attributes attributes) throws NamingException {
+        return search(name, attributes, null);
     }
 
     public NamingEnumeration<SearchResult> search(Name name,

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java?rev=598603&r1=598602&r2=598603&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextImplTest.java
Tue Nov 27 03:32:17 2007
@@ -70,6 +70,23 @@
             // expected
         }
     }
+    
+    public void test_getSchemaClassDefinition() throws NamingException {
+        context = new LdapContextImpl(new MockLdapClient(), null, "");
+        try {
+            context.getSchemaClassDefinition((Name) null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        
+        try {
+            context.getSchemaClassDefinition((String) null);
+            fail("Should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+    }
 
     public void test_composeName_LNameLName() throws Exception {
         context = new LdapContextImpl(new MockLdapClient(), null, "");

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImplTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImplTest.java?rev=598603&r1=598602&r2=598603&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImplTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapSchemaContextImplTest.java
Tue Nov 27 03:32:17 2007
@@ -21,6 +21,7 @@
 import javax.naming.CompositeName;
 import javax.naming.Name;
 import javax.naming.NamingException;
+import javax.naming.OperationNotSupportedException;
 import javax.naming.directory.BasicAttributes;
 import javax.naming.directory.ModificationItem;
 import javax.naming.directory.SchemaViolationException;
@@ -123,5 +124,14 @@
 			// expected
 		}
 	}
+
+    public void test_getNameInNamespace() throws NamingException {
+        try {
+            context.getNameInNamespace();
+            fail("Should throw OperationNotSupportedException");
+        } catch (OperationNotSupportedException e) {
+            // expected
+        }
+    }
 
 }



Mime
View raw message