< prev index next >

test/jdk/com/sun/jndi/ldap/NoWaitForReplyTest.java

Print this page


   1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  28  */
  29 
  30 import java.net.Socket;
  31 import java.net.ServerSocket;
  32 import java.io.*;
  33 import javax.naming.*;
  34 import javax.naming.directory.*;
  35 import java.util.Hashtable;
  36 
  37 public class NoWaitForReplyTest {
  38 
  39     public static void main(String[] args) throws Exception {
  40 
  41         boolean passed = false;
  42 
  43         // start the LDAP server
  44         DummyServer ldapServer = new DummyServer();
  45         ldapServer.start();
  46 
  47         // Set up the environment for creating the initial context
  48         Hashtable env = new Hashtable(11);
  49         env.put(Context.PROVIDER_URL, "ldap://localhost:" +
  50             ldapServer.getPortNumber());
  51         env.put(Context.INITIAL_CONTEXT_FACTORY,
  52             "com.sun.jndi.ldap.LdapCtxFactory");
  53 
  54         // Wait up to 10 seconds for a response from the LDAP server
  55         env.put("com.sun.jndi.ldap.read.timeout", "10000");
  56 
  57         // Don't wait until the first search reply is received
  58         env.put("com.sun.jndi.ldap.search.waitForReply", "false");
  59 
  60         // Send the LDAP search request without first authenticating (no bind)
  61         env.put("java.naming.ldap.version", "3");
  62 
  63 
  64         try {
  65 
  66             // Create initial context
  67             System.out.println("Client: connecting to the server");
  68             DirContext ctx = new InitialDirContext(env);
  69 
  70             SearchControls scl = new SearchControls();
  71             scl.setSearchScope(SearchControls.SUBTREE_SCOPE);
  72             System.out.println("Client: performing search");
  73             NamingEnumeration answer =
  74             ctx.search("ou=People,o=JNDITutorial", "(objectClass=*)", scl);
  75 
  76             // Server will never reply: either we waited in the call above until
  77             // the timeout (fail) or we did not wait and reached here (pass).
  78             passed = true;
  79             System.out.println("Client: did not wait until first reply");
  80 
  81             // Close the context when we're done
  82             ctx.close();
  83 
  84         } catch (NamingException e) {
  85             // timeout (ignore)
  86         }
  87         ldapServer.interrupt();
  88 
  89         if (!passed) {
  90             throw new Exception(
  91                 "Test FAILED: should not have waited until first search reply");
  92         }
  93         System.out.println("Test PASSED");


   1 /*
   2  * Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  28  */
  29 
  30 import java.net.Socket;
  31 import java.net.ServerSocket;
  32 import java.io.*;
  33 import javax.naming.*;
  34 import javax.naming.directory.*;
  35 import java.util.Hashtable;
  36 
  37 public class NoWaitForReplyTest {
  38 
  39     public static void main(String[] args) throws Exception {
  40 
  41         boolean passed = false;
  42 
  43         // start the LDAP server
  44         DummyServer ldapServer = new DummyServer();
  45         ldapServer.start();
  46 
  47         // Set up the environment for creating the initial context
  48         Hashtable<Object, Object> env = new Hashtable<>(11);
  49         env.put(Context.PROVIDER_URL, "ldap://localhost:" +
  50             ldapServer.getPortNumber());
  51         env.put(Context.INITIAL_CONTEXT_FACTORY,
  52             "com.sun.jndi.ldap.LdapCtxFactory");
  53 
  54         // Wait up to 10 seconds for a response from the LDAP server
  55         env.put("com.sun.jndi.ldap.read.timeout", "10000");
  56 
  57         // Don't wait until the first search reply is received
  58         env.put("com.sun.jndi.ldap.search.waitForReply", "false");
  59 
  60         // Send the LDAP search request without first authenticating (no bind)
  61         env.put("java.naming.ldap.version", "3");
  62 
  63 
  64         try {
  65 
  66             // Create initial context
  67             System.out.println("Client: connecting to the server");
  68             DirContext ctx = new InitialDirContext(env);
  69 
  70             SearchControls scl = new SearchControls();
  71             scl.setSearchScope(SearchControls.SUBTREE_SCOPE);
  72             System.out.println("Client: performing search");
  73             NamingEnumeration<?> answer =
  74             ctx.search("ou=People,o=JNDITutorial", "(objectClass=*)", scl);
  75 
  76             // Server will never reply: either we waited in the call above until
  77             // the timeout (fail) or we did not wait and reached here (pass).
  78             passed = true;
  79             System.out.println("Client: did not wait until first reply");
  80 
  81             // Close the context when we're done
  82             ctx.close();
  83 
  84         } catch (NamingException e) {
  85             // timeout (ignore)
  86         }
  87         ldapServer.interrupt();
  88 
  89         if (!passed) {
  90             throw new Exception(
  91                 "Test FAILED: should not have waited until first search reply");
  92         }
  93         System.out.println("Test PASSED");


< prev index next >