J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /** |
| 2 | * @test |
| 3 | * @bug 6176036 |
| 4 | * @summary Read-timeout specification for LDAP operations |
| 5 | */ |
| 6 | |
| 7 | import java.net.Socket; |
| 8 | import java.net.ServerSocket; |
| 9 | import java.io.*; |
| 10 | import javax.naming.*; |
| 11 | import javax.naming.directory.*; |
| 12 | import java.util.Hashtable; |
| 13 | |
| 14 | public class ReadTimeoutTest { |
| 15 | |
| 16 | public static void main(String[] args) throws Exception { |
| 17 | |
| 18 | boolean passed = false; |
| 19 | |
| 20 | // Set up the environment for creating the initial context |
| 21 | Hashtable env = new Hashtable(11); |
| 22 | env.put(Context.INITIAL_CONTEXT_FACTORY, |
| 23 | "com.sun.jndi.ldap.LdapCtxFactory"); |
| 24 | env.put("com.sun.jndi.ldap.read.timeout", "1000"); |
| 25 | env.put(Context.PROVIDER_URL, "ldap://localhost:2001"); |
| 26 | |
| 27 | Server s = new Server(); |
| 28 | |
| 29 | try { |
| 30 | |
| 31 | // start the server |
| 32 | s.start(); |
| 33 | |
| 34 | // Create initial context |
| 35 | DirContext ctx = new InitialDirContext(env); |
| 36 | System.out.println("LDAP Client: Connected to the Server"); |
| 37 | |
| 38 | SearchControls scl = new SearchControls(); |
| 39 | scl.setSearchScope(SearchControls.SUBTREE_SCOPE); |
| 40 | System.out.println("Performing Search"); |
| 41 | NamingEnumeration answer = |
| 42 | ctx.search("ou=People,o=JNDITutorial", "(objectClass=*)", scl); |
| 43 | |
| 44 | // Close the context when we're done |
| 45 | ctx.close(); |
| 46 | } catch (NamingException e) { |
| 47 | passed = true; |
| 48 | e.printStackTrace(); |
| 49 | } |
| 50 | s.interrupt(); |
| 51 | if (!passed) { |
| 52 | throw new Exception("Read timeout test failed," + |
| 53 | " read timeout exception not thrown"); |
| 54 | } |
| 55 | System.out.println("The test PASSED"); |
| 56 | } |
| 57 | |
| 58 | static class Server extends Thread { |
| 59 | |
| 60 | static int serverPort = 2001; |
| 61 | |
| 62 | Server() { |
| 63 | } |
| 64 | |
| 65 | public void run() { |
| 66 | try { |
| 67 | ServerSocket serverSock = new ServerSocket(serverPort); |
| 68 | Socket socket = serverSock.accept(); |
| 69 | System.out.println("Server: Connection accepted"); |
| 70 | |
| 71 | BufferedInputStream bin = new BufferedInputStream(socket. |
| 72 | getInputStream()); |
| 73 | while (true) { |
| 74 | bin.read(); |
| 75 | } |
| 76 | } catch (IOException e) { |
| 77 | // ignore |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |