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