harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lvj...@apache.org
Subject svn commit: r599380 - in /harmony/enhanced/classlib/trunk/modules/jndi/src: main/java/org/apache/harmony/jndi/provider/ldap/ test/java/org/apache/harmony/jndi/provider/ldap/ test/java/org/apache/harmony/jndi/provider/ldap/mock/
Date Thu, 29 Nov 2007 11:11:32 GMT
Author: lvjing
Date: Thu Nov 29 03:11:31 2007
New Revision: 599380

URL: http://svn.apache.org/viewvc?rev=599380&view=rev
Log:
Apply patch for HARMONY-5216 ([classlib][jndi][ldap] implements LdapContext.newInstance, reconnect
and getConnectControls methods)

Added:
    harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.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/LdapMessage.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/mock/MockLdapServer.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=599380&r1=599379&r2=599380&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
Thu Nov 29 03:11:31 2007
@@ -142,6 +142,8 @@
 
     private static final String LDAP_DEREF_ALIASES = "java.naming.ldap.derefAliases"; //$NON-NLS-1$
 
+    private static final String LDAP_CONTROL_CONNECT = "java.naming.ldap.control.connect";
//$NON-NLS-1$
+
     private static final String LDAP_TYPES_ONLY = "java.naming.ldap.typesOnly"; //$NON-NLS-1$
 
     /**
@@ -158,7 +160,7 @@
         connectionProperties.add(Context.SECURITY_CREDENTIALS);
         connectionProperties.add(Context.SECURITY_PRINCIPAL);
         connectionProperties.add(Context.SECURITY_PROTOCOL);
-        connectionProperties.add("java.naming.ldap.factory.socket");
+        connectionProperties.add("java.naming.ldap.factory.socket"); //$NON-NLS-1$
     }
 
     /**
@@ -173,6 +175,10 @@
             Hashtable<Object, Object> environment, String dn)
             throws InvalidNameException {
         initial(context.client, environment, dn);
+
+        // connection request controls are inheritied
+        connCtls = context.connCtls;
+        // request controls are not inheritied
     }
 
     /**
@@ -210,6 +216,13 @@
 
         contextDn = new LdapName(dn);
         parser = new LdapNameParser(dn);
+
+        if (env.get(Context.REFERRAL) == null
+                || env.get(Context.REFERRAL).equals("ignore")) { //$NON-NLS-1$
+            requestControls = new Control[] { NON_CRITICAL_MANAGE_REF_CONTROL };
+        }
+
+        connCtls = (Control[]) env.get(LDAP_CONTROL_CONNECT);
     }
 
     /**
@@ -255,8 +268,7 @@
     }
 
     public Control[] getConnectControls() throws NamingException {
-        // TODO not yet implemented
-        throw new NotYetImplementedException();
+        return copyControls(connCtls);
     }
 
     public Control[] getRequestControls() throws NamingException {
@@ -277,14 +289,22 @@
         return rtValue;
     }
 
-    public LdapContext newInstance(Control[] ac) throws NamingException {
-        // TODO not yet implemented
-        throw new NotYetImplementedException();
+	public LdapContext newInstance(Control[] reqCtrls) throws NamingException {
+        LdapContextImpl instance = new LdapContextImpl(this, env, contextDn
+                .toString());
+		instance.setRequestControls(reqCtrls);
+        return instance;
     }
 
     public void reconnect(Control[] ac) throws NamingException {
-        // TODO not yet implemented
-        throw new NotYetImplementedException();
+        connCtls = ac;
+        try {
+            doBindOperation(connCtls);
+        } catch (IOException e) {
+            CommunicationException ex = new CommunicationException();
+            ex.setRootCause(e);
+            throw ex;
+        }
     }
 
     public void setRequestControls(Control[] controls) throws NamingException {

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapMessage.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapMessage.java?rev=599380&r1=599379&r2=599380&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapMessage.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/main/java/org/apache/harmony/jndi/provider/ldap/LdapMessage.java
Thu Nov 29 03:11:31 2007
@@ -20,6 +20,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 
 import javax.naming.ldap.Control;
@@ -178,6 +179,16 @@
             responseOp.decodeValues((Object[]) chosen.getValue());
         }
 
+        if (values[2] != null) {
+            Collection<Object[]> list = (Collection<Object[]>) values[2];
+            controls = new Control[list.size()];
+            int index = 0;
+            for (Object[] objects : list) {
+                LdapControl lcontrol = new LdapControl();
+                lcontrol.decodeValues(objects);
+                controls[index++] = lcontrol.getControl();
+            }
+        }
     }
 
     public void encodeValues(Object[] values) {

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=599380&r1=599379&r2=599380&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
Thu Nov 29 03:11:31 2007
@@ -45,6 +45,8 @@
 import javax.naming.ldap.BasicControl;
 import javax.naming.ldap.Control;
 import javax.naming.ldap.LdapName;
+import javax.naming.ldap.PagedResultsControl;
+import javax.naming.ldap.SortControl;
 
 import junit.framework.TestCase;
 
@@ -795,6 +797,37 @@
         context.removeNamingListener(listener);
     }
 
+    public void test_reconnect() throws Exception {
+        Hashtable<Object, Object> env = new Hashtable<Object, Object>();
+        env
+        .put("java.naming.ldap.control.connect",
+                new Control[] { new PagedResultsControl(10,
+                        Control.NONCRITICAL) });
+
+        MockLdapClient client = new MockLdapClient();
+        context = new LdapContextImpl(client, env, "cn=test");
+        
+        Control[] controls = context.getConnectControls();
+        assertNotNull(controls);
+        Control c = controls[0];
+        assertTrue(c instanceof PagedResultsControl);
+        assertEquals(Control.NONCRITICAL, ((PagedResultsControl) c).isCritical());
+
+        context.reconnect(new Control[] { new SortControl("",
+                Control.NONCRITICAL) });
+        
+        controls = context.getConnectControls();
+        assertNotNull(controls);
+        assertEquals(1, controls.length);
+        c = controls[0];
+        assertTrue(c instanceof SortControl);
+        assertEquals(Control.NONCRITICAL, ((SortControl) c).isCritical());
+        
+        context.reconnect(null);
+        
+        assertNull(context.getConnectControls());
+    }
+    
     public static class TestNamingListener implements NamespaceChangeListener {
 
         private NamingEvent event;

Added: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java?rev=599380&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java
Thu Nov 29 03:11:31 2007
@@ -0,0 +1,231 @@
+/* 
+ *  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.Hashtable;
+
+import javax.naming.Context;
+import javax.naming.directory.InitialDirContext;
+import javax.naming.ldap.Control;
+import javax.naming.ldap.InitialLdapContext;
+import javax.naming.ldap.LdapContext;
+import javax.naming.ldap.SortControl;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.jndi.provider.ldap.asn1.LdapASN1Constant;
+import org.apache.harmony.jndi.provider.ldap.mock.BindResponse;
+import org.apache.harmony.jndi.provider.ldap.mock.EncodableLdapResult;
+import org.apache.harmony.jndi.provider.ldap.mock.MockLdapServer;
+
+public class LdapContextServerMockedTest extends TestCase {
+    private MockLdapServer server;
+
+    private Hashtable<Object, Object> env = new Hashtable<Object, Object>();
+
+    @Override
+    public void setUp() {
+        server = new MockLdapServer();
+        server.start();
+        env.put(Context.INITIAL_CONTEXT_FACTORY,
+                "org.apache.harmony.jndi.provider.ldap.LdapContextFactory");
+        env.put(Context.PROVIDER_URL, server.getURL());
+    }
+
+    @Override
+    public void tearDown() {
+        server.stop();
+    }
+
+    public void testRequestControls() throws Exception {
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+
+        InitialLdapContext initialContext = new InitialLdapContext(env, null);
+
+        Control[] reqCtls = initialContext.getRequestControls();
+        assertEquals(1, reqCtls.length);
+        assertEquals("2.16.840.1.113730.3.4.2", reqCtls[0].getID());
+        assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());
+
+        initialContext.setRequestControls(new Control[] { new SortControl("",
+                Control.NONCRITICAL) });
+
+        reqCtls = initialContext.getRequestControls();
+        assertEquals(2, reqCtls.length);
+        Control control = reqCtls[0];
+        if (control instanceof SortControl) {
+            assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());
+            assertEquals("2.16.840.1.113730.3.4.2", reqCtls[1].getID());
+            assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
+        } else {
+            assertEquals("2.16.840.1.113730.3.4.2", control.getID());
+            assertEquals(Control.NONCRITICAL, control.isCritical());
+            assertTrue(reqCtls[1] instanceof SortControl);
+            assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
+        }
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_SEARCH_RESULT_DONE,
+                new EncodableLdapResult(), null) });
+
+        LdapContext context = (LdapContext) initialContext.lookup("");
+        // request controls are not inherited
+        reqCtls = context.getRequestControls();
+        assertEquals(1, reqCtls.length);
+        assertEquals("2.16.840.1.113730.3.4.2", reqCtls[0].getID());
+        assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+        context = context.newInstance(new Control[] { new SortControl("",
+                Control.NONCRITICAL) });
+        reqCtls = context.getRequestControls();
+
+        assertEquals(2, reqCtls.length);
+        control = reqCtls[0];
+        if (control instanceof SortControl) {
+            assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());
+            assertEquals("2.16.840.1.113730.3.4.2", reqCtls[1].getID());
+            assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
+        } else {
+            assertEquals("2.16.840.1.113730.3.4.2", control.getID());
+            assertEquals(Control.NONCRITICAL, control.isCritical());
+            assertTrue(reqCtls[1] instanceof SortControl);
+            assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
+        }
+    }
+
+    public void testConnectControls() throws Exception {
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+
+        InitialDirContext initialDirContext = new InitialDirContext(env);
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_SEARCH_RESULT_DONE,
+                new EncodableLdapResult(), null) });
+        LdapContext context = (LdapContext) initialDirContext.lookup("");
+
+        assertNull(context.getConnectControls());
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+        context.reconnect(new Control[] { new SortControl("",
+                Control.NONCRITICAL) });
+
+        Control[] controls = context.getConnectControls();
+        assertNotNull(controls);
+        assertEquals(1, controls.length);
+        Control c = controls[0];
+        assertTrue(c instanceof SortControl);
+        assertEquals(Control.NONCRITICAL, c.isCritical());
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_SEARCH_RESULT_DONE,
+                new EncodableLdapResult(), null) });
+        context = (LdapContext) context.lookup("");
+
+        // connect controls are inherited
+        controls = context.getConnectControls();
+        assertNotNull(controls);
+        assertEquals(1, controls.length);
+        c = controls[0];
+        assertTrue(c instanceof SortControl);
+        assertEquals(Control.NONCRITICAL, c.isCritical());
+
+    }
+
+    public void testConnectControls2() throws Exception {
+        // set connect controls by property "java.naming.ldap.control.connect"
+        env.put("java.naming.ldap.control.connect",
+                new Control[] { new SortControl("", Control.NONCRITICAL) });
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+
+        InitialDirContext initialDirContext = new InitialDirContext(env);
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_SEARCH_RESULT_DONE,
+                new EncodableLdapResult(), null) });
+        LdapContext context = (LdapContext) initialDirContext.lookup("");
+
+        Control[] controls = context.getConnectControls();
+        assertNotNull(controls);
+        assertEquals(1, controls.length);
+        Control c = controls[0];
+        assertTrue(c instanceof SortControl);
+        assertEquals(Control.NONCRITICAL, c.isCritical());
+    }
+
+    public void testConnectControls3() throws Exception {
+        // set connect controls by InitialLdapContext
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+        InitialLdapContext initialDirContext = new InitialLdapContext(env,
+                new Control[] { new SortControl("", Control.NONCRITICAL) });
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_SEARCH_RESULT_DONE,
+                new EncodableLdapResult(), null) });
+        LdapContext context = (LdapContext) initialDirContext.lookup("");
+
+        Control[] controls = context.getConnectControls();
+        assertNotNull(controls);
+        assertEquals(1, controls.length);
+        Control c = controls[0];
+        assertTrue(c instanceof SortControl);
+        assertEquals(Control.NONCRITICAL, c.isCritical());
+
+    }
+
+    public void testnewInstance() throws Exception {
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+        InitialLdapContext initialDirContext = new InitialLdapContext(env, null);
+        Control[] reqCtls = initialDirContext.getRequestControls();
+        assertEquals(1, reqCtls.length);
+        assertEquals("2.16.840.1.113730.3.4.2", reqCtls[0].getID());
+        assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());
+
+        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
+                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
+        LdapContext context = initialDirContext
+                .newInstance(new Control[] { new SortControl("",
+                        Control.NONCRITICAL) });
+
+        assertNotSame(initialDirContext, context);
+        reqCtls = context.getRequestControls();
+        assertEquals(2, reqCtls.length);
+        Control control = reqCtls[0];
+        if (control instanceof SortControl) {
+            assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());
+            assertEquals("2.16.840.1.113730.3.4.2", reqCtls[1].getID());
+            assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
+        } else {
+            assertEquals("2.16.840.1.113730.3.4.2", control.getID());
+            assertEquals(Control.NONCRITICAL, control.isCritical());
+            assertTrue(reqCtls[1] instanceof SortControl);
+            assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
+        }
+
+    }
+}

Propchange: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/LdapContextServerMockedTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapServer.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapServer.java?rev=599380&r1=599379&r2=599380&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapServer.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/jndi/src/test/java/org/apache/harmony/jndi/provider/ldap/mock/MockLdapServer.java
Thu Nov 29 03:11:31 2007
@@ -129,6 +129,11 @@
                             }
                         };
                         request.decode(in);
+                        try {
+                            Thread.sleep(10);
+                        } catch (InterruptedException e) {
+                            //ignore
+                        }
                         out.write(response.encode());
                     }
                     responses = null;



Mime
View raw message