001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.jndi.provider.ldap;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.OutputStream;
025: import java.net.InetAddress;
026: import java.net.Socket;
027: import java.net.UnknownHostException;
028: import java.util.Hashtable;
029:
030: import javax.naming.ConfigurationException;
031: import javax.naming.Context;
032: import javax.net.SocketFactory;
033:
034: import junit.framework.TestCase;
035:
036: public class LdapContextFactoryTest extends TestCase {
037: private Hashtable<String, Object> env = new Hashtable<String, Object>();
038:
039: private LdapContextFactory factory = new LdapContextFactory();
040:
041: public void setUp() {
042: env.clear();
043: }
044:
045: public void test_getInitialContext() throws Exception {
046: env.put(Context.PROVIDER_URL, "ldap://192.168.1.1:389");
047:
048: env.put("java.naming.ldap.factory.socket",
049: MockSocketFactory.class.getName());
050: factory.getInitialContext(env);
051: assertEquals("192.168.1.1", MockSocketFactory.host);
052: assertEquals(389, MockSocketFactory.port);
053: env.clear();
054:
055: env.put(Context.PROVIDER_URL, "ldap:///");
056:
057: env.put("java.naming.ldap.factory.socket",
058: MockSocketFactory.class.getName());
059: factory.getInitialContext(env);
060: // default address
061: assertEquals("localhost", MockSocketFactory.host);
062: // default port
063: assertEquals(389, MockSocketFactory.port);
064: env.clear();
065:
066: env.put(Context.PROVIDER_URL, "ldap:///o=org?objectClass?one");
067: env.put("java.naming.ldap.factory.socket",
068: MockSocketFactory.class.getName());
069: try {
070: // only host, port and dn is allowed
071: factory.getInitialContext(env);
072: fail("Should throws ConfigurationException");
073: } catch (ConfigurationException e) {
074: // expected
075: }
076: env.clear();
077:
078: }
079:
080: public static class MockSocketFactory extends SocketFactory {
081:
082: static String host;
083:
084: static int port;
085:
086: private Socket socket = new MockSocket();
087:
088: @Override
089: public Socket createSocket(String host, int port)
090: throws IOException, UnknownHostException {
091: MockSocketFactory.host = host;
092: MockSocketFactory.port = port;
093: return socket;
094: }
095:
096: @Override
097: public Socket createSocket(InetAddress host, int port)
098: throws IOException {
099: this .host = host.getHostAddress();
100: this .port = port;
101: return socket;
102: }
103:
104: @Override
105: public Socket createSocket(String host, int port,
106: InetAddress localHost, int localPort)
107: throws IOException, UnknownHostException {
108: this .host = host;
109: this .port = port;
110: return socket;
111: }
112:
113: @Override
114: public Socket createSocket(InetAddress address, int port,
115: InetAddress localAddress, int localPort)
116: throws IOException {
117: this .host = address.getHostAddress();
118: this .port = port;
119: return socket;
120: }
121:
122: }
123:
124: public static class MockSocket extends Socket {
125:
126: public InputStream getInputStream() {
127: return new ByteArrayInputStream(new byte[0]);
128: }
129:
130: public OutputStream getOutputStream() {
131: return new ByteArrayOutputStream();
132: }
133: }
134: }
|